-
Notifications
You must be signed in to change notification settings - Fork 44
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 support for calculating eigenvalues and eigenvectors #170
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aa59d8f
add support for eigen values and eigen vectors
hyrodium b3cc7f0
update methods for eigen
hyrodium 6d5bbf4
update tests for eigen
hyrodium b12554c
update 3d eigen
hyrodium 3b42712
add eigen methods for 2d
hyrodium 8f6bc93
update tests for eigen
hyrodium 2ff7b11
update tests for Julia 1.0
hyrodium e0f04c4
update tests for Julia 1.0
hyrodium ca3d870
fix bug in eigen test
hyrodium 4014fc8
fix bug in eigen test
hyrodium 143f9aa
update performance
hyrodium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 3D | ||
function LinearAlgebra.eigvals(R::Rotation{3}) | ||
θ = rotation_angle(R) | ||
return SVector(exp(-θ*im), exp(θ*im), 1) | ||
end | ||
|
||
function LinearAlgebra.eigvecs(R::Rotation{3}) | ||
n3 = normalize(rotation_axis(R)) | ||
n1 = normalize(perpendicular_vector(n3)) | ||
n2 = normalize(n3×n1) | ||
v1 = normalize(n1 + n2*im) | ||
v2 = normalize(n1*im + n2) | ||
v3 = n3 | ||
return hcat(v1,v2,v3) | ||
end | ||
|
||
function LinearAlgebra.eigen(R::Rotation{3}) | ||
return eigen(AngleAxis(R)) | ||
end | ||
|
||
function LinearAlgebra.eigen(R::AngleAxis) | ||
λs = eigvals(R) | ||
vs = eigvecs(R) | ||
return Eigen(λs, vs) | ||
end | ||
|
||
|
||
# 2D | ||
function LinearAlgebra.eigvals(R::Rotation{2}) | ||
θ = rotation_angle(R) | ||
return SVector(exp(-θ*im), exp(θ*im)) | ||
end | ||
|
||
function LinearAlgebra.eigvecs(R::Rotation{2}) | ||
return @SMatrix [1/√2 1/√2;im/√2 -im/√2] | ||
end | ||
|
||
function LinearAlgebra.eigen(R::Rotation{2}) | ||
λs = eigvals(R) | ||
vs = eigvecs(R) | ||
return Eigen(λs, vs) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
@testset "Eigen_3D" begin | ||
all_types = (RotMatrix{3}, AngleAxis, RotationVec, | ||
UnitQuaternion, RodriguesParam, MRP, | ||
RotXYZ, RotYZX, RotZXY, RotXZY, RotYXZ, RotZYX, | ||
RotXYX, RotYZY, RotZXZ, RotXZX, RotYXY, RotZYZ, | ||
RotX, RotY, RotZ, | ||
RotXY, RotYZ, RotZX, RotXZ, RotYX, RotZY) | ||
oneaxis_types = (RotX, RotY, RotZ) | ||
|
||
@testset "$(T)" for T in all_types, F in (one, rand) | ||
R = F(T) | ||
λs = eigvals(R) | ||
vs = eigvecs(R) | ||
E = eigen(R) | ||
v1 = vs[:,1] | ||
v2 = vs[:,2] | ||
v3 = vs[:,3] | ||
@test R * vs ≈ transpose(λs) .* vs | ||
@test norm(v1) ≈ 1 | ||
@test norm(v2) ≈ 1 | ||
@test norm(v3) ≈ 1 | ||
@test E.values == λs | ||
@test E.vectors == vs | ||
if !(T in oneaxis_types) && VERSION ≥ v"1.2" | ||
# If the rotation angle is in [0°, 180°], then the eigvals will be equal. | ||
# Note that the randomized RotX (and etc.) have rotation angle in [0°, 360°]. | ||
# This needs Julia(≥1.2) to get sorted eigenvalues in a canonical order | ||
# See https://github.com/JuliaLang/julia/pull/21598 | ||
@test eigvals(R) ≈ eigvals(collect(R)) | ||
end | ||
end | ||
end | ||
|
||
@testset "Eigen_2D" begin | ||
all_types = (RotMatrix{2}, Angle2d) | ||
|
||
@testset "$(T)" for T in all_types, θ in 0.0:0.1:π | ||
R = T(Angle2d(θ)) | ||
λs = eigvals(R) | ||
vs = eigvecs(R) | ||
E = eigen(R) | ||
v1 = vs[:,1] | ||
v2 = vs[:,2] | ||
@test R * vs ≈ transpose(λs) .* vs | ||
@test norm(v1) ≈ 1 | ||
@test norm(v2) ≈ 1 | ||
@test E.values == λs | ||
@test E.vectors == vs | ||
if VERSION ≥ v"1.2" | ||
# If the rotation angle θ is in [0°, 180°], then the eigvals will be equal. | ||
# Note that the randomized RotX (and etc.) have rotation angle in [0°, 360°]. | ||
# This needs Julia(≥1.2) to get sorted eigenvalues in a canonical order | ||
# See https://github.com/JuliaLang/julia/pull/21598 | ||
@test eigvals(R) ≈ eigvals(collect(R)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How much work is repeated here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't quite understand the question.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant, some of the calculation going into the eigenvalues is probably being recalculated in
eigvecs
. Not the way it is currently written but it usual that it falls out that way.I doubt it has a massive performance impact here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I've updated code and avoid converting to
AngleAxis
twice.Before performance update
After performance update