Class Vector
- java.lang.Object
-
- com.seedfinding.latticg.math.component.Vector
-
public final class Vector extends java.lang.Object
A 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 Vector
add(Vector a)
Adds the given vector to this vector, stores the result in a new vector and returns that vectorVector
addAndSet(Vector a)
Adds the given vector to this vector, modifying this vectorVector
copy()
Creates a copy of this vectorVector
divide(double scalar)
Divides this vector by the given scalar, stores the result in a new vector and returns that vectorVector
divideAndSet(double scalar)
Divides this vector by the given scalar, modifying this vectordouble
dot(Vector v)
Calculates the dot product of this vector and the given vectorboolean
equals(Vector other, double tolerance)
Returns whether this vector has the same dimension as the given vector, and all elements of this vector are withintolerance
of the corresponding elements in the given vectorboolean
equals(java.lang.Object other)
static Vector
fromBigVector(BigVector v)
Converts aBigVector
to a vector of doublesstatic Vector
fromString(java.lang.String raw)
Parses a string in wolfram-style vector notationdouble
get(int i)
Gets the element at the given index in the vectorint
getDimension()
Gets the dimension of the vectordouble
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.int
hashCode()
boolean
isZero()
Returns whether this vector is the zero vectordouble
magnitude()
Returns the magnitude (norm) of the vector.double
magnitudeSq()
Returns the square of the magnitude (norm) of this vectorVector
multiply(double scalar)
Multiplies this vector by the given scalar, stores the result in a new vector and returns that vectorVector
multiply(Matrix m)
Computesthis * m
, interpreting this vector as a row vector, stores the result in a new vector and returns that vectorVector
multiplyAndSet(double scalar)
Multiplies this vector by the given scalar, modifying this vectorstatic Vector
parse(StringParser parser)
Parses a vector from a string parserVector
projectOnto(Vector v)
Calculates the projection of this vector onto the given vector, stores the result in a new vector and returns that vectorvoid
set(int i, double value)
Sets the element at the given index in the vectorVector
subtract(Vector a)
Subtracts the given vector from this vector, stores the result in a new vector and returns that vectorVector
subtractAndSet(Vector a)
Subtracts the given vector from this vector, modifying this vectorVector
swapNums(int i, int j)
Swaps the two numbers at the given indices, stores the result in a new vector and returns that matrixVector
swapNumsAndSet(int i, int j)
Swaps the two numbers at the given indices, modifying the vectorjava.lang.String
toString()
-
-
-
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
- Ifi
is 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
- Ifi
is 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 withj
j
- The row to swap withi
- Returns:
- A new vector containing the result
- Throws:
java.lang.IndexOutOfBoundsException
- Ifi
orj
is 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 withj
j
- The row to swap withi
- Returns:
- This vector
- Throws:
java.lang.IndexOutOfBoundsException
- Ifi
orj
is 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 withintolerance
of 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:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in 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
-
-