org.strbio.math
Class IMatrix

java.lang.Object
  extended by org.strbio.math.IMatrix
Direct Known Subclasses:
StatsMatrix

public class IMatrix
extends java.lang.Object

a class to encapsulate a 2-D matrix of integers Can do a bunch of math functions, and save/load from a file.

 Version 1.13, 7/7/99 - added equals
 Version 1.12, 3/1/99 - added print()
 Version 1.11, 1/29/99 - changed some PrintfStream to Printf.
 Version 1,1, 5/18/98 - added saveBinary, autodetecting load.
 Version 1.01, 4/28/98 - fixed a couple of saving bugs
 Version 1.0, 3/30/98 - original version
 

Version:
1.13, 7/7/99
Author:
JMC
See Also:
DMatrix, IVector

Field Summary
 int[][] data
          data contains the actual matrix itself; it's public so that it can be manipulated directly (for speed, such as that is in java) and so that you can do something like 'im.data[0][0] = tmp' as well as 'tmp = im.data[0][0]' without needing 2 different functions.
 
Constructor Summary
IMatrix()
          makes a blank 0x0 matrix; you can assign a int[][] to the data directly, or load data in from a file
IMatrix(IMatrix x)
          copies another IMatrix.
IMatrix(int[][] x)
          makes a new IMatrix object out of a int[][] array.
IMatrix(int c, int r)
          makes an emptry IMatrix object with a specified number of columns and rows.
 
Method Summary
 void add(IMatrix x)
          Adds another IMatrix to this one.
static IMatrix add(IMatrix a, IMatrix b)
          Adds two IMatrix's and returns a new IMatrix containing the sum.
 int cols()
          returns the number of columns in the encapsulated array.
 boolean equals(java.lang.Object x)
          mathematical objects are equal if their contents are.
 boolean isEmpty()
          Does the matrix contain all zeroes?
 void load(java.io.BufferedReader infile)
          Load text matrix from an open BufferedReader.
 void load(java.io.DataInputStream infile)
          Load binary matrix from an open DataInputStream.
 void load(java.lang.String filename)
          Loads matrix from a file; file type (text/binary) is autodetected.
 int max()
          What's the maximum value in the matrix?
 int min()
          What's the minimum value in the matrix?
static IMatrix multiply(IMatrix a, int b)
          Multiplies a IMatrix by an int and returns a new IMatrix containing the result.
 void multiply(int x)
          Multiplies all values in this IMatrix by a specified int.
static IMatrix multiply(int b, IMatrix a)
          Multiplies a IMatrix by an int and returns a new IMatrix containing the result.
 void print(Printf outfile)
          prints in ascii to an open Printf, with a nicer format than 'save'.
 int rows()
          returns the number of rows in the encapsulated array.
 void save(Printf outfile)
          Save to an open Printf.
 void save(java.lang.String filename)
          Saves to a new text file.
 void saveBinary(java.io.DataOutputStream outfile)
          Save in binary format to an open DataOutputStream
 void saveBinary(java.lang.String filename)
          Saves to a new binary file.
 void setValue(int x)
          Sets every value in the matrix to a specified int.
 void setValueAt(int c, int r, int d)
          Sets the data at a given column/row.
 void subtract(IMatrix x)
          Subtracts another IMatrix from this one.
static IMatrix subtract(IMatrix a, IMatrix b)
          Subtracts one IMatrix's from another returns a new IMatrix containing the difference.
 int valueAt(int c, int r)
          Returns the data at a given column/row.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

public int[][] data
data contains the actual matrix itself; it's public so that it can be manipulated directly (for speed, such as that is in java) and so that you can do something like 'im.data[0][0] = tmp' as well as 'tmp = im.data[0][0]' without needing 2 different functions. In C++ this could all be hidden by operator overloading, but that would be too elegant for some people.

Constructor Detail

IMatrix

public IMatrix(int c,
               int r)
makes an emptry IMatrix object with a specified number of columns and rows. All data should be zeroed out... this is not done explicitly, but relies on Java's default values.

Parameters:
c - columns
r - rows

IMatrix

public IMatrix()
makes a blank 0x0 matrix; you can assign a int[][] to the data directly, or load data in from a file


IMatrix

public IMatrix(int[][] x)
makes a new IMatrix object out of a int[][] array. It copies the data rather than creating a reference to it, so if you manipulate data in your original array after calling this constructor, it will NOT affect the new object. If you want to do the latter, try im = new IMatrix(), then im.data = my_array.


IMatrix

public IMatrix(IMatrix x)
copies another IMatrix. A new copy of the array is made.

Method Detail

equals

public boolean equals(java.lang.Object x)
mathematical objects are equal if their contents are.

Overrides:
equals in class java.lang.Object

cols

public final int cols()
returns the number of columns in the encapsulated array.


rows

public final int rows()
returns the number of rows in the encapsulated array.


valueAt

public final int valueAt(int c,
                         int r)
Returns the data at a given column/row. It's easier to manipulate the data directly, but this method is there if you don't feel like doing that.

Parameters:
c - column
r - row

setValueAt

public final void setValueAt(int c,
                             int r,
                             int d)
Sets the data at a given column/row. It's easier to manipulate the data directly, but this method is there if you don't feel like doing that.

Parameters:
c - column
r - row
d - value to set the data to

setValue

public final void setValue(int x)
Sets every value in the matrix to a specified int.


add

public final void add(IMatrix x)
Adds another IMatrix to this one. If the sizes aren't the same, you will probably get an ArrayOutOfBounds exception.


add

public static final IMatrix add(IMatrix a,
                                IMatrix b)
Adds two IMatrix's and returns a new IMatrix containing the sum. If the sizes aren't the same, you will probably get an ArrayOutOfBounds exception.


subtract

public final void subtract(IMatrix x)
Subtracts another IMatrix from this one. If the sizes aren't the same, you will probably get an ArrayOutOfBounds exception.


subtract

public static final IMatrix subtract(IMatrix a,
                                     IMatrix b)
Subtracts one IMatrix's from another returns a new IMatrix containing the difference. If the sizes aren't the same, you will probably get an ArrayOutOfBounds exception. To do 'c = a - b', use c = subtract(a,b). Too bad operator overloading is so complicated and confusing...


multiply

public final void multiply(int x)
Multiplies all values in this IMatrix by a specified int.


multiply

public static final IMatrix multiply(IMatrix a,
                                     int b)
Multiplies a IMatrix by an int and returns a new IMatrix containing the result.


multiply

public static final IMatrix multiply(int b,
                                     IMatrix a)
Multiplies a IMatrix by an int and returns a new IMatrix containing the result.


isEmpty

public final boolean isEmpty()
Does the matrix contain all zeroes?


max

public final int max()
What's the maximum value in the matrix?


min

public final int min()
What's the minimum value in the matrix?


print

public void print(Printf outfile)
           throws java.io.IOException
prints in ascii to an open Printf, with a nicer format than 'save'.

Throws:
java.io.IOException
See Also:
save(org.strbio.io.Printf)

save

public final void save(Printf outfile)
                throws java.io.IOException
Save to an open Printf.

Throws:
java.io.IOException

saveBinary

public final void saveBinary(java.io.DataOutputStream outfile)
                      throws java.io.IOException
Save in binary format to an open DataOutputStream

Throws:
java.io.IOException

load

public final void load(java.io.BufferedReader infile)
                throws java.io.IOException
Load text matrix from an open BufferedReader.

Throws:
java.io.IOException
See Also:
BufferedReader

load

public final void load(java.io.DataInputStream infile)
                throws java.io.IOException
Load binary matrix from an open DataInputStream.

Throws:
java.io.IOException

save

public final void save(java.lang.String filename)
                throws java.io.IOException
Saves to a new text file. Overwrites any existing file. You can comment the file with lines beginning with a semicolon.

Parameters:
filename - the file name.
Throws:
java.io.IOException

saveBinary

public final void saveBinary(java.lang.String filename)
                      throws java.io.IOException
Saves to a new binary file. Overwrites any existing file.

Parameters:
filename - the file name.
Throws:
java.io.IOException

load

public final void load(java.lang.String filename)
                throws java.io.IOException
Loads matrix from a file; file type (text/binary) is autodetected.

Parameters:
filename - the file name.
Throws:
java.io.IOException