* Bug 16458 fixed: enable mean() for sparse 81/21481/4
Samuel GOUGEON [Tue, 2 Jun 2020 20:44:37 +0000 (22:44 +0200)]
  http://bugzilla.scilab.org/16458
  Overhauled page (PDF): http://bugzilla.scilab.org/attachment.cgi?id=5152

Change-Id: Ife623e88c565237890ca6c0d97dc21d3935a5154

scilab/CHANGES.md
scilab/modules/statistics/help/en_US/2_central_tendency/mean.xml
scilab/modules/statistics/help/ja_JP/2_central_tendency/mean.xml [deleted file]
scilab/modules/statistics/help/ru_RU/2_central_tendency/mean.xml [new file with mode: 0644]
scilab/modules/statistics/macros/%sp_mean.sci [new file with mode: 0644]
scilab/modules/statistics/tests/unit_tests/mean.tst

index 701eba7..f0baeeb 100644 (file)
@@ -310,6 +310,7 @@ Bug Fixes
 * [#16450](https://bugzilla.scilab.org/16450): Concatenating encoded integers with decimal or complex numbers was not possible.
 * [#16452](https://bugzilla.scilab.org/16452): `setdiff(sparse([1 3 0 2]), sparse([3 7]))` missed returning 0, and wrongly returned 3.
 * [#16454](https://bugzilla.scilab.org/16454): `gsort` yielded an error when sorting any sparse vector including some NaN.
+* [#16458](https://bugzilla.scilab.org/16458): `mean()` did not handle sparse numerical matrices.
 * [#16465](https://bugzilla.scilab.org/16465): Scinotes OpenRecent menu was not updated when it should.
 * [#16473](https://bugzilla.scilab.org/16473): Deleting rows in a sparse squared the matrix with padding zeros (Scilab 6 regression).
 
index fb35224..2849d38 100644 (file)
@@ -2,8 +2,8 @@
 <!--
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2000 - INRIA - Carlos Klimann
- *
  * Copyright (C) 2012 - 2016 - Scilab Enterprises
+ * Copyright (C) 2020 - Samuel GOUGEON
  *
  * 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.
  * along with this program.
  *
  -->
-<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:lang="en" xml:id="mean">
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
+          xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML"
+          xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
+          xml:lang="en" xml:id="mean">
     <refnamediv>
         <refname>mean</refname>
-        <refpurpose>mean (row mean, column mean) of vector/matrix
-            entries
+        <refpurpose>mean of all values, or means along a given dimension
         </refpurpose>
     </refnamediv>
     <refsynopsisdiv>
         <title>Syntax</title>
-        <synopsis>y=mean(x)
-            y=mean(x,'r')
-            y=mean(x,'c')
-            y=mean(x,'m')
+        <synopsis>
+            y = mean(x)
+            y = mean(x, orientation)
         </synopsis>
     </refsynopsisdiv>
     <refsection>
             <varlistentry>
                 <term>x</term>
                 <listitem>
-                    <para>real vector or matrix</para>
+                    Vector, matrix, or hypermatrix of real or complex numbers.
+                    Sparse matrices accepted.
+                    <para/>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>orientation</term>
+                <listitem>
+                    direction = index of the dimension along which the mean is computed.
+                    "r" is equivalent to 1. "c" is equivalent to 2. "m" is equivalent
+                    to <literal>find(size(x)>1,1)</literal>.
+                    <para/>
                 </listitem>
             </varlistentry>
             <varlistentry>
                 <term>y</term>
                 <listitem>
-                    <para>scalar or vector</para>
+                    dense scalar if <varname>orientation</varname> is not used or if
+                    <varname>x</varname> is a scalar. Otherwise, array such that
+                    <literal>size(y,orientation)</literal> is 1 (sparse-encoded if
+                    <varname>x</varname> is so).
                 </listitem>
             </varlistentry>
         </variablelist>
     <refsection>
         <title>Description</title>
         <para>
-            For a vector or a matrix <literal>x</literal>, <literal>y=mean(x)</literal> returns
-            in the scalar <literal>y</literal> the mean of all the entries of
-            <literal>x</literal>.
+            <emphasis role="bold">y = mean(x)</emphasis> returns the mean of all
+            entries. If at least one entry is NaN, NaN is returned.
+            This scalar result is always dense-encoded.
         </para>
         <para>
-            <literal>y=mean(x,'r')</literal> (or, equivalently, <literal>y=mean(x,1)</literal>)
-            is the rowwise mean. It returns a row vector: <literal>y(j)=
-                mean(x(:,j))
-            </literal>
+            <emphasis role="bold">y = mean(x,1)</emphasis> or <literal>y=mean(x,"r")</literal>
+            computes the means accross rows. <varname>y</varname> is a row if
+            <varname>x</varname> is a matrix.
         </para>
         <para>
-            <literal>y=mean(x,'c')</literal> (or, equivalently, <literal>y=mean(x,2)</literal>)
-            is the columnwise mean. It returns a column vector: <literal>y(i)=
-                mean(x(i,:))
-            </literal>
+            <emphasis role="bold">y = mean(x,2)</emphasis> or <literal>y=mean(x,"c")</literal>
+            computes the means accross columns. <varname>y</varname> is a column if
+            <varname>x</varname> is a matrix.
         </para>
         <para>
-            <literal>y=mean(x,'m')</literal> is the mean along the first non singleton
-            dimension of x (for compatibility with Matlab).
+            <emphasis role="bold">y = mean(x, n)</emphasis> with <literal>3 ≤ n ≤ ndims(x)</literal>
+            computes the means along the n<superscript>th</superscript> dimension of <varname>x</varname>.
+        </para>
+        <para>
+            <emphasis role="bold">y = mean(x,'m')</emphasis> is the mean along the
+            first non singleton dimension of x (for compatibility with Matlab).
+        </para>
+        <para>
+            <warning>
+             <literal>mean([])</literal> and <literal>mean(sparse([]))</literal> return NaN.
+             For any <varname>orientation</varname> not "m",
+             <literal>mean([], orientation)</literal> returns <literal>[]</literal>, and
+             <literal>mean(sparse([]), orientation)</literal> returns <literal>sparse([])</literal>.
+            </warning>
         </para>
         <note>
             <literal>mean()</literal> can be overloaded.
     </refsection>
     <refsection>
         <title>Examples</title>
+        <para>
+            With a matrix:
+        </para>
+        <programlisting role="example"><![CDATA[
+A = [0,1,1,0,1;1,0,0,1,1;0,0,1,0,0;0,0,1,0,0]
+mean(A)
+mean(A, 'r')
+mean(A, 'c')
+     ]]></programlisting>
+        <screen><![CDATA[
+--> A = [0,1,1,0,1;1,0,0,1,1;0,0,1,0,0;0,0,1,0,0]
+ A  =
+   0.   1.   1.   0.   1.
+   1.   0.   0.   1.   1.
+   0.   0.   1.   0.   0.
+   0.   0.   1.   0.   0.
+
+--> mean(A)
+ ans  =
+   0.4
+
+--> mean(A, 'r')
+ ans  =
+   0.25   0.25   0.75   0.25   0.5
+
+--> mean(A, 'c')
+ ans  =
+   0.6
+   0.6
+   0.2
+   0.2
+]]></screen>
+        <para>
+            With an hypermatrix:
+        </para>
         <programlisting role="example"><![CDATA[
-A=[1,2,10;7,7.1,7.01];
+A = [1,0,0,1,0,0,1,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,0,0,1,0,1,1,0];
+A = matrix(A, [4,5,2])
 mean(A)
-mean(A,'r')
-mean(A,'c')
-A=matrix(1:12,[1,1,2,3,2]);
+mean(A, 'r')
+mean(A, 'c')
+mean(A, 3)
+
+A = matrix(1:12, [1,1,2,3,2]);
 // in this case mean(A,'m') is equivalent to mean(A,3), the first non singleton dimension of A
-y=mean(A,'m')
- ]]></programlisting>
+mean(A, 'm')
+     ]]></programlisting>
+        <screen><![CDATA[
+--> A = matrix(A, [4,5,2])
+ A  =
+(:,:,1)
+   1.   0.   1.   0.   1.
+   0.   0.   1.   1.   0.
+   0.   1.   1.   0.   1.
+   1.   1.   1.   1.   1.
+
+(:,:,2)
+   1.   1.   1.   1.   0.
+   1.   0.   1.   0.   1.
+   1.   1.   1.   0.   1.
+   0.   0.   0.   1.   0.
+
+--> mean(A)
+ ans  =
+   0.625
+
+--> mean(A, 'r')
+ ans  =
+(:,:,1)
+   0.5   0.5   1.   0.5   0.75
+
+(:,:,2)
+   0.75   0.5   0.75   0.5   0.5
+
+--> mean(A, 'c')
+ ans  =
+(:,:,1)
+   0.6
+   0.4
+   0.6
+   1.
+
+(:,:,2)
+   0.8
+   0.6
+   0.8
+   0.2
+
+--> mean(A, 3)
+ ans  =
+   1.    0.5   1.    0.5   0.5
+   0.5   0.    1.    0.5   0.5
+   0.5   1.    1.    0.    1.
+   0.5   0.5   0.5   1.    0.5
+]]></screen>
     </refsection>
     <refsection role="see also">
         <title>See also</title>
@@ -107,6 +218,12 @@ y=mean(A,'m')
                     mean() is now overloadable.
                 </revdescription>
             </revision>
+            <revision>
+                <revnumber>6.1.1</revnumber>
+                <revdescription>
+                    Extension to sparse matrices.
+                </revdescription>
+            </revision>
         </revhistory>
     </refsection>
 </refentry>
diff --git a/scilab/modules/statistics/help/ja_JP/2_central_tendency/mean.xml b/scilab/modules/statistics/help/ja_JP/2_central_tendency/mean.xml
deleted file mode 100644 (file)
index a891116..0000000
+++ /dev/null
@@ -1,110 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
- * Copyright (C) 2000 - INRIA - Carlos Klimann
- *
- * Copyright (C) 2012 - 2016 - Scilab Enterprises
- *
- * 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.
- *
- -->
-<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:lang="ja" xml:id="mean">
-    <refnamediv>
-        <refname>mean</refname>
-        <refpurpose>ベクトル/行列の要素の平均 (行平均, 列平均)
-        </refpurpose>
-    </refnamediv>
-    <refsynopsisdiv>
-        <title>呼び出し手順</title>
-        <synopsis>y=mean(x)
-            y=mean(x,'r')
-            y=mean(x,'c')
-            y=mean(x,'m')
-        </synopsis>
-    </refsynopsisdiv>
-    <refsection>
-        <title>パラメータ</title>
-        <variablelist>
-            <varlistentry>
-                <term>x</term>
-                <listitem>
-                    <para>実数ベクトルまたは行列</para>
-                </listitem>
-            </varlistentry>
-            <varlistentry>
-                <term>y</term>
-                <listitem>
-                    <para>スカラーまたはベクトル</para>
-                </listitem>
-            </varlistentry>
-        </variablelist>
-    </refsection>
-    <refsection>
-        <title>説明</title>
-        <para>
-            <literal>x</literal>がベクトルまたは行列の場合, <literal>y=mean(x)</literal> は,
-            <literal>x</literal>の全ての要素の平均をスカラー<literal>y</literal>に返します.
-        </para>
-        <para>
-            <literal>y=mean(x,'r')</literal> (または等価的に, <literal>y=mean(x,1)</literal>)
-            は行単位の平均です. 行ベクトルを返します: <literal>y(j)=
-                mean(x(:,j))
-            </literal>
-        </para>
-        <para>
-            <literal>y=mean(x,'c')</literal> (または等価的に,<literal>y=mean(x,2)</literal>)
-            は列単位の平均ですn. 列ベクトルを返します: <literal>y(i)=
-                mean(x(i,:))
-            </literal>
-        </para>
-        <para>
-            <literal>y=mean(x,'m')</literal> は,
-            xの最初の1でない次元に沿った平均です(Matlabとの互換用).
-        </para>
-        <note>
-            <literal>mean()</literal> can be overloaded.
-        </note>
-    </refsection>
-    <refsection>
-        <title>例</title>
-        <programlisting role="example"><![CDATA[
-A=[1,2,10;7,7.1,7.01];
-mean(A)
-mean(A,'r')
-mean(A,'c')
-A=matrix(1:12,[1,1,2,3,2]);
-// この場合, mean(A,'m') は mean(A,3)と等価です (Aの最初の1でない次元は3)
-y=mean(A,'m')
- ]]></programlisting>
-    </refsection>
-    <refsection role="see also">
-        <title>参照</title>
-        <simplelist type="inline">
-            <member>
-                <link linkend="sum">sum</link>
-            </member>
-            <member>
-                <link linkend="median">median</link>
-            </member>
-            <member>
-                <link linkend="stdev">stdev</link>
-            </member>
-        </simplelist>
-    </refsection>
-    <refsection role="history">
-        <title>履歴</title>
-        <revhistory>
-            <revision>
-                <revnumber>6.0.1</revnumber>
-                <revdescription>
-                    mean() is now overloadable.
-                </revdescription>
-            </revision>
-        </revhistory>
-    </refsection>
-</refentry>
diff --git a/scilab/modules/statistics/help/ru_RU/2_central_tendency/mean.xml b/scilab/modules/statistics/help/ru_RU/2_central_tendency/mean.xml
new file mode 100644 (file)
index 0000000..5c45201
--- /dev/null
@@ -0,0 +1,236 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2000 - INRIA - Carlos Klimann
+ * Copyright (C) 2012 - 2016 - Scilab Enterprises
+ * Copyright (C) 2020 - Samuel GOUGEON
+ *
+ * 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.
+ *
+ -->
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
+          xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML"
+          xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
+          xml:lang="ru" xml:id="mean">
+    <refnamediv>
+        <refname>mean</refname>
+        <refpurpose>
+            матожидание всех значений или матожидания по заданной размерности
+        </refpurpose>
+    </refnamediv>
+    <refsynopsisdiv>
+        <title>Синтаксис</title>
+        <synopsis>
+            y = mean(x)
+            y = mean(x, orientation)
+        </synopsis>
+    </refsynopsisdiv>
+    <refsection>
+        <title>Аргументы</title>
+        <variablelist>
+            <varlistentry>
+                <term>x</term>
+                <listitem>
+                    вектор, матрица или гиперматрица вещественных или комплексных чисел.
+                    Допускаются разрежённые матрицы.
+                    <para/>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>orientation</term>
+                <listitem>
+                    направление соответствует индексу размерности по которой
+                    вычисляется матожидание. <literal>"r"</literal> эквивалентен 1.
+                    <literal>"c"</literal> эквивалентно 2. <literal>"m"</literal>
+                    эквивалентно <literal>find(size(x)>1,1)</literal>.
+                    <para/>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>y</term>
+                <listitem>
+                    плотный скаляр, если <varname>orientation</varname> не используется
+                    либо если <varname>x</varname> скаляр. В противном случае
+                    массив, такой, что <literal>size(y,orientation)</literal>
+                    равен 1 (разрежённо-кодированный, если <varname>x</varname>
+                    является таким).
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
+    <refsection>
+        <title>Описание</title>
+        <para>
+            <emphasis role="bold">y = mean(x)</emphasis> возвращает матожидание
+            всех элементов. Если хотя бы один элемент равен NaN, то возвращается
+            NaN. Этот скалярный результат всегда плотно кодирован.
+        </para>
+        <para>
+            <emphasis role="bold">y = mean(x,1)</emphasis> или <literal>y=mean(x,"r")</literal>
+            вычисляет матожидания по строкам. <varname>y</varname> является строкой,
+            если <varname>x</varname> является матрицей.
+        </para>
+        <para>
+            <emphasis role="bold">y = mean(x,2)</emphasis> или <literal>y=mean(x,"c")</literal>
+            вычисляет матожидания по столбцам. <varname>y</varname> является столбцом,
+            если <varname>x</varname> является матрицей.
+        </para>
+        <para>
+            <emphasis role="bold">y = mean(x, n)</emphasis> при
+            <literal>3 ≤ n ≤ ndims(x)</literal> вычисляет матожидания по
+            n<superscript>й</superscript> размерности <varname>x</varname>.
+        </para>
+        <para>
+            <emphasis role="bold">y = mean(x,'m')</emphasis> является матожидание
+            по первой неединичной размерности <varname>x</varname> (для совместимости
+            с Matlab).
+        </para>
+        <para>
+            <warning>
+             <literal>mean([])</literal> и <literal>mean(sparse([]))</literal>
+             возвращают NaN. Для любого <varname>orientation</varname> не
+             <literal>"m"</literal>, <literal>mean([], orientation)</literal>
+             возвращает <literal>[]</literal>, а
+             <literal>mean(sparse([]), orientation)</literal> возвращает
+             <literal>sparse([])</literal>.
+            </warning>
+        </para>
+        <note>
+            <literal>mean()</literal> может быть перегружена.
+        </note>
+    </refsection>
+    <refsection>
+        <title>Примеры</title>
+        <para>
+            С матрицей:
+        </para>
+        <programlisting role="example"><![CDATA[
+A = [0,1,1,0,1;1,0,0,1,1;0,0,1,0,0;0,0,1,0,0]
+mean(A)
+mean(A, 'r')
+mean(A, 'c')
+     ]]></programlisting>
+        <screen><![CDATA[
+--> A = [0,1,1,0,1;1,0,0,1,1;0,0,1,0,0;0,0,1,0,0]
+ A  =
+   0.   1.   1.   0.   1.
+   1.   0.   0.   1.   1.
+   0.   0.   1.   0.   0.
+   0.   0.   1.   0.   0.
+
+--> mean(A)
+ ans  =
+   0.4
+
+--> mean(A, 'r')
+ ans  =
+   0.25   0.25   0.75   0.25   0.5
+
+--> mean(A, 'c')
+ ans  =
+   0.6
+   0.6
+   0.2
+   0.2
+]]></screen>
+        <para>
+            С гиперматрицей:
+        </para>
+        <programlisting role="example"><![CDATA[
+A = [1,0,0,1,0,0,1,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1,0,1,0,0,1,0,1,1,0];
+A = matrix(A, [4,5,2])
+mean(A)
+mean(A, 'r')
+mean(A, 'c')
+mean(A, 3)
+
+A = matrix(1:12, [1,1,2,3,2]);
+// в этом случае mean(A,'m') эквивалентно mean(A,3), первая неединичная размерность A
+mean(A, 'm')
+     ]]></programlisting>
+        <screen><![CDATA[
+--> A = matrix(A, [4,5,2])
+ A  =
+(:,:,1)
+   1.   0.   1.   0.   1.
+   0.   0.   1.   1.   0.
+   0.   1.   1.   0.   1.
+   1.   1.   1.   1.   1.
+
+(:,:,2)
+   1.   1.   1.   1.   0.
+   1.   0.   1.   0.   1.
+   1.   1.   1.   0.   1.
+   0.   0.   0.   1.   0.
+
+--> mean(A)
+ ans  =
+   0.625
+
+--> mean(A, 'r')
+ ans  =
+(:,:,1)
+   0.5   0.5   1.   0.5   0.75
+
+(:,:,2)
+   0.75   0.5   0.75   0.5   0.5
+
+--> mean(A, 'c')
+ ans  =
+(:,:,1)
+   0.6
+   0.4
+   0.6
+   1.
+
+(:,:,2)
+   0.8
+   0.6
+   0.8
+   0.2
+
+--> mean(A, 3)
+ ans  =
+   1.    0.5   1.    0.5   0.5
+   0.5   0.    1.    0.5   0.5
+   0.5   1.    1.    0.    1.
+   0.5   0.5   0.5   1.    0.5
+]]></screen>
+    </refsection>
+    <refsection role="see also">
+        <title>Смотрите также</title>
+        <simplelist type="inline">
+            <member>
+                <link linkend="sum">sum</link>
+            </member>
+            <member>
+                <link linkend="median">median</link>
+            </member>
+            <member>
+                <link linkend="stdev">stdev</link>
+            </member>
+        </simplelist>
+    </refsection>
+    <refsection role="history">
+        <title>История</title>
+        <revhistory>
+            <revision>
+                <revnumber>6.0.1</revnumber>
+                <revdescription>
+                    mean() теперь может быть перегружена.
+                </revdescription>
+            </revision>
+            <revision>
+                <revnumber>6.1.1</revnumber>
+                <revdescription>
+                    Расширение до разрежённых матриц.
+                </revdescription>
+            </revision>
+        </revhistory>
+    </refsection>
+</refentry>
diff --git a/scilab/modules/statistics/macros/%sp_mean.sci b/scilab/modules/statistics/macros/%sp_mean.sci
new file mode 100644 (file)
index 0000000..14180db
--- /dev/null
@@ -0,0 +1,64 @@
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+//
+// Copyright (C) 2020 - Samuel GOUGEON
+//
+// 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.
+
+function y = %sp_mean(x, orient)
+    
+    // CHECK ARGUMENTS
+    // ---------------
+    // Number of inputs already checked in mean() that calls %sp_mean()
+    // orient
+    if ~isdef("orient","l") then
+        orient = "*"
+    else
+        ok = %t
+        try
+            if size(orient,"*") > 1
+                msg = _("%s: Argument #%d: Scalar (1 element) expected.\n")
+                error(msprintf(msg, "mean", 2))
+            end
+        catch
+            ok = %f
+        end
+        if ~ok | and(type(orient)<>[1 10]) | ..
+                 (orient<>["r" "c" "m"] & orient<>1:2)
+            msg = _("%s: Argument #%d: Must be in the set {%s}.\n")
+            error(msprintf(msg, "mean", 2, "1,2,""r"",""c"",""m"""))
+        end
+        if orient == "m" then
+            orient = find(size(x) > 1, 1);
+            if orient == [] then
+                orient = "*"
+            end
+        end
+    end
+
+    // ----------
+    // PROCESSING
+    // ----------
+    if isempty(x) then
+        if orient=="*"
+            y = mean([])
+        else
+            y = sparse(mean([], orient))
+        end
+        return
+    end
+    if orient == "*" then
+        y = sum(x) / size(x,"*")
+    else
+        if orient=="r" then
+            orient = 1
+        elseif orient=="c"
+            orient = 2
+        end
+        y = sum(x, orient) ./ size(x,orient)
+    end
+endfunction
index 587edee..c20ee88 100644 (file)
@@ -1,6 +1,6 @@
 // =============================================================================
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
-// Copyright (C) 2017 - Samuel GOUGEON
+// Copyright (C) 2017 - 2020 - Samuel GOUGEON
 //
 //  This file is distributed under the same license as the Scilab package.
 // =============================================================================
@@ -18,7 +18,28 @@ assert_checkequal(mean(1, 1), 1);
 assert_checkequal(mean(1, 2), 1);
 
 
+// Sparse numeric
+// --------------
+s = sprand(20,30,0.15);
+objects = list(sparse([]), s(:)', s(:), s);
+for O = objects
+    o = full(O);
+    m = mean(O);
+    assert_checkequal(m, mean(o));
+    for d = list("r", "c", "m", 1, 2);
+        m = mean(O, d);
+        if d=="m" & ~or(size(o)>1)
+            assert_checkfalse(issparse(m));
+        else
+            assert_checktrue(issparse(m));
+        end
+        assert_checkequal(full(m), mean(o, d));
+    end
+end
+
+
 // Overloading
+// -----------
 m = rand(3,4)*100;
     // for a built-in type
 function s = %i_mean(ob, varargin)