Class VectorOps

java.lang.Object
org.bzdev.math.VectorOps

public class VectorOps extends Object
Vector-algebra operations. These include the addition of two vectors, multiplication by a scalar, dot products, cross products (for 3-dimensional vectors)
  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    add(double[] v1, double[] v2)
    Compute the sum of two vectors.
    static double[]
    add(double[] result, double[] v1, double[] v2)
    Compute the sum of two vectors, storing the results.
    static double[]
    add(double[] result, int offset, double[] v1, int off1, double[] v2, int off2, int n)
    Compute the sum of two vectors, storing the results, with offsets into arrays determining the vectors.
    static double[]
    createUnitVector(double... xi)
    Create a unit vector given the components of a parallel vector.
    static double[]
    createUnitVector3(double phi, double theta)
    Create a three dimensional unit vector given spherical coordinates.
    static double[]
    createVector(double... xi)
    Create a vector given its components.
    static double[]
    createVector3(double r, double phi, double theta)
    Create a vector given its representation in spherical coordinates.
    static double[]
    crossProduct(double[] v1, double[] v2)
    Compute the cross product of two vectors.
    static double[]
    crossProduct(double[] result, double[] v1, double[] v2)
    Compute the cross product of two vectors, storing the results.
    static double[]
    crossProduct(double[] result, int offset, double[] v1, int off1, double[] v2, int off2)
    Compute the cross product of two vectors represented as subarrays.
    static double
    dotCrossProduct(double[] v1, double[] v2, double[] v3)
    Compute the dot product of a vector with the cross product of two other vectors.
    static double
    dotCrossProduct(double[] v1, int off1, double[] v2, int off2, double[] v3, int off3)
    Compute the dot product of a vector with the cross product of two other vectors, with the vectors represented by an array with an offset to the start of the vector.
    static double
    dotProduct(double[] v1, double[] v2)
    Compute the dot product of two vectors.
    static double
    dotProduct(double[] v1, int off1, double[] v2, int off2, int n)
    Compute the dot product of two vectors, represented as subarrays.
    static double[]
    multiply(double[] result, double scalar, double[] v)
    Multiply a vector by a scalar, storing the results.
    static double[]
    multiply(double[] result, int rOffset, double scalar, double[] v, int vOffset, int n)
    Multiply a vector by a scalar, storing the results.
    static double[]
    multiply(double scalar, double[] v)
    Multiply a vector by a scalar.
    static double
    norm(double[] v)
    Compute the norm of a vector.
    static double
    norm(double[] v, int offset, int n)
    Compute the norm of a vector stored in an array at a specified offset.
    static double[]
    normalize(double[] v)
    Normalize a vector.
    static void
    normalize(double[] v, int offset, int n)
    Normalize a vector of length n, stored in an array at a specified offset.
    static double
    normSquared(double[] v)
    Compute the square of the norm of a vector.
    static double
    normSquared(double[] v, int offset, int n)
    Compute the square of the norm of a vector stored in an array at a specified offset.
    static double[]
    sub(double[] v1, double[] v2)
    Compute the difference of two vectors.
    static double[]
    sub(double[] result, double[] v1, double[] v2)
    Compute the difference of two vectors, storing the results.
    static double[]
    sub(double[] result, int offset, double[] v1, int off1, double[] v2, int off2, int n)
    Compute the difference of two vectors, storing the results, with offsets into arrays determining the vectors.
    static double[]
    unitVector(double[] v)
    Get a unit vector with the same direction as a specified vector.
    static double[]
    unitVector(double[] result, double[] v)
    Get a unit vector with the same direction as a specified vector, storing the results.
    static double[]
    unitVector(double[] result, int rOffset, double[] v, int vOffset, int n)
    Get a unit vector with the same direction as a specified vector, storing the results and with each represented by an array and an offset.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • add

      public static double[] add(double[] v1, double[] v2) throws IllegalArgumentException
      Compute the sum of two vectors.
      Parameters:
      v1 - the first vector
      v2 - the second vector
      Returns:
      the sum v1+v2
      Throws:
      IllegalArgumentException - the vectors differ in length
    • add

      public static double[] add(double[] result, double[] v1, double[] v2) throws IllegalArgumentException
      Compute the sum of two vectors, storing the results. The vectors result, v1, and/or v2 may be identical.
      Parameters:
      result - a vector that will store the vector v1+v2; null if a new array should be allocated
      v1 - the first vector
      v2 - the second vector
      Returns:
      the vector equal to v1+v2
      Throws:
      IllegalArgumentException - the vectors differ in length
    • add

      public static double[] add(double[] result, int offset, double[] v1, int off1, double[] v2, int off2, int n)
      Compute the sum of two vectors, storing the results, with offsets into arrays determining the vectors. If result and v1 or v2 are the same vector, then the offsets for result and v1 or v2 must either be identical or such that the areas used do not overlap.
      Parameters:
      result - a vector that will store the vector v1+v2; null if a new array should be allocated
      offset - the offset into the array 'result' (the first argument) at which the vector will be stored
      v1 - the first vector
      off1 - the offset into the first vector's array
      v2 - the second vector
      off2 - the offset into the second vector's array
      n - the length of the vectors
      Returns:
      the result vector with the results starting at the specified offset
    • sub

      public static double[] sub(double[] v1, double[] v2) throws IllegalArgumentException
      Compute the difference of two vectors.
      Parameters:
      v1 - the first vector
      v2 - the second vector
      Returns:
      the difference v1-v2
      Throws:
      IllegalArgumentException - the vectors differ in length
    • sub

      public static double[] sub(double[] result, double[] v1, double[] v2) throws IllegalArgumentException
      Compute the difference of two vectors, storing the results. The vectors result, v1, and/or v2 may be identical vectors.
      Parameters:
      result - a vector that will store the vector v1-v2; null if a new array should be allocated
      v1 - the first vector
      v2 - the second vector
      Returns:
      the vector equal to v1-v2
      Throws:
      IllegalArgumentException - the vectors differ in length
    • sub

      public static double[] sub(double[] result, int offset, double[] v1, int off1, double[] v2, int off2, int n)
      Compute the difference of two vectors, storing the results, with offsets into arrays determining the vectors. If result and v1 or v2 are the same vector, then the offsets for result and v1 or v2 must either be identical or such that the areas used do not overlap.
      Parameters:
      result - a vector that will store the vector v1-v2; null if a new array should be allocated
      offset - the offset into the array 'result' (the first argument) at which the vector will be stored
      v1 - the first vector
      off1 - the offset into the first vector's array
      v2 - the second vector
      off2 - the offset into the second vector's array
      n - the length of each vector
      Returns:
      the result vector with the results starting at the specified offset
      Throws:
      IllegalArgumentException - the vectors differ in length
    • multiply

      public static double[] multiply(double scalar, double[] v)
      Multiply a vector by a scalar.
      Parameters:
      scalar - the scalar
      v - the vector
      Returns:
      a vector containing the product of the scalar with the argument v (which is not modified)
    • multiply

      public static double[] multiply(double[] result, double scalar, double[] v) throws IllegalArgumentException
      Multiply a vector by a scalar, storing the results. The vectors result and v may be identical.
      Parameters:
      result - an array holding the result of the multiplication; null if a new array should be allocated
      scalar - the scalar
      v - the vector
      Returns:
      the result vector
      Throws:
      IllegalArgumentException - the vectors differ in length
    • multiply

      public static double[] multiply(double[] result, int rOffset, double scalar, double[] v, int vOffset, int n)
      Multiply a vector by a scalar, storing the results. If result and v are the same vector, then either the offsets must either be identical or such that the areas used do not overlap.
      Parameters:
      result - an array holding the result of the multiplication; null if a new array should be allocated
      rOffset - the offset into the 'result' array for storing the vector
      scalar - the scalar
      v - the array storing the vector to be multiplied
      vOffset - the offset into the array v for the start of the vector
      n - the length of the vectors
      Returns:
      the result vector with the results starting at the specified offset
    • dotProduct

      public static double dotProduct(double[] v1, double[] v2) throws IllegalArgumentException
      Compute the dot product of two vectors.
      Parameters:
      v1 - the first vector
      v2 - the second vector
      Returns:
      the dot product of v1 and v2
      Throws:
      IllegalArgumentException - the arrays v1 and v2 do not have the same length
    • dotProduct

      public static double dotProduct(double[] v1, int off1, double[] v2, int off2, int n) throws IllegalArgumentException
      Compute the dot product of two vectors, represented as subarrays.
      Parameters:
      v1 - the first vector's array
      off1 - the offset into v1 for the start of the vector
      v2 - the second vector's array
      off2 - the offset into v2 for the start of the vector
      n - the lengths of the vectors
      Returns:
      the dot product of v1 and v2
      Throws:
      IllegalArgumentException - the arrays v1 and v2 do not have n elements past their offsets
    • crossProduct

      public static double[] crossProduct(double[] v1, double[] v2) throws IllegalArgumentException
      Compute the cross product of two vectors.
      Parameters:
      v1 - the first vector
      v2 - the second vector
      Returns:
      the cross product of v1 with v2
      Throws:
      IllegalArgumentException - the arrays v1 and v2 do not have the same length
    • crossProduct

      public static double[] crossProduct(double[] result, double[] v1, double[] v2) throws IllegalArgumentException
      Compute the cross product of two vectors, storing the results.
      Parameters:
      result - a vector that will be set to the cross product v1 × v2; null if a new vector should be allocated
      v1 - the first vector v1
      v2 - the second vector v2
      Returns:
      result, or a new vector equal to v1×v2 if result is null
      Throws:
      IllegalArgumentException - the arrays v1 and v2 do not have the same length
    • crossProduct

      public static double[] crossProduct(double[] result, int offset, double[] v1, int off1, double[] v2, int off2) throws IllegalArgumentException
      Compute the cross product of two vectors represented as subarrays. If result and v1 or v2 are the same vector, then the offsets for result and v1 or v2 must either be identical or such that the areas used do not overlap. The arrays lengths must be at least 3, and the offsets must be such that the vectors fit within their arrays. All vectors have a length of 3.
      Parameters:
      result - an array holding a vector that will be set to the cross product v1 × v2 at the specified offset
      offset - the offset into the array 'result' (the first argument) at which the vector will be stored; ignored if 'result' is null
      v1 - the first vector's array
      off1 - the offset into the first vector's array
      v2 - the second vector's array
      off2 - the offset into the second vector's array
      Returns:
      result, or a new vector if result is null
      Throws:
      IllegalArgumentException - the arrays v1 and v2 are too short
    • dotCrossProduct

      public static double dotCrossProduct(double[] v1, double[] v2, double[] v3) throws IllegalArgumentException
      Compute the dot product of a vector with the cross product of two other vectors. The value it computes, v1⋅(v2×v3), is sometimes called the scalar triple product, written as [v1,v2,v3].
      Parameters:
      v1 - the first vector v1
      v2 - the second vector v2
      v3 - the third vector v3
      Returns:
      the value v1⋅(v2×v3)
      Throws:
      IllegalArgumentException - the arrays v1 and v2 do not have the same length
    • dotCrossProduct

      public static double dotCrossProduct(double[] v1, int off1, double[] v2, int off2, double[] v3, int off3) throws IllegalArgumentException
      Compute the dot product of a vector with the cross product of two other vectors, with the vectors represented by an array with an offset to the start of the vector.
      Parameters:
      v1 - the array for the first vector v1
      off1 - the offset into the array v1 for the start of the vector
      v2 - the array for the second vector v2
      off2 - the offset into the array v2 for the start of the vector
      v3 - the array for the third vector v3
      off3 - the offset into the array v3 for the start of the vector
      Returns:
      the value v1⋅(v2×v3)
      Throws:
      IllegalArgumentException - the arrays v1 and v2 do not have the same length
    • norm

      public static double norm(double[] v)
      Compute the norm of a vector.
      Parameters:
      v - the vector
      Returns:
      the norm
    • normSquared

      public static double normSquared(double[] v)
      Compute the square of the norm of a vector.
      Parameters:
      v - the vector
      Returns:
      the norm
    • norm

      public static double norm(double[] v, int offset, int n)
      Compute the norm of a vector stored in an array at a specified offset.
      Parameters:
      v - the vector's array
      offset - the offset into v
      n - the length of the vector
      Returns:
      the norm
    • normSquared

      public static double normSquared(double[] v, int offset, int n)
      Compute the square of the norm of a vector stored in an array at a specified offset.
      Parameters:
      v - the vector's array
      offset - the offset into v
      n - the length of the vector
      Returns:
      the norm
    • normalize

      public static double[] normalize(double[] v) throws IllegalArgumentException
      Normalize a vector.
      Parameters:
      v - the vector
      Returns:
      the vector that was normalized
      Throws:
      IllegalArgumentException - the vector's norm was zero
    • normalize

      public static void normalize(double[] v, int offset, int n) throws IllegalArgumentException
      Normalize a vector of length n, stored in an array at a specified offset.
      Parameters:
      v - the vector's array
      offset - the offset into the array
      n - the length (dimension) of the vector
      Throws:
      IllegalArgumentException - n is 0, n is negative, the vector's norm is zero, or the vector's length is not compatible with the specified value of n and offset
    • unitVector

      public static double[] unitVector(double[] v) throws IllegalArgumentException
      Get a unit vector with the same direction as a specified vector.
      Parameters:
      v - a vector
      Returns:
      a unit vector
      Throws:
      IllegalArgumentException - n is 0, n is negative, the vector's norm is zero, or the vector's length is not compatible with the specified value of n and offset
    • unitVector

      public static double[] unitVector(double[] result, double[] v) throws IllegalArgumentException
      Get a unit vector with the same direction as a specified vector, storing the results.
      Parameters:
      result - an array to hold the results
      v - a vector
      Returns:
      the resulting vector
      Throws:
      IllegalArgumentException - the vectors differ in length or the v's norm is 0
    • unitVector

      public static double[] unitVector(double[] result, int rOffset, double[] v, int vOffset, int n)
      Get a unit vector with the same direction as a specified vector, storing the results and with each represented by an array and an offset. If result and v are the same vector, then either the offsets must either be identical or such that the areas used do not overlap.
      Parameters:
      result - an array to hold the results; null if a new array should be allocated
      rOffset - the offset into the 'result' array for storing the vector
      v - a vector
      vOffset - the offset into the array v for the start of the vector
      n - the length of the vector.
      Returns:
      the result vector with the results starting at the specified offset
      Throws:
      IllegalArgumentException - the vectors differ in length, n is not positive, the vector's norm is zero, or the vector's length is not compatible with the specified value of n and offset
    • createVector

      public static double[] createVector(double... xi)
      Create a vector given its components.
      Parameters:
      xi - the components of a vector
      Returns:
      the vector
    • createUnitVector

      public static double[] createUnitVector(double... xi)
      Create a unit vector given the components of a parallel vector.
      Parameters:
      xi - the components of a vector
      Returns:
      the vector
    • createUnitVector3

      public static double[] createUnitVector3(double phi, double theta)
      Create a three dimensional unit vector given spherical coordinates.
      Parameters:
      phi - the angle around the Z axis in the counterclockwise direction, starting from the positive X axis
      theta - the angle from the positive Z axis.
      Returns:
      the unit vector
    • createVector3

      public static double[] createVector3(double r, double phi, double theta)
      Create a vector given its representation in spherical coordinates.
      Parameters:
      r - the length of the vector
      phi - the angle around the Z axis in the counterclockwise direction, starting from the positive X axis
      theta - the angle from the positive Z axis.
      Returns:
      the vector