org.strbio.math
Class DVector

java.lang.Object
  extended by org.strbio.math.DVector
Direct Known Subclasses:
DHalfMatrix

public class DVector
extends java.lang.Object

a class to encapsulate a 1-D array of doubles. 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 Double.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, DMatrix

Field Summary
 double[] 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
DVector()
          makes a blank vector of dimension 0; you can assign a int[] to the data directly, or load data in from a file
DVector(double[] x)
          makes a new DVector object out of a int[] array.
DVector(DVector x)
          copies another DVector.
DVector(int dim)
          makes an emptry DVector object with a specified dimension All data should be zeroed out... this is not done explicitly, but relies on Java's default values.
DVector(int[] x)
          makes a new DVector object out of a int[] array.
DVector(IVector x)
          copies an IVector, copying the data and casting to doubles.
 
Method Summary
 void add(DVector x)
          Adds another DVector to this one.
static DVector add(DVector a, DVector b)
          Adds two DVector's and returns a new DVector containing the sum.
 void append(DVector x)
          appends the contents of another DVector to this one.
static DVector append(DVector x, DVector y)
          Returns a new DVector containing the first argument appended to the second.
 double average()
          Returns the average value in the array.
 double averageRW()
          Returns the average value in the array, weighting each value using reciprocal weighting.
 double covar(DVector y)
          Returns the covariance of values in this array with those in another array.
 DVector cross(DVector x)
          Returns the cross product of multiplying this with another vector.
static DVector cross(DVector x, DVector y)
          Returns the cross product of multiplying two vectors.
 int dimension()
          returns the dimension the encapsulated array.
 double distance(DVector x)
          Returns the distance between this point and one pointed two by another DVector.
static double distance(DVector a, DVector b)
          Returns the distance between two points pointed to by DVectors a and b.
 void divide(double x)
          Divides this DVector by a specified double.
static DVector divide(DVector a, double b)
          Divides a DVector by a double, and returns a new DVector containing the result.
 double dot(DVector x)
          Returns the dot product of multiplying this with another vector.
static double dot(DVector x, DVector 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, double v)
          What are the indices of the N closest values to a given double?
 int length()
          returns the dimension the encapsulated array.
 double[] linearFit(DVector y)
          This will return the best linear fit to a bunch of points, assuming that this DVector contains x's, and DVectory 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.
 DVector makeZScores()
          returns a vector containing the Z scores for all data in the array.
 double max()
          What's the maximum value in the array?
 double median()
          Returns the median value in the array.
 double min()
          What's the minimum value in the array?
 void multiply(DMatrix x)
          Multplies this DVector by a DMatrix.
static DVector multiply(DMatrix b, DVector a)
          Multplies a DVector by a DMatrix, and returns a new DVector with the result.
 void multiply(double x)
          Multiplies all values in this DVector by a specified int.
static DVector multiply(double b, DVector a)
          Multiplies a DVector by an int and returns a new DVector containing the result.
static DVector multiply(DVector a, DMatrix b)
          Multplies a DVector by a DMatrix, and returns a new DVector with the result.
static DVector multiply(DVector a, double b)
          Multiplies a DVector by an int and returns a new DVector containing the result.
 double norm()
          What's the norm of this array?
 double normalize()
          Rescale array so the norm is one.
 double pearson(DVector 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 DVector reverse(DVector 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(double x)
          Sets every value in the array to a specified double.
 void setValueAt(int x, double d)
          Sets the data at a given index.
 IVector sort()
          returns a vector of indices to sort an DVector (small -> large).
 double stdev()
          Returns the standard deviation of a distribution containing all values in the array.
 double stdevp()
          Returns the standard deviation of a distribution containing all values in the array.
 void subtract(DVector x)
          Subtracts another DVector from this one.
static DVector subtract(DVector a, DVector b)
          Subtracts one DVector's from another returns a new DVector containing the difference.
 void transform(DMatrix x)
          transforms this vector using a transformation DMatrix.
 double valueAt(int x)
          Returns the data at a given index.
 double zScore(double x)
          Shows how many standard deviations (of this array) a given double 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 double[] 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

DVector

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

DVector

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


DVector

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


DVector

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


DVector

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


DVector

public DVector(IVector x)
copies an IVector, copying the data and casting to doubles.

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


add

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


add

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


append

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


append

public static final DVector append(DVector x,
                                   DVector y)
Returns a new DVector 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 DVector reverse(DVector x)
returns a vector which is the reverse of this one.


subtract

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


subtract

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


multiply

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


multiply

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


multiply

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


multiply

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


multiply

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


divide

public final void divide(double x)
Divides this DVector by a specified double.


divide

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


norm

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


normalize

public final double 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:
DMatrix.normalize()

distance

public static final double distance(DVector a,
                                    DVector b)
Returns the distance between two points pointed to by DVectors a and b. This is the norm of the DVector between the two points


distance

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


dot

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


dot

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


cross

public final DVector cross(DVector 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 DVector cross(DVector x,
                                  DVector 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 double average()
Returns the average value in the array.


averageRW

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


median

public final double 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 double stdev()
Returns the standard deviation of a distribution containing all values in the array. This is the 'sample' standard deviation.


stdevp

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


covar

public final double covar(DVector 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 double pearson(DVector 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 double[] linearFit(DVector y)
This will return the best linear fit to a bunch of points, assuming that this DVector contains x's, and DVectory 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 doubles: the slope (m) and the y-intercept (b); the linear fit is y=mx+b


zScore

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

See Also:
makeZScores()

max

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


min

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


findNClosest

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


makeZScores

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

See Also:
zScore(double)

transform

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

See Also:
DMatrix.makeTransform(double, double, double, double, double, double)

sort

public IVector sort()
returns a vector of indices to sort an DVector (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