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.Sat, 22 Jul 2017 01:02:08 +0200Piecewise Symbolic Function with Conditional Statementhttps://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/I wish to incorporate a conditional Python expression (`if ... else ...`) in a symbolic function.
Suppose I have a piecewise function *k(n)* defined for *n* = 1,2,3... as in the following pseudocode:
k(n) =
2 if n = 1
n otherwise
I compose this with another function *g(x)* and wish to integrate the result. For example,
f(x)=g(x)/k(n)
f(n=...).integrate(x, 0, 1)
How can implement a non-evaluating conditional in a symbolic Sage function?Fri, 21 Jul 2017 18:23:17 +0200https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/Answer by dan_fulea for <p>I wish to incorporate a conditional Python expression (<code>if ... else ...</code>) in a symbolic function.</p>
<p>Suppose I have a piecewise function <em>k(n)</em> defined for <em>n</em> = 1,2,3... as in the following pseudocode:</p>
<pre><code>k(n) =
2 if n = 1
n otherwise
</code></pre>
<p>I compose this with another function <em>g(x)</em> and wish to integrate the result. For example,</p>
<pre><code>f(x)=g(x)/k(n)
f(n=...).integrate(x, 0, 1)
</code></pre>
<p>How can implement a non-evaluating conditional in a symbolic Sage function?</p>
https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/?answer=38352#post-id-38352One way of doing it is going the pure pythonical way, just define `f` to be a python native function. For instance:
sage: var( 'x' );
sage: g(x) = x*sin(x)
sage: def f( n, g ): return g(x)^n
sage: f(3,g).integrate( x, 0, pi )
-40/9*pi + 2/3*pi^3
(The above code is mixing things, bad style, but `def f` answers the question. There are also other ways, but we need the special situation...)
LATER EDIT since *There isn't any sort of conditional statement in the above code*
(Thanks for the remark.)
Above there is the $n$ as a power decorating $g$, not as a denominator, so that the contribution of $n$ cannot be simply moved mathematically using $\int_0^1 g(x)/k(n)\; dx =(1/k(n))\int_0^1 g(x)\, dx$.
But ok, let us require more examples using
def k(n): return (2 if n == 1 else n)
g(x) = x^3
We have for instance as in the example above, replacing the `3` with `k(n)`, thus using `f( k(n), g )`. This was the answer to the comment, and we may stop here. But let us be more explicit in some examples with
$$ n\in\{\ -1,\ 1, 2,\ 17\ \}\ . $$
sage: NVALUES = [ -1,1,2,17 ]
sage: [ k(n) for n in NVALUES ]
[-1, 2, 2, 17]
sage: [ g(x)/k(n) for n in NVALUES ]
[-x^3, 1/2*x^3, 1/2*x^3, 1/17*x^3]
sage: def f(k,g): return g(x)/k
sage: [ f( k(n), g ) for n in NVALUES ]
[-x^3, 1/2*x^3, 1/2*x^3, 1/17*x^3]
sage: [ f( k(n), g ).integrate(x,0,1) for n in NVALUES ]
[-1/4, 1/8, 1/8, 1/68]
(One can of course move the `k(n)` into the definition of `f`.)
Note: The above solution is (rather less arguably) simpler than
sage: var('n');
sage: k = piecewise( [ ([1,1], 2), ((-Infinity,1),n), ((1,Infinity), n) ], var=n );
sage: k(n)
piecewise(n|-->2 on {1}, n|-->n on (-oo, 1), n|-->n on (1, +oo); n)
sage: f(x) = x^3 / k(n)
sage: f(n=-1).integrate( x,0,1 )
-1/4
sage: f(n=1).integrate( x,0,1 )
1/8
sage: f(n=2).integrate( x,0,1 )
1/8
sage: f(n=17).integrate( x,0,1 )
1/68
where we also insist to use symbolic expressions and the substitution `f(n=1)` (into `f`).
Fri, 21 Jul 2017 22:55:01 +0200https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/?answer=38352#post-id-38352Comment by terrygarcia for <p>One way of doing it is going the pure pythonical way, just define <code>f</code> to be a python native function. For instance:</p>
<pre><code>sage: var( 'x' );
sage: g(x) = x*sin(x)
sage: def f( n, g ): return g(x)^n
sage: f(3,g).integrate( x, 0, pi )
-40/9*pi + 2/3*pi^3
</code></pre>
<p>(The above code is mixing things, bad style, but <code>def f</code> answers the question. There are also other ways, but we need the special situation...)</p>
<p>LATER EDIT since <em>There isn't any sort of conditional statement in the above code</em>
(Thanks for the remark.)</p>
<p>Above there is the $n$ as a power decorating $g$, not as a denominator, so that the contribution of $n$ cannot be simply moved mathematically using $\int_0^1 g(x)/k(n)\; dx =(1/k(n))\int_0^1 g(x)\, dx$. </p>
<p>But ok, let us require more examples using</p>
<pre><code>def k(n): return (2 if n == 1 else n)
g(x) = x^3
</code></pre>
<p>We have for instance as in the example above, replacing the <code>3</code> with <code>k(n)</code>, thus using <code>f( k(n), g )</code>. This was the answer to the comment, and we may stop here. But let us be more explicit in some examples with
$$ n\in{\ -1,\ 1, 2,\ 17\ }\ . $$</p>
<pre><code>sage: NVALUES = [ -1,1,2,17 ]
sage: [ k(n) for n in NVALUES ]
[-1, 2, 2, 17]
sage: [ g(x)/k(n) for n in NVALUES ]
[-x^3, 1/2*x^3, 1/2*x^3, 1/17*x^3]
sage: def f(k,g): return g(x)/k
sage: [ f( k(n), g ) for n in NVALUES ]
[-x^3, 1/2*x^3, 1/2*x^3, 1/17*x^3]
sage: [ f( k(n), g ).integrate(x,0,1) for n in NVALUES ]
[-1/4, 1/8, 1/8, 1/68]
</code></pre>
<p>(One can of course move the <code>k(n)</code> into the definition of <code>f</code>.)</p>
<p>Note: The above solution is (rather less arguably) simpler than</p>
<pre><code>sage: var('n');
sage: k = piecewise( [ ([1,1], 2), ((-Infinity,1),n), ((1,Infinity), n) ], var=n );
sage: k(n)
piecewise(n|-->2 on {1}, n|-->n on (-oo, 1), n|-->n on (1, +oo); n)
sage: f(x) = x^3 / k(n)
sage: f(n=-1).integrate( x,0,1 )
-1/4
sage: f(n=1).integrate( x,0,1 )
1/8
sage: f(n=2).integrate( x,0,1 )
1/8
sage: f(n=17).integrate( x,0,1 )
1/68
</code></pre>
<p>where we also insist to use symbolic expressions and the substitution <code>f(n=1)</code> (into <code>f</code>).</p>
https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/?comment=38354#post-id-38354There isn't any sort of conditional statement in the above code, which is what my question addresses.Fri, 21 Jul 2017 23:57:36 +0200https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/?comment=38354#post-id-38354Answer by ndomes for <p>I wish to incorporate a conditional Python expression (<code>if ... else ...</code>) in a symbolic function.</p>
<p>Suppose I have a piecewise function <em>k(n)</em> defined for <em>n</em> = 1,2,3... as in the following pseudocode:</p>
<pre><code>k(n) =
2 if n = 1
n otherwise
</code></pre>
<p>I compose this with another function <em>g(x)</em> and wish to integrate the result. For example,</p>
<pre><code>f(x)=g(x)/k(n)
f(n=...).integrate(x, 0, 1)
</code></pre>
<p>How can implement a non-evaluating conditional in a symbolic Sage function?</p>
https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/?answer=38356#post-id-38356A special solution using symbolic functions:
var('t')
k(t) = t*(2-sign(t-1)^2)
f(x)=sin(x)/k(t)
f(t=3).integrate(x, 0, 1)Sat, 22 Jul 2017 01:02:08 +0200https://ask.sagemath.org/question/38347/piecewise-symbolic-function-with-conditional-statement/?answer=38356#post-id-38356