File: Array.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 (573 lines) | stat: -rw-r--r-- 17,800 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * 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 "Struct.hpp"
#include "MemPtr.hpp"
#include <QtCore>
#include "Algorithms.hpp"
#include "FuncPtr.hpp"
#include "AnonFunc.hpp"

//@@Signature
//function permute PermuteFunction jitsafe
//inputs x p
//outputs y
//DOCBLOCK array_permute
ArrayVector PermuteFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 2) throw Exception("permute requires 2 inputs, the array to permute, and the permutation vector");
  Array permutation(arg[1].asDenseArray().toClass(UInt32));
  const BasicArray<uint32> &perm_dp(permutation.constReal<uint32>());
  uint32 max_perm_value = MaxValue(perm_dp);
  uint32 min_perm_value = MinValue(perm_dp);
  if ((max_perm_value != permutation.length()) || (min_perm_value != 1))
    throw Exception("second argument is not a valid permutation");
  MemBlock<bool> p(max_perm_value);
  bool *d = &p;
  for (index_t i=1;i<=perm_dp.length();i++) 
    d[perm_dp[i]-1] = true;
  for (uint32 i=0;i<max_perm_value;i++)
    if (!d[i]) throw Exception("second argument is not a valid permutation");
  // Convert to an N-Tuple
  NTuple perm(ConvertArrayToNTuple(permutation));
  // Post-fill the N-Tuple so that the permutation covers all of the dimensions
  for (int i=permutation.length();i<NDims;i++)
    perm[i] = (i+1);
  return ArrayVector(Permute(arg[0],perm));
}

//@@Signature
//function repmat RepMatFunction jitsafe
//inputs x rows cols
//outputs y
//DOCBLOCK array_repmat

template <typename T>
static BasicArray<T> RepMat(const BasicArray<T> &dp, const NTuple &outdim, const NTuple &repcount) {
  // Copy can work by pushing or by pulling.  I have opted for
  // pushing, because we can push a column at a time, which might
  // be slightly more efficient.
  index_t colsize = dp.rows();
  index_t colcount = dp.length()/colsize;
  // copySelect stores which copy we are pushing.
  NTuple originalSize(dp.dimensions());
  NTuple copySelect(1,1);
  // anchor is used to calculate where this copy lands in the output matrix
  // sourceAddress is used to track which column we are pushing in the
  // source matrix
  index_t copyCount = repcount.count();
  BasicArray<T> x(outdim);
  for (index_t i=1;i<=copyCount;i++) {
    // Reset the source address
    NTuple sourceAddress(1,1);
    // Next, we loop over the columns of the source matrix
    for (index_t j=1;j<=colcount;j++) {
      NTuple anchor;
      // We can calculate the anchor of this copy by multiplying the source
      // address by the copySelect vector
      for (int k=0;k<NDims;k++)
	anchor[k] = (copySelect[k]-1)*originalSize[k]+sourceAddress[k];
      // Now, we map this to a point in the destination array
      index_t destanchor = outdim.map(anchor);
      // And copy the elements
      for (index_t n=1;n<=colsize;n++)
	x[destanchor+n-1] = dp[(j-1)*colsize+n];
      // Now increment the source address
      originalSize.increment(sourceAddress,0);
    }
    repcount.increment(copySelect);
  }
  return x;
}

template <typename T>
static SparseMatrix<T> RepMat(const SparseMatrix<T>& dp, const NTuple &outdim, 
			      const NTuple &repcount) {
  if (repcount.lastNotOne() > 2)
    throw Exception("repmat cannot create N-dimensional sparse arrays");
  SparseMatrix<T> retvec(outdim);
  for (int rowcopy=0;rowcopy < repcount[0];rowcopy++)
    for (int colcopy=0;colcopy < repcount[1];colcopy++) {
      ConstSparseIterator<T> iter(&dp);
      while (iter.isValid()) {
	retvec.set(NTuple(iter.row()+rowcopy*dp.rows(),
			  iter.col()+colcopy*dp.cols()),
		   iter.value());
	iter.next();
      }
    }
  return retvec;
}

template <typename T>
static Array RepMat(const Array &dp, const NTuple &outdim, const NTuple &repcount) {
  if (dp.isScalar()) {
    if (dp.allReal()) 
      return Array(Uniform(outdim,dp.constRealScalar<T>()));
    else
      return Array(Uniform(outdim,dp.constRealScalar<T>()),
		   Uniform(outdim,dp.constImagScalar<T>()));
  }
  if (dp.isSparse()) {
    if (dp.allReal())
      return Array(RepMat(dp.constRealSparse<T>(),outdim,repcount));
    else
      return Array(RepMat(dp.constRealSparse<T>(),outdim,repcount),
		   RepMat(dp.constImagSparse<T>(),outdim,repcount));
  }
  if (dp.allReal())
    return Array(RepMat(dp.constReal<T>(),outdim,repcount));
  else
    return Array(RepMat(dp.constReal<T>(),outdim,repcount),
		 RepMat(dp.constImag<T>(),outdim,repcount));
}

static Array RepMatCell(const Array &dp, const NTuple &outdim, const NTuple &repcount) {
  return Array(RepMat<Array>(dp.constReal<Array>(),outdim,repcount));
}

static Array RepMatStruct(const StructArray& dp, const NTuple &outdim, const NTuple &repcount) {
  StructArray ret(dp);
  for (int i=0;i<ret.fieldCount();i++)
    ret[i] = RepMat<Array>(ret[i],outdim,repcount);
  ret.updateDims();
  return Array(ret);
}

#define MacroRepMat(ctype,cls)					\
  case cls: return ArrayVector(RepMat<ctype>(x,outdims,repcount));

ArrayVector RepMatFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 2)
    throw Exception("repmat function requires at least two arguments");
  Array x(arg[0]);
  NTuple repcount;
  // Case 1, look for a scalar second argument
  if ((arg.size() == 2) && (arg[1].isScalar())) {
    Array t(arg[1]);
    repcount[0] = t.asInteger();
    repcount[1] = t.asInteger();
  } 
  // Case 2, look for two scalar arguments
  else if ((arg.size() == 3) && (arg[1].isScalar()) && (arg[2].isScalar())) {
    repcount[0] = arg[1].asInteger();
    repcount[1] = arg[2].asInteger();
  }
  // Case 3, look for a vector second argument
  else {
    if (arg.size() > 2) throw Exception("unrecognized form of arguments for repmat function");
    repcount = ConvertArrayToNTuple(arg[1]);
  }
  if (!repcount.isValid())
    throw Exception("negative replication counts not allowed in argument to repmat function");
  // All is peachy.  Allocate an output array of sufficient size.
  NTuple outdims;
  for (int i=0;i<NDims;i++)
    outdims[i] = x.dimensions()[i]*repcount[i];
  if (x.isEmpty()) {
    Array p(arg[0]);
    p.reshape(outdims);
    return ArrayVector(p);
  }
  switch (x.dataClass()) {
  default: throw Exception("Unhandled type for repmat");
    MacroExpandCasesNoCell(MacroRepMat);
  case CellArray:
    return ArrayVector(RepMatCell(x,outdims,repcount));
  case Struct:
    return ArrayVector(RepMatStruct(x.constStructPtr(),outdims,repcount));
  }
}

//@@Signature
//function diag DiagFunction jitsafe
//inputs x n
//outputs y
//DOCBLOCK array_diag
ArrayVector DiagFunction(int nargout, const ArrayVector& arg) {
  // First, get the diagonal order, and synthesize it if it was
  // not supplied
  int diagonalOrder;
  if (arg.size() == 0)
    throw Exception("diag requires at least one argument.\n");
  if (arg.size() == 1) 
    diagonalOrder = 0;
  else {
    if (!arg[1].isScalar()) 
      throw Exception("second argument must be a scalar.\n");
    diagonalOrder = arg[1].asInteger();
  }
  // Next, make sure the first argument is 2D
  if (!arg[0].is2D()) 
    throw Exception("First argument to 'diag' function must be 2D.\n");
  // Case 1 - if the number of columns in a is 1, then this is a diagonal
  // constructor call.
  if (arg[0].isVector())
    {
      Array a = arg[0];
      a.ensureNotScalarEncoded();
      return ArrayVector(DiagonalArray(a,diagonalOrder));
    }
  else
    return ArrayVector(GetDiagonal(arg[0],diagonalOrder));
}

//@@Signature
//sfunction cellfun CellFunFunction
//inputs varargin
//output varargout
//DOCBLOCK array_cellfun
static ArrayVector CellFunNonuniformAnon(int nargout, const ArrayVector &arg,
					  Interpreter *eval, NTuple argdims,
					  int argcount, Array fun)
{
  Array outputs(CellArray, argdims);
  BasicArray<Array> &op = outputs.real<Array>();
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      input.push_back(fun);
      for (int j=1;j<argcount;j++)
	input.push_back(ArrayFromCellArray(arg[j].get(i+1)));
      ArrayVector ret = AnonFuncFevalFunction(nargout,input,eval);
      Array g = CellArrayFromArrayVector(ret);
      op[i+1] = g;
    }
  return outputs;  
}

static ArrayVector CellFunNonuniform(int nargout, const ArrayVector &arg,
				      Interpreter *eval, NTuple argdims,
				      int argcount, FuncPtr fptr)
{
  Array outputs(CellArray, argdims);
  BasicArray<Array> &op = outputs.real<Array>();
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      for (int j=1;j<argcount;j++)
	input.push_back(ArrayFromCellArray(arg[j].get(i+1)));
      ArrayVector ret = fptr->evaluateFunc(eval,input,fptr->outputArgCount());
      Array g = CellArrayFromArrayVector(ret);
      op[i+1] = g;
    }
  return outputs;  
}

static ArrayVector CellFunUniformAnon(int nargout, const ArrayVector &arg,
				       Interpreter *eval, NTuple argdims,
				       int argcount, Array fun)
{
  ArrayVector outputs;
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      input.push_back(fun);
      for (int j=1;j<argcount;j++)
	input.push_back(ArrayFromCellArray(arg[j].get(i+1)));
      ArrayVector ret = AnonFuncFevalFunction(nargout,input,eval);
      if (ret.size() < nargout)
	throw Exception("function returned fewer outputs than expected");
      if (i==0)
	for (int j=0;j<nargout;j++)
	  {
	    if (!ret[j].isScalar()) throw Exception("function returned non-scalar result");
	    outputs.push_back(ret[j]);
	    outputs[j].resize(argdims);
	  }
      else
	for (int j=0;j<nargout;j++)
	  outputs[j].set(i+1,ret[j]);
    }
  return outputs;
}

static ArrayVector CellFunUniform(int nargout, const ArrayVector &arg,
				   Interpreter *eval, NTuple argdims,
				   int argcount, FuncPtr fptr)
{
  ArrayVector outputs;
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      for (int j=1;j<argcount;j++)
	input.push_back(ArrayFromCellArray(arg[j].get(i+1)));
      ArrayVector ret = fptr->evaluateFunc(eval,input,nargout);
      if (ret.size() < nargout)
	throw Exception("function returned fewer outputs than expected");
      if (i==0)
	for (int j=0;j<nargout;j++)
	  {
	    if (!ret[j].isScalar()) throw Exception("function returned non-scalar result");
	    outputs.push_back(ret[j]);
	    outputs[j].resize(argdims);
	  }
      else
	for (int j=0;j<nargout;j++)
	  outputs[j].set(i+1,ret[j]);
    }
  return outputs;
}

ArrayVector CellFunFunction(int nargout, const ArrayVector& arg, 
			     Interpreter*eval) {
  if (arg.size() < 2) return ArrayVector(); // Don't bother
  // Remove the key/value properties
  int argcount = arg.size();
  Array errorHandler;
  bool uniformOutput = true; // We assume this to be the case
  bool foundNVP = true;
  bool customEH = false;
  while (foundNVP && (argcount >=2))
    {
      foundNVP = false;
      if (arg[argcount-2].isString() &&
	  (arg[argcount-2].asString() == "UniformOutput"))
	{
	  uniformOutput = arg[argcount-1].asInteger();
	  argcount-=2;
	  foundNVP = true;
	}
      if (arg[argcount-2].isString() &&
	  (arg[argcount-2].asString() == "ErrorHandler"))
	{
	  errorHandler = arg[argcount-1];
	  customEH = true;
	  argcount-=2;
	  foundNVP = true;
	}
    }
  if (argcount < 2) return ArrayVector();
  NTuple argdims = arg[1].dimensions();
  for (int i=1;i<argcount;i++)
    {
      if (arg[i].dimensions() != argdims)
	throw Exception("All arguments must match dimensions");
      if (arg[i].dataClass() != CellArray)
	throw Exception("All arguments must be cell arrays");
    }
  FuncPtr eh;
  if (customEH) 
    {
      eh = FuncPtrLookup(eval,errorHandler);
      eh->updateCode(eval);
      eval->setTryCatchActive(true);
    }
  try
    {
      if (arg[0].className() == "anonfunction")
	{
	  if (nargout == 0) nargout = 1;
	  if (uniformOutput)
	    return CellFunUniformAnon(nargout,arg,eval,argdims,argcount,arg[0]);
	  return CellFunNonuniformAnon(nargout,arg,eval,argdims,argcount,arg[0]);
	}
      else
	{
	  // Map the first argument to a function ptr
	  FuncPtr fptr = FuncPtrLookup(eval,arg[0]);
	  fptr->updateCode(eval);
	  if (nargout == 0) nargout = 1;
	  if (uniformOutput)
	    return CellFunUniform(nargout,arg,eval,argdims,argcount,fptr);
	  return CellFunNonuniform(nargout,arg,eval,argdims,argcount,fptr);
	}
    }
  catch (Exception &e)
    {
      if (customEH)
	{
	  ArrayVector input;
	  return eh->evaluateFunc(eval,input,1);
	}
      else
	throw;
    }
}


//@@Signature
//sfunction arrayfun ArrayFunFunction
//inputs varargin
//output varargout
//DOCBLOCK array_arrayfun

static ArrayVector ArrayFunNonuniformAnon(int nargout, const ArrayVector &arg,
					  Interpreter *eval, NTuple argdims,
					  int argcount, Array fun)
{
  Array outputs(CellArray, argdims);
  BasicArray<Array> &op = outputs.real<Array>();
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      input.push_back(fun);
      for (int j=1;j<argcount;j++)
	input.push_back(arg[j].get(i+1));
      ArrayVector ret = AnonFuncFevalFunction(nargout,input,eval);
      Array g = CellArrayFromArrayVector(ret);
      op[i+1] = g;
    }
  return outputs;  
}

static ArrayVector ArrayFunNonuniform(int nargout, const ArrayVector &arg,
				      Interpreter *eval, NTuple argdims,
				      int argcount, FuncPtr fptr)
{
  Array outputs(CellArray, argdims);
  BasicArray<Array> &op = outputs.real<Array>();
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      for (int j=1;j<argcount;j++)
	input.push_back(arg[j].get(i+1));
      ArrayVector ret = fptr->evaluateFunc(eval,input,fptr->outputArgCount());
      Array g = CellArrayFromArrayVector(ret);
      op[i+1] = g;
    }
  return outputs;  
}

static ArrayVector ArrayFunUniformAnon(int nargout, const ArrayVector &arg,
				       Interpreter *eval, NTuple argdims,
				       int argcount, Array fun)
{
  ArrayVector outputs;
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      input.push_back(fun);
      for (int j=1;j<argcount;j++)
	input.push_back(arg[j].get(i+1));
      ArrayVector ret = AnonFuncFevalFunction(nargout,input,eval);
      if (ret.size() < nargout)
	throw Exception("function returned fewer outputs than expected");
      if (i==0)
	for (int j=0;j<nargout;j++)
	  {
	    if (!ret[j].isScalar()) throw Exception("function returned non-scalar result");
	    outputs.push_back(ret[j]);
	    outputs[j].resize(argdims);
	  }
      else
	for (int j=0;j<nargout;j++)
	  outputs[j].set(i+1,ret[j]);
    }
  return outputs;
}

static ArrayVector ArrayFunUniform(int nargout, const ArrayVector &arg,
				   Interpreter *eval, NTuple argdims,
				   int argcount, FuncPtr fptr)
{
  ArrayVector outputs;
  for (int i=0;i<argdims.count();i++)
    {
      ArrayVector input;
      for (int j=1;j<argcount;j++)
	input.push_back(arg[j].get(i+1));
      ArrayVector ret = fptr->evaluateFunc(eval,input,nargout);
      if (ret.size() < nargout)
	throw Exception("function returned fewer outputs than expected");
      if (i==0)
	for (int j=0;j<nargout;j++)
	  {
	    if (!ret[j].isScalar()) throw Exception("function returned non-scalar result");
	    outputs.push_back(ret[j]);
	    outputs[j].resize(argdims);
	  }
      else
	for (int j=0;j<nargout;j++)
	  outputs[j].set(i+1,ret[j]);
    }
  return outputs;
}

ArrayVector ArrayFunFunction(int nargout, const ArrayVector& arg, 
			     Interpreter*eval) {
  if (arg.size() < 2) return ArrayVector(); // Don't bother
  // Remove the key/value properties
  int argcount = arg.size();
  Array errorHandler;
  bool uniformOutput = true; // We assume this to be the case
  bool foundNVP = true;
  bool customEH = false;
  while (foundNVP && (argcount >=2))
    {
      foundNVP = false;
      if (arg[argcount-2].isString() &&
	  (arg[argcount-2].asString() == "UniformOutput"))
	{
	  uniformOutput = arg[argcount-1].asInteger();
	  argcount-=2;
	  foundNVP = true;
	}
      if (arg[argcount-2].isString() &&
	  (arg[argcount-2].asString() == "ErrorHandler"))
	{
	  errorHandler = arg[argcount-1];
	  customEH = true;
	  argcount-=2;
	  foundNVP = true;
	}
    }
  if (argcount < 2) return ArrayVector();
  NTuple argdims = arg[1].dimensions();
  for (int i=1;i<argcount;i++)
    if (arg[i].dimensions() != argdims)
      throw Exception("All arguments must match dimensions");
  FuncPtr eh;
  if (customEH) 
    {
      eh = FuncPtrLookup(eval,errorHandler);
      eh->updateCode(eval);
      eval->setTryCatchActive(true);
    }
  try
    {
      if (arg[0].className() == "anonfunction")
	{
	  if (nargout == 0) nargout = 1;
	  if (uniformOutput)
	    return ArrayFunUniformAnon(nargout,arg,eval,argdims,argcount,arg[0]);
	  return ArrayFunNonuniformAnon(nargout,arg,eval,argdims,argcount,arg[0]);
	}
      else
	{
	  // Map the first argument to a function ptr
	  FuncPtr fptr = FuncPtrLookup(eval,arg[0]);
	  fptr->updateCode(eval);
	  if (nargout == 0) nargout = 1;
	  if (uniformOutput)
	    return ArrayFunUniform(nargout,arg,eval,argdims,argcount,fptr);
	  return ArrayFunNonuniform(nargout,arg,eval,argdims,argcount,fptr);
	}
    }
  catch (Exception &e)
    {
      if (customEH)
	{
	  ArrayVector input;
	  return eh->evaluateFunc(eval,input,1);
	}
      else
	throw;
    }
}