Ask Your Question
1

How can I make a python function return a polynomial?

asked 2016-08-11 17:27:15 -0600

done_with_fish gravatar image

I have a file ~/foo.py

from sage.all import *


def my_func(n):
    var('t') 
    f(t) = t**n 
    return f(t)

But issuing sage: import foo raises the error SyntaxError: can't assign to function call

What am I doing wrong?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2016-08-16 17:57:58 -0600

You are using Sage-specific syntax f(t) = t**n.

In a .py file, you cannot do that.

You have to replace it with pure Python-compliant code.

To understand what code you need to use, do this:

sage: preparse('f(t) = t**n')
'__tmp__=var("t"); f = symbolic_expression(t**n).function(t)'

Now you know how to write your function:

def my_func(n):
    t = SR.var('t')
    f = symbolic_expression(t**n).function(t)
    return f(t)

See also these related Ask Sage questions:

edit flag offensive delete link more
0

answered 2016-08-11 19:32:56 -0600

calc314 gravatar image

You can save your file with the .sage extension and then load it in Sage using load('myfile.sage'). Then, your function should work as you have written it.

edit flag offensive delete link more

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: 2016-08-11 17:27:15 -0600

Seen: 318 times

Last updated: Aug 16 '16