Class Vector
- java.lang.Object
-
- com.seedfinding.latticg.math.component.Vector
-
public final class Vector extends java.lang.ObjectA vector with double elements
-
-
Constructor Summary
Constructors Constructor Description Vector(double... numbers)Constructs a vector with the given elementsVector(int dimension)Constructs the zero vector of the given dimensionVector(int dimension, java.util.function.IntToDoubleFunction generator)Generates a vector with the given dimension, applying a function to the index to compute the value of each element
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vectoradd(Vector a)Adds the given vector to this vector, stores the result in a new vector and returns that vectorVectoraddAndSet(Vector a)Adds the given vector to this vector, modifying this vectorVectorcopy()Creates a copy of this vectorVectordivide(double scalar)Divides this vector by the given scalar, stores the result in a new vector and returns that vectorVectordivideAndSet(double scalar)Divides this vector by the given scalar, modifying this vectordoubledot(Vector v)Calculates the dot product of this vector and the given vectorbooleanequals(Vector other, double tolerance)Returns whether this vector has the same dimension as the given vector, and all elements of this vector are withintoleranceof the corresponding elements in the given vectorbooleanequals(java.lang.Object other)static VectorfromBigVector(BigVector v)Converts aBigVectorto a vector of doublesstatic VectorfromString(java.lang.String raw)Parses a string in wolfram-style vector notationdoubleget(int i)Gets the element at the given index in the vectorintgetDimension()Gets the dimension of the vectordoublegramSchmidtCoefficient(Vector v)Returns the Gram-Schmidt coefficient when this vector is projected onto the given vector, that is, the ratio of the length of the projection of this vector onto the given vector, to the given vector's length.inthashCode()booleanisZero()Returns whether this vector is the zero vectordoublemagnitude()Returns the magnitude (norm) of the vector.doublemagnitudeSq()Returns the square of the magnitude (norm) of this vectorVectormultiply(double scalar)Multiplies this vector by the given scalar, stores the result in a new vector and returns that vectorVectormultiply(Matrix m)Computesthis * m, interpreting this vector as a row vector, stores the result in a new vector and returns that vectorVectormultiplyAndSet(double scalar)Multiplies this vector by the given scalar, modifying this vectorstatic Vectorparse(StringParser parser)Parses a vector from a string parserVectorprojectOnto(Vector v)Calculates the projection of this vector onto the given vector, stores the result in a new vector and returns that vectorvoidset(int i, double value)Sets the element at the given index in the vectorVectorsubtract(Vector a)Subtracts the given vector from this vector, stores the result in a new vector and returns that vectorVectorsubtractAndSet(Vector a)Subtracts the given vector from this vector, modifying this vectorVectorswapNums(int i, int j)Swaps the two numbers at the given indices, stores the result in a new vector and returns that matrixVectorswapNumsAndSet(int i, int j)Swaps the two numbers at the given indices, modifying the vectorjava.lang.StringtoString()
-
-
-
Constructor Detail
-
Vector
public Vector(int dimension)
Constructs the zero vector of the given dimension- Parameters:
dimension- The dimension of the vector to create
-
Vector
public Vector(double... numbers)
Constructs a vector with the given elements- Parameters:
numbers- The elements of the vector
-
Vector
public Vector(int dimension, java.util.function.IntToDoubleFunction generator)Generates a vector with the given dimension, applying a function to the index to compute the value of each element- Parameters:
dimension- The dimension of the vectorgenerator- A function accepting the index and producing the desired value for that index
-
-
Method Detail
-
getDimension
public int getDimension()
Gets the dimension of the vector- Returns:
- The dimension of the vector
-
get
public double get(int i)
Gets the element at the given index in the vector- Parameters:
i- The index- Returns:
- The element at the given index
- Throws:
java.lang.IndexOutOfBoundsException- Ifiis out of bounds
-
set
public void set(int i, double value)Sets the element at the given index in the vector- Parameters:
i- The indexvalue- The value to put in that index- Throws:
java.lang.IndexOutOfBoundsException- Ifiis out of bounds
-
magnitude
public double magnitude()
Returns the magnitude (norm) of the vector.magnitudeSq()should be used instead of this method where possible- Returns:
- The magnitude of this vector
-
magnitudeSq
public double magnitudeSq()
Returns the square of the magnitude (norm) of this vector- Returns:
- The square of the magnitude of this vector
-
isZero
public boolean isZero()
Returns whether this vector is the zero vector- Returns:
- Whether this vector is the zero vector
-
add
public Vector add(Vector a)
Adds the given vector to this vector, stores the result in a new vector and returns that vector- Parameters:
a- The vector to add- Returns:
- A new vector containing the result
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
subtract
public Vector subtract(Vector a)
Subtracts the given vector from this vector, stores the result in a new vector and returns that vector- Parameters:
a- The vector to subtract- Returns:
- A new vector containing the result
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
multiply
public Vector multiply(double scalar)
Multiplies this vector by the given scalar, stores the result in a new vector and returns that vector- Parameters:
scalar- The scalar to multiply by- Returns:
- A new vector containing the result
-
multiply
public Vector multiply(Matrix m)
Computesthis * m, interpreting this vector as a row vector, stores the result in a new vector and returns that vector- Parameters:
m- The matrix to right-multiply by- Returns:
- A new vector containing the result
- Throws:
java.lang.IllegalArgumentException- If the dimension of this vector is not equal to the number of rows in the given matrix
-
divide
public Vector divide(double scalar)
Divides this vector by the given scalar, stores the result in a new vector and returns that vector- Parameters:
scalar- The scalar to divide by- Returns:
- A new vector containing the result
-
swapNums
public Vector swapNums(int i, int j)
Swaps the two numbers at the given indices, stores the result in a new vector and returns that matrix- Parameters:
i- The row to swap withjj- The row to swap withi- Returns:
- A new vector containing the result
- Throws:
java.lang.IndexOutOfBoundsException- Ifiorjis out of bounds
-
addAndSet
public Vector addAndSet(Vector a)
Adds the given vector to this vector, modifying this vector- Parameters:
a- The vector to add to this vector- Returns:
- This vector
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
subtractAndSet
public Vector subtractAndSet(Vector a)
Subtracts the given vector from this vector, modifying this vector- Parameters:
a- The vector to subtract from this vector- Returns:
- This vector
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
multiplyAndSet
public Vector multiplyAndSet(double scalar)
Multiplies this vector by the given scalar, modifying this vector- Parameters:
scalar- The scalar to multiply this vector by- Returns:
- This vector
-
divideAndSet
public Vector divideAndSet(double scalar)
Divides this vector by the given scalar, modifying this vector- Parameters:
scalar- The scalar to divide this vector by- Returns:
- This vector
-
swapNumsAndSet
public Vector swapNumsAndSet(int i, int j)
Swaps the two numbers at the given indices, modifying the vector- Parameters:
i- The row to swap withjj- The row to swap withi- Returns:
- This vector
- Throws:
java.lang.IndexOutOfBoundsException- Ifiorjis out of bounds
-
dot
public double dot(Vector v)
Calculates the dot product of this vector and the given vector- Parameters:
v- The vector to find the dot product with- Returns:
- The dot product of this vector and the given vector
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
gramSchmidtCoefficient
public double gramSchmidtCoefficient(Vector v)
Returns the Gram-Schmidt coefficient when this vector is projected onto the given vector, that is, the ratio of the length of the projection of this vector onto the given vector, to the given vector's length. In other words, multiplying the given vector by the Gram-Schmidt coefficient gives the projection of this vector onto the given vector- Parameters:
v- The vector to project onto- Returns:
- The Gram-Schmidt coefficient when this vector is projected onto the given vector
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
projectOnto
public Vector projectOnto(Vector v)
Calculates the projection of this vector onto the given vector, stores the result in a new vector and returns that vector- Parameters:
v- The vector to project onto- Returns:
- A new vector containing the result
- Throws:
java.lang.IllegalArgumentException- If the dimension of the given vector is not the same as the dimension of this vector
-
copy
public Vector copy()
Creates a copy of this vector- Returns:
- A copy of this vector
-
equals
public boolean equals(Vector other, double tolerance)
Returns whether this vector has the same dimension as the given vector, and all elements of this vector are withintoleranceof the corresponding elements in the given vector- Parameters:
other- The vector to test againsttolerance- The maximum amount each element is allowed to differ- Returns:
- Whether this vector is close enough to the given vector
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
fromString
public static Vector fromString(java.lang.String raw)
Parses a string in wolfram-style vector notation- Parameters:
raw- The string in wolfram-style vector notation- Returns:
- The parsed vector
- Throws:
ParseException- If the input is malformed
-
parse
public static Vector parse(StringParser parser)
Parses a vector from a string parser- Parameters:
parser- The parser to parse the vector from- Returns:
- The parsed vector
- Throws:
ParseException- If the input is malformed
-
-