Added license information to R source files.
[openmx:openmx.git] / R / MxAlgebra.R
1 #
2 #   Copyright 2007-2009 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 setClassUnion("MxAlgebraFormula", c("call", "name", "logical"))
18
19 setClass(Class = "MxAlgebra",
20         representation = representation(
21                 formula = "MxAlgebraFormula",
22                 name = "character",
23                 result = "Matrix"))
24                 
25 setMethod("initialize", "MxAlgebra",
26         function(.Object, formula, name) {
27                 .Object@formula <- sys.call(which=-3)[[3]]
28                 .Object@name <- name
29                 return(.Object)
30         }
31 )
32
33 mxAlgebra <- function(expression, name = NA) {
34         if (is.na(name)) {
35                 name <- omxUntitledName()
36         }
37         retval <- new("MxAlgebra", NA, name)
38         retval@formula <- match.call()$expression
39         return(retval)  
40 }
41
42 defStringsAsFactors <- getOption('stringsAsFactors')
43 options(stringsAsFactors = FALSE)
44 data(omxSymbolTable)
45 options(stringsAsFactors = defStringsAsFactors)
46
47
48 omxFormulaList <- function(x) {
49         retval <- as.list(x)
50         retval <- lapply(retval, function(x) {
51                 if(is.call(x)) {omxFormulaList(x)} else {x}
52         })
53         return(retval)
54 }
55
56 omxNumericCheck <- function(formula, name) {
57         formula <- unlist(omxFormulaList(formula))
58         test <- sapply(formula, is.numeric)
59         if(any(test)) {
60                 msg <- paste("There is a numeric operand in",
61                         "the algebra named", omxQuotes(name))
62                 stop(msg, call. = FALSE)
63         }
64 }
65
66 omxSymbolCheck <- function(formula, name) {
67         formula <- unlist(omxFormulaList(formula))
68         test <- sapply(formula, function(x) {!is.numeric(x)})
69         if(length(formula[test]) == 1) {
70                 msg <- paste("The reference", omxQuotes(formula[test]),
71                         "is unknown in the algebra named", omxQuotes(name))
72                 stop(msg, call. = FALSE)
73         } else if (length(formula[test]) > 1) {
74                 msg <- paste("The references", omxQuotes(formula[test]),
75                         "are unknown in the algebra named", omxQuotes(name))
76                 stop(msg, call. = FALSE)                
77         }
78 }
79
80 generateAlgebraHelper <- function(algebra, matrixNames, algebraNames) {
81         retval <- algebra@formula
82         omxNumericCheck(retval, algebra@name)
83         matrixNumbers <- as.list(as.double(-1 : (-length(matrixNames))))
84         algebraNumbers <- as.list(as.double(0 : (length(algebraNames) - 1)))
85         names(matrixNumbers) <- matrixNames
86         names(algebraNumbers) <- algebraNames
87         retval <- eval(substitute(substitute(e, matrixNumbers), list(e = retval)))
88         retval <- eval(substitute(substitute(e, algebraNumbers), list(e = retval)))
89         retval <- substituteOperators(as.list(retval))
90         omxSymbolCheck(retval, algebra@name)
91         return(retval)
92 }
93
94 substituteOperators <- function(algebra) {
95         if ((length(algebra) == 1) && (is.list(algebra))) {
96                 algebra <- list(0, algebra[[1]])
97         } else if ((length(algebra) > 1) && (!is.numeric(algebra[[1]]))) {
98                 names <- omxSymbolTable["R.name"] == as.character(algebra[[1]])
99         variableSymbols <- omxSymbolTable["Number.of.arguments"] == -1
100                 result <- omxSymbolTable[names & variableSymbols, "Num"]
101                 if (length(result) > 1) {
102                                 stop(paste("Ambiguous function with name", algebra[[1]],
103                                         "and", (length(algebra) - 1), "arguments"))
104                 } else if(length(result) == 1) {
105                         head <- as.double(result[[1]])
106                         tail <- lapply(algebra[-1], substituteOperators)
107                         result <- append(tail, head, after=0)
108                         return(result)
109                 } else {
110                         length <- omxSymbolTable["Number.of.arguments"] == (length(algebra) - 1)
111                         result <- omxSymbolTable[names & length, "Num"]
112                         if (length(result) == 0) {
113                                 stop(paste("Could not find function with name", algebra[[1]],
114                                         "and", (length(algebra) - 1), "arguments"))
115                         } else if (length(result) > 1) {
116                                 stop(paste("Ambiguous function with name", algebra[[1]],
117                                         "and", (length(algebra) - 1), "arguments"))
118                         } else {
119                                 head <- as.double(result[[1]])
120                             tail <- lapply(algebra[-1], substituteOperators)
121                                 result <- append(tail, head, after=0)
122                                 return(result)
123                         }
124         }
125         }
126         return(algebra)
127 }