Ask Your Question
1

Getting my own module to work in Sage

asked 2018-02-20 21:54:48 +0100

trenzafeeds gravatar image

Hey everyone. I wrote a sage module that defines a couple functions that I'd like to be able to import into sage any time I want. From what I've researched, it seems the way to do this is save the file that defines the functions as a .py file, then put it into sage's version of python. I've done that, and sage imports the file just fine without errors, except when I attempt to call functions from the file in the sage prompt, it gets stuck on the first sage-specific function, in this case var(). I get this error message:

 sage: polyv1.sift(4, 3)

    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    <ipython-input-20-d64cf99d698f> in <module>()
    ----> 1 polyv1.sift(Integer(4), Integer(3))

    /usr/lib/sagemath/local/lib/python/polyv1.py in sift(r, s)

    NameError: global name 'var' is not defined

I've tried adding 'from sage.all import *' to the beginning of the file, and it doesn't create an error, but it also doesn't fix the problem. Am I missing something really easy? How do you get sage to recognize the sage functions?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-02-20 21:58:29 +0100

tmonteil gravatar image

You have to import what is used in the file. For this, there is a nice tool:

sage: import_statements("var")
from sage.calculus.var import var

then you just have to copy-paste from sage.calculus.var import var in the beginning of your file.

edit flag offensive delete link more

Comments

Unfortunately still getting the same error, any other ideas?

trenzafeeds gravatar imagetrenzafeeds ( 2018-02-20 22:20:34 +0100 )edit

Could you please provide your code so that we understand what happens ?

tmonteil gravatar imagetmonteil ( 2018-02-20 23:39:39 +0100 )edit

For sure. The entire document is pretty long, but I've taken what I think are the most important parts. Let me know if anything else seems important. Here's the header of the document (everything I'm importing):

from sage.calculus.var import var
import itertools
from sympy.utilities.iterables import multiset_permutations

and here's the beginning of the function that gives the above-mentioned error when I call it:

def sift(r, s):
    polynomial = 1

    p = r/2
    q = (s-1)/2
    poly_degree = 0

    #For x-terms
    for j in xrange(2, r+1):
        for i in xrange(1, j):
            term = (var("x" + str(j))-var("x" + str(i)))
            polynomial = polynomial *term
            poly_degree += 1

it's the first 'var("x" + str(j)) there that's giving me the error.

trenzafeeds gravatar imagetrenzafeeds ( 2018-02-21 00:01:00 +0100 )edit

This is all in the same file right? If the import succeeded (i.e. there's no ImportError) then you should have anything you imported defined globally (within the module that imported it). Beyond that, if you're still getting a NameError then there's something else strange in your code that would be hard to pinpoint without seeing all of it.

Iguananaut gravatar imageIguananaut ( 2018-02-21 14:23:28 +0100 )edit

Turns out it was a mistake that I was making, not a code problem! Sorry for troubling you guys, turns out that first answer was totally correct!

trenzafeeds gravatar imagetrenzafeeds ( 2018-02-21 16:04:13 +0100 )edit

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: 2018-02-20 21:54:48 +0100

Seen: 537 times

Last updated: Feb 20 '18