ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 14 Jan 2018 04:34:03 -0600how to define the codomain of a symbolic functionhttp://ask.sagemath.org/question/40363/how-to-define-the-codomain-of-a-symbolic-function/Hi,
I'm perfoming some basic differential calculations in General Relativity.
I created a function a(t), where t is one of the chart coordinates:
> a = function('a')(t)
I do not want to define how it depends on t since I'm calculating stuff like Cristoffel symbols, Einstein equations, etc and I want to keep the resulting formulas independent from the functional behaviour of a(t).
This function appears to be a complex one (i.e. I see the bar over it sometimes - complex conjugate).
This is boring since the calculations does not simplify by themselves.
Is there a way to "assume" that the codomain of the function are the Real Numbers? It would be great also to specify only a range, like (0,+\infty).
ThanksFri, 29 Dec 2017 16:15:57 -0600http://ask.sagemath.org/question/40363/how-to-define-the-codomain-of-a-symbolic-function/Answer by eric_g for <p>Hi,
I'm perfoming some basic differential calculations in General Relativity. </p>
<p>I created a function a(t), where t is one of the chart coordinates:</p>
<blockquote>
<p>a = function('a')(t)</p>
</blockquote>
<p>I do not want to define how it depends on t since I'm calculating stuff like Cristoffel symbols, Einstein equations, etc and I want to keep the resulting formulas independent from the functional behaviour of a(t).</p>
<p>This function appears to be a complex one (i.e. I see the bar over it sometimes - complex conjugate).
This is boring since the calculations does not simplify by themselves.</p>
<p>Is there a way to "assume" that the codomain of the function are the Real Numbers? It would be great also to specify only a range, like (0,+\infty).</p>
<p>Thanks</p>
http://ask.sagemath.org/question/40363/how-to-define-the-codomain-of-a-symbolic-function/?answer=40459#post-id-40459Unfortunately, it does not seem possible to add the codomain of a symbolic function to the list of assumptions known to Sage. However, as a workaround, you may define a Python function to perform the required simplifications; for instance:
def simplify_real(expr, *real_expr):
r"""
- ``expr``: symbolic expression to be simplified
- ``real_expr``: list of subexpressions of ``expr`` assumed to be real
"""
for s in real_expr:
expr = expr.subs({real_part(s): s,
imag_part(s): 0,
conjugate(s): s})
return expr.simplify_full()
You may use it as follows:
sage: t = var('t')
sage: a = function('a')
sage: y = I*a(t) + real_part(I+a(t)) + a(t).conjugate()
sage: simplify_real(y, a(t))
(I + 2)*a(t)
You may have more than one subexpression assumed to be real in the list of inputs:
sage: z = conjugate(a(t) + function('b')(t)) + imag_part(function('c')(t))
sage: simplify_real(z, a(t), b(t))
a(t) + b(t) + imagpart(c(t))
sage: simplify_real(z, a(t), b(t), c(t))
a(t) + b(t)Thu, 04 Jan 2018 07:32:40 -0600http://ask.sagemath.org/question/40363/how-to-define-the-codomain-of-a-symbolic-function/?answer=40459#post-id-40459Comment by scollovati for <p>Unfortunately, it does not seem possible to add the codomain of a symbolic function to the list of assumptions known to Sage. However, as a workaround, you may define a Python function to perform the required simplifications; for instance:</p>
<pre><code>def simplify_real(expr, *real_expr):
r"""
- ``expr``: symbolic expression to be simplified
- ``real_expr``: list of subexpressions of ``expr`` assumed to be real
"""
for s in real_expr:
expr = expr.subs({real_part(s): s,
imag_part(s): 0,
conjugate(s): s})
return expr.simplify_full()
</code></pre>
<p>You may use it as follows:</p>
<pre><code>sage: t = var('t')
sage: a = function('a')
sage: y = I*a(t) + real_part(I+a(t)) + a(t).conjugate()
sage: simplify_real(y, a(t))
(I + 2)*a(t)
</code></pre>
<p>You may have more than one subexpression assumed to be real in the list of inputs:</p>
<pre><code>sage: z = conjugate(a(t) + function('b')(t)) + imag_part(function('c')(t))
sage: simplify_real(z, a(t), b(t))
a(t) + b(t) + imagpart(c(t))
sage: simplify_real(z, a(t), b(t), c(t))
a(t) + b(t)
</code></pre>
http://ask.sagemath.org/question/40363/how-to-define-the-codomain-of-a-symbolic-function/?comment=40598#post-id-40598Thank you very much!
I found useful especially because I can use it also for derivatives:
simplify_real(expression, a(t),diff(a(t),t))
I wrote a similar simple function for assuming that a function is positive: in this way in the expressions a lot of factor simplifies. I suppose that also this assumption cannot be added in Sage.
In your opinion can thees functions be implemented as a method for expressions in a future release?Sun, 14 Jan 2018 04:34:03 -0600http://ask.sagemath.org/question/40363/how-to-define-the-codomain-of-a-symbolic-function/?comment=40598#post-id-40598