Ask Your Question
0

Pycharm + sage incorrect computation

asked 2019-10-25 22:05:49 +0100

Sobieg gravatar image

updated 2019-10-27 09:57:27 +0100

FrédéricC gravatar image

I am using sage with Pycharm with sage by choosing Python2.7 from sage /bin directory like in guide in question 39742.

In computation on finite field there is bug or other incorrect behavior:

p = 5256107634024827443646803157596584915370839195839782329597601469354483229307063
j = 3611062252397503822281535488379195436991347721427144349104935225639485573271142
K = GF(p)
a = K((3 * j) / (1728 - j))

a is equals 5256107634024827443646803157596584915370839195839782329597601469354483229307059, but in fact it should be 1674511800600631022371777328069227143110125063664501651628290807871952520681596.

In stand-alone sage from terminal it works correctly. What should I do for working with sage from Pycharm with correct computation.

Latest version Mac OS, sage 8.9 from Homebrew, latest version Pycharm.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2019-10-27 07:35:06 +0100

vdelecroix gravatar image

The preparser seems turned off in Pycharm. In a plain console you got

sage: p = 5256107634024827443646803157596584915370839195839782329597601469354483229307063
sage: j = 3611062252397503822281535488379195436991347721427144349104935225639485573271142
sage: K = GF(p)
sage: K((3 * j) / (1728 - j))
1674511800600631022371777328069227143110125063664501651628290807871952520681596

versus

sage: preparser(False)
sage: p = 5256107634024827443646803157596584915370839195839782329597601469354483229307063
sage: j = 3611062252397503822281535488379195436991347721427144349104935225639485573271142
sage: K = GF(p)
sage: K((3 * j) / (1728 - j))
5256107634024827443646803157596584915370839195839782329597601469354483229307060

In the console (and when preparser is on) integers are considered as Sage integers for which division produces rationals

sage: preparser(True)
sage: a = 3
sage: type(a)
<type 'sage.rings.integer.Integer'>
sage: 1 / 3
1/3
sage: type(1 / 3)
<type 'sage.rings.rational.Rational'>

whereas in Python (or when the preparser is off) these are Python integers for which division is the floor division or Euclidean division

sage: preparser(False)
sage: a = 3
sage: type(a)
<class 'int'>
sage: 1 / 3
0
sage: type(1 / 3)
<type 'int'>

If you want to fix the behavior in Pycharm you can for example write K(3 * j) / K(1728 - j) that will avoid the Python integer division.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-10-25 22:05:04 +0100

Seen: 359 times

Last updated: Oct 27 '19