SuivantPrec.Bas prec.BasNiv. sup.

4.5 Examples of programs 

4.5.1 User language 

Find the matrix of a quadratic form, return 0 if expression is not a quadratic form. Maple syntax :
 quad:=proc(q,x)
   local fonc,fonc2,fonc3,mat,_i,j,k;
   n:=nops(x);
   mat:=matrix(n,n,0);
   fonc:=q;
   for _i from 1 to n do
    fonc:=subs((x[_i])=0,fonc)
   od;;
   if fonc<>0 then RETURN(0) fi;;
   for _i from 1 to n do
     fonc:=q;
     fonc:=diff(fonc,x[_i]);
     for j from 1 to n do
       fonc:=subs((x[j])=0,fonc)
     od;;
     if fonc<>0 then RETURN(0) fi;
   od;;
   for _i from 1 to n do
     fonc:=diff(q,x[_i]);
     for j from 1 to n do
       fonc1:=diff(fonc,x[j]);
       for k from 1 to n do
         fonc2:=diff(fonc1,x[k]);
         if fonc2<>0 then RETURN(0) fi;
       od;
     od;
   od;;
   for _i from 1 to n do
     for j from 1 to _i do
       fonc:=diff(q,x[_i]);
       fonc:=diff(fonc,x[j]);
       mat[_i,j]:=fonc/2;
       mat[j,_i]:=fonc/2
    od;
                                                                  
                                                                  
   od;;
   RETURN(mat);
 end;
Test with q=x^2+2*y^2+2*x*y and q+1 and q*x. Show the translation in mupad, TI89, xcas syntax.

4.5.2 Standalone program using the library 

 // -*- compile-command: "g++ -g essai.cc -lgiac -lgmp" -*-
 #include<giac/giac.h>
 using namespace std;
 using namespace giac;
 
 int main(){
   gen g;
   cout << "Enter an expression: ";
   cin >> g;
   g=eval(g);
   cout << "Factorization of " << g << " is " << factor(g) << endl;
 }
Once compiled (see commandline above), run it with ./a.out.

Debugging is easy with gdb and the following macro to print giac objects (in .gdbinit) :
 echo Defining v as print command for giac types\n
 define v
 print ($arg0).dbgprint()
 end
 ; $ suppress this line (emacs latex colorization bug)
The main C++ type of giac is the gen class for generic object storage. It consists mainly of a short int, the type of the object, another short int the "subtype" (e.g. lists, sets or sequences have the same type but different subtypes), and an anonymous union that is either the object itself if fixed-size (16 bits integers or double) or a pointer to a referenced-counted object. More info on types are in the headers dispatch.h and gen.h. Typically a code slice could be :
 if (g.type==_VECT){
   vecteur & v = *g._VECTptr;
   int s=v.size();
   for (int i=0;i<s;++i)
     cout << i << ":" << factor(v[i]) << endl;
 }
Many types covered by gen are derived from the STL (Standard Template Library), e.g. vecteur is a typedef for std::vector<gen>, they should therefore be straightforward to use. User functions C++-equivalent have most of the time the same name preceded by _ (for non-unary functions, arguments are grouped in a vecteur before the function call, it appears always as a unary gen'-->gen function).

4.5.3 Dynamic modules 

Writing a dynamic module requires in addition the declaration of user functions, so that they can be called inside an xcas session after an insmod call. For example, a module to have French names for the user language control words, is in the directory src, file progfr.cc. SuivantPrec.Bas prec.HautNiv. sup.