* Bug 15354 fixed: invr(%s^2) coffg([]) coffg(scalar) failed 39/19639/3
Samuel GOUGEON [Wed, 3 Jan 2018 20:39:32 +0000 (21:39 +0100)]
  http://bugzilla.scilab.org/15354

Change-Id: Iede3426a3a0fe015d1dee76fe3ff6ac84ab836d6

scilab/CHANGES.md
scilab/modules/polynomials/help/en_US/coffg.xml
scilab/modules/polynomials/help/fr_FR/coffg.xml
scilab/modules/polynomials/help/ja_JP/coffg.xml
scilab/modules/polynomials/help/pt_BR/coffg.xml
scilab/modules/polynomials/macros/coffg.sci
scilab/modules/polynomials/macros/invr.sci
scilab/modules/polynomials/tests/unit_tests/coffg.tst [new file with mode: 0644]
scilab/modules/polynomials/tests/unit_tests/invr.tst [new file with mode: 0644]

index 599256d..5a7560a 100644 (file)
@@ -242,7 +242,7 @@ Help pages:
   `printf`, `sprintf`, `iconvert`, `stdev`, `xlabel`, `and_op`, `or_op`, `permute`, `tree2code`, `%helps`,
   `scilab|scilex`, `flipdim`, `Matplot_properties`, `meshgrid`, `ismatrix`, `xget`, `xset`, `ieee`, `evstr`,
   `uigetfont`, `uigetdir`, `uigetfile`, `uiputfile`, `cat`, `makecell`, `xstring`, `norm`, `barhomogenize`,
-  `colordef`, `matrix`
+  `colordef`, `matrix`, `coffg`
 * rewritten: `consolebox`, `double`, `isoview`, `pixel_drawing_mode`, `householder`, `or`, `|,||`,
  `and`, `&,&&`, `format`, `type`, `typeof`, `brackets`, `setlanguage`, `sleep`, `isinf`, `unique`,
  `bitor`, `bitxor`, `bitand`, `macr2tree`, `geomean`, `clf`, `getPreferencesValue`, `gcd`, `isglobal`,
@@ -449,7 +449,7 @@ the [development mailing list](dev@lists.scilab.org) for a particular toolbox.
 * [#14703](http://bugzilla.scilab.org/show_bug.cgi?id=14703): `clear linspace, type(linspace)` returned 11 instead of 13.
 * [#14883](http://bugzilla.scilab.org/show_bug.cgi?id=14883): `whereis` did not support builtin functions.
 * [#14886](http://bugzilla.scilab.org/show_bug.cgi?id=14886): `Matplot` save/load failed.
-* [#14896](http://bugzilla.scilab.org/show_bug.cgi?id=15272): Using `set` with multiple properties, only the first one was set.
+* [#14896](http://bugzilla.scilab.org/show_bug.cgi?id=14896): Using `set` with multiple properties, only the first one was set.
 * [#14901](http://bugzilla.scilab.org/show_bug.cgi?id=14901): WScilex.exe was stopped immediately when launched from Java.
 * [#14903](http://bugzilla.scilab.org/show_bug.cgi?id=14903): `plot2d` crashed if a current figure has no axes.
 * [#14910](http://bugzilla.scilab.org/show_bug.cgi?id=14910): The `plot()` example was displayed in overlay to the existing graphics.
@@ -527,6 +527,7 @@ the [development mailing list](dev@lists.scilab.org) for a particular toolbox.
 * [#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.
+* [#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.
 
 
 ### Bugs fixed in 6.0.0:
index 0f79abf..007abb3 100644 (file)
@@ -2,6 +2,7 @@
 <!--
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2006-2008 - INRIA - Francois DELBECQUE
+ * Copyright (C) 2018 - Samuel GOUGEON
  *
  * Copyright (C) 2012 - 2016 - Scilab Enterprises
  *
  * 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="coffg">
+<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="coffg">
     <refnamediv>
         <refname>coffg</refname>
-        <refpurpose>inverse of polynomial matrix</refpurpose>
+        <refpurpose>Co-factors of a matrix of polynomials or rationals</refpurpose>
     </refnamediv>
     <refsynopsisdiv>
         <title>Syntax</title>
-        <synopsis>[Ns,d]=coffg(Fs)</synopsis>
+        <synopsis>[tC, d] = coffg(M)</synopsis>
     </refsynopsisdiv>
     <refsection>
         <title>Arguments</title>
         <variablelist>
             <varlistentry>
-                <term>Fs</term>
+                <term>M</term>
                 <listitem>
-                    <para>square polynomial matrix</para>
+                    <para>
+                        square matrix of numbers or polynomials or rationals, with real or complex
+                        coefficients.
+                    </para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>tC</term>
+                <listitem>
+                    <para>square matrix of the type of <varname>M</varname>: Transposed co-matrix
+                    of <varname>M</varname>, such that <literal>tC/d</literal> is equal to
+                    <literal>inv(M)</literal>.
+                    </para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>d</term>
+                <listitem>
+                    <para>
+                        scalar: determinant <literal>det(M)</literal>, with the
+                        <varname>M</varname>'s data type and encoding.
+                    </para>
                 </listitem>
             </varlistentry>
         </variablelist>
     <refsection>
         <title>Description</title>
         <para>
-            <literal>coffg</literal> computes <literal>Fs^-1</literal> where <literal>Fs</literal> is a polynomial
-            matrix by co-factors method.
-        </para>
-        <para>
-            <literal>Fs</literal> inverse = <literal>Ns/d</literal>
+            <literal>coffg(M)</literal> computes co-factors of <varname>M</varname>, and returns
+            the transposed co-matrix <varname>tC</varname>.
         </para>
         <para>
-            <literal>d</literal> = common denominator; <literal>Ns</literal> =  numerator (a polynomial matrix)
-        </para>
-        <para>
-            (For large matrices,be patient...results are generally reliable)
+            When <varname>M</varname> has a single component, <varname>tC</varname> is
+            <literal>1</literal> and <literal>d==M</literal>
         </para>
+        <warning>
+            For large matrices, calculations may be slow.
+        </warning>
     </refsection>
     <refsection>
         <title>Examples</title>
         <programlisting role="example"><![CDATA[
-s=poly(0,'s')
-a=[ s, s^2+1; s  s^2-1];
-[a1,d]=coffg(a);
-(a1/d)-inv(a)
+// With polynomials
+a = [ %s, %s^2+1; %s  %s^2-1];
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
+
+// With rationals
+a = [%s 0 ; 0 1/%s]
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
  ]]></programlisting>
     </refsection>
     <refsection role="see also">
         <title>See also</title>
         <simplelist type="inline">
             <member>
-                <link linkend="determ">determ</link>
+                <link linkend="inv">inv</link>
             </member>
             <member>
-                <link linkend="detr">detr</link>
+                <link linkend="invr">invr</link>
             </member>
             <member>
-                <link linkend="invr">invr</link>
+                <link linkend="glever">glever</link>
             </member>
             <member>
-                <link linkend="penlaur">penlaur</link>
+                <link linkend="determ">determ</link>
             </member>
             <member>
-                <link linkend="glever">glever</link>
+                <link linkend="detr">detr</link>
             </member>
         </simplelist>
     </refsection>
index 356b3a0..1110eb6 100644 (file)
@@ -1,20 +1,58 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<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="fr" xml:id="coffg">
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) 2006-2008 - INRIA - Francois DELBECQUE
+ * Copyright (C) 2018 - Samuel GOUGEON
+ *
+ * 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="fr" xml:id="coffg">
     <refnamediv>
         <refname>coffg</refname>
-        <refpurpose>inverse d'une matrice de polynômes  </refpurpose>
+        <refpurpose>Co-facteurs d'une matrice carrée de polynômes ou de fractions rationnelles</refpurpose>
     </refnamediv>
     <refsynopsisdiv>
-        <title>Séquence d'appel</title>
-        <synopsis>[Ns,d]=coffg(Fs)</synopsis>
+        <title>Syntaxe</title>
+        <synopsis>[tC, d] = coffg(M)
+        </synopsis>
     </refsynopsisdiv>
     <refsection>
-        <title>Paramètres</title>
+        <title>Arguments</title>
         <variablelist>
             <varlistentry>
-                <term>Fs  </term>
+                <term>M</term>
                 <listitem>
-                    <para>matrice carrée de polynômes
+                    <para>
+                        Matrice carrée de nombres ou de polynômes ou de fractions rationnelles,
+                        à coefficients réels ou complexes.
+                    </para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>tC</term>
+                <listitem>
+                    <para>Matrice carrée du type de <varname>M</varname> : transposée de la co-matrice
+                    de <varname>M</varname>, telle que <literal>tC/d</literal> égale <literal>inv(M)</literal>.
+                    </para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>d</term>
+                <listitem>
+                    <para>
+                        élément unique : déterminant <literal>det(M)</literal>, de type et encodage
+                        identiques à ceux de <varname>M</varname>.
                     </para>
                 </listitem>
             </varlistentry>
     <refsection>
         <title>Description</title>
         <para>
-            <literal>coffg</literal> calcule <literal>Fs^-1</literal> par la méthode des cofacteurs, où <literal>Fs</literal> est une matrice de polynômes.
-        </para>
-        <para>
-            inverse de <literal>Fs</literal> = <literal>Ns/d</literal>
-        </para>
-        <para>
-            <literal>d</literal> = dénominateur commun; <literal>Ns</literal> = numérateur (une matrice de polynômes).
+            <literal>coffg(M)</literal> calcule les co-facteurs de la matrice <varname>M</varname>,
+            et retourne la transposée <varname>tC</varname> de sa co-matrice.
         </para>
         <para>
-            (pour les grandes matrices, s'armer de patience... les résultats sont en général fiables)
+            Lorsque <varname>M</varname> est un élément unique, <varname>tC</varname> est
+            <literal>1</literal> et <literal>d==M</literal>
         </para>
+        <warning>
+            Pour une matrice de grande taille, les calculs peuvent être longs.
+        </warning>
     </refsection>
     <refsection>
         <title>Exemples</title>
         <programlisting role="example"><![CDATA[
-s=poly(0,'s')
-a=[ s, s^2+1; s  s^2-1];
-[a1,d]=coffg(a);
-(a1/d)-inv(a)
+// Matrice de polynômes
+a = [ %s, %s^2+1; %s  %s^2-1];
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
+
+// Matrice de fractions rationnelles
+a = [%s 0 ; 0 1/%s]
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
  ]]></programlisting>
     </refsection>
     <refsection role="see also">
         <title>Voir aussi</title>
         <simplelist type="inline">
             <member>
-                <link linkend="determ">determ</link>
+                <link linkend="inv">inv</link>
             </member>
             <member>
-                <link linkend="detr">detr</link>
+                <link linkend="invr">invr</link>
             </member>
             <member>
-                <link linkend="invr">invr</link>
+                <link linkend="glever">glever</link>
             </member>
             <member>
-                <link linkend="penlaur">penlaur</link>
+                <link linkend="determ">determ</link>
             </member>
             <member>
-                <link linkend="glever">glever</link>
+                <link linkend="detr">detr</link>
             </member>
         </simplelist>
     </refsection>
index 4f6e659..71b215f 100644 (file)
  * 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="coffg">
+<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="coffg">
     <refnamediv>
         <refname>coffg</refname>
         <refpurpose>多項式行列の逆</refpurpose>
     </refnamediv>
     <refsynopsisdiv>
         <title>呼び出し手順</title>
-        <synopsis>[Ns,d]=coffg(Fs)</synopsis>
+        <synopsis>
+            [Ns,d] = coffg(Fs)
+        </synopsis>
     </refsynopsisdiv>
     <refsection>
         <title>パラメータ</title>
     <refsection>
         <title>例</title>
         <programlisting role="example"><![CDATA[
-s=poly(0,'s')
-a=[ s, s^2+1; s  s^2-1];
-[a1,d]=coffg(a);
-(a1/d)-inv(a)
+// With polynomials
+a = [ %s, %s^2+1; %s  %s^2-1];
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
+
+// With rationals
+a = [%s 0 ; 0 1/%s]
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
  ]]></programlisting>
     </refsection>
     <refsection role="see also">
         <title>参照</title>
         <simplelist type="inline">
             <member>
-                <link linkend="determ">determ</link>
+                <link linkend="inv">inv</link>
             </member>
             <member>
-                <link linkend="detr">detr</link>
+                <link linkend="invr">invr</link>
             </member>
             <member>
-                <link linkend="invr">invr</link>
+                <link linkend="glever">glever</link>
             </member>
             <member>
-                <link linkend="penlaur">penlaur</link>
+                <link linkend="determ">determ</link>
             </member>
             <member>
-                <link linkend="glever">glever</link>
+                <link linkend="detr">detr</link>
             </member>
         </simplelist>
     </refsection>
index f8a035e..1417bda 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
+<?xml version="1.0" encoding="UTF-8"?>
 <!--
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) 2006-2008 - INRIA - Francois DELBECQUE
  * 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:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="coffg" xml:lang="pt">
+<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
+        xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
+        xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
+        xmlns:scilab="http://www.scilab.org" xml:id="coffg" xml:lang="pt">
     <refnamediv>
         <refname>coffg</refname>
-        <refpurpose>matriz inversa de polinômios</refpurpose>
+        <refpurpose>matriz inversa de polinômios</refpurpose>
     </refnamediv>
     <refsynopsisdiv>
-        <title> Seqüência de Chamamento </title>
+        <title> Seqüência de Chamamento </title>
         <synopsis>[Ns,d]=coffg(Fs)</synopsis>
     </refsynopsisdiv>
     <refsection>
-        <title>Parâmetros</title>
+        <title>Parâmetros</title>
         <variablelist>
             <varlistentry>
                 <term>Fs</term>
                 <listitem>
-                    <para>matriz quadrada de polinômios</para>
+                    <para>matriz quadrada de polinômios</para>
                 </listitem>
             </varlistentry>
         </variablelist>
     </refsection>
     <refsection>
-        <title>Descrição</title>
+        <title>Descrição</title>
         <para>
-            <literal>coffg</literal> computa <literal>Fs^-1</literal> onde
-            <literal>Fs</literal> é uma matriz de polinômios, pelo método dos
-            cofatores.
+            <literal>coffg</literal> computa <literal>Fs^-1</literal> onde <literal>Fs</literal>
+            é uma matriz de polinômios, pelo método dos cofatores.
         </para>
         <para>
             <literal>Fs</literal> inverse = <literal>Ns/d</literal>
         </para>
         <para>
             <literal>d</literal> = denominador comum; <literal>Ns</literal> =
-            numerador (uma matriz de polinômios)
+            numerador (uma matriz de polinômios)
         </para>
         <para>(Para matrizes grandes, seja paciente... os resultados geralmente
-            são confiáveis)
+            são confiáveis)
         </para>
     </refsection>
     <refsection>
         <title>Exemplos</title>
         <programlisting role="example"><![CDATA[
-s=poly(0,'s')
-a=[ s, s^2+1; s  s^2-1];
-[a1,d]=coffg(a);
-(a1/d)-inv(a)
+// With polynomials
+a = [ %s, %s^2+1; %s  %s^2-1];
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
+
+// With rationals
+a = [%s 0 ; 0 1/%s]
+[a1, d] = coffg(a)
+(a1/d) - inv(a)
  ]]></programlisting>
     </refsection>
     <refsection>
-        <title> Ver Também </title>
+        <title> Ver Também </title>
         <simplelist type="inline">
             <member>
-                <link linkend="determ">determ</link>
+                <link linkend="inv">inv</link>
             </member>
             <member>
-                <link linkend="detr">detr</link>
+                <link linkend="invr">invr</link>
             </member>
             <member>
-                <link linkend="invr">invr</link>
+                <link linkend="glever">glever</link>
             </member>
             <member>
-                <link linkend="penlaur">penlaur</link>
+                <link linkend="determ">determ</link>
             </member>
             <member>
-                <link linkend="glever">glever</link>
+                <link linkend="detr">detr</link>
             </member>
         </simplelist>
     </refsection>
index 7379089..0cfc03e 100644 (file)
@@ -1,5 +1,6 @@
 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 // Copyright (C) ????-2008 - INRIA - Francois DELBECQUE
+// Copyright (C) 2018 - Samuel GOUGEON
 //
 // Copyright (C) 2012 - 2016 - Scilab Enterprises
 //
@@ -11,7 +12,7 @@
 // along with this program.
 
 
-function [Ns,d]=coffg(Fs)
+function [Ns,d] = coffg(Fs)
     // [Ns,d]=coffg(Fs) computes Fs^-1 where Fs is a polynomial
     // or rational matrix by co-factors method.
     // d = common denominator; Ns =  numerator (matrix polynomial)
@@ -20,33 +21,34 @@ function [Ns,d]=coffg(Fs)
     // See also det, detr, invr, penlaur, glever, leverrier
     //!
     //
-    if or(typeof(Fs)==["polynomial" "constant"]) then
-        [n,np]=size(Fs);
-        if n<>np then
-            error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"coffg",1))
-        end
-        d=det(Fs) // common denominator
-        n1=n;
-        for kk=1:n1,for l=1:n1,
-                signe=(-1)^(kk+l);
-                col=[1:kk-1,kk+1:n1];row=[1:l-1,l+1:n1];
-                Ns(kk,l)=-signe*det(Fs(row,col))
-        end;end
-        Ns=-Ns;
-    elseif typeof(Fs)=="rational" then
-        [n,np]=size(Fs);
-        if n<>np then
-            error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"coffg",1))
+    if ~or(typeof(Fs)==["constant" "polynomial" "rational"]) then
+        msg = gettext("%s: Wrong type for input argument #%d: A floating point number or polynomial or rational fraction array expected.\n")
+        error(msprintf(msg,"detr",1))
+    end
+    [n, np] = size(Fs);
+    if n<>np then
+        msg = gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n")
+        error(msprintf(msg,"coffg",1))
+    end
+
+    if n==0 then
+        Ns = [];
+        d = [];
+        return
+    elseif n==1 then
+        d = Fs;         // == det(Fs)
+        Ns = 1 + 0/Fs;
+        return
+    end
+
+    d = det(Fs) // common denominator
+    for kk = 1:n
+        for l = 1:n
+            signe = (-1)^(kk+l);
+            col = [1:kk-1, kk+1:n];
+            row = [1:l-1, l+1:n];
+            Ns(kk,l) = -signe*det(Fs(row,col));
         end
-        d=det(Fs) // common denominator
-        n1=n;
-        for kk=1:n1,for l=1:n1,
-                signe=(-1)^(kk+l);
-                col=[1:kk-1,kk+1:n1];row=[1:l-1,l+1:n1];
-                Ns(kk,l)=-signe*det(Fs(row,col))
-        end;end
-        Ns=-Ns;
-    else
-        error(msprintf(gettext("%s: Wrong type for input argument #%d: A floating point number or polynomial or rational fraction array expected.\n"),"detr",1))
     end
+    Ns = -Ns;
 endfunction
index 875f7a8..6c2a232 100644 (file)
 // For more information, see the COPYING file which you should have received
 // along with this program.
 
-
-function [f,d]=invr(h,flag)
+function [f, d] = invr(h, flag)
     //if h is a scalar, polynomial or rational function matrix, invr
     //computes h^(-1).
+    // "flag" and "d" are not documented. Are they internal?
     //!
+    // The matrix must be square:
+    [m,n] = size(h);
+    if m<>n then
+        msg = gettext("%s: Argument #%d: Square matrix expected.\n");
+        error(msprintf(msg, "invr", 1));
+    end
+    // default
     if argn(2)==1 then
-        flag="C";
+        flag = "C";
     end
-    lhs=argn(1)
+    lhs = argn(1)
     select typeof(h)
     case "constant" then
-        f=inv(h);
+        f = inv(h);
     case "polynomial" then //POLYNOMIAL MATRIX
-        [m,n]=size(h);
-        if m<>n then error(20),end
-        ndeg=max(degree(h));
+        if m*n==1
+            f = 1/h;
+            return
+        end
+        ndeg = max(degree(h));
         if ndeg==1 then   //MATRIX PENCIL
-            E=coeff(h,1);A=-coeff(h,0);
+            E = coeff(h,1);
+            A = -coeff(h,0);
             if norm(E-eye(E),1) < 100*%eps then
                 // sI -A
-                [num,den]=coff(A,varn(h));f=num/den;
+                [num,den] = coff(A, varn(h));
+                f = num/den;
             else
-                [Bfs,Bis,chis]=glever(E,A,varn(h));
-                f=Bfs/chis - Bis;
+                [Bfs,Bis,chis] = glever(E, A, varn(h));
+                f = Bfs/chis - Bis;
                 if lhs==2 then
-                    d=lcm(f("den"));
-                    f=f*d;f=f("num");
+                    d = lcm(f("den"));
+                    f = f*d;
+                    f = f("num");
                 end
             end
         else // GENERAL POLYNOMIAL MATRIX
             select flag
             case "L"
-                f=eye(n,n);
-                for k=1:n-1,
-                    b=h*f,
-                    d=-sum(diag(b))/k
-                    f=b+eye(n,n)*d,
+                f = eye(n,n);
+                for k = 1:n-1
+                    b = h*f;
+                    d = -sum(diag(b))/k;
+                    f = b+eye(n,n)*d;
                 end;
-                d=sum(diag(h*f))/n,
-                if degree(d)==0 then d=coeff(d),end,
-                if lhs==1 then f=f/d;end
+                d = sum(diag(h*f))/n;
+                if degree(d)==0 then
+                    d = coeff(d);
+                end
+                if lhs==1 then
+                    f = f/d;
+                end
             case "C"
-                [f,d]=coffg(h);
-                if degree(d)==0 then d=coeff(d),end
-                if lhs==1 then f=f/d;end
+                [f,d] = coffg(h);
+                if degree(d)==0 then
+                    d = coeff(d);
+                end
+                if lhs==1 then
+                    f = f/d;
+                end
             else
-                error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
-                "invr",2,"''C'',''D''"))
-            end;
+                msg = gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n")
+                error(msprintf(msg,"invr",2,"''C'',''D''"))
+            end
         end
     case "rational" then
-        [m,n]=size(h(2));
-        if m<>n then error(20),end
         if m==1 then
-          f=h.den
-          d=h.num
-          if lhs==1 then f=f/d;end
+          f = h.den
+          d = h.num
+          if lhs==1 then
+              f = f/d;
+          end
           return
         end
         select flag
         case "L" //    Leverrier
-            f=eye(n,n);
-            for k=1:n-1,
-                b=h*f,
-                d=0;for l=1:n,d=d+b(l,l),end,d=-d/k;
-                f=b+eye(n,n)*d,
+            f = eye(n,n);
+            for k = 1:n-1,
+                b = h*f,
+                d = 0;
+                for l = 1:n,
+                    d = d + b(l,l),
+                end
+                d = -d/k;
+                f = b + eye(n,n)*d,
             end;
-            b=h*f;d=0;for l=1:n,d=d+b(l,l),end;d=d/n,
-            if lhs==1 then f=f/d;end
+            b = h*f;
+            d = 0;
+            for l=1:n
+                d = d+b(l,l)
+            end
+            d = d/n
+            if lhs==1 then
+                f = f/d;
+            end
         case "A" //   lcm of all denominator entries
-            denh=lcm(h("den"));
-            Num=h*denh;Num=Num("num");
-            [N,d]=coffg(Num);
-            f=N*denh;
-            if lhs==1 then f=f/d;end
+            denh = lcm(h("den"));
+            Num = h*denh;
+            Num = Num("num");
+            [N, d] = coffg(Num);
+            f = N*denh;
+            if lhs==1 then
+                f = f/d;
+            end
         case "C"// default method by polynomial inverse
-            [Nh,Dh]=lcmdiag(h); //h=Nh*inv(Dh); Dh diagonal;
-            [N,d]=coffg(Nh);
-            f=Dh*N;
-            if lhs==1 then f=f/d;end
+            [Nh,Dh] = lcmdiag(h); //h=Nh*inv(Dh); Dh diagonal;
+            [N, d]  = coffg(Nh);
+            f = Dh*N;
+            if lhs==1 then
+                f = f/d;
+            end
         case "Cof"// cofactors method
-            [f,d]=coffg(h);
-            if lhs==1 then f=f/d;end
+            [f,d] = coffg(h);
+            if lhs==1 then
+                f = f/d;
+            end
         else
-            error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
-            "invr",2,"''L'',''A'',''C'',''Cof''"))
-        end;
+            msg = gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n");
+            error(msprintf(msg, "invr",2,"''L'',''A'',''C'',''Cof''"))
+        end
     else
-        error(msprintf(gettext("%s: Wrong type for input argument #%d: A floating point number or polynomial or rational fraction array expected.\n"),"invr",1))
-    end;
+        msg = gettext("%s: Wrong type for input argument #%d: A floating point number or polynomial or rational fraction array expected.\n");
+        error(msprintf(msg, "invr", 1))
+    end
 endfunction
diff --git a/scilab/modules/polynomials/tests/unit_tests/coffg.tst b/scilab/modules/polynomials/tests/unit_tests/coffg.tst
new file mode 100644 (file)
index 0000000..c0f51c9
--- /dev/null
@@ -0,0 +1,52 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2018 - Samuel GOUGEON
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+// <-- CLI SHELL MODE -->
+// <-- NO CHECK REF -->
+
+// With []
+// -------
+[n, d] = coffg([]);
+assert_checkequal(n, []);
+assert_checkequal(d, []);
+
+// With scalars
+// ------------
+[n, d] = coffg(5);
+assert_checkequal(n, 1);
+assert_checkequal(d, 5);
+
+[n, d] = coffg(5+0*%i);
+assert_checkequal(n, 1 + 0*%i);
+assert_checkequal(d, 5 + 0*%i);
+
+[n, d] = coffg(%s);
+assert_checkequal(n, 1 + 0/%s);
+assert_checkequal(d, %s);
+
+[n, d] = coffg(1+%s^2);
+assert_checkequal(n, 1 + 0/%s);
+assert_checkequal(d, 1 + %s^2);
+
+[n, d] = coffg(1/(1+%s^2));
+assert_checkequal(n, 1 + 0/%s);
+assert_checkequal(d, 1/(1+%s^2));
+
+
+// With matrices
+// -------------
+p = [%s %s ; %s -%s];
+[n, d] = coffg(p);
+assert_checkequal(n, -p);
+assert_checkequal(d, -2*%s^2);
+assert_checkequal(p*n/d-eye(), zeros(2,2)/%s);
+
+r = [1/%s 0 ; 0 %s^2];
+[n, d] = coffg(r);
+assert_checkequal(n, [%s^2 0 ; 0 1/%s]);
+assert_checkequal(d, %s^2/%s);
+assert_checkequal(r*n/d - eye(), zeros(2,2)/%s);
diff --git a/scilab/modules/polynomials/tests/unit_tests/invr.tst b/scilab/modules/polynomials/tests/unit_tests/invr.tst
new file mode 100644 (file)
index 0000000..0e5fff6
--- /dev/null
@@ -0,0 +1,23 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2018 - Samuel GOUGEON
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+
+// <-- CLI SHELL MODE -->
+// <-- NO CHECK REF -->
+
+assert_checkequal(invr([]), []);
+assert_checkequal(invr(5), 0.2);
+assert_checktrue(invr(5+0*%i)==0.2);
+assert_checkequal(invr(5+0*%i), 0.2+0*%i);
+assert_checkequal(invr(%s), 1/%s);
+assert_checkequal(invr(%s^3), 1/%s^3);
+assert_checkequal(invr(1/%s), %s+0/%s);
+assert_checkequal(invr(1/%s^3), %s^3+0/%s);
+
+p = [%s %s ; %s -%s];
+assert_checkequal(p*invr(p), eye(2,2)+0/%s);
+r = [1/%s 0 ; 0 1/%s^2];
+assert_checkequal(invr(r), [%s 0 ; 0 %s^2]+0/%s);