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.Sun, 02 May 2021 21:54:48 +0200- How to define functions with varying number of variableshttps://ask.sagemath.org/question/56908/how-to-define-functions-with-varying-number-of-variables/I want to compute the Frechet derivative for a vector field, and that code works perfectly for functions with two variables *x* and *t*.
var ("t x")
v = function ("v")
u = function ("u")
w1 = function ("w1")
w2 = function ("w2")
eqsys = [diff(v(x,t), x) - u(x,t), diff(v(x,t), t) - diff(u(x,t), x)/(u(x,t)**2)]
def FrechetD (support, dependVar, independVar, testfunction):
frechet = []
eps = var ("eps")
v = independVar[:] + [eps]
for j in range (len(support)):
deriv = []
for i in range (len(support)):
r0 (x, t, eps) = dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
s = support[j].substitute_function (dependVar[i], r0)
deriv.append (diff(s, eps).subs ({eps: 0}))
frechet.append (deriv)
return frechet
FrechetD (eqsys, [u,v], [x,t], [w1,w2])
[[-w1(x, t), diff(w2(x, t), x)],
[2*w1(x, t)*diff(u(x, t), x)/u(x, t)^3 - diff(w1(x, t), x)/u(x, t)^2,
diff(w2(x, t), t)]]
but my problem is the line
r0 (x, t, eps) = dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
because this depends on the hardcoded *x* and *t*. Without it the following derivation for *eps* always is 0. Even when I add someting like
_r0 = function('ro')(*v)
doesn't work.
What i want to do is to something like
r0 (*v) = dependVar[i](*independVar)+ testfunction[i](*independVar) * eps
to get rid of the hardcoded variables and/or the number of variables. Is that possible ?
tamanduaSun, 02 May 2021 21:54:48 +0200https://ask.sagemath.org/question/56908/
- How 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!AskerWed, 19 Jul 2017 16:12:58 +0200https://ask.sagemath.org/question/38326/
- How to substitute a function within derivatives?https://ask.sagemath.org/question/9932/how-to-substitute-a-function-within-derivatives/I want to simplify an ODE by making a substitution, say g(x) -> h(x)*x, but can't get it to work. I tried:
g=function('g', x)
h=function('h', x)
dg = g.diff(x)
dg
sage output: D[0](g)(x)
dg.subs(g==h*x)
sage output: D[0](g)(x)
The substitution is not done for the g function within derivatives. I tried dg.subs(g(x)==h(x)*x) too, got deprecation warnings and the same results. How can I make this work? This is a simplified example, in reality, instead of dg, I have the lhs of an ODE defined interms of g(x).
Thanks
sgiaThu, 21 Mar 2013 23:59:28 +0100https://ask.sagemath.org/question/9932/
- automatic substitution within functions?https://ask.sagemath.org/question/7637/automatic-substitution-within-functions/what i do to do this?
sage: var('t w')
sage: f(t) = sin(w*t)
t |--> sin(t*w)
sage: w = 2
sage: f
t |--> sin(2*w)
Without doing f(w=2)!!! imagine that the function is f->f(a,b,c,d,f....,t) like doing this:
...
...
sage: aw1=aw1(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw2=aw2(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
sage: aw4=aw4(m1=1,m2=0.5,l1=1,l2=0.5,g=9.8)
...
...
etc
ngativWed, 25 Aug 2010 19:04:25 +0200https://ask.sagemath.org/question/7637/
- How do I make functional substitutions?https://ask.sagemath.org/question/9454/how-do-i-make-functional-substitutions/I want to substitute one function for another. Consider this:
v = function('x')
u(x) = (v(x)+v(-x))/2
delta = 10^(-90)
u(delta).subs(v=gamma(x))
The very last line is what I'm trying to accomplish. I know it's the wrong command, but I'm wanting to make the substitution $v(x)\to\Gamma(x)$.
Is there a way along these lines to do such a functional substitution?daniel.e2718Mon, 22 Oct 2012 16:16:35 +0200https://ask.sagemath.org/question/9454/
- How to dynamically substitute a variable in a callable function?https://ask.sagemath.org/question/8825/how-to-dynamically-substitute-a-variable-in-a-callable-function/Hi guys, this is a little problem I came across a week ago.
I'm trying to define a python function that accepts:<br>
1. a callable sage function (of potentially more than one variable) as the first argument (henceforth called func), and <br>
2. a symbolic variable as the second argument (henceforth called xsub)
My function then needs to define a dummy symbolic variable (t), and substitute xsub with t in func. <br>I can do this for one variable equations in current 4.8 sagemath, by ignoring the new substitution syntax, but it throws up a depreciation warning (Which I'm assuming will become an error in 5.0).
Here's what my code looks like:
<pre>def fracintegral(func,xsub,n,a=0):
var('t')
assume(x>a)
assume(t>a)
return integrate((x-t)^(n-1)*func(t),t,a,x)</pre>
The last line should look something like:<pre>
return integrate((x-t)^(n-1)*func(x=t),t,a,x)</pre>
in order to avoid Depreciation Warnings, but this hardcodes x as the variable to be substituted. (Useless if my func is a y function.)
If I try:<pre>
return integrate((x-t)^(n-1)*func(xsub=t),t,a,x)</pre>
then substitution of func(x=t) doesn't occur (and the integrate function effectively treats func as a constant with respect to dt).
Trying:<pre>
return integrate((x-t)^(n-1)*func.subs(xsub==t),t,a,x)</pre>
doesn't work either, same result as func(xsub=t).
So, any idea how a function can accept a symbolic variable, and dynamically substitute out that variable in a callable function?JoalHeagneySat, 24 Mar 2012 21:51:48 +0100https://ask.sagemath.org/question/8825/
- Substitute formal function by an expression in a differential equationhttps://ask.sagemath.org/question/8293/substitute-formal-function-by-an-expression-in-a-differential-equation/This is a follow up to [substitute expression instead of formal function symbol](http://ask.sagemath.org/question/541/substitute-expression-instead-of-formal-function). I tried to no avail to apply the workaround proposed there for the following application where I build a differential equation involving a formal function P:
sage: x,y = var('x,y')
sage: P = function('P',x,y)
sage: z = var('z')
sage: C = function('C',z)
sage: equation = P(x=z,y=C) == 0
sage: dequation = diff(equation, z)
and then I would want to substitute P by a specific expression:
sage: Q(x,y) = y^2 - y + x
sage: dequation.substitute_function(P,Q)
But whatever I tried, I got:
D[0](C)(z)*D[1](P)(z, C(z)) + D[0](P)(z, C(z)) == 0
Is there a natural syntax to achieve this? If not, should this be a ticket?
Thanks!Nicolas M ThiĆ©ryThu, 25 Aug 2011 10:24:25 +0200https://ask.sagemath.org/question/8293/
- substitute expression instead of formal function symbolhttps://ask.sagemath.org/question/8110/substitute-expression-instead-of-formal-function-symbol/Hi, everyone.
The question seems to be basic, but I cannot find out any simple answer for it...
So, the case is as follows. Let's assume, we have some formal function symbol (which is intended later to be replaced by actual expression)
x = var('x'); f = function('f', x)
and some function, which uses 'f(x)' in its definition:
a, b = var('a b')
g(x) = a*f(x) + b*f(x)^2
now we would like to compute derivative of 'g(x)' (and possibly make some other manipulations)
h(x) = diff(g, x)
as a result we have h(x), which includes symbols like 'D[0]f(x)'. This can be answer for some problem in general form. But now we would like to evaluate this expression for particular 'f(x)' (and possibly multiple times with different expressinos), e. g. for 'f(x) = a*x + b'.
It is desirable to have this with something like
answer = h.subs({f:a*x + b}) # do not work :(
but this substitution do not work inside derivatives...
So, the question is: is there some simple solution (like simple subs() function) for the case I've described above? Or may be there is some standard tricks/patterns to solve this issue?
Thanks in advance for any suggestions.Dmitry SemikinWed, 11 May 2011 15:53:09 +0200https://ask.sagemath.org/question/8110/
- Substituting function value in an expressionhttps://ask.sagemath.org/question/8201/substituting-function-value-in-an-expression/I have an expression like `uR(t) == 3*iL(0) + uC(0)/2 - 4`
how can I substitute for `iL(0)` and `uC(0)`, if I'm given, that `iL(0) = 0` and `uC(0) = 0`.
`uR`, `iL`, `uC` are a functions of var `t`:
t = var('t')
uR = function('uR', t)
iL = function('iL', t)
uC = function('uC', t)
thank you :)OndraWed, 29 Jun 2011 23:27:56 +0200https://ask.sagemath.org/question/8201/