예제 #1
0
        /// <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);
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
        /// <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);
        }
예제 #4
0
        public bool Equals(FastMatrixBase <T> 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't same return false
            if ((Rows != matrix.Rows) || (Columns != matrix.Columns))
            {
                return(false);
            }

            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    if (!matrix[i, j].Equals(this[i, j]))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
예제 #5
0
        /// <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);
        }
예제 #6
0
        /// <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);
        }