@@ -21,7 +21,7 @@ bool System::WriteJacobian(int tag) {
2121 // Clear all
2222 mat.param .clear ();
2323 mat.eq .clear ();
24- mat.A .sym .setZero ();
24+ mat.A .sym .clear ();
2525 mat.B .sym .clear ();
2626
2727 for (Param &p : param) {
@@ -35,8 +35,7 @@ bool System::WriteJacobian(int tag) {
3535 mat.eq .push_back (&e);
3636 }
3737 mat.m = mat.eq .size ();
38- mat.A .sym .resize (mat.m , mat.n );
39- mat.A .sym .reserve (Eigen::VectorXi::Constant (mat.n , LikelyPartialCountPerEq));
38+ mat.A .sym .reserve (mat.n * LikelyPartialCountPerEq);
4039
4140 // Fill the param id to index map
4241 std::map<uint32_t , int > paramToIndex;
@@ -72,7 +71,7 @@ bool System::WriteJacobian(int tag) {
7271 pd = pd->FoldConstants ();
7372 if (pd->IsZeroConst ())
7473 continue ;
75- mat.A .sym .insert ( i, j) = pd ;
74+ mat.A .sym .emplace_back (( int ) i, j, pd) ;
7675 }
7776 paramsUsed.clear ();
7877 mat.B .sym .push_back (f);
@@ -84,16 +83,14 @@ void System::EvalJacobian() {
8483 using namespace Eigen ;
8584 mat.A .num .setZero ();
8685 mat.A .num .resize (mat.m , mat.n );
87- const int size = mat.A .sym .outerSize ();
8886
89- for (int k = 0 ; k < size; k++) {
90- for (SparseMatrix <Expr *>::InnerIterator it (mat.A .sym , k); it; ++it) {
91- double value = it.value ()->Eval ();
92- if (EXACT (value == 0.0 )) continue ;
93- mat.A .num .insert (it.row (), it.col ()) = value;
94- }
87+ std::vector<Eigen::Triplet<double >> values;
88+ values.reserve (mat.A .sym .size ());
89+ for (const auto &exprTriplet : mat.A .sym ) {
90+ double value = exprTriplet.value ()->Eval ();
91+ values.emplace_back (exprTriplet.row (), exprTriplet.col (), value);
9592 }
96- mat.A .num .makeCompressed ( );
93+ mat.A .num .setFromTriplets (values. begin (), values. end () );
9794}
9895
9996bool System::IsDragged (hParam p) {
@@ -566,7 +563,7 @@ void System::Clear() {
566563 eq.Clear ();
567564 dragged.Clear ();
568565 mat.A .num .setZero ();
569- mat.A .sym .setZero ();
566+ mat.A .sym .clear ();
570567}
571568
572569void System::MarkParamsFree (bool find) {
0 commit comments