(Functions, Tests, Loops, Lists and Matrices, Advanced)
Memento: Programmation
Elementary commands
Commands are separated with ;
- Common way to store in a variable:
a:=1;
and also: a,b,c:=1,2,3;
- Compute without printing the answer
:;
a:=2^10000:;
-
purge(a,b)
(free the variables a and b)
In giac/xcas, except the storage syntax, the default syntax for functions, loops and tests is like in the C language
- In a program, variables that are not explicitely declared with the
local
instruction are global.
- local variables must be initialised. So to use a formal symbol in a function, either use a non initialised global variable, or do as in example
rfact
below.
- Blocs of code are in
{ }
. One can use several lines in a same bloc.
- A program always return the last evaluation. The
return
command is optional.
- Please do not use
i
as a variable name in loops because it is reserved for the complex number square root of -1.
f(x,y):=x+y;
( Synonym: f:=(x,y) -> x+y;
)
rfact(a,b,c):={
local d,r1,r2,x;
d:=b^2-4*a*c;
x:='x';//synonym: assume(x,symbol)
if(d<0){
return a*x^2+b*x+c;
}
else{
r1:=(-b-sqrt(d))/(2*a);
r2:=(-b+sqrt(d))/(2*a);
return a*(x-r1)*(x-r2);
}
}
- Comparaison between x et y
-
x==y
, x!=y
( equal, different. Synonyms: x=y
, x<>y
)
-
x<y
, x<=y
, x>y
, x>=y
-
and
, or
, not
(synonyms: &&
, ||
, !
)
if( condi ){ instructions ;}
if( condi ){ instructions ;}
else{ instructions ;}
NB: Les parenthesis and accolades in for, while are mandatory.
for( ini; condi; incr ){ instructions ;}
for(j:=0; j<10;j++){ print(j);}
(we print integers from 0 to 9 with a step of 1)
for(j:=10; j>1;j:=j-3){ print(j);}
(we print integers > 1 starting from 10 with a step of -3. ie: 10,7,4)
while(condi){ instructions ;}
j:=2;while(j<10){j:=j*j ;}
Sequences and lists are indexed from 0 (except in mode maple). Use brackets to get an element:
a[0]
is the first element of the list or sequence a
Sequences ( , , )
are ordered objects separated with commas. Les parenthesis are simplified. Operators +,* ... are really not the usual operations of vectors
Below, a
, b
and c
are the same.
-
a:=0,1,2,3,4,5;
-
b:=(0,1),((2,3),4,5);
-
c:=seq(j,j=0..5);
To start with an empty sequence, use the keyword: NULL
-
d:=NULL;d:=d,1;d:=d,2;
Lists [ , , ]
are objets between brackets and separated by commas. They are ordered and no bracket is simplified. They are used for vectors and matrices.
Below, a1
et a2
are identical. They are vectors. They differ from b
who is a matrix with 2 rows and 3 colons.
-
a1:=[0,1,2,3,4,5];
-
a2:=[seq(j,j=0..5)];
-
b:=[[0,1,2],[3,4,5]];
-
a1+2*a2; ker(b);
-
op(a1)
is usefull to remove the external brackets of a list. -
-
[seq(j*j, j=a1)];
(is the list of j*j when j moves in the list a1
)
-
a3:=newList(10^7);
(to efficiently create a list of zeros)
A Matrix [[ , , ],[ , ,]]
is defined by the list of its rows surrounded by brackets. Each row is a list of entries separated by comma and is surrounded by brackets. In the following the matrices m1 and m2 are identical. They have 2 rows and 5 columns
-
m1:=[[0,1,2,3,4],[0,3,6,9,12]];
m2:=[a1,a3];
(where a1,a3 are defined above)
The matrices m3 and m4 are identical and they have 1 row and 5 columns. They differ from m5. The matrix m5 have 1 colomn and 5 rows. None of the mi is a vector. v4 est the vector [30,90].
m3:=[[0,1,2,3,4]]
m4:=[a1]
m5:=[[0],[1],[2],[3],[4]];
v4:=m1*a1;
Many commands (Ex ker) return a list of vectors. In the following example, N
is a basis of the kernel of m1
. N[0]
is the first vector of this basis, N[1] the next one...
Warning: N
will print these vectors as rows.
N:=ker m1;
- Storing with
:=
is by values. With it, data is copied even with big lists or matrices.
To be more efficient, there is also an affectation by memory address:
=<
is the affectation by reference:
Ex: a:=[1,2,3];b=<a;b[1]=<0;
now a
is also aussi [1,0,3]
. On the other side, if we had done b[1]:=0
, a new list b
would have been created and a
wouldn't have changed
-
#
is usefull to de create a variable from a string:
Ex1: "a1"
is a string and #"a1"
is a variable.
Ex2: S:=sum(#("a"+j),j=0..9);normal(S^5);
expand the following: (a0+a1+...+a9)^5.
- Programming syntaxes in maple style:
The following syntaxes should be used only in mode maple mode. If not one must for instance know that in the while
example, condi can't be between parenthesis.
if condi then instrutions fi
for j from 1 to 5 do instruc od
while condi do instrutions od