Rima Manual: Expressions

[ Contents | Previous: References | Next: A Simple LP ]

Expressions are Rima's fundamental construct. They are stored symbolically and can be evaluated with partial or full data at any time. Expressions are built from references (to data or variables), arithmetic operators and functions and by combining other expressions.

Defining an Expression

Defining an expression in Rima is straightforward:


rima.define"x, y"
e = x + y
print(e)                                            --> x + y

Normal math operators (+, -, *, /) all work, and you can use all the functions in the Lua math library, though you have you'll have to use the Rima versions:


print(rima.sin(rima.R"x"))                          --> sin(x)

Expressions can be treated just like variables, and used to build more complex expressions:


x, y = rima.R"x, y"
e = 2 + y
print(x * e)                                         --> x*(2 + y)

Evaluating Expressions

rima.E evaluates expressions as it does references:


rima.define"x, y"
print(rima.E(x + y, { x=1, y=2 }))                  --> 3

Partial lists of values are fine - rima.E doesn't mind if some variables aren't defined:


x, y = rima.R"x, y"
print(rima.E(x * rima.sin(y), { x=5 }))             --> 5*sin(y)

And sometimes it doesn't matter if they're defined or not:


x, y = rima.R"x, y"
print(rima.E(x * y * y / y^2 - x))                  --> 0

If an expression is completely defined, that is, all the references in the expression resolve to numbers, then rima.E returns a number. If not, rima.E returns another expression, which you can evaluate later:


x, y = rima.R"x, y"
e = rima.E(x + y, { x=7 })
print(e)                                            --> 7 + y
print(rima.E(e, { y=13 }))                          --> 20

rima.E will simplify expressions, even without a scope:


x, y = rima.R"x, y"
e = x * x * y * y^2
print(e)                                            --> x*x*y*y^2
print(rima.E(e))                                    --> x^2*y^3

References to Expressions

Just as the values in the scope can be references, they can also be expressions, allowing you to build complex expressions from parts and meaning you can leave some parts of an expression to be defined later:


rima.define"x, y, z"
e = x / y
print(rima.E(e, { y=z^2 }))                         --> x/z^2
print(rima.E(e, { y=z^2, z=x^0.5 }))                --> 1

[ Contents | Previous: References | Next: A Simple LP ]