Documentation on using sage as a library?

49 views
Skip to first unread message

Kolen Cheung

unread,
Nov 17, 2018, 10:07:03 PM11/17/18
to sage-support

Hi, many of the texts and documentations assume one is using the SageMath kernel. So far I see one FAQ address using sage as a library.

But using sage as a library would certainly has a different behavior with sage itself. e.g. Rational('3 / 2') rather than 3 / 2, and displaying in latex seems more indirect:

from sage.misc.latex import MathJax
from IPython.display import HTML

MATHJAX = MathJax()

def print_latex(expression, engine=MATHJAX):
    return HTML(str(engine(expression)))

However I couldn’t find a way to show plots. e.g. plot results in Graphics object consisting of 1 graphics primitive and plot3d results in Graphics3d Object. How should these graphic objects be shown as images? The closet I got is .dumps or .matplotlib methods but they didn’t work.

In general, are there documentations for using sage as a python library, and are there any materials that help Python programmers to pick up sage? i.e. assuming one knows Python already, used to “Pythonism”, and the common Python stacks such as Matplotlib, etc.

Thanks.

Kolen Cheung

unread,
Nov 19, 2018, 7:39:41 AM11/19/18
to sage-support

Here’s the way to translate any Sage program to Python. From https://groups.google.com/d/msg/sage-support/ZtlmX3zE0b8/cdecLnbUAwAJ:

The key are these:

from sage.all import *

def sage_parse(expr):
    '''exec a sage expression in globals
    '''
    expr_str = preparse(expr)
    # print to show what's run
    print(expr_str)
    exec(expr_str, globals())

Example (taken from https://groups.google.com/d/msg/sage-support/ZtlmX3zE0b8/mqmG5KuiEgAJ):

Sage program:

R.<x> = QQ[]
K = R.fraction_field()
H.<i,j,k> = QuaternionAlgebra(K, -1, -1)
def Q(a, b, c, d): return H(a + b*i + c*j + d*k)
@cached_function
def P(n):
    return Q(x+1,1,1,1)*P(n-1) if n > 0 else Q(1,0,0,0)
def p(n): return P(n)[0].numerator().list()
for n in (0..20): print [n], p(n)

Python translation: (grey boxes are the stdouts)

from sage.all import *

def sage_parse(expr):
    '''exec a sage expression in globals
    '''
    expr_str = preparse(expr)
    # print to show what's run
    print(expr_str)
    exec(expr_str, globals())
sage_parse('R.<x> = QQ[]')
R = QQ['x']; (x,) = R._first_ngens(1)
K = R.fraction_field()
sage_parse('H.<i,j,k> = QuaternionAlgebra(K, -1, -1)')
H = QuaternionAlgebra(K, -Integer(1), -Integer(1), names=('i', 'j', 'k',)); (i, j, k,) = H._first_ngens(3)
def Q(a, b, c, d):
    return H(a + b * i + c * j + d * k)
@cached_function
def P(n):
    return Q(x + 1, 1, 1, 1) * P(n - 1) if n > 0 else Q(1, 0, 0, 0)
def p(n):
    return P(n)[0].numerator().list()
for n in range(21):
    print [n], p(n)
[0] [1]
[1] [1, 1]
[2] [-2, 2, 1]
[3] [-8, -6, 3, 1]
[4] [-8, -32, -12, 4, 1]
[5] [16, -40, -80, -20, 5, 1]
[6] [64, 96, -120, -160, -30, 6, 1]
[7] [64, 448, 336, -280, -280, -42, 7, 1]
[8] [-128, 512, 1792, 896, -560, -448, -56, 8, 1]
[9] [-512, -1152, 2304, 5376, 2016, -1008, -672, -72, 9, 1]
[10] [-512, -5120, -5760, 7680, 13440, 4032, -1680, -960, -90, 10, 1]
[11] [1024, -5632, -28160, -21120, 21120, 29568, 7392, -2640, -1320, -110, 11, 1]
[12] [4096, 12288, -33792, -112640, -63360, 50688, 59136, 12672, -3960, -1760, -132, 12, 1]
[13] [4096, 53248, 79872, -146432, -366080, -164736, 109824, 109824, 20592, -5720, -2288, -156, 13, 1]
[14] [-8192, 57344, 372736, 372736, -512512, -1025024, -384384, 219648, 192192, 32032, -8008, -2912, -182, 14, 1]
[15] [-32768, -122880, 430080, 1863680, 1397760, -1537536, -2562560, -823680, 411840, 320320, 48048, -10920, -3640, -210, 15, 1]
[16] [-32768, -524288, -983040, 2293760, 7454720, 4472832, -4100096, -5857280, -1647360, 732160, 512512, 69888, -14560, -4480, -240, 16, 1]
[17] [65536, -557056, -4456448, -5570560, 9748480, 25346048, 12673024, -9957376, -12446720, -3111680, 1244672, 792064, 99008, -19040, -5440, -272, 17, 1]
[18] [262144, 1179648, -5013504, -26738688, -25067520, 35094528, 76038144, 32587776, -22404096, -24893440, -5601024, 2036736, 1188096, 137088, -24480, -6528, -306, 18, 1]
[19] [262144, 4980736, 11206656, -31752192, -127008768, -95256576, 111132672, 206389248, 77395968, -47297536, -47297536, -9674496, 3224832, 1736448, 186048, -31008, -7752, -342, 19, 1]
[20] [-524288, 5242880, 49807360, 74711040, -158760960, -508035072, -317521920, 317521920, 515973120, 171991040, -94595072, -85995520, -16124160, 4961280, 2480640, 248064, -38760, -9120, -380, 20, 1]

Interested in adding this to the documentation / FAQ? It just wasn’t very clear in the FAQ.

Reply all
Reply to author
Forward
0 new messages