Ask Your Question
5

substitute expression instead of formal function symbol

asked 2011-05-11 15:53:09 +0100

Dmitry Semikin gravatar image

Hi, everyone.

The question seems to be basic, but I cannot find out any simple answer for it...

So, the case is as follows. Let's assume, we have some formal function symbol (which is intended later to be replaced by actual expression)

x = var('x'); f = function('f', x)

and some function, which uses 'f(x)' in its definition:

a, b = var('a b')
g(x) = a*f(x) + b*f(x)^2

now we would like to compute derivative of 'g(x)' (and possibly make some other manipulations)

 h(x) = diff(g, x)

as a result we have h(x), which includes symbols like 'D[0]f(x)'. This can be answer for some problem in general form. But now we would like to evaluate this expression for particular 'f(x)' (and possibly multiple times with different expressinos), e. g. for 'f(x) = a*x + b'.

It is desirable to have this with something like

answer = h.subs({f:a*x + b})  # do not work :(

but this substitution do not work inside derivatives...

So, the question is: is there some simple solution (like simple subs() function) for the case I've described above? Or may be there is some standard tricks/patterns to solve this issue?

Thanks in advance for any suggestions.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
5

answered 2011-05-11 20:49:54 +0100

Jason Bandlow gravatar image

I'm not sure I understand why your example doesn't work, but here is a workaround:

sage: var('a b x')
sage: f = function('foo',x)
sage: g(x) = a*foo(x) + b*foo(x)^2
sage: h = g.diff(x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2
edit flag offensive delete link more

Comments

Thanks for your reply. Your example actually works, and here are two points to be mentioned: 1. if we replace in your example h.substitute_function(foo, bar) by h.subs({foo:bar}) we will get exception (something about coercion). 2. if we replace "f = function('foo', x')" with "foo = function('foo', x)" this will not work (i.e. it will not substitute anything). It seems, that there is some obscurity in what is returned by "function()" function (or may be, more precisely, how "assign" ("=") operator behaves). The line "f = function('foo', x)" makes object "foo" the NewSymbolicFunction, while object "f" - Expression (which I did not met in documentations). And it seems, that effects discussed are derived from this.

Dmitry Semikin gravatar imageDmitry Semikin ( 2011-05-12 05:47:22 +0100 )edit

One more thing: Is there some ability to create multi-line comments with code snippets?

Dmitry Semikin gravatar imageDmitry Semikin ( 2011-05-12 05:49:28 +0100 )edit
1

answered 2011-08-26 03:25:04 +0100

Nicolas M Thiéry gravatar image

Hugh Thomas pointed me to the following. When writting:

sage: foo = function('foo',x)

foo is actually foo(x). However, in h, and in particular in pieces like D[0](foo)'', it's not onlyfoo(x)'' that we want to replace, but ``foo''. So a trick is to store the result of function('foo',x) in another variable. And now, substituting with substitute function will work::

sage: var('a b x')
sage: f = function('foo',x)
sage: g = a*foo(x) + b*foo(x)^2
sage: h = diff(g, x)
sage: bar(x) = a*x + b
sage: h.substitute_function(foo, bar)
2*(a*x + b)*a*b + a^2

Now, some questions:

  • Is there a way to recover foo from foo(x)? Something like foo(x).unapply()? Worst come to worst, one can use: sage.symbolic.function_factory.function('foo')

  • Should substitute_function support:

    sage: h.substitute_function(foo(x), bar)

Moral: side effects, like those with function, are ugly ...

edit flag offensive delete link more

Comments

Version that is not dependent on side-effects: sage: foo=function('foo') sage: f=foo(x) Or, version that fully relies on the side effect that top-level "function" has and is explicit about that by not making any assignment (we're really breaking python's convention that functions with side effects should return None) sage: function('foo') foo

nbruin gravatar imagenbruin ( 2014-06-02 14:41:55 +0100 )edit
1

f.operator() is actually foo.

pang gravatar imagepang ( 2015-10-28 11:06:16 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2011-05-11 15:53:09 +0100

Seen: 4,105 times

Last updated: Aug 26 '11