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/subs(_expr) not working properly?https://ask.sagemath.org/question/9711/subs_expr-not-working-properly/Hi, I am trying to substitute in the following expression:
denominator = -1/2*((P3 - sqrt(P1^2 + P2^2 + P3^2))*(P1*m/(P1^2 + P2^2 + P3^2 +
sqrt(P1^2 + P2^2 + P3^2)*P0) - I*P2*m/(P1^2 + P2^2 + P3^2 + sqrt(P1^2 +
P2^2 + P3^2)*P0) + 2*P1 - 2*I*P2) - (P1 - I*P2)*(P3*m/(P1^2 + P2^2 +
P3^2 + sqrt(P1^2 + P2^2 + P3^2)*P0) + 2*P0 + 2*P3 - sqrt(P1^2 + P2^2 +
P3^2)*m/(P1^2 + P2^2 + P3^2 + sqrt(P1^2 + P2^2 +
P3^2)*P0)))/sqrt(1/2*P3*m/(P1^2 + P2^2 + P3^2 + sqrt(P1^2 + P2^2 +
P3^2)*P0) + P0 + P3 - 1/2*sqrt(P1^2 + P2^2 + P3^2)*m/(P1^2 + P2^2 + P3^2
+ sqrt(P1^2 + P2^2 + P3^2)*P0))
which uses the following variables:
var('P,P0,P1,P2,P3,Q0,Q1,Q2,Q3,q0,q1,q2,q3,m')
As you can see its not a trivial one. I am now trying to introduce some abbreviations to make the expression more readable, i.e.
denominator.subs_expr(P1^2+P2^2+P3^2==P)
This command only work partially, i.e. it replaces the expressions inside a square root, but not the remaining ones:
-1/2*((P3 - sqrt(P))*(P1*m/(P1^2 + P2^2 + P3^2 + sqrt(P)*P0) -
I*P2*m/(P1^2 + P2^2 + P3^2 + sqrt(P)*P0) + 2*P1 - 2*I*P2) - (P1 -
I*P2)*(P3*m/(P1^2 + P2^2 + P3^2 + sqrt(P)*P0) + 2*P0 + 2*P3 -
sqrt(P)*m/(P1^2 + P2^2 + P3^2 + sqrt(P)*P0)))/sqrt(1/2*P3*m/(P1^2 + P2^2
+ P3^2 + sqrt(P)*P0) + P0 + P3 - 1/2*sqrt(P)*m/(P1^2 + P2^2 + P3^2 +
sqrt(P)*P0))
Is that the supposed behavior? How can I achieve what I wanted?cschwanWed, 16 Jan 2013 09:44:03 +0100https://ask.sagemath.org/question/9711/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/substitute algebraic numbers into a symbolic expressionhttps://ask.sagemath.org/question/9432/substitute-algebraic-numbers-into-a-symbolic-expression/How can I substitute algebraic numbers into a symbolic expression, in order to evaluate that expression with specific values for these variables? I tried the following:
var('a')
(a*3).substitute(a=AA(2))
but this yields
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_43.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("KGEqMykuc3Vic3RpdHV0ZShhPUFBKDEpKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/private/var/folders/ft/9rycr1td4nxdkvbf122dg4rr0000gn/T/tmpaq8Q8E/___code___.py", line 3, in <module>
exec compile(u'(a*_sage_const_3 ).substitute(a=AA(_sage_const_1 ))
File "", line 1, in <module>
File "expression.pyx", line 3759, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:18273)
File "expression.pyx", line 2304, in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:13187)
File "parent_old.pyx", line 234, in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3573)
File "parent.pyx", line 1000, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8227)
TypeError: no canonical coercion from Algebraic Real Field to Symbolic Ring
How could I modify my code above to compute the result `6` using algebraic numbers along the way? In my real life scenario, I have several variables, so turning everything into polynomials in between doesn't feel right. On the other hand, all my coefficients in my expressions are integers, so there should be no problems due to inexact floating point numbers.MvGTue, 16 Oct 2012 08:43:20 +0200https://ask.sagemath.org/question/9432/Substitution using Dictionary with Matrix as Valuehttps://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/As a newcomer to SAGE, trying to use it to do symbolic linear algebra, I am wondering why substitution of a variable using a dictionary doesn't work in this case:
sage: aMatrix = matrix(SR,1,1)
sage: var('aVariable')
aVariable
sage: aDict = {}
sage: aDict[aVariable] = aMatrix
sage: aDict[aVariable]
[0]
but:
sage: aVariable.subs(aDict)
....
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:16025)()
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:11265)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent_old.so in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3369)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8912)()
TypeError: no canonical coercion from Full MatrixSpace of 1 by 1 dense matrices over Symbolic Ring to Symbolic Ring
Functionality to substitute matrices for variables seems to be indispensable to doing symbolic linear algebra, so I am sure there is a proper way to do this.
heatkernelThu, 14 Jun 2012 17:58:48 +0200https://ask.sagemath.org/question/9075/Substituting for a matrix of variableshttps://ask.sagemath.org/question/8888/substituting-for-a-matrix-of-variables/I constructed a matrix of variables in the following way:
n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
show(S)
When i tried to substitute one of the variables with zero, it did not work.
n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
f = matrix([[S[i][j] for i in range(n)] for j in range(n)])
f({S[0][0]:0})
Interestingly though, the following snippet, decidedly the same to me, works with a similar syntax
xx = [var('alpha_%d'% (i+1)) for i in range(n)]
n = 3
R = PolynomialRing(RR,[['r','t'][cmp(int(i/n),i%n)]+'_'+str(1+int(i/n))+str(1+i%n) for i in range(n^2)])
S = matrix(R,3,R.gens())
def kron(i,j):
if i==j: return 1
else: return 0
def func(S,xx):
return [[(-1/2)*xx[i]*S[i][j]*kron(i,j) for i in range(n)]for j in range(n)]
f = matrix(func(S,xx))
diff_S = S*f.conjugate_transpose()*S - f
print diff_S({S[0][0]:0})
d3banjanSun, 15 Apr 2012 12:39:09 +0200https://ask.sagemath.org/question/8888/Substituting multiple valueshttps://ask.sagemath.org/question/8292/substituting-multiple-values/What is the best way of substituting a list (or vector or whatever) of values into an expression? For example, suppose I have
z = var('x y')
zvals = (1, 2)
w = x^2 + y^2
and want to substitute `zvals` for `z` in the expression `w`. I have tried the following commands:
w.subs(z=zvals) #doesn't work
w.subs({z:zvals}) #doesn't work
w.subs(x=1,y=2) #fine, but cumbersome if z has many elements
w.subs(z[0]=zvals[0],z[1]=zvals[1]) #doesn't work
w.subs({z[0]:zvals[0],z[1]:zvals[1]}) #fine, and could turn this into a loop
#if there are many variables, but ugly
w.subs(dict(zip(z,zvals))) #best I can come up with
As far as I can see, none of this behaviour changes if `z` and `zvals` are vectors or lists instead of tuples.
Is there a simpler way of doing this? Also, why doesn't the fourth attempt work when the fifth one does - is this down to a limitation of Python?
EDIT: I also realised that you can do `w.subs(z[0]==zvals[0])` but `w.subs(z[0]==zvals[0],z[1]==zvals[1])` won't work - why is this?SagenoobThu, 25 Aug 2011 10:09:33 +0200https://ask.sagemath.org/question/8292/Is applying a ring homomorphism faster than symbolic substitution?https://ask.sagemath.org/question/7921/is-applying-a-ring-homomorphism-faster-than-symbolic-substitution/I'm working on a project where I need to do composition of polynomials; something like
P(Q1 + Q2)
where `P`, `Q1`, and `Q2` are univariate polynomials with several hundred terms, and large integer coefficients (on the order of 10^10 or so). I've been doing this with the `.subs()` method which, I think, moves things to the symbolic ring and does the substitutions there. (I think this because when I get errors, they have to do with coercing to or from the symbolic ring.) But it occurred to me I could also define a ring homomorphism sending the variable of `P` to `Q1 + Q2`, and then apply the homomorphism to `P`.
So my question: will this be worth my while, or are the ring homomorphism methods too slow?
nilesSat, 05 Feb 2011 11:41:57 +0100https://ask.sagemath.org/question/7921/