Exemplo n.º 1
0
 public static Matrixs Subtract(Matrixs a, Matrixs b)
 {
     if (SubtractableDimensions(a, b))
     {
         var result = new Matrixs(a.Rows(), b.Cols());
         if (b.Rows() >= 1 && b.Cols() >= 1)
         {
             for (var i = 0; i < a.Rows(); i++)
             {
                 for (var j = 0; j < a.Cols(); j++)
                 {
                     result.data[i, j] = a.data[i, j] - b.data[i, j];
                 }
             }
             return(result);
         }
         else
         {
             for (var i = 0; i < a.Rows(); i++)
             {
                 for (var j = 0; j < a.Cols(); j++)
                 {
                     result.data[i, j] = a.data[i, j] - b.data[0, 0];
                 }
             }
             return(result);
         }
     }
     else
     {
         throw new System.ArgumentException("Matrices dimensions do not match for substraction");
     }
 }
Exemplo n.º 2
0
 private static bool SubtractableDimensions(Matrixs a, Matrixs b)
 {
     if (a.Rows() == b.Rows() && a.Cols() == b.Cols() || b.Rows() == 0 && b.Cols() == 0)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 3
0
 public static double[] ToArray(Matrixs matrix)
 {
     double[] arr = new double[matrix.Rows() + matrix.Cols()];
     for (int i = 0; i < matrix.Rows(); i++)
     {
         for (int j = 0; j < matrix.Cols(); j++)
         {
             arr[i] = matrix.Data()[i, j];
         }
     }
     return(arr);
 }
Exemplo n.º 4
0
        public static Matrixs Transpose(Matrixs matrix)
        {
            var result = new Matrixs(matrix.Cols(), matrix.Rows());

            for (var i = 0; i < matrix.Rows(); i++)
            {
                for (var j = 0; j < matrix.Cols(); j++)
                {
                    result.data[j, i] += matrix.data[i, j];
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        public static Matrixs Map(Matrixs matrix, Func <double, double> f)
        {
            var result = new Matrixs(matrix.Rows(), matrix.Cols());

            //Console.WriteLine("Rows(): " + Rows() + " Cols(): " + Cols());
            for (int i = 0; i < matrix.Rows(); i++)
            {
                for (int j = 0; j < matrix.Cols(); j++)
                {
                    double val = matrix.data[i, j];
                    result.data[i, j] = f(val);
                }
            }
            return(result);
        }
Exemplo n.º 6
0
        public static double[,] Multiply(Matrixs a, Matrixs b)
        {
            int m = a.Rows();
            int n = a.Cols();

            int p = b.Rows();
            int q = b.Cols();

            if (n == p)
            {
                double[,] c = new double[m, q];
                for (var i = 0; i < m; i++)
                {
                    for (var j = 0; j < q; j++)
                    {
                        c[i, j] = 0;
                        for (int k = 0; k < n; k++)
                        {
                            c[i, j] += a.Data()[i, k] * b.Data()[k, j];
                        }
                    }
                }
                return(c);
            }
            else
            {
                throw new System.ArgumentException("Matrices dimensions do not match for multiply", "matrix");
            }
        }
Exemplo n.º 7
0
        public static void DisplayMatrix(Matrixs a)
        {
            int m = a.Rows();
            int n = a.Cols();

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(a.Data()[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 8
0
 public static Matrixs Add(Matrixs a, Matrixs b)
 {
     if (SubtractableDimensions(a, b))
     {
         for (var i = 0; i < a.Rows(); i++)
         {
             for (var j = 0; j < a.Cols(); j++)
             {
                 a.Data()[i, j] += b.Data()[i, j];
             }
         }
         return(a);
     }
     else
     {
         throw new System.ArgumentException("Matrices dimensions do not match for substraction");
     }
 }
Exemplo n.º 9
0
        public static Matrixs Multiply(Matrixs a, Matrixs b)
        {
            int m = a.Rows();
            int n = a.Cols();

            int p = b.Rows();
            int q = b.Cols();

            if (n == p)
            {
                Matrixs c = new Matrixs(m, q);
                for (var i = 0; i < m; i++)
                {
                    for (var j = 0; j < q; j++)
                    {
                        c.data[i, j] = 0;
                        for (var k = 0; k < n; k++)
                        {
                            c.data[i, j] += a.data[i, k] * b.data[k, j];
                        }
                    }
                }
                return(c);
            }
            else if (m == p && n == q) // Element wise multiplication
            {
                Matrixs c = new Matrixs(m, q);
                for (var i = 0; i < m; i++)
                {
                    for (var j = 0; j < q; j++)
                    {
                        c.data[i, j] = a.data[i, j] * b.data[i, j];
                    }
                }
                return(c);
            }
            else
            {
                throw new System.ArgumentException("Matrices dimensions do not match for multiply", "matrix");
            }
        }