isefire icon

MatrixDotAndMultiply

isefire | PRO | 07/16/14 05:43:29 AM UTC | 0 ⭐ | 357 👁️ | Never ⏰ | []
Java |

3.3 KB

|

None

|

0 👍

/

0 👎

/* Matrix
 * <Description>
 * Takes the dot product of 2 1D arrays or multiplies 2 2D arrays.
 * 
 * <Usage>
 * javac Matrix.java
 * ----------Just Tests---------
 * java Matrix
 * ----------End Tests----------
 *
 * ---------1D Arrays(Dot)--------
 * ######################
 * #####example.txt######
 * ######################
 * #4                   #
 * #1 2 3 4             #
 * #4                   #
 * #5 6 7 8             #
 * ######################
 * java Matrix 1 < example.txt
 * --------END 1D Arrays(Dot)-----
 * 
 * ---------2D Arrays(Multiply)-------
 * ######################
 * #####example.txt######
 * ######################
 * #3 3                 #
 * #9 8 7               #
 * #6 5 4               #
 * #3 2 1               #
 * #3 3                 #
 * #1 2 3               #
 * #4 5 6               #
 * #7 8 9               #
 * ######################
 * java Matrix 2 < example.txt
 * -------END 2D Arrays(Multiply------
 * 
 * <References>
 * CIS201-L UAB SUMMER 2014 Dr. Sloan
 * @author
 * Dan Latham <[email protected]>
 *
 * @version 0.0.1
 *
 */
 
 
public class Matrix
{
    public static double dot(double[] a, double[] b)
    {
        //dot = a ** b = a1*b1 + a2*b2 + a3*b3...
        //System.out.println("dot_Test");
        //check
        assert (a.length == b.length);
        //counter
        double c = 0;
        
        for (int i = 0; i <= a.length-1; i+=1)
        {
            double d = a[i]*b[i];
            c += d;
        }
        return c;
    }
        
    public static double[][] multiply(double[][] firstarray, double[][] secondarray)
    {
        /* Create another 2d array to store the result
         * using the original arrays' lengths on row and column respectively. */
        double [][] result = new double[firstarray.length][secondarray[0].length];
 
        /* Loop through each and get product, then sum up and store
         * the value */
        for (int i = 0; i < firstarray.length; i++)
        { 
            for (int j = 0; j < secondarray[0].length; j++)
            { 
                for (int k = 0; k < firstarray[0].length; k++)
                { 
                    result[i][j] += firstarray[i][k] * secondarray[k][j];
                }
            }
        }
        return result;
    }
    public static void test()
    {
        System.out.println("--------------------TESTS------------------");
        //DOT tests
        double[] a0 = {1,2,3,4,5};
        double[] b0 = {6,7,8,9,10};
        System.out.println("DOT test #0: " + dot(a0,b0));
        double[] a1 = {8,3,5,2,4,6,7};
        double[] b1 = {1.2,33.5,5,8,9,1,2};
        System.out.println("DOT test #1: " + dot(a1,b1));
        //Multiply tests
        double[][] a2 = {
            {1,2,3},
            {4,5,6},
            {7,8,9}
            };
        double[][] b2 = {
            {9,8,7},
            {6,5,4},
            {3,2,1}
            };
        double zzz[][] = multiply(a2,b2);
        StdArrayIO.print(zzz);
        
        double[][] a3 = {
            {1,2,3},
            {4,5,6}
            };
        double[][] b3 = {
            {7,8},
            {9,10},
            {11,12}
            };
        double zzz1[][] = multiply(a3,b3);
        StdArrayIO.print(zzz1);
        System.out.println("------------------END TESTS----------------");
    }
    
    public static void main(String[] args)
    {
        if (args.length == 0)
        {
            test();
            System.exit(1);
        }
        int c = Integer.parseInt(args[0]);
        if (c == 1)
        {
            double[] a = StdArrayIO.readDouble1D();
            double[] b = StdArrayIO.readDouble1D();
            System.out.println("Dot is: "+dot(a,b));
        }
        else if (c == 2)
        {
            double[][] a = StdArrayIO.readDouble2D();
            double[][] b = StdArrayIO.readDouble2D();
            StdArrayIO.print(multiply(a,b));
        }
        else System.out.println("Options are 1 for 1D arrays(dot) and 2 for 2D arrays(multiply)");
    }
}

Comments