MxFitFunctionMultigroup
[openmx:openmx.git] / src / omxExpectation.h
1 /*
2  *  Copyright 2007-2013 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 *  omxExpectation.h
20 *
21 *  Created: Timothy R. Brick    Date: 2009-02-17
22 *
23 *       Contains header information for the omxExpectation class
24 *   omxExpectation objects hold necessary information to simplify
25 *       Expectation function calculation.
26 *
27 **********************************************************/
28
29 #ifndef _OMXEXPECTATION_H_
30 #define _OMXEXPECTATION_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 omxExpectation omxExpectation;
41 typedef struct omxDefinitionVar omxDefinitionVar;
42 typedef struct omxThresholdColumn omxThresholdColumn;
43
44 #include "omxMatrix.h"
45 #include "omxAlgebra.h"
46 #include "omxAlgebraFunctions.h"
47 #include "omxData.h"
48 #include "omxState.h"
49
50 /* Def Var and Threshold Structures */
51 struct omxDefinitionVar {                       // Definition Var
52
53         int data, column;               // Where it comes from
54         omxData* source;                // Data source
55         int numLocations;               // Num locations
56         int* rows;                              // row positions
57         int* cols;                              // column positions
58         int* matrices;                  // matrix numbers
59         int  numDeps;           // number of algebra/matrix dependencies
60         int* deps;              // indices of algebra/matrix dependencies
61
62 };
63
64 struct omxThresholdColumn {                     // Threshold
65
66         omxMatrix* matrix;              // Which Matrix/Algebra it comes from
67         int column;                             // Which column has the thresholds
68         int numThresholds;              // And how many thresholds
69
70 };
71
72 /* Expectation structure itself */
73 struct omxExpectation {                                 // An Expectation
74
75         /* Fields unique to Expectation Functions */
76         void (*initFun)(omxExpectation *ox);
77         void (*destructFun)(omxExpectation* ox);                                                                        // Wrapper for the destructor object
78         void (*computeFun)(omxExpectation* ox);                                                                         // Wrapper for the Expectation function itself
79         void (*printFun)(omxExpectation* ox);                                                                           // Prints the appropriate pieces of the expectation
80         void (*populateAttrFun)(omxExpectation* ox, SEXP algebra);                                      // Add attributes to the result algebra object
81         omxMatrix* (*componentFun)(omxExpectation*, omxFitFunction*, const char*);              // Return component locations to expectation
82         void (*mutateFun)(omxExpectation*, omxFitFunction*, const char*, omxMatrix*); // Modify/set/mutate components of expectation
83         
84         SEXP rObj;                                                                                                                                      // Original r Object Pointer
85         void* sharedArgs;                                                                                                                       // Common argument structure
86         void* argStruct;                                                                                                                        // Arguments needed for Expectation function
87         const char* expType;   // pointer to a static string, no need to allocate or free
88         unsigned short int isPrepopulated;                                                                                      // Object has had some values prepopulated to allow object sharing
89         omxData* data;                                                                                                                          // Not sure if this is appropriate, but the expectation passes the actual data object
90         omxMatrix* dataColumns;
91         omxThresholdColumn* thresholds;
92         omxDefinitionVar* defVars;
93         int numOrdinal, numDefs;
94         
95         /* Replication of some of the structures from Matrix */
96         unsigned short isComplete;                                                                                                      // Whether or not this expectation has been initialize
97         omxState* currentState;
98         int expNum;
99
100         omxExpectation *container;
101         int numSubmodels;
102         omxExpectation **submodels;
103 };
104
105 omxExpectation *
106 omxNewInternalExpectation(const char *expType, omxState* os);
107
108         void omxCompleteExpectation(omxExpectation *ox);
109         void omxFreeExpectationArgs(omxExpectation* Expectation);                                       // Frees all args
110 omxExpectation* omxExpectationFromIndex(int expIndex, omxState* os);
111         omxExpectation* omxNewIncompleteExpectation(SEXP mxobj, int expNum, omxState* os);
112         
113
114 /* Expectation-specific implementations of matrix functions */
115         void omxExpectationRecompute(omxExpectation *ox);
116         void omxExpectationCompute(omxExpectation *ox);
117         omxExpectation* omxDuplicateExpectation(const omxExpectation *src, omxState* newState);
118         
119         void omxExpectationPrint(omxExpectation *source, char* d);                                      // Pretty-print a (small-form) expectation
120         
121 omxMatrix* omxGetExpectationComponent(omxExpectation *ox, omxFitFunction *off, const char* component);
122         
123 void omxSetExpectationComponent(omxExpectation *ox, omxFitFunction *off, const char* component, omxMatrix *om);
124
125 #endif /* _OMXEXPECTATION_H_ */