Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 149,619 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,869 people online right now. Registration is fast and FREE... Join Now!





Matrix Arithmetic

A class of static functions that lets you work with multidimensional arrays so as to perform operations in Linear Algebra (find the determinant of a matrix, etc.)

Submitted By: grimpirate
Actions:
Rating:
Views: 1,588

Language: Java

Last Modified: November 15, 2006
Instructions: Copy and paste the code into a file Matrix.java then use it in your other programs to perform the operations

Snippet


  1. import java.lang.Math;
  2.  
  3. public class Matrix
  4. {
  5.     public static double[][] crossProduct(double[][] a, double[][] b) throws IllegalArgumentException
  6.     {
  7.         int m1 = 0;
  8.         int n1 = 0;
  9.         int m2 = 0;
  10.         int n2 = 0;
  11.        
  12.         try
  13.         {
  14.             m1 = a.length;
  15.             n1 = a[0].length;
  16.             m2 = b.length;
  17.             n2 = b[0].length;
  18.            
  19.             if(m1 != m2 || n1 != n2)
  20.             {
  21.                 throw new IllegalArgumentException("The vector lengths do not match.");
  22.             }
  23.             if(m1 != 1 && n1 != 1 || m2 != 1 && n2 != 1)
  24.             {
  25.                 throw new IllegalArgumentException("The parameters are not vectors.");
  26.             }
  27.             if(m1 == 0 || n1 == 0 || m2 == 0 || n2 == 0)
  28.             {
  29.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  30.             }
  31.         }
  32.         catch(ArrayIndexOutOfBoundsException exception)
  33.         {
  34.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  35.         }
  36.        
  37.         if(m1 == 1)
  38.         {
  39.             if(n1 != 3)
  40.             {
  41.                 throw new IllegalArgumentException("The parameters are not vectors.");
  42.             }
  43.             return new double[][] {{a[0][1] * b[0][2] - a[0][2] * b[0][1], a[0][2] * b[0][0] - a[0][0] * b[0][2], a[0][0] * b[0][1] - a[0][1] * b[0][0]}};
  44.         }
  45.         else
  46.         {
  47.             if(m2 != 3)
  48.             {
  49.                 throw new IllegalArgumentException("The parameters are not vectors.");
  50.             }
  51.             return new double[][] {{a[1][0] * b[2][0] - a[2][0] * b[1][0]}, {a[2][0] * b[0][0] - a[0][0] * b[2][0]}, {a[0][0] * b[1][0] - a[1][0] * b[0][0]}};
  52.         }
  53.     }
  54.    
  55.     public static double[][] addition(double[][] A, double[][] B) throws IllegalArgumentException
  56.     {
  57.         int m = 0;
  58.         int n = 0;
  59.        
  60.         try
  61.         {
  62.             m = A.length;
  63.             n = A[0].length;
  64.            
  65.             if(m != B.length || n != B[0].length)
  66.             {
  67.                 throw new IllegalArgumentException("Matrix dimensions don't match.");
  68.             }
  69.             if(m == 0 || n == 0 || B.length == 0 || B[0].length == 0)
  70.             {
  71.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  72.             }
  73.         }
  74.         catch(ArrayIndexOutOfBoundsException exception)
  75.         {
  76.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  77.         }
  78.        
  79.         double[][] M = new double[m][n];
  80.        
  81.         for(int i = 0; i < m; i++)
  82.         {
  83.             for(int j = 0; j < n; j++)
  84.             {
  85.                 M[i][j] = A[i][j] + B[i][j];
  86.             }
  87.         }
  88.        
  89.         return M;
  90.     }
  91.    
  92.     public static double magnitude(double[][] a) throws IllegalArgumentException
  93.     {
  94.         int m = 0;
  95.         int n = 0;
  96.        
  97.         try
  98.         {
  99.             m = a.length;
  100.             n = a[0].length;
  101.            
  102.             if(m == 0 || n == 0)
  103.             {
  104.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  105.             }
  106.             if(m != 1 && n != 1)
  107.             {
  108.                 throw new IllegalArgumentException("The parameter is not a vector.");
  109.             }
  110.         }
  111.         catch(ArrayIndexOutOfBoundsException exception)
  112.         {
  113.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  114.         }
  115.        
  116.         double result = 0;
  117.        
  118.         for(int i = 0; i < m; i++)
  119.         {
  120.             for(int j = 0; j < n; j++)
  121.             {
  122.                 result += Math.pow(a[i][j], 2);
  123.             }
  124.         }
  125.        
  126.         return Math.sqrt(result);
  127.     }
  128.    
  129.     public static double[][] adjoint(double[][] A) throws IllegalArgumentException
  130.     {
  131.         return scale(determinant(A), inverse(A));
  132.     }
  133.    
  134.     public static double[][] scale(double k, double[][] B) throws IllegalArgumentException
  135.     {
  136.         int m = 0;
  137.         int n = 0;
  138.        
  139.         try
  140.         {
  141.             m = B.length;
  142.             n = B[0].length;
  143.            
  144.             if(m == 0 || n == 0)
  145.             {
  146.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  147.             }
  148.         }
  149.         catch(ArrayIndexOutOfBoundsException exception)
  150.         {
  151.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  152.         }
  153.        
  154.         double[][] A = new double[m][n];
  155.        
  156.         for(int i = 0; i < m; i++)
  157.         {
  158.             for(int j = 0; j < n; j++)
  159.             {
  160.                 A[i][j] = k * B[i][j];
  161.             }
  162.         }
  163.        
  164.         return A;
  165.     }
  166.    
  167.     public static double determinant(double[][] B) throws IllegalArgumentException
  168.     {
  169.         try
  170.         {
  171.             if(B.length != B[0].length)
  172.             {
  173.                 throw new IllegalArgumentException("Not a square matrix.");
  174.             }
  175.             if(B.length == 0 || B[0].length == 0)
  176.             {
  177.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  178.             }
  179.         }
  180.         catch(ArrayIndexOutOfBoundsException exception)
  181.         {
  182.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  183.         }
  184.        
  185.         int n = B.length;
  186.         double[][] E = identity(n);
  187.         double[] swap = new double[n];
  188.         double determ = 1;
  189.        
  190.         double[][] A = transpose(transpose(B));
  191.        
  192.         for(int j = 0; j < n; j++)
  193.         {
  194.             for(int i = j; i < n; i++)
  195.             {
  196.                 if(i == j)
  197.                 {
  198.                     if(A[i][j] == 0)
  199.                     {
  200.                         for(int k = j + 1; k < n; k++)
  201.                         {
  202.                             if(A[k][j] != 0)
  203.                             {
  204.                                 swap = A[i];
  205.                                 A[i] = A[k];
  206.                                 A[k] = swap;
  207.                                 determ *= -1;
  208.                                 k = n;
  209.                             }
  210.                         }
  211.                     }
  212.                     determ *= A[i][j];
  213.                 }
  214.                 else
  215.                 {
  216.                     E[i][j] = -A[i][j] / A[j][j];
  217.                 }
  218.                 A = multiply(E, A);
  219.                 E = identity(n);
  220.             }
  221.         }
  222.        
  223.         return determ;
  224.     }
  225.    
  226.     public static double[][] inverse(double[][] B) throws IllegalArgumentException
  227.     {
  228.         if(determinant(B) == 0)
  229.         {
  230.             throw new IllegalArgumentException("This matrix is singular.");
  231.         }
  232.        
  233.         int n = B.length;
  234.         double[][] V = identity(n);
  235.         double[][] E = identity(n);
  236.         double[] swap = new double[n];
  237.        
  238.         double[][] A = transpose(transpose(B));
  239.        
  240.         for(int j = 0; j < n; j++)
  241.         {
  242.             for(int i = j; i < n; i++)
  243.             {
  244.                 if(i == j)
  245.                 {
  246.                     if(A[i][j] == 0)
  247.                     {
  248.                         for(int k = j + 1; k < n; k++)
  249.                         {
  250.                             if(A[k][j] != 0)
  251.                             {
  252.                                 swap = A[i];
  253.                                 A[i] = A[k];
  254.                                 A[k] = swap;
  255.                                 swap = V[i];
  256.                                 V[i] = V[k];
  257.                                 V[k] = swap;
  258.                                 k = n;
  259.                             }
  260.                         }
  261.                     }
  262.                     E[i][j] = 1 / A[i][j];
  263.                 }
  264.                 else
  265.                 {
  266.                     E[i][j] = -A[i][j];
  267.                 }
  268.                 A = multiply(E, A);
  269.                 V = multiply(E, V);
  270.                 E = identity(n);
  271.             }
  272.         }
  273.         for(int j = n - 1; j > 0; j--)
  274.         {
  275.             for(int i = j - 1; i > -1; i--)
  276.             {
  277.                 E[i][j] = -A[i][j];
  278.                 A = multiply(E, A);
  279.                 V = multiply(E, V);
  280.                 E = identity(n);
  281.             }
  282.         }
  283.        
  284.         return V;
  285.     }
  286.    
  287.     public static double[][] multiply(double[][] C, double[][] B) throws IllegalArgumentException
  288.     {
  289.         try
  290.         {
  291.             if(C[0].length != B.length)
  292.             {
  293.                 throw new IllegalArgumentException("Interior dimensions of multiplication are not equal.");
  294.             }
  295.             if(C.length == 0 || C[0].length == 0 || B.length == 0 || B[0].length == 0)
  296.             {
  297.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  298.             }
  299.         }
  300.         catch(ArrayIndexOutOfBoundsException exception)
  301.         {
  302.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  303.         }
  304.        
  305.         int m = C.length;
  306.         int n = B[0].length;
  307.        
  308.         double[][] A = transpose(transpose(C));
  309.         double[][] BT = transpose(B);
  310.         double[][] M = new double[m][n];
  311.         double[][] tempVect1 = new double[1][n];
  312.         double[][] tempVect2 = new double[1][n];
  313.        
  314.         for(int i = 0; i < m; i++)
  315.         {
  316.             for(int j = 0; j < n; j++)
  317.             {
  318.                 tempVect1[0] = A[i];
  319.                 tempVect2[0] = BT[j];
  320.                 M[i][j] = dotProduct(tempVect1, tempVect2);
  321.             }
  322.         }
  323.        
  324.         return M;
  325.     }
  326.    
  327.     public static double dotProduct(double[][] a, double[][] b) throws IllegalArgumentException
  328.     {
  329.         int m1 = 0;
  330.         int n1 = 0;
  331.         int m2 = 0;
  332.         int n2 = 0;
  333.        
  334.         try
  335.         {
  336.             m1 = a.length;
  337.             n1 = a[0].length;
  338.             m2 = b.length;
  339.             n2 = b[0].length;
  340.            
  341.             if(m1 != m2 || n1 != n2)
  342.             {
  343.                 throw new IllegalArgumentException("The vector lengths do not match.");
  344.             }
  345.             if(m1 != 1 && n1 != 1 || m2 != 1 && n2 != 1)
  346.             {
  347.                 throw new IllegalArgumentException("The parameters are not vectors.");
  348.             }
  349.             if(m1 == 0 || n1 == 0 || m2 == 0 || n2 == 0)
  350.             {
  351.                 throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  352.             }
  353.         }
  354.         catch(ArrayIndexOutOfBoundsException exception)
  355.         {
  356.             throw new IllegalArgumentException("0 dimension matrices are non-existent.");
  357.         }
  358.        
  359.         double result = 0;
  360.        
  361.         for(int i = 0; i < m1; i++)
  362.         {
  363.             for(int j = 0; j < n1; j++)
  364.             {
  365.                 result += a[i][j] * b[i][j];
  366.             }
  367.         }
  368.        
  369.         return result;
  370.     }
  371.    
  372.     public static double[][] transpose(double[][] A) throws