Remove unused argument from omxResizeMatrix
[openmx:openmx.git] / src / omxFitFunction.cpp
1 /*
2  *  Copyright 2007-2014 The OpenMx Project
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 /***********************************************************
18
19 *  omxFitFunction.cc
20 *
21 *  Created: Timothy R. Brick    Date: 2008-11-13 12:33:06
22 *
23 *       FitFunction objects are a subclass of data matrix that evaluates
24 *   itself anew at each iteration, so that any changes to
25 *   free parameters can be incorporated into the update.
26 *   // Question: Should FitFunction be a ``subtype'' of 
27 *   // omxAlgebra or a separate beast entirely?
28 *
29 **********************************************************/
30
31 #include "omxFitFunction.h"
32 #include "fitMultigroup.h"
33
34 typedef struct omxFitFunctionTableEntry omxFitFunctionTableEntry;
35
36 struct omxFitFunctionTableEntry {
37
38         char name[32];
39         void (*initFun)(omxFitFunction*);
40         void (*setVarGroup)(omxFitFunction*, FreeVarGroup *);  // TODO ugh, just convert to C++
41
42 };
43
44 static void defaultSetFreeVarGroup(omxFitFunction *ff, FreeVarGroup *fvg)
45 {
46         if (ff->freeVarGroup && ff->freeVarGroup != fvg) {
47                 Rf_warning("%s: setFreeVarGroup called with different group (%d vs %d)",
48                         ff->matrix->name, ff->freeVarGroup->id[0], fvg->id[0]);
49         }
50         ff->freeVarGroup = fvg;
51 }
52
53 static const omxFitFunctionTableEntry omxFitFunctionSymbolTable[] = {
54         {"MxFitFunctionAlgebra",                        &omxInitAlgebraFitFunction, defaultSetFreeVarGroup},
55         {"MxFitFunctionWLS",                            &omxInitWLSFitFunction, defaultSetFreeVarGroup},
56         {"MxFitFunctionRow",                            &omxInitRowFitFunction, defaultSetFreeVarGroup},
57         {"MxFitFunctionML",                             &omxInitMLFitFunction, defaultSetFreeVarGroup},
58         {"imxFitFunctionFIML", &omxInitFIMLFitFunction, defaultSetFreeVarGroup},
59         {"MxFitFunctionR",                                      &omxInitRFitFunction, defaultSetFreeVarGroup},
60         {"MxFitFunctionMultigroup", &initFitMultigroup, mgSetFreeVarGroup},
61 };
62
63 void omxFreeFitFunctionArgs(omxFitFunction *off) {
64         if(off==NULL) return;
65     
66         /* Completely destroy the fit function structures */
67         if(off->matrix != NULL) {
68                 if (off->destructFun) off->destructFun(off);
69                 off->matrix = NULL;
70         }
71 }
72
73 void omxFitFunctionCreateChildren(omxState *globalState)
74 {
75         if (Global->numThreads <= 1) return;
76
77         if (globalState->childList.size()) Rf_error("Children already created");
78
79         int numThreads = Global->numChildren = Global->numThreads;
80
81         globalState->childList.resize(numThreads);
82
83         for(int ii = 0; ii < numThreads; ii++) {
84                 globalState->childList[ii] = new omxState;
85                 omxInitState(globalState->childList[ii]);
86                 omxDuplicateState(globalState->childList[ii], globalState);
87         }
88 }
89
90 void omxDuplicateFitMatrix(omxMatrix *tgt, const omxMatrix *src, omxState* newState) {
91
92         if(tgt == NULL || src == NULL) return;
93
94         omxFitFunction *ff = src->fitFunction;
95         if(ff == NULL) return;
96     
97         omxFillMatrixFromMxFitFunction(tgt, ff->fitType, src->matrixNumber);
98         setFreeVarGroup(tgt->fitFunction, src->fitFunction->freeVarGroup);
99         tgt->fitFunction->rObj = ff->rObj;
100         omxCompleteFitFunction(tgt);
101 }
102
103 void omxFitFunctionCompute(omxFitFunction *off, int want, FitContext *fc)
104 {
105         if (!off->initialized) Rf_error("FitFunction not initialized");
106
107         off->computeFun(off, want, fc);
108         if (fc) fc->wanted |= want;
109
110         if (want & FF_COMPUTE_FIT) {
111                 omxMarkClean(off->matrix);
112         }
113 }
114
115 void defaultAddOutput(omxFitFunction* oo, MxRList *out)
116 {}
117
118 void omxFillMatrixFromMxFitFunction(omxMatrix* om, const char *fitType, int matrixNumber)
119 {
120         omxFitFunction *obj = (omxFitFunction*) R_alloc(1, sizeof(omxFitFunction));
121         memset(obj, 0, sizeof(omxFitFunction));
122
123         /* Register FitFunction and Matrix with each other */
124         obj->matrix = om;
125         omxResizeMatrix(om, 1, 1);                                      // FitFunction matrices MUST be 1x1.
126         om->fitFunction = obj;
127         om->hasMatrixNumber = TRUE;
128         om->matrixNumber = matrixNumber;
129         
130         for (size_t fx=0; fx < OMX_STATIC_ARRAY_SIZE(omxFitFunctionSymbolTable); fx++) {
131                 const omxFitFunctionTableEntry *entry = omxFitFunctionSymbolTable + fx;
132                 if(strcmp(fitType, entry->name) == 0) {
133                         obj->fitType = entry->name;
134                         obj->initFun = entry->initFun;
135
136                         // We need to set up the FreeVarGroup before calling initFun
137                         // because older fit functions expect to know the number of
138                         // free variables during initFun.
139                         obj->setVarGroup = entry->setVarGroup; // ugh!
140                         obj->addOutput = defaultAddOutput;
141                         break;
142                 }
143         }
144
145         if (obj->initFun == NULL) Rf_error("Fit function %s not implemented", fitType);
146 }
147
148 void omxCompleteFitFunction(omxMatrix *om)
149 {
150         omxFitFunction *obj = om->fitFunction;
151         if (obj->initialized) return;
152         SEXP rObj = obj->rObj;
153
154         SEXP slotValue;
155         Rf_protect(slotValue = R_do_slot(rObj, Rf_install("expectation")));
156         if (LENGTH(slotValue) == 1) {
157                 int expNumber = INTEGER(slotValue)[0];  
158                 if(expNumber != NA_INTEGER) {
159                         obj->expectation = omxExpectationFromIndex(expNumber, om->currentState);
160                         setFreeVarGroup(obj->expectation, obj->freeVarGroup);
161                         omxCompleteExpectation(obj->expectation);
162                 }
163         }
164         Rf_unprotect(1);        /* slotValue */
165         
166         obj->initFun(obj);
167
168         if(obj->computeFun == NULL) Rf_error("Failed to initialize fit function %s", obj->fitType); 
169         
170         obj->matrix->data[0] = NA_REAL;
171         omxMarkDirty(obj->matrix);
172         obj->initialized = TRUE;
173 }
174
175 void setFreeVarGroup(omxFitFunction *ff, FreeVarGroup *fvg)
176 {
177         (*ff->setVarGroup)(ff, fvg);
178 }
179
180 void omxFitFunctionPrint(omxFitFunction* off, const char* d) {
181         mxLog("(FitFunction, type %s)", off->fitType);
182         omxPrintMatrix(off->matrix, d);
183 }
184
185
186 /* Helper functions */
187 omxMatrix* omxNewMatrixFromSlot(SEXP rObj, omxState* currentState, const char* slotName) {
188         SEXP slotValue;
189         Rf_protect(slotValue = R_do_slot(rObj, Rf_install(slotName)));
190         omxMatrix* newMatrix = omxMatrixLookupFromState1(slotValue, currentState);
191         Rf_unprotect(1);
192         return newMatrix;
193 }
194