Added setter/mutator method to state space expectation
[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         void (*mutateFun)(omxExpectation*, omxFitFunction*, const char*, omxMatrix*); // Modify/set/mutate components of expectation
94         
95         SEXP rObj;                                                                                                                                      // Original r Object Pointer
96         void* sharedArgs;                                                                                                                       // Common argument structure
97         void* argStruct;                                                                                                                        // Arguments needed for Expectation function
98         char* expType;   // pointer to a static string, no need to allocate or free
99         unsigned short int isPrepopulated;                                                                                      // Object has had some values prepopulated to allow object sharing
100         omxData* data;                                                                                                                          // Not sure if this is appropriate, but the expectation passes the actual data object
101         omxMatrix* dataColumns;
102         omxThresholdColumn* thresholds;
103         omxDefinitionVar* defVars;
104         int numOrdinal, numDefs;
105         
106         /* Replication of some of the structures from Matrix */
107         unsigned short isComplete;                                                                                                      // Whether or not this expectation has been initialize
108         omxState* currentState;
109         int expNum;
110
111 };
112
113 /* Initialize and Destroy */
114         void omxInitEmptyExpectation(omxExpectation *ox);
115         void omxCompleteExpectation(omxExpectation *ox);
116         void omxFreeExpectationArgs(omxExpectation* Expectation);                                       // Frees all args
117         omxExpectation* omxNewExpectationFromExpectationIndex(int expIndex, omxState* os);
118         omxExpectation* omxNewIncompleteExpectation(SEXP mxobj, int expNum, omxState* os);
119         
120
121 /* Expectation-specific implementations of matrix functions */
122         void omxExpectationRecompute(omxExpectation *ox);
123         void omxExpectationCompute(omxExpectation *ox);
124         omxExpectation* omxDuplicateExpectation(const omxExpectation *src, omxState* newState);
125         
126         void omxExpectationPrint(omxExpectation *source, char* d);                                      // Pretty-print a (small-form) expectation
127         
128         omxMatrix* omxGetExpectationComponent(omxExpectation *ox, omxFitFunction *off, char* component);        // Get a component
129         
130         void omxSetExpectationComponent(omxExpectation *ox, omxFitFunction *off, char* component, omxMatrix *om);       // Set a component
131
132 #endif /* _OMXEXPECTATION_H_ */