515
|
1 // f-fill.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1994 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 HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #include "tree-const.h" |
|
29 #include "user-prefs.h" |
|
30 #include "utils.h" |
|
31 #include "error.h" |
|
32 #include "f-fill.h" |
|
33 |
|
34 #ifndef MIN |
|
35 #define MIN(a,b) ((a) < (b) ? (a) : (b)) |
|
36 #endif |
|
37 |
|
38 static void |
|
39 check_dimensions (int& nr, int& nc, const char *warnfor) |
|
40 { |
|
41 if (nr < 0 || nc < 0) |
|
42 { |
|
43 if (user_pref.treat_neg_dim_as_zero) |
|
44 nr = nc = 0; |
|
45 else |
|
46 error ("%s: can't create a matrix with negative dimensions", |
|
47 warnfor); |
|
48 } |
|
49 } |
|
50 |
|
51 static void |
|
52 get_dimensions (const tree_constant& a, const char *warn_for, |
|
53 int& nr, int& nc) |
|
54 { |
|
55 tree_constant tmpa = a.make_numeric (); |
|
56 |
|
57 if (tmpa.is_scalar_type ()) |
|
58 { |
|
59 double tmp = tmpa.double_value (); |
|
60 nr = nc = NINT (tmp); |
|
61 } |
|
62 else |
|
63 { |
|
64 nr = tmpa.rows (); |
|
65 nc = tmpa.columns (); |
|
66 |
|
67 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1)) |
|
68 { |
|
69 ColumnVector v = tmpa.to_vector (); |
|
70 |
|
71 nr = NINT (v.elem (0)); |
|
72 nc = NINT (v.elem (1)); |
|
73 } |
|
74 else |
|
75 warning ("%s (A): use %s (size (A)) instead", warn_for, warn_for); |
|
76 } |
|
77 |
|
78 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
79 } |
|
80 |
|
81 static void |
|
82 get_dimensions (const tree_constant& a, const tree_constant& b, |
|
83 const char *warn_for, int& nr, int& nc) |
|
84 { |
|
85 tree_constant tmpa = a.make_numeric (); |
|
86 tree_constant tmpb = b.make_numeric (); |
|
87 |
|
88 if (tmpa.is_scalar_type () && tmpb.is_scalar_type ()) |
|
89 { |
|
90 nr = NINT (tmpa.double_value ()); |
|
91 nc = NINT (tmpb.double_value ()); |
|
92 |
|
93 check_dimensions (nr, nc, warn_for); // May set error_state. |
|
94 } |
|
95 else |
|
96 error ("%s: expecting two scalar arguments", warn_for); |
|
97 } |
|
98 |
|
99 tree_constant |
|
100 fill_matrix (const tree_constant& a, double val, const char *warn_for) |
|
101 { |
|
102 int nr, nc; |
|
103 get_dimensions (a, warn_for, nr, nc); |
|
104 |
|
105 if (error_state) |
|
106 return tree_constant (); |
|
107 |
|
108 Matrix m (nr, nc, val); |
|
109 |
|
110 return m; |
|
111 } |
|
112 |
|
113 tree_constant |
|
114 fill_matrix (const tree_constant& a, const tree_constant& b, |
|
115 double val, const char *warn_for) |
|
116 { |
|
117 int nr, nc; |
|
118 get_dimensions (a, b, warn_for, nr, nc); // May set error_state. |
|
119 |
|
120 if (error_state) |
|
121 return tree_constant (); |
|
122 |
|
123 Matrix m (nr, nc, val); |
|
124 |
|
125 return m; |
|
126 } |
|
127 |
|
128 tree_constant |
|
129 identity_matrix (const tree_constant& a) |
|
130 { |
|
131 int nr, nc; |
|
132 get_dimensions (a, "eye", nr, nc); // May set error_state. |
|
133 |
|
134 if (error_state) |
|
135 return tree_constant (); |
|
136 |
|
137 Matrix m (nr, nc, 0.0); |
|
138 |
|
139 if (nr > 0 && nc > 0) |
|
140 { |
|
141 int n = MIN (nr, nc); |
|
142 for (int i = 0; i < n; i++) |
|
143 m.elem (i, i) = 1.0; |
|
144 } |
|
145 |
|
146 return m; |
|
147 } |
|
148 |
|
149 tree_constant |
|
150 identity_matrix (const tree_constant& a, const tree_constant& b) |
|
151 { |
|
152 int nr, nc; |
|
153 get_dimensions (a, b, "eye", nr, nc); // May set error_state. |
|
154 |
|
155 if (error_state) |
|
156 return tree_constant (); |
|
157 |
|
158 Matrix m (nr, nc, 0.0); |
|
159 |
|
160 if (nr > 0 && nc > 0) |
|
161 { |
|
162 int n = MIN (nr, nc); |
|
163 for (int i = 0; i < n; i++) |
|
164 m.elem (i, i) = 1.0; |
|
165 } |
|
166 |
|
167 return m; |
|
168 } |
|
169 |
|
170 /* |
|
171 ;;; Local Variables: *** |
|
172 ;;; mode: C++ *** |
|
173 ;;; page-delimiter: "^/\\*" *** |
|
174 ;;; End: *** |
|
175 */ |