From 5b46b07bf363da57d53394e58e4bccf69ef78865 Mon Sep 17 00:00:00 2001 From: marcandre Date: Fri, 3 Oct 2014 03:44:20 +0000 Subject: * lib/matrix.rb: Add hstack & vstack methods. Based on a patch by creasywuqiong. [Fix GH-344] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47769 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/matrix/test_matrix.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'test/matrix') diff --git a/test/matrix/test_matrix.rb b/test/matrix/test_matrix.rb index 3e1e0db91e..eb62a326b6 100644 --- a/test/matrix/test_matrix.rb +++ b/test/matrix/test_matrix.rb @@ -1,6 +1,9 @@ require 'test/unit' require 'matrix' +class SubMatrix < Matrix +end + class TestMatrix < Test::Unit::TestCase def setup @m1 = Matrix[[1,2,3], [4,5,6]] @@ -9,6 +12,8 @@ class TestMatrix < Test::Unit::TestCase @m4 = Matrix[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] @n1 = Matrix[[2,3,4], [5,6,7]] @c1 = Matrix[[Complex(1,2), Complex(0,1), 0], [1, 2, 3]] + @e1 = Matrix.empty(2,0) + @e2 = Matrix.empty(0,3) end def test_matrix @@ -499,4 +504,34 @@ class TestMatrix < Test::Unit::TestCase end assert_equal(1, s1 ** o) end + + def test_hstack + assert_equal Matrix[[1,2,3,2,3,4,1,2,3], [4,5,6,5,6,7,4,5,6]], + @m1.hstack(@n1, @m1) + # Error checking: + assert_raise(TypeError) { @m1.hstack(42) } + assert_raise(TypeError) { Matrix.hstack(42, @m1) } + assert_raise(Matrix::ErrDimensionMismatch) { @m1.hstack(Matrix.identity(3)) } + assert_raise(Matrix::ErrDimensionMismatch) { @e1.hstack(@e2) } + # Corner cases: + assert_equal @m1, @m1.hstack + assert_equal @e1, @e1.hstack(@e1) + assert_equal Matrix.empty(0,6), @e2.hstack(@e2) + assert_equal SubMatrix, SubMatrix.hstack(@e1).class + end + + def test_vstack + assert_equal Matrix[[1,2,3], [4,5,6], [2,3,4], [5,6,7], [1,2,3], [4,5,6]], + @m1.vstack(@n1, @m1) + # Error checking: + assert_raise(TypeError) { @m1.vstack(42) } + assert_raise(TypeError) { Matrix.vstack(42, @m1) } + assert_raise(Matrix::ErrDimensionMismatch) { @m1.vstack(Matrix.identity(2)) } + assert_raise(Matrix::ErrDimensionMismatch) { @e1.vstack(@e2) } + # Corner cases: + assert_equal @m1, @m1.vstack + assert_equal Matrix.empty(4,0), @e1.vstack(@e1) + assert_equal @e2, @e2.vstack(@e2) + assert_equal SubMatrix, SubMatrix.vstack(@e1).class + end end -- cgit v1.2.3