├── .gitignore └── src ├── classes ├── KMeans.cls ├── MatrixOperations.cls-meta.xml ├── MatrixOperationsTest.cls-meta.xml ├── KMeans.cls-meta.xml ├── MathUtil.cls-meta.xml ├── Matrix.cls-meta.xml ├── MathUtilTest.cls-meta.xml ├── MatrixTest.cls-meta.xml ├── LinearRegression.cls-meta.xml ├── LinearRegressionTest.cls-meta.xml ├── LinearRegressionTest.cls ├── MathUtilTest.cls ├── LinearRegression.cls ├── MathUtil.cls ├── MatrixTest.cls ├── MatrixOperations.cls ├── Matrix.cls └── MatrixOperationsTest.cls └── package.xml /.gitignore: -------------------------------------------------------------------------------- 1 | config/ 2 | *.sublime-* 3 | .DS_Store -------------------------------------------------------------------------------- /src/classes/KMeans.cls: -------------------------------------------------------------------------------- 1 | public with sharing class KMeans { 2 | public KMeans() { 3 | 4 | } 5 | } -------------------------------------------------------------------------------- /src/classes/MatrixOperations.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 29.0 4 | -------------------------------------------------------------------------------- /src/classes/MatrixOperationsTest.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 29.0 4 | -------------------------------------------------------------------------------- /src/classes/KMeans.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 39.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/MathUtil.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 39.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/Matrix.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 30.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/MathUtilTest.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 39.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/MatrixTest.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 30.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/LinearRegression.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 39.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/classes/LinearRegressionTest.cls-meta.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 39.0 4 | Active 5 | 6 | -------------------------------------------------------------------------------- /src/package.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MathUtil 5 | LinearRegression 6 | MathUtilTest 7 | LinearRegressionTest 8 | KMeans 9 | ApexClass 10 | 11 | 39.0 12 | -------------------------------------------------------------------------------- /src/classes/LinearRegressionTest.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class LinearRegressionTest { 3 | 4 | private static List testInputs = new List{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; 5 | private static List testOutputs = new List{9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0}; 6 | 7 | @isTest 8 | private static void testPredict() { 9 | Double predictionInput = 10.0; 10 | LinearRegression regression = new LinearRegression(testInputs, testOutputs); 11 | regression.train(); 12 | System.assertEquals(0, ((Decimal)(regression.predict(predictionInput))).setScale(3)); 13 | } 14 | 15 | @isTest 16 | private static void testRootMeanSquaredError() { 17 | LinearRegression regression = new LinearRegression(testInputs, testOutputs); 18 | regression.train(); 19 | System.assertEquals(0, ((Decimal)(regression.rootMeanSquaredError())).setScale(3)); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /src/classes/MathUtilTest.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class MathUtilTest { 3 | 4 | private static List testData = new List{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; 5 | private static List testOutputs = new List{9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0}; 6 | 7 | @isTest 8 | static void testSum() { 9 | System.assertEquals(45, MathUtil.sum(testData)); 10 | } 11 | 12 | @isTest 13 | static void testMean() { 14 | System.assertEquals(5, MathUtil.mean(testData)); 15 | } 16 | 17 | @isTest 18 | static void testStandardDeviation() { 19 | System.assertEquals(2.739, ((Decimal)(MathUtil.standardDeviation(testData))).setScale(3)); 20 | } 21 | 22 | @isTest 23 | static void testVariance() { 24 | System.assertEquals(7.5, ((Decimal)(MathUtil.variance(testData))).setScale(1)); 25 | } 26 | 27 | @isTest 28 | static void testPearsonCorrelation() { 29 | System.assertEquals(-1, ((Decimal)(MathUtil.pearsonCorrelation(testData, testOutputs))).setScale(3)); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /src/classes/LinearRegression.cls: -------------------------------------------------------------------------------- 1 | public with sharing class LinearRegression { 2 | 3 | private Double coefficient {get; private set;} 4 | private Double intercept {get; private set;} 5 | 6 | private List x_train {get; private set;} 7 | private List y_train {get; private set;} 8 | 9 | public LinearRegression(List x_data, List y_data) { 10 | x_train = x_data; 11 | y_train = y_data; 12 | } 13 | 14 | public void train() { 15 | Double mean_x = MathUtil.mean(x_train); 16 | Double mean_y = MathUtil.mean(y_train); 17 | coefficient = MathUtil.pearsonCorrelation(x_train, y_train); 18 | intercept = mean_y - coefficient*mean_x; 19 | } 20 | 21 | public Double predict(Double value) { 22 | return intercept + coefficient*value; 23 | } 24 | 25 | public Double rootMeanSquaredError() { 26 | List predictions = new List(); 27 | Integer num_items = x_train.size(); 28 | for(Integer i = 0; i < num_items; i++) { 29 | predictions.add(predict(x_train[i])); 30 | } 31 | 32 | List squaredErrors = new List(); 33 | for(Integer i = 0; i < num_items; i++) { 34 | squaredErrors.add(Math.pow(predictions[i] - y_train[i], 2)); 35 | } 36 | 37 | return MathUtil.mean(squaredErrors); 38 | } 39 | } -------------------------------------------------------------------------------- /src/classes/MathUtil.cls: -------------------------------------------------------------------------------- 1 | public with sharing class MathUtil { 2 | 3 | public static Double sum(List items) { 4 | Double sum = 0; 5 | Integer numItems = items.size(); 6 | 7 | for(Integer i = 0; i < numItems; i++) { 8 | sum += items[i]; 9 | } 10 | 11 | return sum; 12 | } 13 | 14 | public static Double mean(List items) { 15 | Double sum = sum(items); 16 | Integer numItems = items.size(); 17 | 18 | return sum/numItems; 19 | } 20 | 21 | public static Double standardDeviation(List items) { 22 | return Math.sqrt(variance(items)); 23 | } 24 | 25 | /* 26 | NOTE: 27 | ===== 28 | The assumption is being made here that we are working on a sample of the overall 29 | population and not the total population. 30 | */ 31 | public static Double variance(List items) { 32 | Double mean = mean(items); 33 | 34 | Matrix itemMatrix = new Matrix(items); 35 | Matrix meanMatrix = new Matrix(1, items.size()); 36 | meanMatrix.fill(mean); 37 | 38 | Matrix diffs = MatrixOperations.subtract(itemMatrix, meanMatrix); 39 | diffs = MatrixOperations.pointwiseExponent(diffs, 2); 40 | List diffList = diffs.getRow(0); 41 | 42 | return sum(diffList)/(diffList.size() - 1); 43 | } 44 | 45 | public static Double pearsonCorrelation(List x, List y) { 46 | Double mean_x = mean(x); 47 | Double mean_y = mean(y); 48 | Double std_x = standardDeviation(x); 49 | Double std_y = standardDeviation(y); 50 | 51 | List xy = new List(); 52 | Integer numItems = x.size(); 53 | Double x_diff, y_diff; 54 | for(Integer i = 0; i < numItems; i++) { 55 | x_diff = x[i] - mean_x; 56 | y_diff = y[i] - mean_y; 57 | xy.add((x_diff/std_x)*(y_diff/std_y)); 58 | } 59 | 60 | return sum(xy)/(numItems - 1); 61 | } 62 | } -------------------------------------------------------------------------------- /src/classes/MatrixTest.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class MatrixTest { 3 | 4 | private static String JSONString = '[[1.0,2.0,3.0,4.0],[5.0,6.0,7.0,8.0],[9.0,10.0,11.0,12.0]]'; 5 | private static List dataList = new List{1,2,3,4,5,6,7,8,9,10,11,12}; 6 | private static String printString = '\n1.0\t2.0\t3.0\t4.0\t\n5.0\t6.0\t7.0\t8.0\t\n9.0\t10.0\t11.0\t12.0\t\n'; 7 | 8 | @isTest 9 | static void testInstantiateNewMatrix() { 10 | Matrix A = new Matrix(2, 2); 11 | System.assertEquals(2, A.rows); 12 | System.assertEquals(2, A.columns); 13 | } 14 | 15 | @isTest 16 | static void testSetAndRetrieveValue() { 17 | Matrix A = new Matrix(2, 2); 18 | Double d = 12.34; 19 | A.setElement(0, 1, d); 20 | System.assertEquals(d, A.getElement(0, 1)); 21 | } 22 | 23 | @isTest 24 | static void testSetAndRetrieveNonExistantValue() { 25 | Matrix A = new Matrix(2, 2); 26 | Double d = 12.34; 27 | A.setElement(0, 1, d); 28 | System.assertEquals(null, A.getElement(3, 3)); 29 | } 30 | 31 | @isTest 32 | static void testInstantiateWithJSON() { 33 | Matrix A = new Matrix(JSONString); 34 | System.assertEquals(3, A.rows); 35 | System.assertEquals(4, A.columns); 36 | } 37 | 38 | @isTest 39 | static void testVector() { 40 | List vect = new List{1.0, 2.0, 3.0}; 41 | 42 | Matrix v = new Matrix(vect); 43 | System.assertEquals(1, v.rows); 44 | System.assertEquals(3, v.columns); 45 | } 46 | 47 | @isTest 48 | static void testZeros() { 49 | Matrix A = new Matrix(2, 2); 50 | A.Zeros(); 51 | 52 | for(Integer i = 0; i < 2; i++) { 53 | for(Integer j = 0; j<2; j++) { 54 | System.assertEquals(0, A.getElement(i, j)); 55 | } 56 | } 57 | } 58 | 59 | @isTest 60 | static void testGetAsList() { 61 | Matrix A = new Matrix(JSONString); 62 | System.assertEquals(dataList, A.getDataAsList()); 63 | } 64 | 65 | @isTest 66 | static void testToJSON() { 67 | Matrix A = new Matrix(JSONString); 68 | System.assertEquals(JSONString, A.toJSON()); 69 | } 70 | 71 | @isTest 72 | static void testPrint() { 73 | Matrix A = new Matrix(JSONString); 74 | System.assertEquals(printString, A.Print()); 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /src/classes/MatrixOperations.cls: -------------------------------------------------------------------------------- 1 | public class MatrixOperations { 2 | 3 | public static Matrix multiply(Matrix A, Matrix B) { 4 | 5 | if(A.columns != B.rows) { 6 | return null; 7 | } 8 | 9 | Integer A_rows = A.rows, A_cols = A.columns, B_cols = B.columns; 10 | 11 | Matrix C = new Matrix(A_rows, B_cols); 12 | C.zeros(); 13 | 14 | for(Integer i = 0; i < A_rows; i++) { 15 | List A_row = A.getRow(i); 16 | for(Integer j = 0; j < B_cols; j++) { 17 | for(Integer k = 0; k < A_cols; k++) { 18 | 19 | C.setElement(i, j, C.getElement(i, j) + A_row[k]*B.getElement(k, j)); 20 | } 21 | } 22 | } 23 | 24 | return C; 25 | } 26 | 27 | public static Matrix transpose(Matrix A) { 28 | Integer A_rows = A.rows, A_cols = A.columns; 29 | 30 | Matrix B = new Matrix(A_cols, A_rows); 31 | for(Integer i = 0; i < A_rows; i++) { 32 | List A_row = A.getRow(i); 33 | for(Integer j = 0; j < A_cols; j++) { 34 | B.setElement(j, i, A_row[j]); 35 | } 36 | } 37 | 38 | return B; 39 | } 40 | 41 | public static Matrix add(Matrix A, Matrix B) { 42 | 43 | if((A.rows != B.rows) && (A.columns != B.columns)) { 44 | return null; 45 | } 46 | 47 | Integer A_rows = A.rows, A_cols = A.columns, B_rows = B.rows, B_cols = B.columns; 48 | 49 | Matrix C = new Matrix(A_rows, A_cols); 50 | 51 | for(Integer i = 0; i < A_rows; i++) { 52 | List A_row = A.getRow(i), B_row = B.getRow(i), C_row = new Double[A_cols]; 53 | for(Integer j = 0; j < A_cols; j ++) { 54 | C_row[j] = A_row[j] + B_row[j]; 55 | } 56 | 57 | C.setRow(C_row, i); 58 | } 59 | 60 | return C; 61 | } 62 | 63 | public static Matrix subtract(Matrix A, Matrix B) { 64 | 65 | if((A.rows != B.rows) && (A.columns != B.columns)) { 66 | return null; 67 | } 68 | 69 | Integer A_rows = A.rows, A_cols = A.columns, B_rows = B.rows, B_cols = B.columns; 70 | 71 | Matrix C = new Matrix(A_rows, A_cols); 72 | 73 | for(Integer i = 0; i < A_rows; i++) { 74 | List A_row = A.getRow(i), B_row = B.getRow(i), C_row = new Double[A_cols]; 75 | for(Integer j = 0; j < A_cols; j ++) { 76 | C_row[j] = A_row[j] - B_row[j]; 77 | } 78 | 79 | C.setRow(C_row, i); 80 | } 81 | 82 | return C; 83 | } 84 | 85 | public static Matrix pointwiseMultiply(Matrix A, Matrix B) { 86 | 87 | if((A.rows != B.rows) && (A.columns != B.columns)) { 88 | return null; 89 | } 90 | 91 | Integer A_rows = A.rows, A_cols = A.columns, B_rows = B.rows, B_cols = B.columns; 92 | 93 | Matrix C = new Matrix(A_rows, A_cols); 94 | 95 | for(Integer i = 0; i < A_rows; i++) { 96 | List A_row = A.getRow(i), B_row = B.getRow(i), C_row = new Double[A_cols]; 97 | for(Integer j = 0; j < A_cols; j ++) { 98 | C_row[j] = A_row[j] * B_row[j]; 99 | } 100 | 101 | C.setRow(C_row, i); 102 | } 103 | 104 | return C; 105 | } 106 | 107 | public static Matrix pointwiseExponent(Matrix A, Double exponent) { 108 | 109 | Integer A_rows = A.rows, A_cols = A.columns; 110 | 111 | for(Integer i = 0; i < A_rows; i++) { 112 | List A_row = A.getRow(i), A_row_exp = new Double[A_cols]; 113 | for(Integer j = 0; j < A_cols; j++) { 114 | A_row_exp[j] = Math.pow(A_row[j], exponent); 115 | } 116 | A.setRow(A_row_exp, i); 117 | } 118 | 119 | return A; 120 | } 121 | } -------------------------------------------------------------------------------- /src/classes/Matrix.cls: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Paul Battisson 3 | * @description This is the basic matrix class we will be using to help us define a matrix within our mathematical framework. 4 | */ 5 | 6 | public with sharing class Matrix 7 | { 8 | // Utility variables that we will need as we go 9 | public Integer rows {get; private set;} 10 | public Integer columns {get; private set;} 11 | 12 | //Our data elements 13 | private List> elements; 14 | 15 | //Initialise with JSON - will be useful later 16 | public Matrix(String JSONData) { 17 | 18 | this((List>)JSON.deserialize(JSONData, List>.class)); 19 | } 20 | 21 | //Initialise a matrix with a given number of rows and columns 22 | public Matrix(Integer numRows, Integer numCols) { 23 | rows = numRows; 24 | columns = numCols; 25 | 26 | elements = new List>(); 27 | 28 | for(Integer i = 0; i < numRows; i++) { 29 | elements.add(new Double[numCols]); 30 | } 31 | } 32 | 33 | //Initialise a matrix with a set of elements 34 | public Matrix(List> elems) { 35 | if(elems != null) { 36 | elements = elems; 37 | rows = elements.size(); 38 | columns = elements.get(0).size(); 39 | } 40 | } 41 | 42 | //Initialise a matrix with a set of data 43 | public Matrix(List elems) { 44 | if(elems != null) { 45 | elements = new List>(); 46 | elements.add(elems); 47 | rows = elements.size(); 48 | columns = elements.get(0).size(); 49 | } 50 | } 51 | 52 | //Set a specific element in the matrix 53 | public void setElement(Integer rowNum, Integer colNum, Double value) { 54 | if(rowNum < rows && colNum < columns && rowNum >= 0 && colNum >= 0) { 55 | elements.get(rowNum).set(colNum, value); 56 | } 57 | } 58 | 59 | //Retrieve a specific element from the matrix 60 | public Double getElement(Integer rowNum, Integer colNum) { 61 | if(rowNum < rows && colNum < columns && rowNum >= 0 && colNum >= 0) { 62 | return elements.get(rowNum).get(colNum); 63 | } 64 | 65 | return null; 66 | } 67 | 68 | //Get an entire row 69 | public List getRow(Integer rowNum) { 70 | return elements.get(rowNum); 71 | } 72 | 73 | //Set an entire row 74 | public void setRow(List row, Integer rowNum) { 75 | elements[rowNum] = row; 76 | } 77 | 78 | //Fill a matrix with zeroes 79 | public void Zeros() { 80 | fill(0); 81 | } 82 | 83 | //Create a printable form of the matrix for the debug log or screen 84 | public String Print() { 85 | String output = '\n'; 86 | for(Integer i = 0; i < rows; i++) { 87 | for(Integer j = 0; j < columns; j++) { 88 | output += getElement(i, j) + '\t'; 89 | } 90 | output += '\n'; 91 | } 92 | return output; 93 | } 94 | 95 | //Get the matrix as a single long list 96 | public List getDataAsList() { 97 | 98 | List data = new List(); 99 | 100 | for(List row : elements) { 101 | data.addAll(row); 102 | } 103 | 104 | return data; 105 | } 106 | 107 | //Create a matrix where every element is a specific value 108 | public void fill(Double value) { 109 | for(Integer i = 0; i < rows; i++) { 110 | for(Integer j = 0; j < columns; j++) { 111 | elements.get(i).set(j, value); 112 | } 113 | } 114 | } 115 | 116 | //Serialise our matrix to JSON 117 | public String toJSON() { 118 | return JSON.serialize(elements); 119 | } 120 | } -------------------------------------------------------------------------------- /src/classes/MatrixOperationsTest.cls: -------------------------------------------------------------------------------- 1 | @isTest 2 | private class MatrixOperationsTest { 3 | 4 | private static String JSONString = '[[1,2,3,4],[5,6,7,8],[9,10,11,12]]'; 5 | 6 | @isTest 7 | static void testMatrixMultiplication() { 8 | Matrix A = new Matrix(2, 2); 9 | Matrix B = new Matrix(2, 2); 10 | 11 | Double d = 2; 12 | 13 | A.setElement(0, 0, d); 14 | A.setElement(0, 1, d); 15 | A.setElement(1, 0, d); 16 | A.setElement(1, 1, d); 17 | B.setElement(0, 0, d); 18 | B.setElement(0, 1, d); 19 | B.setElement(1, 0, d); 20 | B.setElement(1, 1, d); 21 | 22 | Matrix C = MatrixOperations.multiply(A, B); 23 | 24 | System.assertEquals(2, C.rows); 25 | System.assertEquals(2, C.columns); 26 | System.assertEquals(8, C.getElement(0, 0)); 27 | System.assertEquals(8, C.getElement(0, 1)); 28 | System.assertEquals(8, C.getElement(1, 0)); 29 | System.assertEquals(8, C.getElement(1, 1)); 30 | 31 | } 32 | 33 | @isTest 34 | static void testInvalidMultiply() { 35 | Matrix A = new Matrix(1,1); 36 | Matrix B = new Matrix(2,2); 37 | 38 | System.assertEquals(null, MatrixOperations.multiply(A, B)); 39 | } 40 | 41 | @isTest 42 | static void testBasicTranspose() { 43 | List row1 = new List{1,2}; 44 | List row2 = new List{3,4}; 45 | Matrix A = new Matrix(new List>{row1, row2}); 46 | 47 | System.assertEquals(2, A.rows); 48 | System.assertEquals(2, A.columns); 49 | System.assertEquals(1, A.getElement(0, 0)); 50 | System.assertEquals(2, A.getElement(0, 1)); 51 | System.assertEquals(3, A.getElement(1, 0)); 52 | System.assertEquals(4, A.getElement(1, 1)); 53 | 54 | Matrix A_tran = MatrixOperations.transpose(A); 55 | 56 | System.assertEquals(2, A_tran.rows); 57 | System.assertEquals(2, A_tran.columns); 58 | System.assertEquals(1, A_tran.getElement(0, 0)); 59 | System.assertEquals(3, A_tran.getElement(0, 1)); 60 | System.assertEquals(2, A_tran.getElement(1, 0)); 61 | System.assertEquals(4, A_tran.getElement(1, 1)); 62 | 63 | } 64 | 65 | @isTest 66 | static void testAddition() { 67 | Matrix A = new Matrix(JSONString); 68 | Matrix B = new Matrix(JSONString); 69 | 70 | Matrix C = MatrixOperations.add(A, B); 71 | 72 | System.assertEquals(A.rows, C.rows); 73 | System.assertEquals(A.columns, C.columns); 74 | 75 | for(Integer i = 0; i < 2; i++) { 76 | for(Integer j = 0; j<2; j++) { 77 | System.assertEquals(2*A.getElement(i, j), C.getElement(i, j)); 78 | } 79 | } 80 | } 81 | 82 | @isTest 83 | static void testInvalidAddition() { 84 | Matrix A = new Matrix(1,1); 85 | Matrix B = new Matrix(2,2); 86 | 87 | System.assertEquals(null, MatrixOperations.add(A, B)); 88 | } 89 | 90 | @isTest 91 | static void testSubtraction() { 92 | Matrix A = new Matrix(JSONString); 93 | Matrix B = new Matrix(JSONString); 94 | 95 | Matrix C = MatrixOperations.subtract(A, B); 96 | 97 | System.assertEquals(A.rows, C.rows); 98 | System.assertEquals(A.columns, C.columns); 99 | 100 | for(Integer i = 0; i < 2; i++) { 101 | for(Integer j = 0; j<2; j++) { 102 | System.assertEquals(0, C.getElement(i, j)); 103 | } 104 | } 105 | } 106 | 107 | @isTest 108 | static void testInvalidSubtraction() { 109 | Matrix A = new Matrix(1,1); 110 | Matrix B = new Matrix(2,2); 111 | 112 | System.assertEquals(null, MatrixOperations.subtract(A, B)); 113 | } 114 | 115 | @isTest 116 | static void testPointwiseMultiply() { 117 | Matrix A = new Matrix(JSONString); 118 | Matrix B = new Matrix(JSONString); 119 | 120 | Matrix C = MatrixOperations.pointwiseMultiply(A, B); 121 | 122 | System.assertEquals(A.rows, C.rows); 123 | System.assertEquals(A.columns, C.columns); 124 | 125 | for(Integer i = 0; i < 2; i++) { 126 | for(Integer j = 0; j<2; j++) { 127 | System.assertEquals(Math.pow(A.getElement(i, j), 2), C.getElement(i, j)); 128 | } 129 | } 130 | } 131 | 132 | @isTest 133 | static void testInvalidPointwiseMultiply() { 134 | Matrix A = new Matrix(1,1); 135 | Matrix B = new Matrix(2,2); 136 | 137 | System.assertEquals(null, MatrixOperations.pointwiseMultiply(A, B)); 138 | } 139 | 140 | @isTest 141 | static void testPointwiseExponent() { 142 | List vect = new List{1.0, 2.0}; 143 | List vect2 = new List{3.0, 4.0}; 144 | 145 | Matrix A = new Matrix(new List>{vect, vect2}); 146 | Matrix B = MatrixOperations.pointwiseExponent(A, 2); 147 | 148 | System.assertEquals(1.0, B.getElement(0, 0)); 149 | System.assertEquals(4.0, B.getElement(0, 1)); 150 | System.assertEquals(9.0, B.getElement(1, 0)); 151 | System.assertEquals(16.0, B.getElement(1, 1)); 152 | } 153 | 154 | } --------------------------------------------------------------------------------