Skip to content

Commit

Permalink
Test for scale and scale! to not assume commutivity. Issue #13690
Browse files Browse the repository at this point in the history
  • Loading branch information
sarvghotra committed Dec 24, 2015
1 parent 155494d commit 997bcc6
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/linalg/generic.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
# This file is a part of Julia. License is MIT: http://julialang.org/license

import Base: *
using Base.Test

# A custom Quaternion type with minimal defined interface and methods.
# Used to test scale and scale! methods to show non-commutativity.
immutable Quaternion{T<:Real} <: Number
s::T
v1::T
v2::T
v3::T
norm::Bool
end
Quaternion(s::Real, v1::Real, v2::Real, v3::Real, n::Bool = false) =
Quaternion( promote(s, v1, v2, v3)..., n)
Quaternion(a::Vector) = Quaternion(0, a[1], a[2], a[3])
(*)(q::Quaternion, w::Quaternion) = Quaternion(q.s*w.s - q.v1*w.v1 - q.v2*w.v2 - q.v3*w.v3,
q.s*w.v1 + q.v1*w.s + q.v2*w.v3 - q.v3*w.v2,
q.s*w.v2 - q.v1*w.v3 + q.v2*w.s + q.v3*w.v1,
q.s*w.v3 + q.v1*w.v2 - q.v2*w.v1 + q.v3*w.s,
q.norm && w.norm)

debug = false

srand(123)
Expand Down Expand Up @@ -119,6 +138,12 @@ b = randn(Base.LinAlg.SCAL_CUTOFF) # make sure we try BLAS path
@test isequal(scale(BigFloat[1.0], 2.0im), Complex{BigFloat}[2.0im])
@test isequal(scale(BigFloat[1.0], 2.0f0im), Complex{BigFloat}[2.0im])

# test scale and scale! for non-commutative multiplication
q = Quaternion([0.44567, 0.755871, 0.882548, 0.423612])
qmat = []
push!(qmat, Quaternion([0.015007, 0.355067, 0.418645, 0.318373]))
@test scale!(q, copy(qmat)) != scale!(copy(qmat), q)

# test ops on Numbers
for elty in [Float32,Float64,Complex64,Complex128]
a = rand(elty)
Expand Down

0 comments on commit 997bcc6

Please sign in to comment.