File: Find.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (269 lines) | stat: -rw-r--r-- 7,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (c) 2009 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "Array.hpp"
#include "SparseCCS.hpp"
#include "Algorithms.hpp"
#include "Math.hpp"

static inline index_t CountNonZeros(const BasicArray<bool> &dp) {
  index_t nonZero = 0;
  for (index_t i=1;i<=dp.length();i++)
    if (dp[i]) nonZero++;
  return nonZero;
}

static ArrayVector SingleFindModeFull(const Array &arg) {
  Array x(arg.toClass(Bool).asDenseArray());
  const BasicArray<bool> &dp(x.constReal<bool>());
  index_t nonZero = CountNonZeros(dp);
  if ((nonZero == 0) && (x.is2D()))
    return ArrayVector(Array(Double));
  NTuple retdims;
  if (x.isRowVector())
    retdims = NTuple(1,nonZero);
  else
    retdims = NTuple(nonZero,1);
  BasicArray<index_t> sp(retdims);
  index_t j = 1;
  for (index_t i=1;i<=dp.length();i++)
    if (dp[i]) 
      sp[j++] = i;
  return ArrayVector(Array(sp));
}
  
static ArrayVector RCFindModeFull(const Array &arg) {
  Array x(arg.toClass(Bool).asDenseArray());
  const BasicArray<bool> &dp(x.constReal<bool>());
  index_t nonZero = CountNonZeros(dp);
  if ((nonZero == 0) && (x.is2D())) {
    ArrayVector retval;
    retval.push_back(Array(Double));
    retval.push_back(Array(Double));
    return retval;
  }
  NTuple retdims;
  if (x.isRowVector())
    retdims = NTuple(1,nonZero);
  else
    retdims = NTuple(nonZero,1);
  BasicArray<index_t> rp(retdims);
  BasicArray<index_t> cp(retdims);
  index_t j = 1;
  index_t rows = arg.rows();
  for (index_t i=1;i<=dp.length();i++)
    if (dp[i]) {
      rp[j] = (uint64(i-1) % uint64(rows)) + 1;
      cp[j] = (uint64(i-1) / uint64(rows)) + 1;
      j++;
    }
  ArrayVector retval;
  retval.push_back(Array(rp));
  retval.push_back(Array(cp));
  return retval;
}

template <class T>
static ArrayVector RCVFindModeFullReal(Array arg) {
  Array x(arg.toClass(Bool).asDenseArray());
  const BasicArray<bool> &dp(x.constReal<bool>());
  const BasicArray<T> &vp(arg.constReal<T>());
  index_t nonZero = CountNonZeros(dp);
  if ((nonZero == 0) && (x.is2D())) {
    ArrayVector retval;
    retval.push_back(Array(Double));
    retval.push_back(Array(Double));
    Array wp(GetDataClass(T(0)));
    if ((wp.dataClass() != Bool) && (wp.dataClass() != Float)
	&& (wp.dataClass() != Double))
      wp = wp.toClass(Double);
    retval.push_back(wp);
    return retval;
  }
  NTuple retdims;
  if (x.isRowVector())
    retdims = NTuple(1,nonZero);
  else
    retdims = NTuple(nonZero,1);
  BasicArray<index_t> rp(retdims);
  BasicArray<index_t> cp(retdims);
  BasicArray<T> qp(retdims);
  index_t j = 1;
  index_t rows = arg.rows();
  for (index_t i=1;i<=dp.length();i++)
    if (dp[i]) {
      rp[j] = (uint64(i-1) % uint64(rows)) + 1;
      cp[j] = (uint64(i-1) / uint64(rows)) + 1;
      qp[j] = vp[i];
      j++;
    }
  ArrayVector retval;
  retval.push_back(Array(rp));
  retval.push_back(Array(cp));
  Array zp(qp);
  if ((zp.dataClass() != Bool) && (zp.dataClass() != Float)
      && (zp.dataClass() != Double))
    zp = zp.toClass(Double);
  retval.push_back(zp);
  return retval;
}  

template <class T>
static ArrayVector RCVFindModeFullComplex(Array arg) {
  Array x(arg.toClass(Bool).asDenseArray());
  const BasicArray<bool> &dp(x.constReal<bool>());
  const BasicArray<T> &vp(arg.constReal<T>());
  const BasicArray<T> &zp(arg.constImag<T>());
  index_t nonZero = CountNonZeros(dp);
  if ((nonZero == 0) && (x.is2D())) {
    ArrayVector retval;
    retval.push_back(Array(Double));
    retval.push_back(Array(Double));
    Array wp(GetDataClass(T(0)));
    if ((wp.dataClass() != Bool) && (wp.dataClass() != Float)
	&& (wp.dataClass() != Double))
      wp = wp.toClass(Double);
    retval.push_back(wp);
    return retval;
  }
  NTuple retdims;
  if (x.isRowVector())
    retdims = NTuple(1,nonZero);
  else
    retdims = NTuple(nonZero,1);
  BasicArray<index_t> rp(retdims);
  BasicArray<index_t> cp(retdims);
  BasicArray<T> qp(retdims);
  BasicArray<T> ip(retdims);
  index_t j = 1;
  index_t rows = arg.rows();
  for (index_t i=1;i<=dp.length();i++)
    if (dp[i]) {
      rp[j] = (uint64(i-1) % uint64(rows)) + 1;
      cp[j] = (uint64(i-1) / uint64(rows)) + 1;
      qp[j] = vp[i];
      ip[j] = zp[i];
      j++;
    }
  ArrayVector retval;
  retval.push_back(Array(rp));
  retval.push_back(Array(cp));
  Array wp(qp,ip);
  if ((wp.dataClass() != Bool) && (wp.dataClass() != Float)
      && (wp.dataClass() != Double))
    wp = wp.toClass(Double);
  retval.push_back(wp);
  return retval;
}

template <class T>
static ArrayVector RCVFindModeFull(Array x) {
  if (x.allReal())
    return RCVFindModeFullReal<T>(x);
  else
    return RCVFindModeFullComplex<T>(x);
}

#define MacroFindFull(ctype,cls) \
  case cls: return RCVFindModeFull<ctype>(x);
  
static ArrayVector RCVFindModeFull(Array x) {
  switch (x.dataClass()) {
  default: throw Exception("find not defined for reference types");
    MacroExpandCasesAll(MacroFindFull);
  }
}

static ArrayVector FindModeSparse(Array x, int nargout) {
  Array xrows, xcols, xdata;
  xdata = SparseToIJV(x,xrows,xcols);
  NTuple retDim;
  index_t nnz = xdata.length();
  if (x.isRowVector())
    retDim = NTuple(1,nnz);
  else
    retDim = NTuple(nnz,1);
  ArrayVector retval;
  if (nargout == 3) {
    retval.push_back(xrows);
    retval.push_back(xcols);
    retval.push_back(xdata);
  } else if (nargout == 2) {
    retval.push_back(xrows);
    retval.push_back(xcols);
  } else {
    retval.push_back(Add(Multiply(Subtract(xcols,Array(double(1))),
				  Array(double(x.rows()))),xrows));
  }
  return retval;
}
  
//@@Signature
//function find FindFunction
//inputs x keep flag
//outputs row col vals
//DOCBLOCK array_find

static ArrayVector FindTrim(ArrayVector a, int cnt, bool first_flag) {
  if (cnt < 0) return a;
  if (a.size() == 0) return a;
  int N = int(a[0].length());
  if (N == 0) return a;
  if (cnt > N) return a;
  ArrayVector ret;
  Array ndx;
  bool vertflag = !(a[0].isRowVector());
  if (first_flag)
    ndx = RangeConstructor(1,1,cnt,vertflag);
  else
    ndx = RangeConstructor((N-cnt)+1,1,N,vertflag);
  for (int i=0;i<a.size();i++) 
    ret.push_back(a[i].get(ndx));
  return ret;
}

ArrayVector FindFunction(int nargout, const ArrayVector& arg) {
  // Detect the Find mode...
  if (arg.size() < 1)
    throw Exception("find function takes at least one argument");
  Array tmp(arg[0]);
  if (tmp.isScalar()) tmp = tmp.asDenseArray();
  int k = -1;
  bool first_flag = true;
  if (arg.size() > 1)
    k  = arg[1].asInteger();
  if (arg.size() == 3) {
    QString flag = arg[2].asString().toLower();
    if (flag=="first")
      first_flag = true;
    else if (flag=="last")
      first_flag = false;
    else
      throw Exception("third option to find must be either 'first' or 'last'");
  }
  if ((nargout <= 1) && !tmp.isSparse())
    return FindTrim(SingleFindModeFull(tmp),k,first_flag);
  if ((nargout == 2) && !tmp.isSparse())
    return FindTrim(RCFindModeFull(tmp),k,first_flag);
  if ((nargout == 3) && !tmp.isSparse())
    return FindTrim(RCVFindModeFull(tmp),k,first_flag);
  if (nargout > 3)
    throw Exception("Do not understand syntax of find call (too many output arguments).");
  return FindTrim(FindModeSparse(tmp,nargout),k,first_flag);
}