1 | ///
|
---|
2 | /// This file is part of ILNumerics Community Edition.
|
---|
3 | ///
|
---|
4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
6 | ///
|
---|
7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
9 | /// the Free Software Foundation.
|
---|
10 | ///
|
---|
11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | /// GNU General Public License for more details.
|
---|
15 | ///
|
---|
16 | /// You should have received a copy of the GNU General Public License
|
---|
17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | ///
|
---|
20 | /// In addition this software uses the following components and/or licenses:
|
---|
21 | ///
|
---|
22 | /// =================================================================================
|
---|
23 | /// The Open Toolkit Library License
|
---|
24 | ///
|
---|
25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
26 | ///
|
---|
27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
29 | /// in the Software without restriction, including without limitation the rights to
|
---|
30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
32 | /// so, subject to the following conditions:
|
---|
33 | ///
|
---|
34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
35 | /// copies or substantial portions of the Software.
|
---|
36 | ///
|
---|
37 | /// =================================================================================
|
---|
38 | ///
|
---|
39 |
|
---|
40 | using System;
|
---|
41 | using System.Collections.Generic;
|
---|
42 | using System.Text;
|
---|
43 | using ILNumerics.Storage;
|
---|
44 | using ILNumerics.Misc;
|
---|
45 | using ILNumerics.Exceptions;
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | namespace ILNumerics {
|
---|
50 | public partial class ILMath {
|
---|
51 |
|
---|
52 | |
---|
53 | /// <summary>
|
---|
54 | /// Find nonzero elements in A
|
---|
55 | /// </summary>
|
---|
56 | /// <param name="A">Input array</param>
|
---|
57 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
58 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
59 | /// Set to 0 to search full array (default).</param>
|
---|
60 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
61 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
62 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
63 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
64 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
65 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
66 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
67 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
68 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
69 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
70 | public static ILRetArray<double> find(ILInArray< double> A, int limit = 0,
|
---|
71 | ILOutArray<double> C = null, ILOutArray< double> V = null) {
|
---|
72 | using (ILScope.Enter(A)) {
|
---|
73 | bool create_row_columns = !Object.Equals(C, null);
|
---|
74 | bool return_values = !Object.Equals(V, null);
|
---|
75 | ILArray<double> ret = empty();
|
---|
76 | ILSize inDim = A.Size;
|
---|
77 | if (inDim.NumberOfElements == 1) {
|
---|
78 | #region SCALAR
|
---|
79 | // scalar -> return copy
|
---|
80 |
|
---|
81 | if (A.GetValue(0, 0) != 0.0) {
|
---|
82 | if (create_row_columns) {
|
---|
83 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
84 | }
|
---|
85 | if (return_values) {
|
---|
86 | V.a = A.C;
|
---|
87 | }
|
---|
88 | return zeros<double>(1, 1);
|
---|
89 | } else {
|
---|
90 | if (create_row_columns) {
|
---|
91 | C.a = empty<double>(ILSize.Empty00);
|
---|
92 | }
|
---|
93 | if (return_values) {
|
---|
94 | V.a = empty<double>(ILSize.Empty00);
|
---|
95 | }
|
---|
96 | return empty<double>(ILSize.Empty00);
|
---|
97 | }
|
---|
98 | #endregion SCALAR
|
---|
99 | }
|
---|
100 | long nrElements = inDim.NumberOfElements;
|
---|
101 |
|
---|
102 |
|
---|
103 | if (limit != 0) {
|
---|
104 | int lim = Math.Abs(limit);
|
---|
105 | if (lim < nrElements)
|
---|
106 | nrElements = lim;
|
---|
107 | }
|
---|
108 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
109 | int foundIdx = 0;
|
---|
110 | // physical -> pointer arithmetic
|
---|
111 | if (limit >= 0) {
|
---|
112 | unsafe {
|
---|
113 | fixed (double* pIndices = indices)
|
---|
114 | fixed ( double* pX = A.GetArrayForRead()) {
|
---|
115 |
|
---|
116 | double* lastElement = pX + inDim.NumberOfElements;
|
---|
117 |
|
---|
118 | double* tmpIn = pX;
|
---|
119 | double* pI = pIndices;
|
---|
120 | double* pFoundLast = pI + indices.Length;
|
---|
121 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
122 |
|
---|
123 | if (*tmpIn != 0.0)
|
---|
124 | *pI++ = (double)(tmpIn - pX);
|
---|
125 | tmpIn++;
|
---|
126 | }
|
---|
127 | foundIdx = (int)(pI - pIndices);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | } else {
|
---|
131 | // search backwards
|
---|
132 | unsafe {
|
---|
133 | fixed (double* pIndices = indices)
|
---|
134 | fixed ( double* pX = A.GetArrayForRead()) {
|
---|
135 |
|
---|
136 | double* lastElementX = pX;
|
---|
137 |
|
---|
138 | double* tmpIn = pX + inDim.NumberOfElements;
|
---|
139 | double* pI = pIndices + indices.Length;
|
---|
140 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
141 | tmpIn--;
|
---|
142 |
|
---|
143 | if (*tmpIn != 0.0)
|
---|
144 | *(--pI) = (double)(tmpIn - pX);
|
---|
145 | }
|
---|
146 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | if (foundIdx == 0) {
|
---|
151 | return empty();
|
---|
152 | }
|
---|
153 | // transform to row / columns; extract values if needed
|
---|
154 | int leadDimLen = inDim[0];
|
---|
155 | if (create_row_columns) {
|
---|
156 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
157 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
158 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
159 | if (return_values) {
|
---|
160 | V.a = new ILRetArray< double>(ILMemoryPool.Pool.New< double>(foundIdx), foundIdx, 1);
|
---|
161 | // copy values, transform to row/columns
|
---|
162 | unsafe {
|
---|
163 | fixed (double* pIndices = indices,
|
---|
164 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
165 | fixed ( double* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
166 | double* pI = (limit >= 0) ?
|
---|
167 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
168 | double* pLastIndex = pI + foundIdx;
|
---|
169 | double* pR = pRows;
|
---|
170 | double* pC = pCols;
|
---|
171 |
|
---|
172 | double* pV = pValues;
|
---|
173 |
|
---|
174 | double* pX = pInput;
|
---|
175 | while (pI < pLastIndex) {
|
---|
176 | *pR++ = *(pI) % leadDimLen;
|
---|
177 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
178 | *pV++ = *(pInput + (int)*pI++);
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | } else {
|
---|
183 | // just return row / columns
|
---|
184 | unsafe {
|
---|
185 | fixed (double* pIndices = indices,
|
---|
186 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
187 | fixed ( double* pInput = A.GetArrayForRead()) {
|
---|
188 | double* pI = (limit >= 0) ?
|
---|
189 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
190 | double* pLastIndex = pI + foundIdx;
|
---|
191 | double* pR = pRows;
|
---|
192 | double* pC = pCols;
|
---|
193 | while (pI < pLastIndex) {
|
---|
194 | *pR++ = *(pI) % leadDimLen;
|
---|
195 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | #endregion RETURN ROWS / COLUMNS
|
---|
201 | } else {
|
---|
202 | #region RETURN INDICES ONLY
|
---|
203 | if (foundIdx != indices.Length) {
|
---|
204 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
205 | unsafe {
|
---|
206 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
207 | double* pI = (limit >= 0) ?
|
---|
208 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
209 | double* pLastIndex = pI + foundIdx;
|
---|
210 | double* pR = pRows;
|
---|
211 | while (pI < pLastIndex) {
|
---|
212 | *pR++ = *pI++;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | } else {
|
---|
217 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
218 | }
|
---|
219 | #endregion RETURN INDICES ONLY
|
---|
220 | }
|
---|
221 | return ret;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | |
---|
225 | #region HYCALPER AUTO GENERATED CODE
|
---|
226 | |
---|
227 | /// <summary>
|
---|
228 | /// Find nonzero elements in A
|
---|
229 | /// </summary>
|
---|
230 | /// <param name="A">Input array</param>
|
---|
231 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
232 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
233 | /// Set to 0 to search full array (default).</param>
|
---|
234 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
235 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
236 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
237 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
238 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
239 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
240 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
241 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
242 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
243 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
244 | public static ILRetArray<double> find(ILInArray< Int64> A, int limit = 0,
|
---|
245 | ILOutArray<double> C = null, ILOutArray< Int64> V = null) {
|
---|
246 | using (ILScope.Enter(A)) {
|
---|
247 | bool create_row_columns = !Object.Equals(C, null);
|
---|
248 | bool return_values = !Object.Equals(V, null);
|
---|
249 | ILArray<double> ret = empty();
|
---|
250 | ILSize inDim = A.Size;
|
---|
251 | if (inDim.NumberOfElements == 1) {
|
---|
252 | #region SCALAR
|
---|
253 | // scalar -> return copy
|
---|
254 | if (A.GetValue(0,0) != 0) {
|
---|
255 | if (create_row_columns) {
|
---|
256 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
257 | }
|
---|
258 | if (return_values) {
|
---|
259 | V.a = A.C;
|
---|
260 | }
|
---|
261 | return zeros<double>(1, 1);
|
---|
262 | } else {
|
---|
263 | if (create_row_columns) {
|
---|
264 | C.a = empty<double>(ILSize.Empty00);
|
---|
265 | }
|
---|
266 | if (return_values) {
|
---|
267 | V.a = empty<Int64>(ILSize.Empty00);
|
---|
268 | }
|
---|
269 | return empty<double>(ILSize.Empty00);
|
---|
270 | }
|
---|
271 | #endregion SCALAR
|
---|
272 | }
|
---|
273 | long nrElements = inDim.NumberOfElements;
|
---|
274 |
|
---|
275 | if (limit != 0) {
|
---|
276 | int lim = Math.Abs(limit);
|
---|
277 | if (lim < nrElements)
|
---|
278 | nrElements = lim;
|
---|
279 | }
|
---|
280 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
281 | int foundIdx = 0;
|
---|
282 | // physical -> pointer arithmetic
|
---|
283 | if (limit >= 0) {
|
---|
284 | unsafe {
|
---|
285 | fixed (double* pIndices = indices)
|
---|
286 | fixed ( Int64* pX = A.GetArrayForRead()) {
|
---|
287 |
|
---|
288 | Int64* lastElement = pX + inDim.NumberOfElements;
|
---|
289 |
|
---|
290 | Int64* tmpIn = pX;
|
---|
291 | double* pI = pIndices;
|
---|
292 | double* pFoundLast = pI + indices.Length;
|
---|
293 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
294 | if (*tmpIn != 0)
|
---|
295 | *pI++ = (double)(tmpIn - pX);
|
---|
296 | tmpIn++;
|
---|
297 | }
|
---|
298 | foundIdx = (int)(pI - pIndices);
|
---|
299 | }
|
---|
300 | }
|
---|
301 | } else {
|
---|
302 | // search backwards
|
---|
303 | unsafe {
|
---|
304 | fixed (double* pIndices = indices)
|
---|
305 | fixed ( Int64* pX = A.GetArrayForRead()) {
|
---|
306 |
|
---|
307 | Int64* lastElementX = pX;
|
---|
308 |
|
---|
309 | Int64* tmpIn = pX + inDim.NumberOfElements;
|
---|
310 | double* pI = pIndices + indices.Length;
|
---|
311 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
312 | tmpIn--;
|
---|
313 | if (*tmpIn != 0)
|
---|
314 | *(--pI) = (double)(tmpIn - pX);
|
---|
315 | }
|
---|
316 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 | if (foundIdx == 0) {
|
---|
321 | return empty();
|
---|
322 | }
|
---|
323 | // transform to row / columns; extract values if needed
|
---|
324 | int leadDimLen = inDim[0];
|
---|
325 | if (create_row_columns) {
|
---|
326 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
327 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
328 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
329 | if (return_values) {
|
---|
330 | V.a = new ILRetArray< Int64>(ILMemoryPool.Pool.New< Int64>(foundIdx), foundIdx, 1);
|
---|
331 | // copy values, transform to row/columns
|
---|
332 | unsafe {
|
---|
333 | fixed (double* pIndices = indices,
|
---|
334 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
335 | fixed ( Int64* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
336 | double* pI = (limit >= 0) ?
|
---|
337 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
338 | double* pLastIndex = pI + foundIdx;
|
---|
339 | double* pR = pRows;
|
---|
340 | double* pC = pCols;
|
---|
341 |
|
---|
342 | Int64* pV = pValues;
|
---|
343 |
|
---|
344 | Int64* pX = pInput;
|
---|
345 | while (pI < pLastIndex) {
|
---|
346 | *pR++ = *(pI) % leadDimLen;
|
---|
347 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
348 | *pV++ = *(pInput + (int)*pI++);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | } else {
|
---|
353 | // just return row / columns
|
---|
354 | unsafe {
|
---|
355 | fixed (double* pIndices = indices,
|
---|
356 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
357 | fixed ( Int64* pInput = A.GetArrayForRead()) {
|
---|
358 | double* pI = (limit >= 0) ?
|
---|
359 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
360 | double* pLastIndex = pI + foundIdx;
|
---|
361 | double* pR = pRows;
|
---|
362 | double* pC = pCols;
|
---|
363 | while (pI < pLastIndex) {
|
---|
364 | *pR++ = *(pI) % leadDimLen;
|
---|
365 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
366 | }
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 | #endregion RETURN ROWS / COLUMNS
|
---|
371 | } else {
|
---|
372 | #region RETURN INDICES ONLY
|
---|
373 | if (foundIdx != indices.Length) {
|
---|
374 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
375 | unsafe {
|
---|
376 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
377 | double* pI = (limit >= 0) ?
|
---|
378 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
379 | double* pLastIndex = pI + foundIdx;
|
---|
380 | double* pR = pRows;
|
---|
381 | while (pI < pLastIndex) {
|
---|
382 | *pR++ = *pI++;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 | } else {
|
---|
387 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
388 | }
|
---|
389 | #endregion RETURN INDICES ONLY
|
---|
390 | }
|
---|
391 | return ret;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | /// <summary>
|
---|
395 | /// Find nonzero elements in A
|
---|
396 | /// </summary>
|
---|
397 | /// <param name="A">Input array</param>
|
---|
398 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
399 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
400 | /// Set to 0 to search full array (default).</param>
|
---|
401 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
402 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
403 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
404 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
405 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
406 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
407 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
408 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
409 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
410 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
411 | public static ILRetArray<double> find(ILInArray< Int32> A, int limit = 0,
|
---|
412 | ILOutArray<double> C = null, ILOutArray< Int32> V = null) {
|
---|
413 | using (ILScope.Enter(A)) {
|
---|
414 | bool create_row_columns = !Object.Equals(C, null);
|
---|
415 | bool return_values = !Object.Equals(V, null);
|
---|
416 | ILArray<double> ret = empty();
|
---|
417 | ILSize inDim = A.Size;
|
---|
418 | if (inDim.NumberOfElements == 1) {
|
---|
419 | #region SCALAR
|
---|
420 | // scalar -> return copy
|
---|
421 | if (A.GetValue(0,0) != 0) {
|
---|
422 | if (create_row_columns) {
|
---|
423 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
424 | }
|
---|
425 | if (return_values) {
|
---|
426 | V.a = A.C;
|
---|
427 | }
|
---|
428 | return zeros<double>(1, 1);
|
---|
429 | } else {
|
---|
430 | if (create_row_columns) {
|
---|
431 | C.a = empty<double>(ILSize.Empty00);
|
---|
432 | }
|
---|
433 | if (return_values) {
|
---|
434 | V.a = empty<Int32>(ILSize.Empty00);
|
---|
435 | }
|
---|
436 | return empty<double>(ILSize.Empty00);
|
---|
437 | }
|
---|
438 | #endregion SCALAR
|
---|
439 | }
|
---|
440 | long nrElements = inDim.NumberOfElements;
|
---|
441 |
|
---|
442 | if (limit != 0) {
|
---|
443 | int lim = Math.Abs(limit);
|
---|
444 | if (lim < nrElements)
|
---|
445 | nrElements = lim;
|
---|
446 | }
|
---|
447 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
448 | int foundIdx = 0;
|
---|
449 | // physical -> pointer arithmetic
|
---|
450 | if (limit >= 0) {
|
---|
451 | unsafe {
|
---|
452 | fixed (double* pIndices = indices)
|
---|
453 | fixed ( Int32* pX = A.GetArrayForRead()) {
|
---|
454 |
|
---|
455 | Int32* lastElement = pX + inDim.NumberOfElements;
|
---|
456 |
|
---|
457 | Int32* tmpIn = pX;
|
---|
458 | double* pI = pIndices;
|
---|
459 | double* pFoundLast = pI + indices.Length;
|
---|
460 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
461 | if (*tmpIn != 0)
|
---|
462 | *pI++ = (double)(tmpIn - pX);
|
---|
463 | tmpIn++;
|
---|
464 | }
|
---|
465 | foundIdx = (int)(pI - pIndices);
|
---|
466 | }
|
---|
467 | }
|
---|
468 | } else {
|
---|
469 | // search backwards
|
---|
470 | unsafe {
|
---|
471 | fixed (double* pIndices = indices)
|
---|
472 | fixed ( Int32* pX = A.GetArrayForRead()) {
|
---|
473 |
|
---|
474 | Int32* lastElementX = pX;
|
---|
475 |
|
---|
476 | Int32* tmpIn = pX + inDim.NumberOfElements;
|
---|
477 | double* pI = pIndices + indices.Length;
|
---|
478 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
479 | tmpIn--;
|
---|
480 | if (*tmpIn != 0)
|
---|
481 | *(--pI) = (double)(tmpIn - pX);
|
---|
482 | }
|
---|
483 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
484 | }
|
---|
485 | }
|
---|
486 | }
|
---|
487 | if (foundIdx == 0) {
|
---|
488 | return empty();
|
---|
489 | }
|
---|
490 | // transform to row / columns; extract values if needed
|
---|
491 | int leadDimLen = inDim[0];
|
---|
492 | if (create_row_columns) {
|
---|
493 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
494 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
495 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
496 | if (return_values) {
|
---|
497 | V.a = new ILRetArray< Int32>(ILMemoryPool.Pool.New< Int32>(foundIdx), foundIdx, 1);
|
---|
498 | // copy values, transform to row/columns
|
---|
499 | unsafe {
|
---|
500 | fixed (double* pIndices = indices,
|
---|
501 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
502 | fixed ( Int32* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
503 | double* pI = (limit >= 0) ?
|
---|
504 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
505 | double* pLastIndex = pI + foundIdx;
|
---|
506 | double* pR = pRows;
|
---|
507 | double* pC = pCols;
|
---|
508 |
|
---|
509 | Int32* pV = pValues;
|
---|
510 |
|
---|
511 | Int32* pX = pInput;
|
---|
512 | while (pI < pLastIndex) {
|
---|
513 | *pR++ = *(pI) % leadDimLen;
|
---|
514 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
515 | *pV++ = *(pInput + (int)*pI++);
|
---|
516 | }
|
---|
517 | }
|
---|
518 | }
|
---|
519 | } else {
|
---|
520 | // just return row / columns
|
---|
521 | unsafe {
|
---|
522 | fixed (double* pIndices = indices,
|
---|
523 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
524 | fixed ( Int32* pInput = A.GetArrayForRead()) {
|
---|
525 | double* pI = (limit >= 0) ?
|
---|
526 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
527 | double* pLastIndex = pI + foundIdx;
|
---|
528 | double* pR = pRows;
|
---|
529 | double* pC = pCols;
|
---|
530 | while (pI < pLastIndex) {
|
---|
531 | *pR++ = *(pI) % leadDimLen;
|
---|
532 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 | }
|
---|
537 | #endregion RETURN ROWS / COLUMNS
|
---|
538 | } else {
|
---|
539 | #region RETURN INDICES ONLY
|
---|
540 | if (foundIdx != indices.Length) {
|
---|
541 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
542 | unsafe {
|
---|
543 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
544 | double* pI = (limit >= 0) ?
|
---|
545 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
546 | double* pLastIndex = pI + foundIdx;
|
---|
547 | double* pR = pRows;
|
---|
548 | while (pI < pLastIndex) {
|
---|
549 | *pR++ = *pI++;
|
---|
550 | }
|
---|
551 | }
|
---|
552 | }
|
---|
553 | } else {
|
---|
554 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
555 | }
|
---|
556 | #endregion RETURN INDICES ONLY
|
---|
557 | }
|
---|
558 | return ret;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | /// <summary>
|
---|
562 | /// Find nonzero elements in A
|
---|
563 | /// </summary>
|
---|
564 | /// <param name="A">Input array</param>
|
---|
565 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
566 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
567 | /// Set to 0 to search full array (default).</param>
|
---|
568 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
569 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
570 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
571 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
572 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
573 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
574 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
575 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
576 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
577 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
578 | public static ILRetArray<double> find(ILInArray< float> A, int limit = 0,
|
---|
579 | ILOutArray<double> C = null, ILOutArray< float> V = null) {
|
---|
580 | using (ILScope.Enter(A)) {
|
---|
581 | bool create_row_columns = !Object.Equals(C, null);
|
---|
582 | bool return_values = !Object.Equals(V, null);
|
---|
583 | ILArray<double> ret = empty();
|
---|
584 | ILSize inDim = A.Size;
|
---|
585 | if (inDim.NumberOfElements == 1) {
|
---|
586 | #region SCALAR
|
---|
587 | // scalar -> return copy
|
---|
588 | if (A.GetValue(0,0) != 0.0) {
|
---|
589 | if (create_row_columns) {
|
---|
590 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
591 | }
|
---|
592 | if (return_values) {
|
---|
593 | V.a = A.C;
|
---|
594 | }
|
---|
595 | return zeros<double>(1, 1);
|
---|
596 | } else {
|
---|
597 | if (create_row_columns) {
|
---|
598 | C.a = empty<double>(ILSize.Empty00);
|
---|
599 | }
|
---|
600 | if (return_values) {
|
---|
601 | V.a = empty<float>(ILSize.Empty00);
|
---|
602 | }
|
---|
603 | return empty<double>(ILSize.Empty00);
|
---|
604 | }
|
---|
605 | #endregion SCALAR
|
---|
606 | }
|
---|
607 | long nrElements = inDim.NumberOfElements;
|
---|
608 |
|
---|
609 | if (limit != 0) {
|
---|
610 | int lim = Math.Abs(limit);
|
---|
611 | if (lim < nrElements)
|
---|
612 | nrElements = lim;
|
---|
613 | }
|
---|
614 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
615 | int foundIdx = 0;
|
---|
616 | // physical -> pointer arithmetic
|
---|
617 | if (limit >= 0) {
|
---|
618 | unsafe {
|
---|
619 | fixed (double* pIndices = indices)
|
---|
620 | fixed ( float* pX = A.GetArrayForRead()) {
|
---|
621 |
|
---|
622 | float* lastElement = pX + inDim.NumberOfElements;
|
---|
623 |
|
---|
624 | float* tmpIn = pX;
|
---|
625 | double* pI = pIndices;
|
---|
626 | double* pFoundLast = pI + indices.Length;
|
---|
627 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
628 | if (*tmpIn != 0.0f)
|
---|
629 | *pI++ = (double)(tmpIn - pX);
|
---|
630 | tmpIn++;
|
---|
631 | }
|
---|
632 | foundIdx = (int)(pI - pIndices);
|
---|
633 | }
|
---|
634 | }
|
---|
635 | } else {
|
---|
636 | // search backwards
|
---|
637 | unsafe {
|
---|
638 | fixed (double* pIndices = indices)
|
---|
639 | fixed ( float* pX = A.GetArrayForRead()) {
|
---|
640 |
|
---|
641 | float* lastElementX = pX;
|
---|
642 |
|
---|
643 | float* tmpIn = pX + inDim.NumberOfElements;
|
---|
644 | double* pI = pIndices + indices.Length;
|
---|
645 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
646 | tmpIn--;
|
---|
647 | if (*tmpIn != 0.0f)
|
---|
648 | *(--pI) = (double)(tmpIn - pX);
|
---|
649 | }
|
---|
650 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
651 | }
|
---|
652 | }
|
---|
653 | }
|
---|
654 | if (foundIdx == 0) {
|
---|
655 | return empty();
|
---|
656 | }
|
---|
657 | // transform to row / columns; extract values if needed
|
---|
658 | int leadDimLen = inDim[0];
|
---|
659 | if (create_row_columns) {
|
---|
660 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
661 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
662 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
663 | if (return_values) {
|
---|
664 | V.a = new ILRetArray< float>(ILMemoryPool.Pool.New< float>(foundIdx), foundIdx, 1);
|
---|
665 | // copy values, transform to row/columns
|
---|
666 | unsafe {
|
---|
667 | fixed (double* pIndices = indices,
|
---|
668 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
669 | fixed ( float* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
670 | double* pI = (limit >= 0) ?
|
---|
671 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
672 | double* pLastIndex = pI + foundIdx;
|
---|
673 | double* pR = pRows;
|
---|
674 | double* pC = pCols;
|
---|
675 |
|
---|
676 | float* pV = pValues;
|
---|
677 |
|
---|
678 | float* pX = pInput;
|
---|
679 | while (pI < pLastIndex) {
|
---|
680 | *pR++ = *(pI) % leadDimLen;
|
---|
681 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
682 | *pV++ = *(pInput + (int)*pI++);
|
---|
683 | }
|
---|
684 | }
|
---|
685 | }
|
---|
686 | } else {
|
---|
687 | // just return row / columns
|
---|
688 | unsafe {
|
---|
689 | fixed (double* pIndices = indices,
|
---|
690 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
691 | fixed ( float* pInput = A.GetArrayForRead()) {
|
---|
692 | double* pI = (limit >= 0) ?
|
---|
693 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
694 | double* pLastIndex = pI + foundIdx;
|
---|
695 | double* pR = pRows;
|
---|
696 | double* pC = pCols;
|
---|
697 | while (pI < pLastIndex) {
|
---|
698 | *pR++ = *(pI) % leadDimLen;
|
---|
699 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
700 | }
|
---|
701 | }
|
---|
702 | }
|
---|
703 | }
|
---|
704 | #endregion RETURN ROWS / COLUMNS
|
---|
705 | } else {
|
---|
706 | #region RETURN INDICES ONLY
|
---|
707 | if (foundIdx != indices.Length) {
|
---|
708 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
709 | unsafe {
|
---|
710 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
711 | double* pI = (limit >= 0) ?
|
---|
712 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
713 | double* pLastIndex = pI + foundIdx;
|
---|
714 | double* pR = pRows;
|
---|
715 | while (pI < pLastIndex) {
|
---|
716 | *pR++ = *pI++;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | }
|
---|
720 | } else {
|
---|
721 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
722 | }
|
---|
723 | #endregion RETURN INDICES ONLY
|
---|
724 | }
|
---|
725 | return ret;
|
---|
726 | }
|
---|
727 | }
|
---|
728 | /// <summary>
|
---|
729 | /// Find nonzero elements in A
|
---|
730 | /// </summary>
|
---|
731 | /// <param name="A">Input array</param>
|
---|
732 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
733 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
734 | /// Set to 0 to search full array (default).</param>
|
---|
735 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
736 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
737 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
738 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
739 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
740 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
741 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
742 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
743 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
744 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
745 | public static ILRetArray<double> find(ILInArray< fcomplex> A, int limit = 0,
|
---|
746 | ILOutArray<double> C = null, ILOutArray< fcomplex> V = null) {
|
---|
747 | using (ILScope.Enter(A)) {
|
---|
748 | bool create_row_columns = !Object.Equals(C, null);
|
---|
749 | bool return_values = !Object.Equals(V, null);
|
---|
750 | ILArray<double> ret = empty();
|
---|
751 | ILSize inDim = A.Size;
|
---|
752 | if (inDim.NumberOfElements == 1) {
|
---|
753 | #region SCALAR
|
---|
754 | // scalar -> return copy
|
---|
755 | if (A.GetValue(0,0).real != 0.0 || A.GetValue(0,0).imag != 0.0) {
|
---|
756 | if (create_row_columns) {
|
---|
757 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
758 | }
|
---|
759 | if (return_values) {
|
---|
760 | V.a = A.C;
|
---|
761 | }
|
---|
762 | return zeros<double>(1, 1);
|
---|
763 | } else {
|
---|
764 | if (create_row_columns) {
|
---|
765 | C.a = empty<double>(ILSize.Empty00);
|
---|
766 | }
|
---|
767 | if (return_values) {
|
---|
768 | V.a = empty<fcomplex>(ILSize.Empty00);
|
---|
769 | }
|
---|
770 | return empty<double>(ILSize.Empty00);
|
---|
771 | }
|
---|
772 | #endregion SCALAR
|
---|
773 | }
|
---|
774 | long nrElements = inDim.NumberOfElements;
|
---|
775 |
|
---|
776 | if (limit != 0) {
|
---|
777 | int lim = Math.Abs(limit);
|
---|
778 | if (lim < nrElements)
|
---|
779 | nrElements = lim;
|
---|
780 | }
|
---|
781 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
782 | int foundIdx = 0;
|
---|
783 | // physical -> pointer arithmetic
|
---|
784 | if (limit >= 0) {
|
---|
785 | unsafe {
|
---|
786 | fixed (double* pIndices = indices)
|
---|
787 | fixed ( fcomplex* pX = A.GetArrayForRead()) {
|
---|
788 |
|
---|
789 | fcomplex* lastElement = pX + inDim.NumberOfElements;
|
---|
790 |
|
---|
791 | fcomplex* tmpIn = pX;
|
---|
792 | double* pI = pIndices;
|
---|
793 | double* pFoundLast = pI + indices.Length;
|
---|
794 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
795 | if ((*tmpIn).real != 0.0 || (*tmpIn).imag != 0.0)
|
---|
796 | *pI++ = (double)(tmpIn - pX);
|
---|
797 | tmpIn++;
|
---|
798 | }
|
---|
799 | foundIdx = (int)(pI - pIndices);
|
---|
800 | }
|
---|
801 | }
|
---|
802 | } else {
|
---|
803 | // search backwards
|
---|
804 | unsafe {
|
---|
805 | fixed (double* pIndices = indices)
|
---|
806 | fixed ( fcomplex* pX = A.GetArrayForRead()) {
|
---|
807 |
|
---|
808 | fcomplex* lastElementX = pX;
|
---|
809 |
|
---|
810 | fcomplex* tmpIn = pX + inDim.NumberOfElements;
|
---|
811 | double* pI = pIndices + indices.Length;
|
---|
812 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
813 | tmpIn--;
|
---|
814 | if ((*tmpIn).real != 0.0 || (*tmpIn).imag != 0.0)
|
---|
815 | *(--pI) = (double)(tmpIn - pX);
|
---|
816 | }
|
---|
817 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
818 | }
|
---|
819 | }
|
---|
820 | }
|
---|
821 | if (foundIdx == 0) {
|
---|
822 | return empty();
|
---|
823 | }
|
---|
824 | // transform to row / columns; extract values if needed
|
---|
825 | int leadDimLen = inDim[0];
|
---|
826 | if (create_row_columns) {
|
---|
827 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
828 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
829 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
830 | if (return_values) {
|
---|
831 | V.a = new ILRetArray< fcomplex>(ILMemoryPool.Pool.New< fcomplex>(foundIdx), foundIdx, 1);
|
---|
832 | // copy values, transform to row/columns
|
---|
833 | unsafe {
|
---|
834 | fixed (double* pIndices = indices,
|
---|
835 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
836 | fixed ( fcomplex* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
837 | double* pI = (limit >= 0) ?
|
---|
838 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
839 | double* pLastIndex = pI + foundIdx;
|
---|
840 | double* pR = pRows;
|
---|
841 | double* pC = pCols;
|
---|
842 |
|
---|
843 | fcomplex* pV = pValues;
|
---|
844 |
|
---|
845 | fcomplex* pX = pInput;
|
---|
846 | while (pI < pLastIndex) {
|
---|
847 | *pR++ = *(pI) % leadDimLen;
|
---|
848 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
849 | *pV++ = *(pInput + (int)*pI++);
|
---|
850 | }
|
---|
851 | }
|
---|
852 | }
|
---|
853 | } else {
|
---|
854 | // just return row / columns
|
---|
855 | unsafe {
|
---|
856 | fixed (double* pIndices = indices,
|
---|
857 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
858 | fixed ( fcomplex* pInput = A.GetArrayForRead()) {
|
---|
859 | double* pI = (limit >= 0) ?
|
---|
860 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
861 | double* pLastIndex = pI + foundIdx;
|
---|
862 | double* pR = pRows;
|
---|
863 | double* pC = pCols;
|
---|
864 | while (pI < pLastIndex) {
|
---|
865 | *pR++ = *(pI) % leadDimLen;
|
---|
866 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
867 | }
|
---|
868 | }
|
---|
869 | }
|
---|
870 | }
|
---|
871 | #endregion RETURN ROWS / COLUMNS
|
---|
872 | } else {
|
---|
873 | #region RETURN INDICES ONLY
|
---|
874 | if (foundIdx != indices.Length) {
|
---|
875 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
876 | unsafe {
|
---|
877 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
878 | double* pI = (limit >= 0) ?
|
---|
879 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
880 | double* pLastIndex = pI + foundIdx;
|
---|
881 | double* pR = pRows;
|
---|
882 | while (pI < pLastIndex) {
|
---|
883 | *pR++ = *pI++;
|
---|
884 | }
|
---|
885 | }
|
---|
886 | }
|
---|
887 | } else {
|
---|
888 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
889 | }
|
---|
890 | #endregion RETURN INDICES ONLY
|
---|
891 | }
|
---|
892 | return ret;
|
---|
893 | }
|
---|
894 | }
|
---|
895 | /// <summary>
|
---|
896 | /// Find nonzero elements in A
|
---|
897 | /// </summary>
|
---|
898 | /// <param name="A">Input array</param>
|
---|
899 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
900 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
901 | /// Set to 0 to search full array (default).</param>
|
---|
902 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
903 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
904 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
905 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
906 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
907 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
908 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
909 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
910 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
911 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
912 | public static ILRetArray<double> find(ILInArray< complex> A, int limit = 0,
|
---|
913 | ILOutArray<double> C = null, ILOutArray< complex> V = null) {
|
---|
914 | using (ILScope.Enter(A)) {
|
---|
915 | bool create_row_columns = !Object.Equals(C, null);
|
---|
916 | bool return_values = !Object.Equals(V, null);
|
---|
917 | ILArray<double> ret = empty();
|
---|
918 | ILSize inDim = A.Size;
|
---|
919 | if (inDim.NumberOfElements == 1) {
|
---|
920 | #region SCALAR
|
---|
921 | // scalar -> return copy
|
---|
922 | if (A.GetValue(0,0).real != 0.0 || A.GetValue(0,0).imag != 0.0) {
|
---|
923 | if (create_row_columns) {
|
---|
924 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
925 | }
|
---|
926 | if (return_values) {
|
---|
927 | V.a = A.C;
|
---|
928 | }
|
---|
929 | return zeros<double>(1, 1);
|
---|
930 | } else {
|
---|
931 | if (create_row_columns) {
|
---|
932 | C.a = empty<double>(ILSize.Empty00);
|
---|
933 | }
|
---|
934 | if (return_values) {
|
---|
935 | V.a = empty<complex>(ILSize.Empty00);
|
---|
936 | }
|
---|
937 | return empty<double>(ILSize.Empty00);
|
---|
938 | }
|
---|
939 | #endregion SCALAR
|
---|
940 | }
|
---|
941 | long nrElements = inDim.NumberOfElements;
|
---|
942 |
|
---|
943 | if (limit != 0) {
|
---|
944 | int lim = Math.Abs(limit);
|
---|
945 | if (lim < nrElements)
|
---|
946 | nrElements = lim;
|
---|
947 | }
|
---|
948 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
949 | int foundIdx = 0;
|
---|
950 | // physical -> pointer arithmetic
|
---|
951 | if (limit >= 0) {
|
---|
952 | unsafe {
|
---|
953 | fixed (double* pIndices = indices)
|
---|
954 | fixed ( complex* pX = A.GetArrayForRead()) {
|
---|
955 |
|
---|
956 | complex* lastElement = pX + inDim.NumberOfElements;
|
---|
957 |
|
---|
958 | complex* tmpIn = pX;
|
---|
959 | double* pI = pIndices;
|
---|
960 | double* pFoundLast = pI + indices.Length;
|
---|
961 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
962 | if ((*tmpIn).real != 0.0 || (*tmpIn).imag != 0.0)
|
---|
963 | *pI++ = (double)(tmpIn - pX);
|
---|
964 | tmpIn++;
|
---|
965 | }
|
---|
966 | foundIdx = (int)(pI - pIndices);
|
---|
967 | }
|
---|
968 | }
|
---|
969 | } else {
|
---|
970 | // search backwards
|
---|
971 | unsafe {
|
---|
972 | fixed (double* pIndices = indices)
|
---|
973 | fixed ( complex* pX = A.GetArrayForRead()) {
|
---|
974 |
|
---|
975 | complex* lastElementX = pX;
|
---|
976 |
|
---|
977 | complex* tmpIn = pX + inDim.NumberOfElements;
|
---|
978 | double* pI = pIndices + indices.Length;
|
---|
979 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
980 | tmpIn--;
|
---|
981 | if ((*tmpIn).real != 0.0 || (*tmpIn).imag != 0.0)
|
---|
982 | *(--pI) = (double)(tmpIn - pX);
|
---|
983 | }
|
---|
984 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
985 | }
|
---|
986 | }
|
---|
987 | }
|
---|
988 | if (foundIdx == 0) {
|
---|
989 | return empty();
|
---|
990 | }
|
---|
991 | // transform to row / columns; extract values if needed
|
---|
992 | int leadDimLen = inDim[0];
|
---|
993 | if (create_row_columns) {
|
---|
994 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
995 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
996 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
997 | if (return_values) {
|
---|
998 | V.a = new ILRetArray< complex>(ILMemoryPool.Pool.New< complex>(foundIdx), foundIdx, 1);
|
---|
999 | // copy values, transform to row/columns
|
---|
1000 | unsafe {
|
---|
1001 | fixed (double* pIndices = indices,
|
---|
1002 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
1003 | fixed ( complex* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
1004 | double* pI = (limit >= 0) ?
|
---|
1005 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
1006 | double* pLastIndex = pI + foundIdx;
|
---|
1007 | double* pR = pRows;
|
---|
1008 | double* pC = pCols;
|
---|
1009 |
|
---|
1010 | complex* pV = pValues;
|
---|
1011 |
|
---|
1012 | complex* pX = pInput;
|
---|
1013 | while (pI < pLastIndex) {
|
---|
1014 | *pR++ = *(pI) % leadDimLen;
|
---|
1015 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
1016 | *pV++ = *(pInput + (int)*pI++);
|
---|
1017 | }
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 | } else {
|
---|
1021 | // just return row / columns
|
---|
1022 | unsafe {
|
---|
1023 | fixed (double* pIndices = indices,
|
---|
1024 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
1025 | fixed ( complex* pInput = A.GetArrayForRead()) {
|
---|
1026 | double* pI = (limit >= 0) ?
|
---|
1027 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
1028 | double* pLastIndex = pI + foundIdx;
|
---|
1029 | double* pR = pRows;
|
---|
1030 | double* pC = pCols;
|
---|
1031 | while (pI < pLastIndex) {
|
---|
1032 | *pR++ = *(pI) % leadDimLen;
|
---|
1033 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 | }
|
---|
1037 | }
|
---|
1038 | #endregion RETURN ROWS / COLUMNS
|
---|
1039 | } else {
|
---|
1040 | #region RETURN INDICES ONLY
|
---|
1041 | if (foundIdx != indices.Length) {
|
---|
1042 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
1043 | unsafe {
|
---|
1044 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
1045 | double* pI = (limit >= 0) ?
|
---|
1046 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
1047 | double* pLastIndex = pI + foundIdx;
|
---|
1048 | double* pR = pRows;
|
---|
1049 | while (pI < pLastIndex) {
|
---|
1050 | *pR++ = *pI++;
|
---|
1051 | }
|
---|
1052 | }
|
---|
1053 | }
|
---|
1054 | } else {
|
---|
1055 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
1056 | }
|
---|
1057 | #endregion RETURN INDICES ONLY
|
---|
1058 | }
|
---|
1059 | return ret;
|
---|
1060 | }
|
---|
1061 | }
|
---|
1062 | /// <summary>
|
---|
1063 | /// Find nonzero elements in A
|
---|
1064 | /// </summary>
|
---|
1065 | /// <param name="A">Input array</param>
|
---|
1066 | /// <param name="limit">[Optional] Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
1067 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
1068 | /// Set to 0 to search full array (default).</param>
|
---|
1069 | /// <param name="C">[Optional] If not null, the function will return the row indices of nonzero elements
|
---|
1070 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
1071 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
1072 | /// <param name="V">[Optional] If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
1073 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
1074 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
1075 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
1076 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
1077 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
1078 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
1079 | public static ILRetArray<double> find(ILInArray< byte> A, int limit = 0,
|
---|
1080 | ILOutArray<double> C = null, ILOutArray< byte> V = null) {
|
---|
1081 | using (ILScope.Enter(A)) {
|
---|
1082 | bool create_row_columns = !Object.Equals(C, null);
|
---|
1083 | bool return_values = !Object.Equals(V, null);
|
---|
1084 | ILArray<double> ret = empty();
|
---|
1085 | ILSize inDim = A.Size;
|
---|
1086 | if (inDim.NumberOfElements == 1) {
|
---|
1087 | #region SCALAR
|
---|
1088 | // scalar -> return copy
|
---|
1089 | if (A.GetValue(0,0) != 0) {
|
---|
1090 | if (create_row_columns) {
|
---|
1091 | C.a = zeros<double>(ILSize.Scalar1_1);
|
---|
1092 | }
|
---|
1093 | if (return_values) {
|
---|
1094 | V.a = A.C;
|
---|
1095 | }
|
---|
1096 | return zeros<double>(1, 1);
|
---|
1097 | } else {
|
---|
1098 | if (create_row_columns) {
|
---|
1099 | C.a = empty<double>(ILSize.Empty00);
|
---|
1100 | }
|
---|
1101 | if (return_values) {
|
---|
1102 | V.a = empty<byte>(ILSize.Empty00);
|
---|
1103 | }
|
---|
1104 | return empty<double>(ILSize.Empty00);
|
---|
1105 | }
|
---|
1106 | #endregion SCALAR
|
---|
1107 | }
|
---|
1108 | long nrElements = inDim.NumberOfElements;
|
---|
1109 |
|
---|
1110 | if (limit != 0) {
|
---|
1111 | int lim = Math.Abs(limit);
|
---|
1112 | if (lim < nrElements)
|
---|
1113 | nrElements = lim;
|
---|
1114 | }
|
---|
1115 | double[] indices = ILMemoryPool.Pool.New<double>(nrElements); // init return array with most elements for non logical inarray -> shorten afterwards
|
---|
1116 | int foundIdx = 0;
|
---|
1117 | // physical -> pointer arithmetic
|
---|
1118 | if (limit >= 0) {
|
---|
1119 | unsafe {
|
---|
1120 | fixed (double* pIndices = indices)
|
---|
1121 | fixed ( byte* pX = A.GetArrayForRead()) {
|
---|
1122 |
|
---|
1123 | byte* lastElement = pX + inDim.NumberOfElements;
|
---|
1124 |
|
---|
1125 | byte* tmpIn = pX;
|
---|
1126 | double* pI = pIndices;
|
---|
1127 | double* pFoundLast = pI + indices.Length;
|
---|
1128 | while (tmpIn < lastElement && pI < pFoundLast) {
|
---|
1129 | if (*tmpIn != 0)
|
---|
1130 | *pI++ = (double)(tmpIn - pX);
|
---|
1131 | tmpIn++;
|
---|
1132 | }
|
---|
1133 | foundIdx = (int)(pI - pIndices);
|
---|
1134 | }
|
---|
1135 | }
|
---|
1136 | } else {
|
---|
1137 | // search backwards
|
---|
1138 | unsafe {
|
---|
1139 | fixed (double* pIndices = indices)
|
---|
1140 | fixed ( byte* pX = A.GetArrayForRead()) {
|
---|
1141 |
|
---|
1142 | byte* lastElementX = pX;
|
---|
1143 |
|
---|
1144 | byte* tmpIn = pX + inDim.NumberOfElements;
|
---|
1145 | double* pI = pIndices + indices.Length;
|
---|
1146 | while (tmpIn > lastElementX && pI > pIndices) {
|
---|
1147 | tmpIn--;
|
---|
1148 | if (*tmpIn != 0)
|
---|
1149 | *(--pI) = (double)(tmpIn - pX);
|
---|
1150 | }
|
---|
1151 | foundIdx = (int)(pIndices + indices.Length - pI);
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 | if (foundIdx == 0) {
|
---|
1156 | return empty();
|
---|
1157 | }
|
---|
1158 | // transform to row / columns; extract values if needed
|
---|
1159 | int leadDimLen = inDim[0];
|
---|
1160 | if (create_row_columns) {
|
---|
1161 | #region RETURN ROWS / COLUMNS /VALUES
|
---|
1162 | C.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
1163 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
1164 | if (return_values) {
|
---|
1165 | V.a = new ILRetArray< byte>(ILMemoryPool.Pool.New< byte>(foundIdx), foundIdx, 1);
|
---|
1166 | // copy values, transform to row/columns
|
---|
1167 | unsafe {
|
---|
1168 | fixed (double* pIndices = indices,
|
---|
1169 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
1170 | fixed ( byte* pValues = V.GetArrayForWrite(), pInput = A.GetArrayForRead()) {
|
---|
1171 | double* pI = (limit >= 0) ?
|
---|
1172 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
1173 | double* pLastIndex = pI + foundIdx;
|
---|
1174 | double* pR = pRows;
|
---|
1175 | double* pC = pCols;
|
---|
1176 |
|
---|
1177 | byte* pV = pValues;
|
---|
1178 |
|
---|
1179 | byte* pX = pInput;
|
---|
1180 | while (pI < pLastIndex) {
|
---|
1181 | *pR++ = *(pI) % leadDimLen;
|
---|
1182 | *pC++ = (int)*(pI) / leadDimLen;
|
---|
1183 | *pV++ = *(pInput + (int)*pI++);
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 | } else {
|
---|
1188 | // just return row / columns
|
---|
1189 | unsafe {
|
---|
1190 | fixed (double* pIndices = indices,
|
---|
1191 | pRows = ret.GetArrayForWrite(), pCols = C.GetArrayForWrite())
|
---|
1192 | fixed ( byte* pInput = A.GetArrayForRead()) {
|
---|
1193 | double* pI = (limit >= 0) ?
|
---|
1194 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
1195 | double* pLastIndex = pI + foundIdx;
|
---|
1196 | double* pR = pRows;
|
---|
1197 | double* pC = pCols;
|
---|
1198 | while (pI < pLastIndex) {
|
---|
1199 | *pR++ = *(pI) % leadDimLen;
|
---|
1200 | *pC++ = (int)(*(pI++) / leadDimLen);
|
---|
1201 | }
|
---|
1202 | }
|
---|
1203 | }
|
---|
1204 | }
|
---|
1205 | #endregion RETURN ROWS / COLUMNS
|
---|
1206 | } else {
|
---|
1207 | #region RETURN INDICES ONLY
|
---|
1208 | if (foundIdx != indices.Length) {
|
---|
1209 | ret.a = new ILRetArray<double>(ILMemoryPool.Pool.New<double>(foundIdx), foundIdx, 1);
|
---|
1210 | unsafe {
|
---|
1211 | fixed (double* pIndices = indices, pRows = ret.GetArrayForWrite()) {
|
---|
1212 | double* pI = (limit >= 0) ?
|
---|
1213 | pIndices : (pIndices + indices.Length - foundIdx);
|
---|
1214 | double* pLastIndex = pI + foundIdx;
|
---|
1215 | double* pR = pRows;
|
---|
1216 | while (pI < pLastIndex) {
|
---|
1217 | *pR++ = *pI++;
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | } else {
|
---|
1222 | ret.a = new ILRetArray<double>(indices, foundIdx, 1);
|
---|
1223 | }
|
---|
1224 | #endregion RETURN INDICES ONLY
|
---|
1225 | }
|
---|
1226 | return ret;
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | #endregion HYCALPER AUTO GENERATED CODE
|
---|
1231 |
|
---|
1232 | #region logicals
|
---|
1233 | /// <summary>
|
---|
1234 | /// Find nonzero elements in A
|
---|
1235 | /// </summary>
|
---|
1236 | /// <param name="A">Input array</param>
|
---|
1237 | /// <param name="limit">Number of elements to search for. If this value is <![CDATA[< 0]]> the function
|
---|
1238 | /// will return at most 'limit' nonzero elements from the end of the array ordered by ascending index.
|
---|
1239 | /// Set to 0 to search full array (default).</param>
|
---|
1240 | /// <param name="C">If not null, the function will return the row indices of nonzero elements
|
---|
1241 | /// as main return value. C will therefore hold the column indices of those elements. If A
|
---|
1242 | /// has more than 2 dimensions, the column indices will go along the 2nd dimension.</param>
|
---|
1243 | /// <param name="V">If not null on entrance, V will hold a copy of the values of nonzero elements returned.</param>
|
---|
1244 | /// <returns>Vector containing (sequential) indices of nonzero elements in A. If C was
|
---|
1245 | /// not null, return value will contain row indices of nonzero elements. </returns>
|
---|
1246 | /// <remarks>The return type of the index vectors is always 'double'. The return type
|
---|
1247 | /// of the element vector 'V' depends on the type of input array A. V and C may be null on
|
---|
1248 | /// entrance, indicating their information is not needed. If V is not null (e.g. 'empty()') C must be
|
---|
1249 | /// not null also. Any initial data of V or C will be lost.</remarks>
|
---|
1250 | public static ILRetArray<double> find(ILInLogical A, int limit,
|
---|
1251 | ILOutArray<double> C, ILOutLogical V) {
|
---|
1252 | using (ILScope.Enter(A)) {
|
---|
1253 | if (isnullorempty(A)) {
|
---|
1254 | if (!isnull(V)) {
|
---|
1255 | V.a = new ILLogical(ILSize.Empty00);
|
---|
1256 | }
|
---|
1257 | return empty();
|
---|
1258 | }
|
---|
1259 | ILArray<byte> tmpA = new ILArray<byte>(A.Storage);
|
---|
1260 | ILArray<byte> tmpV = null;
|
---|
1261 | if (!isnull(V)) {
|
---|
1262 | tmpV = new ILArray<byte>(V.Storage);
|
---|
1263 | }
|
---|
1264 | ILArray<double> ret = find(tmpA, limit, C, tmpV);
|
---|
1265 | if (!isnull(tmpV)) {
|
---|
1266 | V.a = new ILLogical(new ILLogicalStorage( tmpV.Storage.GetDataArray(), tmpV.S));
|
---|
1267 | }
|
---|
1268 | return ret;
|
---|
1269 | }
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | #endregion
|
---|
1273 |
|
---|
1274 | }
|
---|
1275 | }
|
---|