User`s guide

4 Using MWArra y Classes
a.set(index, 2.0);
else if (index[1] == index[0]+1 || index[1] == index[0]-1)
a.set(index, -1.0);
}
}
The example allocates the 4-by-4 sparse matrix with a capacity of 10 nonzero
elements. Initially, the array has no nonzero elements. The
for loops set the
array’s values using an index array.
Sparse arrays allocate storage only for the nonzero eleme nts that are
assigned. This e xample preallocates the array with a capacity of 10 elements
because it is known in advance that this many nonzeros are needed. If you
set additiona l zero e lements to nonzero v alu es, the allocated ca pa city is
automatically increased to accommodate the new values.
Examples of Using get. The
get methods work like the set methods. The
get methods support indexing through one-based offset or index array. The
next example displays the elements of an N-dim ensio nal array where all
indices are equal:
public void printDiagonals(MWNumericArray a)
{
int[] dims = a.getDimensions();
int n = dims[0];
for (int i = 1; i < dims.length; i++)
{
if (dims[i] < n)
n = dims[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < dims.length; j++)
dims[j] = n;
System.out.print("[");
for (int j = 0; j < dims.length; j++)
System.out.print(i + (j!=dims.length-1?",":""));
4-20