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