The
Expression Parser
If you need to calculate a matrix as
result of a complex expression or formula (several
functions
combined together), you have several choices:
- add the functions and the
intermediate and final result matrices one by one
to the project.
- build
your own template (probably using a script
language) that calculates the complex formula and make a function from
it.
- call an
external application through a special function, called adapter.
Adapters are not installed with Matrex, but can be installed
separately.
- use the Expression
Parser.
The last option is
usually the one
that requires the last of your time. The idea is to write the
expression as a text, to parse it and produce the
intermediate
and final functions and result matrices from it.
There is only one
constraint:
the expression parser can only parse formulas with functions that
return one and only one matrix
(most of the functions are like that).
Here is it
how it works.
We say that it is possible to write a
function as a text in the following way:
template_name(input_matrix1,
input_matrix2 .. input_matrixN, “parameter1”,
“parameter2”,
.. “parameterN”)
where:
- template_name is the name
of the template on the base of the function.
- input_matrix1 ..
input_matrixN are the input matrices of the
function, in the correct order.
- parameter1 ..
parameterN are the parameters
of the function, in the correct order. To distinguish them from the
input matrices, they need to be written in quotes.
For example:
plus(matrix1, matrix2)
or interval(“1”,
“100”, “1”)
The matrices in the expression are assigned to the input matrices of the template following their order: the 1st matrix to the 1st template's matrix, the 2nd to the 2nd and so on.
The expression is valid if there are enough matrices to cover the number of the template's mandatory input matrices.
The parameters in the expression are assigned to the parameters of the template following their order: the 1st parameter to the 1st template's p the 2nd to the 2nd and so on.
If
in the template there are a set of parameters that are optional, they
are not needed in the expression if they are at the end of the
template's parameter set.
For example, if the template has 5
parameters and parameter 2, 4 and 5 are optional, the expression needs
values for the parameters 1, 2, 3 (in the same order) but not for
parameters 4 and 5, since they are the last 2.
Since the input matrix of a function
can be the result of another one, we can combine the functions in an
expression, for example:
plus(log(matrix1), exp(matrix2))
Parsing this expression, the parser
produces the following items:
- one function
that is the one producing the final result of the expression (plus in
this case)
- one
matrix that is the final result of the expression
(the result of plus in this case)
- the intermediate
functions for
log and exp in a separate sub-package called im_<final function
name> (for example “im_plus”)
- the
intermediate matrices of
the expression, for log and exp, in a separate sub-package called
im_<result matrix name> (for example
“im_plus”)
Here is an example of
the output
matrices of the expression parser for the plus(log(matrix1),
exp(matrix2)) expression:
The
fact that the intermediate
functions and matrices are stored in separate sub-packages keeps the
project structure simple and efficient:
- Of the whole expression
you are normally only interested in the final result; so it
is good
that the intermediate matrices are in a sub-package that you can keep
closed.
- Nevertheless
it is good to be able to look at the intermediate matrices if you are
not sure about the final result or want to have some more information
about the process that produced it.
To start the
expression
parser right
click with the mouse on the functions
tree and select the Expression
Parser menu, which shows the expression parser wizard.
The
parser is organized as a wizard,
therefore is divided in steps.
In each
step the
parser shows
you the current status of the parsing and/or tells you to enter or
choose some parameter.
Click on the Next
button to
go to the
next step, Back to go to the previous.
Sometimes it is not possible to go to
the next step because some of the data entered in the current step or
in a previous step is wrong. In this case you can fix the wrong
data, then go again to the next steps.
When you open the wizard, you are on
the first step. The wizard has also step 2 (raw tree), step 3 (refined tree), step 4 (files), step 5 (summary).