GNU Octave 7.1.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-2022 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
39OCTAVE_NAMESPACE_BEGIN
40
41// Wrapper for SLATEC/PCHIP function DPCHIM to calculate the derivates
42// for piecewise polynomials.
43
44DEFUN (__pchip_deriv__, args, ,
45 doc: /* -*- texinfo -*-
46@deftypefn {} {} __pchip_deriv__ (@var{x}, @var{y}, @var{dim})
47Undocumented internal function.
48@end deftypefn */)
49{
50 octave_value retval;
51 int nargin = args.length ();
52
53 bool rows = (nargin == 3 && args(2).uint_value () == 2);
54
55 if (nargin >= 2)
56 {
57 if (args(0).is_single_type () || args(1).is_single_type ())
58 {
59 FloatColumnVector xvec (args(0).float_vector_value ());
60 FloatMatrix ymat (args(1).float_matrix_value ());
61
62 F77_INT nx = to_f77_int (xvec.numel ());
63
64 if (nx < 2)
65 error ("__pchip_deriv__: X must be at least of length 2");
66
67 octave_idx_type nyr = ymat.rows ();
68 octave_idx_type nyc = ymat.columns ();
69
70 if (nx != (rows ? nyc : nyr))
71 error ("__pchip_deriv__: X and Y dimension mismatch");
72
73 FloatMatrix dmat (nyr, nyc);
74
76 const F77_INT incfd = (rows ? to_f77_int (nyr) : 1);
77 volatile const octave_idx_type inc = (rows ? 1 : nyr);
78 volatile octave_idx_type k = 0;
79
80 for (volatile octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
81 {
82 F77_XFCN (pchim, PCHIM, (nx, xvec.data (),
83 ymat.data () + k * inc,
84 dmat.fortran_vec () + k * inc,
85 incfd, ierr));
86
87 k++;
88
89 if (ierr < 0)
90 error ("__pchip_deriv__: PCHIM failed with ierr = %"
91 OCTAVE_F77_INT_TYPE_FORMAT, ierr);
92 }
93
94 retval = dmat;
95 }
96 else
97 {
98 ColumnVector xvec (args(0).vector_value ());
99 Matrix ymat (args(1).matrix_value ());
100
101 F77_INT nx = to_f77_int (xvec.numel ());
102
103 if (nx < 2)
104 error ("__pchip_deriv__: X must be at least of length 2");
105
106 octave_idx_type nyr = ymat.rows ();
107 octave_idx_type nyc = ymat.columns ();
108
109 if (nx != (rows ? nyc : nyr))
110 error ("__pchip_deriv__: X and Y dimension mismatch");
111
112 Matrix dmat (nyr, nyc);
113
115 const F77_INT incfd = (rows ? to_f77_int (nyr) : 1);
116 volatile const octave_idx_type inc = (rows ? 1 : nyr);
117 volatile octave_idx_type k = 0;
118
119 for (volatile octave_idx_type i = (rows ? nyr : nyc); i > 0; i--)
120 {
121 F77_XFCN (dpchim, DPCHIM, (nx, xvec.data (),
122 ymat.data () + k * inc,
123 dmat.fortran_vec () + k * inc,
124 incfd, ierr));
125 k++;
126
127 if (ierr < 0)
128 error ("__pchip_deriv__: DPCHIM failed with ierr = %"
129 OCTAVE_F77_INT_TYPE_FORMAT, ierr);
130 }
131
132 retval = dmat;
133 }
134 }
135
136 return retval;
137}
138
139/*
140## No test needed for internal helper function.
141%!assert (1)
142*/
143
144OCTAVE_NAMESPACE_END
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
octave_idx_type rows(void) const
Definition: Array.h:449
octave_idx_type columns(void) const
Definition: Array.h:458
const T * data(void) const
Size of the specified dimension.
Definition: Array.h:616
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
Definition: Array.cc:1744
Definition: dMatrix.h:42
OCTINTERP_API octave_idx_type length(void) const
#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:980
#define F77_XFCN(f, F, args)
Definition: f77-fcn.h:45
octave_f77_int_type F77_INT
Definition: f77-fcn.h:306
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
subroutine pchim(N, X, F, D, INCFD, IERR)
Definition: pchim.f:3