Ask Your Question
0

Sagemath in multiple files

asked 2023-10-31 12:17:23 +0100

dozius gravatar image

updated 2023-10-31 12:18:00 +0100

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

edit retag flag offensive close merge delete

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 ( 2023-10-31 13:38:19 +0100 )edit

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

John Palmieri gravatar imageJohn Palmieri ( 2023-10-31 16:01:59 +0100 )edit

Alternatively, you can attach myfile.sage

Max Alekseyev gravatar imageMax Alekseyev ( 2023-10-31 18:31:35 +0100 )edit

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

dozius gravatar imagedozius ( 2023-11-04 07:58:20 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2023-10-31 15:11:57 +0100

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[].

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: 2023-10-31 12:17:23 +0100

Seen: 156 times

Last updated: Oct 31 '23