Mercurial > hg > octave-thorsten
comparison liboctave/Array2-idx.h @ 2382:58e5955495d7
[project @ 1996-10-12 17:45:06 by jwe]
author | jwe |
---|---|
date | Sat, 12 Oct 1996 17:46:29 +0000 |
parents | aaeccf9e1d32 |
children | 58113987ee03 |
comparison
equal
deleted
inserted
replaced
2381:81298641458d | 2382:58e5955495d7 |
---|---|
31 { | 31 { |
32 Array2<T> retval; | 32 Array2<T> retval; |
33 | 33 |
34 int n_idx = index_count (); | 34 int n_idx = index_count (); |
35 | 35 |
36 int nr = d1; | |
37 int nc = d2; | |
38 | |
39 if (n_idx == 2) | 36 if (n_idx == 2) |
40 { | 37 { |
41 idx_vector *tmp = get_idx (); | 38 idx_vector *tmp = get_idx (); |
42 idx_vector idx_i = tmp[0]; | 39 idx_vector idx_i = tmp[0]; |
43 idx_vector idx_j = tmp[1]; | 40 idx_vector idx_j = tmp[1]; |
44 | 41 |
45 int n = idx_i.freeze (nr, "row", liboctave_pzo_flag); | 42 return index (idx_i, idx_j); |
46 int m = idx_j.freeze (nc, "column", liboctave_pzo_flag); | |
47 | |
48 if (idx_i && idx_j) | |
49 { | |
50 if (n == 0) | |
51 { | |
52 if (m == 0 || idx_j.is_colon_equiv (nc, 1)) | |
53 retval.resize (0, 0); | |
54 else | |
55 (*current_liboctave_error_handler) | |
56 ("invalid row index = 0"); | |
57 } | |
58 else if (m == 0) | |
59 { | |
60 if (n == 0 || idx_i.is_colon_equiv (nr, 1)) | |
61 retval.resize (0, 0); | |
62 else | |
63 (*current_liboctave_error_handler) | |
64 ("invalid column index = 0"); | |
65 } | |
66 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) | |
67 { | |
68 retval = *this; | |
69 } | |
70 else | |
71 { | |
72 retval.resize (n, m); | |
73 | |
74 for (int j = 0; j < m; j++) | |
75 { | |
76 int jj = idx_j.elem (j); | |
77 for (int i = 0; i < n; i++) | |
78 { | |
79 int ii = idx_i.elem (i); | |
80 retval.elem (i, j) = elem (ii, jj); | |
81 } | |
82 } | |
83 } | |
84 } | |
85 // idx_vector::freeze() printed an error message for us. | |
86 } | 43 } |
87 else if (n_idx == 1) | 44 else if (n_idx == 1) |
88 { | 45 { |
89 if (nr == 1 && nc == 1) | 46 idx_vector *tmp = get_idx (); |
90 { | 47 idx_vector idx = tmp[0]; |
91 Array<T> tmp = Array<T>::value (); | 48 |
92 | 49 return index (idx); |
93 int len = tmp.length (); | |
94 | |
95 if (len == 0) | |
96 retval = Array2<T> (0, 0); | |
97 else | |
98 { | |
99 if (liboctave_pcv_flag) | |
100 retval = Array2<T> (tmp, len, 1); | |
101 else | |
102 retval = Array2<T> (tmp, 1, len); | |
103 } | |
104 } | |
105 else if (nr == 1 || nc == 1) | |
106 { | |
107 int result_is_column_vector = (nc == 1); | |
108 | |
109 if (liboctave_dfi_flag) | |
110 { | |
111 idx_vector *tmp = get_idx (); | |
112 idx_vector idx = tmp[0]; | |
113 | |
114 if (idx.is_colon ()) | |
115 result_is_column_vector = 1; | |
116 } | |
117 | |
118 Array<T> tmp = Array<T>::value (); | |
119 | |
120 int len = tmp.length (); | |
121 | |
122 if (len == 0) | |
123 retval = Array2<T> (0, 0); | |
124 else | |
125 { | |
126 if (result_is_column_vector) | |
127 retval = Array2<T> (tmp, len, 1); | |
128 else | |
129 retval = Array2<T> (tmp, 1, len); | |
130 } | |
131 } | |
132 else if (liboctave_dfi_flag) | |
133 { | |
134 // This code is only for indexing matrices. The vector | |
135 // cases are handled above. | |
136 | |
137 idx_vector *tmp = get_idx (); | |
138 idx_vector idx = tmp[0]; | |
139 | |
140 idx.freeze (nr * nc, "matrix", liboctave_pzo_flag); | |
141 | |
142 if (idx) | |
143 { | |
144 int result_nr = idx.orig_rows (); | |
145 int result_nc = idx.orig_columns (); | |
146 | |
147 if (idx.is_colon ()) | |
148 { | |
149 result_nr = nr * nc; | |
150 result_nc = 1; | |
151 } | |
152 else if (idx.one_zero_only ()) | |
153 { | |
154 result_nr = idx.ones_count (); | |
155 result_nc = (result_nr > 0 ? 1 : 0); | |
156 } | |
157 | |
158 retval.resize (result_nr, result_nc); | |
159 | |
160 int k = 0; | |
161 for (int j = 0; j < result_nc; j++) | |
162 { | |
163 for (int i = 0; i < result_nr; i++) | |
164 { | |
165 int ii = idx.elem (k++); | |
166 int fr = ii % nr; | |
167 int fc = (ii - fr) / nr; | |
168 retval.elem (i, j) = elem (fr, fc); | |
169 } | |
170 } | |
171 } | |
172 // idx_vector::freeze() printed an error message for us. | |
173 } | |
174 else | |
175 (*current_liboctave_error_handler) | |
176 ("single index only valid for row or column vector"); | |
177 } | 50 } |
178 else | 51 else |
179 (*current_liboctave_error_handler) | 52 (*current_liboctave_error_handler) |
180 ("invalid number of indices for matrix expression"); | 53 ("invalid number of indices for matrix expression"); |
181 | 54 |
182 clear_index (); | 55 clear_index (); |
56 | |
57 return retval; | |
58 } | |
59 | |
60 template <class T> | |
61 Array2<T> | |
62 Array2<T>::index (idx_vector& idx) const | |
63 { | |
64 Array2<T> retval; | |
65 | |
66 int nr = d1; | |
67 int nc = d2; | |
68 | |
69 if (nr == 1 && nc == 1) | |
70 { | |
71 Array<T> tmp = Array<T>::index (idx); | |
72 | |
73 int len = tmp.length (); | |
74 | |
75 if (len == 0) | |
76 retval = Array2<T> (0, 0); | |
77 else | |
78 { | |
79 if (liboctave_pcv_flag) | |
80 retval = Array2<T> (tmp, len, 1); | |
81 else | |
82 retval = Array2<T> (tmp, 1, len); | |
83 } | |
84 } | |
85 else if (nr == 1 || nc == 1) | |
86 { | |
87 int result_is_column_vector = (nc == 1); | |
88 | |
89 if (liboctave_dfi_flag && idx.is_colon ()) | |
90 result_is_column_vector = 1; | |
91 | |
92 Array<T> tmp = Array<T>::index (idx); | |
93 | |
94 int len = tmp.length (); | |
95 | |
96 if (len == 0) | |
97 retval = Array2<T> (0, 0); | |
98 else | |
99 { | |
100 if (result_is_column_vector) | |
101 retval = Array2<T> (tmp, len, 1); | |
102 else | |
103 retval = Array2<T> (tmp, 1, len); | |
104 } | |
105 } | |
106 else if (liboctave_dfi_flag) | |
107 { | |
108 // This code is only for indexing matrices. The vector | |
109 // cases are handled above. | |
110 | |
111 idx.freeze (nr * nc, "matrix", liboctave_pzo_flag); | |
112 | |
113 if (idx) | |
114 { | |
115 int result_nr = idx.orig_rows (); | |
116 int result_nc = idx.orig_columns (); | |
117 | |
118 if (idx.is_colon ()) | |
119 { | |
120 result_nr = nr * nc; | |
121 result_nc = 1; | |
122 } | |
123 else if (idx.one_zero_only ()) | |
124 { | |
125 result_nr = idx.ones_count (); | |
126 result_nc = (result_nr > 0 ? 1 : 0); | |
127 } | |
128 | |
129 retval.resize (result_nr, result_nc); | |
130 | |
131 int k = 0; | |
132 for (int j = 0; j < result_nc; j++) | |
133 { | |
134 for (int i = 0; i < result_nr; i++) | |
135 { | |
136 int ii = idx.elem (k++); | |
137 int fr = ii % nr; | |
138 int fc = (ii - fr) / nr; | |
139 retval.elem (i, j) = elem (fr, fc); | |
140 } | |
141 } | |
142 } | |
143 // idx_vector::freeze() printed an error message for us. | |
144 } | |
145 else | |
146 (*current_liboctave_error_handler) | |
147 ("single index only valid for row or column vector"); | |
148 | |
149 return retval; | |
150 } | |
151 | |
152 template <class T> | |
153 Array2<T> | |
154 Array2<T>::index (idx_vector& idx_i, idx_vector& idx_j) const | |
155 { | |
156 Array2<T> retval; | |
157 | |
158 int nr = d1; | |
159 int nc = d2; | |
160 | |
161 int n = idx_i.freeze (nr, "row", liboctave_pzo_flag); | |
162 int m = idx_j.freeze (nc, "column", liboctave_pzo_flag); | |
163 | |
164 if (idx_i && idx_j) | |
165 { | |
166 if (n == 0) | |
167 { | |
168 if (m == 0 || idx_j.is_colon_equiv (nc, 1)) | |
169 retval.resize (0, 0); | |
170 else | |
171 (*current_liboctave_error_handler) | |
172 ("invalid row index = 0"); | |
173 } | |
174 else if (m == 0) | |
175 { | |
176 if (n == 0 || idx_i.is_colon_equiv (nr, 1)) | |
177 retval.resize (0, 0); | |
178 else | |
179 (*current_liboctave_error_handler) | |
180 ("invalid column index = 0"); | |
181 } | |
182 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) | |
183 { | |
184 retval = *this; | |
185 } | |
186 else | |
187 { | |
188 retval.resize (n, m); | |
189 | |
190 for (int j = 0; j < m; j++) | |
191 { | |
192 int jj = idx_j.elem (j); | |
193 for (int i = 0; i < n; i++) | |
194 { | |
195 int ii = idx_i.elem (i); | |
196 retval.elem (i, j) = elem (ii, jj); | |
197 } | |
198 } | |
199 } | |
200 } | |
201 | |
202 // idx_vector::freeze() printed an error message for us. | |
183 | 203 |
184 return retval; | 204 return retval; |
185 } | 205 } |
186 | 206 |
187 template <class T> | 207 template <class T> |