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 +0200How 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/Substitution in Expression of Symmetric Functions over Polynomial Ringhttps://ask.sagemath.org/question/56515/substitution-in-expression-of-symmetric-functions-over-polynomial-ring/ Hi everyone! So I'm working in the ring of symmetric functions over a polynomial ring, say $\mathbb{Q}[q]$, and I have an expression of the form
$$ q^2s_{2,1} + (q-1)s_{1,1,1}$$
I want to perform the substitution $q = q+1$ in this expression, but all of the usual methods (subs, substitute, etc.) give me an error. Does anyone know how to do this?
Thanks in advance!RaymondChouTue, 06 Apr 2021 08:12:11 +0200https://ask.sagemath.org/question/56515/Substitutionhttps://ask.sagemath.org/question/51446/substitution/After some calculation I arrive to the following equation $\frac{D*p - D+ I}{D*(P-1)}$.
Whichever be the command I use I cannot put it as $\frac{D*(p - 1)+ I}{D*(P-1)}$ or as $1-\frac{I}{D*(P-1)}$ or $1-\frac{I}{D}\frac{1}{(P-1)}$.
This last equation is the one of interest since I would like to solve $1-\frac{I}{D}\frac{1}{(P-1)}==0$ according to $\frac{D}{I}$.
Is there a way to use `solve()` acording to $\frac{D}{I}$ ? I have tried to substitue `x` with a form of `substitute()`for it, but it was an echec. I understand that substitue a ratio of 2 variables for one is a little bit complex.CyrilleMon, 18 May 2020 15:10:50 +0200https://ask.sagemath.org/question/51446/Automatic substitution of unicode charactershttps://ask.sagemath.org/question/51361/automatic-substitution-of-unicode-characters/ With Python3 sagemath accept unicode characters. But not necessary it's periferic friends as Maxima. So for instance $\beta$ could not be use in `solve` could we imagine an automatic substitution process such that when $\beta$ is use sagemath send for instance be to Maxima and in return know that it must substitute $\beta$ for be in the other way.
This is perfectly doable on the spot. What I want to know is if one can construct such a mechanism ?CyrilleSun, 10 May 2020 09:26:55 +0200https://ask.sagemath.org/question/51361/substitute variable inside functionhttps://ask.sagemath.org/question/50662/substitute-variable-inside-function/I can do
f(a,b) = a^2+b^3; f=f-f.subs(a=(a-1))
(a, b) |--> -(a - 1)^2 + a^2
I tried to write a function which does this for arbitrary expressions and variables
def test(self, symb):
return (self-self.subs(symb=(symb-1)))
but
test(f,a)
(a, b) |--> 0
fails miserably. It seems self.subs(symb=(symb-1)) can not be evaluated properly. How can I achieve what I want?Emil WidmannFri, 10 Apr 2020 18:53:00 +0200https://ask.sagemath.org/question/50662/Unable to use substitute_function in SageManifoldshttps://ask.sagemath.org/question/49586/unable-to-use-substitute_function-in-sagemanifolds/I'm trying to do a function substitution into one of the Einstein Field equations, using tools from SageManifolds. I am doing nearly the same thing as the tutorial on the Lemaitre-Tolman equations (https://nbviewer.jupyter.org/github/egourgoulhon/BHLectures/blob/master/sage/Lemaitre_Tolman.ipynb). However, using substitute_function does not seem to achive the result. The MWE is below. The setup is the following:
M = Manifold(4, 'R^4')
coord.<t,x,z1,z2> = M.chart(r't x z1 z2') #name of the chart is coord.
a=function('a')(t)
g = M.metric('g')
g[0,0] = -1
g[1,1] = a^2
g[2,2] = a^2*sin(x)^2
g[3,3] = a^2*cos(x)^2
nab = g.connection()
ric = nab.ricci()
SCAL = g.ricci_scalar()
phi = M.scalar_field(function('phi')(*coord), name='phi')
dphi=nab(phi)
dphiu=dphi.up(g,0)
T1=dphi['_a']*dphi['_b']
T2=g.inverse()['^ab']*T1['_ab']
T3=dphi*dphi+1/2*g*T2
Obviously, on my sheet I run all these and they display what's expected - let me know if you think more info is needed here, and I'll attach the sheet as well (sorry, turns out I don't have the rep for that). So now I define the Einstein equations and try to make a trivial substitution:
var('G')
Lam=var('Lam', latex_name=r'\Lambda')
E=ric - SCAL/2*g + Lam*g - (8*pi*G)*T3
E[0,0].expr().expand() == 0
The result is
-12*pi*G*diff(phi(t, x, z1, z2), t)^2 + 4*pi*G*diff(phi(t, x, z1, z2), x)^2/a(t)^2 - Lam + 3*diff(a(t), t)^2/a(t)^2 + 4*pi*G*diff(phi(t, x, z1, z2), z1)^2/(a(t)^2*sin(x)^2) + 4*pi*G*diff(phi(t, x, z1, z2), z2)^2/(a(t)^2*cos(x)^2) + 3/a(t)^2 == 0
Cool, so what I really want to do is make some assumptions about the scalar field, but I can't even figure out how to substitute a different, equivalent function. My first try:
F1=M.scalar_field(function('F1')(*coord),name='F1')
E[0,0].expr().substitute_function(phi,F1)
Results in
(4*pi*G*cos(x)^2*diff(phi(t, x, z1, z2), z1)^2 + (4*pi*G*diff(phi(t, x, z1, z2), z2)^2 - (12*pi*G*a(t)^2*diff(phi(t, x, z1, z2), t)^2 - 4*pi*G*diff(phi(t, x, z1, z2), x)^2 + Lam*a(t)^2 - 3*diff(a(t), t)^2 - 3)*cos(x)^2)*sin(x)^2)/(a(t)^2*cos(x)^2*sin(x)^2)
e.g. exactly the same thing (specifically, phi is not replaced with F1). Here are bunch of other tries that also return the same thing:
E_expr=E[0,0].expr()==0
E_expr.substitute_function(phi, F1)
F2=function('F2')(*coord)
E[0,0].expr().substitute_function(phi,F2)
E_expr.substitute_function(phi, F2)
Anyone have any insight into what is happening?thethinkerFri, 17 Jan 2020 22:33:24 +0100https://ask.sagemath.org/question/49586/substitution of variable in the result of monomials()https://ask.sagemath.org/question/49089/substitution-of-variable-in-the-result-of-monomials/Given a polynomial in n variables, I'd like to extract the list of its monomials, and then manipulate that list by substituting certain variables for others.
Simple example: in $Z[x,y]$ consider the polynomial $1+x+y^2$; the list of its monomials (in some ordering) is $(1,x,y^2)$. My function should be able e.g. to take that list and substitute $y$ with $x$, namely return $[1,x,x^2]$.
At the moment, my code gives error, but I do not understand how to fix it.
N.<x1,x2> = PolynomialRing(ZZ, 2)
f = 1+x1+x2
g = f.monomials()
for i in range(3):
g[i] = g[i].substitute_expression(x2==x1)
Namely, how do I make a variable in the polynomial ring also have the substitute attribute? or is there a better way to achieve this?rue82Thu, 19 Dec 2019 20:18:18 +0100https://ask.sagemath.org/question/49089/Substituting variable valuehttps://ask.sagemath.org/question/49064/substituting-variable-value/ I use the following code to calculate the Kretschmann scala of the Schwartzchild spacetime:
print("Initialization of manifold, chart, and metric. Definitions of constants.")
M=Manifold(4,'M',structure='Lorentzian')
X.<t,r,th,ph> = M.chart(r"t r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi")
m=var('m')
g=M.metric()
g[0,0]=-(1-2*m/r)
g[1,1]=1/(1-2*m/r)
g[2,2]=r^2
g[3,3]=r^2*sin(th)^2
print('Calculating Riemann tensor')
R = g.riemann()
print(R[:])
uR=R.up(g)
dR=R.down(g)
print('Calculating Kretschmann scalar')
kr= uR['^{abcd}']*dR['_{abcd}']
print(kr.expr())
It works, but now I want to substitute a value for the variable m. I have tried with:
kr.substitute(m==10)
kr.subs(m==10)
kr.substitute_expression(m==10)
but none of these work. How can I make the substituteion m=10?mattiavWed, 18 Dec 2019 11:16:14 +0100https://ask.sagemath.org/question/49064/From numerical variables to symbolic variableshttps://ask.sagemath.org/question/46653/from-numerical-variables-to-symbolic-variables/I would like to define an Hamiltonian dynamics that I will integrate later using scipy.integrate. The Hamiltonian is computed with a symbolic expression and then I need to make substitution from the symbolic variables to the numerical variables.
N=2
var('q1 q2')
var('p1 p2')
zs = [q1,q2,p1,p2]
def dynq(t,z):
H = p1*(q1^2+1/3*q2^2) + p2*(cos(q1)-2*sin(q2*q1)^2)
jacHp = jacobian(H,tuple(ps))
dqdt = list(jacHp[0])
dqdt[0].subs({zs[i]:z[i] for i in range(0,2*N)})
dqdt[1].subs({zs[i]:z[i] for i in range(0,2*N)})
print(dqdt)
return dqdt
I call `dynq(0.,[1.,0.5,4.,2.])` and the output is
[q1^2, -2*sin(q1*q2)^2 + cos(q1)]
so q1,q2,p1,p2 are still in the expression and are not replaced by the numerical values of the list $z$.sagenotdeadFri, 24 May 2019 19:10:20 +0200https://ask.sagemath.org/question/46653/SUBSTITUTIONS IN SYMBOLIC EXPRESSIONShttps://ask.sagemath.org/question/45893/substitutions-in-symbolic-expressions/ Hi,
I would like to know if it is possible to substitute expressions inside some equation or expression.
Consider the following expression:
**(r+1)*(r+2)+g+a**
Then, I would like to say that I want this same expression for **c+d = r+1 +a**.
Is this possible? Can I implement something like this in SAGE?idriskameniSun, 24 Mar 2019 19:41:35 +0100https://ask.sagemath.org/question/45893/Subtitute functions - in a differential equation - Sagemanifoldhttps://ask.sagemath.org/question/44557/subtitute-functions-in-a-differential-equation-sagemanifold/Dear community,
I have a differential equation that depends on a function $\xi(t)$, but is a component of a tensor (calculated with `sagemanifold`)
M = Manifold(4, 'M', latex_name=r"\mathcal{M}")
U.<t,r,th,ph> = M.chart(r't r:(0,+oo) th:(0,pi):\theta ph:(0,2*pi):\phi')
xi = function('xi')(t)
f = function('f')(t)
h = function('h')(t)
g = function('g')(t)
Ric = M.tensor_field(0,2, 'Ric')
Ric[0,0] = 3/2*f*h - 3/4*h^2 + 3/2*f*xi + 3/4*xi^2 - 3/2*diff(h, t) - 3/2*diff(xi, t)
Ric.display()
I'd like to define the restriction to $\xi = 0$, and assign it to a new tensor
Ric0 = M.tensor_field(0,2, 'Ric0')
Ric0[0,0] = Ric[0,0].substitute_expression({xi:0, diff(xi, t):0})
Ric0.display()
but I get an `AttributeError` because the
AttributeError: 'ChartFunctionRing_with_category.element_class' object has no attribute 'substitute_expression'
I know that it works for functions
var('t')
xi = function('xi')(t)
f = function('f')(t)
h = function('h')(t)
ode = 3/2*f*h - 3/4*h^2 + 3/2*f*xi + 3/4*xi^2 - 3/2*diff(h, t) - 3/2*diff(xi, t)
ode0 = ode.substitute_expression({xi:0, diff(xi, t):0})
<h2>Question</h2>
Is there a way to substitute functions that are not `𝚜𝚊𝚐𝚎.𝚜𝚢𝚖𝚋𝚘𝚕𝚒𝚌.𝚎𝚡𝚙𝚛𝚎𝚜𝚜𝚒𝚘𝚗.𝙴𝚡𝚙𝚛𝚎𝚜𝚜𝚒𝚘𝚗` but `ChartFunctionRing_with_category.element_class`?DoxMon, 03 Dec 2018 18:33:37 +0100https://ask.sagemath.org/question/44557/substitution of ideal generators of a free algebrahttps://ask.sagemath.org/question/43903/substitution-of-ideal-generators-of-a-free-algebra/I'm trying to map the generators of an ideal $I$ of a free $k$-algebra $A = k \\{ x, y \\}$ to a different free $k$-algebra $B = k \\{ u, v \\}$ (really I'm trying something more complicated, but the failure occurs in even this simplified example). I was attempting to do this via `subs` by creating a dictionary taking $x$ to $u$ and $y$ to $v$, but this is not working.
sage: A.<x, y> = FreeAlgebra(QQ, 2)
sage: I = A*[x*y - y*x - 1]*A
sage: B.<u, v> = FreeAlgebra(QQ, 2)
sage: genMap = {'x':'u', 'y':'v'}
sage: I.gen(0).subs(genMap)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-9d969fff6a4a> in <module>()
----> 1 I.gen(Integer(0)).subs(genMap)
sage/structure/element.pyx in sage.structure.element.Element.subs (build/cythonized/sage/structure/element.c:7572)()
/usr/lib/python2.7/dist-packages/sage/algebras/free_algebra_element.pyc in __call__(self, *x, **kwds)
176 for m, c in six.iteritems(self._monomial_coefficients):
177 if result is None:
--> 178 result = c*m(x)
179 else:
180 result += c*m(x)
sage/rings/rational.pyx in sage.rings.rational.Rational.__mul__ (build/cythonized/sage/rings/rational.c:21325)()
sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel_cache_maps.bin_op (build/cythonized/sage/structure/coerce.c:10686)()
TypeError: unsupported operand parent(s) for *: 'Rational Field' and 'Free monoid on 2 generators (x, y)'
This error seems strange to me; is it not understanding elements of $\mathbb{Q}$ and $\\{ x, y \\}$ as elements of $\mathbb{Q} \\{ x, y \\}$? I also tried to do this via a homomorphism $A \to B$ but I could not get this to work, as it seems they are not fully implemented yet for free algebras, from what I could tell. Any help or alternatives would be very appreciated.
**EDIT**: I got a slight work-around, by converting the elements to strings, replacing the variables as characters, and the evaluating the string in the target. However, I feel like this is way more costly than a substitution would be, so I am still interested in a solution.rhoadskjWed, 10 Oct 2018 17:04:18 +0200https://ask.sagemath.org/question/43903/Substitution of multiple variables, for linearizationhttps://ask.sagemath.org/question/42822/substitution-of-multiple-variables-for-linearization/ Hi
we want to substitute multiple variables, for example
x1*x3 would be z1, x2*x4 would be z2 and so on.
We save all combinations of monomials in a dictionary, then we wanted to use subs to replace the variables, but it does not work.
How could one implement this?
The purpose is for linearization of a large system of equations.
A system like this:
x[1] + x[1]*x[2]*x[3] + x[1]*x[4]*x[6] + x[1] + x[4] + x[5] + x[7],
x[0]*x[1]*x[4] + x[0]*x[1] + x[2]*x[4] + x[3]*x[5]*x[6] + x[3]*x[5] + x[7] + 1,
x[1]*x[2] + x[2]*x[3]*x[6] + x[2]*x[4] + 1,
x[0]*x[5]*x[6]*x[7] + x[1]*x[7] + x[2] + x[7],
x[1]*x[3]*x[7] + x[4] + x[5]*x[6] + x[6],
x[1]*x[3] + x[2]*x[4] + x[3]*x[4]cryptoWed, 04 Jul 2018 11:11:49 +0200https://ask.sagemath.org/question/42822/Changes of variable from quartic to Weierstrasshttps://ask.sagemath.org/question/40455/changes-of-variable-from-quartic-to-weierstrass/I have the quartic curve `v^2 = u^4-2*u^3+5*u^2+8*u+4` which I convert to an elliptic curve of the form `y^2 + a1*x*y + a3*y = x^3 + a2*x^2 + a4*x + a6`. The code is given by :
var( 'u,v,a,b,c,q,x,y,d' );
f = a*u^4 + b*u^3 + c*u^2 + d*u + q^2
# we want to find an algebraic passage from u, v and the quartic
# v^2 = f
# to a Weierstass equation in two new variables x, y
x = (2*q*(v+q) + d*u)/u^2
y = ((2*q)^3*(v+q) + (2*q)^2*( d*u + c*u^2 ) - d^2*u^2)/((2*q)*u^3)
# inverse transformation:
u = ((2*q)*(x+c)-d^2/q)/y
v = -q + (u*(u*x-d))/(2*q)
# one can easily define rational functions (u,v) -> (x,y) and (x,y) -> (u,v)
# to be used in the concrete situation
a1 = d/q
a2 = c - ((d/(2*q)))^2
a3 = b*(2*q)
a4 = -a*(2*q)^2
a6 = a2*a4
((y^2 + a1*x*y + a3*y) - (x^3 + a2*x^2 + a4*x + a6)).factor()
def passageQuarticToWeierstrass( a, b, c, d, q, field=QQ ):
"""implement the standard formulas
"""
a1, a2, a3, a4 = d/q, c - ((d/(2*q)))^2, b*(2*q), -a*(2*q)^2
a6 = a2 * a4
print ( a1, a2, a3, a4, a6 )
return EllipticCurve(field,(a1, a2, a3, a4, a6))
a, b, c, d, q = 1,-2,5,8,2
E = passageQuarticToWeierstrass( a, b, c, d, q );E
The output yield :
Elliptic Curve defined by E : y^2 + 4*x*y - 8*y = x^3 + x^2 - 16*x - 16 over Rational Field.
Now, I want to transform `E` into a short Weierstrass equation which should yield : `E' : y^2 = x^3 -3267*x +45630` with the corresponding change of variable.
This is what I tried, but did not quite worked. (slightly different numerator expression but correct denominators for `u` and `v`).
reset('x')
reset('y')
var('x,y,Y')
a, b, c, d, q = 1,-2,5,8,2 #coefficients of quartic v^2
a1, a2, a3, a4, a6 = 4, 1, -8, -16, -16 #coefficients of elliptic curve E
E=y^2 + 4*x*y - 8*y -(x^3 + x^2 - 16*x - 16)
F=E.subs({y:y-(a1*x+a3)/2}) #eliminates xy and y term on the LHS of E
eq2=F.canonicalize_radical()
eq3=eq2.subs({x:x-((a2+1/4*(a1)^2)/3)}) #eliminates the x^2 term from the RHS to give E of the form y^2 = x^3 +Ax +B
eq4=eq3.canonicalize_radical()
eq5=eq4.subs({x:(1/3^2)*x})
eq6=eq5*729;eq6
u=((2*q)*(x+c)-(d^2/q))/y #the inverse transformation from from E to quartic
eq7=u.subs({y:y-(a1*x+a3)/2}).subs({x:x-((a2+1/4*(a1)^2)/3)})
eq8=eq7.subs({x : (x/3^2)}).subs({y:y/(3^3)});eq8
v = -q + (u*(u*x-d))/(2*q) #the inverse transformation from from E to quartic
eq9=v.subs({y:y-(a1*x+a3)/2}).subs({x:x-((a2+1/4*(a1)^2)/3)})
eq10=eq9.subs({x : (x/3^2)}).subs({y:y/(3^3)})
eq11=eq10.simplify_full()
eq12=eq11.subs({y^2:(x^3-3267*x+45630)});eq12
The output yield :
-x^3 + 729*y^2 + 3267*x - 45630 #wrote this in the form:Y^2 = x^3 - 3267*x +45630 where Y=3^3*y
u = -12*(x - 42)/(6*x - y - 198)
v = 2*(x^3 - 162*x^2 + 6291*x + 108*y - 37962)/(x^3 + 36*x^2 - 12*(x - 33)*y - 5643*x + 84834)
The problem lies in the expression of `u` and `v`. The correct expression is suppose to be
`u = -12*(x - 6)/(6*x - y - 198)`
and
`v = 2*((x^3-18*x^2+3267*x-324*y-71658)/(x^3+36*x^2-12*x*y-5643*x+396*y+84834))`.
I have checked and have no idea where my coding went wrong that it yields the incorrect numerator but the correct denominator in the expression of `u` and `v`. Can someone help me throw in some suggestions.ShaThu, 04 Jan 2018 02:47:48 +0100https://ask.sagemath.org/question/40455/Substitution of subexpressionhttps://ask.sagemath.org/question/39659/substitution-of-subexpression/Hi! My task is to derive some expression and then find subexpressions in it and substitute. In details I need to find derivative of psi and then substitute expressions back. The way I found this derivative looks ugly but I suppose it's easier to do substitution in such form of expression.
var('x, y, t, a, w, alpha, beta')
assume(w > 0)
assume(a > 0)
assume(x, y, t, 'real')
x1 = x*cos(w*t) + y*sin(w*t)
y1 = y*cos(w*t) - x*sin(w*t)
psi10 = 2/a * sin(2*pi/a*x2) * sin(pi/a*y2)
psi01 = 2/a * sin(pi/a*x2) * sin(2*pi/a*y2)
psi = alpha*psi10 + beta*psi01
psi_der = psi.diff(x2)*x1.diff(t) + psi.diff(y2)*y1.diff(t)
psi_der
$-2{\left(w x \cos\left(t w\right) + w y \sin\left(t w\right)\right)} {\left(\frac{\pi \alpha \cos\left(\frac{\pi y_{2}}{a}\right) \sin\left(\frac{2 \pi x_{2}}{a}\right)}{a^{2}} + \frac{2 \pi \beta \cos\left(\frac{2 \pi y_{2}}{a}\right) \sin\left(\frac{\pi x_{2}}{a}\right)}{a^{2}}\right)}+$
$+2{\left(w y \cos\left(t w\right) - w x \sin\left(t w\right)\right)} {\left(\frac{\pi \beta \cos\left(\frac{\pi x_{2}}{a}\right) \sin\left(\frac{2 \pi y_{2}}{a}\right)}{a^{2}} + \frac{2 \pi \alpha \cos\left(\frac{2\pi x_{2}}{a}\right) \sin\left(\frac{\pi y_{2}}{a}\right)}{a^{2}}\right)}$
Then I need to substitute x*cos(w*t) + y*sin(w*t) to x2 and y*cos(w*t) - x*sin(w*t) to y2 for further integration over x2 and y2.
Unfortunately, I didn't understand how to do this except that I need to use wild cards. So
w0 = SR.wild(0)
w1 = SR.wild(1)
pattern = x1*w0 + w1*y1
pattern
$\$0 {\left(x \cos\left(t w\right) + y \sin\left(t w\right)\right)} + \$1 {\left(y \cos\left(t w\right) - x \sin\left(t w\right)\right)}$
which is looks pretty the same but somehow don't match:
print psi_der.match(pattern)
gives None.
Could someone explain what is going on or maybe I should use something else?wobbuuuSun, 19 Nov 2017 11:46:08 +0100https://ask.sagemath.org/question/39659/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/Substitute piecewise function variablehttps://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/I have the following piecewise function:
phi = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi = phi.extension(0);
It appears to be a valid function since I can obtain/plot its values for any 'x'. But whenever I try to substitute the variable, it does not work. For example,
phi(2)
0
but if I declare another variable 'h' and try to input that variable into the piecewise function, it does not appear to work:
h=pi/2
phi(h)
TypeError: self must be a numeric expression
At first I thought that 'h' was not a 'numeric' or 'real' value, but when I test it, it is a real value:
h.is_real()
True
How can I overcome this? How can I successfully operate a variable substitution in my piecewise function?jrojasquFri, 24 Mar 2017 20:39:26 +0100https://ask.sagemath.org/question/37066/substitution not simplifyinghttps://ask.sagemath.org/question/35368/substitution-not-simplifying/I have the following code where everything works fine until `eq4` expression where all higher powers of `B` were simplified, unfortunately with `eq5` I can still see higher powers of `a` like `a^6` etc that were not simplified.
B,a,x,y = var('B a x y')
R=x^2+y^2;R
eq1=R.subs({x:((-12*a^3+92*a+24)*B-48*a^3-80*a+6)/(8*B-6)});eq1
eq2=eq1.subs({y:((11*a^3+65*a-52)*B-605*a^3+13*a-12)/(8*B+70)});eq2
eq3=eq2.simplify_full();eq3
eq4=eq3.subs({B: sqrt(-2)}).subs({sqrt(-2):B});eq4
eq5= eq4.subs({a^4: 5*a^2+32});eq5
which gives
1/4*(7291921*a^6 - 4678952*a^3 + 2*(3424035*a^6 + 1472328*a^3 + 24725813*a^2 + 1819560*a + 106127568)*B - 16*(19639*a^6 - 103688*a^3 + 1003105*a^2 - 163544*a + 8052784)*B - 70791609*a^2 + 10593256*a - 477643380)/(35072*B + 13999)
Is there a way I can fix this so that the only higher powers of `a` remaining aren't higher than `3` because of the `a^4` substitution.ShaThu, 03 Nov 2016 02:47:59 +0100https://ask.sagemath.org/question/35368/equation not simplifying and substituting properlyhttps://ask.sagemath.org/question/34854/equation-not-simplifying-and-substituting-properly/ I have the following code where I want to substitute t and p expression into my 'a' expression. Unfortunately what I get is a long equation which still have some powers of p and t variable in it. It seems like the substitution did not work properly
a,t,p,l,k,c,b=var('a t p l c k b')
a=(-2*p*t^2-p^2*t)+(2*t*p-p^2)+t+1;a
A=a.subs({t:((c-a)+4*l)/(a+b+c)}).subs({p:((b-c)+4*k)/(a+b+c)});A
This is what I obtained :
-(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - k - 4*l - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)/(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) + b + k - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1) + 2*(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - k - 4*l - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)*(b + 4*c - k)/((p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)*(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) + b + k - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)) + 2*(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - k - 4*l - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)^2*(b + 4*c - k)/((p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)*(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) + b + k - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)^2) - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + (2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - k - 4*l - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)*(b + 4*c - k)^2/((p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2*(2*(b + 4*c - k)*t^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) + b + k - 2*(b + 4*c - k)*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1) - (b + 4*c - k)^2*t/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + t - (b + 4*c - k)^2/(p^2*t + 2*p*t^2 + p^2 - 2*p*t - b - k - t - 1)^2 + 1)) + 1
And I also have `l^2=2c^2+2a^2-b^2` and `k^2=2b^2+2c^2-a^2`. Is there a way I can make all this substitution at once. The final answer should simplify down to 'a'.ShaMon, 19 Sep 2016 09:33:01 +0200https://ask.sagemath.org/question/34854/Substitution of several variableshttps://ask.sagemath.org/question/32720/substitution-of-several-variables/
Let $f=f(x_1(t),x_2(t))$ be defined as follows:
sage: var('t mu')
sage: x=list()
sage: x.append( function('x1')(t) )
sage: x.append( function('x2')(t) )
sage: f = x[0] - mu*x[0]^2*x[1]
sage: f
$f = x_1(t) - \mu x_1(t)^2 x_2(t)$.
Now, I need to to substitute the term containing $x_1^2(t) x_2(t)$ in $f$ by some new auxiliary variable $x_3(t)$, that is, to obtain $f = x_1(t) - \mu x_3(t) $. However, this code doesn't work:
sage: x.append( function('x3')(t) )
sage: f.subs({x[0]^2*x[1] : x[2]})
Some ideas? Thanks.
This code is motivated by symbolic manipulation with ODEs (think of $f$ as being the right-hand side term in the autonomous ODE system $\dot{x}(t) = f(x(t))$ ).mforetsSat, 05 Mar 2016 12:36:51 +0100https://ask.sagemath.org/question/32720/Is it possible to define (or assume) the derivative of a functionhttps://ask.sagemath.org/question/32669/is-it-possible-to-define-or-assume-the-derivative-of-a-function/ What i would like to do is define formal symbolic functions
`f1 = function('f1',latex_name = 'f_1')(x)`
and set their derivative or at least be able to substitute their derivative. As an example assume I defined f1,f2,f3,f4 then I would set dx f1 = f2+f3 and what I would expect is
f1.derivative(x)
to output
f2(x)+ f3(x)ywidmeThu, 25 Feb 2016 14:49:39 +0100https://ask.sagemath.org/question/32669/Substitution of parametershttps://ask.sagemath.org/question/31314/substitution-of-parameters/I would like to understand what is the correct and more efficient way for performing sostitution of parameters in objects of some category.
For example, a typical problem for me is the following. I have a 1-form in an ExteriorAlgebra, whose coefficients depends on some variable, let's say t. I find conditions on such parameters, let's say, t should be zero in order that some property holds. (In general, i will have solutions of linear systems.) I try to use "substitute" for setting the parameter to be 0, but this does not work to me..
E.<x,y> = ExteriorAlgebra(SR)
_=var("t")
theta = 3*x+t*y
print "theta = ", theta
theta2 = theta.substitute({t:0})
print "modified theta = ", theta2
Another solution I tried is the following. I assume t==0 at a certain point. But this seems to me to force the substitution before too.
E.<x,y> = ExteriorAlgebra(SR)
_=var("t")
theta = 3*x+t*y
print "theta = ", theta
assume(t==0)
theta2 = theta
print "modified theta = ", theta2
Thanks in advance for any suggestions!danieleFri, 04 Dec 2015 10:05:49 +0100https://ask.sagemath.org/question/31314/sqrt function not working properlyhttps://ask.sagemath.org/question/30325/sqrt-function-not-working-properly/ I have the following code where I want to substitute `a, b, c` into `s`. Since `s` factors as a square, I want to get the square root of it :
p, t= var('p t')
a=(-2*p*t^2-p^2*t)+(2*t*p-p^2)+t+1
b=(p*t^2+2*p^2*t)+(2*t*p-t^2)-p+1
c=(p*t^2-p^2*t)+(t^2+2*t*p+p^2)+t-p #3 sides (a,b,c) in terms of theta and phi [equation (1.1)]
s=(factor(2*c^2+2*a^2-b^2));s
S=s.sqrt();S
Unfortunately the answer I get is
sqrt((3*p*t^2 - 2*p^2 - 2*p*t + t^2 + p + 2*t + 1)^2)
The `sqrt` and the square power does not cancel off which I want it to cancel. I tried using the code `S.simplify_full()` to simplify it hoping the sqrt and square power will cancel off but no luck. Is there any other specific code I can use for that.
ShaThu, 29 Oct 2015 00:45:04 +0100https://ask.sagemath.org/question/30325/How to factor the coefficienthttps://ask.sagemath.org/question/30169/how-to-factor-the-coefficient/ I have the following code :
r,s,u= var('r s u')
U=u^4-A*u^2+17*B;U
eq1=U.subs({r:-21,s:324});eq1
factor(eq1)
which yields
(u^2 - 4281747124104997066155259836393062400)*(u^2 - 4281747146564836134743374782202380288)
My question : is there a code where I can factor the equation to yield something like this
(u^2 - 2^24*3^36*5^2*17*19^2*29*47^2*173)*(u^2 - 2^24*3^34*29*173*55229^2)
Because my aim is to look at the non-square numbers that appear in both factorization. In this case 29*173.ShaWed, 21 Oct 2015 06:25:47 +0200https://ask.sagemath.org/question/30169/substitution not working properlyhttps://ask.sagemath.org/question/30099/substitution-not-working-properly/ I have the following code and I want to substitute all B^2=17.
B,x,y= var('B x y')
eq=81*x^16*B^6 + 40662*x^15*B^3 + 14353281*x^14*B^2;eq
eq1=eq.subs({B^2: (17)});eq1
and I get
81*B^6*x^16 + 40662*B^3*x^15 + 244005777*x^14
I am not sure why the substitution didn't bother to simplify `B^6=17^3` and `B^3=17*B`
I also tried using this code which was suggested earlier :
B,x,y= var('B x y')
eq=81*x^16*B^6 + 40662*x^15*B^3 + 14353281*x^14*B^2;eq
eq1=eq.subs({B: sqrt(17)});eq1
which yields
397953*x^16 + 691254*sqrt(17)*x^15 + 244005777*x^14
and I have this `sqrt(17)` terms which is not written as B. I need it to be written as B because later on I need to collect the coefficient of B. Perhaps I should use something like while{B^2 : 17} but no idea how to do that.
ShaMon, 19 Oct 2015 01:32:21 +0200https://ask.sagemath.org/question/30099/incomplete substitution of expressionhttps://ask.sagemath.org/question/29956/incomplete-substitution-of-expression/ I want to substitute expression of x and y into the equation `y^2-x^3+3267*x-45630` which works fine. The tricky part is when i wanted to substitute `v^2=p^4-2*p^3+5*p^2+8*p+4` into the equation which would simplify to 0.
p,x,v,y= var('p x v y')
x=3*((5*p^2+24*p+12*v+24)/p^2)
y=108*((-p^3+5*p^2+2*p*v+12*p+4*v+8)/p^3)
eq1=expand(y^2-x^3+3267*x-45630); eq1
eq=eq1.subs({v^2: p^4-2*p^3+5*p^2+8*p+4});eq
Instead of getting 0 I get this expression here :
46656*v/p^2 - 93312*v/p^3 + 233280*v/p^4 - 46656*v^3/p^6 + 373248*v/p^5 + 186624*v/p^6
I notice that the substitution didn't complete because I still have v^3 which did not simplify using the substitution `v^2=p^4-2*p^3+5*p^2+8*p+4`. Is there any other correct code I should use instead of `subs` to make it work?ShaTue, 13 Oct 2015 07:44:32 +0200https://ask.sagemath.org/question/29956/Substituting derivative in equationhttps://ask.sagemath.org/question/28720/substituting-derivative-in-equation/ There is likely a simple way to solve this, but I cannot seem to find a way. Let say I have two functions, f(x,y) and g(x,y)
> var('x,y')
>f=function('f',x,y)
>g=function('g',x,y)
I want to be able to substitute a value for one of the derivatives. For example, if I have the equation x^2y+f.diff(x,1), I may want to substitute in the function g, so I have x^2y+g. I tried the following
>(x^2y+f.diff(x,1)).subs(f.diff(x,1)=g)
And get an error that a keyword can't be an expression. Is there anyway to make this substitution?zalbaWed, 29 Jul 2015 20:21:39 +0200https://ask.sagemath.org/question/28720/Has ticket #6480 been fixed?https://ask.sagemath.org/question/27217/has-ticket-6480-been-fixed/The ticket is:
>.subs_expr() method doesn't work for argument of D derivative operator
Here is an example (from Sage 6.2, which I'm currently using):
var('x,y,f,F,Fx,Fy')
y = function('y',x)
f = function('f',x,y)
d1 = diff(f,x,1)
d1.subs_expr(diff(y,x,1)==F)
F*D[1](f)(x, y(x)) + D[0](f)(x, y(x))
So far, all good. But when I try:
d1.subs_expr(diff(y,x,1)==F,diff(f,x,1)==Fx,diff(f,y,1)==Fy)
I receive the error:
TypeError: argument symb must be a symbol
Maybe my syntax is wrong, so either my question is moot, in which case what am I doing wrong? Or maybe my syntax is correct and the fault is still a ticket for `subs_expr`. Advice as always would be very welcome!AlasdairSun, 28 Jun 2015 02:47:35 +0200https://ask.sagemath.org/question/27217/substitute x*y by uhttps://ask.sagemath.org/question/25972/substitute-xy-by-u/ Hi there!
I have a polynomial $f(x,y) = x*y + x^2*y^2 + x*y^2$
I want to substitute $x*y$ instances by a new unknown $u$ (s.t. $u = x * y$)
This is how I proceeded:
# this is the default code
P.<x, y> = PolynomialRing(Zmod(5))
f = x*y + x^2*y^2 + x*y^2
# now I want to introduce the substitution so I have a f(x,y,u)
new_ring.<x,y,u> = PolynomialRing(ZZ)
ff = f.sub(x*y = u)
Of course this code doesn't work... Any idea what I could do?mimooSat, 28 Feb 2015 19:00:54 +0100https://ask.sagemath.org/question/25972/complete expansion of polynomial substitutionhttps://ask.sagemath.org/question/25827/complete-expansion-of-polynomial-substitution/ In the following code, I have substituted a polynomial in other polynomial and the result even after using the command
".expand" shows no complete expansion,Is there any other way to get complete expansion of polynomial substitution?
g1=1+ (s^2) ;
g2=g1.subs(s= 1/(z-1) ) ;
g2.expand()
Result:
1+ 1/((z-1)^2) is shown, but not expansion (z^2-z+2)/z^2-z+1.
kartheekSat, 14 Feb 2015 20:16:35 +0100https://ask.sagemath.org/question/25827/