Memento: Programmation
Elementary commands
Commands are separated with ;
- Common way to store in a variable:
a:=1;
ans also: a,b,c:=1,2,3;
- Compute without printing the answer
:;
a:=2^10000:;
-
purge(a,b)
(free the variables a and b)
Except the storing, the default programation syntax of giac/xcas is like the C language
Functions
- In a program, variables that are not explicitely declared with the
local
instruction are global.
- local variables must be initialised. So to une 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';//ou bien: 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);
}
}
Tests
- 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 ;}
Main loops
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 [ , , ]
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)
Notions avancées
- Affectation 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