/// <summary> /// Compute the dot product for two matrixes. Note: both matrixes must be vectors. /// </summary> /// <param name="a">The first matrix, must be a vector.</param> /// <param name="b">The second matrix, must be a vector.</param> /// <returns>The dot product of the two matrixes.</returns> public static double DotProduct(Matrix a, Matrix b) { if (!a.IsVector() || !b.IsVector()) { throw new MatrixError( "To take the dot product, both matrixes must be vectors."); } Double[] aArray = a.ToPackedArray(); Double[] bArray = b.ToPackedArray(); if (aArray.Length != bArray.Length) { throw new MatrixError( "To take the dot product, both matrixes must be of the same length."); } double result = 0; int length = aArray.Length; for (int i = 0; i < length; i++) { result += aArray[i] * bArray[i]; } return(result); }
/// <summary> /// Calculate the vector length of the matrix. /// </summary> /// <param name="input">The vector to calculate for.</param> /// <returns>The vector length.</returns> public static double VectorLength(Matrix input) { if (!input.IsVector()) { throw new MatrixError( "Can only take the vector length of a vector."); } Double[] v = input.ToPackedArray(); double rtn = 0.0; for (int i = 0; i < v.Length; i++) { rtn += Math.Pow(v[i], 2); } return(Math.Sqrt(rtn)); }
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 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()); }
/// <summary> /// Calculate the vector length of the matrix. /// </summary> /// <param name="input">The vector to calculate for.</param> /// <returns>The vector length.</returns> public static double VectorLength(Matrix input) { if (!input.IsVector()) { throw new MatrixError( "Can only take the vector length of a vector."); } Double[] v = input.ToPackedArray(); double rtn = 0.0; for (int i = 0; i < v.Length; i++) { rtn += Math.Pow(v[i], 2); } return Math.Sqrt(rtn); }
/// <summary> /// Compute the dot product for two matrixes. Note: both matrixes must be vectors. /// </summary> /// <param name="a">The first matrix, must be a vector.</param> /// <param name="b">The second matrix, must be a vector.</param> /// <returns>The dot product of the two matrixes.</returns> public static double DotProduct(Matrix a, Matrix b) { if (!a.IsVector() || !b.IsVector()) { throw new MatrixError( "To take the dot product, both matrixes must be vectors."); } Double[] aArray = a.ToPackedArray(); Double[] bArray = b.ToPackedArray(); if (aArray.Length != bArray.Length) { throw new MatrixError( "To take the dot product, both matrixes must be of the same length."); } double result = 0; int length = aArray.Length; for (int i = 0; i < length; i++) { result += aArray[i]*bArray[i]; } return result; }