Пример #1
0
        public BarPlotValue Function(MatrixValue Y, MatrixValue x)
        {
            var bp = new BarPlotValue();
            var X  = new double[x.Length];

            for (var i = 0; i < x.Length; i++)
            {
                X[i] = x[i + 1].Re;
            }

            if (Y.IsVector)
            {
                bp.AddPoints(YMath.Histogram(Y, X));
            }
            else
            {
                var M = new MatrixValue();

                for (var i = 1; i <= Y.DimensionX; i++)
                {
                    var N = YMath.Histogram(Y.GetColumnVector(i), X);

                    for (var j = 1; j <= N.Length; j++)
                    {
                        M[j, i] = N[j];
                    }
                }

                bp.AddPoints(M);
            }

            return(bp);
        }
Пример #2
0
        public BarPlotValue Function(MatrixValue Y, ScalarValue nbins)
        {
            var nn = nbins.GetIntegerOrThrowException("nbins", Name);
            var bp = new BarPlotValue();

            if (Y.IsVector)
            {
                bp.AddPoints(YMath.Histogram(Y, nn));
            }
            else
            {
                var M = new MatrixValue();

                for (var i = 1; i <= Y.DimensionX; i++)
                {
                    var N = YMath.Histogram(Y.GetColumnVector(i), nn);

                    for (var j = 1; j <= N.Length; j++)
                    {
                        M[j, i] = N[j];
                    }
                }

                bp.AddPoints(M);
            }

            return(bp);
        }
        public MatrixValue Function(MatrixValue Y, MatrixValue x)
        {
            var X = new Double[x.Length];

            for (var i = 0; i < x.Length; i++)
            {
                X[i] = x[i + 1].Re;
            }

            if (!Y.IsVector)
            {
                var M = new MatrixValue();

                for (var i = 1; i <= Y.DimensionX; i++)
                {
                    var N = YMath.Histogram(Y.GetColumnVector(i), X);

                    for (var j = 1; j <= N.Length; j++)
                    {
                        M[j, i] = N[j];
                    }
                }

                return(M);
            }

            return(YMath.Histogram(Y, X));
        }
Пример #4
0
        public override MatrixValue Function(MatrixValue m)
        {
            var M = new MatrixValue(1, m.DimensionX);

            for (var i = 1; i <= m.DimensionX; i++)
            {
                M[1, i] = GetVectorMax(m.GetColumnVector(i));
            }

            return(M);
        }
Пример #5
0
        public MatrixValue Function(MatrixValue m)
        {
            if (m.DimensionX == 1)
            {
                return GetVectorProd(m.GetColumnVector(1));
            }
            else if (m.DimensionY == 1)
            {
                return GetVectorProd(m.GetRowVector(1));
            }
            else
            {
                var M = new MatrixValue(m.DimensionY, m.DimensionX);

                for (var i = 1; i <= m.DimensionX; i++)
                {
                    M.SetColumnVector(i, GetVectorProd(m.GetColumnVector(i)));
                }

                return M;
            }
        }
Пример #6
0
        public Value Function(MatrixValue m)
        {
            if (m.DimensionX == 1)
            {
                return GetVectorSum(m.GetColumnVector(1));
            }
            else if (m.DimensionY == 1)
            {
                return GetVectorSum(m.GetRowVector(1));
            }
            else
            {
                var M = new MatrixValue(1, m.DimensionX);

                for (var i = 1; i <= m.DimensionX; i++)
                {
                    M[1, i] = GetVectorSum(m.GetColumnVector(i));
                }

                return M;
            }
        }
Пример #7
0
        public Value Function(MatrixValue m)
        {
            if (m.DimensionX == 1)
            {
                return(GetVectorProduct(m.GetColumnVector(1)));
            }
            else if (m.DimensionY == 1)
            {
                return(GetVectorProduct(m.GetRowVector(1)));
            }
            else
            {
                var M = new MatrixValue(1, m.DimensionX);

                for (var i = 1; i <= m.DimensionX; i++)
                {
                    M[1, i] = GetVectorProduct(m.GetColumnVector(i));
                }

                return(M);
            }
        }
Пример #8
0
        public MatrixValue Function(MatrixValue m)
        {
            if (m.DimensionX == 1)
            {
                return(GetVectorSum(m.GetColumnVector(1)));
            }
            else if (m.DimensionY == 1)
            {
                return(GetVectorSum(m.GetRowVector(1)));
            }
            else
            {
                var M = new MatrixValue(m.DimensionY, m.DimensionX);

                for (var i = 1; i <= m.DimensionX; i++)
                {
                    M.SetColumnVector(i, GetVectorSum(m.GetColumnVector(i)));
                }

                return(M);
            }
        }
        /// <summary>
        /// Solves the system of linear equations.
        /// </summary>
        /// <param name="b">The vector b in A * x = b.</param>
        /// <returns>The solution vector x.</returns>
        public override MatrixValue Solve(MatrixValue b)
        {
            var         k = Restart;
            MatrixValue x;

            if (X0 == null)
            {
                X0 = new MatrixValue(b.DimensionY, b.DimensionX);
            }
            else if (X0.DimensionX != b.DimensionX || X0.DimensionY != b.DimensionY)
            {
                throw new YAMPDifferentDimensionsException(X0, b);
            }

            H     = new MatrixValue(k + 1, k);
            V     = new MatrixValue(X0.DimensionY, k);
            c     = new MatrixValue(k - 1, 1);
            s     = new MatrixValue(k - 1, 1);
            gamma = new MatrixValue(k + 1, 1);
            var converged = false;

            do
            {
                var j = 0;
                x = X0.Clone();
                var r0   = b - A * x;
                var beta = r0.Abs().Re;

                H.Clear();
                V.Clear();
                gamma.Clear();

                gamma[1] = new ScalarValue(beta);
                c.Clear();
                s.Clear();

                V.SetColumnVector(1, r0 / gamma[1]);

                if (beta < Tolerance)
                {
                    break;
                }

                do
                {
                    j++;
                    i++;

                    var Avj = A * V.GetColumnVector(j);
                    var sum = new MatrixValue(Avj.DimensionY, Avj.DimensionX);

                    for (var m = 1; m <= j; m++)
                    {
                        var w = V.GetColumnVector(m);
                        H[m, j] = w.ComplexDot(Avj);
                        sum    += H[m, j] * w;
                    }

                    var wj = Avj - sum;
                    H[j + 1, j] = wj.Abs();
                    Rotate(j);

                    if (H[j + 1, j].AbsSquare() == 0.0)
                    {
                        converged = true;
                        break;
                    }

                    V.SetColumnVector(j + 1, wj / H[j + 1, j]);
                    beta = gamma[j + 1].Abs();

                    if (beta < Tolerance)
                    {
                        converged = true;
                        break;
                    }
                }while (j < k);

                var y = new MatrixValue(j, 1);

                for (int l = j; l >= 1; l--)
                {
                    var sum = ScalarValue.Zero;

                    for (var m = l + 1; m <= j; m++)
                    {
                        sum += H[l, m] * y[m];
                    }

                    y[l] = (gamma[l] - sum) / H[l, l];
                }

                for (var l = 1; l <= j; l++)
                {
                    x += y[l] * V.GetColumnVector(l);
                }

                if (converged)
                {
                    break;
                }

                X0 = x;
            }while (i < MaxIterations);

            return(x);
        }
Пример #10
0
        /// <summary>
        /// Solves the system of linear equations.
        /// </summary>
        /// <param name="b">The vector b in A * x = b.</param>
        /// <returns>The solution vector x.</returns>
        public override MatrixValue Solve(MatrixValue b)
        {
            var k = Restart;
            MatrixValue x;

            if (X0 == null)
            {
                X0 = new MatrixValue(b.DimensionY, b.DimensionX);
            }
            else if (X0.DimensionX != b.DimensionX || X0.DimensionY != b.DimensionY)
            {
                throw new YAMPDifferentDimensionsException(X0, b);
            }

            H = new MatrixValue(k + 1, k);
            V = new MatrixValue(X0.DimensionY, k);
            c = new MatrixValue(k - 1, 1);
            s = new MatrixValue(k - 1, 1);
            gamma = new MatrixValue(k + 1, 1);
            var converged = false;

            do
            {
                var j = 0;
                x = X0.Clone();
                var r0 = b - A * x;
                var beta = r0.Abs().Re;

                H.Clear();
                V.Clear();
                gamma.Clear();

                gamma[1] = new ScalarValue(beta);
                c.Clear();
                s.Clear();

                V.SetColumnVector(1, r0 / gamma[1]);

                if (beta < Tolerance)
                {
                    break;
                }

                do
                {
                    j++;
                    i++;

                    var Avj = A * V.GetColumnVector(j);
                    var sum = new MatrixValue(Avj.DimensionY, Avj.DimensionX);

                    for (var m = 1; m <= j; m++)
                    {
                        var w = V.GetColumnVector(m);
                        H[m, j] = w.ComplexDot(Avj);
                        sum += H[m, j] * w;
                    }

                    var wj = Avj - sum;
                    H[j + 1, j] = wj.Abs();
                    Rotate(j);

                    if (H[j + 1, j].AbsSquare() == 0.0)
                    {
                        converged = true;
                        break;
                    }

                    V.SetColumnVector(j + 1, wj / H[j + 1, j]);
                    beta = gamma[j + 1].Abs();

                    if (beta < Tolerance)
                    {
                        converged = true;
                        break;
                    }
                }
                while (j < k);

                var y = new MatrixValue(j, 1);

                for (int l = j; l >= 1; l--)
                {
                    var sum = ScalarValue.Zero;

                    for (var m = l + 1; m <= j; m++)
                    {
                        sum += H[l, m] * y[m];
                    }

                    y[l] = (gamma[l] - sum) / H[l, l];
                }

                for (var l = 1; l <= j; l++)
                {
                    x += y[l] * V.GetColumnVector(l);
                }

                if (converged)
                {
                    break;
                }

                X0 = x;
            }
            while (i < MaxIterations);

            return x;
        }