From c9639e7ed5b9882910c3fffc7c74d510f40e1395 Mon Sep 17 00:00:00 2001 From: Samuel GOUGEON Date: Thu, 24 Dec 2020 01:05:16 +0100 Subject: [PATCH] * Bug 16620 fixed: polyint() introduced http://bugzilla.scilab.org/16620 polyint page (en_US PDF): http://bugzilla.scilab.org/attachment.cgi?id=5204 Change-Id: I4d8c0e7d754886bf2900796ee2113f44311725c8 --- scilab/CHANGES.md | 2 + .../helptools/data/configuration/scilab_macros.txt | 1 + scilab/modules/polynomials/help/en_US/polyint.xml | 177 ++++++++++++++++++++ scilab/modules/polynomials/macros/polyint.sci | 92 ++++++++++ .../polynomials/tests/unit_tests/polyint.tst | 55 ++++++ 5 files changed, 327 insertions(+) create mode 100644 scilab/modules/polynomials/help/en_US/polyint.xml create mode 100644 scilab/modules/polynomials/macros/polyint.sci create mode 100644 scilab/modules/polynomials/tests/unit_tests/polyint.tst diff --git a/scilab/CHANGES.md b/scilab/CHANGES.md index 7349040..6b4e6cc 100644 --- a/scilab/CHANGES.md +++ b/scilab/CHANGES.md @@ -223,6 +223,7 @@ Feature changes and additions on 6.1.1 * `kron` and `.*.` are extended to boolean arrays. * `gamma` is extended to incomplete gamma integrals. * `close` is extended to close the help browser, xcos, or the variables browser or editor GUIs. +* `polyint` is introduced to compute polynomial antiderivatives. Help pages: @@ -416,6 +417,7 @@ Bug Fixes * [#16613](https://bugzilla.scilab.org/16613): Neither `.*.` nor `kron()` worked with boolean or sparse boolean matrices. * [#16617](https://bugzilla.scilab.org/16617): `gamma` did not support incomplete gamma integrals. * [#16618](https://bugzilla.scilab.org/16618): `close` could not close the help browser, xcos, nor the variable editor or browser. +* [#16620](https://bugzilla.scilab.org/16620): `derivat` had no reciprocal `polyint` polynomial integration function. * [#16622](https://bugzilla.scilab.org/16622): `inv` could no longer be overloaded for hypermatrices of decimal or complex numbers. * [#16623](https://bugzilla.scilab.org/16623): `rand(2,2,2)^2` yielded a wrong result instead of trying to call the `%s_p_s` overload for input hypermatrices. * [#16629](https://bugzilla.scilab.org/16629): `interp1`'s documentation did not tell the spline edges conditions ; extrapolation modes were poorly explained. ; the description of the result's size was completely wrong ; x as an option was not documented. A wrong extrapolation value could silently return a wrong result. There was some dead code like `if varargin(5)==%nan`. A bugged error message yielded its own error. When x is implicit, the argument index in error messages could be wrong. `periodic` and `edgevalue` extrapolation modes were not available. `linear` extrapolation was not available for splines. When `xp` is an hypermatrix with `size(xp,1)==1`, the size of the result was irregular/wrong. diff --git a/scilab/modules/helptools/data/configuration/scilab_macros.txt b/scilab/modules/helptools/data/configuration/scilab_macros.txt index 382907c..6534624 100644 --- a/scilab/modules/helptools/data/configuration/scilab_macros.txt +++ b/scilab/modules/helptools/data/configuration/scilab_macros.txt @@ -571,6 +571,7 @@ pfactors pol2des pol2str polfact +polyint rowcompr sylm systmat diff --git a/scilab/modules/polynomials/help/en_US/polyint.xml b/scilab/modules/polynomials/help/en_US/polyint.xml new file mode 100644 index 0000000..8125081 --- /dev/null +++ b/scilab/modules/polynomials/help/en_US/polyint.xml @@ -0,0 +1,177 @@ + + + + + polyint + + Polynomial integration + + + + Syntax + + polyint // example + Q = polyint(P) + Q = polyint(P, Const) + + + + Arguments + + + P, Q + + arrays (from scalars to hypermatrices) of polynomials with real or complex + coefficients, of same size. + + + + + Const + + Set of real or complex constants of integration. + length(Const) sets the number of consecutive integrations + to perform. By default, 0 is used. + Const can be one of the following: + + + a scalar: then only the first antiderivatives are computed, and all use + the same Const value. + + + an array of size size(P): then only the first antiderivatives are computed, + with the respective Const(i) for Q(i). + + + a list that can mix scalars, arrays of size size(P), or undefined elements. + Then Const(i) is used for the ith + antiderivatives. Undefined elements are equivalent to 0. + + + + + + + + + Description + + polyint(..) computes the first or any nth antiderivative + of each polynomial of the input array. + + + + Examples + + Single integration (first order): + + + P = [-2, s ; s^2-3, s-2*s^3] + P = + -2 s + -3 +s² s -2s³ + +--> polyint(P) + ans = + -2s 0.5s² + -3s +0.3333333s³ 0.5s² -0.5s⁴ + +--> polyint(P, 1) + ans = + 1 -2s 1 +0.5s² + 1 -3s +0.3333333s³ 1 +0.5s² -0.5s⁴ + +--> polyint(P, [1 2 ; 3 4]) + ans = + 1 -2s 2 +0.5s² + 3 -3s +0.3333333s³ 4 +0.5s² -0.5s⁴ +]]> + + + Multiple integrations: + + + polyint(1-3*s, list(-1,1)) + ans = + 1 -s +0.5s² -0.5s³ + +--> P = [3 ; 1-s ; 6*s^2-2] + P = + 3 + 1 -s + -2 +6s² + +--> polyint(P, list(2, [-2 1 0])) + ans = + -2 +2s +1.5s² + 1 +2s +0.5s² -0.1666667s³ + 2s -s² +0.5s⁴ + +--> polyint(P, list(, [-2 1 0])) + ans = + -2 +1.5s² + 1 +0.5s² -0.1666667s³ + -s² +0.5s⁴ + + +--> k = list(); k(5) = 0; +--> polyint(1+0*s, k) + ans = + 0.0083333s⁵ + +--> 1 / factorial(5) + ans = + 0.0083333 +]]> + + + See also + + + derivat + + + + + History + + + 6.1.1 + + polyint() introduced. + + + + + diff --git a/scilab/modules/polynomials/macros/polyint.sci b/scilab/modules/polynomials/macros/polyint.sci new file mode 100644 index 0000000..5642639 --- /dev/null +++ b/scilab/modules/polynomials/macros/polyint.sci @@ -0,0 +1,92 @@ +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2020 - Samuel GOUGEON - Le Mans Université +// +// 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 M = polyint(M, cstes) + // M : array (from scalar to hypermatrix) of polynomials with real or + // complex coefficients + // Cstes: Integration constants (real or complex): scalar, array of size + // size(M), or list of mixed scalar or sized(M) arrays or + // undefined elements (valued as 0). + + // EXAMPLE + // ======= + if argn(2)==0 then + m = mode(); mode(6) + M = [ -1+%z, 1+2*%z+%z^2 ; 2-%z^3, 6] + mprintf("\n%s", "polyint(M)") + I = polyint(M) + mprintf("\n%s", "polyint(M, 1)") + I = polyint(M, 1) + mprintf("\n%s", "polyint(M, [1 2 ; 3 4])") + I = polyint(M, [1 2 ; 3 4]) + mode(m) + M = I + return + end + + s = size(M) + n = length(M) + + // CHECKING ARGUMENTS + // ================== + rhs = argn(2) + if type(M)<>2 then + msg = _("%s: Argument #%d: Polynomial expected.\n") + error(msprintf(msg, "polyint",1)) + end + if ~isdef("cstes","l") + cstes = 0 + end + if and(type(cstes)<>[1 15]) + msg = _("%s: Argument #%d: numbers or list of numbers expected.\n") + error(msprintf(msg, "polyint",2)) + end + if type(cstes) <> 15 then + cstes = list(cstes) + end + for i = 1:length(cstes) + k = cstes(i) + if isdef("k","l") + if type(k) <> 1 + msg = _("%s: Argument #%d(%d): Decimal or complex number expected.\n") + error(msprintf(msg, "polyint",2,i)) + end + if length(k)<>1 & or(size(k)<>s) + msg = _("%s: Argument #%d(%d): Array of size [%s] expected.\n") + ts = strcat(msprintf("%d\n",s(:))," ") + error(msprintf(msg, "polyint", 2, i, ts)) + end + end + end + + // PROCESSING + // ========== + vn = poly(0, varn(M)) + M = matrix(M, -1, 1) + for i = cstes + if ~isdef("i","l") + i = zeros(n,1) + elseif length(i)==1 + i = i * ones(n,1) + else + i = matrix(i,-1,1) + end + M = M * vn + d = max(max(degree(M),0)) + p = (0:d) .*. ones(M) + c = coeff(M) + k = find(p > 0) + c(k) = c(k) ./ p(k) + c(:,1) = c(:,1) + i + M = inv_coeff(c, d, varn(M)) + end + + M = matrix(M, s) +endfunction diff --git a/scilab/modules/polynomials/tests/unit_tests/polyint.tst b/scilab/modules/polynomials/tests/unit_tests/polyint.tst new file mode 100644 index 0000000..1cfd31c --- /dev/null +++ b/scilab/modules/polynomials/tests/unit_tests/polyint.tst @@ -0,0 +1,55 @@ +// ============================================================================= +// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab +// Copyright (C) 2020 - Samuel GOUGEON - Le Mans Université +// +// This file is distributed under the same license as the Scilab package. +// ============================================================================= +// +// <-- CLI SHELL MODE --> +// <-- NO CHECK REF --> + +s = %s; +P = s^[0 1 ; 2 3]; + +// SINGLE INTEGRATION +// ================== +assert_checkequal(polyint(s), s^2/2); +Ref0 = s^[1 2 ; 3 4] ./ [1 2 ; 3 4]; +assert_checkequal(polyint(P), Ref0); +Ref = [0, -s+0.5*s^2 ; -s+s^3/3,-s+0.25*s^4]; +assert_checkequal(polyint(P-1), Ref); + +// With consts +// ----------- +assert_checkequal(polyint(P, 1), Ref0+1); +assert_checkequal(polyint(P, [1 2 ; 3 4]), Ref0+[1 2 ; 3 4]); +assert_checkequal(polyint(P, list([1 2 ; 3 4])), Ref0+[1 2 ; 3 4]); + + +// MULTIPLE INTEGRATIONS +// ===================== +assert_checkequal(polyint(s,list(-1,1)), 1-s+s^3/6); + +o = list(); o(5) = 0; // undefined elements #1-4 +assert_checkequal(polyint(1+0*s, o), s^5/factorial(5)); + +Ref = [-2+2*s+1.5*s^2 ; 1+2*s+0.5*s^2-s^3/6 ; 2*s-s^2+s^4/12]; +assert_checkequal(polyint([3 ; 1-s ; s^2-2], list(2,[-2 1 0]')), Ref); +Ref = [-2 + 1.5*s^2 ; 1 + 0.5*s^2 - s^3/6 ; -s^2 + s^4/12]; +assert_checkequal(polyint([3 ; 1-s ; s^2-2], list(,[-2 1 0]')), Ref); + + +// ERROR MESSAGES +// ============== +assert_checkerror("polyint(1,2,3)", _("Wrong number of input arguments.")); +msg = msprintf(_("%s: Argument #%d: Polynomial expected.\n"), "polyint",1); +assert_checkerror("polyint(3)", msg); +msg = _("%s: Argument #%d: numbers or list of numbers expected.\n"); +assert_checkerror("polyint(%s, %f)", msprintf(msg, "polyint",2)); +msg = _("%s: Argument #%d(%d): Decimal or complex number expected.\n"); +assert_checkerror("polyint(%s, list(3,%f))", msprintf(msg, "polyint",2,2)); +msg = _("%s: Argument #%d(%d): Array of size [%s] expected.\n"); +assert_checkerror("polyint(%s, [1 2])", msprintf(msg, "polyint",2,1,"1 1")); +assert_checkerror("polyint(%s, list([1 2]))", msprintf(msg, "polyint",2,1,"1 1")); +assert_checkerror("polyint(%s, list([1 2],4))", msprintf(msg, "polyint",2,1,"1 1")); +assert_checkerror("polyint(%s, list(4,,[1 2]))", msprintf(msg, "polyint",2,3,"1 1")); -- 1.7.9.5