From 87d19c362de73d6156df9258143df1ed7c346e51 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 9 Jan 2011 02:09:40 +0900 Subject: [PATCH] fixed CvMat#solve, and added some tests --- ext/cvmat.cpp | 2 +- test/test_cvmat.rb | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/ext/cvmat.cpp b/ext/cvmat.cpp index eb1f398..069c1d6 100644 --- a/ext/cvmat.cpp +++ b/ext/cvmat.cpp @@ -2292,7 +2292,7 @@ rb_solve(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "11", &mat, &symbol); if (!rb_obj_is_kind_of(mat, rb_klass)) rb_raise(rb_eTypeError, "argument 1 (right-hand part of the linear system) should be %s.)", rb_class2name(rb_klass)); - VALUE dest = new_object(cvGetSize(CVARR(self)), cvGetElemType(CVARR(self))); + VALUE dest = new_object(CVMAT(self)->rows, CVMAT(mat)->cols, cvGetElemType(CVARR(self))); cvSolve(CVARR(self), CVARR(mat), CVARR(dest), CVMETHOD("INVERSION_METHOD", symbol, CV_LU)); return dest; } diff --git a/test/test_cvmat.rb b/test/test_cvmat.rb index 6a9345a..0b901ec 100755 --- a/test/test_cvmat.rb +++ b/test/test_cvmat.rb @@ -1661,6 +1661,76 @@ class TestCvMat < OpenCVTestCase } assert_in_delta(CvScalar.new(30, 60, 90, 120), m0.trace, 0.001) end + + def test_transpose + m0 = create_cvmat(5, 5, :cv32f, 4) { |j, i, c| + CvScalar.new(c * 0.5, c * 1.0, c * 1.5, c * 2.0) + } + m1 = m0.clone + m2 = m1.transpose + m1.transpose! + + [m1, m2].each { |m| + assert_equal(m0.width, m.width) + assert_equal(m0.height, m.height) + assert_each_cvscalar(m, 0.001) { |j, i, c| + m0[i, j] + } + } + end + + def test_det + elems = [2.5, 4.5, 2.0, + 3.0, 2.5, -0.5, + 1.0, 0.5, 1.5] + m0 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c| + CvScalar.new(elems[c]) + } + assert_in_delta(-14.5, m0.det, 0.001) + end + + def test_invert + elems = [1, 2, 3, + 2, 6, 9, + 1, 4, 7] + m0 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c| + CvScalar.new(elems[c]) + } + m1 = m0.invert + m2 = m0.invert(:lu) + m3 = m0.invert(:svd) + m4 = m0.invert(:svd_sym) + m5 = m0.invert(:svd_symmetric) + + expected = [3, -1, 0, -2.5, 2, -1.5, 1, -1, 1] + [m1, m2, m3, m4, m5].each { |m| + assert_equal(m0.width, m.width) + assert_equal(m0.height, m.height) + assert_each_cvscalar(m, 0.001) { |j, i, c| + CvScalar.new(expected[c]) + } + } + end + + def test_solve + elems1 = [3, 4, 5, + 8, 9, 6, + 3, 5, 9] + elems2 = [3, + 4, + 5] + m0 = create_cvmat(3, 3, :cv32f, 1) { |j, i, c| + CvScalar.new(elems1[c]) + } + m1 = create_cvmat(3, 1, :cv32f, 1) { |j, i, c| + CvScalar.new(elems2[c]) + } + m2 = m0.solve(m1) + expected = [2, -2, 1] + assert_each_cvscalar(m2, 0.001) { |j, i, c| + CvScalar.new(expected[c]) + } + end end