Skip to content

Instantly share code, notes, and snippets.

@c42f
Forked from andyferris/SimpleSymbolic.jl
Last active July 26, 2016 03:05
Show Gist options
  • Save c42f/7daa9c64670df0cc7600be7f8cf65e91 to your computer and use it in GitHub Desktop.
Save c42f/7daa9c64670df0cc7600be7f8cf65e91 to your computer and use it in GitHub Desktop.
Simply symbolic manipulations and some matrix math for Euler angle rotations
module SimpleSymbolic
immutable S{Ex}
x::Ex
end
macro S(ex)
Expr(:call, :S, Expr(:quote, ex))
end
Base.show(io::IO, s::S) = print(io, s.x)
import Base: +, *, -, /
-(a::S) = S(:(-$(a.x)))
+(a::S, b::S) = S(:($(a.x) + $(b.x)))
+(a::S, b::Number) = b == 0 ? a : a + S(b)
+(a::Number, b::S) = a == 0 ? b : S(a) + b
-(a::S, b::S) = S(:($(a.x) - $(b.x)))
-(a::S, b::Number) = b == 0 ? -a : a - S(b)
-(a::Number, b::S) = a == 0 ? b : S(a) - b
*(a::S, b::S) = S(:($(a.x) * $(b.x)))
*(a::S, b::Number) = b == 0 ? 0 : b == 1 ? a : a * S(b)
*(a::Number, b::S) = a == 0 ? 0 : a == 1 ? b : S(a) * b
/(a::S, b::S) = S(:($(a.x) / $(b.x)))
/(a::S, b::Number) = b == 1 ? a : a / S(b)
/(a::Number, b::S) = a == 0 ? 0 : S(a) / b # Hmm, assumes b != 0
export S, @S
end # module
#-------------------------------------------------------------------------------
using SimpleSymbolic
s1 = @S sin(θ₁)
c1 = @S cos(θ₁)
s2 = @S sin(θ₂)
c2 = @S cos(θ₂)
s3 = @S sin(θ₃)
c3 = @S cos(θ₃)
mx1 = [1 0 0;
0 c1 -s1;
0 s1 c1]
my1 = [c1 0 s1;
0 1 0;
-s1 0 c1]
mz1 = [c1 -s1 0;
s1 c1 0;
0 0 1]
mx2 = [1 0 0;
0 c2 -s2;
0 s2 c2]
my2 = [c2 0 s2;
0 1 0;
-s2 0 c2]
mz2 = [c2 -s2 0;
s2 c2 0;
0 0 1]
mx3 = [1 0 0;
0 c3 -s3;
0 s3 c3]
my3 = [c3 0 s3;
0 1 0;
-s3 0 c3]
mz3 = [c3 -s3 0;
s3 c3 0;
0 0 1]
v = [@S(v[1]), @S(v[2]), @S(v[3])]
myx = my1 * mx2
mxy = mx1 * my2
mxz = mx1 * mz2
mzx = mz1 * mx2
mzy = mz1 * my2
myz = my1 * mz2
myxy = my1 * mx2 * my3
myxz = my1 * mx2 * mz3
mxyx = mx1 * my2 * mx3
mxyz = mx1 * my2 * mz3
mxzx = mx1 * mz2 * mx3
mxzy = mx1 * mz2 * my3
mzxz = mz1 * mx2 * mz3
mzxy = mz1 * mx2 * my3
mzyz = mz1 * my2 * mz3
mzyx = mz1 * my2 * mx3
myzy = my1 * mz2 * my3
myzx = my1 * mz2 * mx3
@c42f
Copy link
Author

c42f commented Jul 26, 2016

@awbsmith for your amusement, here's a way to construct symbolic representations of Euler rotations directly in Julia. Just a refactor of what @andyferris has been hacking on.

@c42f
Copy link
Author

c42f commented Jul 26, 2016

Oops, just fixed a rather horrible copy & paste bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment