0

I want to have abstract $f$ function that has a given derivative. But, when I try to substitute it to D[0](f)(t), Sage says:

NameError: name 'D' is not defined



    R.<t,u1,u2> = PolynomialRing(RR,3,'t' 'u1' 'u2') 
    tmp1 = r1*k1*u1-(r1/k1)*k1^2*u1^2-r1*b12/k1*k1*u1*k2*u2

    f=function('f',t)
    a=diff(f)
    a.substitute_expression((D[0](f)(t))==tmp1)

tmp1.integral() won't do the job. I also can't substitute the integral, although it gives no warning.

%var u10, u20,r1,r2,k1,k2,b12,b21,t
u1=function('u1',t)
u2=function('u2',t)
tmp1 = r1*k1*u1-(r1/k1)*k1^2*u1^2-r1*b12/k1*k1*u1*k2*u2
tmp2 = r2*u2*k2-r2/k2*k2^2*u2^2-((r2*b21)/k2)*u1*u2*k1*k2    
v1=integral(tmp1,t)
v2=integral(tmp2,t)
sep1=tmp1.substitute_expression(u1==v1,u2==v2)
sep2=tmp2.substitute_expression(u1==v1,u2==v2)
trial=diff(sep1,t)
trial.substitute_expression((integrate(-b12*k2*r1*u1(t)*u2(t) - k1*r1*u1(t)^2 +    k1*r1*u1(t), t))==v1,  (integrate(-b12*k2*r1*u1(t)*u2(t) - k1*r1*u1(t)^2 + k1*r1*u1(t), t))==v2)

Now let's go back to original version:

d1=diff(tmp1,t)
d1.substitute_function((D[0](u1)(t)),tmp1)



Error in lines 13-13
Traceback (most recent call last):
  File "/projects/b501d31c-1f5d-48aa-bee3-73a2dcb30a39/.sagemathcloud/sage_server.py", line 733, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
 File "", line 1, in <module>
NameError: name 'D' is not defined
Gerard Domek
  • 89
  • 1
  • 6
  • What you are asking simply doesn't make sense to Sage: `D[0](f)(t)` is a graphical representation of the derivative of `f`, not an expression. Maybe you want `tmp1.integral(t)`? – Luca De Feo May 21 '14 at 18:28
  • Not really. Isn't there a way to substitute it? Problem is that I also have for example variables $$u_1,u_2$$ and their derivates $$du_1/dt$$, $$du_2/dt$$ depend on $$u_1,u_2$$. I want to compute nth derivative of $$u_1,u_2$$ – Gerard Domek May 21 '14 at 19:24
  • The code you pasted cannot possibly define du₂/dt. As you defined it, the derivative of `u1` with respect to `t` is simply 0, try it yourself. You must have defined `u1` another way. Can you paste a working example? – Luca De Feo May 21 '14 at 22:05
  • I have just added a working example – Gerard Domek May 22 '14 at 08:27
  • I do not understand what you want do achieve. Why do you say "you can't substitute the integral"? Do you want to solve a differential equation? Maybe have a look at http://www.sagemath.org/doc/reference/calculus/sage/calculus/desolvers.html – Luca De Feo May 22 '14 at 09:42
  • I have alreeady read this. I want to have second derivative in terms of u1,u2. – Gerard Domek May 22 '14 at 11:28
  • `d1.substitute_function(diff(u1,t),tmp1)`, maybe? – Luca De Feo May 22 '14 at 13:00
  • Problem is: as far as I understand, it works. It gives no error, it returns a result, and it is semantically equivalent to `d1.substitute_function((D[0](u1)(t)),tmp1)`. Maybe the result is not what you expect, but then I do not understand your question well enough to guess what your expected result is. Sorry. Try to simplify and explain the maths of your problem, and you might have more luck in finding an answer. – Luca De Feo May 22 '14 at 13:51
  • But it just doesn't substiute anything. At least not in sagecloud, sagenb.org or sage 5.13. – Gerard Domek May 22 '14 at 14:59
  • Symbolic computation is not an exact science. `d1.substitute(diff(u1,t)==tmp1)`? Just guessing. – Luca De Feo May 22 '14 at 16:39

1 Answers1

0

I don't know if this is really what you are looking for. But it offers at least some semblance of it.

sage: def myfunc(self, *args, **kwds): return e^(args[0])^2
sage: foo = function('foo', nargs=1, tderivative_func=myfunc)
sage: foo(x)
foo(x)
sage: foo(x).diff(x)
e^(x^2)
sage: foo(x).diff(x,3)
4*x^2*e^(x^2) + 2*e^(x^2)

You'll need to read the documentation of function (gotten by typing function?) very carefully to use this well, especially the comment

Note that custom methods must be instance methods, i.e., expect the instance of the symbolic function as the first argument.

The doc is quite subtle and could use some improvement.

kcrisman
  • 4,374
  • 20
  • 41