|
| 1 | +%% Copyright (C) 2022 Alex Vong |
| 2 | +%% |
| 3 | +%% This file is part of OctSymPy. |
| 4 | +%% |
| 5 | +%% OctSymPy is free software; you can redistribute it and/or modify |
| 6 | +%% it under the terms of the GNU General Public License as published |
| 7 | +%% by the Free Software Foundation; either version 3 of the License, |
| 8 | +%% or (at your option) any later version. |
| 9 | +%% |
| 10 | +%% This software is distributed in the hope that it will be useful, |
| 11 | +%% but WITHOUT ANY WARRANTY; without even the implied warranty |
| 12 | +%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | +%% the GNU General Public License for more details. |
| 14 | +%% |
| 15 | +%% You should have received a copy of the GNU General Public |
| 16 | +%% License along with this software; see the file COPYING. |
| 17 | +%% If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +%% -*- texinfo -*- |
| 20 | +%% @documentencoding UTF-8 |
| 21 | +%% @defmethod @@sym piecewise (@var{cond1}, @var{val1}, @var{cond2}, @var{val2}, @dots{}) |
| 22 | +%% @defmethodx @@sym piecewise (@var{cond1}, @var{val1}, @var{cond2}, @var{val2}, @dots{}, @var{else_val}) |
| 23 | +%% Construct piecewise function. |
| 24 | +%% |
| 25 | +%% The returned piecewise function evaluates to @var{val1} if @var{cond1} |
| 26 | +%% holds, @var{val2} if @var{cond2} holds, @dots{} etc. In the case where none |
| 27 | +%% of the conditions hold, it evaluates to @var{else_val} if provided. If |
| 28 | +%% @var{else_val} is not provided, it evaluates to @code{nan}. |
| 29 | +%% |
| 30 | +%% Examples: |
| 31 | +%% @example |
| 32 | +%% @group |
| 33 | +%% syms x real |
| 34 | +%% f = piecewise (abs (x) < 1, exp (- 1 / (1 - x^2)), abs (x) >= 1, 0) |
| 35 | +%% @result{} f = (sym) |
| 36 | +%% ⎧ -1 |
| 37 | +%% ⎪ ────── |
| 38 | +%% ⎪ 2 |
| 39 | +%% ⎨ 1 - x |
| 40 | +%% ⎪ℯ for │x│ < 1 |
| 41 | +%% ⎪ |
| 42 | +%% ⎩ 0 otherwise |
| 43 | +%% @end group |
| 44 | +%% @end example |
| 45 | +%% |
| 46 | +%% For this piecewise function, we can omit the redundant condition at the end: |
| 47 | +%% @example |
| 48 | +%% @group |
| 49 | +%% syms x real |
| 50 | +%% f = piecewise (abs (x) < 1, exp (- 1 / (1 - x^2)), 0) |
| 51 | +%% @result{} f = (sym) |
| 52 | +%% ⎧ -1 |
| 53 | +%% ⎪ ────── |
| 54 | +%% ⎪ 2 |
| 55 | +%% ⎨ 1 - x |
| 56 | +%% ⎪ℯ for │x│ < 1 |
| 57 | +%% ⎪ |
| 58 | +%% ⎩ 0 otherwise |
| 59 | +%% @end group |
| 60 | +%% @end example |
| 61 | +%% |
| 62 | +%% @seealso{if} |
| 63 | +%% @end defmethod |
| 64 | + |
| 65 | + |
| 66 | +function f = piecewise (varargin) |
| 67 | + if nargin < 1 |
| 68 | + print_usage (); |
| 69 | + end |
| 70 | + |
| 71 | + cmd = {'def chunks_of(ls, n):' |
| 72 | + ' return itertools.zip_longest(*[ls[k::n] for k in range(n)])' |
| 73 | + 'args = [(val, cond) if val is not None else (cond, True)' |
| 74 | + ' for cond, val in chunks_of(_ins, 2)]' |
| 75 | + 'return Piecewise(*args)' |
| 76 | + }; |
| 77 | + |
| 78 | + args = cellfun (@sym, varargin, 'UniformOutput', false); |
| 79 | + f = pycall_sympy__ (cmd, args{:}); |
| 80 | +end |
| 81 | + |
| 82 | + |
| 83 | +%!test |
| 84 | +%! % basic |
| 85 | +%! syms x real |
| 86 | +%! f = piecewise (abs (x) < 1, 1); |
| 87 | +%! assert (isnan (subs (f, -1))); |
| 88 | +%! assert (isequal (subs (f, 0), 1)); |
| 89 | +%! assert (isnan (subs (f, 1))); |
| 90 | + |
| 91 | +%!test |
| 92 | +%! % heaviside |
| 93 | +%! syms x real |
| 94 | +%! f = rewrite (heaviside (x, 1 / sym (2)), 'Piecewise'); |
| 95 | +%! g = piecewise (x < 0, 0, x == 0, 1 / sym (2), x > 0, 1); |
| 96 | +%! assert (logical (simplify (f == g))); |
| 97 | + |
| 98 | +%% FIXME: expand test suite, add SMT compat tests, ... |
0 commit comments