Package io.bhagat.math.linearalgebra
Class Vector
- java.lang.Object
-
- io.bhagat.math.linearalgebra.Vector
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<Vector>
public class Vector extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<Vector>
This class is a class representing a Vector of any number of dimensions- Author:
- Bhagat
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classVector.CrossProductMatrixstatic classVector.InvalidLengthExceptionstatic classVector.OutOfDimensionsExceptionstatic classVector.VectorIndex
-
Field Summary
Fields Modifier and Type Field Description private double[]datathe array storing the data of the vectorprivate static longserialVersionUIDprivate intsizethe length of the stored data
-
Constructor Summary
Constructors Constructor Description Vector(double... args)constructs a new Vector using componentsVector(int length)constructs a new Vector with a set number of dimensionsVector(int... args)constructs a new Vector using int componentsVector(java.lang.Double... args)constructs a new Vector using components
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vectoradd(double x)adds a scalar to each value in the VectorVectoradd(Vector v)Performs element wise additionVectorclone()static Vectorclone(Vector v)static clone function that calls the clone function of a vectorintcompareTo(Vector v)compares two vectors by determinantstatic Vectorcross(Vector... vectors)computes the cross product of the input vectorsVectordivide(double scalar)Performs scalar division (element wise) on the Vectorstatic Vectordivide(double scalar, Vector a)static divide scalar functionVectordivide(Vector vector)Element-wise Vector divisionstatic Vectordivide(Vector a, double scalar)static divide scalar functiondoubledot(Vector vector)performs the dot product with another vectorstatic doubledot(Vector a, Vector b)static dot product function that calls the dot function of a vectorbooleanequals(Vector v)checks if two matrices have the same datastatic VectorgenerateUnitVector(int index, int size)creates a unit vectordoubleget(int index)Gets the data at a particular indexvoidget(Vector.VectorIndex vectorIndex)Gets data from an index using the VectorIndex object and stores the value into this objectdouble[]getData()doublegetMagnitude()intgetSize()doublegetSum()static doubleinner(Vector a, Vector b)performs the inner product on two vectorsVectormap(Function<java.lang.Double,java.lang.Double> function)maps a function onto each element in the vectorVectormapWithIndex(Function<Vector.VectorIndex,java.lang.Double> function)maps a function onto each element in the vectorVectormultiply(double scalar)Performs scalar multiplication (element wise) on the Vectorstatic Vectormultiply(double scalar, Vector a)static multiply scalar functionVectormultiply(Vector v)element wise multiplicationstatic Vectormultiply(Vector a, double scalar)static multiply scalar functionVectornormalize()booleanorthogonal(Vector v)Checks if this vector and the one sent in are orthogonalstatic booleanorthogonal(Vector v1, Vector v2)Checks if two vectors are orthogonalstatic Vector[]orthogonalize(Vector[] x)make the input vectors orthogonalstatic Matrixouter(Vector a, Vector b)performs the outer product on two vectorsstatic voidprint(Vector... vectors)prints any number of vectorsvoidrandomize()Fills the Vector with random values from -1 to 1voidrandomize(double min, double max)Fills the Vector with random values from min to maxvoidset(int index, double value)sets the value at a certain indexvoidset(Vector.VectorIndex vectorIndex)Set a value in the vector using a VectorIndexVectorsubtract(double x)subtracts a scalar to each value in the VectorVectorsubtract(Vector v)Performs element wise subtractionMatrixtoMatrix()converts the vector into a matrix columnMatrixtoMatrixColumn()converts the vector into a matrix columnMatrixtoMatrixRow()converts the vector into a matrix rowjava.lang.StringtoString()
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
data
private double[] data
the array storing the data of the vector
-
size
private int size
the length of the stored data
-
-
Constructor Detail
-
Vector
public Vector(double... args)
constructs a new Vector using components- Parameters:
args- The components to set into the Vector
-
Vector
public Vector(java.lang.Double... args)
constructs a new Vector using components- Parameters:
args- The components to set into the Vector
-
Vector
public Vector(int... args)
constructs a new Vector using int components- Parameters:
args- The components to set into the Vector
-
Vector
public Vector(int length)
constructs a new Vector with a set number of dimensions- Parameters:
length- The number of dimensions in the Vector
-
-
Method Detail
-
dot
public double dot(Vector vector) throws Vector.OutOfDimensionsException, Vector.InvalidLengthException
performs the dot product with another vector- Parameters:
vector- the vector to dot with- Returns:
- returns scalar result
- Throws:
Vector.OutOfDimensionsException- thrown when a value in the specified vector does not have a valueVector.InvalidLengthException- thrown when the two vectors have unequal lengths
-
add
public Vector add(Vector v)
Performs element wise addition- Parameters:
v- the vector to add to this one- Returns:
- a reference to this vector
-
subtract
public Vector subtract(Vector v)
Performs element wise subtraction- Parameters:
v- the vector to subtract from this one- Returns:
- a reference to this vector
-
add
public Vector add(double x)
adds a scalar to each value in the Vector- Parameters:
x- the scalar to add- Returns:
- this vector
-
subtract
public Vector subtract(double x)
subtracts a scalar to each value in the Vector- Parameters:
x- the scalar to subtract- Returns:
- this vector
-
multiply
public Vector multiply(double scalar)
Performs scalar multiplication (element wise) on the Vector- Parameters:
scalar- the scalar to multiply with- Returns:
- returns this vector after the multiplication
-
multiply
public Vector multiply(Vector v)
element wise multiplication- Parameters:
v- vector to multiply with- Returns:
- returns this vector after the multiplication
-
divide
public Vector divide(double scalar)
Performs scalar division (element wise) on the Vector- Parameters:
scalar- the scalar to divide with- Returns:
- returns this vector after the division
-
divide
public Vector divide(Vector vector)
Element-wise Vector division- Parameters:
vector- the vector to divide by- Returns:
- this vector after the division
-
randomize
public void randomize(double min, double max)Fills the Vector with random values from min to max- Parameters:
min- the minimum random numbermax- the maximum random number
-
randomize
public void randomize()
Fills the Vector with random values from -1 to 1
-
getMagnitude
public double getMagnitude()
- Returns:
- the magnitude of the vector
-
compareTo
public int compareTo(Vector v)
compares two vectors by determinant- Specified by:
compareToin interfacejava.lang.Comparable<Vector>- Parameters:
v- the vector to compare to- Returns:
- the integer representing which vector's magnitude is greater
-
equals
public boolean equals(Vector v)
checks if two matrices have the same data- Parameters:
v- the vector to compare with- Returns:
- whether or not they are the same
-
getSum
public double getSum()
- Returns:
- the sum of each component
-
get
public double get(int index) throws Vector.OutOfDimensionsExceptionGets the data at a particular index- Parameters:
index- the index- Returns:
- the data at the index
- Throws:
Vector.OutOfDimensionsException- when the index is invalid
-
get
public void get(Vector.VectorIndex vectorIndex) throws Vector.OutOfDimensionsException
Gets data from an index using the VectorIndex object and stores the value into this object- Parameters:
vectorIndex- the vector index object- Throws:
Vector.OutOfDimensionsException- when the index is invalid
-
set
public void set(int index, double value) throws Vector.OutOfDimensionsExceptionsets the value at a certain index- Parameters:
index- the indexvalue- the value- Throws:
Vector.OutOfDimensionsException- if the index is out of the dimensions of the vector
-
set
public void set(Vector.VectorIndex vectorIndex) throws Vector.OutOfDimensionsException
Set a value in the vector using a VectorIndex- Parameters:
vectorIndex- the VectorIndex object holding the index and value- Throws:
Vector.OutOfDimensionsException- if the index is out of the dimensions of the vector
-
map
public Vector map(Function<java.lang.Double,java.lang.Double> function)
maps a function onto each element in the vector- Parameters:
function- the function to map- Returns:
- a reference of this vector after the mapping
-
mapWithIndex
public Vector mapWithIndex(Function<Vector.VectorIndex,java.lang.Double> function)
maps a function onto each element in the vector- Parameters:
function- the function to map that receives a VectorIndex- Returns:
- a reference of this vector after the mapping
-
toMatrixColumn
public Matrix toMatrixColumn()
converts the vector into a matrix column- Returns:
- the matrix with one column
-
toMatrixRow
public Matrix toMatrixRow()
converts the vector into a matrix row- Returns:
- the matrix with one row
-
toMatrix
public Matrix toMatrix()
converts the vector into a matrix column- Returns:
- the matrix with one column
-
clone
public Vector clone()
- Overrides:
clonein classjava.lang.Object- Returns:
- a copy with a new memory allocation
-
orthogonal
public boolean orthogonal(Vector v)
Checks if this vector and the one sent in are orthogonal- Parameters:
v- the second vector- Returns:
- a boolean that is true if they are orthogonal
-
normalize
public Vector normalize()
- Returns:
- the unit vector with the same direction as this vector
-
getData
public double[] getData()
- Returns:
- the data
-
getSize
public int getSize()
- Returns:
- the size
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representing this Vector object in the form <x1, x2, . . . xn>
-
clone
public static Vector clone(Vector v)
static clone function that calls the clone function of a vector- Parameters:
v- the vector to clone- Returns:
- the cloned vector with a new memory allocation
-
dot
public static double dot(Vector a, Vector b) throws Vector.OutOfDimensionsException, Vector.InvalidLengthException
static dot product function that calls the dot function of a vector- Parameters:
a- first vectorb- vector to dot with- Returns:
- the dot product of the two
- Throws:
Vector.OutOfDimensionsException- when the vectors are not the same length but is not caught by InvalidLengthExceptionVector.InvalidLengthException- when the vectors are not the same length
-
multiply
public static Vector multiply(Vector a, double scalar)
static multiply scalar function- Parameters:
a- the vector to multiplyscalar- the scalar to multiply- Returns:
- the resultant vector
-
multiply
public static Vector multiply(double scalar, Vector a)
static multiply scalar function- Parameters:
scalar- the scalar to multiplya- the vector to multiply- Returns:
- the resultant vector
-
divide
public static Vector divide(Vector a, double scalar)
static divide scalar function- Parameters:
a- the vector to dividescalar- the scalar to divide- Returns:
- the resultant vector
-
divide
public static Vector divide(double scalar, Vector a)
static divide scalar function- Parameters:
scalar- the scalar to dividea- the vector to divide- Returns:
- the resultant vector
-
cross
public static Vector cross(Vector... vectors)
computes the cross product of the input vectors- Parameters:
vectors- the vectors to cross- Returns:
- the resultant vector
-
inner
public static double inner(Vector a, Vector b)
performs the inner product on two vectors- Parameters:
a- a vectorb- another vector- Returns:
- the inner product
-
outer
public static Matrix outer(Vector a, Vector b)
performs the outer product on two vectors- Parameters:
a- a vectorb- another vector- Returns:
- the matrix result
-
orthogonalize
public static Vector[] orthogonalize(Vector[] x)
make the input vectors orthogonal- Parameters:
x- the original vectors- Returns:
- the orthogonalized vectors
-
generateUnitVector
public static Vector generateUnitVector(int index, int size)
creates a unit vector- Parameters:
index- this value will be 1size- the size of the vector- Returns:
- the unit vector
-
print
public static void print(Vector... vectors)
prints any number of vectors- Parameters:
vectors- the vectors to print
-
-