Merging the FitExpectation branch into the trunk.
[openmx:openmx.git] / src / omxFitFunction.h
1 /*
2  *  Copyright 2007-2012 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.h
20 *
21 *  Created: Timothy R. Brick    Date: 2009-02-17
22 *
23 *       Contains header information for the omxFitFunction class
24 *   omxFitFunction objects hold necessary information to simplify
25 *       fit function calculation.
26 *
27 **********************************************************/
28
29 #ifndef _OMXFITFUNCTION_H_
30 #define _OMXFITFUNCTION_H_
31
32 #include "R.h"
33 #include <Rinternals.h> 
34 #include <Rdefines.h>
35 #include <R_ext/Rdynload.h> 
36 #include <R_ext/BLAS.h>
37 #include <R_ext/Lapack.h>
38 #include "omxDefines.h"
39
40 typedef struct omxFitFunction omxFitFunction;
41
42
43 #include "omxMatrix.h"
44 #include "omxAlgebra.h"
45 #include "omxAlgebraFunctions.h"
46 #include "omxData.h"
47 #include "omxState.h"
48 #include "omxFitFunctionTable.h"
49 #include "omxExpectation.h"
50
51 struct omxFitFunction {                                 // A fit function
52
53         /* Fields unique to FitFunction Functions */
54         void (*initFun)(omxFitFunction *oo, SEXP rObj);                                                         // Wrapper for initialization function (probably not needed)
55         void (*destructFun)(omxFitFunction* oo);                                                                        // Wrapper for the destructor object
56         void (*computeFun)(omxFitFunction* oo);                                                                         // Wrapper for the fit function itself
57         void (*repopulateFun)(omxFitFunction* oo, double* x, int n);                                    // To repopulate any data stored in the fit function
58         omxRListElement* (*setFinalReturns)(omxFitFunction* oo, int *numVals);          // Sets any R returns.
59         double* (*getStandardErrorFun)(omxFitFunction* oo);                                                     // To calculate standard errors
60         void (*gradientFun)(omxFitFunction* oo, double* grad);                                          // To calculate gradient
61         void (*populateAttrFun)(omxFitFunction* oo, SEXP algebra);                                      // Add attributes to the result algebra object
62         
63         SEXP rObj;                                                                                                                                      // Original r Object Pointer
64         omxExpectation* expectation;                                                                                            // Data expectation object
65         void* sharedArgs;                                                                                                                       // Common argument structure
66         void* argStruct;                                                                                                                        // Arguments needed for fit function
67 // This is always a pointer to a static string.
68 // We do not need to allocate or free it.
69         const char* fitType;                                                                                                            // Type of FitFunction Function
70         double* hessian;                                                                                                                        // Hessian details
71         double* gradient;                                                                                                                       // Gradient details
72         double* stdError;                                                                                                                       // Standard Error estimates
73         unsigned short int isPrepopulated;                                                                                      // Object has had some values prepopulated to allow object sharing
74
75         omxMatrix* matrix;                                                                                                                      // The (1x1) matrix populated by this fit function
76
77 };
78
79 /* Initialize and Destroy */
80         void omxInitEmptyFitFunction(omxFitFunction *oo);
81         void omxFillMatrixFromMxFitFunction(omxMatrix* om, SEXP mxobj, 
82                 unsigned short hasMatrixNumber, int matrixNumber);                                              // Create an omxFitFunction from an R MxFitFunction object
83         void omxFreeFitFunctionArgs(omxFitFunction* fitFunction);                                               // Frees all args
84         void omxGetFitFunctionStandardErrors(omxFitFunction *oo);                                       // Get Standard Errors
85
86 /* FitFunction-specific implementations of matrix functions */
87         void omxFitFunctionCompute(omxFitFunction *oo);
88         void omxFitFunctionGradient(omxFitFunction* oo, double* gradient);                      // For gradient calculation.  If needed.
89         void omxDuplicateFitMatrix(omxMatrix *tgt, const omxMatrix *src, omxState* targetState);
90         omxFitFunction* omxCreateDuplicateFitFunction(omxFitFunction *tgt, const omxFitFunction *src, omxState* newState);
91         
92         void omxFitFunctionPrint(omxFitFunction *source, char* d);                                      // Pretty-print a (small) matrix
93         
94         /* Helper functions */
95         void omxCalculateStdErrorFromHessian(double scale, omxFitFunction *oo); // Does what it says
96         
97         /* Helpers related to fit function initialization */
98         omxMatrix* omxNewMatrixFromIndexSlot(SEXP rObj, omxState* state, char* const slotName); // Gets a matrix from an R SEXP slot
99
100         omxData* omxNewDataFromDataSlot(SEXP rObj, omxState* state, char* const dataSlotName);  // Gets an mxData object from a data slot
101
102 #endif /* _OMXFITFUNCTION_H_ */