Mercurial > octave
comparison liboctave/FEGrid.cc @ 3:9a4c07481e61
[project @ 1993-08-08 01:20:23 by jwe]
Initial revision
author | jwe |
---|---|
date | Sun, 08 Aug 1993 01:21:46 +0000 |
parents | |
children | 1a48a1b91489 |
comparison
equal
deleted
inserted
replaced
2:c0190df9885d | 3:9a4c07481e61 |
---|---|
1 // FEGrid.cc -*- C++ -*- | |
2 /* | |
3 | |
4 Copyright (C) 1992, 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 "FEGrid.h" | |
29 | |
30 // error handling | |
31 | |
32 void | |
33 FEGrid::error (const char* msg) const | |
34 { | |
35 cerr << "Fatal FEGrid error. " << msg << "\n"; | |
36 exit(1); | |
37 } | |
38 | |
39 void | |
40 FEGrid::nel_error (void) const | |
41 { | |
42 error ("number of elements less than 1"); | |
43 } | |
44 | |
45 // Constructors | |
46 | |
47 FEGrid::FEGrid (int nel, double width) | |
48 { | |
49 if (nel < 1) | |
50 nel_error (); | |
51 | |
52 elem.resize (nel+1); | |
53 | |
54 for (int i = 0; i <= nel; i++) | |
55 elem.elem (i) = i * width; | |
56 } | |
57 | |
58 FEGrid::FEGrid (int nel, double left, double right) | |
59 { | |
60 if (nel < 1) | |
61 nel_error (); | |
62 | |
63 elem.resize (nel+1); | |
64 | |
65 double width = (right - left) / (double) nel; | |
66 | |
67 for (int i = 0; i <= nel; i++) | |
68 elem.elem (i) = i * width + left; | |
69 | |
70 check_grid (); | |
71 } | |
72 | |
73 int | |
74 FEGrid::element (double x) const | |
75 { | |
76 if (! in_bounds (x)) | |
77 error ("value not within grid boundaries"); | |
78 | |
79 int nel = elem.capacity () - 1; | |
80 for (int i = 1; i <= nel; i++) | |
81 { | |
82 if (x >= elem.elem (i-1) && x <= elem.elem (i)) | |
83 return i; | |
84 } | |
85 return -1; | |
86 | |
87 } | |
88 | |
89 void | |
90 FEGrid::check_grid (void) const | |
91 { | |
92 int nel = elem.capacity () - 1; | |
93 if (nel < 1) | |
94 nel_error (); | |
95 | |
96 for (int i = 1; i <= nel; i++) | |
97 { | |
98 if (elem.elem (i-1) > elem.elem (i)) | |
99 error ("element boundaries not in ascending order"); | |
100 | |
101 if (elem.elem (i-1) == elem.elem (i)) | |
102 error ("zero width element"); | |
103 } | |
104 } | |
105 | |
106 ostream& | |
107 operator << (ostream& s, const FEGrid& g) | |
108 { | |
109 s << g.element_boundaries (); | |
110 return s; | |
111 } | |
112 | |
113 /* | |
114 ;;; Local Variables: *** | |
115 ;;; mode: C++ *** | |
116 ;;; page-delimiter: "^/\\*" *** | |
117 ;;; End: *** | |
118 */ |