org.strbio.math
Class IVector

java.lang.Object
  extended by org.strbio.math.IVector
Direct Known Subclasses:
Bin, FBin

public class IVector
extends java.lang.Object

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

 Version 1.03, 7/7/99 - added equals
 Version 1.02, 7/6/99 - added reverse
 Version 1.01, 6/28/99 - added print, changed some PrintfStream to Printf
 Version 1.0, 3/30/98 - original version.
 

Version:
1.03, 7/7/99
Author:
JMC
See Also:
DVector, IMatrix

Field Summary
 int[] data
          data contains the actual array 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 'iv.data[0] = tmp' as well as 'tmp = iv.data[0]' without needing 2 different functions.
 
Constructor Summary
IVector()
          makes a blank vector of dimension 0; you can assign a int[] to the data directly, or load data in from a file
IVector(int dim)
          makes an emptry IVector object with a specified dimension All data should be zeroed out... this is not done explicitly, but relies on Java's default values.
IVector(int[] x)
          makes a new IVector object out of a int[] array.
IVector(IVector x)
          copies another IVector.
 
Method Summary
 void add(IVector x)
          Adds another IVector to this one.
static IVector add(IVector a, IVector b)
          Adds two IVector's and returns a new IVector containing the sum.
 IVector cross(IVector x)
          Returns the cross product of multiplying this with another vector.
static IVector cross(IVector x, IVector y)
          Returns the cross product of multiplying two vectors.
 int dimension()
          returns the dimension the encapsulated array.
 int dot(IVector x)
          Returns the dot product of multiplying this with another vector.
static int dot(IVector x, IVector y)
          Returns the dot product of multiplying two vectors.
 IVector eliminate(int pos)
          Return a copy of the vector with one position removed.
 boolean equals(java.lang.Object x)
          mathematical objects are equal if their contents are.
 int length()
          returns the dimension the encapsulated array.
 void load(java.io.BufferedReader infile)
          Load from an open BufferedReader.
 void load(java.lang.String filename)
          Loads from a text file.
 int max()
          What's the maximum value in the array?
 int min()
          What's the minimum value in the array?
 void multiply(int x)
          Multiplies all values in this IVector by a specified int.
static IVector multiply(int b, IVector a)
          Multiplies a IVector by an int and returns a new IVector containing the result.
static IVector multiply(IVector a, int b)
          Multiplies a IVector by an int and returns a new IVector containing the result.
 void print(Printf outfile)
          prints in ascii to an open Printf, with a nicer format than 'save'.
 void reverse()
          reverses the order of data in the vector.
static IVector reverse(IVector x)
          returns a vector which is the reverse of this one.
 void save(Printf outfile)
          Save to an open PrintfStream.
 void save(java.lang.String filename)
          Saves to a new text file.
 void setValue(int x)
          Sets every value in the matrix to a specified int.
 void setValueAt(int x, int d)
          Sets the data at a given index.
 IVector sort()
          returns a vector of indices to sort an IVector (small -> large).
 void subtract(IVector x)
          Subtracts another IVector from this one.
static IVector subtract(IVector a, IVector b)
          Subtracts one IVector's from another returns a new IVector containing the difference.
 int valueAt(int x)
          Returns the data at a given index.
 
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 array 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 'iv.data[0] = tmp' as well as 'tmp = iv.data[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

IVector

public IVector(int dim)
makes an emptry IVector object with a specified dimension All data should be zeroed out... this is not done explicitly, but relies on Java's default values.

Parameters:
dim - dimension

IVector

public IVector()
makes a blank vector of dimension 0; you can assign a int[] to the data directly, or load data in from a file


IVector

public IVector(int[] x)
makes a new IVector 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 IVector(), then iv.data = my_array.


IVector

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

Method Detail

eliminate

public IVector eliminate(int pos)
Return a copy of the vector with one position removed.


equals

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

Overrides:
equals in class java.lang.Object

length

public final int length()
returns the dimension the encapsulated array.


dimension

public final int dimension()
returns the dimension the encapsulated array.


valueAt

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

Parameters:
x - index

setValueAt

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

Parameters:
x - index
d - value to set the data to

setValue

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


reverse

public final void reverse()
reverses the order of data in the vector.


reverse

public static final IVector reverse(IVector x)
returns a vector which is the reverse of this one.


add

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


add

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


subtract

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


subtract

public static final IVector subtract(IVector a,
                                     IVector b)
Subtracts one IVector's from another returns a new IVector 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 IVector by a specified int.


multiply

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


multiply

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


dot

public final int dot(IVector x)
Returns the dot product of multiplying this with another vector.


dot

public static final int dot(IVector x,
                            IVector y)
Returns the dot product of multiplying two vectors.


cross

public final IVector cross(IVector x)
Returns the cross product of multiplying this with another vector. Cross product is not defined for dimensions other than 3... this function trusts you not do do something stupid.


cross

public static final IVector cross(IVector x,
                                  IVector y)
Returns the cross product of multiplying two vectors. Cross product is not defined for dimensions other than 3... this function trusts you not do do something stupid.


max

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


min

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


sort

public IVector sort()
returns a vector of indices to sort an IVector (small -> large). i.e. if vs = v.sort(), then vs.data[0] is the index of the smallest value in v, vs.data[1] is the next smallest, etc. This uses bubble sort; somebody should implement a quicksort if this ever becomes CPU-limiting.


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 PrintfStream.

Throws:
java.io.IOException
See Also:
PrintfStream

load

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

Throws:
java.io.IOException
See Also:
BufferedReader

save

public final void save(java.lang.String filename)
                throws java.io.IOException
Saves to a new text 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 from a text file.

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