ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 23 Dec 2011 05:11:46 -0600Is it possible to edit the content of an expression by hand?http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/Hi,
Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that.
To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example,
sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1
I want to delete the "+1" in the output and save the "D\[0](f)(t)^2 + 2*D\[0](f)(t)" in another variable. Is it possible to do that? Directly write
g = D[0](f)(t)^2 + 2*D[0](f)(t)
doesn't make sense. Sage says 'D' is not defined.
Wed, 21 Dec 2011 15:33:54 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/Comment by kcrisman for <p>Hi, </p>
<p>Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that. </p>
<p>To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example, </p>
<pre><code>sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1
</code></pre>
<p>I want to delete the "+1" in the output and save the "D[0](f)(t)^2 + 2*D[0](f)(t)" in another variable. Is it possible to do that? Directly write</p>
<pre><code>g = D[0](f)(t)^2 + 2*D[0](f)(t)
</code></pre>
<p>doesn't make sense. Sage says 'D' is not defined.</p>
http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20663#post-id-20663I think that no one knows how to do this. Usually you really can just take the output, or something in it, but these symbolic derivatives have been a thorn on this front - see http://trac.sagemath.org/sage_trac/ticket/6756 for an example of this.Wed, 21 Dec 2011 16:09:45 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20663#post-id-20663Comment by tririver for <p>Hi, </p>
<p>Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that. </p>
<p>To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example, </p>
<pre><code>sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1
</code></pre>
<p>I want to delete the "+1" in the output and save the "D[0](f)(t)^2 + 2*D[0](f)(t)" in another variable. Is it possible to do that? Directly write</p>
<pre><code>g = D[0](f)(t)^2 + 2*D[0](f)(t)
</code></pre>
<p>doesn't make sense. Sage says 'D' is not defined.</p>
http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20664#post-id-20664BTW, I asked a similar question two days ago without answer. I feel my question maybe not clear. Thus here I ask again. I deleted the original question in order not to pollute the forum. Thank you!Wed, 21 Dec 2011 15:35:00 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20664#post-id-20664Comment by tririver for <p>Hi, </p>
<p>Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that. </p>
<p>To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example, </p>
<pre><code>sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1
</code></pre>
<p>I want to delete the "+1" in the output and save the "D[0](f)(t)^2 + 2*D[0](f)(t)" in another variable. Is it possible to do that? Directly write</p>
<pre><code>g = D[0](f)(t)^2 + 2*D[0](f)(t)
</code></pre>
<p>doesn't make sense. Sage says 'D' is not defined.</p>
http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20657#post-id-20657@kcrisman: Thanks for your comment. I met a few inconvenient cases where a general function is treated very differently from a variable. Seems I am a stranger here who always use a general function to produce troubles :-)Thu, 22 Dec 2011 00:59:48 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20657#post-id-20657Answer by DSM for <p>Hi, </p>
<p>Is it possible to edit an expression by hand? I know some terms in my calculation are not important and want to delete them by hand before the next step. However, I didn't find a way to do that. </p>
<p>To be explicit, is there a function to print the sage output in terms of an input form (like the Mathematica InputForm[] function), so I can edit and evaluate it again? For example, </p>
<pre><code>sage: f = function('f', var('t'))
sage: expand((diff(f,t)+1)**2)
D[0](f)(t)^2 + 2*D[0](f)(t) + 1
</code></pre>
<p>I want to delete the "+1" in the output and save the "D[0](f)(t)^2 + 2*D[0](f)(t)" in another variable. Is it possible to do that? Directly write</p>
<pre><code>g = D[0](f)(t)^2 + 2*D[0](f)(t)
</code></pre>
<p>doesn't make sense. Sage says 'D' is not defined.</p>
http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?answer=13057#post-id-13057Here are several non-answers, in random order of craziness and/or workingness (which isn't a word, but you know what I mean.) All of them are dangerous and/or will break in many important cases. I'm posting them more as possible directions to follow up than as anything that anyone would actually use.
(1) If you're willing to live with a DeprecationWarning, the following should do most of what you want:
class D(object):
def __getitem__(self, *args):
args = flatten(args)
def dfun(f):
ops = f.operands()
dv = [f] + [ops[i] for i in args]
return diff(*dv)
return dfun
D = D()
After which the D operator kind of works:
sage: f = function('f', var('t'), var("x"), var("y"))
sage: expand((diff(f,x,y)+1)**2)
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage:
sage: old_form = expand((diff(f,x,y)+1)**2)
sage: new_form = D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
sage: bool(old_form == new_form)
True
(2) You could simply define a print function to make sure that you get the output in a form you can re-enter (i.e. one with diffs). Here I'll cheat a little and use the existing maxima printing:
def dpr(obj):
print repr(obj._maxima_()).replace("'","")
sage: z = expand((diff(f,x,y)+1)**2)
sage: z
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: dpr(z)
(diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1
sage: bool((diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1 == z)
True
(3) You can play games with FDerivativeOperator's repr, but I couldn't get this to work because of the way Expressions print. Namely:
sage: f = function("f", var("t"))
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
D[0](f)
sage:
sage: sage.symbolic.operators.FDerivativeOperator.__repr__ = lambda *args: 'fred'
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
fred
I can change the name that the operator itself has, but that doesn't seem to be used along the Expression printing path. I always get lost in the code at this point, so this is kind of a wash. But I feel like something along these lines *should* work, even though it doesn't seem to.
If I actually needed to do something like this I would probably use a variant of (2). I can't help but wonder if there isn't a better way to accomplish your goal, though.Wed, 21 Dec 2011 17:20:18 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?answer=13057#post-id-13057Comment by tririver for <div class="snippet"><p>Here are several non-answers, in random order of craziness and/or workingness (which isn't a word, but you know what I mean.) All of them are dangerous and/or will break in many important cases. I'm posting them more as possible directions to follow up than as anything that anyone would actually use.</p>
<p>(1) If you're willing to live with a DeprecationWarning, the following should do most of what you want:</p>
<pre><code>class D(object):
def __getitem__(self, *args):
args = flatten(args)
def dfun(f):
ops = f.operands()
dv = [f] + [ops[i] for i in args]
return diff(*dv)
return dfun
D = D()
</code></pre>
<p>After which the D operator kind of works:</p>
<pre><code>sage: f = function('f', var('t'), var("x"), var("y"))
sage: expand((diff(f,x,y)+1)**2)
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage:
sage: old_form = expand((diff(f,x,y)+1)**2)
sage: new_form = D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
sage: bool(old_form == new_form)
True
</code></pre>
<p>(2) You could simply define a print function to make sure that you get the output in a form you can re-enter (i.e. one with diffs). Here I'll cheat a little and use the existing maxima printing:</p>
<pre><code>def dpr(obj):
print repr(obj._maxima_()).replace("'","")
sage: z = expand((diff(f,x,y)+1)**2)
sage: z
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: dpr(z)
(diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1
sage: bool((diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1 == z)
True
</code></pre>
<p>(3) You can play games with FDerivativeOperator's repr, but I couldn't get this to work because of the way Expressions print. Namely:</p>
<pre><code>sage: f = function("f", var("t"))
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
D[0](f)
sage:
sage: sage.symbolic.operators.FDerivativeOperator.__repr__ = lambda *args: 'fred'
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
fred
</code></pre>
<p>I can change the name that the operator itself has, but that doesn't seem to be used along the Expression printing path. I always get lost in the code at this point, so this is kind of a wash. But I feel like something along these lines <em>should</em> work, even though it doesn't seem to.</p>
<p>If I actually needed to do something like this I would probably use a variant of (2). I can't help but wonder if there isn't a better way to accomplish ...<span class="expander"> <a>(more)</a></span></p></div>http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20656#post-id-20656@DSM: Thank you very much! I think as long as it seems an open question, I cannot expect a better answer before Sage is improved. Thus I mark this answer as the correct solution. I think both (1) and (2) are fine enough work arounds for my purpose. Also, I am very glad to find I have learnt how to use the star operator in Python from your post:-)Thu, 22 Dec 2011 01:04:16 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20656#post-id-20656Comment by kcrisman for <div class="snippet"><p>Here are several non-answers, in random order of craziness and/or workingness (which isn't a word, but you know what I mean.) All of them are dangerous and/or will break in many important cases. I'm posting them more as possible directions to follow up than as anything that anyone would actually use.</p>
<p>(1) If you're willing to live with a DeprecationWarning, the following should do most of what you want:</p>
<pre><code>class D(object):
def __getitem__(self, *args):
args = flatten(args)
def dfun(f):
ops = f.operands()
dv = [f] + [ops[i] for i in args]
return diff(*dv)
return dfun
D = D()
</code></pre>
<p>After which the D operator kind of works:</p>
<pre><code>sage: f = function('f', var('t'), var("x"), var("y"))
sage: expand((diff(f,x,y)+1)**2)
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage:
sage: old_form = expand((diff(f,x,y)+1)**2)
sage: new_form = D[1,2](f)(t, x, y)^2 + 2*D[1,2](f)(t, x, y) + 1
sage: bool(old_form == new_form)
True
</code></pre>
<p>(2) You could simply define a print function to make sure that you get the output in a form you can re-enter (i.e. one with diffs). Here I'll cheat a little and use the existing maxima printing:</p>
<pre><code>def dpr(obj):
print repr(obj._maxima_()).replace("'","")
sage: z = expand((diff(f,x,y)+1)**2)
sage: z
D[1, 2](f)(t, x, y)^2 + 2*D[1, 2](f)(t, x, y) + 1
sage: dpr(z)
(diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1
sage: bool((diff(f(t,x,y),x,1,y,1))^2+2*diff(f(t,x,y),x,1,y,1)+1 == z)
True
</code></pre>
<p>(3) You can play games with FDerivativeOperator's repr, but I couldn't get this to work because of the way Expressions print. Namely:</p>
<pre><code>sage: f = function("f", var("t"))
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
D[0](f)
sage:
sage: sage.symbolic.operators.FDerivativeOperator.__repr__ = lambda *args: 'fred'
sage: diff(f,t)
D[0](f)(t)
sage: diff(f,t).operator()
fred
</code></pre>
<p>I can change the name that the operator itself has, but that doesn't seem to be used along the Expression printing path. I always get lost in the code at this point, so this is kind of a wash. But I feel like something along these lines <em>should</em> work, even though it doesn't seem to.</p>
<p>If I actually needed to do something like this I would probably use a variant of (2). I can't help but wonder if there isn't a better way to accomplish ...<span class="expander"> <a>(more)</a></span></p></div>http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20642#post-id-20642DSM, if you can find an appropriate Trac ticket to put this on, that would be very useful.Fri, 23 Dec 2011 05:11:46 -0600http://ask.sagemath.org/question/8586/is-it-possible-to-edit-the-content-of-an-expression-by-hand/?comment=20642#post-id-20642