void FinishDeferredSF (void) { if (deferSetFormula->lLength) { SortLists (deferSetFormula, &deferIsConstant); _SimpleList tcache; long iv, i = variableNames.Traverser (tcache,iv,variableNames.GetRoot()); for (; i >= 0; i = variableNames.Traverser (tcache,iv)) { _Variable* theV = FetchVar(i); if (theV->IsContainer()) { ((_VariableContainer*)theV)->SetMDependance (*deferSetFormula); } } for (long j = 0; j<likeFuncList.lLength; j++) if (((_String*)likeFuncNamesList(j))->sLength) { _LikelihoodFunction * lf = (_LikelihoodFunction*)likeFuncList(j); for (long k = 0; k < deferSetFormula->lLength; k++) { lf->UpdateIndependent(deferSetFormula->lData[k],deferIsConstant.lData[k]); } } } DeleteObject (deferSetFormula); deferSetFormula = nil; deferIsConstant.Clear(); }
void _Variable::SetFormula (_Formula& theF) // set the value of the var to a formula { bool changeMe = false, isAConstant = theF.IsAConstant(); _Formula* myF = &theF; if (isAConstant) { _PMathObj theP = theF.Compute(); if (theP) { myF = new _Formula ((_PMathObj)theP->makeDynamic(),false); checkPointer (myF); } else { return; } } _SimpleList vars; { _AVLList vA (&vars); theF.ScanFForVariables (vA,true); vA.ReorderList(); } if (vars.BinaryFind(theIndex)>=0) { _String * sf = (_String*)theF.toStr(); WarnError ((_String("Can't set variable ")&*GetName()&" to "&*sf&" because it would create a circular dependance.")); DeleteObject(sf); if (&theF!=myF) { delete myF; } return; } varFlags &= HY_VARIABLE_SET; if (varFlags & HY_VARIABLE_CHANGED) { varFlags -= HY_VARIABLE_CHANGED; } if (varFormula) { delete (varFormula); varFormula = nil; } else { changeMe = true; } if (varValue) { DeleteObject (varValue); varValue=nil; } //_Formula::Duplicate ((BaseRef)myF); varFormula = new _Formula; varFormula->Duplicate ((BaseRef)myF); // mod 20060125 added a call to simplify constants varFormula->SimplifyConstants (); // also update the fact that this variable is no longer independent in all declared // variable containers which contain references to this variable if (changeMe) if (deferSetFormula) { *deferSetFormula << theIndex; deferIsConstant << isAConstant; } else { long i; _SimpleList tcache; long iv; i = variableNames.Traverser (tcache,iv,variableNames.GetRoot()); for (; i >= 0; i = variableNames.Traverser (tcache,iv)) { _Variable* theV = FetchVar(i); if (theV->IsContainer()) { _VariableContainer* theVC = (_VariableContainer*)theV; if (theVC->SetDependance(theIndex) == -2) { ReportWarning ((_String("Can't make variable ")&*GetName()&" dependent in the context of "&*theVC->GetName()&" because its template variable is bound by another relation in the global context.")); continue; } } } { for (long i = 0; i<likeFuncList.lLength; i++) if (((_String*)likeFuncNamesList(i))->sLength) { ((_LikelihoodFunction*)likeFuncList(i))->UpdateIndependent(theIndex,isAConstant); } } } if (&theF!=myF) { delete myF; } }
//__________________________________________________________________________________ void _Variable::SetValue (_PMathObj theP, bool dup) // set the value of the var { //hasBeenChanged = true; varFlags &= HY_VARIABLE_SET; varFlags |= HY_VARIABLE_CHANGED; long valueClass = theP->ObjectClass(); if (valueClass==NUMBER) { if (varFormula) { // also update the fact that this variable is no longer dependent in all declared // variable containers which contain references to this variable long i; for (i = 0; i<variablePtrs.lLength; i++) { if (freeSlots.Find(i)>=0) { continue; } _Variable* theV = (_Variable*)variablePtrs(i); if (theV->IsContainer()) { _VariableContainer* theVC = (_VariableContainer*)theV; if (!theVC->RemoveDependance (theIndex)) { ReportWarning ((_String("Can't make variable ")&*GetName()&" independent in the context of "&*theVC->GetName()&" because its template variable is not independent.")); continue; } } } for (i = 0; i<likeFuncList.lLength; i++) if (((_String*)likeFuncNamesList(i))->sLength) { ((_LikelihoodFunction*)likeFuncList(i))->UpdateDependent(theIndex); } //_Formula::Clear(); delete (varFormula); varFormula = nil; } if (varValue) { DeleteObject (varValue); varValue=nil; } theValue = theP->Value(); if (!dup) { DeleteObject (theP); } if (theValue<lowerBound || theValue>upperBound) { if (theValue <= lowerBound+1e-50) { theValue = lowerBound; } else { theValue = upperBound; } } } else { if (varFormula) { delete (varFormula); varFormula = nil; //theFormula.Clear(); } if (varValue) { DeleteObject (varValue); varValue=nil; } if (valueClass==TREE) { variablePtrs.lData[theIndex] = (long)(((_TheTree*)theP)->makeDynamicCopy(GetName())); DeleteObject(this); } else { if (dup) { varValue = (_PMathObj)theP->makeDynamic(); } else { varValue = theP; } } } }