User Tools

Site Tools


reference_guide:math_expression_language:mathexpressionlanguage

ME-L Core Functions

Math Expression Language (short:ME-L) supports numerous forms of functional, logical and vector processing semantics and has been extended to support thinkingParticles OpenVDB operations.

In it's core, ME-L is based on the highly optimized ExprTk library which, in addition, has been highly modified to enhance OpenVDB and thinkingParticles operations. If you want to learn more about the inner workings, check the following web page: http://www.partow.net/programming/exprtk/

Specific thinkingParticles Constants, Variables and Functions

Constants.

t - current animation animation time in seconds

dt - delta time from one to the next sub sample in seconds

valueoff - return value used to to turn off a value (e.g. removing voxel)

Variables

vp - vector position in voxel space (vp[0] represents x, vp[1] represents y, vp[2] represents z)

op - vector position in object (particle) space

wp -vector position in world space

bp - vector position relative to bounding box space x,y,z normalized to 0 - 1. 0,0,0 representing bottom left corner

cbp - vector position in center of the bounding box space, x,y,z where the center is 0,0,0 extending to 1,1,1 and -1,-1,-1 respectively.

fv - scalar value from- or written to the voxel. Example: fv := 1 will set all voxels to value of 1.0. 

vv - equivalent to fv but for vector values. This is used for voxel values within a vector grid (color, uvw, or velocity).

Example:
vv[0] := 0.5; vv[1] := 0.5; vv[2] := 0.5; sets the voxel of a color grid to r,g,b gray of 0.5  

Functions

noise(x,y,z) - 3D perlin noise function

noise(x,y) - 2D perlin noise

ncellular(x,y,z) - 3D cellular

nchip(x,y,z) - 3D cellular in chip mode

nvoronoi(x,y,z) - 3D voronoi    

rndseed(s) -set random seed
    
rnd01() - random value between  0.0 to 1.0

rnd11() - random value between -1.0 to 1.0

rndxx(x,y) - random value between  x to y

lowhighcut(v, low, high) - convert range between low and high to 0.0 and 1.0    

ndistance(vector pos1, vector pos2, norm_radius) - distance between (position1 - position2).length / norm_radius

distance(vector pos1, vector pos2) - distance between (position1 - position2).length

dir_length(invector, outdirection) -  outdirection will receive the return vector (direction)

Example:
var length;
var dir[3];
length = dir_length(invec, dir);

sqrdistance(vector pos1, vectorpos2) - distance between (position1 - position2).length_sqr

Note: 
Positions can be 1D,2D, 3D or even multidimensional.

Example:
pos1[1] is 1D,
pos1[2] is 2D
pos1[3] is 3D


ME-L supports three fundamental types which can be used freely in expressions. The types are as follows:

  1. Scalar
  2. Vector
  3. String


Scalar 
The scalar type  is a singular  numeric value. 


Vector 
The vector is a fixed size sequence of contiguous scalar  values. A  vector  can be  indexed  resulting in  a  scalar value.  Operations between a vector and scalar will result in a vector with a size  equal to that  of the  original vector,  whereas operations  between vectors will result in a  vector of size equal  to that of the  smaller of the two. In both mentioned cases, the operations will occur element-wise.


String 

The string type is a variable length sequence of 8-bit chars.  Strings can be  assigned and concatenated to  one another,  they can  also be manipulated via sub-ranges using the range definition syntax.  Strings however can not interact with scalar or vector types.


Arithmetic & Assignment Operators

Operator Definition
+ Addition between x and y.  (eg: x + y)
- Subtraction between x and y.  (eg: x - y)
* Multiplication between x and y.  (eg: x * y)
/ Division between x and y.  (eg: x / y)
% Modulus of x with respect to y.  (eg: x % y)
^ x to the power of y.  (eg: x ^ y)
:= or  =  Assign the value of x to y. Where y is either a variable or vector type.  (eg: y := x)
+= Increment x by the value of the expression on the right hand side. Where x is either a variable or vector type. (eg: x += abs(y - z))          
-= Decrement x by the value of the expression on the right  hand side. Where x is either a variable or vector type. (eg: x[i] -= abs(y + z))      
*= Assign the multiplication of x by the value of the expression on the right hand side to x. Where x is either a variable or vector type.    (eg: x *= abs(y / z))              
/= Assign the division of x by the value of the expression on the right-hand side to x. Where x is either a  variable or vector type.  (eg: x[i + j] /= abs(y * z)) 
%= Assign x modulo the value of the expression on the right hand side to x. Where x is either a variable or vector type.  (eg: x[2] %= y ^ 2)  


Comparison Operations

Operator Definition
== True only if x is strictly equal to y. (eg: x == y)  
<> or != True only if x does not equal y. (eg: x <> y or x != y)
<   True only if x is less than y. (eg: x < y) 
True only if x is less than or equal to y. (eg: x ⇐ y)
> True only if x is less than y. (eg: x < y)
>= True only if x greater than or equal to y. (eg: x >= y)  


Boolean Operations

Operator Definition
true True state or any value other than zero (typically 1)
false False state, value of exactly zero.
and Logical AND, True only if x and y are both true (eg: x and y).
mand Multi-input logical AND, True only if all inputs are true. Left to right short-circuiting of expressions. (eg: mand(x > y, z < w, u or v, w and x))  
mor Multi-input logical OR, True if at least one of the inputs are true. Left to right short-circuiting of expressions.  (eg: mor(x > y, z < w, u or v, w and x)) 
nand Logical NAND, True only if either x or y is false. (eg: x nand y)
nor    Logical NOR, True only if the result of x or y is false (eg: x nor y)
not Logical NOT, Negate the logical sense of the input. (eg: not(x and y) == x nand y)
or Logical OR, True if either x or y is true. (eg: x or y)
xor Logical XOR, True only if the logical states of x and y differ.  (eg: x xor y)
xnor Logical XNOR, True iff the bi-conditional of x and y is satisfied.  (eg: x xnor y)
& Similar to AND but with left to right expression short circuiting optimization.  (eg: (x & y) == (y and x))  
| Similar to OR but with left to right expression short circuiting optimization.  (eg: (x or y) == (y or x))  


General Math Functions

Operator Definition
abs Absolute value of x.  (eg: abs(x))
avg Average of all the inputs.(eg: avg(x,y,z,w,u,v) == (x + y + z + w + u + v) / 6)
ceil Smallest integer that is greater than or equal to x.
clamp Clamp x in range between r0 and r1, where r0 < r1. (eg: clamp(r0,x,r1))
equal Equality test between x and y using normalized epsilon
erf Error function of x.  (eg: erf(x))
erfc    Complimentary error function of x.  (eg: erfc(x))
exp e to the power of x.  (eg: exp(x))
expm1 e to the power of x minus 1, where x is very small. (eg: expm1(x))
floor Largest integer that is less than or equal to x.(eg: floor(x))
frac Fractional portion of x.  (eg: frac(x))
hypot Hypotenuse of x and y (eg: hypot(x,y) = sqrt(x*x + y*y))
iclamp Inverse-clamp x outside of the range r0 and r1. Where r0 < r1. If x is within the range it will snap to the closest bound. (eg: iclamp(r0,x,r1)    
inrange In-range returns 'true' when x is within the range r0 and r1. Where r0 < r1.  (eg: inrange(r0,x,r1)
log Natural logarithm of x.  (eg: log(x))
log10 Base 10 logarithm of x.  (eg: log10(x))
log1p Natural logarithm of 1 + x, where x is very small.(eg: log1p(x))
log2 Base 2 logarithm of x.  (eg: log2(x))
logn Base N logarithm of x. where n is a positive integer. (eg: logn(x,8))
max Largest value of all the inputs. (eg: max(x,y,z,w,u,v))
min Smallest value of all the inputs. (eg: min(x,y,z,w,u))
mul Product of all the inputs. (eg: mul(x,y,z,w,u,v,t) == (x * y * z * w * u * v * t))
ncdf Normal cumulative distribution function.  (eg: ncdf(x))
not_equal Not-equal test between x and y using normalized epsilon
pow x to the power of y.  (eg: pow(x,y) == x ^ y)
root Nth-Root of x. where n is a positive integer. (eg: root(x,3) == x^(1/3))
round Round x to the nearest integer.  (eg: round(x))
roundn Round x to n decimal places  (eg: roundn(x,3)) where n > 0 and is an integer.(eg: roundn(1.2345678,4) == 1.2346)
sgn Sign of x, -1 where x < 0, +1 where x > 0, else zero. (eg: sgn(x))
sqrt Square root of x, where x >= 0.  (eg: sqrt(x))
sum Sum of all the inputs.(eg: sum(x,y,z,w,u,v,t) == (x + y + z + w + u + v + t))
swap
Swap the values of the variables x and y and return thecurrent value of y.  (eg: swap(x,y) or x ⇔ y)
trunc Integer portion of x.  (eg: trunc(x))

 

Trigonometry Functions

Operator Definition
acos Arc cosine of x expressed in radians. Interval [-1,+1] (eg: acos(x)
acosh Inverse hyperbolic cosine of x expressed in radians. (eg: acosh(x))
asin Arc sine of x expressed in radians. Interval [-1,+1]
asinh Inverse hyperbolic sine of x expressed in radians. (eg: asinh(x))
atan Inverse hyperbolic tangent of x expressed in radians. (eg: atanh(x))
cos Cosine of x.  (eg: cos(x))
cosh    Hyperbolic cosine of x.  (eg: cosh(x))
cot Cotangent of x.  (eg: cot(x))  
csc Cosecant of x.  (eg: csc(x))
sec Secant of x.  (eg: sec(x))
sin Sine of x.  (eg: sin(x))  
sinc Sine cardinal of x.  (eg: sinc(x))
sinh Hyperbolic sine of x.  (eg: sinh(x))    
tan Tangent of x.  (eg: tan(x))
tanh Hyperbolic tangent of x.  (eg: tanh(x))
deg2rad Convert x from degrees to radians.  (eg: deg2rad(x))
deg2grad Convert x from degrees to radians.  (eg: deg2grad(x))
rad2deg Convert x from radians to degrees.  (eg: rad2deg(x))
grad2deg Convert x from radians to degrees.  (eg: grad2deg(x))

 

String Functions

Operator Definition
==, ⇐, >=,
< , >
All common equality/inequality operators are applicable  to strings and are applied in a case sensitive manner. In the following example x, y and z are of type string. (eg: not( (x ⇐ 'AbC') and ('1x2y3z' <> y)) or (z == x)
in True only if x is a sub-string of y. (eg: x in y or 'abc' in 'abcdefgh')
like True only if the string x matches the pattern y. Available wildcard characters are '*' and '?' denoting zero or more and zero or one matches respectively. (eg: x like y or 'abcdefgh' like 'a?d*h')
ilike True only if the string x matches the pattern y in a case insensitive manner. Available wildcard characters are '*' and '?' denoting zero or more and zero or one matches respectively. (eg: x ilike y or 'a1B2c3D4e5F6g7H' ilike 'a?d*h')
[r0:r1] The closed interval [r0,r1] of the specified string. eg: Given a string x with a value of 'abcdefgh' then:
1. x[1:4] == 'bcde'
2. x[ :5] == x[:10 / 2] == 'abcdef'
3. x[2 + 1: ] == x[3:] =='defgh'
4. x[ : ] == x[:] == 'abcdefgh'
5. x[4/2:3+2] == x[2:5] == 'cdef'

Note: Both r0 and r1 are assumed to be integers, where r0 ⇐ r1. They may also be the result of an expression, in the event they have fractional components truncation will be performed. (eg: 1.67 –> 1)
:= Assign the value of x to y. Where y is a mutable string or string range and x is either a string or a string range. eg:
1. y := x
2. y := 'abc'
3. y := x[:i + j
4. y := '0123456789'[2:7]
5. y := '0123456789'[2i + 1:7]
6. y := (x := '0123456789'[2:7])
7. y[i:j] := x
8. y[i:j] := (x + 'abcdefg'[8 / 4:5])[m:n]

Note: For options 7 and 8 the shorter of the two ranges will denote the number characters that are to be copied.
+    Concatenation of x and y. Where x and y are strings or string ranges. eg  
1. x + y
2. x + 'abc'
3. x + y[:i + j]
4. x[i:j] + y[2:3] + '0123456789'[2:7]
5. 'abc' + x + y
6. 'abc' + '1234567'
7. (x + 'a1B2c3D4' + y)[i:2j]
+= Append to x the value of y. Where x is a mutable string and y is either a string or a string range. eg:
1. x += y
2. x += 'abc'
3. x += y[:i + j] + 'abc'
4. x += '0123456789'[2:7]
Swap the values of x and y. Where x and y are mutable strings.  (eg: x ⇔ y)
[] The string size operator returns the size of the string being actioned.
eg:
1. 'abc'[] == 3
2. var max_str_length := max(s0[],s1[],s2[],s3[])
3. ('abc' + 'xyz')[] == 6
4. ( ('abc' + 'xyz')[1:4])[] == 4
sin Sine of x.  (eg: sin(x))
sinc Sine cardinal of x.  (eg: sinc(x))
sinh Hyperbolic sine of x.  (eg: sinh(x))
tan Tangent of x.  (eg: tan(x))
tanh Hyperbolic tangent of x.  (eg: tanh(x))
deg2rad Convert x from degrees to radians.  (eg: deg2rad(x))
deg2grad Convert x from degrees to gradians.  (eg: deg2grad(x))
rad2deg Convert x from radians to degrees.  (eg: rad2deg(x))
grad2deg Convert x from gradians to degrees.  (eg: grad2deg(x))

 

Flow Execution Functions

Operator Definition
if If x is true then return y else return z.
eg:
1. if (x, y, z)
2. if ( (x + 1) > 2y, z + 1, w / v)
3. if (x > y) z;
4. if (x ⇐ 2*y) { z + w };  
if-else he if-else/else-if statement. Subject to the condition branch the statement will return either the value of the consequent or the alternative branch.
 eg:
1. if (x > y) z; else w;
2. if (x > y) z; else if (w != u) v;
3. if (x < y) { z; w + 1; } else u;
4. if ( (x != y) and (z > w))
{
     y := sin(x) / u;
     z := w + 1;
}
else if (x > (z + 1))
{
    w := abs (x - y) + z;
    u := (x + 1) > 2y ? 2u : 3u;
}
switch The first true case condition that is encountered will determine the result of the switch. If none of the case conditions hold true, the default action is assumed as the final return value. This is sometimes also known as a multi-way branch mechanism.
eg:
switch
{
     case x > (y + z) : 2 * x / abs(y - z);
     case x < 3       : sin(x + y);
     default          : 1 + x;
}                                        
while The structure will repeatedly evaluate the internal statement(s) 'while' the condition is true. The final statement in the final iteration will be used as the return value of the loop.
eg:
while ( (x -= 1) > 0)
{
     y := x + z;
    w := u + y;
}                          
repeat/until The structure will repeatedly evaluate the internal statement(s) 'until' the condition is true. The final statement in the final iteration will be used as the return value of the loop.
eg:
repeat
y := x + z;
w := u + y;
until ( (x += 1) > 100)
for The structure will repeatedly evaluate the internal statement(s) while the condition is true. On each loop iteration, an 'incrementing' expression is evaluated. The conditional is mandatory whereas the initializer and incrementing expressions are optional.
eg:
for (var x := 0; (x < n) and (x != y); x += 1)
{
     y := y + x / 2 - z;
    w := u + y;
}                      
break

break[]
Break terminates the execution of the nearest enclosed loop, allowing for the execution to continue on external to the loop. The default break statement will set the return value of the loop to NaN, where as the return based form will set the value to that of the break expression.
eg:
while ( (i += 1) < 10)
{
     if (i < 5)
     j -= i + 2;
     else if (i % 2 == 0)
       break;
     else
       break[2i + 3];
}                                            
continue Continue results in the remaining portion of the nearest enclosing loop body to be skipped.
eg:
for (var i := 0; i < 10; i += 1)
{
   if (i < 5)
     continue;
   j -= i + 2;
}          
return Return immediately from within the current expression. With the option of passing back a variable number of values (scalar, vector or string).
eg:
1. return [1];
2. return [x, 'abx'];
3. return [x, x + y,'abx'];
4. return [];
5. if (x < y)
return [x, x - y, 'result-set1', 123.456];
else
return [y, x + y, 'result-set2'];
? Ternary conditional statement, similar to that of the above denoted if-statement. eg:
1. x ? y : z
2. x + 1 > 2y ? z + 1 : (w / v)
3. min(x,y) > z ? (x < y + 1) ? x : y : (w * v)
~ Evaluate each sub-expression, then return as the result the value of the last sub-expression. This is sometimes known as multiple sequence point evaluation.
eg:
 ~(i := x + 1, j := y / z, k := sin(w/u)) == (sin(w/u)))
~{i := x + 1; j := y / z; k := sin(w/u)} == (sin(w/u)))
[*] Evaluate any consequent for which its case statement is true. The return value will be either zero or the result of the last consequent to have been evaluated.   eg:
[*]
{
   case (x + 1) > (y - 2)    : x := z / 2 + sin(y / pi); 
   case (x + 2) < abs(y + 3) : w / 4 + min(5y,9);
   case (x + 3) == (y * 4)   : y := abs(z / 6) + 7y;
}
[] The vector size operator returns the size of the vector being actioned.
eg:
1. v[]
2. max_size := max(v0[],v1[],v2[],v3[]

                                     

 

Expression Syntax

Multi-Statement Expressions


Expressions in ME-L can be  comprised of one or more statements. The following are  two examples of expressions stored in variables, the  first is a single statement and the second a multi-statement  expression:

  z := x + y;

   var temp := x; 
   x := y + z;    
   y := temp;  

In a  multi-statement expression, the final statement will determine the overall result of the expression. In the following multi-statement expression, the result of the expression when evaluated will be '2.3',
which will also be the value stored in the 'y' variable.

   z := x + y;
   y := 2.3;

As  demonstrated  in  the  expression  above,  statements  within   an expression are  separated using the semi-colon  ';' operator.  In the event  two  statements are  not  separated by  a  semi-colon, and the implied multiplication  feature is  active (enabled  by default),  the compiler  will  assume  a multiplication  operation  between  the two statements.

In the following example we have a multi-statement expression composed of two variable definitions and initialization for variables x and  y and two seemingly separate mathematical operations.

   var x:= 2;
   var y:= 3;
   x + 1
   y * 2

However the result of  the expression will not  be 6 as may  have been assumed based on  the calculation of  'y * 2',  but rather the  result will be 8. This  is because the compiler  will have conjoined the  two mathematical statements into one  via a multiplication operation. The expression when compiled will actually evaluate as the following:

   var x:= 2;
   var y:= 3;
   x + 1 * y * 2;   // 2 + 1 * 3 * 2 == 8

Any valid statement will itself return a value.  This value can  further  be  used  in conjunction with other statements. This includes language structures such as if-statements, loops (for, while) and the switch statement. Typically the last statement executed in the given construct (conditional, loop etc), will be the value that is returned.

In the following example, the  return value of the expression  will be 11, which is the sum of the variable 'x' and the final value  computed within the loop body on its last iteration:

   var x := 1;
   x + for (var i := x; i < 10; i += 1)
       {
         i / 2;
         i + 1;
       }

Conditional Statements (If-Then-Else)

ME-L support two forms of conditional branching or  otherwise known as if-statements. The first form, is a  simple  function   based conditional  statement, that  takes exactly  three input  expressions: condition, consequent  and alternative.  The following  is an  example expression that utilizes the function based if-statement.

   x := if (y < z, y + 1, 2 * z)

In the  example above,  if the condition 'y< z'  is true, then the consequent 'y + 1' will be evaluated, its value will be  returned  and subsequently assigned to the  variable 'x'. Otherwise the  alternative '2 *  z' will  be evaluated  and its  value will  be returned. This is essentially the  simplest form of  an if-then-else statement. A simple variation of the expression where the value of the if-statement is used within another statement is as follows:

   x := 3 * if (y < z, y + 1, 2 * z) / 2

The second form of if-statement resembles the standard syntax found in most imperative languages. There are two variations of the statement:

   If-Statement
   If-Then-Else Statement

If-Statement

This version  of the  conditional statement  returns the  value of the consequent expression when the condition expression is true,  else it will return a quiet NaN value as its result.

   Example 1:
   x := if (y < z) y + 3;

   Example 2:
   x := if (y < z)
        {
          y + 3
        }

The two  example expressions  above are  equivalent. If  the condition 'y < z' is true, the  'x' variable will be  assigned the value of  the consequent 'y + 3', otherwise it  will be assigned the value of  quiet NaN.  As  previously  discussed,  if-statements  are  value  returning constructs, and if  not properly terminated  using a semi-colon,  will end-up  combining  with  the  next  statement  via a multiplication
operation. The following example will NOT result in the expected value of 'w + x' being returned:

   x := if (y < z) y + 3  // missing semi-colon ';'
   w + x

When the  above supposed  multi-statement expression  is compiled, the expression  will  have  a multiplication  inserted  between  the  two 'intended' statements resulting in the unanticipated expression:

   x := (if (y < z) y + 3) * w + x


The  solution  to  the  above situation  is  to simply  terminate the conditional statement with a semi-colon as follows:

   x := if (y < z) y + 3;
   w + x

If-Then-Else Statement

The second variation of  the if-statement is to  allow for the use  of Else and If-Else cascading statements. Examples of such statements are as follows:

Example 1 Example 2 Example 3
if (x < y)
z := x + 3;
else
y := x - z;
if (x < y)
 {  
    y := z + x;
    z := x + 3;
 }  
else
   y := x - z;  
if (x > y + 1)
  y := abs(x - z);
else
{
  y := z + x;
  z := x + 3;
};

 

Example 4 Example 5 Example 6
if (2 * x < max(y,3))
{
    y := z + x;
    z := x + 3;
}
else if (2y - z)
    y := x - z;  
if (x < y)
{  
    z := x + 3;
else if (2y != z)
    z := x + 3;
    y := x - z;
}
else
    x * x;
if (x < y or (x + z) > y)
{
    z := x + 3;
    y := x - z;
 }
else if (abs(2y - z) >= 3)
    y := x - z;
else
{
    z := abs(x * x); 
x * y * z;
};


In  the case  where  there  is no  final else  statement and  the flow through the conditional  arrives at this  final point, the  same rules apply to this form of if-statement as to the previous. That is a quiet NaN will be  returned as the  result of the  if-statement. Furthermore the same requirements of terminating the statement with a semi-colon apply.

Variables

Variables are of numeric type denoting a single value. They can be explicitly initialized to a value, otherwise they will be defaulted to zero. The following are examples of variable definitions:

   Initialize x to zero
       var x;

   Initialize y to three
       var y := 3;

   Initialize z to the expression
       var z := if (max(1, x + y) > 2, w, v);

Vector Definition

Vectors are arrays of a common numeric type. The elements in a  vector can be explicitly initialized, otherwise they will all be defaulted to zero.The following are examples of vector definitions:

   initialize all values to zero
       var x[3];

   initialize all values to zero
       var x[3] := {};

   initialize all values to given expression
       var x[3] := [123 + 3y + sin(w / z)];

   initialize the first two values, all other elements to zero
       var x[3] := { 1 + x[2], sin(y[0] / x[]) + 3 };

   initialize the first three (all) values
       var x[3] := { 1, 2, 3 };

   initialize vector from a vector
       var x[4] := { 1, 2, 3, 4 };
       var y[3] := x;

   initialize vector from a smaller vector
       var x[3] := { 1, 2, 3 };
       var y[5] := x;   // 1, 2, 3, ??, ??

   Non-initialized vector
       var x[3] := null; // ?? ?? ??

   Error as there are too many initializers
       var x[3] := { 1, 2, 3, 4 };

   Error as a vector of size zero is not allowed.
       var x[0];

String Definition

Strings are sequences comprised of 8-bit characters. They can only be defined with an explicit  initialization value. The following are examples of string variable definitions:

   Initialize to a string
       var x := 'abc';

   Initialize to an empty string
       var x := '';

   Initialize to a string expression
       var x := 'abc' + '123';

   Initialize to a string range
       var x := 'abc123'[2:4];

   Initialize to another string variable
       var x := 'abc';
       var y := x;

   Initialize to another string variable range
       var x := 'abc123';
       var y := x[2:4];

   Initialize to a string expression
       var x := 'abc';
       var y := x + '123';

   Initialize to a string expression range
       var x := 'abc';
       var y := (x + '123')[1:3];

Return Value

Variable and vector definitions have a  return value. In  the case of variable definitions, the value  to which the variable  is initialized will be returned. Where as for vectors, the value of the first element
(eg: v[0]) will be returned.

   8 == ( (var x := 7;) + 1)
   4 == (var y[3] := {4, 5, 6};)

Variable/Vector Assignment

The value of a variable can be assigned to a vector and a vector or a vector expression can be assigned to a variable.

Variable To Vector:
      Every element of the vector is assigned the value of the variable
      or expression.
      var x    := 3;
      var y[3] := { 1, 2, 3 };
      y := x + 1;

Vector To Variable:
      The variable is assigned the value of the first element of the
      vector (aka vec[0])
      var x    := 3;
      var y[3] := { 1, 2, 3 };
      x := y + 1;

Vector Math

ME-L provides  support  for various forms of vector oriented arithmetic, inequalities and processing. The various  supported pairs are as follows:

   vector and vector (eg: v0 + v1)
   vector and scalar (eg: v  + 33)
   scalar and vector (eg: 22 *  v)

The following is a list of operations that can be used in  conjunction with vectors:

   Arithmetic:       +, -, *, /, %
   Exponentiation:   vector ^ scalar
   Assignment:       :=, +=, -=, *=, /=, %=, ⇔
   Inequalities:     <, ⇐, >, >=, ==, =, equal
   Boolean logic:    and, nand, nor, or, xnor, xor
   Unary operations:
       abs, acos, acosh, asin, asinh, atan, atanh, ceil, cos,  cosh,
       cot, csc,  deg2grad, deg2rad,  erf, erfc,  exp, expm1, floor,
       frac, grad2deg, log, log10, log1p, log2, rad2deg, round, sec,
       sgn, sin, sinc, sinh, sqrt, swap, tan, tanh, trunc
   Aggregate and Reduce operations:
       avg, max, min, mul,  dot, dotk, sum, sumk,  count, all_true,
       all_false, any_true, any_false
   Transformation operations:
       copy, rotate-left/right, shift-left/right, sort, nth_element
   BLAS-L1:
       axpy, axpby, axpyz, axpbyz, axpbz

Note: When one of  the above  described operations is being performed between two  vectors, the operation will  only span  the size  of the smallest vector. The elements  of the  larger vector  outside of the range will  not be included. The  operation  itself will be processed element-wise over values the smaller of the two ranges.

The  following  simple  example  demonstrates  the vector processing capabilities by computing the dot-product of the vectors v0 and v1 and then assigning it to the variable v0dotv1:

   var v0[3] := { 1, 2, 3 };
   var v1[3] := { 4, 5, 6 };
   var v0dotv1 := sum(v0 * v1);

The following is a for-loop based implementation that is equivalent to the previously mentioned dot-product computation expression:

   var v0[3] := { 1, 2, 3 };
   var v1[3] := { 4, 5, 6 };
   var v0dotv1;

   for (var i := 0; i < min(v0[],v1[]); i += 1)
   {
     v0dotv1 += (v0[i] * v1[i]);
   }


Note: When  the aggregate or reduction  operations denoted  above are used  in conjunction with a  vector or  vector  expression, the return value is not a vector but rather a single value.

   var x[3] := { 1, 2, 3 };

   sum(x)      ==  6
   sum(1 + 2x) == 15
   avg(3x + 1) ==  7
   min(1 / x)  == (1 / 3)
   max(x / 2)  == (3 / 2)
   sum(x > 0 and x < 5) == x[]

 

©2024, cebas Visual Technology Inc.

reference_guide/math_expression_language/mathexpressionlanguage.txt · Last modified: 2024/01/24 18:34 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki