Giac API reference

Librairie C++ de calcul formel/ C++ symbolic computation library

Modérateur : xcasadmin

Lephe
Messages : 5
Inscription : sam. déc. 29, 2018 8:44 am

Giac API reference

Message par Lephe » mer. janv. 02, 2019 5:12 pm

First steps here, hope I'm doing things right! :D

I'd like to use Giac as a library in a project that involves basic computer algebra. You could say I have the specification of a minimal computer algebra language, and I want to interpret it while using Giac functions to do the calculations. Using the interactive shell is not really an option because inter-process communication is involved and the output is plain text, not structured.

So I browsed the different books available on the software's page, downloaded the source code and dived into the headers, but so far I did not find a centralized reference of the library's API. As I have only used Xcas occasionally on graphing calculators, I don't have a good understanding of the big picture yet. ^^

I would like to know if such a reference exists, and in case it doesn't, how I can search for functions relevant to a given task such as "factor a polynomial" without getting completely lost.

Thanks for taking the time to read this,
Lephe' :)

parisse
Messages : 5731
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Giac API reference

Message par parisse » mer. janv. 02, 2019 6:24 pm

A first example: https://www-fourier.ujf-grenoble.fr/~pa ... st-example
This HTML file is a good abstract on how to start using giac. Basically, there is a generic data type, C++ type gen, that contains all kind of objects. You can call (almost) all Xcas user functions by a C++ call to the same function name with an _ prefix, e.g. _factor corresponds to Xcas factor. If an Xcas command requires several arguments, group them with a call to makesequence (for example _integrate(makesequence(e,x,0,1),&ct) will integrate e with respect to x from 0 to 1).
Most C++ functions require a last argument that is a pointer to the context (this make it possible to run multi-threaded application without bad interaction between contexts). Just declare once a context variable, and pass a pointer to it as last argument.

Lephe
Messages : 5
Inscription : sam. déc. 29, 2018 8:44 am

Re: Giac API reference

Message par Lephe » mer. janv. 02, 2019 8:59 pm

Thanks for your answer!

I tried out the example program, which was the most precise thing I'd found. I was able to play around with a couple of functions, evaluate formally and with floating-points. Everything interacts very nicely with the input/output formatting! :lol:

I think that I'll be able to pull off whatever required to evaluate expressions in my mini-language. May I know what the purpose of the level argument in eval() is?

Something probably more involved which I need is to define variables to later refer to them, which I would do in Giac like this, for instance:

Code : Tout sélectionner

0>> f(x):=x+1
1>> f(2)
I looked up the definition of the context object but found nothing relevant in the class itself. Could you point me to the appropriate methods?

Thanks again, this is helping me out a lot. :)

parisse
Messages : 5731
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Giac API reference

Message par parisse » mer. janv. 02, 2019 9:14 pm

Lephe a écrit :
mer. janv. 02, 2019 8:59 pm
think that I'll be able to pull off whatever required to evaluate expressions in my mini-language. May I know what the purpose of the level argument in eval() is?
Evaluation level, i.e. number of recursive replacements of identifiers. If for example you define a:=b+1 then b:=c+2 evaluating a at level 1 returns b+1, at level 2 (c+2)+1.
Something probably more involved which I need is to define variables to later refer to them, which I would do in Giac like this, for instance:

Code : Tout sélectionner

0>> f(x):=x+1
1>> f(2)
You can either eval a gen constructed from a string "f(x):=x+1" or call sto like in sto(1,x,&ct) if x is a gen containing an identifier (defining a function without the parser is a bit more complex than a more common variable because you must create a symbolic of root node at_program).
I looked up the definition of the context object but found nothing relevant in the class itself. Could you point me to the appropriate methods?
It's more a struct than a class, there is no method (except constructor, destructor and clone() to handle memory transparently), just data fields. They are not protected but it's probably not a good idea to try to modify them directly, it's better to modify the context by assigning variables or calling status change functions like complex_mode(true,&ct) if you want to switch to complex mode instead of real mode.

Lephe
Messages : 5
Inscription : sam. déc. 29, 2018 8:44 am

Re: Giac API reference

Message par Lephe » mer. janv. 02, 2019 9:38 pm

Alright, I checked out sto() and managed to store pretty much anything in a gen declared as a name. Awesome! :lol:

Then I tried to define a function as you said (pretty much what I had attempted before):

Code : Tout sélectionner

gen f("f(x):=x^2-1", &ct);
gen y("f(2)", &ct);
eval(y, 1, &ct); /* f(2) */
This doesn't work (I suppose f is considered unknown?), however evaluating f before evaluating y works:

Code : Tout sélectionner

gen f("f(x):=x^2-1", &ct);
eval(f, 1, &ct);
gen y("f(2)", &ct);
eval(y, 1, &ct); /* 3 */
Is there an internal reason for that? Is this the proper way to go to manipulate functions? (And if yes, is there other types of data that could be a concern?)

I apologize if these simple questions already have an answer somewhere. :oops:

parisse
Messages : 5731
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Giac API reference

Message par parisse » jeu. janv. 03, 2019 6:55 am

The reason behind is that your gen f is not the variable f but the expression 'f:=x->x^2-1'. You should define the function in the evalation context with eval(gen("f(x):=x^2-1",&ct),1,&ct); then define the gen f as gen f("f",&ct); then eval it with f=eval(f,1,&ct);
Now f(2,&ct) will work (or f of any evaluated gen).

Répondre