Le forum de XCAS

Xcas: un logiciel libre de calcul formel
Nous sommes actuellement le Lun Mai 21, 2018 1:14 am

Heures au format UTC




Publier un nouveau sujet Répondre au sujet  [ 4 messages ] 
Auteur Message
 Sujet du message: hashing polynomials
MessagePublié: Ven Avr 27, 2018 1:55 pm 
Hors-ligne

Inscrit le: Lun Avr 17, 2017 4:32 pm
Messages: 80
Hi,

Maybe this is a giacpy tip of the day, or maybe this is a question...

In python it would sometimes be beneficial if you could put some giacpy gen types into a dictionary or a set. As a simple example, consider a polynomial x^2+1. The following code will fail

Code:
from giacpy import giac
f=giac("x^2+1")
hash(f) # fails, thus..
s=set()
s.add(f) # also fails... and
d={}
d[f]="some dict value" # also fails


There is a workaround maybe. One could convert the polynomial (for the time being I am assuming univariate, I do not want to know what happens if I have more variables) into a list uniquely represented in giac.
Continuing with the code ...

Code:
t = tuple(int(i) for i in f.e2r())
s=set.add(t)
d[t]="some dict value"


Notice the following:
I need to convert the values in f.e2r() into int (because they are still gen types which are unhashable)
I cannot use e.g. str(f) instead of f.e2r() because I might have different ordering of the monomials in strings representing the same polynomial.

Do you think this is the best way I am handling this. So far this is my only solution.

Jose


Haut
 Profil  
 
 Sujet du message: Re:
MessagePublié: Lun Avr 30, 2018 7:39 am 
Hors-ligne

Inscrit le: Dim Mai 20, 2007 7:09 am
Messages: 1065
Localisation: Paris
Indeed I should have add a hash method but I don't know a hash function for a gen in giac.

Bernard, is there already some hash function in giac that I could use because I fear that:

Code:
    def __hash__(self):
        return hash(str(self))

would help but it could be very slow.


Haut
 Profil  
 
 Sujet du message: Re:
MessagePublié: Lun Avr 30, 2018 12:59 pm 
Hors-ligne

Inscrit le: Lun Avr 17, 2017 4:32 pm
Messages: 80
frederic han a écrit:
Indeed I should have add a hash method but I don't know a hash function for a gen in giac.

Bernard, is there already some hash function in giac that I could use because I fear that:

Code:
    def __hash__(self):
        return hash(str(self))

would help but it could be very slow.


I think using str is a bad idea e.g. for polynomials. Because converting a polynomial to string depends on the order for which the monomials occur. So two polynomials defined with different order of monomials will have a different hash value. For polynomial, I suggested e2r (which is invariant from the order of monomials).


Haut
 Profil  
 
 Sujet du message: Re:
MessagePublié: Lun Avr 30, 2018 6:41 pm 
Hors-ligne

Inscrit le: Mar Déc 20, 2005 4:02 pm
Messages: 4319
giac has C++ hash functions, but not exposed to the userspace because they work on monomial indices of multivariate polynomials, like
Code:
  class hash_function_object {
  public:
    size_t operator () (const index_t & v) const { return index_hash_function(v); }
    hash_function_object() {};
  };

Perhaps you can work with something like convert(e2r(3*x*y+2*y+1,[x,y]),list)? Back conversion with convert(,,polynom)


Haut
 Profil  
 
Afficher les messages publiés depuis:  Trier par  
Publier un nouveau sujet Répondre au sujet  [ 4 messages ] 

Heures au format UTC


Qui est en ligne ?

Utilisateurs parcourant actuellement ce forum : Aucun utilisateur inscrit et 2 invités


Vous ne pouvez pas publier de nouveaux sujets dans ce forum
Vous ne pouvez pas répondre aux sujets dans ce forum
Vous ne pouvez pas éditer vos messages dans ce forum
Vous ne pouvez pas supprimer vos messages dans ce forum
Vous ne pouvez pas insérer de pièces jointes dans ce forum

Rechercher pour:
Sauter vers:  
cron
Powered by phpBB® Forum Software © phpBB Group
Traduction réalisée par Maël Soucaze © 2009 phpBB.fr