Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2646 from marcusps/kron-fix

RFC: A _working_ fix to Kronecker product
  • Loading branch information...
commit 4514791a0146b3ced0c3d0c88ae0b23a863aa2eb 2 parents 26fe85f + 894001d
@ViralBShah ViralBShah authored
Showing with 27 additions and 6 deletions.
  1. +10 −6 base/linalg/dense.jl
  2. +17 −0 test/linalg.jl
View
16 base/linalg/dense.jl
@@ -171,7 +171,11 @@ function trace{T}(A::Matrix{T})
return t
end
-kron(a::Vector, b::Vector) = [ a[i]*b[j] for i=1:length(a), j=1:length(b) ]
+kron(a::Vector, b::Vector)=vec(kron(reshape(a,length(a),1),reshape(b,length(b),1)))
+
+kron(a::Matrix, b::Vector)=kron(a,reshape(b,length(b),1))
+
+kron(a::Vector, b::Matrix)=kron(reshape(a,length(a),1),b)
function kron{T,S}(a::Matrix{T}, b::Matrix{S})
R = Array(promote_type(T,S), size(a,1)*size(b,1), size(a,2)*size(b,2))
@@ -191,11 +195,11 @@ function kron{T,S}(a::Matrix{T}, b::Matrix{S})
R
end
-kron(a::Number, b::Number) = a * b
-kron(a::Vector, b::Number) = a * b
-kron(a::Number, b::Vector) = a * b
-kron(a::Matrix, b::Number) = a * b
-kron(a::Number, b::Matrix) = a * b
+kron(a::Number, b::Number) = a * b
+kron(a::Vector, b::Number) = a * b
+kron(a::Number, b::Vector) = a * b
+kron(a::Matrix, b::Number) = a * b
+kron(a::Number, b::Matrix) = a * b
randsym(n) = symmetrize!(randn(n,n))
View
17 test/linalg.jl
@@ -398,3 +398,20 @@ let
N = 3
@test_approx_eq log(det(eye(N))) logdet(eye(N))
end
+
+# issue 2637
+let
+ a = [1, 2, 3]
+ b = [4, 5, 6]
+ @test kron(eye(2),eye(2)) == eye(4)
+ @test kron(a,b) == [4,5,6,8,10,12,12,15,18]
+ @test kron(a',b') == [4 5 6 8 10 12 12 15 18]
+ @test kron(a,b') == [4 5 6; 8 10 12; 12 15 18]
+ @test kron(a',b) == [4 8 12; 5 10 15; 6 12 18]
+ @test kron(a,eye(2)) == [1 0; 0 1; 2 0; 0 2; 3 0; 0 3]
+ @test kron(eye(2),a) == [ 1 0; 2 0; 3 0; 0 1; 0 2; 0 3]
+ @test kron(eye(2),2) == 2*eye(2)
+ @test kron(3,eye(3)) == 3*eye(3)
+ @test kron(a,2) == [2, 4, 6]
+ @test kron(b',2) == [8 10 12]
+end
Please sign in to comment.
Something went wrong with that request. Please try again.