Post Reply 
(Hyper) Dual Numbers for automatic differentiation (CAS)
02-13-2024, 07:18 AM (This post was last modified: 02-13-2024 07:39 AM by Thomas Klemm.)
Post: #3
RE: (Hyper) Dual Numbers for automatic differentiation (CAS)
This is an implementation for the HP-42S: Automatic differentiation using dual numbers.
I used complex number as representation, so we get addition, subtraction and scalar multiplication for free.

But you could use the following matrix representation instead:

\(
\begin{bmatrix}
u & {u}' \\
0 & u \\
\end{bmatrix}
\)

With this you get also multiplication and division for free:

\(
\begin{bmatrix}
u & {u}' \\
0 & u \\
\end{bmatrix}
\cdot
\begin{bmatrix}
v & {v}' \\
0 & v \\
\end{bmatrix}
=
\begin{bmatrix}
uv & u{v}'+{u}'v \\
0 & uv \\
\end{bmatrix}
\)

\(
\begin{bmatrix}
u & {u}' \\
0 & u \\
\end{bmatrix}
\cdot
\begin{bmatrix}
v & {v}' \\
0 & v \\
\end{bmatrix}^{-1}
=
\begin{bmatrix}
\frac{u}{v} & \frac{{u}'v - u{v}'}{v^2} \\
0 & \frac{u}{v} \\
\end{bmatrix}
\)

In case of function application it really depends on how it is implemented.
The result should implement the chain-rule:

\(
f \left(
\begin{bmatrix}
u & {u}' \\
0 & u \\
\end{bmatrix}
\right) =
\begin{bmatrix}
f(u) & {f}'(u){u}' \\
0 & f(u) \\
\end{bmatrix}
\)



Example

Code:
from sympy import Matrix

x = Matrix([
    [3, 1],
    [0, 3]
])

y = Matrix([
    [2, 1],
    [0, 2]
])

(x**2 + 7*y)*(7*x**2 + 5*y)

\(
\begin{bmatrix}
1679 & 2030 \\
0 & 1679
\end{bmatrix}
\)

But here we use the same \(\varepsilon = \varepsilon_{1} = \varepsilon_{2}\).
So it might not be exactly what you want.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: (Hyper) Dual Numbers for automatic differentiation (CAS) - Thomas Klemm - 02-13-2024 07:18 AM



User(s) browsing this thread: 1 Guest(s)