Updates to MxModel and MxMatrix to work with back end solver.
[openmx:openmx.git] / R / MxModel.R
1 setClass(Class = "MxModel",
2         representation = representation(
3                 matrices = "list",
4                 algebras = "list"))
5                 
6 setMethod("initialize", "MxModel",
7         function(.Object, matrices=list(), algebras=list()) {
8                 .Object@matrices = matrices
9                 .Object@algebras = algebras
10                 return(.Object)
11         }
12 )
13
14 setMethod("[[", "MxModel",
15         function(x, i, j, ..., drop = FALSE) {
16                 first <- x@matrices[[i]]
17                 second <- x@algebras[[i]]
18                 if (is.null(first)) {
19                         return(second)
20                 } else {
21                         return(first)
22                 }       
23         }
24 )
25
26 setReplaceMethod("[[", "MxModel",
27         function(x, i, j, value) {
28                 if (is(value,"MxMatrix")) {
29                         if (!is.null(x@algebras[[i]])) {
30                                 stop(paste(i, "is already an MxAlgebra object"))
31                         }
32                         x@matrices[[i]] <- value
33                 } else if (is(value,"MxAlgebra")) {
34                         if (!is.null(x@matrices[[i]])) {
35                                 stop(paste(i, "is already an MxMatrix object"))
36                         }
37                         x@algebras[[i]] <- value                
38                 } else {
39                         stop(paste("Unknown type of value", value))
40                 }
41                 return(x)
42         }
43 )
44
45 mxGenerateMatrixList <- function(mxModel) {
46         return(lapply(mxModel@matrices, generateMatrixListHelper))
47 }
48
49 mxGenerateParameterList <- function(mxModel) {
50         result <- list()
51         for(i in 1:length(mxModel@matrices)) {
52                 result <- generaterParameterListHelper(mxModel@matrices[[i]], result, i - 1)
53         }       
54         return(result)
55 }