org.strbio.math
Class FVector

java.lang.Object
  extended by org.strbio.math.FVector
Direct Known Subclasses:
FHalfMatrix

public class FVector
extends java.lang.Object

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

 Version 1.21, 7/7/99 - added equals
 Version 1.2, 7/6/99 - added findNClosest, append, reverse
 Version 1.13, 6/7/99 - added print, changed some PrintfStream to Printf
 Version 1.12, 6/2/99 - added median, print
 Version 1.11, 4/16/99 - some error conditions (i.e. min() when there's
   no data) return Float.NaN, instead of (incorrect) 0.0.
 Version 1.1, 9/30/98 - added linearFit
 Version 1.01, 5/11/98 - added constructors from IVector, int[].
 Version 1.0, 3/30/98 - original version.
 

Version:
1.21, 7/7/99
Author:
JMC
See Also:
IVector, FMatrix

Field Summary
 float[] 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 'dv.data[0] = tmp' as well as 'tmp = dv.data[0]' without needing 2 different functions.
 
Constructor Summary
FVector()
          makes a blank vector of dimension 0; you can assign a int[] to the data directly, or load data in from a file
FVector(float[] x)
          makes a new FVector object out of a int[] array.
FVector(FVector x)
          copies another FVector.
FVector(int dim)
          makes an emptry FVector object with a specified dimension All data should be zeroed out... this is not done explicitly, but relies on Java's default values.
FVector(int[] x)
          makes a new FVector object out of a int[] array.
FVector(IVector x)
          copies an IVector, copying the data and casting to floats.
 
Method Summary
 void add(FVector x)
          Adds another FVector to this one.
static FVector add(FVector a, FVector b)
          Adds two FVector's and returns a new FVector containing the sum.
 void append(FVector x)
          appends the contents of another FVector to this one.
static FVector append(FVector x, FVector y)
          Returns a new FVector containing the first argument appended to the second.
 float average()
          Returns the average value in the array.
 float averageRW()
          Returns the average value in the array, weighting each value using reciprocal weighting.
 float covar(FVector y)
          Returns the covariance of values in this array with those in another array.
 FVector cross(FVector x)
          Returns the cross product of multiplying this with another vector.
static FVector cross(FVector x, FVector y)
          Returns the cross product of multiplying two vectors.
 int dimension()
          returns the dimension the encapsulated array.
 float distance(FVector x)
          Returns the distance between this point and one pointed two by another FVector.
static float distance(FVector a, FVector b)
          Returns the distance between two points pointed to by FVectors a and b.
 void divide(float x)
          Divides this FVector by a specified float.
static FVector divide(FVector a, float b)
          Divides a FVector by a float, and returns a new FVector containing the result.
 float dot(FVector x)
          Returns the dot product of multiplying this with another vector.
static float dot(FVector x, FVector y)
          Returns the dot product of multiplying two vectors.
 boolean equals(java.lang.Object x)
          mathematical objects are equal if their contents are.
 IVector findNClosest(int n, float v)
          What are the indices of the N closest values to a given float?
 int length()
          returns the dimension the encapsulated array.
 float[] linearFit(FVector y)
          This will return the best linear fit to a bunch of points, assuming that this FVector contains x's, and FVectory y contains y's.
 void load(java.io.BufferedReader infile)
          Load from an open BufferedReader.
 void load(java.lang.String filename)
          Loads from a text file.
 FVector makeZScores()
          returns a vector containing the Z scores for all data in the array.
 float max()
          What's the maximum value in the array?
 float median()
          Returns the median value in the array.
 float min()
          What's the minimum value in the array?
 void multiply(float x)
          Multiplies all values in this FVector by a specified int.
static FVector multiply(float b, FVector a)
          Multiplies a FVector by an int and returns a new FVector containing the result.
 void multiply(FMatrix x)
          Multplies this FVector by a FMatrix.
static FVector multiply(FMatrix b, FVector a)
          Multplies a FVector by a FMatrix, and returns a new FVector with the result.
static FVector multiply(FVector a, float b)
          Multiplies a FVector by an int and returns a new FVector containing the result.
static FVector multiply(FVector a, FMatrix b)
          Multplies a FVector by a FMatrix, and returns a new FVector with the result.
 float norm()
          What's the norm of this array?
 float normalize()
          Rescale array so the norm is one.
 float pearson(FVector y)
          Returns the Pearson correlation coefficient R of values in this array with values in another array.
 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 FVector reverse(FVector x)
          returns a vector which is the reverse of this one.
 void save(Printf outfile)
          Save to an open Printf.
 void save(java.lang.String filename)
          Saves to a new text file.
 void setValue(float x)
          Sets every value in the array to a specified float.
 void setValueAt(int x, float d)
          Sets the data at a given index.
 IVector sort()
          returns a vector of indices to sort an FVector (small -> large).
 float stdev()
          Returns the standard deviation of a distribution containing all values in the array.
 float stdevp()
          Returns the standard deviation of a distribution containing all values in the array.
 void subtract(FVector x)
          Subtracts another FVector from this one.
static FVector subtract(FVector a, FVector b)
          Subtracts one FVector's from another returns a new FVector containing the difference.
 void transform(FMatrix x)
          transforms this vector using a transformation FMatrix.
 float valueAt(int x)
          Returns the data at a given index.
 float zScore(float x)
          Shows how many standard deviations (of this array) a given float is below the average (of this array)
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

public float[] 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 'dv.data[0] = tmp' as well as 'tmp = dv.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

FVector

public FVector(int dim)
makes an emptry FVector 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

FVector

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


FVector

public FVector(float[] x)
makes a new FVector 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 FVector(), then dv.data = my_array.


FVector

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


FVector

public FVector(int[] x)
makes a new FVector object out of a int[] array. All data is copied and cast to floats.


FVector

public FVector(IVector x)
copies an IVector, copying the data and casting to floats.

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

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 float 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,
                             float 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(float x)
Sets every value in the array to a specified float.


add

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


add

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


append

public final void append(FVector x)
appends the contents of another FVector to this one.


append

public static final FVector append(FVector x,
                                   FVector y)
Returns a new FVector containing the first argument appended to the second.


reverse

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


reverse

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


subtract

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


subtract

public static final FVector subtract(FVector a,
                                     FVector b)
Subtracts one FVector's from another returns a new FVector 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(float x)
Multiplies all values in this FVector by a specified int.


multiply

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


multiply

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


multiply

public final void multiply(FMatrix x)
Multplies this FVector by a FMatrix. This will probably give you an ArrayOutOfBounds exception if the FMatrix doesn't have the right dimensions.


multiply

public static final FVector multiply(FVector a,
                                     FMatrix b)
Multplies a FVector by a FMatrix, and returns a new FVector with the result. This will probably give you an ArrayOutOfBounds exception if the FMatrix doesn't have the right dimensions.


multiply

public static final FVector multiply(FMatrix b,
                                     FVector a)
Multplies a FVector by a FMatrix, and returns a new FVector with the result. This will probably give you an ArrayOutOfBounds exception if the FMatrix doesn't have the right dimensions.


divide

public final void divide(float x)
Divides this FVector by a specified float.


divide

public static final FVector divide(FVector a,
                                   float b)
Divides a FVector by a float, and returns a new FVector containing the result.


norm

public final float norm()
What's the norm of this array?


normalize

public final float normalize()
Rescale array so the norm is one. If the norm is zero, this can't be done, so it does nothing. Note that this is not the same as normalizing a matrix.

See Also:
FMatrix.normalize()

distance

public static final float distance(FVector a,
                                   FVector b)
Returns the distance between two points pointed to by FVectors a and b. This is the norm of the FVector between the two points


distance

public final float distance(FVector x)
Returns the distance between this point and one pointed two by another FVector. This is the norm of the FVector between the two points


dot

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


dot

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


cross

public final FVector cross(FVector 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 FVector cross(FVector x,
                                  FVector 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.


average

public final float average()
Returns the average value in the array.


averageRW

public final float averageRW()
Returns the average value in the array, weighting each value using reciprocal weighting.


median

public final float median()
Returns the median value in the array. If an odd number of data points, this is the middle value in the sorted array. If an even number of data points, this is the average of the middle 2 values.


stdev

public final float stdev()
Returns the standard deviation of a distribution containing all values in the array. This is the 'sample' standard deviation.


stdevp

public final float stdevp()
Returns the standard deviation of a distribution containing all values in the array. This is the 'population' standard deviation.


covar

public final float covar(FVector y)
Returns the covariance of values in this array with those in another array.

Throws:
java.lang.IllegalArgumentException - if the arrays are not the same size.

pearson

public final float pearson(FVector y)
Returns the Pearson correlation coefficient R of values in this array with values in another array.

Throws:
java.lang.IllegalArgumentException - if the arrays are not the same size.

linearFit

public final float[] linearFit(FVector y)
This will return the best linear fit to a bunch of points, assuming that this FVector contains x's, and FVectory y contains y's. There must be at least 2 points, and the vectors must be the same size. This function returns an array of 2 floats: the slope (m) and the y-intercept (b); the linear fit is y=mx+b


zScore

public final float zScore(float x)
Shows how many standard deviations (of this array) a given float is below the average (of this array)

See Also:
makeZScores()

max

public final float max()
What's the maximum value in the array? NaN if no data.


min

public final float min()
What's the minimum value in the array? NaN if no data.


findNClosest

public final IVector findNClosest(int n,
                                  float v)
What are the indices of the N closest values to a given float? This will return a smaller IVector if this FVector is smaller than N.


makeZScores

public final FVector makeZScores()
returns a vector containing the Z scores for all data in the array.

See Also:
zScore(float)

transform

public final void transform(FMatrix x)
transforms this vector using a transformation FMatrix. The transformation matrix should have the same number of columns as the vector has dimensions, and one extra row.

See Also:
FMatrix.makeTransform(float, float, float, float, float, float)

sort

public IVector sort()
returns a vector of indices to sort an FVector (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 void save(Printf outfile)
          throws java.io.IOException
Save to an open Printf.

Throws:
java.io.IOException
See Also:
Printf

load

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

Throws:
java.io.IOException
See Also:
BufferedReader

save

public 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 void load(java.lang.String filename)
          throws java.io.IOException
Loads from a text file.

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