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.Fri, 07 May 2021 23:16:37 +0200Order of variables in partial derivative expressionshttps://ask.sagemath.org/question/57010/order-of-variables-in-partial-derivative-expressions/I want to sort a set of homogenous partial derivatives lexigraphically , e.g.
d²x dy > dxd²y > d³y
because it is more often differentiated in the first term than in the second, and so on. It is assumed that x > y. I implemented a function which just generates tuples of integers which can be sorted easily, analoguos to term ordering im multivariate polynomials. So the function returns, for the example given, the tuples:
(2,1), (1,2),(0,3)
which can easily be sorted in whatever way I want. When I switched to more complicated expression I stumbled over the follwing problem:
P.<x,y,z, t> = PolynomialRing(QQ, 4, order='lex')
print(x>y>z>t)
u = function("u")(x,y,z,t)
v = function("v")(x,y,z,t)
w = function("w")(x,y,z,t)
d1 = diff(u(x,y,z,t), x, t, t, z)
d2 = diff(v(x,y,z,t), z,z,t,t)
print (pylie.order_of_derivative(d1))
print (pylie.order_of_derivative(d2))
print (u.variables())
print (u)
print (d1)
gives the output (my function is called 'order_of_derivative')
True
[0, 1, 2, 1]
[0, 2, 2, 0]
(t, x, y, z)
u(x, y, z, t)
diff(u(y, z, t, x), z, t, t, x)
So one can see that we have given an order to the variables which seems to work). But the two tuples don't behave like expected: *d1* 1st index is 0, though I expected 1 because the is a derivaton for x. When I look at the variables I can see the order *(t, x, y, z)*, the function shows *u( x, y, z, t)*, and the derivative shows *u(y,z,t,x)*, which is yet another order. Here is my function:
def order_of_derivative (e):
opr = e.operator ()
opd = e.operands ()
if not isinstance(opr, sage.symbolic.operators.FDerivativeOperator):
return [0] * len (e.variables())
res = [opr.parameter_set().count(i) for i in range (len(opd))]
return res
so the internal sort order seems to be lexicographicallly *t,x,y,z*. Of course I can build some complex workarounds, but my question is if there is a **simple** solution for that problem.
Fri, 07 May 2021 18:35:11 +0200https://ask.sagemath.org/question/57010/order-of-variables-in-partial-derivative-expressions/Answer by rburing for <p>I want to sort a set of homogenous partial derivatives lexigraphically , e.g. </p>
<pre><code>d²x dy > dxd²y > d³y
</code></pre>
<p>because it is more often differentiated in the first term than in the second, and so on. It is assumed that x > y. I implemented a function which just generates tuples of integers which can be sorted easily, analoguos to term ordering im multivariate polynomials. So the function returns, for the example given, the tuples:</p>
<pre><code>(2,1), (1,2),(0,3)
</code></pre>
<p>which can easily be sorted in whatever way I want. When I switched to more complicated expression I stumbled over the follwing problem:</p>
<pre><code>P.<x,y,z, t> = PolynomialRing(QQ, 4, order='lex')
print(x>y>z>t)
u = function("u")(x,y,z,t)
v = function("v")(x,y,z,t)
w = function("w")(x,y,z,t)
d1 = diff(u(x,y,z,t), x, t, t, z)
d2 = diff(v(x,y,z,t), z,z,t,t)
print (pylie.order_of_derivative(d1))
print (pylie.order_of_derivative(d2))
print (u.variables())
print (u)
print (d1)
</code></pre>
<p>gives the output (my function is called 'order_of_derivative')</p>
<pre><code>True
[0, 1, 2, 1]
[0, 2, 2, 0]
(t, x, y, z)
u(x, y, z, t)
diff(u(y, z, t, x), z, t, t, x)
</code></pre>
<p>So one can see that we have given an order to the variables which seems to work). But the two tuples don't behave like expected: <em>d1</em> 1st index is 0, though I expected 1 because the is a derivaton for x. When I look at the variables I can see the order <em>(t, x, y, z)</em>, the function shows <em>u( x, y, z, t)</em>, and the derivative shows <em>u(y,z,t,x)</em>, which is yet another order. Here is my function:</p>
<pre><code>def order_of_derivative (e):
opr = e.operator ()
opd = e.operands ()
if not isinstance(opr, sage.symbolic.operators.FDerivativeOperator):
return [0] * len (e.variables())
res = [opr.parameter_set().count(i) for i in range (len(opd))]
return res
</code></pre>
<p>so the internal sort order seems to be lexicographicallly <em>t,x,y,z</em>. Of course I can build some complex workarounds, but my question is if there is a <strong>simple</strong> solution for that problem.</p>
https://ask.sagemath.org/question/57010/order-of-variables-in-partial-derivative-expressions/?answer=57013#post-id-57013Your `u` variable is a symbolic expression, and the function call syntax with positional/unnamed arguments -- that you use when you write `u(x,y,z,t)` -- is deprecated, exactly because it is confusing.
You should write instead
d1 = diff(u,x,t,t,z)
d2 = diff(v,z,z,t,t)
Also, you defined `x,y,z,t` as generators of a polynomial ring, but when you substitute them into a symbolic expression (like your symbolic function) they are coerced to symbolic variables of the same name, so you might as well have defined `var('x,y,z,t')`.Fri, 07 May 2021 23:16:37 +0200https://ask.sagemath.org/question/57010/order-of-variables-in-partial-derivative-expressions/?answer=57013#post-id-57013