Line 6: | Line 6: | ||
# it starts with expr | # it starts with expr | ||
− | # it is surrounded by simple quotes ' ' | + | # it is then followed by any code that could be entered into a ftm.mess but it needs to be surrounded by simple quotes ' ' |
− | # | + | # to be evaluated the code has to be encapsulated within parenthesis (just like any code in ftm.mess) |
For example, the expression returning the value 1 is | For example, the expression returning the value 1 is | ||
expr '(1)' | expr '(1)' | ||
+ | |||
+ | Expression returning a tuple {1 2 3} could be: | ||
+ | |||
+ | expr '1 (1 + 1) (1 + 2)' | ||
+ | |||
+ | But the following expression returns just 3: | ||
+ | |||
+ | expr '1, (1 + 1), (1 + 2)' | ||
+ | |||
+ | Although all of the the comma separated parts are executed, only the last one i returned. So in the following example, after first execution the value returned would be 9: | ||
+ | |||
+ | expr '($var += 10), ($var - 1)' | ||
+ | |||
+ | where var is a global variable which was initialized to 0. Since both parts are executed, var is first incremented by 10, but only the result of ($var - 1) is returned. After second execution the returned value would be 19, after third 29, and so on. | ||
You can use all the [[List of functions | FTM Functions]] available to create your expression. Moreover, $1, $2, etc will be replaced by values passed to your instance of expr. | You can use all the [[List of functions | FTM Functions]] available to create your expression. Moreover, $1, $2, etc will be replaced by values passed to your instance of expr. | ||
Line 27: | Line 41: | ||
* within [[ftm.mess]] | * within [[ftm.mess]] | ||
− | + | (new expr '(sin $1)') | |
+ | |||
+ | where it can be evaluated using its ''eval'' method: | ||
+ | |||
+ | ((new expr '(sin $1)') eval $pi) | ||
+ | |||
+ | pi being a local variable passed as first argument to the expression (replacing $1). | ||
+ | |||
+ | or passed as an argument to a method to another object, like in the following example: | ||
((new fmat 10 3) fill (new expr '(random 1 3)')) | ((new fmat 10 3) fill (new expr '(random 1 3)')) | ||
Line 33: | Line 55: | ||
== Scope of variables == | == Scope of variables == | ||
− | In the previous example, you could be tempted to use $1 and $2 to choose the boundaries of the random function within the | + | In the previous example, you could be tempted to use $1 and $2 to choose the boundaries of the random function within the expression but the variables $1 and $2 would not refer to the [[ftm.mess]] inputs, but to the expression arguments. We therefore need to pass those input variables (local to the [[ftm.mess]]) as arguments to our newly created instance of expr by placing them directly after expr statement: |
((new fmat 10 3) fill (new expr '(random $1 $2)') $1 $2) | ((new fmat 10 3) fill (new expr '(random $1 $2)') $1 $2) | ||
− | == Special Variables == | + | In the case our expression has been previously defined in a ftm.object and named for example '''randomize''' the above could look like this: |
+ | |||
+ | ((new fmat 10 3) fill $randomize $1 $2) | ||
+ | |||
+ | == Special Variables == | ||
Depending of context of use, an expr instance has access to some specific variables | Depending of context of use, an expr instance has access to some specific variables | ||
Line 43: | Line 69: | ||
* FMAT | * FMAT | ||
** Fill | ** Fill | ||
− | *** $row | + | *** $row - the row number of currently processed element of a fmat |
− | *** $col | + | *** $col - the col number of currently processed element of a fmat |
− | *** $self | + | *** $self - reference to currently processed fmat |
+ | |||
+ | For example: | ||
+ | |||
+ | ((new fmat 10 2) fill (new expr '(($self rows) - $row - $col)')) | ||
+ | |||
+ | would return a fmat filled with numbers from 10 to 1 in its first column, and 9 to 0 in the second. | ||
** Apply | ** Apply | ||
− | *** $x | + | *** $x - the value of currently processed element of fmat |
− | *** $self | + | *** $self - reference to currently processed fmat |
− | ** | + | ** Find |
+ | *** $x - the value of currently processed element of fmat | ||
+ | *** $i - the index of currently processed element of fmat | ||
+ | *** $self - reference to currently processed fmat | ||
− | + | with $i growing row by row in a column and continuing from the beginning of the next column as if the columns were placed on after another in a single vector. | |
* FVEC | * FVEC | ||
** Fill | ** Fill | ||
− | *** $idx | + | *** $idx - the index number of currently processed element of a fvec |
− | *** $self | + | *** $self - reference to currently processed fvec |
** Apply | ** Apply | ||
− | *** $x | + | *** $x - the value of currently processed element of fvec |
− | *** $self | + | *** $self - reference to currently processed fvec |
− | |||
− | |||
− | |||
[[Category:expr Class]] [[Category:Expressions]] [[Category:ftm.mess]] [[Category:ftm.object]] | [[Category:expr Class]] [[Category:Expressions]] [[Category:ftm.mess]] [[Category:ftm.object]] | ||
[[Category:FTM Documentation]] | [[Category:FTM Documentation]] | ||
[[Category:FAQ]] | [[Category:FAQ]] |
Revision as of 09:50, 7 September 2010
This is a simple explanation for the use of the EXPR class. See also Expression_Class_Arguments in the FAQ.
Creation - Elements of the call
An EXPRession is a sentence which can be applied to the content of an FMAT or an FVEC. It can be created in an ftm.object (class expr) or in an FTM message box ftm.mess. In both case, it shares the same syntax:
- it starts with expr
- it is then followed by any code that could be entered into a ftm.mess but it needs to be surrounded by simple quotes ' '
- to be evaluated the code has to be encapsulated within parenthesis (just like any code in ftm.mess)
For example, the expression returning the value 1 is
expr '(1)'
Expression returning a tuple {1 2 3} could be:
expr '1 (1 + 1) (1 + 2)'
But the following expression returns just 3:
expr '1, (1 + 1), (1 + 2)'
Although all of the the comma separated parts are executed, only the last one i returned. So in the following example, after first execution the value returned would be 9:
expr '($var += 10), ($var - 1)'
where var is a global variable which was initialized to 0. Since both parts are executed, var is first incremented by 10, but only the result of ($var - 1) is returned. After second execution the returned value would be 19, after third 29, and so on.
You can use all the FTM Functions available to create your expression. Moreover, $1, $2, etc will be replaced by values passed to your instance of expr.
For example, the expression returning the sinus of its input is
expr '(sin $1)'
There are two places you can create a new expr instance:
- within a ftm.object
as shown in the ftm.class.expr.maxhelp document. On the left, you put the expression, on the right its name, the latter being the way to call the expression
- within ftm.mess
(new expr '(sin $1)')
where it can be evaluated using its eval method:
((new expr '(sin $1)') eval $pi)
pi being a local variable passed as first argument to the expression (replacing $1).
or passed as an argument to a method to another object, like in the following example:
((new fmat 10 3) fill (new expr '(random 1 3)'))
Scope of variables
In the previous example, you could be tempted to use $1 and $2 to choose the boundaries of the random function within the expression but the variables $1 and $2 would not refer to the ftm.mess inputs, but to the expression arguments. We therefore need to pass those input variables (local to the ftm.mess) as arguments to our newly created instance of expr by placing them directly after expr statement:
((new fmat 10 3) fill (new expr '(random $1 $2)') $1 $2)
In the case our expression has been previously defined in a ftm.object and named for example randomize the above could look like this:
((new fmat 10 3) fill $randomize $1 $2)
Special Variables
Depending of context of use, an expr instance has access to some specific variables
- FMAT
- Fill
- $row - the row number of currently processed element of a fmat
- $col - the col number of currently processed element of a fmat
- $self - reference to currently processed fmat
- Fill
For example:
((new fmat 10 2) fill (new expr '(($self rows) - $row - $col)'))
would return a fmat filled with numbers from 10 to 1 in its first column, and 9 to 0 in the second.
- Apply
- $x - the value of currently processed element of fmat
- $self - reference to currently processed fmat
- Apply
- Find
- $x - the value of currently processed element of fmat
- $i - the index of currently processed element of fmat
- $self - reference to currently processed fmat
- Find
with $i growing row by row in a column and continuing from the beginning of the next column as if the columns were placed on after another in a single vector.
- FVEC
- Fill
- $idx - the index number of currently processed element of a fvec
- $self - reference to currently processed fvec
- Fill
- Apply
- $x - the value of currently processed element of fvec
- $self - reference to currently processed fvec
- Apply