From ce07ade908c1f54f59ebc62d0451a0e859f0af9f Mon Sep 17 00:00:00 2001 From: "Colin B. Macdonald" Date: Tue, 2 Jan 2024 19:51:32 -0800 Subject: [PATCH] Add collect function Fixes #1019. Thanks Robert Jenssen, who sent such an implementation years ago. I added some doctests. Also fixes upstream issue [1]. [1] https://savannah.gnu.org/bugs/?59014 --- NEWS | 1 + inst/@sym/collect.m | 66 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 inst/@sym/collect.m diff --git a/NEWS b/NEWS index a152126f..f3a56b8b 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ octsympy 3.1.1+ * New symbolic commands: + collect fplot * Move repo to https://github.com/gnu-octave/symbolic diff --git a/inst/@sym/collect.m b/inst/@sym/collect.m new file mode 100644 index 00000000..97240c3b --- /dev/null +++ b/inst/@sym/collect.m @@ -0,0 +1,66 @@ +%% Copyright (C) 2020 Robert Jenssen +%% Copyright (C) 2023-2024 Colin B. Macdonald +%% +%% OctSymPy is free software; you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published +%% by the Free Software Foundation; either version 3 of the License, +%% or (at your option) any later version. +%% +%% This software is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty +%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +%% the GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public +%% License along with this software; see the file COPYING. +%% If not, see . + +%% -*- texinfo -*- +%% @documentencoding UTF-8 +%% @deftypemethod @@sym {@var{e} =} collect (@var{f}, @var{x}) +%% Collect common powers of a term in an expression. +%% +%% An example of collecting terms in a polynomial: +%% @example +%% @group +%% syms x y +%% f = y*x^2 + x*y + x*y^2 +%% @result{} f = (sym) +%% 2 2 +%% x ⋅y + x⋅y + x⋅y +%% +%% collect(f, x) +%% @result{} (sym) +%% 2 ⎛ 2 ⎞ +%% x ⋅y + x⋅⎝y + y⎠ +%% +%% collect(f, y) +%% @result{} (sym) +%% 2 ⎛ 2 ⎞ +%% x⋅y + y⋅⎝x + x⎠ +%% @end group +%% @end example +%% +%% @seealso{@@sym/expand} +%% @end deftypemethod + + +function e = collect(f, varargin) + + if (nargout > 1) + print_usage (); + endif + + f = sym(f); + for i = 1:length(varargin) + varargin{i} = sym(varargin{i}); + endfor + + e = pycall_sympy__ ('return collect(*_ins)', f, varargin{:}); + +endfunction + + +%!test syms x y z +%! f = [x*y + x - 3 + 2*x^2 - z*x^3 + x^3]; +%! assert (logical (collect (f,x) == ((x^3)*(1 - z) + 2*(x^2) + x*(y + 1) - 3)))