Ask Your Question
0

Sagemath in multiple files

asked 1 year ago

dozius gravatar image

updated 1 year ago

Greetings!

I am migrating my applications from "pure python" to sagemath. If I use any sagemath functions in python modules, they dont work... I tried the "from sage.all import *" , but it works only partially.

e.g. I can use x = var('x') but not R.<x> = QQ[]

I tried to write my code in multple notebooks and import them as modules in the main application - with import_ipynb, but changes are recognized only with kernel restart (even with using autorelod). So this approach is not very efficient for development.

Is there any efficient way to split my app in multiple files/modules? or it has to be written in a single notebook? (we talk about thousands of lines).

Thank you

Preview: (hide)

Comments

This is because of the preparser, which is turning some sage-specific constructions into correct python code. See https://doc.sagemath.org/html/en/refe...

FrédéricC gravatar imageFrédéricC ( 1 year ago )

If you name your files with extension “.sage” instead of “.py”, Sage should preparse them automatically.

John Palmieri gravatar imageJohn Palmieri ( 1 year ago )

Alternatively, you can attach myfile.sage

Max Alekseyev gravatar imageMax Alekseyev ( 1 year ago )

Thanks everyone. You have been very helpful. Really appreciated!

dozius gravatar imagedozius ( 1 year ago )

1 Answer

Sort by » oldest newest most voted
0

answered 1 year ago

eric_g gravatar image

Building upon @FrédéricC 's comment, here is how to convert SageMath's syntax R.<x> = QQ[] into Python code to be written in a file. In a SageMath session, type

preparse("R.<x> = QQ[]")

This reveals that R.<x> = QQ[] is a shortcut for the following sequence of two Python commands:

R = QQ['x']
(x,) = R._first_ngens(1)

These two lines are to be included in your Python file instead of R.<x> = QQ[].

Preview: (hide)
link

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: 1 year ago

Seen: 175 times

Last updated: Oct 31 '23