(Functions, Tests, Loops, Lists and Matrices, Advanced)

Memento: Programmation

Elementary commands


Commands are separated with ; In giac/xcas, except the storage syntax, the default syntax for functions, loops and tests is like in the C language

Functions

  1. In a program, variables that are not explicitely declared with the local instruction are global.
  2. 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.
  3. Blocs of code are in { }. One can use several lines in a same bloc.
  4. A program always return the last evaluation. The return command is optional.
  5. Please do not use i as a variable name in loops because it is reserved for the complex number square root of -1.

Tests

Main loops


NB: Les parenthesis and accolades in for, while are mandatory.

Sequences ( , , ) Lists [ , , ] and Matrices

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

Lists [ , , ] are objets between brackets and separated by commas. They are ordered and no bracket is simplified. They are used for vectors and matrices.

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

Advanced Notions

  • 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