[ Contents | Previous: A Simple LP | Next: Sums ]
Array indexing in Rima (and Lua) uses square brackets (x[1]) while
array construction uses curly braces (x = {13, 15, 17}).
x = rima.R"x"
print(x[1] + x[2] + x[3]) --> x[1] + x[2] + x[3]
print(rima.E(x[1] + x[2] + x[3], {x={10,20,30}})) --> 60
If you try to index something that's not an array Rima will complain:
x = rima.R"x"
print(rima.E(x[1], {x=2}))
--> error evaluating 'x[1]':
--> error indexing 'x' as 'x[1]': can't index a number
If you wish to use 2D arrays, you need to use two separate sets of square brackets
(x[2][3] rather than x[2, 3]),
and construct an array of arrays (x = {{2, 4}, {3, 5}}).
x = rima.R"x"
e = x[1][1] + x[2][2]
print(rima.E(e, {x={{5,7},{11,13}}})) --> 18
So far, we've passed data to rima.E in plain Lua tables,
but in fact, rima converts these tables into rima scopes.
Scopes offer a number of features beyond what plain tables offer,
and in this case,
they support more complex indexing.
While it's often convenient (or even necessary) to use scopes rather than plain tables, in all cases where you're not using any of the extra features of a scope, you can use a plain table.
You can assign to a whole array at once using a reference as one of your indices,
but you have to use a scope to get the indexing ability.
Scopes are created with rima.scope.new:
x, i = rima.R"x, i"
S = rima.scope.new()
S.x[i] = 10 -- This won't work with a plain table
print(rima.E(x[1], S), rima.E(x[7], S)) --> 10 10
Of course, you can compute the values in the array based on the indexes:
x, i = rima.R"x, i"
S = rima.scope.new()
S.x[i] = 2^i
print(rima.E(x[1], S), rima.E(x[7], S)) --> 2 128
You can use this method of indexing directly in the call to rima.scope.new,
but you have to enclose the index expression in square brackets:
x, i = rima.R"x, i"
S = rima.scope.new{ [x[i]] = 2^i }
print(rima.E(x[1], S), rima.E(x[7], S)) --> 2 128
[ Contents | Previous: A Simple LP | Next: Sums ]