Ask Your Question

# 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.

edit retag close merge delete

## Comments

BTW, 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!

( 2011-12-21 15:35:00 -0500 )edit

I 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.

( 2011-12-21 16:09:45 -0500 )edit

@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 :-)

( 2011-12-22 00:59:48 -0500 )edit

## 1 answer

Sort by » oldest newest most voted

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.

(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 ...

more

## Comments

@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:-)

( 2011-12-22 01:04:16 -0500 )edit

DSM, if you can find an appropriate Trac ticket to put this on, that would be very useful.

( 2011-12-23 05:11:46 -0500 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2011-12-21 15:33:54 -0500

Seen: 277 times

Last updated: Dec 21 '11