__pchip_deriv__.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2002-2012 Kai Habel
00004 Copyright (C) 2008-2009 Jaroslav Hajek
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include "defun-dld.h"
00029 #include "error.h"
00030 #include "gripes.h"
00031 #include "oct-obj.h"
00032 #include "utils.h"
00033 #include "f77-fcn.h"
00034 
00035 extern "C"
00036 {
00037   F77_RET_T
00038   F77_FUNC (dpchim, DPCHIM) (const octave_idx_type& n, const double *x,
00039                              const double *f, double *d,
00040                              const octave_idx_type &incfd,
00041                              octave_idx_type *ierr);
00042 
00043   F77_RET_T
00044   F77_FUNC (pchim, PCHIM) (const octave_idx_type& n, const float *x,
00045                            const float *f, float *d,
00046                            const octave_idx_type& incfd,
00047                            octave_idx_type *ierr);
00048 }
00049 
00050 // Wrapper for SLATEC/PCHIP function DPCHIM to calculate the derivates
00051 // for piecewise polynomials.
00052 
00053 DEFUN_DLD (__pchip_deriv__, args, ,
00054   "-*- texinfo -*-\n\
00055 @deftypefn {Loadable Function} {} __pchip_deriv__ (@var{x}, @var{y}, @var{dim})\n\
00056 Undocumented internal function.\n\
00057 @end deftypefn")
00058 {
00059   octave_value retval;
00060   const int nargin = args.length ();
00061 
00062   bool rows = (nargin == 3 && args (2).uint_value() == 2);
00063 
00064   if (nargin >= 2)
00065     {
00066       if (args(0).is_single_type () || args(1).is_single_type ())
00067         {
00068           FloatColumnVector xvec (args(0).float_vector_value ());
00069           FloatMatrix ymat (args(1).float_matrix_value ());
00070 
00071           octave_idx_type nx = xvec.length ();
00072 
00073           if (nx < 2)
00074             {
00075               error ("__pchip_deriv__: X must be at least of length 2");
00076               return retval;
00077             }
00078 
00079           octave_idx_type nyr = ymat.rows ();
00080           octave_idx_type nyc = ymat.columns ();
00081 
00082           if (nx != (rows ? nyc : nyr))
00083             {
00084               error ("__pchip_deriv__: X and Y dimension mismatch");
00085               return retval;
00086             }
00087 
00088           const float *yvec = ymat.data ();
00089           FloatMatrix dmat (nyr, nyc);
00090           float *dvec = dmat.fortran_vec ();
00091 
00092           octave_idx_type ierr;
00093           const octave_idx_type incfd = rows ? nyr : 1;
00094           const octave_idx_type inc = rows ? 1 : nyr;
00095 
00096           for (octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
00097             {
00098               F77_FUNC (pchim, PCHIM) (nx, xvec.data (),
00099                                        yvec, dvec, incfd, &ierr);
00100 
00101               yvec += inc;
00102               dvec += inc;
00103 
00104               if (ierr < 0)
00105                 {
00106                   error ("PCHIM: error: %i\n", ierr);
00107                   return retval;
00108                 }
00109             }
00110 
00111           retval = dmat;
00112         }
00113       else
00114         {
00115           ColumnVector xvec (args(0).vector_value ());
00116           Matrix ymat (args(1).matrix_value ());
00117 
00118           octave_idx_type nx = xvec.length ();
00119 
00120           if (nx < 2)
00121             {
00122               error ("__pchip_deriv__: X must be at least of length 2");
00123               return retval;
00124             }
00125 
00126           octave_idx_type nyr = ymat.rows ();
00127           octave_idx_type nyc = ymat.columns ();
00128 
00129           if (nx != (rows ? nyc : nyr))
00130             {
00131               error ("__pchip_deriv__: X and Y dimension mismatch");
00132               return retval;
00133             }
00134 
00135           const double *yvec = ymat.data ();
00136           Matrix dmat (nyr, nyc);
00137           double *dvec = dmat.fortran_vec ();
00138 
00139           octave_idx_type ierr;
00140           const octave_idx_type incfd = rows ? nyr : 1;
00141           const octave_idx_type inc = rows ? 1 : nyr;
00142 
00143           for (octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
00144             {
00145               F77_FUNC (dpchim, DPCHIM) (nx, xvec.data (),
00146                                          yvec, dvec, incfd, &ierr);
00147 
00148               yvec += inc;
00149               dvec += inc;
00150 
00151               if (ierr < 0)
00152                 {
00153                   error ("DPCHIM: error: %i\n", ierr);
00154                   return retval;
00155                 }
00156             }
00157 
00158           retval = dmat;
00159         }
00160     }
00161 
00162   return retval;
00163 }
00164 
00165 /*
00166 
00167 ## No test needed for internal helper function.
00168 %!assert (1)
00169 
00170 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines