515
|
1 // f-find.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 "error.h" |
519
|
30 #include "defun-dld.h" |
515
|
31 |
|
32 static Octave_object |
|
33 find_to_fortran_idx (const ColumnVector i_idx, const ColumnVector j_idx, |
|
34 const tree_constant& val, int nr, int nc, int nargout) |
|
35 { |
|
36 Octave_object retval (nargout); |
|
37 |
|
38 switch (nargout) |
|
39 { |
|
40 case 1: |
|
41 { |
|
42 int count = i_idx.length (); |
|
43 ColumnVector tmp (count); |
|
44 for (int i = 0; i < count; i++) |
|
45 tmp (i) = nr * (j_idx (i) - 1.0) + i_idx (i); |
|
46 retval(0) = tree_constant (tmp, 1); |
|
47 // If you want this to work more like Matlab, use the following line |
|
48 // instead of the previous one. |
|
49 // retval(0) = tree_constant (tmp, (nr != 1)); |
|
50 } |
|
51 break; |
|
52 case 3: |
|
53 retval(2) = val; |
|
54 case 2: |
|
55 retval(0) = tree_constant (i_idx, 1); |
|
56 // If you want this to work more like Matlab, use the following line |
|
57 // instead of the previous one. |
|
58 // retval(0) = tree_constant (i_idx, (nr != 1)); |
|
59 retval(1) = tree_constant (j_idx, 1); |
|
60 break; |
|
61 default: |
|
62 panic_impossible (); |
|
63 break; |
|
64 } |
|
65 |
|
66 return retval; |
|
67 } |
|
68 |
|
69 static Octave_object |
|
70 find_nonzero_elem_idx (const Matrix& m, int nargout) |
|
71 { |
|
72 int count = 0; |
|
73 int m_nr = m.rows (); |
|
74 int m_nc = m.columns (); |
|
75 |
|
76 int i, j; |
|
77 for (j = 0; j < m_nc; j++) |
|
78 for (i = 0; i < m_nr; i++) |
|
79 if (m.elem (i, j) != 0.0) |
|
80 count++; |
|
81 |
|
82 Matrix result; |
|
83 Octave_object retval (nargout, result); |
|
84 |
|
85 if (count == 0) |
|
86 return retval; |
|
87 |
|
88 ColumnVector i_idx (count); |
|
89 ColumnVector j_idx (count); |
|
90 ColumnVector v (count); |
|
91 |
|
92 count = 0; |
|
93 for (j = 0; j < m_nc; j++) |
|
94 for (i = 0; i < m_nr; i++) |
|
95 { |
|
96 double d = m.elem (i, j); |
|
97 if (d != 0.0) |
|
98 { |
|
99 i_idx (count) = i + 1; |
|
100 j_idx (count) = j + 1; |
|
101 v (count) = d; |
|
102 count++; |
|
103 } |
|
104 } |
|
105 |
|
106 tree_constant tmp (v, 1); |
|
107 return find_to_fortran_idx (i_idx, j_idx, tmp, m_nr, m_nc, nargout); |
|
108 } |
|
109 |
|
110 static Octave_object |
|
111 find_nonzero_elem_idx (const ComplexMatrix& m, int nargout) |
|
112 { |
|
113 int count = 0; |
|
114 int m_nr = m.rows (); |
|
115 int m_nc = m.columns (); |
|
116 |
|
117 int i, j; |
|
118 for (j = 0; j < m_nc; j++) |
|
119 for (i = 0; i < m_nr; i++) |
|
120 if (m.elem (i, j) != 0.0) |
|
121 count++; |
|
122 |
|
123 Matrix result; |
|
124 Octave_object retval (nargout, result); |
|
125 |
|
126 if (count == 0) |
|
127 return retval; |
|
128 |
|
129 ColumnVector i_idx (count); |
|
130 ColumnVector j_idx (count); |
|
131 ComplexColumnVector v (count); |
|
132 |
|
133 count = 0; |
|
134 for (j = 0; j < m_nc; j++) |
|
135 for (i = 0; i < m_nr; i++) |
|
136 { |
|
137 Complex c = m.elem (i, j); |
|
138 if (c != 0.0) |
|
139 { |
|
140 i_idx (count) = i; |
|
141 j_idx (count) = j; |
|
142 v (count) = c; |
|
143 count++; |
|
144 } |
|
145 } |
|
146 |
|
147 tree_constant tmp (v, 1); |
|
148 return find_to_fortran_idx (i_idx, j_idx, tmp, m_nr, m_nc, nargout); |
|
149 } |
|
150 |
519
|
151 DEFUN_DLD ("find", Ffind, Sfind, 2, 3, |
|
152 "find (X) or [I, J, V] = find (X): Return indices of nonzero elements") |
515
|
153 { |
519
|
154 Octave_object retval; |
|
155 |
|
156 int nargin = args.length (); |
|
157 |
|
158 if (nargin != 2 || nargout > 3) |
|
159 { |
|
160 print_usage ("find"); |
|
161 return retval; |
|
162 } |
515
|
163 |
|
164 nargout = (nargout == 0) ? 1 : nargout; |
|
165 |
519
|
166 retval.resize (nargout, Matrix ()); |
|
167 |
|
168 tree_constant tmp = args(1).make_numeric (); |
515
|
169 |
|
170 switch (tmp.const_type ()) |
|
171 { |
|
172 case tree_constant_rep::matrix_constant: |
|
173 { |
|
174 Matrix m = tmp.matrix_value (); |
|
175 return find_nonzero_elem_idx (m, nargout); |
|
176 } |
|
177 break; |
|
178 case tree_constant_rep::scalar_constant: |
|
179 { |
|
180 double d = tmp.double_value (); |
|
181 if (d != 0.0) |
|
182 { |
|
183 retval(0) = 1.0; |
|
184 if (nargout > 1) |
|
185 retval(1) = 1.0; |
|
186 if (nargout > 2) |
|
187 retval(2) = d; |
|
188 } |
|
189 } |
|
190 break; |
|
191 case tree_constant_rep::complex_matrix_constant: |
|
192 { |
|
193 ComplexMatrix m = tmp.complex_matrix_value (); |
|
194 return find_nonzero_elem_idx (m, nargout); |
|
195 } |
|
196 break; |
|
197 case tree_constant_rep::complex_scalar_constant: |
|
198 { |
|
199 Complex c = tmp.complex_value (); |
|
200 if (c != 0.0) |
|
201 { |
|
202 retval(0) = 1.0; |
|
203 if (nargout > 1) |
|
204 retval(1) = 1.0; |
|
205 if (nargout > 2) |
|
206 retval(2) = c; |
|
207 } |
|
208 } |
|
209 break; |
|
210 default: |
|
211 break; |
|
212 } |
|
213 return retval; |
|
214 } |
|
215 |
|
216 /* |
|
217 ;;; Local Variables: *** |
|
218 ;;; mode: C++ *** |
|
219 ;;; page-delimiter: "^/\\*" *** |
|
220 ;;; End: *** |
|
221 */ |