ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 19 Jul 2017 18:12:07 +0200How can I substitute "target" functions inside expressions?https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/Hi all,
I'm a Sage newbie striving to manipulate complex-valued expressions. In particular, I need to convert expressions like abs(x)^2 into x*conj(x) and back, within expressions including multiple instances of these patterns. In other words, x is here just a placeholder for what may be a list of different variables or expressions, but I do not want to substitute each of these separately or manually.
Here is some experimenting that I have been doing on the matter with generic functions, as well as standard functions (sin):
# Some initialization
reset()
forget()
f = function('foo')(x)
g = function('goo')(x)
h = function('hoo')(x)
h(x) = f(x)^2
# Types and basic substitutions
print(type(foo))
print(type(goo))
print(type(hoo))
print(type(f))
print(type(g))
print(type(h))
print(h(x))
print(h.substitute_function(f,g))
print(h.substitute_function(foo,goo))
# Substitution of a function
h(x) = sin(x)^2
print(type(h))
print(type(sin))
print(h.substitute_function(sin,goo))
If I try, however, to substitute abs with some other function, I do not get what I want:
# Substitution of a built-in function (not working)
h(x) = abs(x)^2
print(type(h))
print(type(abs))
print(h.substitute_function(abs,goo))
Here is a workaround that I came up with, but I hope someone can let me know a more elegant/standard technique:
# Substitute abs(x) with sqrt(x*conj(x)): a workaround
moo = sage.functions.other.Function_abs()
m(x) = abs(x)
c(x) = (x*x.conjugate()).sqrt()
print(type(moo))
print(type(m))
print(type(c))
s(x) = h.substitute_function(moo,c)
print(s(x))
Also, I found a lot of headaches with the opposite conversion, and following is my attempt at solving the problem:
# Substitute sqrt(x*conj(x)) with abs(x): a workaround
doo(x) = goo(x)/x
qoo(x) = abs(x)^2
b_temp(x) = s.substitute_function(conjugate,goo)
print(b_temp(x))
b_temp(x) = b_temp.substitute_function(goo,doo)
print(b_temp(x))
b(x) = b_temp.substitute_function(goo,qoo)
print(b(x))
Honestly, it seems strange to me that one cannot easily recast an expression in order to make certain target functions to appear.
Thank you in advance for your support!Wed, 19 Jul 2017 16:12:58 +0200https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/Comment by vdelecroix for <p>Hi all,
I'm a Sage newbie striving to manipulate complex-valued expressions. In particular, I need to convert expressions like abs(x)^2 into x*conj(x) and back, within expressions including multiple instances of these patterns. In other words, x is here just a placeholder for what may be a list of different variables or expressions, but I do not want to substitute each of these separately or manually.</p>
<p>Here is some experimenting that I have been doing on the matter with generic functions, as well as standard functions (sin):</p>
<pre><code># Some initialization
reset()
forget()
f = function('foo')(x)
g = function('goo')(x)
h = function('hoo')(x)
h(x) = f(x)^2
# Types and basic substitutions
print(type(foo))
print(type(goo))
print(type(hoo))
print(type(f))
print(type(g))
print(type(h))
print(h(x))
print(h.substitute_function(f,g))
print(h.substitute_function(foo,goo))
# Substitution of a function
h(x) = sin(x)^2
print(type(h))
print(type(sin))
print(h.substitute_function(sin,goo))
</code></pre>
<p>If I try, however, to substitute abs with some other function, I do not get what I want:</p>
<pre><code># Substitution of a built-in function (not working)
h(x) = abs(x)^2
print(type(h))
print(type(abs))
print(h.substitute_function(abs,goo))
</code></pre>
<p>Here is a workaround that I came up with, but I hope someone can let me know a more elegant/standard technique:</p>
<pre><code># Substitute abs(x) with sqrt(x*conj(x)): a workaround
moo = sage.functions.other.Function_abs()
m(x) = abs(x)
c(x) = (x*x.conjugate()).sqrt()
print(type(moo))
print(type(m))
print(type(c))
s(x) = h.substitute_function(moo,c)
print(s(x))
</code></pre>
<p>Also, I found a lot of headaches with the opposite conversion, and following is my attempt at solving the problem:</p>
<pre><code># Substitute sqrt(x*conj(x)) with abs(x): a workaround
doo(x) = goo(x)/x
qoo(x) = abs(x)^2
b_temp(x) = s.substitute_function(conjugate,goo)
print(b_temp(x))
b_temp(x) = b_temp.substitute_function(goo,doo)
print(b_temp(x))
b(x) = b_temp.substitute_function(goo,qoo)
print(b(x))
</code></pre>
<p>Honestly, it seems strange to me that one cannot easily recast an expression in order to make certain target functions to appear.</p>
<p>Thank you in advance for your support!</p>
https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?comment=38328#post-id-38328an advice: If you want clear answer post *one simple question*. Not 50 lines of a mix of try and errors, comments and questions.Wed, 19 Jul 2017 16:52:18 +0200https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?comment=38328#post-id-38328Answer by vdelecroix for <p>Hi all,
I'm a Sage newbie striving to manipulate complex-valued expressions. In particular, I need to convert expressions like abs(x)^2 into x*conj(x) and back, within expressions including multiple instances of these patterns. In other words, x is here just a placeholder for what may be a list of different variables or expressions, but I do not want to substitute each of these separately or manually.</p>
<p>Here is some experimenting that I have been doing on the matter with generic functions, as well as standard functions (sin):</p>
<pre><code># Some initialization
reset()
forget()
f = function('foo')(x)
g = function('goo')(x)
h = function('hoo')(x)
h(x) = f(x)^2
# Types and basic substitutions
print(type(foo))
print(type(goo))
print(type(hoo))
print(type(f))
print(type(g))
print(type(h))
print(h(x))
print(h.substitute_function(f,g))
print(h.substitute_function(foo,goo))
# Substitution of a function
h(x) = sin(x)^2
print(type(h))
print(type(sin))
print(h.substitute_function(sin,goo))
</code></pre>
<p>If I try, however, to substitute abs with some other function, I do not get what I want:</p>
<pre><code># Substitution of a built-in function (not working)
h(x) = abs(x)^2
print(type(h))
print(type(abs))
print(h.substitute_function(abs,goo))
</code></pre>
<p>Here is a workaround that I came up with, but I hope someone can let me know a more elegant/standard technique:</p>
<pre><code># Substitute abs(x) with sqrt(x*conj(x)): a workaround
moo = sage.functions.other.Function_abs()
m(x) = abs(x)
c(x) = (x*x.conjugate()).sqrt()
print(type(moo))
print(type(m))
print(type(c))
s(x) = h.substitute_function(moo,c)
print(s(x))
</code></pre>
<p>Also, I found a lot of headaches with the opposite conversion, and following is my attempt at solving the problem:</p>
<pre><code># Substitute sqrt(x*conj(x)) with abs(x): a workaround
doo(x) = goo(x)/x
qoo(x) = abs(x)^2
b_temp(x) = s.substitute_function(conjugate,goo)
print(b_temp(x))
b_temp(x) = b_temp.substitute_function(goo,doo)
print(b_temp(x))
b(x) = b_temp.substitute_function(goo,qoo)
print(b(x))
</code></pre>
<p>Honestly, it seems strange to me that one cannot easily recast an expression in order to make certain target functions to appear.</p>
<p>Thank you in advance for your support!</p>
https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?answer=38327#post-id-38327`abs` is a Python builtin function (so as `sum`, `len`, `iter` or `next`). The symbolic absolute value function is named `abs_symbolic` in Sage. Note the difference
sage: type(abs)
<type 'builtin_function_or_method'>
sage: abs_symbolic
abs
Using `abs_symbolic` works as expected
sage: h(x) = abs(x)^2
sage: h.substitute_function(abs_symbolic, sin)
sin(x)^2
Wed, 19 Jul 2017 16:50:05 +0200https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?answer=38327#post-id-38327Comment by vdelecroix for <p><code>abs</code> is a Python builtin function (so as <code>sum</code>, <code>len</code>, <code>iter</code> or <code>next</code>). The symbolic absolute value function is named <code>abs_symbolic</code> in Sage. Note the difference</p>
<pre><code>sage: type(abs)
<type 'builtin_function_or_method'>
sage: abs_symbolic
abs
</code></pre>
<p>Using <code>abs_symbolic</code> works as expected</p>
<pre><code>sage: h(x) = abs(x)^2
sage: h.substitute_function(abs_symbolic, sin)
sin(x)^2
</code></pre>
https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?comment=38331#post-id-38331Great! If you are happy with the answer please set it as accepted (tick box on the left of the answer). If you do so, the question will not be marked anymore as "unanswered" (and I will get some more karma ;-)Wed, 19 Jul 2017 18:12:07 +0200https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?comment=38331#post-id-38331Comment by Asker for <p><code>abs</code> is a Python builtin function (so as <code>sum</code>, <code>len</code>, <code>iter</code> or <code>next</code>). The symbolic absolute value function is named <code>abs_symbolic</code> in Sage. Note the difference</p>
<pre><code>sage: type(abs)
<type 'builtin_function_or_method'>
sage: abs_symbolic
abs
</code></pre>
<p>Using <code>abs_symbolic</code> works as expected</p>
<pre><code>sage: h(x) = abs(x)^2
sage: h.substitute_function(abs_symbolic, sin)
sin(x)^2
</code></pre>
https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?comment=38329#post-id-38329Thank you very much: this is exactly what I needed! I suspected something of this sort due to the way the keyword abs is highlighted in the notebook, but didn't find the straight solution. I will be more cautious in the future, as well as post shorter questions ;)Wed, 19 Jul 2017 17:35:23 +0200https://ask.sagemath.org/question/38326/how-can-i-substitute-target-functions-inside-expressions/?comment=38329#post-id-38329