Piecewise functions in the Octave symbolic package?

Unlike Matlab, Octave Symbolic has no piecewise function. Is there any work? I would like to do something like this:

 syms x
 y = piecewise (x0, 1)

Also, how to get pieces of piecewise function? I ran the following:

 >> int (exp (-a * x), x, 0, t)

And I received the following correct answer, displayed and stored in a variable:

                                                                                  
       t for a = 0

        -a * t
   1 e
   - - ----- otherwise
   aa

But now I would like to access the โ€œotherโ€ part of the answer so that I can process it. How can I do it?

(Yes, I can influence this in my head, but I train when more complex expressions appear. I am also really looking for an approach using symbolic expressions - although numerical numbers may work fine anyway, I want to understand the symbolic approach.)

Thanks!

+8
octave
source share
1 answer

The Matlab piecewise function seems pretty new (introduced in 2016b), but basically it looks like an illustrious ternary operator. Unfortunately, I do not have 2016 to check if it performs any checks on inputs or not, but in general you can recreate the โ€œtripleโ€ operator in an octave by indexing into a cell using logical indexing. For example.

 {@() return_A(), @() return_B(), @() return_default()}([test1, test2, true]){1}() 

Explanation:

  • Step 1: You put all the values โ€‹โ€‹of interest in an array of cells. Wrap them in functional descriptors if you want them not to be evaluated during parsing (for example, if you want the output of a ternary operator to cause an error)
  • Step 2. Index this array of cells using logical indexing, where in each index you perform a logical test
  • Step 3: If you need a โ€œdefaultโ€ case, use the โ€œtrueโ€ test for the last item.
  • Step 4: From the array of cells (sub), which is obtained above, select the first element and "run" the resulting function descriptor. Selecting the first element leads to the fact that when you successfully complete several tests, you select only the first result; provided that the default test is always successful, it also ensures that it will not be selected unless it performs the first and only test (which it does by default).

Below are the steps described above implemented in the function (the corresponding brevity is checked here for brevity), following the same syntax as matlab piecewise :

 function Out = piecewise (varargin) Conditions = varargin(1:2:end); % Select all 'odd' inputs Values = varargin(2:2:end); % Select all 'even' inputs N = length (Conditions); if length (Values) ~= N % 'default' case has been provided Values{end+1} = Conditions{end}; % move default return-value to 'Values' Conditions{end} = true; % replace final (ie. default) test with true end % Wrap return-values into function-handles ValFuncs = cell (1, N); for n = 1 : N; ValFuncs{n} = @() Values{n}; end % Grab funhandle for first successful test and call it to return its value Out = ValFuncs([Conditions{:}]){1}(); end 

Usage example:

 >> syms xt; >> F = @(a) piecewise(a == 0, t, (1/a)*exp(-a*t)/a); >> F(0) ans = (sym) t >> F(3) ans = (sym) -3โ‹…t โ„ฏ โ”€โ”€โ”€โ”€โ”€ 9 
0
source share

All Articles