Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand suspicious syntax application by the Sagemath library in Python

Tags:

I'm using Sagemath / Cocalc (feel free to run it in your browser, no sign-up no nothing required). As far as I understand, Sagemath is just a library on top of Python and the Sagemath notebook implicitly imports many things from the Sagemath library.

What puzzles me though is the following syntax:

R.<x,y> = AA[]
I = R.ideal(x^2 * y - 18,x * y^3  - 24, x * y - 6)
I.variety()

(This solves the system of polynomials x^2 * y - 18=0, x * y^3 - 24=, x * y - 6=0, and returns x=2,y=3, nice!)

Running type(AA), I see that it is

<class 'sage.rings.qqbar.AlgebraicRealField_with_category'>. 

Running type(R), I see that it is

<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain_with_category'>

Ok, b what does it mean in Python to use brackets [] at a class, i.e. AA[]?

What does it mean, in Python's syntax, to do R.<x,y>? Mind you, I haven't defined previously x and y as strings or anything, so this syntax seems very weird to me.

like image 223
user719220 Avatar asked May 26 '20 08:05

user719220


1 Answers

SageMath is built on top of Python, but it preparses input, and this is an example. Within SageMath:

sage: preparse('R.<x,y> = AA[]')
"R = AA['x, y']; (x, y,) = R._first_ngens(2)"
  • The preparse command tells you how Sage will convert the string before evaluating it as standard Python.
  • The __getitem__ method (which is what gets called when you do AA[args]) for rings like AA creates a polynomial ring with the named generators.
  • So R.<x,y> = AA[] creates a polynomial ring, and also defines R, x, and y as the ring and its two generators.

You can achieve the same thing by R.<x,y> = PolynomialRing(AA).

like image 186
John Palmieri Avatar answered Oct 12 '22 02:10

John Palmieri