Ask Your Question
1

Piecewise Symbolic Function with Conditional Statement

asked 2017-07-21 11:23:17 -0500

terrygarcia gravatar image

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?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-07-21 18:02:08 -0500

ndomes gravatar image

updated 2017-07-23 10:38:00 -0500

A 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)
edit flag offensive delete link more
0

answered 2017-07-21 15:55:01 -0500

dan_fulea gravatar image

updated 2017-07-23 10:22:00 -0500

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

edit flag offensive delete link more

Comments

There isn't any sort of conditional statement in the above code, which is what my question addresses.

terrygarcia gravatar imageterrygarcia ( 2017-07-21 16:57:36 -0500 )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

1 follower

Stats

Asked: 2017-07-21 11:23:17 -0500

Seen: 282 times

Last updated: Jul 23