* [#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).
// 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)
[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
--- /dev/null
+// 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