Mercurial > hg > octave-shane
changeset 10386:84e226380769
remove old convn
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 02 Mar 2010 14:49:00 +0100 |
parents | 56116dceb1e0 |
children | eb9b2674501e |
files | scripts/ChangeLog scripts/polynomial/convn.m scripts/polynomial/module.mk src/ChangeLog src/DLD-FUNCTIONS/__convn__.cc src/DLD-FUNCTIONS/module-files |
diffstat | 6 files changed, 10 insertions(+), 370 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2010-03-02 Jaroslav Hajek <highegg@gmail.com> + + * polynomial/convn.m: Remove. + * polynomial/module.mk: Update. + 2010-02-28 John W. Eaton <jwe@octave.org> * set/unique.m: If the argument is sparse and we are not
deleted file mode 100644 --- a/scripts/polynomial/convn.m +++ /dev/null @@ -1,128 +0,0 @@ -## Copyright (C) 2008, 2009 Soren Hauberg -## -## 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 3 of the License, 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, see -## <http://www.gnu.org/licenses/>. - -## -*- texinfo -*- -## @deftypefn {Function File} {@var{c} =} convn (@var{a}, @var{b}, @var{shape}) -## @math{N}-dimensional convolution of matrices @var{a} and @var{b}. -## -## The size of the output is determined by the @var{shape} argument. -## This can be any of the following character strings: -## -## @table @asis -## @item "full" -## The full convolution result is returned. The size out of the output is -## @code{size (@var{a}) + size (@var{b})-1}. This is the default behavior. -## @item "same" -## The central part of the convolution result is returned. The size out of the -## output is the same as @var{a}. -## @item "valid" -## The valid part of the convolution is returned. The size of the result is -## @code{max (size (@var{a}) - size (@var{b})+1, 0)}. -## @end table -## -## @seealso{conv, conv2} -## @end deftypefn - -function c = convn (a, b, shape = "full") - - if (nargin < 2) - error ("convn: not enough input arguments"); - endif - - if (!ismatrix (a) || !ismatrix (b) || ndims (a) != ndims (b)) - error ("convn: first and second arguments must be matrices of the same dimensionality"); - endif - - if (!ischar (shape)) - error ("convn: third input argument must be a string"); - endif - - if (!any (strcmpi (shape, {"full", "same", "valid"}))) - error ("convn: invalid shape argument: '%s'", shape); - endif - - ## Should we swap 'a' and 'b'? - ## FIXME -- should we also swap in any of the non-full cases? - if (numel (b) > numel (a) && strcmpi (shape, "full")) - tmp = a; - a = b; - b = tmp; - endif - - ## Pad A. - switch (lower (shape)) - case "full" - a = pad (a, size (b)-1, size (b)-1); - case "same" - a = pad (a, floor ((size (b)-1)/2), ceil ((size (b)-1)/2)); - endswitch - - ## Perform convolution. - c = __convn__ (a, b); - -endfunction - -## Helper function that performs the padding. -function a = pad (a, left, right) - cl = class (a); - for dim = 1:ndims (a) - l = r = size (a); - l(dim) = left(dim); - r(dim) = right(dim); - a = cat (dim, zeros (l, cl), a, zeros (r, cl)); - endfor -endfunction - -%!test -%! ## Compare to conv2 -%! a = rand (100); -%! b = ones (3); -%! c2 = conv2 (a, b, "full"); -%! cn = convn (a, b, "full"); -%! assert (max (abs (cn(:)-c2(:))), 0, 100*eps); - -%!test -%! ## Compare to conv2 -%! a = rand (100); -%! b = ones (3); -%! c2 = conv2 (a, b, "same"); -%! cn = convn (a, b, "same"); -%! assert (max (abs (cn(:)-c2(:))), 0, 100*eps); - -%!test -%! ## Compare to conv2 -%! a = rand (100); -%! b = ones (3); -%! c2 = conv2 (a, b, "valid"); -%! cn = convn (a, b, "valid"); -%! assert (max (abs (cn(:)-c2(:))), 0, 100*eps); - -%!test -%! ## Real data -%! a = ones (10,10,10); -%! b = ones (3,3,3); -%! c = convn (a, b, "valid"); -%! assert (all (c == numel (b))); - -%!test -%! ## Complex data -%! a = complex(ones (10,10,10), ones(10,10,10)); -%! b = complex(ones (3,3,3), ones(3,3,3)); -%! c = convn (a, b, "valid"); -%! assert (all (c == 2*i*numel (b))); -
--- a/scripts/polynomial/module.mk +++ b/scripts/polynomial/module.mk @@ -3,7 +3,6 @@ polynomial_FCN_FILES = \ polynomial/compan.m \ polynomial/conv.m \ - polynomial/convn.m \ polynomial/deconv.m \ polynomial/mkpp.m \ polynomial/mpoles.m \
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2010-03-02 Jaroslav Hajek <highegg@gmail.com> + + * DLD-FUNCTIONS/__convn__.cc: Remove. + * DLD-FUNCTIONS/module-files: Update. + 2010-03-02 Jaroslav Hajek <highegg@gmail.com> * DLD-FUNCTIONS/conv.cc (Fconv2): Rewrite using convn from liboctave.
deleted file mode 100644 --- a/src/DLD-FUNCTIONS/__convn__.cc +++ /dev/null @@ -1,240 +0,0 @@ -/* - -Copyright (C) 2008 Soren Hauberg - -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 3 of the License, 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, see -<http://www.gnu.org/licenses/>. - -*/ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <algorithm> - -#include "dNDArray.h" -#include "CNDArray.h" - -#include "defun-dld.h" - -template <class T1, class T2> -class -octave_convn_traits -{ -public: - // The return type for a T1 by T2 convn operation. - typedef T1 TR; -}; - -#define OCTAVE_CONVN_TRAIT(T1, T2, T3) \ - template<> \ - class octave_convn_traits <T1, T2> \ - { \ - public: \ - typedef T3 TR; \ - } - -OCTAVE_CONVN_TRAIT (NDArray, NDArray, NDArray); -OCTAVE_CONVN_TRAIT (ComplexNDArray, NDArray, ComplexNDArray); -OCTAVE_CONVN_TRAIT (NDArray, ComplexNDArray, ComplexNDArray); -OCTAVE_CONVN_TRAIT (ComplexNDArray, ComplexNDArray, ComplexNDArray); - -OCTAVE_CONVN_TRAIT (FloatNDArray, FloatNDArray, FloatNDArray); -OCTAVE_CONVN_TRAIT (FloatComplexNDArray, FloatNDArray, FloatComplexNDArray); -OCTAVE_CONVN_TRAIT (FloatNDArray, FloatComplexNDArray, FloatComplexNDArray); -OCTAVE_CONVN_TRAIT (FloatComplexNDArray, FloatComplexNDArray, FloatComplexNDArray); - -// FIXME -- this function should maybe be available in liboctave? -template <class MTa, class MTb> -octave_value -convn (const MTa& a, const MTb& b) -{ - octave_value retval; - - // Get sizes - const octave_idx_type ndims = a.ndims (); - const octave_idx_type b_numel = b.numel (); - - const dim_vector a_size = a.dims (); - const dim_vector b_size = b.dims (); - - if (ndims != b.ndims ()) - { - error ("__convn__: first and second argument must have same dimensionality"); - return retval; - } - - // Allocate output - dim_vector out_size (a_size); - for (octave_idx_type n = 0; n < ndims; n++) - out_size(n) = std::max (a_size(n) - b_size(n) + 1, - static_cast<octave_idx_type> (0)); - - typedef typename octave_convn_traits<MTa, MTb>::TR MTout; - - MTout out (out_size); - - const octave_idx_type out_numel = out.numel (); - - // Iterate over every element of 'out'. - dim_vector idx_dim (ndims, 1); - - Array<octave_idx_type> a_idx (idx_dim); - Array<octave_idx_type> b_idx (idx_dim); - Array<octave_idx_type> out_idx (idx_dim, 0); - - for (octave_idx_type i = 0; i < out_numel; i++) - { - OCTAVE_QUIT; - - // For each neighbour - typename MTout::element_type sum = 0; - - for (octave_idx_type n = 0; n < ndims; n++) - b_idx(n) = 0; - - for (octave_idx_type j = 0; j < b_numel; j++) - { - for (octave_idx_type n = 0; n < ndims; n++) - a_idx(n) = out_idx(n) + (b_size(n) - 1 - b_idx(n)); - - sum += a(a_idx) * b(b_idx); - - b.increment_index (b_idx, b_size); - } - - // Compute filter result - out(out_idx) = sum; - - // Prepare for next iteration - out.increment_index (out_idx, out_size); - } - - return out; -} - -DEFUN_DLD (__convn__, args, , - "-*- texinfo -*-\n\ -@deftypefn {Loadable Function} {} __convn__(@var{a}, @var{b})\n\ -Undocumented internal function.\n\ -@end deftypefn\n\ -") -{ - octave_value retval; - - if (args.length () == 2) - { - if (args(0).is_single_type() || args(1).is_single_type()) - { - if (args(0).is_real_type ()) - { - if (args(1).is_real_type ()) - { - const FloatNDArray a = args (0).float_array_value (); - const FloatNDArray b = args (1).float_array_value (); - - if (! error_state) - retval = convn (a, b); - } - else if (args(1).is_complex_type ()) - { - const FloatNDArray a = args (0).float_array_value (); - const FloatComplexNDArray b = args (1).float_complex_array_value (); - - if (! error_state) - retval = convn (a, b); - } - else - error ("__convn__: invalid call"); - } - else if (args(0).is_complex_type ()) - { - if (args(1).is_complex_type ()) - { - const FloatComplexNDArray a = args (0).float_complex_array_value (); - const FloatComplexNDArray b = args (1).float_complex_array_value (); - - if (! error_state) - retval = convn (a, b); - } - else if (args(1).is_real_type ()) - { - const FloatComplexNDArray a = args (0).float_complex_array_value (); - const FloatNDArray b = args (1).float_array_value (); - - if (! error_state) - retval = convn (a, b); - } - else - error ("__convn__: invalid call"); - } - else - error ("__convn__: invalid call"); - } - else - { - if (args(0).is_real_type ()) - { - if (args(1).is_real_type ()) - { - const NDArray a = args (0).array_value (); - const NDArray b = args (1).array_value (); - - if (! error_state) - retval = convn (a, b); - } - else if (args(1).is_complex_type ()) - { - const NDArray a = args (0).array_value (); - const ComplexNDArray b = args (1).complex_array_value (); - - if (! error_state) - retval = convn (a, b); - } - else - error ("__convn__: invalid call"); - } - else if (args(0).is_complex_type ()) - { - if (args(1).is_complex_type ()) - { - const ComplexNDArray a = args (0).complex_array_value (); - const ComplexNDArray b = args (1).complex_array_value (); - - if (! error_state) - retval = convn (a, b); - } - else if (args(1).is_real_type ()) - { - const ComplexNDArray a = args (0).complex_array_value (); - const NDArray b = args (1).array_value (); - - if (! error_state) - retval = convn (a, b); - } - else - error ("__convn__: invalid call"); - } - else - error ("__convn__: invalid call"); - } - } - else - print_usage (); - - return retval; -}