@@ -0,0 +1,2175 @@
+package org.stuygfx.math;%0A%0Apublic class MatrixMath %7B%0A%0A    public static double dotProduct(Matrix m1, Matrix m2) %7B%0A        assert (m1.rows() == 1 && m2.rows() == 1);%0A        assert (m1.cols() == m2.cols());%0A        double sum = 0.0;%0A        for (int i = 0; i %3C m1.cols(); i++) %7B%0A            sum += m1.data%5B0%5D%5Bi%5D * m2.data%5B0%5D%5Bi%5D;%0A        %7D%0A        return sum;%0A    %7D%0A%0A    public static double dotProduct(double%5B%5D%5B%5D m1, double%5B%5D%5B%5D m2) %7B%0A        return dotProduct(new Matrix(m1), new Matrix(m2));%0A    %7D%0A%0A    public static Matrix crossProduct(Matrix m1, Matrix m2) %7B%0A        assert (m1.cols() == m2.rows());%0A        Matrix crossProd = new Matrix(m1.rows(), m2.cols());%0A        for (int i = 0; i %3C m1.rows(); i++) %7B%0A            for (int j = 0; j %3C m2.cols(); j++) %7B%0A                // Construct temporary mats to dot product with%0A                Matrix tmp1 = new Matrix(1, m1.cols());%0A                for (int k = 0; k %3C m1.cols(); k++) %7B%0A                    tmp1.data%5B0%5D%5Bk%5D = m1.data%5Bi%5D%5Bk%5D;%0A                %7D%0A%0A                Matrix tmp2 = new Matrix(1, m2.rows());%0A                for (int k = 0; k %3C m2.rows(); k++) %7B%0A                    tmp2.data%5B0%5D%5Bk%5D = m2.data%5Bk%5D%5Bj%5D;%0A                %7D%0A%0A                crossProd.data%5Bi%5D%5Bj%5D = dotProduct(tmp1, tmp2);%0A            %7D%0A        %7D%0A        return crossProd;%0A    %7D%0A%0A    public static Matrix crossProduct(double%5B%5D%5B%5D m1, double%5B%5D%5B%5D m2) %7B%0A        return crossProduct(new Matrix(m1), new Matrix(m2));%0A    %7D%0A%0A    public static void scalarMultiplyInPlace(Matrix m, int factor) %7B%0A        for (int i = 0; i %3C m.rows(); i++) %7B%0A            int j;%0A            for (j = 0; j %3C m.cols(); j++) %7B%0A                m.data%5Bi%5D%5Bj%5D *= factor;%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static Matrix scalarMultiply(Matrix m, int factor) %7B%0A        Matrix clone = m.clone();%0A        scalarMultiplyInPlace(clone, factor);%0A        return clone;%0A    %7D%0A%0A    public static Matrix identityMatrix(int size) %7B%0A        Matrix idMat = new Matrix(size, size);%0A        for (int i = 0; i %3C idMat.rows(); i++) %7B%0A            for (int j = 0; j %3C idMat.cols(); j++) %7B%0A                idMat.data%5Bi%5D%5Bj%5D = (i == j) ? 1 : 0;%0A            %7D%0A        %7D%0A        return idMat;%0A    %7D%0A%7D%0A

Add Matrix Math operations