ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 27 Aug 2020 19:47:03 +0200Prevent sage from calculating a function?https://ask.sagemath.org/question/31809/prevent-sage-from-calculating-a-function/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.Wed, 23 Dec 2015 19:15:59 +0100https://ask.sagemath.org/question/31809/prevent-sage-from-calculating-a-function/Answer by slelievre for <p>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.</p>
<p>For example, if I have;</p>
<pre><code>var(x)
f(x) = x^2
c = 2
</code></pre>
<p>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.</p>
https://ask.sagemath.org/question/31809/prevent-sage-from-calculating-a-function/?answer=31868#post-id-31868In 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
Mon, 28 Dec 2015 09:09:59 +0100https://ask.sagemath.org/question/31809/prevent-sage-from-calculating-a-function/?answer=31868#post-id-31868Comment by klx for <p>In Sage, what you want is called "holding" the operation.</p>
<p>Find more about it by searching the word "hold" on this page:</p>
<pre><code>http://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/expression.html
</code></pre>
<p>For example, compare the <code>abs</code> operation, without holding or with holding:</p>
<pre><code>sage: SR(-5).abs()
5
sage: SR(-5).abs(hold=True)
abs(-5)
</code></pre>
<p>However, the hold context is not implemented for all operations.</p>
<p>In your case, I would use the string representation for <code>f(x)</code> and replace <code>x</code> by <code>2</code>:</p>
<pre><code>sage: f(x) = x^2
sage: print str(f(x)).replace('x', '2')
2^2
</code></pre>
https://ask.sagemath.org/question/31809/prevent-sage-from-calculating-a-function/?comment=53201#post-id-53201is there a solution for this one? [finding inverse of en element wiht Ext-GCD fails due to defining polynomial converts zero in function](https://ask.sagemath.org/question/53183/finding-inverse-of-en-element-wiht-ext-gcd-fails-due-to-defining-polynomial-converts-zero-in-function/)Thu, 27 Aug 2020 19:47:03 +0200https://ask.sagemath.org/question/31809/prevent-sage-from-calculating-a-function/?comment=53201#post-id-53201