Constant Substitution implemented in repository

Posted on
Picture of user. mspiegel 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).

Replied on Wed, 09/02/2009 - 10:29
Picture of user. neale Joined: Jul 31, 2009

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?

Replied on Wed, 09/02/2009 - 10:57
Picture of user. mspiegel Joined: Jul 31, 2009

In reply to by neale

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.

Replied on Wed, 09/02/2009 - 11:26
Picture of user. tbates Joined: Jul 31, 2009

In reply to by neale

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
Replied on Mon, 09/07/2009 - 15:43
Picture of user. mspiegel Joined: Jul 31, 2009

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)
Replied on Mon, 09/07/2009 - 18:55
Picture of user. tbates Joined: Jul 31, 2009

In reply to by mspiegel

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