3
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2002, 2003, 2004, |
|
4 2005, 2007 John W. Eaton |
3
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
3
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
3
|
21 |
|
22 */ |
|
23 |
238
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
3
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
238
|
29 |
3
|
30 #include "FEGrid.h" |
227
|
31 #include "lo-error.h" |
3
|
32 |
|
33 // error handling |
|
34 |
|
35 void |
|
36 FEGrid::error (const char* msg) const |
|
37 { |
227
|
38 (*current_liboctave_error_handler) ("fatal FEGrid error: %s", msg); |
3
|
39 } |
|
40 |
|
41 void |
|
42 FEGrid::nel_error (void) const |
|
43 { |
|
44 error ("number of elements less than 1"); |
|
45 } |
|
46 |
|
47 // Constructors |
|
48 |
5275
|
49 FEGrid::FEGrid (octave_idx_type nel, double width) |
3
|
50 { |
|
51 if (nel < 1) |
227
|
52 { |
|
53 nel_error (); |
|
54 return; |
|
55 } |
3
|
56 |
|
57 elem.resize (nel+1); |
|
58 |
5275
|
59 for (octave_idx_type i = 0; i <= nel; i++) |
3
|
60 elem.elem (i) = i * width; |
|
61 } |
|
62 |
5275
|
63 FEGrid::FEGrid (octave_idx_type nel, double l, double r) |
3
|
64 { |
|
65 if (nel < 1) |
227
|
66 { |
|
67 nel_error (); |
|
68 return; |
|
69 } |
3
|
70 |
|
71 elem.resize (nel+1); |
|
72 |
4587
|
73 double width = (r - l) / nel; |
3
|
74 |
5275
|
75 for (octave_idx_type i = 0; i <= nel; i++) |
4587
|
76 elem.elem (i) = i * width + l; |
3
|
77 |
|
78 check_grid (); |
|
79 } |
|
80 |
5275
|
81 octave_idx_type |
3
|
82 FEGrid::element (double x) const |
|
83 { |
|
84 if (! in_bounds (x)) |
227
|
85 { |
|
86 error ("value not within grid boundaries"); |
|
87 return -1; |
|
88 } |
3
|
89 |
5275
|
90 octave_idx_type nel = elem.capacity () - 1; |
|
91 for (octave_idx_type i = 1; i <= nel; i++) |
3
|
92 { |
|
93 if (x >= elem.elem (i-1) && x <= elem.elem (i)) |
|
94 return i; |
|
95 } |
|
96 return -1; |
|
97 |
|
98 } |
|
99 |
|
100 void |
|
101 FEGrid::check_grid (void) const |
|
102 { |
5275
|
103 octave_idx_type nel = elem.capacity () - 1; |
3
|
104 if (nel < 1) |
227
|
105 { |
|
106 nel_error (); |
|
107 return; |
|
108 } |
3
|
109 |
5275
|
110 for (octave_idx_type i = 1; i <= nel; i++) |
3
|
111 { |
|
112 if (elem.elem (i-1) > elem.elem (i)) |
227
|
113 { |
|
114 error ("element boundaries not in ascending order"); |
|
115 return; |
|
116 } |
3
|
117 |
|
118 if (elem.elem (i-1) == elem.elem (i)) |
227
|
119 { |
|
120 error ("zero width element"); |
|
121 return; |
|
122 } |
3
|
123 } |
|
124 } |
|
125 |
3504
|
126 std::ostream& |
|
127 operator << (std::ostream& s, const FEGrid& g) |
3
|
128 { |
|
129 s << g.element_boundaries (); |
|
130 return s; |
|
131 } |
|
132 |
|
133 /* |
|
134 ;;; Local Variables: *** |
|
135 ;;; mode: C++ *** |
|
136 ;;; End: *** |
|
137 */ |