First time here? Check out the FAQ!

Ask Your Question
1

How can I make a python function return a polynomial?

asked 8 years ago

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?

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 8 years ago

slelievre gravatar image

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:

Preview: (hide)
link
0

answered 8 years ago

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.

Preview: (hide)
link

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: 8 years ago

Seen: 1,150 times

Last updated: Aug 17 '16