using Giac in c++

Messages in english

Modérateur : xcasadmin

Répondre
lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

using Giac in c++

Message par lukamar » jeu. août 17, 2017 1:03 pm

When programming in c++ using Giac library, how do I create a new real symbolic variable (identifier) and make it constrained to some segment (which in Xcas would be done with 'assume' command)? I need to introduce several constrained variables and then solve a system of nonlinear but rational equations symbolically, with respect to the constraints. As I understand, Giac can do so by computing the Groebner basis. Is it the right way to use the usual function '_solve'?

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

Re: using Giac in c++

Message par parisse » ven. août 18, 2017 6:50 am

lukamar a écrit :When programming in c++ using Giac library, how do I create a new real symbolic variable (identifier)
For example gen g("ab");
and make it constrained to some segment (which in Xcas would be done with 'assume' command)?

Code : Tout sélectionner

intgab.h:  bool assume_t_in_ab(const gen & t,const gen & a,const gen & b,bool exclude_a,bool exclude_b,GIAC_CONTEXT);
I need to introduce several constrained variables and then solve a system of nonlinear but rational equations symbolically, with respect to the constraints. As I understand, Giac can do so by computing the Groebner basis. Is it the right way to use the usual function '_solve'?
_solve(makesquence(list_of_equations,list_of_unknowns),contextptr) should indeed do that

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: using Giac in c++

Message par lukamar » mer. août 23, 2017 9:18 am

How to get bounds assumed for a variable most easily? I know about function '_about', but I wonder if there is a more practical function.
Also, I noticed that if I set bounds to a symbol with 'assume_t_in_ab', they're erased after a call to '_solve' with that symbol as one of unknowns. How to make Giac stick to assumption? I need to call '_solve' several times, so now I have to use 'assume_t_in_ab' prior to every call.

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

Re: using Giac in c++

Message par parisse » mer. août 23, 2017 10:08 am

lukamar a écrit :How to get bounds assumed for a variable most easily? I know about function '_about', but I wonder if there is a more practical function.
Perhaps you will find find_range more convenient:
alg_ext.h: int find_range(const gen & g,vecteur & a,GIAC_CONTEXT);
Also, I noticed that if I set bounds to a symbol with 'assume_t_in_ab', they're erased after a call to '_solve' with that symbol as one of unknowns. How to make Giac stick to assumption? I need to call '_solve' several times, so now I have to use 'assume_t_in_ab' prior to every call.
Can you give an example? I tried assume(x>0); then after a call to solve, the assumption on x is kept.

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: using Giac in c++

Message par lukamar » mer. août 23, 2017 11:13 am

parisse a écrit :Can you give an example? I tried assume(x>0); then after a call to solve, the assumption on x is kept.
Yes, you're right. Minimal example works, I've probably overlooked something else.

But the following may be a bug: when I enter

Code : Tout sélectionner

assume(x>-pi/2 and x<pi/2);solve((x^2-x)/(1+tan(x)^2)=0,x)
I get list[-pi/2,0,1,pi/2], which contains -pi/2 and pi/2 not actually being allowed for x. Certainly, they shouldn't appear no matter the assumption because tan is not defined there.

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: using Giac in c++

Message par lukamar » mer. août 23, 2017 9:47 pm

What is the best way to check that a gen is rational expression (in several variables)? I see that in solve.cc there is a check for rationality in one place, but I fail to understand how it's done.

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

Re: using Giac in c++

Message par parisse » jeu. août 24, 2017 6:48 am

I guess you mean:this code slice:

Code : Tout sélectionner

    // check rational
    for (it=var.begin();it!=itend;++it){
      if (it->type!=_IDNT) // should not occur!
	return vecteur(1,gensizeerr(gettext("Bad var ")+it->print(contextptr)));
      vecteur l(rlvarx(eq,*it));
      if (l.size()>1)
	return vecteur(1,gensizeerr(gen(l).print(contextptr)+gettext(" is not rational w.r.t. ")+it->print(contextptr)));
    }
For each variable in var, It will compute the list of extended variables in eq depending on *it, if the size is != from 1, then eq is not rational w.r.t *it.
By extended variable, I mean x but also sin(x) (since sin(x) is not rational w.r.t. x and can not be reduced further).

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: using Giac in c++

Message par lukamar » mar. sept. 19, 2017 12:26 pm

Hi again,

what's the difference between 'is_zero' and 'is_exactly_zero' functions? Does the first one return true for numbers small enough? I wonder if I'm using them properly...

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

Re: using Giac in c++

Message par parisse » mar. sept. 19, 2017 1:11 pm

Yes, that's exactly the difference, is_zero treats numbers smaller than epsilon like 0, unlike is_exactly_zero.

lukamar
Messages : 331
Inscription : ven. juin 30, 2017 9:55 am
Localisation : Zagreb, Croatia

Re: using Giac in c++

Message par lukamar » mer. sept. 20, 2017 9:03 am

Thanks for the clarification. How does is_zero compare with epsilon as it doesn't get a pointer to context?

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

Re: using Giac in c++

Message par parisse » mer. sept. 20, 2017 9:30 am

is_zero has a pointer to the context, if you don't pass it, it will use the null context (default parameter).

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

Re: using Giac in c++

Message par parisse » mer. nov. 01, 2017 1:51 pm

I don't know, valgrind says:

Code : Tout sélectionner

==7664== Memcheck, a memory error detector
==7664== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==7664== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7664== Command: ./a.out
==7664== 
==7664== Invalid read of size 1
==7664==    at 0x5C312A0: giac::gen::gen(giac::gen const&) (in /usr/lib/libgiac.so.0.0.0)
==7664==    by 0x400C54: main (bug.cc:7)
==7664==  Address 0x8f460700008 is not stack'd, malloc'd or (recently) free'd
==7664== 
=
It's probably related to object loading...

Répondre