rtable_eval - evaluate the entries of an rtable under certain conditions
|
Calling Sequence
|
|
rtable_eval(A,inplace)
|
|
Parameters
|
|
A
|
-
|
rtable object
|
inplace
|
-
|
(optional) BooleanOpt(inplace); specifies if output overwrites input
|
|
|
|
|
Description
|
|
•
|
The rtable_eval(A) command, where A is an Array, Matrix, or Vector, returns either a copy of A, or the original input A depending if evaluation is needed. It also changes the way elements are evaluated when the returned rtable is subsequently referenced.
|
|
Evaluation is needed provided the following two conditions are met. Firstly, eval(A[i],1) <> eval(A[i]) for a given element . Secondly, full evaluation must have been applied to A prior to calling rtable_eval(A).
|
|
Applying eval() to an rtable returns immediately without changing the given rtable. This is unlike a list, which maps eval() onto each element and returns a copy. Evaluation of rtable elements is normally deferred until elements are referenced. If the same elements are repeatedly referenced, evaluation of those elements is also repeated. The rtable_eval(A) command provides a way to evaluate every element once so that future references can bypass evaluation, without necessarily making a copy.
|
•
|
Applying rtable_eval to any rtable with a hardware datatype always returns the input rtable unchanged.
|
•
|
Applying rtable_eval to any rtable with a user defined indexing-function always returns the input rtable unchanged.
|
•
|
The result of rtable_eval is an rtable with the same attributes as the input rtable, including shape, storage, and datatype.
|
•
|
If the optional second argument, inplace is specified, the input rtable is changed if necessary. No copy is made.
|
|
|
Thread Safety
|
|
•
|
The rtable_eval command is thread-safe as of Maple 15.
|
|
|
Examples
|
|
Create an rtable that prints something when an element is referenced.
>
|
p := proc(i) printf("eval element %d\n",i); end proc:
|
>
|
|
Reference the first element 4 times. This generates 4 eval()'s.
>
|
f := proc(V) V[1]; V[1]; V[1]; V[1]; end proc:
|
>
|
|
eval element 1
eval element 1
eval element 1
eval element 1
| |
Use rtable_eval() to evaluate every element initially. Subsequent references will already contain the results of evaluating, so V[1] does not print anything.
>
|
f := proc(VV) local V; V := rtable_eval(VV); V[1]; V[1]; V[1]; V[1]; end proc:
|
>
|
|
eval element 1
eval element 3
eval element 2
| |
Generate a Matrix containing polynomials that are expensive to evaluate.
>
|
F := proc(n) option remember; if n <= 2 then n else x*F(n-1)+F(n-2); end if; end proc:
|
>
|
|
Compare the cost of an O(N^3) operation with and without rtable_eval().
>
|
dostuff1 := proc(M) local i, j, k, y;
for i from 1 to op([1,1],M) do
for j from 1 to op([1,2],M) do
for k from 1 to op([1,2],M) do
y := M[i,j];
end do;
end do;
end do;
end proc:
|
>
|
dostuff2 := proc(M) local i, j, k, y;
rtable_eval(M,'inplace');
for i from 1 to op([1,1],M) do
for j from 1 to op([1,2],M) do
for k from 1 to op([1,2],M) do
y := M[i,j];
end do;
end do;
end do;
end proc:
|
>
|
|
| (1) |
>
|
|
| (2) |
|
|