Expression & Syntax - VirtualRaven/mathlibra GitHub Wiki
Abstract
This page provides an overview of Mathlibra's syntax and it's types using and example based approach. It contains information directed both to the end-users and developers.
Some examples
Before describing details, lets showcase some of the valid expression
67*2+1-2
An simple arithmetic expression4*sin(3.14)
Function callsqrt sin PI/2
Nested function call2*(3+1)
Using parentheses
Well not really anything special, but mathlibra does also support implicit multiplication. So let's shorten it a bit
84sin(4)
2(3+1)
Note:
The expression 1 2 3
will be treated as implicit multiplication between the three numbers. Thus 1*2*3
which
equals 6
.
The first can be expression can be further rewritten as:
84 sin 4
So we don't have to write those parentheses, a third variant is also possible:
84sin 4
I would argue against this, as I believe this is less clear than the others.
Variables are also supported
x=43-1
y=2x-1
5(y-1)
4sin(PI)
sin 2PI
sin2PI
It is also possible declare matrices and vectors
v=[1,2,3]
A row vectorv=[1|2|3]
A column vectorI=[1,0,0|0,1,0|0,0,1]
A 3 by 3 identity matrice
Scalar and matrice multiplication
2*[1,2,3]
Scalar multiplication2[1,2,3]
Same as above[3,1,2^4]*[7|5|1]
Vector multiplication[3,1,2^4][7|5|1]
Same as above
Strings are created by writing "Hello world"
Values
Values is any constant representing a number, matrice, vector or string.
Notes: Understanding values further than the definition given above is only important for plugin creators and mathlibra contributors. All valid expression results in an value being created. For example 2*3
takes to values 2 and 3 and creates an new value 6. Variables are not values but are implicitly converted to values in all contexts requiring it. For example, 2*x
where x has the value 5 is evaluated to 10. Here the multiplication operator takes two values
and returns an new value. The x is implicitly converted to the value 5. In the expression x=2
the assignment operator '=' takes an left hand-side variable and an right hand-side value. Thus in this context variables are not implicitly converted to values.
Numbers
Numbers in mathlibra is an type of value which represents points on the real line. These are represented by floating point doubles.
All expressions below are valid numbers.
3
0.2
.3
4.2e2
Scientific notation23e-4
Scientific notation
Fun fact: .
by itself happens to be a valid number and equal to 0
.
Matrices
Matices is an different type of value which contains several numbers in a two dimensional grid.These are created using the bracket syntax seen in the example above. The pipe character "|" is used as an new line sign inside the brackets. Matrices initialized with the bracket syntax enforce that each row is of equal length.
To get the size of a matrix use the size function, for example
size([1,0,0|0,0,1])
which returns [2,3]
which in turn tells us the matrix has 2 rows and 3 columns.
Note: The matrix [1]
represents a number that is equal to 1
.
Variables
Variables can store any type of value and is created using the assignment operator "=" which takes an variable as an left hand side argument and a expression on the right hand side.
Example:
x=2
x=sin(2)
Variables can also represent constant values. These works exactly like variables but their value can not be changed using the assignment operator. These constant values are set by the host application.
Note: Variables are not defined until assigning them an value. Evaluating expressions containing undefined variables results in an error.
Note: Both constant values and variables can be set and changed by the host application using the functions included in the external API
Strings
Variables is a type which stores text which is created using using the following syntax "hello world"
This section will be expanded in the future
###Functions
Functions takes one or more value(s) and returns a new one. Each argument is delimited by comma, and the end
of the argument list is denoted by a space or an closing parenthesis, depending on if a opening parenthesis was used.
The following list is examples of function calls, note that the function func
is not defined by default in mathlibra.
sin 2PI
which is equal tosin(2*PI)
asin sin 2PI
which is equal toasin(sin(2*PI))
sin PI +1
which is equal tosin(PI)+1
note that the space afterPI
denotes the end of the argument list.sin(2 +1)
which is equal tosin(2+1)
note that the space after2
doesn't denote the end of the argument list, as a opening parenthesis was used.func 2,3
which is equal tofunc(2,3)
but not equal tofunc 2 ,3
which isfunc(2),3
Functions mainly fall into three main categories.
- Built in - functions that are defined by Mathlibra
- Plugin - functions that are defined by an external plugin
- User specified - functions which are defined by the end user
The last type is created using the function definition operator ":" and defines an function in terms of x. For example
f:x
defines the identity function f(x)=xf:sin(x)+2
defines the function f(x)=sin(x)+2
User defined functions are in contrast to the other types limited to only taking one argument.
Note: There exists an certain ambiguity in some expression due to the support for implicit multiplication. The expression f(1+2)
can mean to different things depending on context. If the function f exists the expression will be interpreted as an function call with 1+2
as it's argument. On the other hand if f is an variable f(1+2)
will be interpreted as f*(1+2)
. If f is neither an defined function nor an variable f will be treated as undefined variable and thus the second case still applies.