1
|
1 // tc-quad.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
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 2, or (at your option) any |
|
11 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, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include "Quad.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "variables.h" |
|
32 #include "mappers.h" |
|
33 #include "gripes.h" |
|
34 #include "error.h" |
|
35 #include "utils.h" |
|
36 |
|
37 // Global pointer for user defined function required by quadrature functions. |
|
38 static tree *quad_fcn; |
|
39 |
|
40 #ifdef WITH_DLD |
|
41 tree_constant * |
|
42 builtin_quad_2 (tree_constant *args, int nargin, int nargout) |
|
43 { |
|
44 return do_quad (args, nargin, nargout); |
|
45 } |
|
46 #endif |
|
47 |
|
48 double |
|
49 quad_user_function (double x) |
|
50 { |
|
51 double retval = 0.0; |
|
52 |
|
53 // tree_constant name = tree_constant (quad_fcn->name ()); |
|
54 tree_constant *args = new tree_constant [2]; |
|
55 // args[0] = name; |
|
56 args[1] = tree_constant (x); |
|
57 |
|
58 if (quad_fcn != NULL_TREE) |
|
59 { |
|
60 tree_constant *tmp = quad_fcn->eval (args, 2, 1, 0); |
|
61 delete [] args; |
|
62 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
63 { |
|
64 retval = tmp[0].to_scalar (); |
|
65 delete [] tmp; |
|
66 } |
|
67 else |
|
68 { |
|
69 delete [] tmp; |
|
70 gripe_user_supplied_eval ("quad"); |
|
71 jump_to_top_level (); |
|
72 } |
|
73 } |
|
74 |
|
75 return retval; |
|
76 } |
|
77 |
|
78 tree_constant * |
|
79 do_quad (tree_constant *args, int nargin, int nargout) |
|
80 { |
|
81 // Assumes that we have been given the correct number of arguments. |
|
82 |
|
83 tree_constant *retval = NULL_TREE_CONST; |
|
84 |
|
85 quad_fcn = is_valid_function (args[1], "fsolve", 1); |
|
86 if (quad_fcn == NULL_TREE |
|
87 || takes_correct_nargs (quad_fcn, 2, "fsolve", 1) != 1) |
|
88 return retval; |
|
89 |
|
90 double a = args[2].to_scalar (); |
|
91 double b = args[3].to_scalar (); |
|
92 |
|
93 int indefinite = 0; |
|
94 IndefQuad::IntegralType indef_type = IndefQuad::doubly_infinite; |
|
95 double bound = 0.0; |
|
96 if ((int) xisinf (a) && (int) xisinf (b)) |
|
97 { |
|
98 indefinite = 1; |
|
99 indef_type = IndefQuad::doubly_infinite; |
|
100 } |
|
101 else if ((int) xisinf (a)) |
|
102 { |
|
103 indefinite = 1; |
|
104 bound = b; |
|
105 indef_type = IndefQuad::neg_inf_to_bound; |
|
106 } |
|
107 else if ((int) xisinf (b)) |
|
108 { |
|
109 indefinite = 1; |
|
110 bound = a; |
|
111 indef_type = IndefQuad::bound_to_inf; |
|
112 } |
|
113 |
|
114 int ier = 0; |
|
115 int nfun = 0; |
|
116 double abserr = 0.0; |
|
117 double val = 0.0; |
|
118 double abstol = 1e-6; |
|
119 double reltol = 1e-6; |
|
120 Vector tol (2); |
|
121 Vector sing; |
|
122 int have_sing = 0; |
|
123 switch (nargin) |
|
124 { |
|
125 case 6: |
|
126 if (indefinite) |
|
127 { |
|
128 message ("quad", "sorry, singularities not allowed on infinite intervals"); |
|
129 return retval; |
|
130 } |
|
131 have_sing = 1; |
|
132 sing = args[5].to_vector (); |
|
133 case 5: |
|
134 tol = args[4].to_vector (); |
|
135 switch (tol.capacity ()) |
|
136 { |
|
137 case 2: |
|
138 reltol = tol.elem (1); |
|
139 case 1: |
|
140 abstol = tol.elem (0); |
|
141 break; |
|
142 default: |
|
143 message ("quad", "expecting tol to contain no more than two values"); |
|
144 return retval; |
|
145 } |
|
146 case 4: |
|
147 if (indefinite) |
|
148 { |
|
149 IndefQuad iq (quad_user_function, bound, indef_type, abstol, reltol); |
|
150 val = iq.integrate (ier, nfun, abserr); |
|
151 } |
|
152 else |
|
153 { |
|
154 if (have_sing) |
|
155 { |
|
156 DefQuad dq (quad_user_function, a, b, sing, abstol, reltol); |
|
157 val = dq.integrate (ier, nfun, abserr); |
|
158 } |
|
159 else |
|
160 { |
|
161 DefQuad dq (quad_user_function, a, b, abstol, reltol); |
|
162 val = dq.integrate (ier, nfun, abserr); |
|
163 } |
|
164 } |
|
165 break; |
|
166 default: |
|
167 panic_impossible (); |
|
168 break; |
|
169 } |
|
170 |
|
171 retval = new tree_constant [5]; |
|
172 |
|
173 retval[0] = tree_constant (val); |
|
174 retval[1] = tree_constant ((double) ier); |
|
175 retval[2] = tree_constant ((double) nfun); |
|
176 retval[3] = tree_constant (abserr); |
|
177 |
|
178 return retval; |
|
179 } |
|
180 |
|
181 /* |
|
182 ;;; Local Variables: *** |
|
183 ;;; mode: C++ *** |
|
184 ;;; page-delimiter: "^/\\*" *** |
|
185 ;;; End: *** |
|
186 */ |
|
187 |