<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE> S-Lang Run-Time Library Reference: Version 1.4.0: Array Functions</TITLE>
<LINK HREF="slangfun-2.html" REL=next>

<LINK HREF="slangfun.html#toc1" REL=contents>
</HEAD>
<BODY>
<A HREF="slangfun-2.html">Next</A>
Previous
<A HREF="slangfun.html#toc1">Contents</A>
<HR>
<H2><A NAME="s1">1. Array Functions</A></H2>

<P>
<H2><A NAME="_reshape"></A> <A NAME="ss1.1">1.1 <B>_reshape</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Copy an array to a new shape
<DT><B> Usage </B><DD><P><CODE>Array_Type _reshape (Array_Type A, Array_Type I)</CODE>
<DT><B> Description </B><DD><P>The <CODE>_reshape</CODE> function creates a copy of an array <CODE>A</CODE>,
reshapes it to the form specified by <CODE>I</CODE> and returns the result.
The elements of <CODE>I</CODE> specify the new dimensions of the copy of
<CODE>A</CODE> and must be consistent with the number of elements <CODE>A</CODE>.
<DT><B> Example </B><DD><P>If <CODE>A</CODE> is a <CODE>100</CODE> element 1-d array, a new array 2-d array of 
size <CODE>20</CODE> by <CODE>5</CODE> may be created from the elements of <CODE>A</CODE>
by
<BLOCKQUOTE><CODE>
<PRE>
      A = _reshape (A, [20, 5]);
</PRE>
</CODE></BLOCKQUOTE>

In this example, the original array was no longer needed.  Hence, it
is preferable to make use of the <CODE>__tmp</CODE> operator to avoid the
creation of a new array, i.e.,
<BLOCKQUOTE><CODE>
<PRE>
      A = _reshape (__tmp(A), [20,5]);
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Notes </B><DD><P>The <CODE>reshape</CODE> function performs a similar function to
<CODE>_reshape</CODE>.  In fact, the <CODE>_reshape</CODE> function could have been
implemented via:
<BLOCKQUOTE><CODE>
<PRE>
     define _reshape (a, i)
     {
        a = @a;     % Make a new copy
        reshape (a, i);
        return a;
     }
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> See Also </B><DD><P><CODE>reshape, array_info</CODE>
</DL>
<P>
<P>
<H2><A NAME="array_info"></A> <A NAME="ss1.2">1.2 <B>array_info</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Returns information about an array
<DT><B> Usage </B><DD><P><CODE>(Array_Type, Integer_Type, DataType_Type) array_info (Array_Type a)</CODE>
<DT><B> Description </B><DD><P>The <CODE>array_info</CODE> function returns information about the array <CODE>a</CODE>.
It returns three values: an 1-d integer array array specifying the
size of each dimension of <CODE>a</CODE>, the number of dimensions of
<CODE>a</CODE>, and the data type of <CODE>a</CODE>.
<DT><B> Example </B><DD><P>The <CODE>array_info</CODE> function may be used to find the number of rows
of an array:
<BLOCKQUOTE><CODE>
<PRE>
    define num_rows (a)
    {
       variable dims, num_dims, data_type;

       (dims, num_dims, data_type) = array_info (a);
       return dims [0];
    }
</PRE>
</CODE></BLOCKQUOTE>

For 1-d arrays, this information is more easily obtained from the
<CODE>length</CODE> function.
<DT><B> See Also </B><DD><P><CODE>typeof, reshape, length, _reshape</CODE>
</DL>
<P>
<P>
<H2><A NAME="array_map"></A> <A NAME="ss1.3">1.3 <B>array_map</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Apply a function to each element of an array
<DT><B> Usage </B><DD><P><CODE>Array_Type array_map (type, func, arg0, ...)</CODE>
<BLOCKQUOTE><CODE>
<PRE>
    DataType_Type type;
    Ref_Type func;
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Description </B><DD><P>The <CODE>array_map</CODE> function may be used to apply a function to each
element of an array and returns the result as an array of a
specified type.  The <CODE>type</CODE> parameter indicates what kind of
array should be returned and generally corresponds to the return
type of the function.  The <CODE>arg0</CODE> parameter should be an array
and is used to determine the dimensions of the resulting array.  If
any subsequent arguments correspond to an array of the same size,
then those array elements will be passed in parallel with the first
arrays arguments.
<DT><B> Example </B><DD><P>The first example illustrates how to apply the <CODE>strlen</CODE> function
to an array of strings:
<BLOCKQUOTE><CODE>
<PRE>
     S = ["", "Train", "Subway", "Car"];
     L = array_map (Integer_Type, &amp;strlen, S);
</PRE>
</CODE></BLOCKQUOTE>

This is equivalent to:
<BLOCKQUOTE><CODE>
<PRE>
     S = ["", "Train", "Subway", "Car"];
     L = Integer_Type [length (S)];
     for (i = 0; i &lt; length (S); i++) L[i] = strlen (S[i]);
</PRE>
</CODE></BLOCKQUOTE>
<P>Now consider an example involving the <CODE>strcat</CODE> function:
<BLOCKQUOTE><CODE>
<PRE>
     files = ["slang", "slstring", "slarray"];

     exts = ".c";
     cfiles = array_map (String_Type, &amp;strcat, files, exts);
     % ==&gt; cfiles = ["slang.c slstring.c slarray.c"];

     exts =  [".a",".b",".c"];
     xfiles = array_map (String_Type, &amp;strcat, files, exts);
     % ==&gt; xfiles = ["slang.a", "slstring.b", "slarray.c"];
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> Notes </B><DD><P>Many mathemetical functions already work transparantly on arrays.
For example, the following two statements produce identical results:
<BLOCKQUOTE><CODE>
<PRE>
     B = sin (A);
     B = array_map (Double_Type, &amp;sin, A);
</PRE>
</CODE></BLOCKQUOTE>
<DT><B> See Also </B><DD><P><CODE>array_info, strlen, strcat, sin</CODE>
</DL>
<P>
<P>
<H2><A NAME="array_sort"></A> <A NAME="ss1.4">1.4 <B>array_sort</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Sort an array
<DT><B> Usage </B><DD><P><CODE>Array_Type array_sort (Array_Type a [, String_Type or Ref_Type f])</CODE>
<DT><B> Description </B><DD><P><CODE>array_sort</CODE> sorts the array <CODE>a</CODE> into ascending order and
returns an integer array that represents the result of the sort. If
the optional second parameter <CODE>f</CODE> is present, the function
specified by <CODE>f</CODE> will be used to compare elements of <CODE>a</CODE>;
otherwise, a built-in sorting function will be used.  
<P>If <CODE>f</CODE> is present, then it must be either a string representing
the name of the comparison function, or a reference to the function.
The sort function represented by <CODE>f</CODE> must be a <B>S-lang</B>
user-defined function that takes two arguments.  The function must
return an integer that is less than zero if the first parameter is
considered to be less than the second, zero if they are equal, and a
value greater than zero if the first is greater than the second.
<P>If the comparision function is not specified, then a built-in comparison
function appropriate for the data type will be used.  For example,
if <CODE>a</CODE> is an array of character strings, then the sort will be
preformed using <CODE>strcmp</CODE>.
<P>The integer array returned by this function is simply an index that
indicates the order of the sorted array.  The input array <CODE>a</CODE> is
not changed.
<DT><B> Example </B><DD><P>An array of strings may be sorted using the <CODE>strcmp</CODE> function
since it fits the specification for the sorting function described
above:
<BLOCKQUOTE><CODE>
<PRE>
     variable A = String_Type [3];
     A[0] = "gamma"; A[1] = "alpha"; A[2] = "beta";

     variable I = array_sort (A, &amp;strcmp);
</PRE>
</CODE></BLOCKQUOTE>

Alternatively, one may use
<BLOCKQUOTE><CODE>
<PRE>
     variable I = array_sort (A);     
</PRE>
</CODE></BLOCKQUOTE>

to use the built-in comparison function.
<P>After the <CODE>array_sort</CODE> has executed, the variable <CODE>I</CODE> will
have the values <CODE>[2, 0, 1]</CODE>.  This array can be used to
re-shuffle the elements of <CODE>A</CODE> into the sorted order via the
array index expression <CODE>A = A[I]</CODE>.
<DT><B> See Also </B><DD><P><CODE>strcmp</CODE>
</DL>
<P>
<P>
<H2><A NAME="init_char_array"></A> <A NAME="ss1.5">1.5 <B>init_char_array</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Initialize an array of characters
<DT><B> Usage </B><DD><P><CODE>init_char_array (Array_Type a, String_Type s)</CODE>
<DT><B> Description </B><DD><P>The <CODE>init_char_array</CODE> function may be used to initialize a
character array <CODE>a</CODE> by setting the elements of the array
<CODE>a</CODE> to the corresponding characters of the string <CODE>s</CODE>.
<DT><B> Example </B><DD><P>The statements
<BLOCKQUOTE><CODE>
<PRE>
     variable a = Char_Type [10];
     init_char_array (a, "HelloWorld");
</PRE>
</CODE></BLOCKQUOTE>

creates an character array and initializes its elements to the
characters in the string <CODE>"HelloWorld"</CODE>.
<DT><B> Notes </B><DD><P>The character array must be large enough to hold all the characters
of the initialization string.
<DT><B> See Also </B><DD><P><CODE>bstring_to_array, strlen, strcat</CODE>
</DL>
<P>
<P>
<H2><A NAME="length"></A> <A NAME="ss1.6">1.6 <B>length</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Get the length of an object
<DT><B> Usage </B><DD><P><CODE>Integer_Type length (obj)</CODE>
<DT><B> Description </B><DD><P>The <CODE>length</CODE> function may be used to get information about the
length of an object.  For simple scalar data-types, it returns <CODE>1</CODE>.
For arrays, it returns the total number of elements of the array.
<DT><B> Notes </B><DD><P>If <CODE>obj</CODE> is a string, <CODE>length</CODE> returns <CODE>1</CODE> because a
<CODE>String_Type</CODE> object is considered to be a scalar.  To get the
number of characters in a string, use the <CODE>strlen</CODE> function.
<DT><B> See Also </B><DD><P><CODE>array_info, typeof, strlen</CODE>
</DL>
<P>
<P>
<H2><A NAME="reshape"></A> <A NAME="ss1.7">1.7 <B>reshape</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Reshape an array
<DT><B> Usage </B><DD><P><CODE>reshape (Array_Type A, Array_Type I)</CODE>
<DT><B> Description </B><DD><P>The <CODE>reshape</CODE> function changes the size of <CODE>A</CODE> to have the size
specified by the 1-d integer array <CODE>I</CODE>.  The elements of <CODE>I</CODE>
specify the new dimensions of <CODE>A</CODE> and must be consistent with
the number of elements <CODE>A</CODE>.
<DT><B> Example </B><DD><P>If <CODE>A</CODE> is a <CODE>100</CODE> element 1-d array, it can be changed to a
2-d <CODE>20</CODE> by <CODE>5</CODE> array via
<BLOCKQUOTE><CODE>
<PRE>
      reshape (A, [20, 5]);
</PRE>
</CODE></BLOCKQUOTE>

However, <CODE>reshape(A, [11,5])</CODE> will result in an error because
the the <CODE>[11,5]</CODE> array specifies <CODE>55</CODE> elements.
<DT><B> Notes </B><DD><P>Since <CODE>reshape</CODE> modifies the shape of an array, and arrays are
treated as references, then all references to the array will
reference the new shape.  If this effect is unwanted, then use the 
<CODE>_reshape</CODE> function instead.
<DT><B> See Also </B><DD><P><CODE>_reshape, array_info</CODE>
</DL>
<P>
<P>
<H2><A NAME="transpose"></A> <A NAME="ss1.8">1.8 <B>transpose</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Transpose a 2d array
<DT><B> Usage </B><DD><P><CODE>Array_Type transpose (Array_Type a)</CODE>
<DT><B> Description </B><DD><P>The <CODE>transpose</CODE> function returns the transpose of a specified
array.  By definition, the transpose of an array, say one with
elements <CODE>a[i,j,...k]</CODE> is an array whose elements are
<CODE>a[k,...,j,i]</CODE>.
<DT><B> See Also </B><DD><P><CODE>_reshape, reshape, array_info</CODE>
</DL>
<P>
<P>
<H2><A NAME="where"></A> <A NAME="ss1.9">1.9 <B>where</B></A>
</H2>

<P>
<DL>
<DT><B> Synopsis </B><DD><P>Get indices where an integer array is non-zero
<DT><B> Usage </B><DD><P><CODE>Array_Type where (Array_Type a)</CODE>
<DT><B> Description </B><DD><P>The <CODE>where</CODE> function examines an integer array <CODE>a</CODE> and
returns a 2-d integer array whose rows are the indices of <CODE>a</CODE>
where the corresponding element of <CODE>a</CODE> is non-zero.
<DT><B> Example </B><DD><P>Consider the following:
<BLOCKQUOTE><CODE>
<PRE>
    variable X = [0.0:10.0:0.01];
    variable A = sin (X);
    variable I = where (A &lt; 0.0);
    A[I] = cos (X) [I];
</PRE>
</CODE></BLOCKQUOTE>

Here the variable <CODE>X</CODE> has been assigned an array of doubles
whose elements range from <CODE>0.0</CODE> through <CODE>10.0</CODE> in
increments of <CODE>0.01</CODE>.  The second statement assigns <CODE>A</CODE> to
an array whose elements are the <CODE>sin</CODE> of the elements of <CODE>X</CODE>.
The third statement uses the where function to get the indices of
the elements of <CODE>A</CODE> that are less than <CODE>0.0</CODE>.  Finally, the
last statement substitutes into <CODE>A</CODE> the <CODE>cos</CODE> of the
elements of <CODE>X</CODE> at the positions of <CODE>A</CODE> where the
corresponding <CODE>sin</CODE> is less than <CODE>0</CODE>.  The end result is
that the elements of <CODE>A</CODE> are a mixture of sines and cosines.
<DT><B> See Also </B><DD><P><CODE>array_info, sin, cos</CODE>
</DL>
<P>
<P>
<P>
<HR>
<A HREF="slangfun-2.html">Next</A>
Previous
<A HREF="slangfun.html#toc1">Contents</A>
</BODY>
</HTML>
