Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add matrix exponentials #46

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Quaternion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,25 @@ function slerp(qa::Quaternion{T}, qb::Quaternion{T}, t::T) where {T}
qa.v3 * ratio_a + qm.v3 * ratio_b,
)
end

function _complex_representation(A::Matrix{Quaternion{T}}) where {T}
# convert a quatenion matrix to corresponding complex matrix
sw2030 marked this conversation as resolved.
Show resolved Hide resolved
a = map(t->t.s, A)
b = map(t->t.v1, A)
c = map(t->t.v2, A)
d = map(t->t.v3, A)

return [a+im*b c+im*d;-c+im*d a-im*b]
end

function _quaternion_representation(A::Matrix{Complex{T}}) where {T}
n = round(Int, size(A, 1)/2)
sw2030 marked this conversation as resolved.
Show resolved Hide resolved
a = real(A[1:n, 1:n])
sw2030 marked this conversation as resolved.
Show resolved Hide resolved
b = imag(A[1:n, 1:n])
c = real(A[1:n, n+1:2n])
d = imag(A[1:n, n+1:2n])
return [Quaternion(a[i,j], b[i,j], c[i,j], d[i,j]) for i in 1:n, j in 1:n]
end

# A quick way of doing the quaternionic matrix exponential
exp(A::Matrix{Quaternion{T}}) where {T} = _quaternion_representation(exp(_complex_representation(A)))
5 changes: 3 additions & 2 deletions src/Quaternions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Quaternions

import Base: +, -, *, /, ^
import Base: abs, abs2, angle, conj, cos, exp, inv, isfinite, log, real, sin, sqrt
import Base: convert, promote_rule, float
import LinearAlgebra: norm, normalize
import Base: convert, promote_rule, float, imag
sw2030 marked this conversation as resolved.
Show resolved Hide resolved
import LinearAlgebra: norm, normalize, exp
sw2030 marked this conversation as resolved.
Show resolved Hide resolved

include("Quaternion.jl")
include("Octonion.jl")
Expand All @@ -32,4 +32,5 @@ module Quaternions
export qrotation
export rotationmatrix
export slerp
export exp
end