GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "lo-slatec-proto.h"
31 
32 #include "defun.h"
33 #include "error.h"
34 #include "errwarn.h"
35 #include "ovl.h"
36 #include "utils.h"
37 #include "f77-fcn.h"
38 
39 // Wrapper for SLATEC/PCHIP function DPCHIM to calculate the derivates
40 // for piecewise polynomials.
41 
42 DEFUN (__pchip_deriv__, args, ,
43  doc: /* -*- texinfo -*-
44 @deftypefn {} {} __pchip_deriv__ (@var{x}, @var{y}, @var{dim})
45 Undocumented internal function.
46 @end deftypefn */)
47 {
49  int nargin = args.length ();
50 
51  bool rows = (nargin == 3 && args(2).uint_value () == 2);
52 
53  if (nargin >= 2)
54  {
55  if (args(0).is_single_type () || args(1).is_single_type ())
56  {
57  FloatColumnVector xvec (args(0).float_vector_value ());
58  FloatMatrix ymat (args(1).float_matrix_value ());
59 
60  F77_INT nx = octave::to_f77_int (xvec.numel ());
61 
62  if (nx < 2)
63  error ("__pchip_deriv__: X must be at least of length 2");
64 
65  octave_idx_type nyr = ymat.rows ();
66  octave_idx_type nyc = ymat.columns ();
67 
68  if (nx != (rows ? nyc : nyr))
69  error ("__pchip_deriv__: X and Y dimension mismatch");
70 
71  FloatMatrix dmat (nyr, nyc);
72 
73  F77_INT ierr;
74  const F77_INT incfd = (rows ? octave::to_f77_int (nyr) : 1);
75  volatile const octave_idx_type inc = (rows ? 1 : nyr);
76  volatile octave_idx_type k = 0;
77 
78  for (volatile octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
79  {
80  F77_XFCN (pchim, PCHIM, (nx, xvec.data (),
81  ymat.data () + k * inc,
82  dmat.fortran_vec () + k * inc,
83  incfd, ierr));
84 
85  k++;
86 
87  if (ierr < 0)
88  error ("__pchip_deriv__: PCHIM failed with ierr = %i", ierr);
89  }
90 
91  retval = dmat;
92  }
93  else
94  {
95  ColumnVector xvec (args(0).vector_value ());
96  Matrix ymat (args(1).matrix_value ());
97 
98  F77_INT nx = octave::to_f77_int (xvec.numel ());
99 
100  if (nx < 2)
101  error ("__pchip_deriv__: X must be at least of length 2");
102 
103  octave_idx_type nyr = ymat.rows ();
104  octave_idx_type nyc = ymat.columns ();
105 
106  if (nx != (rows ? nyc : nyr))
107  error ("__pchip_deriv__: X and Y dimension mismatch");
108 
109  Matrix dmat (nyr, nyc);
110 
111  F77_INT ierr;
112  const F77_INT incfd = (rows ? octave::to_f77_int (nyr) : 1);
113  volatile const octave_idx_type inc = (rows ? 1 : nyr);
114  volatile octave_idx_type k = 0;
115 
116  for (volatile octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
117  {
118  F77_XFCN (dpchim, DPCHIM, (nx, xvec.data (),
119  ymat.data () + k * inc,
120  dmat.fortran_vec () + k * inc,
121  incfd, ierr));
122  k++;
123 
124  if (ierr < 0)
125  error ("__pchip_deriv__: DPCHIM failed with ierr = %i", ierr);
126  }
127 
128  retval = dmat;
129  }
130  }
131 
132  return retval;
133 }
134 
135 /*
136 ## No test needed for internal helper function.
137 %!assert (1)
138 */
octave_idx_type columns(void) const
Definition: Array.h:424
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:377
const T * data(void) const
Size of the specified dimension.
Definition: Array.h:581
octave_idx_type rows(void) const
Definition: Array.h:415
const T * fortran_vec(void) const
Size of the specified dimension.
Definition: Array.h:583
Definition: dMatrix.h:42
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
subroutine dpchim(N, X, F, D, INCFD, IERR)
Definition: dpchim.f:3
void error(const char *fmt,...)
Definition: error.cc:968
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:44
octave_f77_int_type F77_INT
Definition: f77-fcn.h:305
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE const F77_INT F77_INT & ierr
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE const F77_INT & incfd
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811
subroutine pchim(N, X, F, D, INCFD, IERR)
Definition: pchim.f:3