public void RowsAndCols() { var matrix = new Matrix(6, 3); Assert.AreEqual(matrix.Rows, 6); Assert.AreEqual(matrix.Cols, 3); matrix[1, 2] = 1.5; Assert.AreEqual(matrix[1, 2], 1.5); }
public void DotProduct() { double[][] matrixData1 = {new[] {1.0, 2.0, 3.0, 4.0}}; double[][] matrixData2 = { new[] {5.0}, new[] {6.0}, new[] {7.0}, new[] {8.0} }; var matrix1 = new Matrix(matrixData1); var matrix2 = new Matrix(matrixData2); double dotProduct = MatrixMath.DotProduct(matrix1, matrix2); Assert.AreEqual(dotProduct, 70.0); // test dot product errors double[][] nonVectorData = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; double[][] differentLengthData = { new[] {1.0} }; var nonVector = new Matrix(nonVectorData); var differentLength = new Matrix(differentLengthData); try { MatrixMath.DotProduct(matrix1, nonVector); Assert.IsTrue(false); } catch (MatrixError) { } try { MatrixMath.DotProduct(nonVector, matrix2); Assert.IsTrue(false); } catch (MatrixError) { } try { MatrixMath.DotProduct(matrix1, differentLength); Assert.IsTrue(false); } catch (MatrixError) { } }
public void Copy() { double[][] data = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; var source = new Matrix(data); var target = new Matrix(2, 2); MatrixMath.Copy(source, target); Assert.IsTrue(source.Equals(target)); }
public void RowAndColRangeUnder() { var matrix = new Matrix(6, 3); // make sure set registers error on under-bound row try { matrix[-1, 0] = 1; Assert.IsTrue(false); // should have thrown an exception } catch (MatrixError) { } // make sure set registers error on under-bound col try { matrix[0, -1] = 1; Assert.IsTrue(false); // should have thrown an exception } catch (MatrixError) { } // make sure get registers error on under-bound row try { double d = matrix[-1, 0]; matrix[0, 0] = d; Assert.IsTrue(false); // should have thrown an exception } catch (MatrixError) { } // make sure set registers error on under-bound col try { double d = matrix[0, -1]; matrix[0, 0] = d; Assert.IsTrue(false); // should have thrown an exception } catch (MatrixError) { } }
public void Boolean() { bool[][] matrixDataBoolean = { new[] {true, false}, new[] {false, true} }; double[][] matrixDataDouble = { new[] {1.0, -1.0}, new[] {-1.0, 1.0}, }; var matrixBoolean = new Matrix(matrixDataBoolean); var matrixDouble = new Matrix(matrixDataDouble); Assert.IsTrue(matrixBoolean.Equals(matrixDouble)); }
public void Inverse() { double[][] matrixData1 = { new[] {1.0, 2.0, 3.0, 4.0} }; double[][] matrixData2 = { new[] {1.0}, new[] {2.0}, new[] {3.0}, new[] {4.0} }; var matrix1 = new Matrix(matrixData1); var checkMatrix = new Matrix(matrixData2); Matrix matrix2 = MatrixMath.Transpose(matrix1); Assert.IsTrue(matrix2.Equals(checkMatrix)); }
public void Bipolar2Double() { // test a 1x4 bool[] boolData1 = { true, false, true, false }; double[] checkData1 = { 1, -1, 1, -1 }; Matrix matrix1 = Matrix.CreateRowMatrix(BiPolarUtil.Bipolar2double(boolData1)); Matrix checkMatrix1 = Matrix.CreateRowMatrix(checkData1); Assert.IsTrue(matrix1.Equals(checkMatrix1)); // test a 2x2 bool[][] boolData2 = { new[]{ true, false }, new[]{ false, true } }; double[][] checkData2 = { new[] { 1.0, -1.0 }, new[] { -1.0, 1.0 } }; var matrix2 = new Matrix(BiPolarUtil.Bipolar2double(boolData2)); var checkMatrix2 = new Matrix(checkData2); Assert.IsTrue(matrix2.Equals(checkMatrix2)); }
public void DeleteRow() { double[][] origData = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; double[][] checkData = {new[] {3.0, 4.0}}; var orig = new Matrix(origData); Matrix matrix = MatrixMath.DeleteRow(orig, 0); var check = new Matrix(checkData); Assert.IsTrue(check.Equals(matrix)); try { MatrixMath.DeleteRow(orig, 10); Assert.IsTrue(false); } catch (MatrixError) { } }
public void VerifySame() { double[][] dataBase = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; double[][] dataTooManyRows = { new[] {1.0, 2.0}, new[] {3.0, 4.0}, new[] {5.0, 6.0} }; double[][] dataTooManyCols = { new[] {1.0, 2.0, 3.0}, new[] {4.0, 5.0, 6.0} }; var baseMatrix = new Matrix(dataBase); var tooManyRows = new Matrix(dataTooManyRows); var tooManyCols = new Matrix(dataTooManyCols); MatrixMath.Add(baseMatrix, baseMatrix); try { MatrixMath.Add(baseMatrix, tooManyRows); Assert.IsFalse(true); } catch (MatrixError) { } try { MatrixMath.Add(baseMatrix, tooManyCols); Assert.IsFalse(true); } catch (MatrixError) { } }
public void MultiplyScalar() { double[][] data = { new[] {2.0, 4.0}, new[] {6.0, 8.0} }; var matrix = new Matrix(data); Matrix result = MatrixMath.Multiply(matrix, 2.0); Assert.AreEqual(4.0, result[0, 0]); }
public void Multiply() { double[][] matrixData1 = { new[] {1.0, 4.0}, new[] {2.0, 5.0}, new[] {3.0, 6.0} }; double[][] matrixData2 = { new[] {7.0, 8.0, 9.0}, new[] {10.0, 11.0, 12.0} }; double[][] matrixData3 = { new[] {47.0, 52.0, 57.0}, new[] {64.0, 71.0, 78.0}, new[] {81.0, 90.0, 99.0} }; var matrix1 = new Matrix(matrixData1); var matrix2 = new Matrix(matrixData2); var matrix3 = new Matrix(matrixData3); Matrix result = MatrixMath.Multiply(matrix1, matrix2); Assert.IsTrue(result.Equals(matrix3)); }
public void Identity() { try { MatrixMath.Identity(0); Assert.IsTrue(false); } catch (MatrixError) { } double[][] checkData = { new[] {1.0, 0.0}, new[] {0.0, 1.0} }; var check = new Matrix(checkData); Matrix matrix = MatrixMath.Identity(2); Assert.IsTrue(check.Equals(matrix)); }
public void GetCol() { double[][] matrixData1 = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; double[][] matrixData2 = { new[] {2.0}, new[] {4.0} }; var matrix1 = new Matrix(matrixData1); var matrix2 = new Matrix(matrixData2); Matrix matrixCol = matrix1.GetCol(1); Assert.IsTrue(matrixCol.Equals(matrix2)); try { matrix1.GetCol(3); Assert.IsTrue(false); } catch (MatrixError) { Assert.IsTrue(true); } }
public void MatrixMultiply() { double[][] a = { new[] {1.0, 0.0, 2.0}, new[] {-1.0, 3.0, 1.0} }; double[][] b = { new[] {3.0, 1.0}, new[] {2.0, 1.0}, new[] {1.0, 0.0} }; double[][] c = { new[] {5.0, 1.0}, new[] {4.0, 2.0} }; var matrixA = new Matrix(a); var matrixB = new Matrix(b); var matrixC = new Matrix(c); var result = (Matrix) matrixA.Clone(); result.ToString(); result = MatrixMath.Multiply(matrixA, matrixB); Assert.IsTrue(result.Equals(matrixC)); double[][] a2 = { new[] {1.0, 2.0, 3.0, 4.0}, new[] {5.0, 6.0, 7.0, 8.0} }; double[][] b2 = { new[] {1.0, 2.0, 3.0}, new[] {4.0, 5.0, 6.0}, new[] {7.0, 8.0, 9.0}, new[] {10.0, 11.0, 12.0} }; double[][] c2 = { new[] {70.0, 80.0, 90.0}, new[] {158.0, 184.0, 210.0} }; matrixA = new Matrix(a2); matrixB = new Matrix(b2); matrixC = new Matrix(c2); result = MatrixMath.Multiply(matrixA, matrixB); Assert.IsTrue(result.Equals(matrixC)); matrixB.Clone(); try { MatrixMath.Multiply(matrixB, matrixA); Assert.IsTrue(false); } catch (MatrixError) { } }
public void Divide() { double[][] data = { new[] {2.0, 4.0}, new[] {6.0, 8.0} }; var matrix = new Matrix(data); Matrix result = MatrixMath.Divide(matrix, 2.0); Assert.AreEqual(1.0, result[0, 0]); }
public void Sum() { double[][] doubleData = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; var matrix = new Matrix(doubleData); Assert.AreEqual((int) matrix.Sum(), 1 + 2 + 3 + 4); }
public void MatrixConstruct() { double[][] m = { new[] {1.0, 2.0, 3.0, 4.0}, new[] {5.0, 6.0, 7.0, 8.0}, new[] {9.0, 10.0, 11.0, 12.0}, new[] {13.0, 14.0, 15.0, 16.0} }; var matrix = new Matrix(m); Assert.AreEqual(matrix.Rows, 4); Assert.AreEqual(matrix.Cols, 4); }
public void MatrixEquals() { double[][] m1 = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; double[][] m2 = { new[] {0.0, 2.0}, new[] {3.0, 4.0} }; var matrix1 = new Matrix(m1); var matrix2 = new Matrix(m1); Assert.IsTrue(matrix1.Equals(matrix2)); matrix2 = new Matrix(m2); Assert.IsFalse(matrix1.Equals(matrix2)); }
public void Randomize() { const double min = 1.0; const double max = 10.0; var matrix = new Matrix(10, 10); matrix.Ramdomize(min, max); var array = matrix.ToPackedArray(); foreach (double t in array) { if (t < min || t > max) Assert.IsFalse(true); } }
public void PackedArray2() { double[] data = {1.0, 2.0, 3.0, 4.0}; var matrix = new Matrix(1, 4); matrix.FromPackedArray(data, 0); Assert.AreEqual(1.0, matrix[0, 0]); Assert.AreEqual(2.0, matrix[0, 1]); Assert.AreEqual(3.0, matrix[0, 2]); }
public void PackedArray() { double[][] matrixData = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; var matrix = new Matrix(matrixData); double[] matrixData2 = matrix.ToPackedArray(); Assert.AreEqual(4, matrixData2.Length); Assert.AreEqual(1.0, matrix[0, 0]); Assert.AreEqual(2.0, matrix[0, 1]); Assert.AreEqual(3.0, matrix[1, 0]); Assert.AreEqual(4.0, matrix[1, 1]); var matrix2 = new Matrix(2, 2); matrix2.FromPackedArray(matrixData2, 0); Assert.IsTrue(matrix.Equals(matrix2)); }
public void IsVector() { double[] matrixData = {1.0, 2.0, 3.0, 4.0}; Matrix matrixCol = Matrix.CreateColumnMatrix(matrixData); Matrix matrixRow = Matrix.CreateRowMatrix(matrixData); Assert.IsTrue(matrixCol.IsVector()); Assert.IsTrue(matrixRow.IsVector()); double[][] matrixData2 = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; var matrix = new Matrix(matrixData2); Assert.IsFalse(matrix.IsVector()); }
public void VectorLength() { double[] vectorData = {1.0, 2.0, 3.0, 4.0}; Matrix vector = Matrix.CreateRowMatrix(vectorData); Assert.AreEqual(5, (int) MatrixMath.VectorLength(vector)); var nonVector = new Matrix(2, 2); try { MatrixMath.VectorLength(nonVector); Assert.IsTrue(false); } catch (MatrixError) { } }
public void Zero() { double[][] doubleData = { new[] {0.0, 0.0}, new[] {0.0, 0.0} }; var matrix = new Matrix(doubleData); Assert.IsTrue(matrix.IsZero()); }
public void Size() { double[][] data = { new[] {1.0, 2.0}, new[] {3.0, 4.0} }; var matrix = new Matrix(data); Assert.AreEqual(4, matrix.Size); }
public void MatrixEqualsPrecision() { double[][] m1 = { new[] {1.1234, 2.123}, new[] {3.123, 4.123} }; double[][] m2 = { new[] {1.123, 2.123}, new[] {3.123, 4.123} }; var matrix1 = new Matrix(m1); var matrix2 = new Matrix(m2); Assert.IsTrue(matrix1.equals(matrix2, 3)); Assert.IsFalse(matrix1.equals(matrix2, 4)); double[][] m3 = { new[] {1.1, 2.1}, new[] {3.1, 4.1} }; double[][] m4 = { new[] {1.2, 2.1}, new[] {3.1, 4.1} }; var matrix3 = new Matrix(m3); var matrix4 = new Matrix(m4); Assert.IsTrue(matrix3.equals(matrix4, 0)); Assert.IsFalse(matrix3.equals(matrix4, 1)); try { matrix3.equals(matrix4, -1); Assert.IsTrue(false); } catch (MatrixError) { } try { matrix3.equals(matrix4, 19); Assert.IsTrue(false); } catch (MatrixError) { } }