Reputation: 181
I am writing a Python module using SageMath9.4. Basically, I want to import this module into Jupyterlab Notebooks (running a SageMath 9.4 kernel) to do calculations etc.
Here is the start of it:
class Coxeter_System:
'''This class defines the standard root system associated to an abstract Coxeter group.'''
def __init__(self, coxeter_matrix):
'''Sets up a Coxeter system and root system. At this stage, limited to up to rank 7.
'''
def set_up_coefficient_space(coxeter_matrix):
'''Sets up a polynomial ring a Free module over a polynomial ring quotiented by the minimal polynomials of the
non-rational cos(pi/m_ij) values.
This is so roots can be compared using an abstract free module rather than over reals'''
A = coxeter_matrix
k = len(A.rows())
# Get the cos(pi/m_ij) which are irrational
non_rational_angles = [x for x in [cos(pi/x) for x in set(A[i,j] for i in range(0,k) for j in range(0,k))] if x not in QQ]
However, when I open another Jupyterlab session, import the Python module and try to create an instance of the object "Coxeter_System", I get the following error (I have tried to do from math import cos
both from the notebook where I want to import the module to, and in the module itself, but I still get the same error.
Any help would be greatly appreciated!):
NameError Traceback (most recent call last)
<ipython-input-6-6b542b6cb042> in <module>
----> 1 W = c.Coxeter_System(Matrix([[Integer(1),Integer(3),Integer(4)],[Integer(3),Integer(1),Integer(3)],[Integer(4),Integer(3),Integer(1)]]))
~/coxeter_groups.py in __init__(self, coxeter_matrix)
60 return matrix(R,k,B)
61
---> 62 R = set_up_coefficient_space(coxeter_matrix)
63 A = coxeter_matrix
64 k = len(A.rows())
~/coxeter_groups.py in set_up_coefficient_space(coxeter_matrix)
17
18 # Get the cos(pi/m_ij) which are irrational
---> 19 non_rational_angles = [x for x in [cos(pi/x) for x in set(A[i,j] for i in range(0,k) for j in range(0,k))] if x not in QQ]
20
21 # sort the irrational values of cos(pi/m_ij) in ascending order
~/coxeter_groups.py in <listcomp>(.0)
17
18 # Get the cos(pi/m_ij) which are irrational
---> 19 non_rational_angles = [x for x in [cos(pi/x) for x in set(A[i,j] for i in range(0,k) for j in range(0,k))] if x not in QQ]
20
21 # sort the irrational values of cos(pi/m_ij) in ascending order
NameError: name 'cos' is not defined
Upvotes: 1
Views: 5125
Reputation: 220
For most IDEs you will have to import some of the most popular math packages,
# Example:
import numpy as np
import math as m
import sagemath as sm
numpy and math you will have no problem using it with a simple import numpy
in Google Colab or most popular IDEs.
SageMath, previously known as Sage, is a computational algebraic system (CAS) that stands out for being built on mathematical and contrasted packages such as NumPy, Sympy, PARI/GP o Máxima.
In the case of sagemath, inside jupyter you will have to do some extra steps.
To install the sagemath package we must do a pip:
!pip install sagemath
The image shows the confirmation within Jupyter
# import and add the alias s
import sagemath as s
Yo use it we place the alias "s" in front of the function
s.cos(s.pi/x)
If sagemath does not always work correctly.
We know the following: "The philosophy of SageMath is to use existing open-source libraries wherever they exist. Therefore, it uses many libraries from other projects."
For these mathematical functions you can use numpy:
import numpy as np
np.cos(np.pi/x)
It will not fail and also in Jupyter you do not need additional installation.
I hope I've been useful.
Upvotes: 0
Reputation: 109
Just use math.cos
instead of using cos after from math import cos
e.g. math.cos(pi/x)
import math
math.cos(pi/x)
Upvotes: 3