Ejemplo n.º 1
0
        public static Object Where(this Double[,] matrix, Function f)
        {
            var result  = new List <Double>();
            var args    = new Object[4];
            var rows    = matrix.GetRows();
            var columns = matrix.GetColumns();

            for (int i = 0, k = 0; i < rows; i++)
            {
                for (var j = 0; j < columns; j++, k++)
                {
                    var value = matrix[i, j];
                    args[0] = value;
                    args[1] = (Double)k;
                    args[2] = (Double)i;
                    args[3] = (Double)j;

                    if (f(args).ToBoolean())
                    {
                        result.Add(value);
                    }
                }
            }

            return(result.ToMatrix());
        }
Ejemplo n.º 2
0
        public static Double[,] Multiply(this Double[,] a, Double[,] b)
        {
            var rows   = a.GetRows();
            var cols   = b.GetColumns();
            var length = a.GetColumns();

            if (length == b.GetRows())
            {
                var result = new Double[rows, cols];

                for (var i = 0; i < rows; i++)
                {
                    for (var j = 0; j < cols; j++)
                    {
                        var value = 0.0;

                        for (var k = 0; k < length; k++)
                        {
                            value += a[i, k] * b[k, j];
                        }

                        result[i, j] = value;
                    }
                }

                return(result);
            }

            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts the given matrix to a JSON string.
        /// </summary>
        /// <param name="matrix">The matrix to represent.</param>
        /// <returns>The JSON representation.</returns>
        public static String AsJson(Double[,] matrix)
        {
            var sb   = StringBuilderPool.Pull();
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();

            sb.Append('[');

            for (var i = 0; i < rows; i++)
            {
                if (i > 0)
                {
                    sb.Append(", ");
                }
                sb.Append('[');

                for (var j = 0; j < cols; j++)
                {
                    if (j > 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append(This(matrix[i, j]));
                }

                sb.Append(']');
            }

            sb.Append(']');
            return(sb.Stringify());
        }
Ejemplo n.º 4
0
        public static Double ToNumber(this Double[,] matrix)
        {
            if (matrix.GetRows() == 1 && matrix.GetColumns() == 1)
            {
                return(matrix[0, 0]);
            }

            return(Double.NaN);
        }
Ejemplo n.º 5
0
        public static Boolean Fits(this Double[,] a, Double[,] b)
        {
            var rowsA = a.GetRows();
            var rowsB = b.GetRows();
            var colsA = a.GetColumns();
            var colsB = b.GetColumns();

            return(rowsA == rowsB && colsA == colsB);
        }
Ejemplo n.º 6
0
        public static void SetValue(this Double[,] matrix, Int32 row, Int32 col, Double value)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();

            if (row >= 0 && row < rows && col >= 0 && col < cols)
            {
                matrix[row, col] = value;
            }
        }
Ejemplo n.º 7
0
        public static Double GetValue(this Double[,] matrix, Int32 row, Int32 col)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();

            if (row >= 0 && row < rows && col >= 0 && col < cols)
            {
                return(matrix[row, col]);
            }

            return(0.0);
        }
Ejemplo n.º 8
0
        public static Double[,] Identity(this Double[,] matrix)
        {
            var rows   = matrix.GetRows();
            var cols   = matrix.GetColumns();
            var result = new Double[rows, cols];
            var length = Math.Min(rows, cols);

            for (var i = 0; i < length; i++)
            {
                result[i, i] = 1.0;
            }

            return(result);
        }
Ejemplo n.º 9
0
        public static Object Reduce(this Double[,] matrix, Func <Double, Double, Double> reducer)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();

            if (rows == 1 && cols == 1)
            {
                return(matrix[0, 0]);
            }
            else if (rows == 1)
            {
                var element = matrix[0, 0];

                for (var i = 1; i < cols; i++)
                {
                    element = reducer.Invoke(element, matrix[0, i]);
                }

                return(element);
            }
            else if (cols == 1)
            {
                var element = matrix[0, 0];

                for (var i = 1; i < rows; i++)
                {
                    element = reducer.Invoke(element, matrix[i, 0]);
                }

                return(element);
            }
            else
            {
                var result = new Double[rows, 1];

                for (var i = 0; i < rows; i++)
                {
                    var element = matrix[i, 0];

                    for (var j = 1; j < cols; j++)
                    {
                        element = reducer.Invoke(element, matrix[i, j]);
                    }

                    result[i, 0] = element;
                }

                return(result);
            }
        }
Ejemplo n.º 10
0
        public static Double[,] Transpose(this Double[,] matrix)
        {
            var rows   = matrix.GetRows();
            var cols   = matrix.GetColumns();
            var result = new Double[cols, rows];

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    result[j, i] = matrix[i, j];
                }
            }

            return(result);
        }
Ejemplo n.º 11
0
        public static Double[,] ForEach(this Double[,] matrix, Func <Double, Double> apply)
        {
            var rows   = matrix.GetRows();
            var cols   = matrix.GetColumns();
            var result = new Double[rows, cols];

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    result[i, j] = apply(matrix[i, j]);
                }
            }

            return(result);
        }
Ejemplo n.º 12
0
        public static Double[,] Pow(this Double value, Double[,] matrix)
        {
            var result = matrix.Fill(value);
            var rows   = matrix.GetRows();
            var cols   = matrix.GetColumns();

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    result[i, j] = Math.Pow(result[i, j], matrix[i, j]);
                }
            }

            return(result);
        }
Ejemplo n.º 13
0
        public static Double Abs(Double[,] matrix)
        {
            var sum  = 0.0;
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    sum += matrix[i, j] * matrix[i, j];
                }
            }

            return(Math.Sqrt(sum));
        }
Ejemplo n.º 14
0
        public static Boolean AllTrue(this Double[,] matrix)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();
            var res  = true;

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; res && j < cols; j++)
                {
                    res = matrix[i, j].ToBoolean();
                }
            }

            return(res);
        }
Ejemplo n.º 15
0
        public static Double[,] Multiply(this Double[,] a, Double b)
        {
            var rows   = a.GetRows();
            var cols   = a.GetColumns();
            var result = new Double[rows, cols];

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    result[i, j] = a[i, j] * b;
                }
            }

            return(result);
        }
Ejemplo n.º 16
0
        public static List <Double> ToList(this Double[,] matrix)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();
            var list = new List <Double>(rows * cols);

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    list.Add(matrix[i, j]);
                }
            }

            return(list);
        }
Ejemplo n.º 17
0
        public static Double[,] Fill(this Double[,] matrix, Double value)
        {
            var rows   = matrix.GetRows();
            var cols   = matrix.GetColumns();
            var result = new Double[rows, cols];
            var length = Math.Min(rows, cols);

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    result[i, j] = value;
                }
            }

            return(result);
        }
Ejemplo n.º 18
0
        public static Boolean HasAny(this Double[,] matrix, Predicate <Double> check)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    if (check(matrix[i, j]))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 19
0
        public static Boolean TryGetIndices(this Double[,] matrix, Object[] arguments, out Int32 row, out Int32 col)
        {
            var rows = matrix.GetRows();
            var cols = matrix.GetColumns();
            var n    = 0;

            row = -1;
            col = -1;

            if (arguments.Length == 1 && arguments[0].TryGetIndex(out n))
            {
                col = n % cols;
                row = n / cols;
            }
            else if (arguments.Length == 2 && arguments[0].TryGetIndex(out row) && arguments[1].TryGetIndex(out col))
            {
            }

            return(row >= 0 && col >= 0 && row < rows && col < cols);
        }
Ejemplo n.º 20
0
        public static Double[,] Or(this Double[,] a, Double[,] b)
        {
            if (a.Fits(b))
            {
                var rows   = a.GetRows();
                var cols   = a.GetColumns();
                var result = new Double[rows, cols];

                for (var i = 0; i < rows; i++)
                {
                    for (var j = 0; j < cols; j++)
                    {
                        result[i, j] = (a[i, j].ToBoolean() || b[i, j].ToBoolean()).ToNumber();
                    }
                }

                return(result);
            }

            return(null);
        }
Ejemplo n.º 21
0
        public static Object Map(this Double[,] matrix, Function f)
        {
            var result  = new Dictionary <String, Object>();
            var args    = new Object[4];
            var rows    = matrix.GetRows();
            var columns = matrix.GetColumns();

            for (int i = 0, k = 0; i < rows; i++)
            {
                for (var j = 0; j < columns; j++, k++)
                {
                    args[0] = matrix[i, j];
                    args[1] = (Double)k;
                    args[2] = (Double)i;
                    args[3] = (Double)j;
                    result[k.ToString()] = f(args);
                }
            }

            return(result);
        }
Ejemplo n.º 22
0
        public static Double[,] IsGreaterOrEqual(this Double[,] a, Double[,] b)
        {
            if (a.Fits(b))
            {
                var rows   = a.GetRows();
                var cols   = a.GetColumns();
                var result = new Double[rows, cols];

                for (var i = 0; i < rows; i++)
                {
                    for (var j = 0; j < cols; j++)
                    {
                        result[i, j] = (a[i, j] >= b[i, j]).ToNumber();
                    }
                }

                return(result);
            }

            return(null);
        }
Ejemplo n.º 23
0
        public static Double[,] Pow(this Double[,] a, Double[,] b)
        {
            if (a.Fits(b))
            {
                var rows   = a.GetRows();
                var cols   = a.GetColumns();
                var result = new Double[rows, cols];

                for (var i = 0; i < rows; i++)
                {
                    for (var j = 0; j < cols; j++)
                    {
                        result[i, j] = Math.Pow(a[i, j], b[i, j]);
                    }
                }

                return(result);
            }

            return(null);
        }
Ejemplo n.º 24
0
        public static Object Reduce(this Double[,] matrix, Function f, Object start)
        {
            var args    = new Object[5];
            var result  = start;
            var rows    = matrix.GetRows();
            var columns = matrix.GetColumns();

            for (int i = 0, k = 0; i < rows; i++)
            {
                for (var j = 0; j < columns; j++, k++)
                {
                    args[0] = result;
                    args[1] = matrix[i, j];
                    args[2] = (Double)k;
                    args[3] = (Double)i;
                    args[4] = (Double)j;
                    result  = f(args);
                }
            }

            return(result);
        }
Ejemplo n.º 25
0
 public static Boolean IsSquare(this Double[,] matrix)
 {
     return(matrix.GetColumns() == matrix.GetRows());
 }