isefire

MatrixDotAndMultiply

Jul 16th, 2014
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. /* Matrix
  2.  * <Description>
  3.  * Takes the dot product of 2 1D arrays or multiplies 2 2D arrays.
  4.  *
  5.  * <Usage>
  6.  * javac Matrix.java
  7.  * ----------Just Tests---------
  8.  * java Matrix
  9.  * ----------End Tests----------
  10.  *
  11.  * ---------1D Arrays(Dot)--------
  12.  * ######################
  13.  * #####example.txt######
  14.  * ######################
  15.  * #4                   #
  16.  * #1 2 3 4             #
  17.  * #4                   #
  18.  * #5 6 7 8             #
  19.  * ######################
  20.  * java Matrix 1 < example.txt
  21.  * --------END 1D Arrays(Dot)-----
  22.  *
  23.  * ---------2D Arrays(Multiply)-------
  24.  * ######################
  25.  * #####example.txt######
  26.  * ######################
  27.  * #3 3                 #
  28.  * #9 8 7               #
  29.  * #6 5 4               #
  30.  * #3 2 1               #
  31.  * #3 3                 #
  32.  * #1 2 3               #
  33.  * #4 5 6               #
  34.  * #7 8 9               #
  35.  * ######################
  36.  * java Matrix 2 < example.txt
  37.  * -------END 2D Arrays(Multiply------
  38.  *
  39.  * <References>
  40.  * CIS201-L UAB SUMMER 2014 Dr. Sloan
  41.  * @author
  42.  * Dan Latham <[email protected]>
  43.  *
  44.  * @version 0.0.1
  45.  *
  46.  */
  47.  
  48.  
  49. public class Matrix
  50. {
  51.     public static double dot(double[] a, double[] b)
  52.     {
  53.         //dot = a ** b = a1*b1 + a2*b2 + a3*b3...
  54.         //System.out.println("dot_Test");
  55.         //check
  56.         assert (a.length == b.length);
  57.         //counter
  58.         double c = 0;
  59.        
  60.         for (int i = 0; i <= a.length-1; i+=1)
  61.         {
  62.             double d = a[i]*b[i];
  63.             c += d;
  64.         }
  65.         return c;
  66.     }
  67.        
  68.     public static double[][] multiply(double[][] firstarray, double[][] secondarray)
  69.     {
  70.         /* Create another 2d array to store the result
  71.          * using the original arrays' lengths on row and column respectively. */
  72.         double [][] result = new double[firstarray.length][secondarray[0].length];
  73.  
  74.         /* Loop through each and get product, then sum up and store
  75.          * the value */
  76.         for (int i = 0; i < firstarray.length; i++)
  77.         {
  78.             for (int j = 0; j < secondarray[0].length; j++)
  79.             {
  80.                 for (int k = 0; k < firstarray[0].length; k++)
  81.                 {
  82.                     result[i][j] += firstarray[i][k] * secondarray[k][j];
  83.                 }
  84.             }
  85.         }
  86.         return result;
  87.     }
  88.     public static void test()
  89.     {
  90.         System.out.println("--------------------TESTS------------------");
  91.         //DOT tests
  92.         double[] a0 = {1,2,3,4,5};
  93.         double[] b0 = {6,7,8,9,10};
  94.         System.out.println("DOT test #0: " + dot(a0,b0));
  95.         double[] a1 = {8,3,5,2,4,6,7};
  96.         double[] b1 = {1.2,33.5,5,8,9,1,2};
  97.         System.out.println("DOT test #1: " + dot(a1,b1));
  98.         //Multiply tests
  99.         double[][] a2 = {
  100.             {1,2,3},
  101.             {4,5,6},
  102.             {7,8,9}
  103.             };
  104.         double[][] b2 = {
  105.             {9,8,7},
  106.             {6,5,4},
  107.             {3,2,1}
  108.             };
  109.         double zzz[][] = multiply(a2,b2);
  110.         StdArrayIO.print(zzz);
  111.        
  112.         double[][] a3 = {
  113.             {1,2,3},
  114.             {4,5,6}
  115.             };
  116.         double[][] b3 = {
  117.             {7,8},
  118.             {9,10},
  119.             {11,12}
  120.             };
  121.         double zzz1[][] = multiply(a3,b3);
  122.         StdArrayIO.print(zzz1);
  123.         System.out.println("------------------END TESTS----------------");
  124.     }
  125.    
  126.     public static void main(String[] args)
  127.     {
  128.         if (args.length == 0)
  129.         {
  130.             test();
  131.             System.exit(1);
  132.         }
  133.         int c = Integer.parseInt(args[0]);
  134.         if (c == 1)
  135.         {
  136.             double[] a = StdArrayIO.readDouble1D();
  137.             double[] b = StdArrayIO.readDouble1D();
  138.             System.out.println("Dot is: "+dot(a,b));
  139.         }
  140.         else if (c == 2)
  141.         {
  142.             double[][] a = StdArrayIO.readDouble2D();
  143.             double[][] b = StdArrayIO.readDouble2D();
  144.             StdArrayIO.print(multiply(a,b));
  145.         }
  146.         else System.out.println("Options are 1 for 1D arrays(dot) and 2 for 2D arrays(multiply)");
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment