Class LinAlgHelpers


  • public class LinAlgHelpers
    extends java.lang.Object
    Basic vector and matrix operations implemented on double[] and double[][].
    Author:
    Tobias Pietzsch, Stephan Saalfeld
    • Constructor Summary

      Constructors 
      Constructor Description
      LinAlgHelpers()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void add​(double[][] A, double[][] B, double[][] C)
      set C = A + B.
      static void add​(double[] a, double[] b, double[] c)
      set c = a + b.
      static double angleFromR​(double[][] R)
      compute the angle of rotation from a rotation matrix.
      static void axisFromR​(double[][] R, double[] a)
      compute the axis of rotation from a rotation matrix.
      static int cols​(double[][] A)  
      static void copy​(double[][] A, double[][] C)
      set C = A, where A is a matrix.
      static void cross​(double[] a, double[] b, double[] c)
      compute cross product, set c = a ^ b.
      static double det3x3​(double[] a)
      Calculates the determinant of a 3x3 matrix given as a double[] (row major).
      static double det3x3​(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
      Calculate the determinant of a 3x3 matrix.
      static double distance​(double[] a, double[] b)
      get the length of (a - b).
      static double dot​(double[] a, double[] b)
      compute dot product a * b.
      static void getCol​(int c, double[][] A, double[] b)
      extract column c of A into vector b.
      static void getRow​(int r, double[][] A, double[] b)
      extract row r of A into vector b.
      static void invert3x3​(double[] m)
      Invert a 3x3 matrix given as row major double[] in place.
      static double[] invert3x3​(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
      Invert a 3x3 matrix given as elements in row major order.
      static void invertSymmetric2x2​(double[][] m, double[][] inverse)
      Inverts a (invertible) symmetric 2x2 matrix.
      static void invertSymmetric3x3​(double[][] m, double[][] inverse)
      Inverts a (invertible) symmetric 3x3 matrix.
      static double length​(double[] a)
      get the length of a.
      static void mult​(double[][] A, double[][] B, double[][] C)
      set C = A * B.
      static void mult​(double[][] A, double[] b, double[] c)
      set c = A * b.
      static void multABT​(double[][] A, double[][] B, double[][] C)
      set C = A * B^T.
      static void multATB​(double[][] A, double[][] B, double[][] C)
      set C = A^T * B.
      static void multT​(double[][] A, double[] b, double[] c)
      set c = A^T * b.
      static void normalize​(double[] a)
      normalize a, i.e., scale to unit length.
      static void outer​(double[] a, double[] b, double[][] C)
      compute outer product, set C = a * b^T.
      static void quaternionApply​(double[] q, double[] p, double[] qp)
      Apply quaternion rotation q to 3D point p, set qp = q * p.
      static void quaternionFromAngleAxis​(double[] axis, double angle, double[] q)
      compute a quaternion from rotation axis and angle.
      static void quaternionFromR​(double[][] R, double[] q)
      compute a unit quaternion from a rotation matrix.
      static void quaternionInvert​(double[] p, double[] q)
      invert quaternion, set q = p^{-1}.
      static void quaternionMultiply​(double[] p, double[] q, double[] pq)
      compute the quaternion product pq = p * q.
      static void quaternionPower​(double[] q, double a, double[] qa)
      compute the power of a quaternion q raised to the exponent a.
      static void quaternionToR​(double[] q, double[][] R)
      compute a rotation matrix from a unit quaternion.
      static int rows​(double[] a)  
      static int rows​(double[][] A)  
      static void scale​(double[][] A, double b, double[][] C)
      set C = A * b, where A is a matrix and b is scalar.
      static void scale​(double[] a, double b, double[] c)
      set c = a * b, where a is a vector and b is scalar.
      static void setCol​(int c, double[] a, double[][] B)
      set column c of B to vector a.
      static void setRow​(int r, double[] a, double[][] B)
      set row r of B to vector a.
      static double squareDistance​(double[] a, double[] b)
      get the squared length of (a - b).
      static double squareLength​(double[] a)
      get the squared length of a.
      static void subtract​(double[] a, double[] b, double[] c)
      set c = a - b.
      static java.lang.String toString​(double[] a)  
      static java.lang.String toString​(double[][] A)  
      static java.lang.String toString​(double[][] A, java.lang.String format)  
      static java.lang.String toString​(double[] a, java.lang.String format)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LinAlgHelpers

        public LinAlgHelpers()
    • Method Detail

      • rows

        public static int rows​(double[] a)
      • rows

        public static int rows​(double[][] A)
      • cols

        public static int cols​(double[][] A)
      • squareLength

        public static double squareLength​(double[] a)
        get the squared length of a.
        Parameters:
        a -
      • length

        public static double length​(double[] a)
        get the length of a.
        Parameters:
        a -
      • squareDistance

        public static double squareDistance​(double[] a,
                                            double[] b)
        get the squared length of (a - b).
        Parameters:
        a -
        b -
      • distance

        public static double distance​(double[] a,
                                      double[] b)
        get the length of (a - b).
        Parameters:
        a -
        b -
      • scale

        public static void scale​(double[] a,
                                 double b,
                                 double[] c)
        set c = a * b, where a is a vector and b is scalar. Dimensions of a and c must match. In place scaling (c==a) is permitted.
        Parameters:
        a -
        b -
        c -
      • scale

        public static void scale​(double[][] A,
                                 double b,
                                 double[][] C)
        set C = A * b, where A is a matrix and b is scalar. Dimensions of A and C must match. In place scaling (C==A) is permitted.
        Parameters:
        A -
        b -
        C -
      • copy

        public static void copy​(double[][] A,
                                double[][] C)
        set C = A, where A is a matrix. Dimensions of A and C must match.
        Parameters:
        A -
        C -
      • subtract

        public static void subtract​(double[] a,
                                    double[] b,
                                    double[] c)
        set c = a - b. Dimensions of a, b, and c must match. In place subtraction (c==a) is permitted.
        Parameters:
        a -
        b -
        c -
      • add

        public static void add​(double[] a,
                               double[] b,
                               double[] c)
        set c = a + b. Dimensions of a, b, and c must match. In place addition (c==a) is permitted.
        Parameters:
        a -
        b -
        c -
      • mult

        public static void mult​(double[][] A,
                                double[] b,
                                double[] c)
        set c = A * b. Dimensions of A, b, and c must match. That is, cols(A) == rows(b), and rows(c) == rows(A).
        Parameters:
        A -
        b -
        c -
      • multT

        public static void multT​(double[][] A,
                                 double[] b,
                                 double[] c)
        set c = A^T * b. Dimensions of A, b, and c must match. That is, rows(A) == rows(b), and rows(c) == cols(A).
        Parameters:
        A -
        b -
        c -
      • mult

        public static void mult​(double[][] A,
                                double[][] B,
                                double[][] C)
        set C = A * B. Dimensions of A, B, and C must match. That is, cols(A) == rows(B), rows(C) == rows(A), and cols(C) == cols(B).
        Parameters:
        A -
        B -
        C -
      • multABT

        public static void multABT​(double[][] A,
                                   double[][] B,
                                   double[][] C)
        set C = A * B^T. Dimensions of A, B, and C must match. That is, cols(A) == cols(B), rows(C) == rows(A), and cols(C) == rows(B).
        Parameters:
        A -
        B -
        C -
      • multATB

        public static void multATB​(double[][] A,
                                   double[][] B,
                                   double[][] C)
        set C = A^T * B. Dimensions of A, B, and C must match. That is, rows(A) == rows(B), rows(C) == cols(A), and cols(C) == cols(B).
        Parameters:
        A -
        B -
        C -
      • add

        public static void add​(double[][] A,
                               double[][] B,
                               double[][] C)
        set C = A + B. Dimensions of A, B, and C must match. In place addition (C==A or C==B) is permitted.
        Parameters:
        A -
        B -
        C -
      • getCol

        public static void getCol​(int c,
                                  double[][] A,
                                  double[] b)
        extract column c of A into vector b. Dimensions of A and b must match. That is, rows(A) == rows(b).
        Parameters:
        c -
        A -
        b -
      • setCol

        public static void setCol​(int c,
                                  double[] a,
                                  double[][] B)
        set column c of B to vector a. Dimensions of a and B must match. That is, rows(a) == rows(B).
        Parameters:
        c -
        a -
        B -
      • getRow

        public static void getRow​(int r,
                                  double[][] A,
                                  double[] b)
        extract row r of A into vector b. Dimensions of A and b must match. That is, cols(A) == rows(b).
        Parameters:
        r -
        A -
        b -
      • setRow

        public static void setRow​(int r,
                                  double[] a,
                                  double[][] B)
        set row r of B to vector a. Dimensions of a and B must match. That is, rows(a) == cols(B).
        Parameters:
        r -
        a -
        B -
      • normalize

        public static void normalize​(double[] a)
        normalize a, i.e., scale to unit length.
        Parameters:
        a -
      • dot

        public static double dot​(double[] a,
                                 double[] b)
        compute dot product a * b. Dimensions of a and b must match.
        Parameters:
        a -
        b -
      • cross

        public static void cross​(double[] a,
                                 double[] b,
                                 double[] c)
        compute cross product, set c = a ^ b. Dimensions of a, b, and c must equal 3.
        Parameters:
        a -
        b -
      • outer

        public static void outer​(double[] a,
                                 double[] b,
                                 double[][] C)
        compute outer product, set C = a * b^T. Dimensions of a, b, and C must match. That is, rows(a) == rows(C), and rows(b) == cols(C).
        Parameters:
        a -
        b -
        C -
      • angleFromR

        public static double angleFromR​(double[][] R)
        compute the angle of rotation from a rotation matrix. The returned value is in the range [0, PI].
        Parameters:
        R - rotation matrix
      • axisFromR

        public static void axisFromR​(double[][] R,
                                     double[] a)
        compute the axis of rotation from a rotation matrix.
        Parameters:
        R - rotation matrix
        a - rotation axis is stored here
      • quaternionFromR

        public static void quaternionFromR​(double[][] R,
                                           double[] q)
        compute a unit quaternion from a rotation matrix.
        Parameters:
        R - rotation matrix.
        q - unit quaternion (w, x, y, z) is stored here.
      • quaternionToR

        public static void quaternionToR​(double[] q,
                                         double[][] R)
        compute a rotation matrix from a unit quaternion.
        Parameters:
        q - unit quaternion (w, x, y, z).
        R - rotation matrix is stored here.
      • quaternionFromAngleAxis

        public static void quaternionFromAngleAxis​(double[] axis,
                                                   double angle,
                                                   double[] q)
        compute a quaternion from rotation axis and angle.
        Parameters:
        axis - rotation axis as a unit vector.
        angle - rotation angle [rad].
        q - unit quaternion (w, x, y, z) is stored here.
      • quaternionMultiply

        public static void quaternionMultiply​(double[] p,
                                              double[] q,
                                              double[] pq)
        compute the quaternion product pq = p * q. applying rotation pq corresponds to applying first q, then p (i.e. same as multiplication of rotation matrices).
        Parameters:
        p - unit quaternion (w, x, y, z).
        q - unit quaternion (w, x, y, z).
        pq - quaternion product p * q is stored here.
      • quaternionPower

        public static void quaternionPower​(double[] q,
                                           double a,
                                           double[] qa)
        compute the power of a quaternion q raised to the exponent a.
        Parameters:
        q - unit quaternion (w, x, y, z).
        a - exponent.
        qa - q^a is stored here.
      • quaternionInvert

        public static void quaternionInvert​(double[] p,
                                            double[] q)
        invert quaternion, set q = p^{-1}. In place inversion (p==q) is permitted.
        Parameters:
        p - unit quaternion (w, x, y, z).
        q - inverse of p is stored here.
      • quaternionApply

        public static void quaternionApply​(double[] q,
                                           double[] p,
                                           double[] qp)
        Apply quaternion rotation q to 3D point p, set qp = q * p. In place rotation (p==qp) is permitted.
        Parameters:
        q - unit quaternion (w, x, y, z).
        p - 3D point.
        qp - rotated 3D point is stored here.
      • det3x3

        public static final double det3x3​(double[] a)
        Calculates the determinant of a 3x3 matrix given as a double[] (row major).
        Parameters:
        a -
        Returns:
        determinant
      • det3x3

        public static final double det3x3​(double m00,
                                          double m01,
                                          double m02,
                                          double m10,
                                          double m11,
                                          double m12,
                                          double m20,
                                          double m21,
                                          double m22)
        Calculate the determinant of a 3x3 matrix.
        Parameters:
        m00 -
        m01 -
        m02 -
        m10 -
        m11 -
        m12 -
        m20 -
        m21 -
        m22 -
        Returns:
      • invert3x3

        public static final void invert3x3​(double[] m)
                                    throws java.lang.IllegalArgumentException
        Invert a 3x3 matrix given as row major double[] in place.
        Parameters:
        m - matrix
        Throws:
        java.lang.IllegalArgumentException - if matrix is not invertible
      • invert3x3

        public static final double[] invert3x3​(double m00,
                                               double m01,
                                               double m02,
                                               double m10,
                                               double m11,
                                               double m12,
                                               double m20,
                                               double m21,
                                               double m22)
                                        throws java.lang.IllegalArgumentException
        Invert a 3x3 matrix given as elements in row major order.
        Returns:
        inverted matrix as row major double[]
        Throws:
        java.lang.IllegalArgumentException - if matrix is not invertible
      • invertSymmetric3x3

        public static void invertSymmetric3x3​(double[][] m,
                                              double[][] inverse)
        Inverts a (invertible) symmetric 3x3 matrix.
        Parameters:
        m - symmetric matrix to invert.
        inverse - inverse of m is stored here.
      • invertSymmetric2x2

        public static void invertSymmetric2x2​(double[][] m,
                                              double[][] inverse)
        Inverts a (invertible) symmetric 2x2 matrix.
        Parameters:
        m - symmetric matrix to invert.
        inverse - inverse of m is stored here.
      • toString

        public static java.lang.String toString​(double[][] A)
      • toString

        public static java.lang.String toString​(double[][] A,
                                                java.lang.String format)
      • toString

        public static java.lang.String toString​(double[] a)
      • toString

        public static java.lang.String toString​(double[] a,
                                                java.lang.String format)