File: Inspect.cpp

package info (click to toggle)
freemat 4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 174,736 kB
  • ctags: 67,053
  • sloc: cpp: 351,060; ansic: 255,892; sh: 40,590; makefile: 4,323; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (565 lines) | stat: -rw-r--r-- 16,009 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
 /*
 * Copyright (c) 2002-2006 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 "Interpreter.hpp"
#include <QtCore>
#include "Algorithms.hpp"


//!
//@Module END End Function
//@@Section INSPECTION
//@@Usage
//Computes the size of a variable along a given dimension.  The syntax
//for its use is 
//@[
//   y = end(x,dim,subindexes)
//@]
//where @|x| is the array to be analyzed, @|dim| is the dimension along
//which to compute the end, and @|subindexes| indicates how many dimensions
//are involved in the @|end| calculation.
//@@Signature
//function p_end EndFunction
//inputs x dim subindexes
//outputs y
//!
ArrayVector EndFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 3)
    throw Exception("End function requires 3 arguments, the array, the end index, and the number of subindexes");
  NTuple t(arg[0].dimensions());
  index_t enddim(arg[1].asDouble());
  index_t totalndxs(arg[2].asDouble());
  if (totalndxs == 1)
    return ArrayVector(Array(index_t(arg[0].length())));
  return ArrayVector(Array(index_t(t[int(enddim-1)])));
}

//!
//@Module WHO Describe Currently Defined Variables
//@@Section INSPECTION
//@@Usage
//Reports information on either all variables in the current context
//or on a specified set of variables.  For each variable, the @|who|
//function indicates the size and type of the variable as well as 
//if it is a global or persistent.  There are two formats for the 
//function call.  The first is the explicit form, in which a list
//of variables are provided:
//@[
//  who a1 a2 ...
//@]
//In the second form
//@[
//  who
//@]
//the @|who| function lists all variables defined in the current 
//context (as well as global and persistent variables). Note that
//there are two alternate forms for calling the @|who| function:
//@[
//  who 'a1' 'a2' ...
//@]
//and
//@[
//  who('a1','a2',...)
//@]
//@@Example
//Here is an example of the general use of @|who|, which lists all of the variables defined.
//@<
//c = [1,2,3];
//f = 'hello';
//p = randn(1,256);
//who
//@>
//In the second case, we examine only a specific variable:
//@<
//who c
//who('c')
//@>
//@@Signature
//sfunction who WhoFunction
//inputs varargin
//outputs none
//!
ArrayVector WhoFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  StringVector names;
  Context *context = eval->getContext();
  // Bypass our context (the who(who) one)
  ParentScopeLocker lock(context);
  // Search upwards until we find an active scope
  int bypasscount = 0;
  while (!context->isScopeActive()) {
    bypasscount++;
    context->bypassScope(1);
  }
  if (arg.size() == 0)
    names = eval->getContext()->listAllVariables();
  else
    for (int i=0;i<arg.size();i++)
      names.push_back(arg[i].asString());
  qSort(names.begin(),names.end());
  eval->outputMessage("  Variable Name       Type   Flags             Size\n");
  for (int i=0;i<names.size();i++) {
    Array lookup;
    ArrayReference ptr;
    eval->outputMessage(names[i].rightJustified(15,' ',false));
    ptr = eval->getContext()->lookupVariable(names[i]);
    if (!ptr.valid())
      eval->outputMessage("   <undefined>");
    else {
      lookup = *ptr;
      eval->outputMessage(lookup.className().rightJustified(10,' ',false));
      if (lookup.isSparse())
	eval->outputMessage("   sparse");
      else
	eval->outputMessage("         ");	  
      if (eval->getContext()->isVariableGlobal(names[i])) {
	eval->outputMessage("  global ");
      } else if (eval->getContext()->isVariablePersistent(names[i])) {
	eval->outputMessage(" persist ");
      } else {
	eval->outputMessage("         ");
      }
      eval->outputMessage(QString("  [") + 
			  lookup.dimensions().toString() + 
			  QString("]"));
    }
    eval->outputMessage("\n");
  }
  context->restoreScope(bypasscount);
  return ArrayVector();
}

//!
//@Module WHOS Describe Currently Defined Variables With Memory Usage
//@@Section INSPECTION
//@@Usage
//Reports information on either all variables in the current context
//or on a specified set of variables.  For each variable, the @|who|
//function indicates the size and type of the variable as well as 
//if it is a global or persistent.  There are two formats for the 
//function call.  The first is the explicit form, in which a list
//of variables are provided:
//@[
//  whos a1 a2 ...
//@]
//In the second form
//@[
//  whos
//@]
//the @|whos| function lists all variables defined in the current 
//context (as well as global and persistent variables). Note that
//there are two alternate forms for calling the @|whos| function:
//@[
//  whos 'a1' 'a2' ...
//@]
//and
//@[
//  whos('a1','a2',...)
//@]
//@@Signature
//sfunction whos WhosFunction
//inputs varargin
//outputs none
//!
ArrayVector WhosFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  StringVector names;
  Context *context = eval->getContext();
  // Bypass our context (the who(who) one)
  ParentScopeLocker lock(context);
  // Search upwards until we find an active scope
  int bypasscount = 0;
  while (!context->isScopeActive()) {
    bypasscount++;
    context->bypassScope(1);
  }
  if (arg.size() == 0)
    names = eval->getContext()->listAllVariables();
  else
    for (int i=0;i<arg.size();i++)
      names.push_back(arg[i].asString());
  qSort(names.begin(),names.end());
  eval->outputMessage("  Variable Name       Type   Flags             Size       Bytes\n");
  for (int i=0;i<names.size();i++) {
    Array lookup;
    ArrayReference ptr;
    eval->outputMessage(names[i].rightJustified(15,' ',false));
    ptr = eval->getContext()->lookupVariable(names[i]);
    if (!ptr.valid())
      eval->outputMessage("   <undefined>");
    else {
      lookup = *ptr;
      eval->outputMessage(lookup.className().rightJustified(10,' ',false));
      if (lookup.isSparse())
	eval->outputMessage("   sparse");
      else
	eval->outputMessage("         ");	  
      if (eval->getContext()->isVariableGlobal(names[i])) {
	eval->outputMessage("  global ");
      } else if (eval->getContext()->isVariablePersistent(names[i])) {
	eval->outputMessage(" persist ");
      } else {
	eval->outputMessage("         ");
      }
      QString txt(QString("  [") + 
		  lookup.dimensions().toString() + 
		  QString("]"));
      eval->outputMessage(txt.leftJustified(15,' ',false));
      eval->outputMessage(QString("   %1").arg(lookup.bytes()));
    }
    eval->outputMessage("\n");
  }
  context->restoreScope(bypasscount);
  return ArrayVector();
}


//!
//@Module FIELDNAMES Fieldnames of a Structure
//@@Section INSPECTION
//@@Usage
//Returns a cell array containing the names of the fields in
//a structure array.  The syntax for its use is
//@[
//   x = fieldnames(y)
//@]
//where @|y| is a structure array of object array.  The result
//is a cell array, with one entry per field in @|y|.
//@@Example
//We define a simple structure array:
//@<
//y.foo = 3; y.goo = 'hello';
//x = fieldnames(y)
//@>
//@@Tests
//@{ test_fieldnames1.m
//function test_val = test_fieldnames1
//  x.foo = 3; x.goo = 'hello';
//  y = fieldnames(x);
//  z = {'foo';'goo'};
//  test_val = issame(y,z);
//@}
//@@Signature
//function fieldnames FieldNamesFunction
//inputs y
//outputs x
//!
ArrayVector FieldNamesFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() < 1)
    throw Exception("fieldnames function requires at least one argument");
  if (arg[0].dataClass() != Struct)
    return ArrayVector(Array(CellArray,NTuple(0,0)));
  StringVector names(FieldNames(arg[0]));
  ArrayMatrix m;
  for (int i=0;i<names.size();i++)
    m.push_back(ArrayVector(Array(names.at(i))));
  return ArrayVector(CellConstructor(m));
}


//!
//@Module WHERE Get Information on Program Stack
//@@Section INSPECTION
//@@Usage
//Returns information on the current stack.  The usage is
//@[
//   where
//@]
//The result is a kind of stack trace that indicates the state
//of the current call stack, and where you are relative to the
//stack.
//@@Example
//Suppose we have the following chain of functions.
//@{ chain1.m
//function chain1
//  a = 32;
//  b = a + 5;
//  chain2(b)
//@}
//@{ chain2.m
//function chain2(d)
//  d = d + 5;
//  chain3
//@}
//@{ chain3.m
//function chain3
//  g = 54;
//  f = g + 1;
//  keyboard
//@}
//The execution of the @|where| command shows the stack trace.
//@<
//chain1
//where
//@>
//@@Signature
//sfunction where WhereFunction
//inputs none
//outputs none
//!
ArrayVector WhereFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  eval->stackTrace(1);
  return ArrayVector();
}

ArrayVector AddrFunction(int nargout, const ArrayVector& arg) {
  return ArrayVector(Array(arg[0].address()));
}

//!
//@Module NARGIN Number of Input Arguments
//@@Section FUNCTIONS
//@@Usage
//The @|nargin| function returns the number of arguments passed
//to a function when it was called.  The general syntax for its
//use is
//@[
//  y = nargin
//@]
//FreeMat allows for
//fewer arguments to be passed to a function than were declared,
//and @|nargin|, along with @|isset| can be used to determine
//exactly what subset of the arguments were defined.
//@@Example
//Here is a function that is declared to take five 
//arguments, and that simply prints the value of @|nargin|
//each time it is called.
//@{ nargintest.m
//function nargintest(a1,a2,a3,a4,a5)
//  printf('nargin = %d\n',nargin);
//@}
//@<
//nargintest(3);
//nargintest(3,'h');
//nargintest(3,'h',1.34);
//nargintest(3,'h',1.34,pi,e);
//@>
//@@Tests
//@{ test_nargin1.m
//function test_val = test_nargin1
//  test_val = (sub_test_nargin1(3) == 1) && (sub_test_nargin1(3,'h',1.34,pi,e) == 5);
//end
//
//function x = sub_test_nargin1(a1,a2,a3,a4,a5)
//  x = nargin;
//end
//@}
//@{ test_nargin2.m
//function test_val = test_nargin2
//  test_val = (sub_test_nargin2(3) == 1) && (sub_test_nargin2(3,'h',1.34,pi,e) == 5);
//end
//
//function x = sub_test_nargin2(varargin)
//  x = nargin;
//end
//@}
//@@Signature
//sfunction nargin NarginFunction
//inputs none
//outputs count
//!
ArrayVector NarginFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  Context *ctxt = eval->getContext();
  ParentScopeLocker lock(ctxt);
  int nargin = ctxt->scopeNargin();
  return ArrayVector() << Array(double(nargin));
}

//!
//@Module NARGOUT Number of Output Arguments
//@@Section FUNCTIONS
//@@Usage
//The @|nargout| function computes the number of return values requested from
//a function when it was called.  The general syntax for its use
//@[
//   y = nargout
//@]
//FreeMat allows for
//fewer return values to be requested from a function than were declared,
//and @|nargout| can be used to determine exactly what subset of 
//the functions outputs are required.  
//@@Example
//Here is a function that is declared to return five 
//values, and that simply prints the value of @|nargout|
//each time it is called.
//@{ nargouttest.m
//function [a1,a2,a3,a4,a5] = nargouttest
//  printf('nargout = %d\n',nargout);
//  a1 = 1; a2 = 2; a3 = 3; a4 = 4; a5 = 5;
//@}
//@<
//a1 = nargouttest
//[a1,a2] = nargouttest
//[a1,a2,a3] = nargouttest
//[a1,a2,a3,a4,a5] = nargouttest
//@>
//@@Signature
//sfunction nargout NargoutFunction
//inputs none
//outputs count
//!
ArrayVector NargoutFunction(int, const ArrayVector&arg, Interpreter* eval) {
  Context *ctxt = eval->getContext();
  ParentScopeLocker lock(ctxt);
  int nargout = ctxt->scopeNargout();
  return ArrayVector() << Array(double(nargout));
}


//!
//@Module WHICH Get Information on Function
//@@Section INSPECTION
//@@Usage
//Returns information on a function (if defined).  The usage is
//@[
//   which(fname)
//@]
//where @|fname| is a @|string| argument that contains the name of the 
//function.  For functions and scripts defined
//via @|.m| files, the @|which| command returns the location of the source
//file:
//@[
//   y = which(fname)
//@]
//will return the filename for the @|.m| file corresponding to the given
//function, and an empty string otherwise.
//@@Example
//First, we apply the @|which| command to a built in function.
//@<
//which fft
//@>
//Next, we apply it to a function defined via a @|.m| file.
//@<
//which fliplr
//@>
//@@Signature
//sfunction which WhichFunction
//inputs functionname
//outputs location
//!
ArrayVector WhichFunction(int nargout, const ArrayVector& arg, 
			  Interpreter* eval) {
  if (arg.size() != 1)
    throw Exception("which function takes one string argument (the name of the function to look up)");
  QString fname = arg[0].asString();
  bool isFun;
  FuncPtr val;
  isFun = eval->lookupFunction(fname,val);
  Array ret(Double,NTuple(0,0));
  if (isFun) {
    if (val->type() == FM_M_FUNCTION) {
      MFunctionDef *mptr;
      mptr = (MFunctionDef *) val;
      try {
	mptr->updateCode(eval);
      } catch (Exception &e) {}
      if (mptr->pcodeFunction) {
	if (mptr->scriptFlag) {
	  if (nargout == 0) {
	    eval->outputMessage("Function "+fname+", P-code script\n");
	  }
	} else {
	  if (nargout == 0) {
	    eval->outputMessage("Function "+fname+", P-code function\n");
	  }
	}
      } else {
	if (mptr->scriptFlag) {
	  if (nargout == 0) {
	    eval->outputMessage("Function "+fname+", M-File script in file '"+mptr->fileName+"'\n");
	  } else 
	    ret = Array(mptr->fileName);
	} else {
	  if (nargout == 0) {
	    eval->outputMessage("Function "+fname+", M-File function in file '"+mptr->fileName+"'\n");
	  } else
	    ret = Array(mptr->fileName);
	}
      }
    } else if ((val->type() == FM_BUILT_IN_FUNCTION) || 
	       (val->type() == FM_SPECIAL_FUNCTION) ) {
      if (nargout == 0) {
	eval->outputMessage("Function "+fname+" is a built in function\n");
      }
    } else {
      if (nargout == 0) {
	eval->outputMessage("Function "+fname+" is an imported function\n");
      }
    }
  } else {
    if (nargout == 0) {
      eval->outputMessage("Function "+fname+" is unknown!\n");
    }
  }
  if (nargout > 0)
    return ArrayVector(ret);
  else
    return ArrayVector();
}


//!
//@Module MFILENAME Name of Current Function
//@@Section FreeMat
//@@Usage
//Returns a string describing the name of the current function.  For M-files
//this string will be the complete filename of the function.  This is true even
//for subfunctions.  The syntax for its use is
//@[
//    y = mfilename
//@]
//@@Signature
//sfunction mfilename MFilenameFunction
//inputs none
//outputs filename
//!
ArrayVector MFilenameFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  return ArrayVector(Array(QFileInfo(eval->getMFileName()).fileName()));
}

//!
//@Module COMPUTER Computer System FreeMat is Running On
//@@Section FreeMat
//@@Usage
//Returns a string describing the name of the system FreeMat is running on.
//The exact value of this string is subject to change, although the @|'MAC'|
//and @|'PCWIN'| values are probably fixed.
//@[
//  str = computer
//@]
//Currently, the following return values are defined
//\begin{itemize}
//  \item @|'PCWIN'| - MS Windows
//  \item @|'MAC'| - Mac OS X
//  \item @|'UNIX'| - All others
//\end{itemize}
//@@Signature
//function computer ComputerFunction
//inputs none
//outputs str
//!
ArrayVector ComputerFunction(int nargout, const ArrayVector& arg) {
#ifdef WIN32
  return ArrayVector(Array(QString("PCWIN")));
#elif defined(__APPLE__)
  return ArrayVector(Array(QString("MAC")));
#else
  return ArrayVector(Array(QString("UNIX")));
#endif
}