Merging the FitExpectation branch into the trunk.
[openmx:openmx.git] / src / omxExpectation.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 *  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 omxRListElement omxRListElement;
42 typedef struct omxDefinitionVar omxDefinitionVar;
43 typedef struct omxThresholdColumn omxThresholdColumn;
44
45 #include "omxMatrix.h"
46 #include "omxAlgebra.h"
47 #include "omxAlgebraFunctions.h"
48 #include "omxData.h"
49 #include "omxState.h"
50
51 struct omxRListElement {
52         char label[250];
53         double* values;
54         int numValues;
55         int rows, cols;
56 };
57
58 /* Def Var and Threshold Structures */
59 struct omxDefinitionVar {                       // Definition Var
60
61         int data, column;               // Where it comes from
62         omxData* source;                // Data source
63         int numLocations;               // Num locations
64         int* rows;                              // row positions
65         int* cols;                              // column positions
66         int* matrices;                  // matrix numbers
67         int  numDeps;           // number of algebra/matrix dependencies
68         int* deps;              // indices of algebra/matrix dependencies
69
70 };
71
72 struct omxThresholdColumn {                     // Threshold
73
74         omxMatrix* matrix;              // Which Matrix/Algebra it comes from
75         int column;                             // Which column has the thresholds
76         int numThresholds;              // And how many thresholds
77
78 };
79
80 /* Expectation structure itself */
81 struct omxExpectation {                                 // An Expectation
82
83         /* Fields unique to Expectation Functions */
84         void (*initFun)(omxExpectation *ox, SEXP rObj);                                                         // Wrapper for initialization function (probably not needed)
85         void (*destructFun)(omxExpectation* ox);                                                                        // Wrapper for the destructor object
86         void (*computeFun)(omxExpectation* ox);                                                                         // Wrapper for the Expectation function itself
87         void (*printFun)(omxExpectation* ox);                                                                           // Prints the appropriate pieces of the expectation
88         void (*setFitFun)(omxExpectation ox, omxFitFunction* off);                                      // To handle fit interactions
89         void (*repopulateFun)(omxExpectation* ox, double* x, int n);                            // To repopulate any data stored in the Expectation function
90         omxRListElement* (*setFinalReturns)(omxExpectation* ox, int *numVals);          // Sets any R returns.
91         void (*populateAttrFun)(omxExpectation* ox, SEXP algebra);                                      // Add attributes to the result algebra object
92         omxMatrix* (*componentFun)(omxExpectation*, omxFitFunction*, const char*);              // Return component locations to expectation
93         
94         SEXP rObj;                                                                                                                                      // Original r Object Pointer
95         void* sharedArgs;                                                                                                                       // Common argument structure
96         void* argStruct;                                                                                                                        // Arguments needed for Expectation function
97         char* expType;   // pointer to a static string, no need to allocate or free
98         unsigned short int isPrepopulated;                                                                                      // Object has had some values prepopulated to allow object sharing
99         omxData* data;                                                                                                                          // Not sure if this is appropriate, but the expectation passes the actual data object
100         omxMatrix* dataColumns;
101         omxThresholdColumn* thresholds;
102         omxDefinitionVar* defVars;
103         int numOrdinal, numDefs;
104         
105         /* Replication of some of the structures from Matrix */
106         unsigned short isComplete;                                                                                                      // Whether or not this expectation has been initialize
107         omxState* currentState;
108         int expNum;
109
110 };
111
112 /* Initialize and Destroy */
113         void omxInitEmptyExpectation(omxExpectation *ox);
114         void omxCompleteExpectation(omxExpectation *ox);
115         void omxFreeExpectationArgs(omxExpectation* Expectation);                                       // Frees all args
116         omxExpectation* omxNewExpectationFromExpectationIndex(int expIndex, omxState* os);
117         omxExpectation* omxNewIncompleteExpectation(SEXP mxobj, int expNum, omxState* os);
118         
119
120 /* Expectation-specific implementations of matrix functions */
121         void omxExpectationRecompute(omxExpectation *ox);
122         void omxExpectationCompute(omxExpectation *ox);
123         omxExpectation* omxDuplicateExpectation(const omxExpectation *src, omxState* newState);
124         
125         void omxExpectationPrint(omxExpectation *source, char* d);                                      // Pretty-print a (small-form) expectation
126         
127         omxMatrix* omxGetExpectationComponent(omxExpectation *ox, omxFitFunction *off, char* component);        // Get a component
128
129 #endif /* _OMXEXPECTATION_H_ */