GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
__pchip_deriv__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2002-2013 Kai Habel
4 Copyright (C) 2008-2009 Jaroslav Hajek
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include "defun.h"
29 #include "error.h"
30 #include "gripes.h"
31 #include "oct-obj.h"
32 #include "utils.h"
33 #include "f77-fcn.h"
34 
35 extern "C"
36 {
37  F77_RET_T
38  F77_FUNC (dpchim, DPCHIM) (const octave_idx_type& n, const double *x,
39  const double *f, double *d,
40  const octave_idx_type &incfd,
42 
43  F77_RET_T
44  F77_FUNC (pchim, PCHIM) (const octave_idx_type& n, const float *x,
45  const float *f, float *d,
46  const octave_idx_type& incfd,
48 }
49 
50 // Wrapper for SLATEC/PCHIP function DPCHIM to calculate the derivates
51 // for piecewise polynomials.
52 
53 DEFUN (__pchip_deriv__, args, ,
54  "-*- texinfo -*-\n\
55 @deftypefn {Built-in Function} {} __pchip_deriv__ (@var{x}, @var{y}, @var{dim})\n\
56 Undocumented internal function.\n\
57 @end deftypefn")
58 {
59  octave_value retval;
60  const int nargin = args.length ();
61 
62  bool rows = (nargin == 3 && args (2).uint_value () == 2);
63 
64  if (nargin >= 2)
65  {
66  if (args(0).is_single_type () || args(1).is_single_type ())
67  {
68  FloatColumnVector xvec (args(0).float_vector_value ());
69  FloatMatrix ymat (args(1).float_matrix_value ());
70 
71  octave_idx_type nx = xvec.length ();
72 
73  if (nx < 2)
74  {
75  error ("__pchip_deriv__: X must be at least of length 2");
76  return retval;
77  }
78 
79  octave_idx_type nyr = ymat.rows ();
80  octave_idx_type nyc = ymat.columns ();
81 
82  if (nx != (rows ? nyc : nyr))
83  {
84  error ("__pchip_deriv__: X and Y dimension mismatch");
85  return retval;
86  }
87 
88  const float *yvec = ymat.data ();
89  FloatMatrix dmat (nyr, nyc);
90  float *dvec = dmat.fortran_vec ();
91 
93  const octave_idx_type incfd = rows ? nyr : 1;
94  const octave_idx_type inc = rows ? 1 : nyr;
95 
96  for (octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
97  {
98  F77_FUNC (pchim, PCHIM) (nx, xvec.data (),
99  yvec, dvec, incfd, &ierr);
100 
101  yvec += inc;
102  dvec += inc;
103 
104  if (ierr < 0)
105  {
106  error ("PCHIM: error: %i\n", ierr);
107  return retval;
108  }
109  }
110 
111  retval = dmat;
112  }
113  else
114  {
115  ColumnVector xvec (args(0).vector_value ());
116  Matrix ymat (args(1).matrix_value ());
117 
118  octave_idx_type nx = xvec.length ();
119 
120  if (nx < 2)
121  {
122  error ("__pchip_deriv__: X must be at least of length 2");
123  return retval;
124  }
125 
126  octave_idx_type nyr = ymat.rows ();
127  octave_idx_type nyc = ymat.columns ();
128 
129  if (nx != (rows ? nyc : nyr))
130  {
131  error ("__pchip_deriv__: X and Y dimension mismatch");
132  return retval;
133  }
134 
135  const double *yvec = ymat.data ();
136  Matrix dmat (nyr, nyc);
137  double *dvec = dmat.fortran_vec ();
138 
140  const octave_idx_type incfd = rows ? nyr : 1;
141  const octave_idx_type inc = rows ? 1 : nyr;
142 
143  for (octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
144  {
145  F77_FUNC (dpchim, DPCHIM) (nx, xvec.data (),
146  yvec, dvec, incfd, &ierr);
147 
148  yvec += inc;
149  dvec += inc;
150 
151  if (ierr < 0)
152  {
153  error ("DPCHIM: error: %i\n", ierr);
154  return retval;
155  }
156  }
157 
158  retval = dmat;
159  }
160  }
161 
162  return retval;
163 }
164 
165 /*
166 ## No test needed for internal helper function.
167 %!assert (1)
168 */