# How are symbolic derivatives composed in quaternions?

The scripts below were run in: Sage Cell Server, version: 'SageMath version 9.0, Release Date: 2020-01-01' I am a new user of SageMath. I have previously used math packages, but SageMath is above and beyond all I have encountered before. It also has remarkably comprehensive documentation. In particular I have found Vector calculus with SageMath and Sage Reference Manual: Quaternion Algebras (insufficient karma to post links). I note that the latter is dated Jan 01, 2020. The examples below are drawn from those two sources. I am searching for calculus tools in the quaternion algebra package. I want to do something like this, which works in EuclideanSpace:

Sage: %display latex
Sage: from sage.manifolds.operators import *
Sage: E.<x,y,z> = EuclideanSpace()
Sage: F = E.scalar_field(function('f')(x,y,z), name='F')
grad(F) = d(f)/dx e_x + d(f)/dy e_y + d(f)/dz e_z The EucliedanSpace also knows how to pretty-print it with LaTeX.


This is as close as I have gotten with quaternions:

Sage: N.<a,b,c,d,y> = QQ[]
Sage: Q.<i,j,k> = QuaternionAlgebra(SR,-1,-1)
Sage: def qd(u):
Sage:     w = j*(diff(u[0],y) + diff(u[1],y)*i + diff(u[2],y)*j + diff(u[3],y)*k)
Sage:     return w
Sage: b = sin(y)
Sage: f = a+b*i+c*j+d*k
Sage: t = qd(f)
Sage: show(t)
(-cos(y))*k


This is the correct quaternion result, but I want to change the declaration of "b" so that I get something like (-d(f)/dy) * k. Here are the problems that concern me.

1. "diff" does not correctly handle "f" as an argument. It returns 0.
2. The definition in the function "qd" (quaternion derivative) of w should contain 3 more rows, but this one is enough to illustrate my main issue: If "b" is not defined as a specific function (e.g. sin(y)), "diff" returns 0. I would like it to return the derivative display formula, as the Euclidean example does.
3. Replacing show(t) with t.display() returns error messages such as "object has no attribute 'blah_blah'" and "raise AttributeError(dummy_error_message)." There may still be some work in progress here.


I hope there is presently a solution within SageMath. Please adapt the second script or give me an example script.

If not, I am reasonably competent with Python 3. If someone can give me links to the relevant source for the EuclideanSpace methods of "grad" and "function" and to the QuaternionAlgebra source for "diff," I may be able to add a method or two to the QuaternionAlgebra implementation and advance the development of that part of the system, or at least register myself as a beta-tester.

edit retag close merge delete

I don't know anything about quaternion derivatives, but in your attempt a,b,c,d are first defined as generators of a polynomial ring (hence they are constant w.r.t. y); then the variable b is overwritten to be a symbolic expression dependent on y, hence the result. You are not using any symbolic function (dependent on y) in your attempt, in contrast to the Euclidean example. For example, instead of your definitions of N,a,b,c,d,y, how about this:

sage: var('y')
sage: f = function('a')(y) + function('b')(y)*i + function('c')(y)*j + function('d')(y)*k
sage: qd(f)
-diff(c(y), y) + diff(d(y), y)*i + diff(a(y), y)*j + (-diff(b(y), y))*k

( 2020-02-04 15:31:00 -0500 )edit

That is a big step in the right direction! The individual function components make it through the operator. Perhaps I am asking too much, but here is a script implementing your change (now in 3 dimensions) and generating two lines of output.

%display latex
N.<x,y,z> = QQ[]
Q.<i,j,k> = QuaternionAlgebra(SR,-1,-1)
def qd(u):
w = j*(derivative(u[0],y) + derivative(u[1],y)*i + derivative(u[2],y)*j + derivative(u[3],y)*k)
return w
f = function('a')(x,y,z) + function('b')(x,y,z)*i + function('c')(x,y,z)*j + function('d')(x,y,z)*k
g = qd(f)
show(g)
E.<u,v,w> = EuclideanSpace()
F = E.scalar_field(function('f')(u,v,w), name='F')
from sage.manifolds.operators import *


When I run this script, I get two lines...

( 2020-02-04 16:29:12 -0500 )edit

I get two lines. The first is equivalent to what you have suggested, but in 3 dimensions. The second is the LaTeX massaging of a comparable formula, the kind of result I would dearly love to produce in the quaternion example. (Quaternions are the natural 'coordinates' for describing Maxwell's Equations and related physics.) [This continuation is necessary because I hit the maximum number of characters in the preceding post.]

( 2020-02-04 16:33:41 -0500 )edit

It seems LaTeX formulas for elements of QuaternionAlgebra are unfortunately not yet implemented. As a workaround you can make your own e.g. like this:

def qlatex(u):
return LatexExpr(' + '.join(r'\bigg[' + latex(u[k]) + r'\bigg]\cdot' + latex(Q.basis()[k]) for k in range(len(Q.basis()))))
show(qlatex(g))


Of course this can be improved (draw parentheses only if necessary, skip zeros, extract signs, etc.)

( 2020-02-04 16:52:20 -0500 )edit

Issue #6183 Quaternion algebra latexification has been open for 11 years :) If you want to revive it, you could bring it up (with or without patch) on sage-devel. The old patch linked in the issue also has the start of a _latex_ method.

( 2020-02-04 17:18:18 -0500 )edit

Sort by » oldest newest most voted

1) Use symbolic functions, e.g.:

sage: var('y')
sage: f = function('a')(y) + function('b')(y)*i + function('c')(y)*j + function('d')(y)*k
sage: qd(f)
-diff(c(y), y) + diff(d(y), y)*i + diff(a(y), y)*j + (-diff(b(y), y))*k


2) Do the LaTeX yourself (for now), e.g. like this:

def qlatex(u):
return LatexExpr(' + '.join(r'\bigg[' + latex(u[k]) + r'\bigg]\cdot' + latex(Q.basis()[k]) for k in range(len(Q.basis()))))
show(qlatex(g))


Of course this can be improved (draw parentheses only if necessary, skip zeros, extract signs, etc.)

The documentation of LatexExpr is under LaTeX printing support.

3) Issue #6183 Quaternion algebra latexification has been open for 11 years.

If you want to revive it, you could bring it up (with or without patch) on sage-devel.

The old patch linked in the issue also has the start of a _latex_ method.

more

With this demonstration of 'LatexExpr' you have given me all the answers I need. Thanks, rburing! Looking around, I have found Sage, LaTeX and Friends (https://doc.sagemath.org/html/en/tuto...) LaTeX macros (https://doc.sagemath.org/html/en/refe...) and Using SageTeX (https://doc.sagemath.org/html/en/tuto...). None of them contain the string "LatexExpr." If you can suggest documentation for it, I would appreciate seeing it, but I now consider this question answered.

If I can figure out how to upvote or thumbs-up your avatar, I will do so.

more