Class Equations



  • public class Equations
    extends Object
    This contains a static method to solve a system of simple linear equations.
    • Field Summary

      Fields

      Modifier and Type Field and Description
      static boolean VERBOSE_EXCEPTIONS
      Generating the text in certain exceptions is not a trivial task if you expect those exceptions to be called millions of times.
    • Constructor Summary

      Constructors

      Constructor and Description
      Equations()
       
    • Method Summary

      Methods

      Modifier and Type Method and Description
      static void solve(BigDecimal[][] coefficients)
      Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.
      static void solve(BigDecimal[][] coefficients, boolean sort)
      Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.
      static void solve(double[][] coefficients)
      Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.
      static void solve(double[][] coefficients, boolean sort)
      Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.
      static String toString(BigDecimal[] d)
       
      static String toString(BigDecimal[][] d)
       
      static String toString(double[] d)
       
      static String toString(double[][] d)
       
    • Field Detail

      • VERBOSE_EXCEPTIONS

        public static boolean VERBOSE_EXCEPTIONS
        Generating the text in certain exceptions is not a trivial task if you expect those exceptions to be called millions of times.
    • Constructor Detail

      • Equations

        public Equations()
    • Method Detail

      • toString

        public static String toString(double[][] d)
      • toString

        public static String toString(double[] d)
      • solve

        public static void solve(double[][] coefficients)
        Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.

        For example, if you have the three equations:
        2x + y + z = 1
        6x + 2y + z = -1
        -2x + 2y + z = 7

        You can call:
        double[][] coeffs = { { 2, 1, 1, 1},

        { 6, 2, 1, -1},

        { -2, 2, 1, 7} };

        Equations.solve(coeffs,true);

        Then this method will simplify the matrix to:
        { { 1, 0, 0, -1},

        { 0, 1, 0, 2},

        { 0, 0, 1, 1} }

        This indicates that:
        1*x+0*y+0*z = -1
        0*x+1*y+0*z = 2
        0*x+0*y+1*z = 1

        This method uses Gaussian Elimination and back substitution.

        Note that due to the limits of double-precision, some safeguards were built-in that may round values less than .00000001 to zero. If you need better precision, you may have to copy and paste the algorithm in this code and use a more precise number format.

        Parameters:
        coefficients - the matrix of coefficients. It must be N x (N+1) units in size.
        Throws:
        IllegalArgumentException - if it is impossible to solve for each variable. In this case an exception is thrown, and no guarantee is made regarding the final state of the coefficients matrix.

        Also an exception may be thrown if the matrix is not correctly sized.

      • solve

        public static void solve(BigDecimal[][] coefficients)
        Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.

        For example, if you have the three equations:
        2x + y + z = 1
        6x + 2y + z = -1
        -2x + 2y + z = 7

        You can call:
        double[][] coeffs = { { 2, 1, 1, 1},

        { 6, 2, 1, -1},

        { -2, 2, 1, 7} };

        Equations.solve(coeffs,true);

        Then this method will simplify the matrix to:
        { { 1, 0, 0, -1},

        { 0, 1, 0, 2},

        { 0, 0, 1, 1} }

        This indicates that:
        1*x+0*y+0*z = -1
        0*x+1*y+0*z = 2
        0*x+0*y+1*z = 1

        This method uses Gaussian Elimination and back substitution.

        Note that due to the limits of double-precision, some safeguards were built-in that may round values less than .00000001 to zero. If you need better precision, you may have to copy and paste the algorithm in this code and use a more precise number format.

        Parameters:
        coefficients - the matrix of coefficients. It must be N x (N+1) units in size.
        Throws:
        IllegalArgumentException - if it is impossible to solve for each variable. In this case an exception is thrown, and no guarantee is made regarding the final state of the coefficients matrix.

        Also an exception may be thrown if the matrix is not correctly sized.

      • solve

        public static void solve(double[][] coefficients,
                                 boolean sort)
        Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.

        For example, if you have the three equations:
        2x + y + z = 1
        6x + 2y + z = -1
        -2x + 2y + z = 7

        You can call:
        double[][] coeffs = { { 2, 1, 1, 1},

        { 6, 2, 1, -1},

        { -2, 2, 1, 7} };

        Equations.solve(coeffs,true);

        Then this method will simplify the matrix to:
        { { 1, 0, 0, -1},

        { 0, 1, 0, 2},

        { 0, 0, 1, 1} }

        This indicates that:
        1*x+0*y+0*z = -1
        0*x+1*y+0*z = 2
        0*x+0*y+1*z = 1

        This method uses Gaussian Elimination and back substitution.

        Note that due to the limits of double-precision, some safeguards were built-in that may round values less than .00000001 to zero. If you need better precision, you may have to copy and paste the algorithm in this code and use a more precise number format.

        Parameters:
        coefficients - the matrix of coefficients. It must be N x (N+1) units in size.
        sort - If this is true, then you're guaranteed for your final solution to be sorted such that the 1's form a diagonal line from [0,0] to [N,N]. If this is false, then the solution may not form a diagonal line of 1's.

        (If there are no zeroes in the matrix, the diagonal line will be there by default, but if zeroes are present then some rows may have to be skipped and the normal order is disrupted.)

        The sort is performed by calling Arrays.sort().

        Throws:
        IllegalArgumentException - if it is impossible to solve for each variable. In this case an exception is thrown, and no guarantee is made regarding the final state of the coefficients matrix.

        Also an exception may be thrown if the matrix is not correctly sized.

      • solve

        public static void solve(BigDecimal[][] coefficients,
                                 boolean sort)
        Given a matrix of variable coefficients for a linear system of equations, this will solve for each variable.

        For example, if you have the three equations:
        2x + y + z = 1
        6x + 2y + z = -1
        -2x + 2y + z = 7

        You can call:
        double[][] coeffs = { { 2, 1, 1, 1},

        { 6, 2, 1, -1},

        { -2, 2, 1, 7} };

        Equations.solve(coeffs,true);

        Then this method will simplify the matrix to:
        { { 1, 0, 0, -1},

        { 0, 1, 0, 2},

        { 0, 0, 1, 1} }

        This indicates that:
        1*x+0*y+0*z = -1
        0*x+1*y+0*z = 2
        0*x+0*y+1*z = 1

        This method uses Gaussian Elimination and back substitution.

        Note that due to the limits of double-precision, some safeguards were built-in that may round values less than .00000001 to zero. If you need better precision, you may have to copy and paste the algorithm in this code and use a more precise number format.

        Parameters:
        coefficients - the matrix of coefficients. It must be N x (N+1) units in size.
        sort - If this is true, then you're guaranteed for your final solution to be sorted such that the 1's form a diagonal line from [0,0] to [N,N]. If this is false, then the solution may not form a diagonal line of 1's.

        (If there are no zeroes in the matrix, the diagonal line will be there by default, but if zeroes are present then some rows may have to be skipped and the normal order is disrupted.)

        The sort is performed by calling Arrays.sort().

        Throws:
        IllegalArgumentException - if it is impossible to solve for each variable. In this case an exception is thrown, and no guarantee is made regarding the final state of the coefficients matrix.

        Also an exception may be thrown if the matrix is not correctly sized.