From: Samuel GOUGEON Date: Mon, 10 May 2021 17:59:21 +0000 (+0200) Subject: * Bug 16664 fixed: diag(spzeros(2,2)) warned for Operation - [] X-Git-Tag: 6.1.1~41 X-Git-Url: http://gitweb.scilab.org/?p=scilab.git;a=commitdiff_plain;h=f8ff9927f796d2c03a03becb11a240a2f98242d2 * Bug 16664 fixed: diag(spzeros(2,2)) warned for Operation - [] http://bugzilla.scilab.org/16664 Test: diag(spzeros(2,2)) // Must return without warning --> diag(spzeros(2,2)) ans = ( 2, 1)zero sparse matrix Change-Id: I3a55d6d2108b19e5f1cbadfcaaf46856760b20f8 --- diff --git a/scilab/CHANGES.md b/scilab/CHANGES.md index b5c3edd..636349a 100644 --- a/scilab/CHANGES.md +++ b/scilab/CHANGES.md @@ -466,6 +466,7 @@ Bug Fixes * [#16644](https://bugzilla.scilab.org/16644): `input("message:")` yielded a wrong error message about `mprintf` in case of non-interpretable input. * [#16654](https://bugzilla.scilab.org/16654): `interp` was leaking memory. * [#16661](https://bugzilla.scilab.org/16661): `x=spzeros(1e10,1e10)` yielded an incorrect error message. +* [#16664](https://bugzilla.scilab.org/16664): `diag(spzeros(2,2))` yielded an `Operation -[]` warning. * [#16665](https://bugzilla.scilab.org/16665): `help echo` could not redirect to `help mode` when preferred, for new users coming from Octave. * [#16677](https://bugzilla.scilab.org/16677): In offline mode, `atomsInstall` was flashing many times the console. * [#16679](https://bugzilla.scilab.org/16679): `get_function_path("acosh")` yielded an error (regression from Scilab 6.0.0). diff --git a/scilab/modules/elementary_functions/macros/%sp_diag.sci b/scilab/modules/elementary_functions/macros/%sp_diag.sci index 32a0878..d388c09 100644 --- a/scilab/modules/elementary_functions/macros/%sp_diag.sci +++ b/scilab/modules/elementary_functions/macros/%sp_diag.sci @@ -10,7 +10,7 @@ // For more information, see the COPYING file which you should have received // along with this program. -function d=%sp_diag(a,k) +function d = %sp_diag(a,k) // %sp_diag - implement diag function for sparse matrix, rational matrix ,.. [lhs,rhs]=argn(0) @@ -19,7 +19,11 @@ function d=%sp_diag(a,k) [ij,v,sz]=spget(a) m=sz(1);n=sz(2) if m>1&n>1 then - l=find(ij(:,1)==(ij(:,2)-k)) + if ij<>[] + l = find(ij(:,1)==(ij(:,2)-k)) + else + l = [] + end if k<=0 then mn=min(m+k,n) i0=-k diff --git a/scilab/modules/elementary_functions/tests/unit_tests/diag.tst b/scilab/modules/elementary_functions/tests/unit_tests/diag.tst new file mode 100644 index 0000000..9e302cc --- /dev/null +++ b/scilab/modules/elementary_functions/tests/unit_tests/diag.tst @@ -0,0 +1,43 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2021 - ESI Group - Clement DAVID +// +// This file is hereby licensed under the terms of the GNU GPL v2.0, +// pursuant to article 5.3.4 of the CeCILL v.2.1. +// This file was originally licensed under the terms of the CeCILL v2.1, +// and continues to be available under such terms. +// For more information, see the COPYING file which you should have received +// along with this program. +// ---------------------------------------------------------------------------- + +// <-- CLI SHELL MODE --> +// <-- NO CHECK REF --> +// <-- IMPOSED ENGLISH --> +// +// -------------------------- +// Unit tests for diag() +// -------------------------- + +// Double +assert_checkequal(diag(1), 1); +assert_checkequal(diag([1 ; 2]), [1 0 ; 0 2]); +assert_checkequal(diag([1 ; 2 ; 3]), [1 0 0 ; 0 2 0 ; 0 0 3]); +assert_checkequal(diag(diag([1 ; 2])), [1 ; 2]); +assert_checkequal(diag(diag([1 ; 2 ; 3])), [1 ; 2 ; 3]); + +assert_checkequal(diag(1, 1), [0 1 ; 0 0]); +assert_checkequal(diag([1 ; 2], 1), [0 1 0 ; 0 0 2 ; 0 0 0]); +assert_checkequal(diag([1 ; 2 ; 3], 1), [0 1 0 0 ; 0 0 2 0 ; 0 0 0 3 ; 0 0 0 0]); + +// Sparse +assert_checkequal(diag(sparse(1)), sparse(1)); +assert_checkequal(diag(sparse([1 ; 2])), sparse([1 0 ; 0 2])); +assert_checkequal(diag(sparse([1 ; 2 ; 3])), sparse([1 0 0 ; 0 2 0 ; 0 0 3])); +assert_checkequal(diag(sparse(diag([1 ; 2]))), sparse([1 ; 2])); +assert_checkequal(diag(sparse(diag([1 ; 2 ; 3]))), sparse([1 ; 2 ; 3])); + +assert_checkequal(diag(sparse(1), 1), sparse([0 1 ; 0 0])); +assert_checkequal(diag(sparse([1 ; 2]), 1), sparse([0 1 0 ; 0 0 2 ; 0 0 0])); +assert_checkequal(diag(sparse([1 ; 2 ; 3]), 1), sparse([0 1 0 0 ; 0 0 2 0 ; 0 0 0 3 ; 0 0 0 0])); + +// http://bugzilla.scilab.org/16664 +diag(spzeros(2,2)) \ No newline at end of file