Mercurial > hg > octave-terminal
changeset 5820:27c966e4b2dc
[project @ 2006-05-17 21:00:54 by jwe]
author | jwe |
---|---|
date | Wed, 17 May 2006 21:00:54 +0000 |
parents | e54c11df0524 |
children | 11e4ae44984f |
files | scripts/ChangeLog scripts/general/cplxpair.m scripts/general/cumtrapz.m scripts/general/isdir.m scripts/general/trapz.m scripts/miscellaneous/dos.m scripts/miscellaneous/getfield.m scripts/miscellaneous/setfield.m scripts/plot/fplot.m scripts/set/intersect.m scripts/signal/filter2.m scripts/signal/ifftshift.m scripts/specfun/betaln.m scripts/specfun/factorial.m scripts/strings/strvcat.m src/ChangeLog |
diffstat | 16 files changed, 1065 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,12 @@ +2006-05-17 David Bateman <dbateman@free.fr> + + * general/cplxpair.m, general/trapz.m, general/cumtrapz.m, + general/isdir.m, miscellaneous/dos.m, miscellaneous/getfield.m, + miscellaneous/setfield.m, plot/fplot.m, set/intersect.m, + signal/ifftshift.m, signal/filter2.m, specfun/betaln.m, + specfun/factorial.m, strings/strvcat.m: New files from Octave + Forge. + 2006-05-11 John W. Eaton <jwe@octave.org> * path/path.m: Delete (now a built-in function). @@ -354,7 +363,7 @@ * general/triu.m: Minimum change to allow sparse matrix. More needed for arbitrary user type. * general/tril.m: ditto. - * sparse/sprand.m : Doc fix. + * sparse/sprand.m: Doc fix. * sparse/sprandn.m: Ditto. * sparse/sprandsym.m: New function. * audio/setaudio.m, general/cart2pol.m, general/cart2sph.m,
new file mode 100644 --- /dev/null +++ b/scripts/general/cplxpair.m @@ -0,0 +1,154 @@ +## Copyright (C) 2000 Paul Kienzle +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} cplxpair (@var{z}, @var{tol}, @var{dim}) +## Sort the numbers @var{z} into complex conjugate pairs ordered by +## increasing real part. With identical real parts, order by increasing +## imaginary magnitude. Place the negative imaginary complex number +## first within each pair. Place all the real numbers after all the +## complex pairs (those with @code {abs ( imag (@var{z}) / @var{z}) < +## @var{tol}}), where the default value of @var{tol} is @code{100 * +## @var{eps}}. +## +## By default the complex pairs are sorted along the first non-singleton +## dimension of @var{z}. If @var{dim} is specified, then the complex +## pairs are sorted along this dimension. +## +## Signal an error if some complex numbers could not be paired. Requires +## all complex numbers to be exact conjugates within tol, or signals an +## error. Note that there are no guarantees on the order of the returned +## pairs with identical real parts but differing imaginary parts. +## +## @example +## cplxpair (exp(2i*pi*[0:4]'/5)) == exp(2i*pi*[3; 2; 4; 1; 0]/5) +## @end example +## @end deftypefn + +## TODO: subsort returned pairs by imaginary magnitude +## TODO: Why doesn't exp(2i*pi*[0:4]'/5) produce exact conjugates. Does +## TODO: it in Matlab? The reason is that complex pairs are supposed +## TODO: to be exact conjugates, and not rely on a tolerance test. + +## 2006-05-12 David Bateman - Modified for NDArrays + +function y = cplxpair (z, tol, dim) + + if nargin < 1 || nargin > 3 + usage ("z = cplxpair (z, tol, dim);"); + endif + + if (length (z) == 0) + y = zeros (size (z)); + return; + endif + + if (nargin < 2 || isempty (tol)) + tol = 100*eps; + endif + + nd = ndims (z); + orig_dims = size (z); + if (nargin < 3) + ## Find the first singleton dimension. + dim = 0; + while (dim < nd && orig_dims(dim+1) == 1) + dim++; + endwhile + dim++; + if (dim > nd) + dim = 1; + endif + else + dim = floor(dim); + if (dim < 1 || dim > nd) + error ("cplxpair: invalid dimension along which to sort"); + endif + endif + + ## Move dimension to treat first, and convert to a 2-D matrix + perm = [dim:nd, 1:dim-1]; + z = permute (z, perm); + sz = size (z); + n = sz (1); + m = prod (sz) / n; + z = reshape (z, n, m); + + ## Sort the sequence in terms of increasing real values + [q, idx] = sort (real (z), 1); + z = z(idx + n * ones (n, 1) * [0:m-1]); + + ## Put the purely real values at the end of the returned list + [idxi, idxj] = find (abs (imag (z)) ./ (abs (z) + realmin) < tol ); + q = sparse (idxi, idxj, 1, n, m); + nr = sum (q, 1); + [q, idx] = sort (q, 1); + z = z(idx); + y = z; + + ## For each remaining z, place the value and its conjugate at the + ## start of the returned list, and remove them from further + ## consideration. + for j = 1:m + p = n - nr(j); + for i=1:2:p + if (i+1 > p) + error ("cplxpair could not pair all complex numbers"); + endif + [v, idx] = min (abs (z(i+1:p) - conj (z(i)))); + if (v > tol) + error ("cplxpair could not pair all complex numbers"); + endif + if (imag (z(i)) < 0) + y([i, i+1]) = z([i, idx+i]); + else + y([i, i+1]) = z([idx+i, i]); + endif + z(idx+i) = z(i+1); + endfor + endfor + + ## Reshape the output matrix + y = ipermute (reshape (y, sz), perm); + +endfunction + +%!demo +%! [ cplxpair(exp(2i*pi*[0:4]'/5)), exp(2i*pi*[3; 2; 4; 1; 0]/5) ] + +%!assert (isempty(cplxpair([]))); +%!assert (cplxpair(1), 1) +%!assert (cplxpair([1+1i, 1-1i]), [1-1i, 1+1i]) +%!assert (cplxpair([1+1i, 1+1i, 1, 1-1i, 1-1i, 2]), \ +%! [1-1i, 1+1i, 1-1i, 1+1i, 1, 2]) +%!assert (cplxpair([1+1i; 1+1i; 1; 1-1i; 1-1i; 2]), \ +%! [1-1i; 1+1i; 1-1i; 1+1i; 1; 2]) +%!assert (cplxpair([0, 1, 2]), [0, 1, 2]); + +%!shared z +%! z=exp(2i*pi*[4; 3; 5; 2; 6; 1; 0]/7); +%!assert (cplxpair(z(randperm(7))), z); +%!assert (cplxpair(z(randperm(7))), z); +%!assert (cplxpair(z(randperm(7))), z); +%!assert (cplxpair([z(randperm(7)),z(randperm(7))]),[z,z]) +%!assert (cplxpair([z(randperm(7)),z(randperm(7))],[],1),[z,z]) +%!assert (cplxpair([z(randperm(7)).';z(randperm(7)).'],[],2),[z.';z.']) + +%!## tolerance test +%!assert (cplxpair([1i, -1i, 1+(1i*eps)],2*eps), [-1i, 1i, 1+(1i*eps)]);
new file mode 100644 --- /dev/null +++ b/scripts/general/cumtrapz.m @@ -0,0 +1,114 @@ +## Copyright (C) 2000 Kai Habel +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{z} =} cumtrapz (@var{y}) +## @deftypefnx {Function File} {@var{z} =} cumtrapz (@var{x}, @var{y}) +## @deftypefnx {Function File} {@var{z} =} cumtrapz (@dots{}, @var{dim}) +## +## Cumulative numerical intergration using trapezodial method. +## @code{trapz (@var{y})} computes the cummulative integral of the +## @var{y} along the first non singleton dimension. If the argument +## @var{x} is omitted a equally spaced vector is assumed. @code{trapz +## (@var{x}, @var{y})} evaluates the cummulative integral with respect +## to @var{x}. +## +## @seealso{trapz,cumsum} +## @end deftypefn + +## Author: Kai Habel <kai.habel@gmx.de> +## +## also: June 2000 Paul Kienzle (fixes,suggestions) +## 2006-05-12 David Bateman - Modified for NDArrays + +function z = cumtrapz (x, y, dim) + + if (nargin < 1) || (nargin > 3) + usage ("cumtrapz (x, y, dim)"); + endif + + nd = ndims (x); + sz = size (x); + + have_x = false; + have_dim = false; + if (nargin == 3) + have_x = true; + have_dim = true; + endif + if (nargin == 2) + if (size (x) != size (y) && isscalar (y)) + dim = y; + have_dim = true; + else + have_x = true; + endif + endif + + if (! have_dim) + ## Find the first singleton dimension. + dim = 0; + while (dim < nd && sz(dim+1) == 1) + dim++; + endwhile + dim++; + if (dim > nd) + dim = 1; + endif + else + dim = floor (dim); + if (dim < 1 || dim > nd) + error ("cumtrapz: invalid dimension along which to sort"); + endif + endif + + n = sz(dim); + idx1 = cell (); + for i = 1:nd + idx1{i} = 1:sz(i); + endfor + idx2 = idx1; + idx1{dim} = 2 : n; + idx2{dim} = 1 : (n - 1); + + if (! have_x) + z = 0.5 * cumsum (x(idx1{:}) + x(idx2{:}), dim); + else + if (size (x) != size (y)) + error ("cumtrapz: x and y must have same shape"); + endif + z = 0.5 * cumsum ((x(idx1{:}) - x(idx2{:})) .* + (y(idx1{:}) + y(idx2{:})), dim); + endif + + sz(dim) = 1; + z = cat (dim, zeros (sz), z); + +endfunction + +%!shared x1,x2,y +%! x1 = [0,0,0;2,2,2]; +%! x2 = [0,2,4;0,2,4]; +%! y = [1,2,3;4,5,6]; +%!assert (cumtrapz(y),[0,0,0;2.5,3.5,4.5]) +%!assert (cumtrapz(x1,y),[0,0,0;5,7,9]) +%!assert (cumtrapz(y,1),[0,0,0;2.5,3.5,4.5]) +%!assert (cumtrapz(x1,y,1),[0,0,0;5,7,9]) +%!assert (cumtrapz(y,2),[0,1.5,4;0,4.5,10]) +%!assert (cumtrapz(x2,y,2),[0,3,8;0,9,20])
new file mode 100644 --- /dev/null +++ b/scripts/general/isdir.m @@ -0,0 +1,31 @@ +## Copyright (C) 2004 Alois Schloegl +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} isdir (@var{f}) +## Return true if @var{f} is a directory. +## @end deftypefn + +function t = isdir (x) + if (nargin == 1) + t = exist (x, "dir"); + else + print_usage ("isdir"); + endif +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/general/trapz.m @@ -0,0 +1,106 @@ +## Copyright (C) 2000 Kai Habel +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{z} =} trapz (@var{y}) +## @deftypefnx {Function File} {@var{z} =} trapz (@var{x}, @var{y}) +## @deftypefnx {Function File} {@var{z} =} trapz (@dots{}, @var{dim}) +## +## Numerical intergration using trapezodial method. @code{trapz +## (@var{y})} computes the integral of the @var{y} along the first +## non singleton dimension. If the argument @var{x} is omitted a +## equally spaced vector is assumed. @code{trapz (@var{x}, @var{y})} +## evaluates the integral with respect to @var{x}. +## +## @seealso{cumtrapz} +## @end deftypefn + +## Author: Kai Habel <kai.habel@gmx.de> +## +## also: June 2000 - Paul Kienzle (fixes,suggestions) +## 2006-05-12 David Bateman - Modified for NDArrays + +function z = trapz (x, y, dim) + + + if (nargin < 1) || (nargin > 3) + usage ("trapz (x, y, dim)"); + endif + + nd = ndims (x); + sz = size (x); + + have_x = false; + have_dim = false; + if (nargin == 3) + have_x = true; + have_dim = true; + endif + if (nargin == 2) + if (size (x) != size (y) && isscalar (y)) + dim = y; + have_dim = true; + else + have_x = true; + endif + endif + + if (! have_dim) + ## Find the first singleton dimension. + dim = 0; + while (dim < nd && sz(dim+1) == 1) + dim++; + endwhile + dim++; + if (dim > nd) + dim = 1; + endif + else + dim = floor (dim); + if (dim < 1 || dim > nd) + error ("cumtrapz: invalid dimension along which to sort"); + endif + endif + + n = sz(dim); + idx1 = cell (); + for i = 1:nd + idx1{i} = 1:sz(i); + endfor + idx2 = idx1; + idx1{dim} = 2 : n; + idx2{dim} = 1 : (n - 1); + + if (! have_x) + z = 0.5 * sum (x(idx1{:}) + x(idx2{:}), dim); + else + if (size (x) != size (y)) + error ("cumtrapz: x and y must have same shape"); + endif + z = 0.5 * sum ((x(idx1{:}) - x(idx2{:})) .* + (y(idx1{:}) + y(idx2{:})), dim); + endif +endfunction + +%!assert (trapz(1:5), 12) +%!assert (trapz(0:0.5:2,1:5), 6) +%!assert (trapz([1:5;1:5],2),[12;12]) +%!assert (trapz([1:5;1:5].',1),[12,12]) +%!assert (trapz([0:0.5:2;0:0.5:2],[1:5;1:5],2),[6;6]) +%!assert (trapz([0:0.5:2;0:0.5:2].',[1:5;1:5].',1),[6,6])
new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/dos.m @@ -0,0 +1,45 @@ +## Copyright (C) 2004 John W. Eaton +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {[@var{status}, @var{text}] =} dos (@var{command}) +## @deftypefnx {Function File} {[@var{status}, @var{text}] =} dos (@var{command}, "-echo") +## Execute a system command if running under a Windos-like operating +## system, otherwise do nothing. Return the exit status of the program +## in @var{status} and any output sent to the standard output in +## @var{text}. If the optional second argument @code{"-echo"} is given, +## then also send the output from the command to the standard output. +## @seealso{unix, isunix, ispc, system} +## @end deftypefn + +## Author: octave-forge ??? +## Adapted by: jwe + +function [status, text] = dos (cmd, echo_arg) + + if (nargin < 1 || nargin > 2) + usage ( "[status, text] = dos (cmd, '-echo')"); + elseif (! isunix ()) + [status, text] = system (cmd); + if (nargin > 1 || nargout == 0) + printf ("%s\n", text); + endif + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/getfield.m @@ -0,0 +1,62 @@ +## Copyright (C) 2000 Etienne Grossmann +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Built-in Function} {} [@var{v1},...] = +## @code{getfield (@var{s}, 'k1',...)} extract fields from a structure. +## For example +## +## @example +## @group +## ss(1,2).fd(3).b=5; +## getfield(ss,@{1,2@},'fd',@{3@},'b') +## @result{} ans = 5 +## @end group +## @end example +## +## Note that this function could be written as +## +## @example +## i1= @{1,2@}; i2= 'fd'; i3= @{3@}; i4= 'b'; +## ss( i1@{:@} ).( i2 )( i3@{:@} ).( i4 ) +## @end example +## @seealso{setfield,rmfield,isfield,isstruct,fields,struct} +## @end deftypefn + +## Author: Etienne Grossmann <etienne@cs.uky.edu> + +function s = getfield (s, varargin) + + for idx = 1:nargin-1 + i = varargin{idx}; + if (iscell (i)) + s = s(i{:}); + else + s = s.(i); + endif + endfor + +endfunction + +%!test +%! x.a = "hello"; +%! assert(getfield(x,"a"),"hello"); +%!test +%! ss(1,2).fd(3).b = 5; +%! assert(getfield(ss,{1,2},'fd',{3},'b'),5)
new file mode 100644 --- /dev/null +++ b/scripts/miscellaneous/setfield.m @@ -0,0 +1,71 @@ +## Copyright (C) 2000 Etienne Grossmann +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Built-in Function} {} [@var{k1},..., @var{v1}] = +## @code{setfield (@var{s}, 'k1',..., 'v1')} sets field members in a structure +## +## @example +## @group +## oo(1,1).f0= 1; +## oo = setfield(oo,@{1,2@},'fd',@{3@},'b', 6); +## oo(1,2).fd(3).b == 6 +## @result{} ans = 1 +## @end group +## @end example +## +## Note that this function could be written +## +## @example +## i1= @{1,2@}; i2= 'fd'; i3= @{3@}; i4= 'b'; +## oo( i1@{:@} ).( i2 )( i3@{:@} ).( i4 ) == 6; +## @end example +## @seealso{getfield, rmfield, isfield, isstruct, fieldnames, struct} +## @end deftypefn + +## Author: Etienne Grossmann <etienne@cs.uky.edu> + +function obj = setfield (obj, varargin) + field = "obj"; + for i = 1:nargin-2 + v = varargin{i}; + if (iscell (v)) + sep = "("; + for j = 1:length (v) + field = sprintf ("%s%s%s", field, sep, num2str (v{j})); + sep = ","; + endfor + field = sprintf ("%s)", field); + else + field = sprintf ("%s.%s", field, v); + endif + endfor + val = varargin{nargin-1}; + eval (sprintf ("%s=val;", field)); +endfunction + +%!test +%! x.a = "hello"; +%! x = setfield(x,"b","world"); +%! y = struct('a','hello','b','world'); +%! assert(x,y); +%!test +%! oo(1,1).f0= 1; +%! oo = setfield(oo,{1,2},'fd',{3},'b', 6); +%! assert (oo(1,2).fd(3).b, 6)
new file mode 100644 --- /dev/null +++ b/scripts/plot/fplot.m @@ -0,0 +1,63 @@ +## Copyright (C) 2005 Paul Kienzle <pkienzle@users.sf.net> +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} fplot (@var{fn}, @var{limits}) +## @deftypefnx {Function File} {} fplot (@var{fn}, @var{limits}, @var{n}) +## Plots a function @var{fn}, within the defined limits. @var{fn} +## an be either a string, a function handle or an inline function. +## The limits of the plot are given by @var{limits} of the form +## @code{[@var{xlo}, @var{xhi}]} or @code{[@var{xlo}, @var{xhi}, +## @var{ylo}, @var{yhi}]}. @var{n} is the number of points to use and +## defaults to 100. +## +## @example +## fplot('cos',[0,2*pi]) +## fplot('[cos(x),sin(x)]',[0,2*pi]) +## @end example +## @end deftypefn + +function fplot (fn, limits, n) + if (nargin < 2 || nargin > 3) + usage ("fplot (fn, limits, n)"); + endif + + if (nargin < 3) + n = 100; + endif + + x = linspace (limits(1), limits(2), n)'; + + if (strcmp (class (fn), "inline function") + || strcmp (class (fn), "function handle")) + y = fn (x); + elseif (all (isalnum (fn))) + y = feval (fn, x); + else + finl = inline (fn); + y = finl (x); + endif + + if (length (limits) > 2) + axis (limits); + endif + + plot (x, y, [";", fn, ";"]); + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/set/intersect.m @@ -0,0 +1,73 @@ +## Copyright (C) 2000 Paul Kienzle +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} intersect(@var{a}, @var{b}) +## @deftypefnx {Function File} {[@var{c}, @var{ia}, @var{ib}] = } intersect (@var{a}, @var{b}) +## +## Return the elements in both @var{a} and @var{b}, sorted in ascending +## order. If @var{a} and @var{b} are both column vectors return a column +## vector, otherwise return a row vector. +## +## Return index vectors @var{ia} and @var{ib} such that @code{a(ia)==c} and +## @code{b(ib)==c}. +## +## @end deftypefn +## @seealso{unique, union, setxor, setdiff, ismember} + +function [c, ia, ib] = intersect (a, b) + if (nargin != 2) + usage ("intersect (a, b)"); + endif + + if (isempty (a) || isempty (b)) + c = ia = ib = []; + else + ## form a and b into sets + [a, ja] = unique (a); + [b, jb] = unique (b); + + c = [ a(:); b(:) ]; + [c, ic] = sort( c ); ## [a(:);b(:)](ic) == c + + ii = find( c(1:end-1) == c(2:end) ); + + c = c(ii); ## The answer + ia = ja(ic(ii)); ## a(ia) == c + ib = jb(ic(ii+1) - length(a)); ## b(ib) == c + + + if ( size (b, 1) == 1 || size (a, 1) == 1 ) + c = c.'; + endif + endif + +endfunction + + +%!# Test the routine for index vectors ia and ib +%!test +%! a = [3 2 4 5 7 6 5 1 0 13 13]; +%! b = [3 5 12 1 1 7]; +%! [c,ia,ib] = intersect(a,b); +%! assert( c,[1 3 5 7]); +%! assert(ia,[8 1 7 5]); +%! assert(ib,[5 1 2 6]); +%! assert(a(ia),c); +%! assert(b(ib),c);
new file mode 100644 --- /dev/null +++ b/scripts/signal/filter2.m @@ -0,0 +1,55 @@ +## Copyright (C) 2001 Paul Kienzle +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## @deftypefn {Function File} {@var{y} =} filter2 (@var{b, @var{x}}) +## @deftypefnx {Function File} {@var{y} =} filter2 (@var{b, @var{x}}, @var{shape}) +## Apply the 2-D FIR filter @var{b} to @var{x}. If the argument +## @var{shape} is specified, return an array of the desired shape. +## Possible values are: +## +## @table @asis +## @item 'full' +## pad @var{x} with zeros on all sides before filtering. +## @item 'same' +## unpadded @var{x} (default) +## @item 'valid' +## trim @var{x} after filtering so edge effects are no included. +## @end table +## +## Note this is just a variation on convolution, with the parameters +## reversed and @var{b} rotated 180 degrees. +## @seealso{conv2} +## @end{deftypefn} + +## Author: Paul Kienzle <pkienzle@users.sf.net> +## 2001-02-08 +## * initial release + +function Y = filter2 (B, X, shape) + + if (nargin < 2 || nargin > 3) + usage ("Y = filter2 (B, X [, 'shape'])"); + endif + if (nargin < 3) + shape = "same"; + endif + + [nr, nc] = size(B); + Y = conv2 (X, B(nr:-1:1, nc:-1:1), shape); +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/signal/ifftshift.m @@ -0,0 +1,72 @@ +## Copyright (C) 1997 by Vincent Cautaerts +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} ifftshift (@var{v}) +## @deftypefnx {Function File} {} ifftshift (@var{v}, @var{dim}) +## Undo the action of the @code{fftshift} function. For even length +## @var{v}, @code{fftshift} is its own inverse, but odd lengths differ +## slightly. +## @end deftypefn + +## Author: Vincent Cautaerts <vincent@comf5.comm.eng.osaka-u.ac.jp> +## Created: July 1997 +## Adapted-By: jwe +## Modified-By: Paul Kienzle, converted from fftshift +## Modified-By: David Bateman, add NDArray capability and option dim arg + +function retval = ifftshift (V, dim) + + retval = 0; + + if (nargin != 1 && nargin != 2) + usage ("usage: ifftshift (X, dim)"); + endif + + if (nargin == 2) + if (!isscalar (dim)) + error ("ifftshift: dimension must be an integer scalar"); + endif + nd = ndims (V); + sz = size (V); + sz2 = floor (sz(dim) / 2); + idx = cell (); + for i=1:nd + idx {i} = 1:sz(i); + endfor + idx {dim} = [sz2+1:sz(dim), 1:sz2]; + retval = V (idx{:}); + else + if (isvector (V)) + x = length (V); + xx = floor (x/2); + retval = V([xx+1:x, 1:xx]); + elseif (ismatrix (V)) + nd = ndims (V); + sz = size (V); + sz2 = floor (sz ./ 2); + idx = cell (); + for i=1:nd + idx{i} = [sz2(i)+1:sz(i), 1:sz2(i)]; + endfor + retval = V (idx{:}); + else + error ("ifftshift: expecting vector or matrix argument"); + endif + endif + +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/specfun/betaln.m @@ -0,0 +1,54 @@ +## Copyright (C) 1998 by Nicol N. Schraudolph +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Mapping Function} {} betaln (@var{a}, @var{b}) +## Return the log of the Beta function, +## @iftex +## @tex +## $$ +## B (a, b) = \log {\Gamma (a) \Gamma (b) \over \Gamma (a + b)}. +## $$ +## @end tex +## @end iftex +## @ifinfo +## +## @example +## betaln (a, b) = gammaln (a) + gammaln (b) - gammaln (a + b) +## @end example +## @end ifinfo +## @seealso{beta, betai, gammaln} +## @end deftypefn + +## Author: Nicol N. Schraudolph <nic@idsia.ch> +## Created: 06 Aug 1998 +## Keywords: log beta special function + +function retval = betaln (a, b) + if (nargin != 2) + usage ("betaln (a, b)"); + endif + + retval = gammaln (a) + gammaln (b) - gammaln (a + b); +endfunction + +%!assert (betaln(3,4),log(beta(3,4)),eps) + +%!error (betaln(1.)) +%!error (betaln(1.,1.,1.))
new file mode 100644 --- /dev/null +++ b/scripts/specfun/factorial.m @@ -0,0 +1,39 @@ +## Copyright (C) 2000 Paul Kienzle +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} factorial (@var{n}) +## Return the factorial of @var{n}. If @var{n} is scalar, this is +## equivalent to @code{prod (1:@var{n})}. If @var{n} is an array, +## the factorial of the elements of the array are returned. +## @end deftypefn + +function x = factorial (n) + if (any (n(:) < 0)) + error ("factorial: n be be a scalar or array of positive integers"); + endif + if (isscalar (n)) + x = prod (2:n); + else + n (find (n < 1)) = 1; + m = max (n(:)); + c = cumprod (1:m); + x = c(floor (n)); + endif +endfunction
new file mode 100644 --- /dev/null +++ b/scripts/strings/strvcat.m @@ -0,0 +1,115 @@ +## Copyright (C) 1996 Kurt Hornik +## +## This file is part of Octave. +## +## Octave 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 2, or (at your option) +## any later version. +## +## Octave 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 Octave; see the file COPYING. If not, write to the Free +## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +## 02110-1301, USA. + +## -*- texinfo -*- +## @deftypefn {Function File} {} strvcat (@var{s_1}, @dots{}, @var{s_n}) +## Return a matrix containing the strings (and cell-strings) +## @var{s_1}, @dots{}, @var{s_n} as +## its rows. Each string is padded with blanks in order to form a valid +## matrix. Unlike @var{str2mat}, empty strings are ignored. +## @seealso{strcat, str2mat} +## @end deftypefn + +## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> +## Adapted-By: jwe +## Modified: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> converted +## str2mat to strvcat. Same function except that strvcat +## ignores empty strings. +## Modified by Alois Schloegl <a.schloegl@ieee.org> Mar 2005 +## added support for cell-strings +## Modifed by David Bateman for NDArrays + +function retval = strvcat (varargin) + + if (nargin == 0) + usage ("strvcat (s1, ...)"); + endif + + nr = zeros (nargin, 1); + nc = zeros (nargin, 1); + K = 0; + nd = ndims (varargin {1}); + sz = size (varargin {1}); + for k = 1 : nargin + s = varargin{k}; + if (iscell (s)) + for k1 = 1:length(s) + K = K+1; + nr(K) = size (s{k1}, 1); + nc(K) = size (s{k1}, 2); + if (ndims (s{k1}) != nd) + error ("strvcat: dimension mismatch"); + else + if (any (sz(3:nd) != size (s{k1}) (3:nd))) + error ("strvcat: dimension mismatch"); + endif + endif + endfor + else + K = K + 1; + nr(K) = size (s, 1); + nc(K) = size (s, 2); + if (ndims (s) != nd) + error ("strvcat: dimension mismatch"); + else + if (any (sz(3:nd) != size (s) (3:nd))) + error ("strvcat: dimension mismatch"); + endif + endif + endif + endfor + + sz(1) = sum (nr); + sz(2) = max (nc); + retval = char (ones (sz) * toascii (" ")); + + idx = cell(nd,1); + for k = 3 : nd; + idx {k} = sz {k}; + endfor + + K = 0; + row_offset = 0; + for k = 1 : nargin + s = varargin{k}; + if (iscell (s)) + for k1 = 1:length(s) + K = K + 1; + idx{1} = [row_offset + 1 : row_offset + nr(k)]; + idx{2} = [1 : nc(K)]; + retval(idx{:}) = char(s{k1}); + row_offset = row_offset + size (s{k1}, 1); + endfor + else + K = K + 1; + if (nc(K) > 0) + retval ((row_offset + 1) : (row_offset + nr(K)), 1:nc(K)) = char(s); + endif + row_offset = row_offset + nr(K); + endif + endfor + +endfunction + +%!shared s1,s2,s3,s4,c +%! s1 = "quick"; s2 = "brown"; s3 = "fox"; s4 = ["quick";"brown";"fox "]; +%! c{1} = s1; c{2} = s2; c{3} = s3; +%!assert(strvcat(s1,s2,s3),s4) +%!assert(strvcat(c),s4) +%!assert(strvcat(s4,s4),[s4;s4]);