whatis.sci

lvd: Enrico, please say something about your function here.

function rv = whatis(name_arr)
// NAME
//   whatis - listing of variables in extended format
//
// CALLING SEQUENCE
//   whatis()
//   whatis(name_arr)
//
// PARAMETER
//   name_arr : array of variables names
//
// DESCRIPTION
//   whatis returns a column-vector with the names,
//   types, and sizes of all local variables
//   (first form), or only of the variables whose
//   names (as strings!) are given in the matrix
//   name_arr (second form).
//
// EXAMPLES
//   whatis()
//   whatis('my_mat')
//   whatis(['foo' 'bar' 'baz'; 'foobar 'morefoo' 'foobaz'])
//
// SEE ALSO
//   who, whos
//
// AUTHORS
//   Enrico Segre, Lydia van Dijk
//
//   Copyright 1999 by Enrico Segre and Lydia van Dijk.
//   whatis is free software distributed under the terms
//   of the GNU General Public License, version 2.
//!

// LAST REVISION
// lvd, Fri Dec  3 01:01:45 UTC 1999

// TO DO/TO FIX
//
// - Accepting a regexp as an argument would be nice.  This in turn
//   leads to complete boolean expressions doing the variable selection
//   resembling what the UNI* find utility does.  Example:
//   All vars ending in a 'v' that are complex and larger than
//   1000 words.
// - The behavior with undefined variables is unsatisfactory.


[nl, nr] = argn(0);
clear nl;
if nr == 0 then
    // no arguments --> take all variables like whos() does
    clear nr;
    name_arr = sort(who("get"));
end
clear nr;

if type(name_arr) ~= 10 then
    error("Expecting a string or an array of strings, got a " ..
            + typeof(name_arr) + "!");
    return;
end

[namev, memv] = who("get"); // get memory usage of all local vars

// define isreal() for hypermatrices
deff('b = %hm_isreal(hm)', ..
     'if size(hm, ""*"") == 0 then b = %t; ..
      else ..
          b = isreal(hm(1)); ..
      end');

deff('b = hm_isbool(hm)', ..
     'if size(hm, ""*"") == 0 then ..
          b = %t; ..
      else ..
          b = type(hm(1)) == 4; ..
      end');

deff('b = hm_isstring(hm)', ..
     'if size(hm, ""*"") == 0 then ..
          b = %t; ..
      else ..
          b = type(hm(1)) == 10; ..
      end');

deff('b = hm_isint(hm)', ..
     'if size(hm, ""*"") == 0 then ..
          b = %t; ..
      else ..
          b = type(hm(1)) == 8; ..
      end');

rv = [];
for name = matrix(name_arr, 1, size(name_arr, "*")) do
    if isdef(name) then
        clear var;
        var = evstr(name);     // convert var's name back into var
        //                     
        // type classification 
        //                     
        ty = type(var);        // type number
        select ty              // type 16 and 17 are not recognized
        case 16 then           //   by the function typeof()
            tgenp = %f;        // we know the tlist's type for these
            lab = var(1);      // vector of labels
            select lab(1)      // 1st label defines the type
            case "ar" then
                tnam = "ARMAX process";
            case "des" then
                tnam = "descriptor system";
            case "linpro" then
                tnam = "linear programming data";
            case "lss" then
                tnam = "linear system";
            case "r" then
                tnam = "rational";
            case "scs_tree" then
                tnam = "SCICOS navigator data";
            case "xxx" then
                tnam= "SCICOS menu data";
            else
                tnam = "generic tlist " + lab(1);
                tgenp = %t;
            end // select lab(1)
        case 17 then
            tnam = "hypermatrix";
        else
            tnam = typeof(var);         // type name, a string
        end // select ty
        if ty==1 | ty==2 | ty==5 | ty==17 then
            // boolean, string, integral, real, or complex, 
            // possibly sparse matrix or hypermatrix (yuck!)
            if hm_isbool(var) then
                tnam = "boolean " + tnam;
            elseif hm_isstring(var) then
                tnam = "string " + tnam;
            elseif hm_isint(var) then
                tnam = "int " + tnam;
            else
                if isreal(var) then
                    tnam = "real " + tnam;
                else
                    tnam = "complex " + tnam;
                end
            end
        end
        tmp = name + ":  ";
        //
        // size determination
        //
        if ty==1 | ty==2 | ty==4 | ty==5 | ty==8 | ty==10 | ty==17 then
            // any kind of matrix
            sz = size(var);           // var's dimensions
            tmp = tmp + string(sz(1));
            for j = 2:length(sz)
                tmp = tmp + "x" + string(sz(j));
            end
            tmp = tmp + " ";
        elseif ty==16
            // user-defined aka generic tlist
            if tgenp then
                tmp = tmp + string(size(var(1), "*")) + " element ";
            end
        else
            // function, library, or other non-atomic object
        end
        //
        // memory usage
        //
        i = find(namev == evstr("name")); // index of var's entry
        tmp = tmp + tnam + ", " + string(memv(i)) + " words";
    else
        tmp = """" + name + """ is not defined";
        warning("variable " + tmp);
    end
    rv = [rv; tmp];
end