Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sagemath as a python library

Tags:

sage

Is it possible to import the sagemath functions inside a python session?

What I wish to do, from a user perspective is something like:

>>> import sage
>>> sage.kronecker_symbol(3,5)  # ...or any other sage root functions

instead of accessing kronecker_symbol(3,5) from a sagemath session. If possible, it would be very handy, as would allow to embed all the functionalities of sagemath within the python world.

like image 540
SeF Avatar asked Apr 24 '20 12:04

SeF


People also ask

Is SageMath a library?

SageMath consists of a collection of mathematical software and a core library bundling the functionality of these components into one consistent experience. Additionally to that it provides a framework to express mathematical calculations and a library of mathematical algorithms.

Does SageMath use Python?

SageMath is a free open-source mathematics software system licensed under the GPL. It builds on top of many existing open-source packages: NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R and many more. Access their combined power through a common, Python-based language or directly via interfaces or wrappers.

What is SageMath?

SageMath (previously Sage or SAGE, "System for Algebra and Geometry Experimentation") is a computer algebra system (CAS) with features covering many aspects of mathematics, including algebra, combinatorics, graph theory, numerical analysis, number theory, calculus and statistics.


2 Answers

Importing SageMath functions in a Python session

There are several ways to achieve that.

SageMath from the operating system's package manager

Some operating systems have Sage packaged natively, for example Arch Linux, Debian, Fedora, Gentoo, NixOS, and their derivatives (Linux Mint, Manjaro, Ubuntu...).

See the dedicated "Distribution" page on the Sage wiki:

  • SageMath distribution and packaging

If using one of those, use the package manager to install sage or sagemath and then the Sage library will be installed on the system's Python, and in that Python it will become possible to do things like

>>> from sage.arith.misc import kronecker
>>> kronecker(3, 5)
-1

Another option is to use a cross-platform package manager such as Conda, Guix and Nix. These should work on most Linux distributions and macOS. Yet another option would be to run a Docker container.

I will detail the Conda case below.

SageMath with Conda

Install Sage with Conda and you will get that.

Instructions are here:

  • SageMath installation: install from conda-forge

and start by installing a Conda distribution, either Miniconda, Minimamba or Anaconda, and then creating a sage conda environment.

Once a sage conda environment is installed, activate it:

conda activate sage

With that sage conda environment active, run

python

and importing the sage module or importing functions such as kronecker from that module should work.

like image 80
Samuel Lelièvre Avatar answered Oct 19 '22 01:10

Samuel Lelièvre


This is a complementary answer for those who fail to use some SageMath function that is not compatible with Python syntax.

For example;

from sage.all import *

F = GF(2)
R.<x> = k[]

This will give an error on R.<x> = k[] since it is not a valid Python syntax. So how to solve this issue?

SageMath parses the SageMath syntax then use Python. One needs to use preparse to see the actual command.

sage: preparse('R.<x> = k[]')
"R = k['x']; (x,) = R._first_ngens(1)"

So replace the line, done!

from sage.all import *

F = GF(2)
R = k['x']; (x,) = R._first_ngens(1)

Unfortunately one must use SageMath to identify these.

like image 36
kelalaka Avatar answered Oct 19 '22 01:10

kelalaka