GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__pchip_deriv__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2002-2018 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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "lo-slatec-proto.h"
29 
30 #include "defun.h"
31 #include "error.h"
32 #include "errwarn.h"
33 #include "ovl.h"
34 #include "utils.h"
35 #include "f77-fcn.h"
36 
37 // Wrapper for SLATEC/PCHIP function DPCHIM to calculate the derivates
38 // for piecewise polynomials.
39 
40 DEFUN (__pchip_deriv__, args, ,
41  doc: /* -*- texinfo -*-
42 @deftypefn {} {} __pchip_deriv__ (@var{x}, @var{y}, @var{dim})
43 Undocumented internal function.
44 @end deftypefn */)
45 {
47  int nargin = args.length ();
48 
49  bool rows = (nargin == 3 && args(2).uint_value () == 2);
50 
51  if (nargin >= 2)
52  {
53  if (args(0).is_single_type () || args(1).is_single_type ())
54  {
55  FloatColumnVector xvec (args(0).float_vector_value ());
56  FloatMatrix ymat (args(1).float_matrix_value ());
57 
58  F77_INT nx = octave::to_f77_int (xvec.numel ());
59 
60  if (nx < 2)
61  error ("__pchip_deriv__: X must be at least of length 2");
62 
63  octave_idx_type nyr = ymat.rows ();
64  octave_idx_type nyc = ymat.columns ();
65 
66  if (nx != (rows ? nyc : nyr))
67  error ("__pchip_deriv__: X and Y dimension mismatch");
68 
69  FloatMatrix dmat (nyr, nyc);
70 
71  F77_INT ierr;
72  const F77_INT incfd = (rows ? octave::to_f77_int (nyr) : 1);
73  volatile const octave_idx_type inc = (rows ? 1 : nyr);
74  volatile octave_idx_type k = 0;
75 
76  for (volatile octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
77  {
78  F77_XFCN (pchim, PCHIM, (nx, xvec.data (),
79  ymat.data () + k * inc,
80  dmat.fortran_vec () + k * inc,
81  incfd, ierr));
82 
83  k++;
84 
85  if (ierr < 0)
86  error ("__pchip_deriv__: PCHIM failed with ierr = %i", ierr);
87  }
88 
89  retval = dmat;
90  }
91  else
92  {
93  ColumnVector xvec (args(0).vector_value ());
94  Matrix ymat (args(1).matrix_value ());
95 
96  F77_INT nx = octave::to_f77_int (xvec.numel ());
97 
98  if (nx < 2)
99  error ("__pchip_deriv__: X must be at least of length 2");
100 
101  octave_idx_type nyr = ymat.rows ();
102  octave_idx_type nyc = ymat.columns ();
103 
104  if (nx != (rows ? nyc : nyr))
105  error ("__pchip_deriv__: X and Y dimension mismatch");
106 
107  Matrix dmat (nyr, nyc);
108 
109  F77_INT ierr;
110  const F77_INT incfd = (rows ? octave::to_f77_int (nyr) : 1);
111  volatile const octave_idx_type inc = (rows ? 1 : nyr);
112  volatile octave_idx_type k = 0;
113 
114  for (volatile octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
115  {
116  F77_XFCN (dpchim, DPCHIM, (nx, xvec.data (),
117  ymat.data () + k * inc,
118  dmat.fortran_vec () + k * inc,
119  incfd, ierr));
120  k++;
121 
122  if (ierr < 0)
123  error ("__pchip_deriv__: DPCHIM failed with ierr = %i", ierr);
124  }
125 
126  retval = dmat;
127  }
128  }
129 
130  return retval;
131 }
132 
133 /*
134 ## No test needed for internal helper function.
135 %!assert (1)
136 */
octave_idx_type rows(void) const
Definition: Array.h:404
const T * data(void) const
Definition: Array.h:582
for large enough k
Definition: lu.cc:617
const T * fortran_vec(void) const
Definition: Array.h:584
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
void error(const char *fmt,...)
Definition: error.cc:578
octave_idx_type columns(void) const
Definition: Array.h:413
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:41
subroutine DPCHIM(N, X, F, D, INCFD, IERR)
Definition: dpchim.f:3
octave_value retval
Definition: data.cc:6246
subroutine PCHIM(N, X, F, D, INCFD, IERR)
Definition: pchim.f:3
Definition: dMatrix.h:36
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE const F77_INT & incfd
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
args.length() nargin
Definition: file-io.cc:589
for i
Definition: data.cc:5264
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
octave_idx_type length(void) const
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE const F77_INT F77_INT & ierr