1 | initial version |
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...)
2 | No.2 Revision |
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 )
:
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
).
3 | No.3 Revision |
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
).