@@ -1130,3 +1130,86 @@ function build_explicit_observed_function(sys, ts;
11301130 return f
11311131 end
11321132end
1133+
1134+ """
1135+ $(TYPEDSIGNATURES)
1136+
1137+ Return matrix `A` and vector `b` such that the system `sys` can be represented as
1138+ `A * x = b` where `x` is `unknowns(sys)`. Errors if the system is not affine.
1139+
1140+ # Keyword arguments
1141+
1142+ - `sparse`: return a sparse `A`.
1143+ """
1144+ function calculate_A_b (sys:: System ; sparse = false )
1145+ rhss = [eq. rhs for eq in full_equations (sys)]
1146+ dvs = unknowns (sys)
1147+
1148+ A = Matrix {Any} (undef, length (rhss), length (dvs))
1149+ b = Vector {Any} (undef, length (rhss))
1150+ for (i, rhs) in enumerate (rhss)
1151+ resid = rhs
1152+ for (j, var) in enumerate (dvs)
1153+ p, q, islinear = Symbolics. linear_expansion (resid, var)
1154+ if ! islinear
1155+ throw (ArgumentError (" System is not linear. Equation $((0 ~ rhs)) is not linear in unknown $var ." ))
1156+ end
1157+ A[i, j] = p
1158+ resid = q
1159+ end
1160+ # negate beucause `resid` is the residual on the LHS
1161+ b[i] = - resid
1162+ end
1163+
1164+ @assert all (Base. Fix1 (isassigned, A), eachindex (A))
1165+ @assert all (Base. Fix1 (isassigned, A), eachindex (b))
1166+
1167+ if sparse
1168+ A = SparseArrays. sparse (A)
1169+ end
1170+ return A, b
1171+ end
1172+
1173+ """
1174+ $(TYPEDSIGNATURES)
1175+
1176+ Given a system `sys` and the `A` from [`calculate_A_b`](@ref) generate the function that
1177+ updates `A` given the parameter object.
1178+
1179+ # Keyword arguments
1180+
1181+ $GENERATE_X_KWARGS
1182+
1183+ All other keyword arguments are forwarded to [`build_function_wrapper`](@ref).
1184+ """
1185+ function generate_update_A (sys:: System , A:: AbstractMatrix ; expression = Val{true },
1186+ wrap_gfw = Val{false }, eval_expression = false , eval_module = @__MODULE__ , kwargs... )
1187+ ps = reorder_parameters (sys)
1188+
1189+ res = build_function_wrapper (sys, A, ps... ; p_start = 1 , expression = Val{true },
1190+ similarto = typeof (A), kwargs... )
1191+ return maybe_compile_function (expression, wrap_gfw, (1 , 1 , is_split (sys)), res;
1192+ eval_expression, eval_module)
1193+ end
1194+
1195+ """
1196+ $(TYPEDSIGNATURES)
1197+
1198+ Given a system `sys` and the `b` from [`calculate_A_b`](@ref) generate the function that
1199+ updates `b` given the parameter object.
1200+
1201+ # Keyword arguments
1202+
1203+ $GENERATE_X_KWARGS
1204+
1205+ All other keyword arguments are forwarded to [`build_function_wrapper`](@ref).
1206+ """
1207+ function generate_update_b (sys:: System , b:: AbstractVector ; expression = Val{true },
1208+ wrap_gfw = Val{false }, eval_expression = false , eval_module = @__MODULE__ , kwargs... )
1209+ ps = reorder_parameters (sys)
1210+
1211+ res = build_function_wrapper (sys, b, ps... ; p_start = 1 , expression = Val{true },
1212+ similarto = typeof (b), kwargs... )
1213+ return maybe_compile_function (expression, wrap_gfw, (1 , 1 , is_split (sys)), res;
1214+ eval_expression, eval_module)
1215+ end
0 commit comments