diff --git a/solvespace.h b/solvespace.h index abbb1ba8..be9bcf32 100644 --- a/solvespace.h +++ b/solvespace.h @@ -250,7 +250,7 @@ public: double B[], int N); bool SolveLeastSquares(void); - void WriteJacobian(int tag); + bool WriteJacobian(int tag); void EvalJacobian(void); void WriteEquationsExceptFor(hConstraint hc, hGroup hg); diff --git a/system.cpp b/system.cpp index 7758cb08..e5ffd86c 100644 --- a/system.cpp +++ b/system.cpp @@ -3,11 +3,13 @@ const double System::RANK_MAG_TOLERANCE = 1e-4; const double System::CONVERGE_TOLERANCE = 1e-10; -void System::WriteJacobian(int tag) { +bool System::WriteJacobian(int tag) { int a, i, j; j = 0; for(a = 0; a < param.n; a++) { + if(j >= MAX_UNKNOWNS) return false; + Param *p = &(param.elem[a]); if(p->tag != tag) continue; mat.param[j] = p->h; @@ -17,6 +19,8 @@ void System::WriteJacobian(int tag) { i = 0; for(a = 0; a < eq.n; a++) { + if(i >= MAX_UNKNOWNS) return false; + Equation *e = &(eq.elem[a]); if(e->tag != tag) continue; @@ -43,6 +47,8 @@ void System::WriteJacobian(int tag) { i++; } mat.m = i; + + return true; } void System::EvalJacobian(void) { @@ -449,7 +455,11 @@ void System::Solve(Group *g, bool andFindFree) { // Now write the Jacobian for what's left, and do a rank test; that // tells us if the system is inconsistently constrained. - WriteJacobian(0); + if(!WriteJacobian(0)) { + g->solved.how = Group::TOO_MANY_UNKNOWNS; + TextWindow::ReportHowGroupSolved(g->h); + return; + } EvalJacobian(); diff --git a/textscreens.cpp b/textscreens.cpp index 20764ec3..7f63f015 100644 --- a/textscreens.cpp +++ b/textscreens.cpp @@ -516,6 +516,10 @@ void TextWindow::ShowGroupSolveInfo(void) { Printf(true, "%FxSOLVE FAILED!%Fd inconsistent system"); Printf(true, "remove any one of these to fix it"); break; + + case Group::TOO_MANY_UNKNOWNS: + Printf(true, "Too many unknowns in a single group!"); + return; } for(int i = 0; i < g->solved.remove.n; i++) {