예제 #1
0
        // Perform a left Reimann sum
        public static double ReimannSum(Fxy f, double x, double x0 = 0, double y0 = 0, double dx = 1e-6)
        {
            if (dx <= 0)
            {
                throw new ArgumentOutOfRangeException("dx", "dx must be greater than zero.");
            }
            if (x0 > x)
            {
                throw new ArgumentOutOfRangeException("x0, x", "x0 must be less than x.");
            }

            double intervalSize = Abs(x - x0);
            uint   n            = checked ((uint)(intervalSize / dx));
            double sum          = y0;

            for (uint i = 0; i < n; i++)
            {
                sum += f(x0 + i * dx, sum) * dx;
            }

            // Sum the last term that will be left over if the interval isn't divisible by the increment
            double summedIntervalSize = n * dx;

            if (summedIntervalSize < intervalSize)
            {
                double lastDx = intervalSize - summedIntervalSize;
                sum += f(x - lastDx, sum) * lastDx;
            }

            return(sum);
        }
예제 #2
0
파일: OPs.cs 프로젝트: WVitek/DotGlue
        public static async Task <object> binaryOpAsync(AsyncExprCtx ae,
                                                        Fxy op, object a, object b, bool resultCanBeLazy)
        {
            var valueA = await ConstValueOf(ae, a);

            var valueB = await ConstValueOf(ae, b);

            return(binaryOpConst(op, valueA, valueB, resultCanBeLazy));
        }
예제 #3
0
파일: OPs.cs 프로젝트: WVitek/DotGlue
        public static object GenFxyCall(Fxy op, object a, object b, ValueKind kind, bool resultCanBeLazy)
        {
            switch (kind)
            {
            case ValueKind.Const:
                return(binaryOpConst(op, a, b, resultCanBeLazy));

            case ValueKind.Sync:
                return((LazySync) delegate() { return binaryOpSync(op, a, b, resultCanBeLazy); });

            case ValueKind.Async:
                return((LazyAsync) delegate(AsyncExprCtx ae)
                       { return binaryOpAsync(ae, op, a, b, resultCanBeLazy); });
            }
            throw new InvalidOperationException();
        }
예제 #4
0
        // Perform the left ReimannSum until the target value is reached
        public static double ReimannSumTo(
            Fxy f, double y, double x = 1000, double x0 = 0, double y0 = 0, double dx = 1e-6)
        {
            if (dx <= 0)
            {
                throw new ArgumentOutOfRangeException("dx", "dx must be greater than zero.");
            }
            if (x0 > x)
            {
                throw new ArgumentOutOfRangeException("x0, x", "x0 must be less than x.");
            }

            if (y == y0)
            {
                return(x0);
            }

            double intervalSize = Abs(x - x0);
            uint   n            = checked ((uint)(intervalSize / dx));
            double sum          = y0;
            // We can be approaching the target value from above or below depending on the initial value
            IsSumReached isSumReached = y > y0 ? (IsSumReached)(() => sum >= y) : () => sum <= y;

            for (uint i = 0; i < n; i++)
            {
                sum += f(x0 + i * dx, sum) * dx;
                if (isSumReached())
                {
                    return(x0 + (i + 1) * dx);
                }
            }

            // Sum the last term that will be left over if the interval isn't divisible by the increment
            double summedIntervalSize = n * dx;

            if (summedIntervalSize < intervalSize)
            {
                double lastDx = intervalSize - summedIntervalSize;
                sum += f(x - lastDx, sum) * lastDx;
                if (isSumReached())
                {
                    return(x);
                }
            }

            return(double.NaN);
        }
예제 #5
0
파일: Expr.cs 프로젝트: WVitek/DotGlue
 public CallExpr(Fxy func, params Expr[] args) : this((Delegate)func, args)
 {
 }
예제 #6
0
파일: Expr.cs 프로젝트: WVitek/DotGlue
 public CallExpr(Fxy func, IList <Expr> args) : this((Delegate)func, args)
 {
 }
예제 #7
0
파일: OPs.cs 프로젝트: WVitek/DotGlue
 public static object binaryOpSync(Fxy op, object a, object b, bool resultCanBeLazy)
 {
     return(binaryOpConst(op, ConstValueOf(a), ConstValueOf(b), resultCanBeLazy));
 }
예제 #8
0
파일: OPs.cs 프로젝트: WVitek/DotGlue
        public static object binaryOpConst(Fxy op, object constA, object constB, bool resultCanBeLazy)
        {
            IList arrA = constA as IList;
            IList arrB = constB as IList;

            if (arrA == null && arrB == null)
            {
                return(op(constA, constB));
            }
            try
            {
                ArrayList result;
                if (arrA == null)      // || arrA.Count == 1)
                {
                    object a = constA; // arrA == null ? argA : arrA[0];
                    if (resultCanBeLazy)
                    {
                        result = new ListOfVals(arrB.Count);
                    }
                    else
                    {
                        result = new ListOfConst(arrB.Count);
                    }
                    for (int i = 0; i < arrB.Count; i++)
                    {
                        result.Add(op(a, arrB[i]));
                    }
                }
                else if (arrB == null) // || arrB.Count == 1)
                {
                    object b = constB; // arrB == null ? argB : arrB[0];
                    if (resultCanBeLazy)
                    {
                        result = new ListOfVals(arrA.Count);
                    }
                    else
                    {
                        result = new ListOfConst(arrA.Count);
                    }
                    for (int i = 0; i < arrA.Count; i++)
                    {
                        result.Add(op(arrA[i], b));
                    }
                }
                else if (arrA.Count == arrB.Count)
                {
                    int n = arrA.Count;
                    if (resultCanBeLazy)
                    {
                        result = new ListOfVals(n);
                    }
                    else
                    {
                        result = new ListOfConst(n);
                    }
                    for (int i = 0; i < n; i++)
                    {
                        result.Add(op(arrA[i], arrB[i]));
                    }
                }
                else
                {
                    return(new ArgumentOutOfRangeException("binaryOp: arguments dimensions mismatch").Wrap());
                }
                return(result);
            }
            catch (Exception ex) { return(ex.Wrap()); }// new Exception(string.Format("binaryOp: " + ex.Message)); }
        }