Perform() public method

Invokes the given function value.
public Perform ( ParseContext context, Value argument ) : Value
context ParseContext The context of the invocation.
argument Value The argument(s) to use for the invocation.
return Value
        public FunctionValue Function(MatrixValue x, MatrixValue y, FunctionValue f)
        {
            var context = Context;

            if (x.Length != y.Length)
            {
                throw new YAMPDifferentLengthsException(x.Length, y.Length);
            }

            var _fx = f.Perform(context, x[1]);

            if (_fx is MatrixValue == false)
            {
                throw new YAMPArgumentInvalidException("Linfit", "f", 3);
            }

            var fx = _fx as MatrixValue;

            var m = fx.Length;

            if (m < 2)
            {
                throw new YAMPArgumentInvalidException("Linfit", "f", 3);
            }

            var M = new MatrixValue(x.Length, m);

            for (var j = 1; j <= M.Rows; j++)
            {
                if (j > 1)
                {
                    fx = f.Perform(context, x[j]) as MatrixValue;
                }

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

            var p = Function(x, y, M);

            return(new FunctionValue((parseContext, variable) => ((f.Perform(parseContext, variable) as MatrixValue) * p)[1], true));
        }
Exemplo n.º 2
0
        public FunctionValue Function(MatrixValue x, MatrixValue y, FunctionValue f)
        {
            var context = Context;

            if (x.Length != y.Length)
                throw new YAMPDifferentLengthsException(x.Length, y.Length);

            var _fx = f.Perform(context, x[1]);

            if (_fx is MatrixValue == false)
                throw new YAMPArgumentInvalidException("Linfit", "f", 3);

            var fx = _fx as MatrixValue;

            var m = fx.Length;

            if (m < 2)
                throw new YAMPArgumentInvalidException("Linfit", "f", 3);

            var M = new MatrixValue(x.Length, m);

            for (var j = 1; j <= M.Rows; j++)
            {
                if (j > 1)
                {
                    fx = f.Perform(context, x[j]) as MatrixValue;
                }

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

            var p = Function(x, y, M);
            return new FunctionValue((parseContext, variable) => ((f.Perform(parseContext, variable) as MatrixValue) * p)[1], true);
        }
Exemplo n.º 3
0
        public ScalarValue Function(FunctionValue f, ScalarValue x)
        {
            var lambda = new Func<Double, Double>(t =>
            {
                var sv = f.Perform(Context, new ScalarValue(t));

                if (sv is ScalarValue == false)
                {
                    throw new YAMPArgumentInvalidException(Name, sv.Header, 1);
                }

                return ((ScalarValue)sv).Re;
            });

            var newton = new NewtonMethod(lambda, x.Re, 0.00001);
            return new ScalarValue(newton.Result[0, 0]);
        }
Exemplo n.º 4
0
        public ScalarValue Function(FunctionValue f, ScalarValue x)
        {
            Func <double, double> lambda = t =>
            {
                var sv = f.Perform(Context, new ScalarValue(t));

                if (!(sv is ScalarValue))
                {
                    throw new YAMPArgumentInvalidException(Name, sv.Header, 1);
                }

                return(((ScalarValue)sv).Re);
            };

            var newton = new NewtonMethod(lambda, x.Re, 0.00001);

            return(new ScalarValue(newton.Result[0, 0]));
        }
Exemplo n.º 5
0
        public MatrixValue Function(FunctionValue deq, MatrixValue points, ScalarValue x0)
        {
            Func <double, double, double> lambda = (t, x) =>
            {
                var av = new ArgumentsValue(new ScalarValue(t), new ScalarValue(x));
                var sv = deq.Perform(Context, av);

                if (!(sv is ScalarValue))
                {
                    throw new YAMPArgumentInvalidException(Name, sv.Header, 1);
                }

                return(((ScalarValue)sv).Re);
            };

            var euler = new RungeKutta(lambda, points[1].Re, points[points.Length].Re, x0.Re, points.Length - 1);

            return(new MatrixValue(euler.Result));
        }
        public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P)
        {
            var numberOfBootstrapSamples = n.GetIntegerOrThrowException("n", Name);
            var nConfigs     = cfgs.DimensionY;
            var nData        = cfgs.DimensionX;
            var distribution = new DiscreteUniformDistribution(Rng)
            {
                Beta  = nConfigs,
                Alpha = 1
            };

            if (numberOfBootstrapSamples <= 1)
            {
                throw new YAMPException("Bootstrap: The number of bootstrap samples n is smaller or equal to 1!");
            }

            var parameters = new ArgumentsValue(cfgs);

            foreach (var m in P.Values)
            {
                parameters.Insert(m);
            }

            var temp    = f.Perform(Context, parameters);
            var nResult = 0;//dimension of the result

            if (temp is ScalarValue)
            {
                nResult = 1;
            }
            else if (temp is MatrixValue)
            {
                nResult = ((MatrixValue)temp).Length;
            }
            else
            {
                throw new YAMPException("Bootstrap: The observable f has to return either a scalar or a matrix!");
            }

            var BootstrapObservable = new MatrixValue(numberOfBootstrapSamples, nResult);

            for (var i = 1; i <= numberOfBootstrapSamples; i++)
            {
                var BootstrapConfigs = new MatrixValue(nConfigs, nData);

                for (var j = 1; j <= nConfigs; j++)
                {
                    var idx = distribution.Next();

                    for (var k = 1; k <= nData; k++)
                    {
                        BootstrapConfigs[j, k] = cfgs[idx, k];
                    }
                }

                parameters = new ArgumentsValue(BootstrapConfigs);

                foreach (var m in P.Values)
                {
                    parameters.Insert(m);
                }

                temp = f.Perform(Context, parameters);

                if (temp is ScalarValue)
                {
                    BootstrapObservable[i] = (ScalarValue)temp;
                }
                else
                {
                    var m = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        BootstrapObservable[i, k] = m[k];
                    }
                }
            }

            temp = YMath.Average(BootstrapObservable);

            for (var i = 1; i <= numberOfBootstrapSamples; i++)
            {
                if (temp is ScalarValue)
                {
                    BootstrapObservable[i] -= temp as ScalarValue;
                    BootstrapObservable[i] *= BootstrapObservable[i];
                }
                else
                {
                    var T = temp as MatrixValue;

                    for (var k = 1; k <= nResult; k++)
                    {
                        BootstrapObservable[i, k] -= T[k];
                        BootstrapObservable[i, k] *= BootstrapObservable[i, k];
                    }
                }
            }

            var error = YMath.Average(BootstrapObservable);
            var sqrt  = new SqrtFunction();

            error = sqrt.Perform(error);
            var result = new MatrixValue(2, nResult);

            if (temp is ScalarValue)
            {
                result[1] = (ScalarValue)temp;
                result[2] = (ScalarValue)error;
            }
            else
            {
                var T = (MatrixValue)temp;
                var E = (MatrixValue)error;

                for (var k = 1; k <= nResult; k++)
                {
                    result[1, k] = T[k];
                    result[2, k] = E[k];
                }
            }

            return(result);
        }
Exemplo n.º 7
0
        public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P)
        {
            var numberOfBlocks = n.GetIntegerOrThrowException("n", Name);
            var nConfigs       = cfgs.DimensionY;
            var nData          = cfgs.DimensionX;

            if (numberOfBlocks > nConfigs)
            {
                throw new YAMPException("Jackknife: The number of measurements n is greater than the number of configurations cfgs!");
            }

            if (numberOfBlocks <= 1)
            {
                throw new YAMPException("Jackknife: The number of measurements n <= 1!");
            }

            var parameters = new ArgumentsValue(cfgs);

            foreach (var m in P.Values)
            {
                parameters.Insert(m);
            }

            var temp = f.Perform(Context, parameters);
            int nResult;//dimension of the result

            if (temp is ScalarValue)
            {
                nResult = 1;
            }
            else if (temp is MatrixValue)
            {
                nResult = ((MatrixValue)temp).Length;
            }
            else
            {
                throw new YAMPException("Jackknife: Observable f has to return either a scalar or a matrix!");
            }

            var JackknifeObservable = new MatrixValue(numberOfBlocks, nResult);
            var BlockSize           = nConfigs / numberOfBlocks;
            var nConfigsBlocked     = BlockSize * numberOfBlocks;
            var residualConfigs     = nConfigs - nConfigsBlocked;

            for (var i = 1; i <= numberOfBlocks; i++)
            {
                if (i <= numberOfBlocks - residualConfigs)
                {
                    //the first (NumberOfBlocks - residualConfigs) blocks discard (BlockSize) elements ...
                    var JackknifeConfigs = new MatrixValue(nConfigs - BlockSize, nData);
                    var j = 1;

                    for (; j <= (i - 1) * BlockSize; j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j, k] = cfgs[j, k];
                        }
                    }

                    j += BlockSize;

                    for (; j <= nConfigs; j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j - BlockSize, k] = cfgs[j, k];
                        }
                    }

                    parameters = new ArgumentsValue(JackknifeConfigs);
                }
                else
                {
                    //... whereas the residual (residualConfigs) blocks discard (BlockSize + 1) elements
                    var JackknifeConfigs = new MatrixValue(nConfigs - BlockSize - 1, nData);
                    var j = 1;

                    for (; j <= nConfigs - (numberOfBlocks - (i - 1)) * (BlockSize + 1); j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j, k] = cfgs[j, k];
                        }
                    }

                    j += BlockSize + 1;

                    for (; j <= nConfigs; j++)
                    {
                        for (var k = 1; k <= nData; k++)
                        {
                            JackknifeConfigs[j - BlockSize - 1, k] = cfgs[j, k];
                        }
                    }

                    parameters = new ArgumentsValue(JackknifeConfigs);
                }

                foreach (var m in P.Values)
                {
                    parameters.Insert(m);
                }

                temp = f.Perform(Context, parameters);

                if (temp is ScalarValue)
                {
                    JackknifeObservable[i] = (ScalarValue)temp;
                }
                else
                {
                    var T = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        JackknifeObservable[i, k] = T[k];
                    }
                }
            }

            temp = YMath.Average(JackknifeObservable);

            for (var i = 1; i <= numberOfBlocks; i++)
            {
                if (temp is ScalarValue)
                {
                    JackknifeObservable[i] -= temp as ScalarValue;
                    JackknifeObservable[i] *= JackknifeObservable[i];
                }
                else
                {
                    var m = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        JackknifeObservable[i, k] -= m[k];
                        JackknifeObservable[i, k] *= JackknifeObservable[i, k];
                    }
                }
            }

            var error = YMath.Average(JackknifeObservable);
            var scale = numberOfBlocks - 1.0;

            if (error is ScalarValue)
            {
                error = ((ScalarValue)error) * scale;
            }
            else
            {
                var e = (MatrixValue)error;

                for (var i = 1; i <= e.DimensionY; i++)
                {
                    for (var j = 1; j <= e.DimensionX; j++)
                    {
                        e[i, j] *= scale;
                    }
                }
            }

            var sqrt = new SqrtFunction();

            error = sqrt.Perform(error);
            var result = new MatrixValue(2, nResult);

            if (temp is ScalarValue)
            {
                result[1] = (ScalarValue)temp;
                result[2] = (ScalarValue)error;
            }
            else
            {
                var T = (MatrixValue)temp;
                var E = (MatrixValue)error;

                for (var k = 1; k <= nResult; k++)
                {
                    result[1, k] = T[k];
                    result[2, k] = E[k];
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        public MatrixValue Function(MatrixValue cfgs, ScalarValue n, FunctionValue f, ArgumentsValue P)
        {
            var numberOfBootstrapSamples = n.GetIntegerOrThrowException("n", Name);
            var nConfigs = cfgs.DimensionY;
            var nData = cfgs.DimensionX;
            var distribution = new DiscreteUniformDistribution(Rng)
            {
                Beta = nConfigs,
                Alpha = 1
            };

            if (numberOfBootstrapSamples <= 1)
                throw new YAMPException("Bootstrap: The number of bootstrap samples n is smaller or equal to 1!");

            var parameters = new ArgumentsValue(cfgs);

            foreach (var m in P.Values)
            {
                parameters.Insert(m);
            }

            var temp = f.Perform(Context, parameters);
            var nResult = 0;//dimension of the result

            if (temp is ScalarValue)
            {
                nResult = 1;
            }
            else if (temp is MatrixValue)
            {
                nResult = ((MatrixValue)temp).Length;
            }
            else
            {
                throw new YAMPException("Bootstrap: The observable f has to return either a scalar or a matrix!");
            }

            var BootstrapObservable = new MatrixValue(numberOfBootstrapSamples, nResult);

            for (var i = 1; i <= numberOfBootstrapSamples; i++)
            {
                var BootstrapConfigs = new MatrixValue(nConfigs, nData);

                for (var j = 1; j <= nConfigs; j++)
                {
                    var idx = distribution.Next();

                    for (var k = 1; k <= nData; k++)
                    {
                        BootstrapConfigs[j, k] = cfgs[idx, k];
                    }
                }

                parameters = new ArgumentsValue(BootstrapConfigs);

                foreach (var m in P.Values)
                {
                    parameters.Insert(m);
                }

                temp = f.Perform(Context, parameters);

                if (temp is ScalarValue)
                {
                    BootstrapObservable[i] = (ScalarValue)temp;
                }
                else
                {
                    var m = (MatrixValue)temp;

                    for (var k = 1; k <= nResult; k++)
                    {
                        BootstrapObservable[i, k] = m[k];
                    }
                }
            }

            temp = YMath.Average(BootstrapObservable);

            for (var i = 1; i <= numberOfBootstrapSamples; i++)
            {
                if (temp is ScalarValue)
                {
                    BootstrapObservable[i] -= temp as ScalarValue;
                    BootstrapObservable[i] *= BootstrapObservable[i];
                }
                else
                {
                    var T = temp as MatrixValue;

                    for (var k = 1; k <= nResult; k++)
                    {
                        BootstrapObservable[i, k] -= T[k];
                        BootstrapObservable[i, k] *= BootstrapObservable[i, k];
                    }
                }
            }

            var error = YMath.Average(BootstrapObservable);
            var sqrt = new SqrtFunction();
            error = sqrt.Perform(error);
            var result = new MatrixValue(2, nResult);

            if (temp is ScalarValue)
            {
                result[1] = (ScalarValue)temp;
                result[2] = (ScalarValue)error;
            }
            else
            {
                var T = (MatrixValue)temp;
                var E = (MatrixValue)error;

                for (var k = 1; k <= nResult; k++)
                {
                    result[1, k] = T[k];
                    result[2, k] = E[k];
                }
            }

            return result;
        }