From 04145c416bb64321271e3487e9ed9a6bfe708629 Mon Sep 17 00:00:00 2001 From: Chris Binz Date: Sun, 18 Dec 2016 15:51:58 -0500 Subject: [PATCH] Allow multiplication with scalar of different type Add tests for scalar multiplication Add spaces to remove ambiguity --- src/pdmat.jl | 2 +- test/generics.jl | 24 ++++++++++++++++++++++++ test/runtests.jl | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/generics.jl diff --git a/src/pdmat.jl b/src/pdmat.jl index e61546d..b1eccfb 100644 --- a/src/pdmat.jl +++ b/src/pdmat.jl @@ -34,7 +34,7 @@ function pdadd!(r::Matrix, a::Matrix, b::PDMat, c) _addscal!(r, a, b.mat, c) end -*{T<:Real}(a::PDMat{T}, c::T) = PDMat(a.mat * c) +*{S<:Real, T<:Real}(a::PDMat{S}, c::T) = PDMat(a.mat * c) *(a::PDMat, x::StridedVecOrMat) = a.mat * x \(a::PDMat, x::StridedVecOrMat) = a.chol \ x diff --git a/test/generics.jl b/test/generics.jl new file mode 100644 index 0000000..86ec859 --- /dev/null +++ b/test/generics.jl @@ -0,0 +1,24 @@ + +# test operators with pd matrix types +using PDMats +using Base.Test + +# test scalar multiplication +print_with_color(:blue, "Testing scalar multiplication\n") +pm1 = PDMat(eye(3)) +pm2 = PDiagMat(ones(3)) +pm3 = ScalMat(3,1) + +pm1a = PDMat(3.0 .* eye(3)) +pm2a = PDiagMat(3.0 .* ones(3)) +pm3a = ScalMat(3, 3) + +pmats = Any[pm1, pm2, pm3] +pmatsa= Any[pm1a,pm2a,pm3a] + +for i in 1:length(pmats) + @test full(3.0 * pmats[i]) == full(pmatsa[i]) + @test full(pmats[i] * 3.0) == full(pmatsa[i]) + @test full(3 * pmats[i]) == full(pmatsa[i]) + @test full(pmats[i] * 3) == full(pmatsa[i]) +end diff --git a/test/runtests.jl b/test/runtests.jl index 8adef3f..a527e0a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -tests = ["pdmtypes", "addition"] +tests = ["pdmtypes", "addition", "generics"] println("Running tests ...") for t in tests