Constant Substitution implemented in repository

5 replies [Last post]
mspiegel's picture
Offline
Joined: 07/31/2009

As of revision 784, literal constant substitution is implemented in the repository. This only works for literal expressions that evaluate to constants, such as "1" or "1.0" or "8675903". There is a trivial example in models/passing/ConstantSubstitution.R replicated here in its entirety:

    require(OpenMx)
    foo <- mxAlgebra(1 + 2 + 3, 'foo')
    model <- mxModel('model', foo)
    modelOut <- mxRun(model)
    omxCheckEquals(6, mxEval(foo, modelOut))

The next step will be to implement free parameter and fixed parameter substitution (this week), and then I can finish with global variable substitution in algebra expressions (either this week or next week).

mspiegel's picture
Offline
Joined: 07/31/2009
Global variable substitution

Global variable substitution has been checked into the repository. Any global variable that evaluates to a numeric type (scalar, vector, or matrix) and whose name does not match a free parameter, fixed parameter, or named entity, can be used in an mxAlgebra statement. A simple example is the following:

    foo <- mxAlgebra(pi, name = 'foo')
    model <- mxModel('model', foo)
    modelOut <- mxRun(model)
    mxEval(foo, modelOut)

tbates's picture
Offline
Joined: 07/31/2009
nice! modelOut=

nice!
modelOut= mxRun(mxModel('model', mxMatrix("Full", 2,2,values=0:3, name="A"), mxAlgebra(pi%x%A, name = 'foo') ) )
Running model
> mxEval(foo, modelOut)
[,1] [,2]
[1,] 0.000000 6.283185
[2,] 3.141593 9.424778

neale's picture
Offline
Joined: 07/31/2009
Awesome! To be clear,

Awesome! To be clear, numeric constants are treated as 1x1 matrices, so that they can be used with, e.g., Kronecker product or other matrix operators? And second, can exponential or double precision format constants be used, 1.234E+56 or 1.234D+56?

tbates's picture
Offline
Joined: 07/31/2009
Indeed awesome! Must be

Indeed awesome! Must be watching Entourage: likes E

model <- mxRun(mxModel('model', mxAlgebra(1 + 2 + 3.32E5, 'foo'))); mxEval(foo,model)
       [,1]
[1,] 332003

Doesn't like D

model <- mxRun(mxModel('model', mxAlgebra(1.234D6 + 3.32E5, 'foo')))
Error: syntax error

Kronecker happy

mxMatrix('Full',3,3,F,c(1,2,3,4,5,6), name="M")->matM; 
model <- mxRun(mxModel('model', matM, mxAlgebra(M %x% 3.32E5, 'foo')));
mxEval(foo,model)
 
       [,1]    [,2]   [,3]
[1,] 332000 1328000 332000
[2,] 664000 1660000 664000
[3,] 996000 1992000 996000

mspiegel's picture
Offline
Joined: 07/31/2009
Yup, numeric constants are

Yup, numeric constants are treated as 1x1 matrices, so that they can be used with, e.g., Kronecker product or other matrix operator. Oops, you need to svn update to revision 785 to write 1.234E+56 or 1.234e+56. I'm not supporting 1.234D+56 because R doesn't recognize that expression.