/// <summary> /// Multiplies two matrices on the CPU using a single thread /// </summary> /// <param name="one">The first matrix</param> /// <param name="two">The second matrix</param> /// <returns>The result of the multiplication</returns> public FastMatrix <T> Multiply(FastMatrixBase <T> one, FastMatrixBase <T> two) { if (one == null || two == null) { throw new ArgumentNullException(); } if (one.Columns != two.Rows) { throw new BadDimensionException(one.Rows, one.Columns, two.Rows, two.Columns); } FastMatrix <T> returnMatrix = new FastMatrix <T>(one.Rows, two.Columns); for (int i = 0; i < returnMatrix.Rows; i++) { for (int j = 0; j < returnMatrix.Columns; j++) { T sum = one[i, 0].Multiply(two[0, j]); for (int k = 1; k < one.Rows; k++) { sum = sum.Add(one[i, k].Multiply(two[k, j])); } returnMatrix[i, j] = sum; } } return(returnMatrix); }
/// <summary> /// Transposes a matrix on the CPU using a single thread /// </summary> /// <param name="matrix">The matrix</param> /// <returns>The result of the transpose</returns> public FastMatrix <T> Transpose(FastMatrixBase <T> matrix) { if (matrix == null) { throw new ArgumentNullException(); } FastMatrix <T> returnMatrix = new FastMatrix <T>(matrix.Columns, matrix.Rows); for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { returnMatrix[j, i] = matrix[i, j]; } } return(returnMatrix); }
/// <summary> /// Transposes a matrix on the CPU using multiple threads /// </summary> /// <param name="matrix">The matrix</param> /// <returns>The transposed matrix</returns> public FastMatrix <T> Transpose(FastMatrixBase <T> matrix) { if (matrix == null) { throw new ArgumentNullException(); } FastMatrix <T> returnMatrix = new FastMatrix <T>(matrix.Columns, matrix.Rows); Parallel.For(0, matrix.Rows, (i) => { for (int j = 0; j < matrix.Columns; j++) { returnMatrix[j, i] = matrix[i, j]; } }); return(returnMatrix); }
/// <summary> /// Subtracts two matrices on the CPU using a single thread /// </summary> /// <param name="one">The first matrix</param> /// <param name="two">The second matrix</param> /// <returns>The result of the subtraction (one - two)</returns> public FastMatrix <T> Subtract(FastMatrixBase <T> one, FastMatrixBase <T> two) { if (one == null || two == null) { throw new ArgumentNullException(); } if ((one.Rows != two.Rows) || (one.Columns != two.Columns)) { throw new BadDimensionException(one.Rows, one.Columns, two.Rows, two.Columns); } FastMatrix <T> fastMatrix = new FastMatrix <T>(one.Rows, two.Columns); for (int i = 0; i < one.Rows; i++) { for (int j = 0; j < one.Columns; j++) { fastMatrix[i, j] = one[i, j].Subtract(two[i, j]); } } return(fastMatrix); }
/// <summary> /// Adds two matrices on the CPU using multiple threads /// </summary> /// <param name="one">The first matrix</param> /// <param name="two">The second matrix</param> /// <returns>The result of the addition</returns> public FastMatrix <T> Add(FastMatrixBase <T> one, FastMatrixBase <T> two) { if (one == null || two == null) { throw new ArgumentNullException(); } if ((one.Rows != two.Rows) || (one.Columns != two.Columns)) { throw new BadDimensionException(one.Rows, one.Columns, two.Rows, two.Columns); } FastMatrix <T> fastMatrix = new FastMatrix <T>(one.Rows, two.Columns); Parallel.For(0, one.Rows, i => { for (int j = 0; j < one.Columns; j++) { fastMatrix[i, j] = one[i, j].Add(two[i, j]); } }); return(fastMatrix); }
/// <summary> /// Checks if this matrix is equal to another by looking at their contents /// </summary> /// <param name="matrix">The matrix to compare to</param> /// <returns>A bool representing wheter they are equal or not</returns> public bool Equals(FastMatrix matrix) { // If parameter is null, return false. if (Object.ReferenceEquals(matrix, null)) { return(false); } // Optimization for a common success case. if (Object.ReferenceEquals(this, matrix)) { return(true); } // If run-time types are not exactly the same, return false. if (this.GetType() != matrix.GetType()) { return(false); } //if sizes aren'double same return false if ((GetSize(0) != matrix.GetSize(0)) || (GetSize(1) != matrix.GetSize(1))) { return(false); } for (int i = 0; i < GetSize(0); i++) { for (int j = 0; j < GetSize(1); j++) { if (!matrix[i, j].Equals(this[i, j])) { return(false); } } } return(true); }