3
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
3
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3
|
20 |
|
21 */ |
|
22 |
238
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
3
|
25 #endif |
|
26 |
3503
|
27 #include <iostream> |
238
|
28 |
3
|
29 #include "FEGrid.h" |
227
|
30 #include "lo-error.h" |
3
|
31 |
|
32 // error handling |
|
33 |
|
34 void |
|
35 FEGrid::error (const char* msg) const |
|
36 { |
227
|
37 (*current_liboctave_error_handler) ("fatal FEGrid error: %s", msg); |
3
|
38 } |
|
39 |
|
40 void |
|
41 FEGrid::nel_error (void) const |
|
42 { |
|
43 error ("number of elements less than 1"); |
|
44 } |
|
45 |
|
46 // Constructors |
|
47 |
5275
|
48 FEGrid::FEGrid (octave_idx_type nel, double width) |
3
|
49 { |
|
50 if (nel < 1) |
227
|
51 { |
|
52 nel_error (); |
|
53 return; |
|
54 } |
3
|
55 |
|
56 elem.resize (nel+1); |
|
57 |
5275
|
58 for (octave_idx_type i = 0; i <= nel; i++) |
3
|
59 elem.elem (i) = i * width; |
|
60 } |
|
61 |
5275
|
62 FEGrid::FEGrid (octave_idx_type nel, double l, double r) |
3
|
63 { |
|
64 if (nel < 1) |
227
|
65 { |
|
66 nel_error (); |
|
67 return; |
|
68 } |
3
|
69 |
|
70 elem.resize (nel+1); |
|
71 |
4587
|
72 double width = (r - l) / nel; |
3
|
73 |
5275
|
74 for (octave_idx_type i = 0; i <= nel; i++) |
4587
|
75 elem.elem (i) = i * width + l; |
3
|
76 |
|
77 check_grid (); |
|
78 } |
|
79 |
5275
|
80 octave_idx_type |
3
|
81 FEGrid::element (double x) const |
|
82 { |
|
83 if (! in_bounds (x)) |
227
|
84 { |
|
85 error ("value not within grid boundaries"); |
|
86 return -1; |
|
87 } |
3
|
88 |
5275
|
89 octave_idx_type nel = elem.capacity () - 1; |
|
90 for (octave_idx_type i = 1; i <= nel; i++) |
3
|
91 { |
|
92 if (x >= elem.elem (i-1) && x <= elem.elem (i)) |
|
93 return i; |
|
94 } |
|
95 return -1; |
|
96 |
|
97 } |
|
98 |
|
99 void |
|
100 FEGrid::check_grid (void) const |
|
101 { |
5275
|
102 octave_idx_type nel = elem.capacity () - 1; |
3
|
103 if (nel < 1) |
227
|
104 { |
|
105 nel_error (); |
|
106 return; |
|
107 } |
3
|
108 |
5275
|
109 for (octave_idx_type i = 1; i <= nel; i++) |
3
|
110 { |
|
111 if (elem.elem (i-1) > elem.elem (i)) |
227
|
112 { |
|
113 error ("element boundaries not in ascending order"); |
|
114 return; |
|
115 } |
3
|
116 |
|
117 if (elem.elem (i-1) == elem.elem (i)) |
227
|
118 { |
|
119 error ("zero width element"); |
|
120 return; |
|
121 } |
3
|
122 } |
|
123 } |
|
124 |
3504
|
125 std::ostream& |
|
126 operator << (std::ostream& s, const FEGrid& g) |
3
|
127 { |
|
128 s << g.element_boundaries (); |
|
129 return s; |
|
130 } |
|
131 |
|
132 /* |
|
133 ;;; Local Variables: *** |
|
134 ;;; mode: C++ *** |
|
135 ;;; End: *** |
|
136 */ |