# 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')
Sage: grad(F).display()
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.

Thanks very much for any help you can give me!

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 areconstantw.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: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.

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

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.]

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:Of course this can be improved (draw parentheses only if necessary, skip zeros, extract signs, etc.)

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.