Ask Your Question
2

Prevent sage from calculating a function?

asked 2015-12-23 19:15:59 +0100

Jason021 gravatar image

updated 2015-12-23 19:17:49 +0100

So I have some code where I would like to formally plug in a number value into a symbolic function without having sage calculate it.

For example, if I have;

var(x)
f(x) = x^2 
c = 2

And then I put in f(c), I'd like it to return 2^2 and not 4. I imagine there must be a way to do this, but I can't seem to find it with googling. Any suggestions would be appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-12-28 09:09:59 +0100

slelievre gravatar image

In Sage, what you want is called "holding" the operation.

Find more about it by searching the word "hold" on this page:

http://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html

For example, compare the abs operation, without holding or with holding:

sage: SR(-5).abs()
5
sage: SR(-5).abs(hold=True)
abs(-5)

However, the hold context is not implemented for all operations.

In your case, I would use the string representation for f(x) and replace x by 2:

sage: f(x) = x^2
sage: print str(f(x)).replace('x', '2')
2^2
edit flag offensive delete link more

Comments

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: 2015-12-23 19:15:59 +0100

Seen: 859 times

Last updated: Dec 28 '15