# substitute expression instead of formal function symbol

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 close merge delete

Sort by » oldest newest most voted

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

more

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.

( 2011-05-12 05:47:22 +0200 )edit

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

( 2011-05-12 05:49:28 +0200 )edit

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

more

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

( 2014-06-02 14:41:55 +0200 )edit
1

f.operator() is actually foo.

( 2015-10-28 11:06:16 +0200 )edit