Class Vector

java.lang.Object
maxharold.matrix.Vector

public class Vector extends Object
Class for creating vectors and performing vector operations
  • 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

      public Matrix toColumnMatrix()
      Converts vector to column matrix
      Returns:
      the column vector as a matrix
    • get

      public Double get(int i)
      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 change
      val - the value to change it to
    • magnitude

      public double magnitude()
      Calculates the magnitude of the vector
      Returns:
      the magnitude
    • scale

      public Vector scale(double scalar)
      Scales all elements of vector by some scalar
      Parameters:
      scalar - the scalar
      Returns:
      the scaled vector
    • unitVector

      public Vector unitVector()
      Converts current vector to unit vector of magnitude 1
      Returns:
      the unit vector
    • dot

      public static Double dot(Vector v1, Vector v2)
      Performs dot product with two vectors
      Parameters:
      v1 - first vector
      v2 - second vector
      Returns:
      the dot product of v1 and v2 (returns null if invalid)
    • dot

      public Double dot(Vector v2)
      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

      public static Vector crossProduct(Vector v1, Vector v2)
      Performs cross product of two 3D vectors
      Parameters:
      v1 - the first vector
      v2 - the second vector
      Returns:
      the cross product (returns null if invalid)
    • crossProduct

      public Vector crossProduct(Vector v2)
      Performs cross product with current vector instance and another instance
      Parameters:
      v2 - the second vector
      Returns:
      the cross product (returns null if invalid)
    • angleBetween

      public static Double angleBetween(Vector v1, Vector v2)
      Calculates the angle between two vectors
      Parameters:
      v1 - the first vector
      v2 - the second vector
      Returns:
      the angle between the two vectors in radians (returns null if invalid)
    • angleBetween

      public double angleBetween(Vector v2)
      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

      public String toString()
      Formats vector as a string
      Overrides:
      toString in class Object
      Returns:
      the formatted string