GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
colloc.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 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 <algorithm>
31 #include <string>
32 
33 #include "CollocWt.h"
34 #include "lo-mappers.h"
35 
36 #include "defun.h"
37 #include "error.h"
38 #include "ovl.h"
39 #include "utils.h"
40 
42 
43 DEFUN (colloc, args, ,
44  doc: /* -*- texinfo -*-
45 @deftypefn {} {[@var{r}, @var{amat}, @var{bmat}, @var{q}] =} colloc (@var{n}, "left", "right")
46 Compute derivative and integral weight matrices for orthogonal collocation.
47 
48 Reference: @nospell{J. Villadsen}, @nospell{M. L. Michelsen},
49 @cite{Solution of Differential Equation Models by Polynomial Approximation}.
50 @end deftypefn */)
51 {
52  int nargin = args.length ();
53 
54  if (nargin < 1 || nargin > 3)
55  print_usage ();
56 
57  if (! args(0).is_scalar_type ())
58  error ("colloc: N must be a scalar");
59 
60  double tmp = args(0).double_value ();
61  if (math::isnan (tmp))
62  error ("colloc: N cannot be NaN");
63 
64  octave_idx_type ncol = math::nint_big (tmp);
65  if (ncol < 0)
66  error ("colloc: N must be positive");
67 
68  octave_idx_type ntot = ncol;
69  octave_idx_type left = 0;
70  octave_idx_type right = 0;
71 
72  for (int i = 1; i < nargin; i++)
73  {
74  std::string s = args(i).xstring_value ("colloc: optional arguments must be strings");
75 
76  std::transform (s.begin (), s.end (), s.begin (), ::tolower);
77 
78  if (s == "r" || s == "right")
79  right = 1;
80  else if (s == "l" || s == "left")
81  left = 1;
82  else
83  error (R"(colloc: string argument must be "left" or "right")");
84  }
85 
86  ntot += left + right;
87  if (ntot < 1)
88  error (R"("colloc: the total number of roots (N + "left" + "right") must be >= 1)");
89 
90  CollocWt wts (ncol, left, right);
91 
92  ColumnVector r = wts.roots ();
93  Matrix A = wts.first ();
94  Matrix B = wts.second ();
95  ColumnVector q = wts.quad_weights ();
96 
97  return ovl (r, A, B, q);
98 }
99 
100 /*
101 
102 %!assert (colloc (1), 0.5)
103 %!assert (colloc (1, "left"), [0; 0.5])
104 %!assert (colloc (1, "right"), [0.5; 1])
105 %!assert (colloc (1, "left", "right"), [0; 0.5; 1])
106 
107 ## Test input validation
108 %!error colloc ()
109 %!error colloc (1,2,3,4)
110 %!error <N must be a scalar> colloc (ones (2,2))
111 %!error <N cannot be NaN> colloc (NaN)
112 %!error <N must be positive> colloc (-1)
113 %!error <optional arguments must be strings> colloc (1, 1)
114 %!error <string argument must be "left" or "right"> colloc (1, "foobar")
115 %!error <total number of roots .* must be .= 1> colloc (0)
116 
117 */
118 
119 OCTAVE_END_NAMESPACE(octave)
ColumnVector roots()
Definition: CollocWt.h:148
Matrix first()
Definition: CollocWt.h:166
ColumnVector quad_weights()
Definition: CollocWt.h:164
Matrix second()
Definition: CollocWt.h:174
Definition: dMatrix.h:42
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void() error(const char *fmt,...)
Definition: error.cc:988
ColumnVector transform(const Matrix &m, double x, double y, double z)
Definition: graphics.cc:5468
F77_RET_T const F77_INT F77_CMPLX const F77_INT F77_CMPLX * B
F77_RET_T const F77_INT F77_CMPLX * A
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:188
bool isnan(bool)
Definition: lo-mappers.h:178
T * r
Definition: mx-inlines.cc:781
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219