예제 #1
0
파일: Matrix.cs 프로젝트: tantock/Math.NET
        /// <summary>
        /// Matrix multiplication
        /// </summary>
        /// <param name="L">Left matrix</param>
        /// <param name="R">Right matrix</param>
        /// <returns>new Matrix(L*R)</returns>
        public static Matrix <U> operator *(Matrix <U> L, Matrix <U> R)
        {
            int m = L.M;
            int n = L.N;
            int p = R.N;

            U[][] result = new U[m][];

            if (R.numRows != L.numColumns)
            {
                throw new InvalidOperationException("Invalid dimensions for matrix multiplication");
            }
            else
            {
                //create new 2d array
                for (int i = 0; i < m; i++)
                {
                    result[i] = new U[p];
                }
                Parallel.For(0, m, i =>//for (int i = 0; i < m; i++)
                {
                    for (int j = 0; j < p; j++)
                    {
                        result[i][j] = operators.GetZeroValue();
                        for (int k = 0; k < n; k++)
                        {
                            result[i][j] = operators.add(operators.multiply(L.values[i][k], R.values[k][j]), result[i][j]);
                        }
                    }
                });

                return(new Matrix <U>(result));
            }
        }
예제 #2
0
 /// <summary>
 /// Vector addition
 /// </summary>
 /// <param name="L">Left vector</param>
 /// <param name="R">Right vector</param>
 /// <returns>new Vector(L+R)</returns>
 public static Vector <T> operator +(Vector <T> L, Vector <T> R)
 {
     T[] result = new T[L.numEntries];
     if (R.numEntries != L.numEntries)
     {
         throw new InvalidOperationException("Invalid dimensions for vector addition");
     }
     else
     {
         for (int i = 0; i < result.Length; i++)
         {
             result[i] = operators.add(L.values[i], R.values[i]);
         }
         return(new Vector <T>(result));
     }
 }