Karim Belabas on Sat, 06 Oct 2012 17:56:31 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Comparing two integers in âincorrect type in comparisonâ according to GP. |
* Ewan Delanoy [2012-10-06 16:48]: > can anyone explain how to fix the code below. If I understood correctly, what happens that although > I know that the polcoeffâs I compute are integers on the examples I chose, the interpreter does not > and complains just because they *might* be non-integers. Should I use some kind of âconversion to intâ? It depends on variable priorities, but in most settings they *are* non-integers (at least in the stable branch 2.5.*; in the testing branch, the situation is different): (17:42) gp > example_test=decompose(a1) *** at top-level: example_test=decompose(a1) *** ^------------- *** in function decompose: ...3,pair=temp[k];if(pair[2]>0,positive_part=con *** ^-------------------- *** _>_: incorrect type in comparison. *** Break loop: type 'break' to go back to GP break> pair[2] 0 break> type(pair[2]) "t_POL" break> variable(pair[2]) a1 As a polynomial in variable a2, we have a1 = Pol(0,'a1)*a2^1 + Pol(1,;a1)*a2^0 The simplest solution is to replace temp = simplify(temp); > aa_array=['a1,'a2,'a3] > > decompose(expr)={ > local(positive_part,negative_part,k,pair); > temp=vector(2,k,[k,polcoeff(expr,1,aa_array[k])]); > positive_part=[]; > negative_part=[]; > for(k=1,3,\ > pair=temp[k];\ > if(pair[2]>0,positive_part=concat(positive_part,[pair]););\ > if(pair[2]<0,negative_part=concat(negative_part,[pair]););\ > ); > return([positive_part,negative_part]) > } > > example_test=decompose(a1) Further remarks: - temp was probably meant to be local() as well. - This code will break again later because temp[k] is undefined for k = 3. - The \ at end-of-line could be omitted - You could use lists to simplify your code positive_part = List(); ... listput(positive_part, pair); Cheers, K.B. -- Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17 Universite Bordeaux 1 Fax: (+33) (0)5 40 00 69 50 351, cours de la Liberation http://www.math.u-bordeaux1.fr/~belabas/ F-33405 Talence (France) http://pari.math.u-bordeaux1.fr/ [PARI/GP] `