Package maxharold.matrix
Class Vector
java.lang.Object
maxharold.matrix.Vector
Class for creating vectors and performing vector operations
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubleangleBetween(Vector v2) Calculates the angle between the current vector instance and another onestatic DoubleangleBetween(Vector v1, Vector v2) Calculates the angle between two vectorscrossProduct(Vector v2) Performs cross product with current vector instance and another instancestatic VectorcrossProduct(Vector v1, Vector v2) Performs cross product of two 3D vectorsintdim()Method that returns the dimension of the vectorPerforms dot product with current instance of vector and another onestatic DoublePerforms dot product with two vectorsget(int i) Gets element at certain indexdoubleCalculates the magnitude of the vectorscale(double scalar) Scales all elements of vector by some scalarvoidsetValue(int i, double val) Sets value of vector at certain index (does nothing if index is invalid)Converts vector to column matrixtoString()Formats vector as a stringConverts current vector to unit vector of magnitude 1double[]Method that turns the vector into an array
-
Constructor Details
-
Vector
public Vector(double... elements) Constructs a vector from an array- Parameters:
elements- the vector as an array
-
Vector
public Vector(int dim) Constructs an empty vector from a specified dimension- Parameters:
dim- the dimension of the vector
-
-
Method Details
-
dim
public int dim()Method that returns the dimension of the vector- Returns:
- the dimension of the vector
-
vectorToArray
public double[] vectorToArray()Method that turns the vector into an array- Returns:
- the vector as an array
-
toColumnMatrix
Converts vector to column matrix- Returns:
- the column vector as a matrix
-
get
Gets element at certain index- Parameters:
i- the i-th index of the vector- Returns:
- the element of the vector (returns null if invalid)
-
setValue
public void setValue(int i, double val) Sets value of vector at certain index (does nothing if index is invalid)- Parameters:
i- the index to changeval- the value to change it to
-
magnitude
public double magnitude()Calculates the magnitude of the vector- Returns:
- the magnitude
-
scale
Scales all elements of vector by some scalar- Parameters:
scalar- the scalar- Returns:
- the scaled vector
-
unitVector
Converts current vector to unit vector of magnitude 1- Returns:
- the unit vector
-
dot
Performs dot product with two vectors- Parameters:
v1- first vectorv2- second vector- Returns:
- the dot product of v1 and v2 (returns null if invalid)
-
dot
Performs dot product with current instance of vector and another one- Parameters:
v2- the second vector- Returns:
- the dot product (returns null if invalid)
-
crossProduct
Performs cross product of two 3D vectors- Parameters:
v1- the first vectorv2- the second vector- Returns:
- the cross product (returns null if invalid)
-
crossProduct
Performs cross product with current vector instance and another instance- Parameters:
v2- the second vector- Returns:
- the cross product (returns null if invalid)
-
angleBetween
Calculates the angle between two vectors- Parameters:
v1- the first vectorv2- the second vector- Returns:
- the angle between the two vectors in radians (returns null if invalid)
-
angleBetween
Calculates the angle between the current vector instance and another one- Parameters:
v2- the second vector- Returns:
- the angle between the two vectors in radians (returns null if invalid)
-
toString
Formats vector as a string
-