3
|
1 // FEGrid.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 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 |
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
238
|
32 #include <iostream.h> |
|
33 |
3
|
34 #include "FEGrid.h" |
227
|
35 #include "lo-error.h" |
3
|
36 |
|
37 // error handling |
|
38 |
|
39 void |
|
40 FEGrid::error (const char* msg) const |
|
41 { |
227
|
42 (*current_liboctave_error_handler) ("fatal FEGrid error: %s", msg); |
3
|
43 } |
|
44 |
|
45 void |
|
46 FEGrid::nel_error (void) const |
|
47 { |
|
48 error ("number of elements less than 1"); |
|
49 } |
|
50 |
|
51 // Constructors |
|
52 |
|
53 FEGrid::FEGrid (int nel, double width) |
|
54 { |
|
55 if (nel < 1) |
227
|
56 { |
|
57 nel_error (); |
|
58 return; |
|
59 } |
3
|
60 |
|
61 elem.resize (nel+1); |
|
62 |
|
63 for (int i = 0; i <= nel; i++) |
|
64 elem.elem (i) = i * width; |
|
65 } |
|
66 |
|
67 FEGrid::FEGrid (int nel, double left, double right) |
|
68 { |
|
69 if (nel < 1) |
227
|
70 { |
|
71 nel_error (); |
|
72 return; |
|
73 } |
3
|
74 |
|
75 elem.resize (nel+1); |
|
76 |
|
77 double width = (right - left) / (double) nel; |
|
78 |
|
79 for (int i = 0; i <= nel; i++) |
|
80 elem.elem (i) = i * width + left; |
|
81 |
|
82 check_grid (); |
|
83 } |
|
84 |
|
85 int |
|
86 FEGrid::element (double x) const |
|
87 { |
|
88 if (! in_bounds (x)) |
227
|
89 { |
|
90 error ("value not within grid boundaries"); |
|
91 return -1; |
|
92 } |
3
|
93 |
|
94 int nel = elem.capacity () - 1; |
|
95 for (int i = 1; i <= nel; i++) |
|
96 { |
|
97 if (x >= elem.elem (i-1) && x <= elem.elem (i)) |
|
98 return i; |
|
99 } |
|
100 return -1; |
|
101 |
|
102 } |
|
103 |
|
104 void |
|
105 FEGrid::check_grid (void) const |
|
106 { |
|
107 int nel = elem.capacity () - 1; |
|
108 if (nel < 1) |
227
|
109 { |
|
110 nel_error (); |
|
111 return; |
|
112 } |
3
|
113 |
|
114 for (int i = 1; i <= nel; i++) |
|
115 { |
|
116 if (elem.elem (i-1) > elem.elem (i)) |
227
|
117 { |
|
118 error ("element boundaries not in ascending order"); |
|
119 return; |
|
120 } |
3
|
121 |
|
122 if (elem.elem (i-1) == elem.elem (i)) |
227
|
123 { |
|
124 error ("zero width element"); |
|
125 return; |
|
126 } |
3
|
127 } |
|
128 } |
|
129 |
|
130 ostream& |
|
131 operator << (ostream& s, const FEGrid& g) |
|
132 { |
|
133 s << g.element_boundaries (); |
|
134 return s; |
|
135 } |
|
136 |
|
137 /* |
|
138 ;;; Local Variables: *** |
|
139 ;;; mode: C++ *** |
|
140 ;;; page-delimiter: "^/\\*" *** |
|
141 ;;; End: *** |
|
142 */ |