From 5bbe9d40046ed49af3c64567abc901f4c05a1dc4 Mon Sep 17 00:00:00 2001 From: Samuel GOUGEON Date: Sun, 17 Dec 2017 17:40:02 +0100 Subject: [PATCH] * Bug 15347 fixed: toeplitz failed with rationals http://bugzilla.scilab.org/15347 + other code improvements + unit tests extended a lot (~exhaustive) Change-Id: I61cba5d6e3dc3bb6194406b3e0f909e9f23fabf0 --- scilab/CHANGES.md | 1 + .../elementary_functions/macros/toeplitz.sci | 49 ++++++++------- .../tests/unit_tests/toeplitz.dia.ref | 15 ----- .../tests/unit_tests/toeplitz.tst | 66 +++++++++++++++++++- 4 files changed, 92 insertions(+), 39 deletions(-) delete mode 100644 scilab/modules/elementary_functions/tests/unit_tests/toeplitz.dia.ref diff --git a/scilab/CHANGES.md b/scilab/CHANGES.md index c92abc7..44835a8 100644 --- a/scilab/CHANGES.md +++ b/scilab/CHANGES.md @@ -507,6 +507,7 @@ Known issues * [#15339](http://bugzilla.scilab.org/show_bug.cgi?id=15339): `uigetfont()` returned garbage on user cancellation. * [#15340](http://bugzilla.scilab.org/show_bug.cgi?id=15340): Scilab did not build against lucene 6.1.0. * [#15342](http://bugzilla.scilab.org/show_bug.cgi?id=15342): `replot()` and `Reframe to contents` failed on graphics with xstring objects. +* [#15347](http://bugzilla.scilab.org/show_bug.cgi?id=15347): `toeplitz` failed with rationals. * [#15354](http://bugzilla.scilab.org/show_bug.cgi?id=15354): `invr(%s^2)` returned 0 instead of `1/%s^2`. For any scalar number, polynomial or rational `a`, `coffg(a)` returned `0` instead of `1`. `coffg([])` yielded an error. `invr` and `coffg` had no unitary tests. The `coffg` help page was inaccurate and unclear. * [#15375](http://bugzilla.scilab.org/show_bug.cgi?id=15375): A .zcos file opened as a palette was greyed out. diff --git a/scilab/modules/elementary_functions/macros/toeplitz.sci b/scilab/modules/elementary_functions/macros/toeplitz.sci index 3baefbb..cf2cf98 100644 --- a/scilab/modules/elementary_functions/macros/toeplitz.sci +++ b/scilab/modules/elementary_functions/macros/toeplitz.sci @@ -2,6 +2,7 @@ // Copyright (C) INRIA // Copyright (C) DIGITEO - 2011 - Allan CORNET // Copyright (C) Scilab Enterprises - 2011 - Calixte DENIZET +// Copyright (C) 2017 - Samuel GOUGEON // // Copyright (C) 2012 - 2016 - Scilab Enterprises // @@ -13,36 +14,38 @@ // along with this program. function a = toeplitz(c, r) - //a=toeplitz(c,r) returns the Toepliz matrix whose first row - //is r and first column is c .( r(1) = c(1) is assumed). - // - //r and c can be constant, polynomial or character string matrices. - //! + // a = toeplitz(c,r) returns the Toepliz matrix whose first row + // is r and first column is c .( r(1) = c(1) is assumed). - [lhs, rhs] = argn(0); + // Checking input arguments + // ------------------------ + rhs = argn(2); if rhs < 1 then - error(msprintf(gettext("%s: Wrong number of input argument(s): %d expected.\n"), "toeplitz", 1)); + msg = _("%s: Wrong number of input argument(s): %d to %d expected.\n"); + error(msprintf(msg, "toeplitz", 1, 2)); end - if rhs == 1 then - r = c(:); + r = c; end - - nr = size(r, "*"); - r = matrix(r, 1, nr); - nc = size(c, "*"); - c = matrix(c, 1, nc); - - if nr * nc == 0 then - a = []; - return; + + if isempty(c) | isempty(r) then + a = [] + return end - - if r(1) <> c(1) then - error(msprintf(gettext("%s: Wrong values for input arguments #%d and #%d: c(1) must be equal to r(1).\n"), "toeplitz", 1, 2)); + // Checking types compatibility & upper left common corner: + r = matrix(r, 1, -1); + c = matrix(c, 1, -1); + tmp = [c(1,1) r(1,1)]; + if tmp(1,1) <> tmp(1,2) then + msg = _("%s: Wrong values for input arguments #%d and #%d: c(1) must be equal to r(1).\n"); + error(msprintf(msg, "toeplitz", 1, 2)); end + // Processing + // ---------- + nr = size(r, "*"); + nc = size(c, "*"); index = ones(1, nr) .*. ((nc - 1):-1:0)' + (1:nr) .*. ones(nc, 1); - b = [c($:-1:2) r]; - a = matrix(b(index), nc, nr); + b = [c(1, $:-1:2) r]; + a = matrix(b(1,index(:)'), nc, nr); endfunction diff --git a/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.dia.ref b/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.dia.ref deleted file mode 100644 index 8001f56..0000000 --- a/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.dia.ref +++ /dev/null @@ -1,15 +0,0 @@ -// ============================================================================= -// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab -// Copyright (C) 2012 - Scilab Enterprises -// -// This file is distributed under the same license as the Scilab package. -// ============================================================================= -// <-- CLI SHELL MODE --> -r = 1:5; -c = [1 6:9]; -toe = [1 6 7 8 9; -2 1 6 7 8; -3 2 1 6 7; -4 3 2 1 6; -5 4 3 2 1]; -assert_checkequal(toeplitz(r, c), toe); diff --git a/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.tst b/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.tst index 0ab3a09..cb6557a 100644 --- a/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.tst +++ b/scilab/modules/elementary_functions/tests/unit_tests/toeplitz.tst @@ -1,10 +1,33 @@ // ============================================================================= // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab // Copyright (C) 2012 - Scilab Enterprises +// Copyright (C) 2017 - Samuel GOUGEON // // This file is distributed under the same license as the Scilab package. // ============================================================================= +// // <-- CLI SHELL MODE --> +// <-- NO CHECK REF --> + +// With [] +assert_checkequal(toeplitz([]), []); +assert_checkequal(toeplitz([],[]), []); + +// With booleans +expected = [1 0 0 0 1 1 + 0 1 0 0 0 1 + 1 0 1 0 0 0 + ]==1; +assert_checkequal(toeplitz([%t %f %t], [%t %f %f %f %t %t]), expected); + +// With encoded integers +expected = int8([-1 0 1 2 ; 0 -1 0 1 ; 1 0 -1 0 ; 2 1 0 -1]); +assert_checkequal(toeplitz(int8(-1:2)), expected); + +// With decimal numbers (dense) +expected = [-1 0 1 2 ; 0 -1 0 1 ; 1 0 -1 0 ; 2 1 0 -1]; +assert_checkequal(toeplitz(-1:2), expected); + r = 1:5; c = [1 6:9]; toe = [1 6 7 8 9; @@ -12,4 +35,45 @@ toe = [1 6 7 8 9; 3 2 1 6 7; 4 3 2 1 6; 5 4 3 2 1]; -assert_checkequal(toeplitz(r, c), toe); \ No newline at end of file +assert_checkequal(toeplitz(r, c), toe); + +// With sparse +expected = [ + 0 -1 -2 0 0 0 + 1 0 -1 -2 0 0 + 0 1 0 -1 -2 0 + 0 0 1 0 -1 -2 + ]; +T = toeplitz([0 1 0 0 ], sparse([0 -1 -2 0 0 0])); +assert_checktrue(type(T)==5); +assert_checkequal(full(T), expected); + +T = toeplitz(sparse([0 1 0 0 ]), [0 -1 -2 0 0 0]); +assert_checktrue(type(T)==5); +assert_checkequal(full(T), expected); + +T = toeplitz(sparse([0 1 0 0 ]), sparse([0 -1 -2 0 0 0])); +assert_checktrue(type(T)==5); +assert_checkequal(full(T), expected); + +// With polynomials +expected = [1 2 3 4 5 + %s 1 2 3 4 + %s^2 %s 1 2 3 + %s^3 %s^2 %s 1 2 + ]; +assert_checkequal(toeplitz([1 %s %s^2 %s^3], [1:5]), expected); + +// With rationals +expected = 1 ./ %s.^toeplitz([0 1],0:3); +assert_checkequal(toeplitz([1 1/%s], [1 1/%s 1/%s^2 1/%s^3]), expected); + +// With texts +expected = [ + "-" "a" "b" "c" "d" "e" + "A" "-" "a" "b" "c" "d" + "B" "A" "-" "a" "b" "c" + "C" "B" "A" "-" "a" "b" + ]; +T = toeplitz(["-" "A" "B" "C"], ["-" "a" "b" "c" "d" "e"]); +assert_checkequal(T, expected); -- 1.7.9.5