NumExp can be called with 3 executables:
To quit text-based interface, numexp-client or numexpc, just press Control-D.
When you execute numexpc or numexp-client for the first time, you can get something like :
$ numexp-client Numexp 0.16.1 (c)1998-2007 The NumExp Team ::> sin[2] ans = sin[2] ::> eval(ans) IDL:Numexp/EvalError:1.0: Fonction sin non définie ou fonction employée avec un mauvais nombre de paramètres
or like :
$ numexpc numexp-kernel-Message: nxp_kernel_init numexp-kernel-Message: L'ouverture du fichier « /home/david/.numexprc » a échoué : Aucun fichier ou répertoire de ce type
So, things begin badly. The second case asks for a file .numexprc in the user directory.
So, here is an example of file .numexprc you can give :
[ import["stdlib"] ]
The NumExp's stdlib is a very important library that contains many functions, like sin, cos, exp…
This file is loaded when NumExp starts, so here the stdlib will be loaded.
In NumExp, there are two modes: eval or simplify.
By default, numexp-client works in simplify mode whereas numexpc works in eval mode.
In gNumexp, you can choose what mode is the default one by clicking the View menu and selecting or note the simplification item.
You can always force one mode with the two following functions :
numexp> 2.3+1.2 ans = 3.5
numexp> 1/2+3/5 ans = 1.1
If we want to simplify this result :
numexp> simplify[1/2+3/5] ans = 11/10
Variables must be defined :
numexp> a eval error: undefined variable 'a'
numexp> a=2 ans = 2
numexp> a/7 ans = 0.2857142857142857
The star is not needed to enter a product:
numexp> 2 3 7 ans = 42
Arithmetic on integers are not approximated:
numexp> 2^1000 ans = 107150860718626732094842504906000181056140481170553360744375038837035105 112493612249319837881569585812759467291755314682518714528569231404359845775746 985748039345677748242309854210746050623711418779541821530464749835819412673987 67559165543946077062914571196477686542167660429831652624386837205668069376
numexp> 50!+1 ans = 30414093201713378043612608166064768844377641568960512000000000001
If, in an operation, we use floating points numbers, NumExp works the same in eval or simplify mode.
numexp> 2.3+1.2 ans = 3.5
numexp> 2.3+1/3 ans = 2.6333333333333333
numexp> 1/2+3/5 ans = 11/10
Variables don't have to be defined to be used :
numexp> a ans = a
numexp> 2a+b-a^2 ans = -a ^ 2 + b + 2 * a
numexp> a=2 ans = 2
numexp> a/7 ans = 2/7
numexp> 3(a+2)^2-4 ans = 44
In NumExp, we have
This is just to satisfy mathematicians, engineers and physicians ; not totally, we know that mathematicians define often j such that
But this is wrong in NumExp.
So we can enter any operation with i or j:
numexp> simplify[(2+i/3)^18] ans = -128112059869885/387420489 + 809201139076/14348907j
numexp> simplify[(2+j/3)^18] ans = -128112059869885/387420489 + 809201139076/14348907j
numexp> eval[(1+i)^2] ans = 2j
NumExp can load and execute scripts written in the NumExp language.
Open your favorite text editor, enter
print["Hello!\n"]
Save this file under hello.nxp
Then under the numexp-core, enter
load["hello.nxp"]
Like many others programming languages, we can enter blocks of instructions in NumExp
Each instruction in a block must be finished by a new line or a semicolon.
numexp> a=2; ++a ans = 3
If you create a program, you'll have to markup blocks, it'll be do using brackets.
Be careful, even with brackets, each expression must be finished by a new line or a semicolon
The following code will lead to an error:
[ a=2 b=3 a+b]
because of the last line.
Also the following example gives error
[a=2; b=3; a+b]
Whereas those examples work:
[a=2; [a=2; b=3;a+b;] b=3 a+b ]
The if instruction works with the following syntax:
if a>0 then [ print["a is greater than 0\n"] ] else [ print["a is less than 0\n"] ]
We don't have to enter the else instruction each time but the conclusions after then and after else must be blocks delimited by brackets.
This example *doesn't work*:
if a>0 then print["a is greater than 0\n"]
For simple cases, we have a second syntax that works like this:
if[a>0, print["a > 0"], print["a <= 0"]]
the first argument is the condition. If this condition is true, the second argument is executed else the third is.
We can also enter this:
if[a>0, print["a > 0"]]
So here there is no else condition.
The while instruction works almost like if
So here are examples:
a=0 while a<10 do [ ++a ]
This can also be written like:
a=0; while[a<10, ++a];
The for loop cannot do many things at the moment, it will be extended later.
We have this:
for x in {1...3} do [
print[x]
]
or this:
for[x, {1...3}, print[x]]