当前位置: 首页>>代码示例>>C#>>正文


C# Mul.Calculate方法代码示例

本文整理汇总了C#中xFunc.Maths.Expressions.Mul.Calculate方法的典型用法代码示例。如果您正苦于以下问题:C# Mul.Calculate方法的具体用法?C# Mul.Calculate怎么用?C# Mul.Calculate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在xFunc.Maths.Expressions.Mul的用法示例。


在下文中一共展示了Mul.Calculate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MultiOpAddMul

        public void MultiOpAddMul()
        {
            // ({2, 3, 4} + {1, 2, 3}) * 4
            var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) });
            var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) });
            var add = new Add(vector2, vector1);
            var mul = new Mul(add, new Number(4));

            var expected = new Vector(new[] { new Number(12), new Number(20), new Number(28) });
            var result = mul.Calculate();

            Assert.AreEqual(expected, result);
        }
开发者ID:ronnycsharp,项目名称:xFunc,代码行数:13,代码来源:VectorTest.cs

示例2: SimplifyMul

        private IExpression SimplifyMul(Mul mul)
        {
            // mul by zero
            if (mul.Left.Equals(zero) || mul.Right.Equals(zero))
                return zero;

            // mul by 1
            if (mul.Left.Equals(one))
                return mul.Right;
            if (mul.Right.Equals(one))
                return mul.Left;

            if (mul.Left is Number && mul.Right is Number)
                return new Number((double)mul.Calculate());

            // 2 * (2 * x)
            // 2 * (x * 2)
            // (2 * x) * 2
            // (x * 2) * 2
            Mul bracketMul = null;
            Number firstNumber = null;
            if (mul.Left is Mul && mul.Right is Number)
            {
                bracketMul = mul.Left as Mul;
                firstNumber = mul.Right as Number;
            }
            else if (mul.Right is Mul && mul.Left is Number)
            {
                bracketMul = mul.Right as Mul;
                firstNumber = mul.Left as Number;
            }
            if (bracketMul != null)
            {
                if (bracketMul.Left is Number)
                {
                    var secondNumber = bracketMul.Left as Number;
                    var result = new Mul(new Number(firstNumber.Value * secondNumber.Value), bracketMul.Right);

                    return _Simplify(result);
                }
                if (bracketMul.Right is Number)
                {
                    var secondNumber = bracketMul.Right as Number;
                    var result = new Mul(new Number(firstNumber.Value * secondNumber.Value), bracketMul.Left);

                    return _Simplify(result);
                }
            }

            // 2 * (2 / x)
            // 2 * (x / 2)
            // (2 / x) * 2
            // (x / 2) * 2
            Div bracketDiv = null;
            if (mul.Left is Div && mul.Right is Number)
            {
                bracketDiv = mul.Left as Div;
                firstNumber = mul.Right as Number;
            }
            else if (mul.Right is Div && mul.Left is Number)
            {
                bracketDiv = mul.Right as Div;
                firstNumber = mul.Left as Number;
            }
            if (bracketDiv != null)
            {
                if (bracketDiv.Left is Number)
                {
                    var secondNumber = bracketDiv.Left as Number;
                    var result = new Div(new Number(firstNumber.Value * secondNumber.Value), bracketDiv.Right);

                    return _Simplify(result);
                }
                if (bracketDiv.Right is Number)
                {
                    var secondNumber = bracketDiv.Right as Number;
                    var result = new Mul(new Number(firstNumber.Value / secondNumber.Value), bracketDiv.Left);

                    return _Simplify(result);
                }
            }

            return mul;
        }
开发者ID:smwentum,项目名称:xFunc,代码行数:84,代码来源:Simplifier.cs

示例3: CalculateTest

        public void CalculateTest()
        {
            IExpression exp = new Mul(new Number(2), new Number(2));

            Assert.AreEqual(4.0, exp.Calculate());
        }
开发者ID:ronnycsharp,项目名称:xFunc,代码行数:6,代码来源:MultiplicationTest.cs

示例4: MultiOpMulMul

        public void MultiOpMulMul()
        {
            // ({1, 2, 3} * 2) * 4
            var vector = new Vector(new[] { new Number(1), new Number(2), new Number(3) });
            var mul1 = new Mul(vector, new Number(2));
            var mul2 = new Mul(mul1, new Number(4));

            var expected = new Vector(new[] { new Number(8), new Number(16), new Number(24) });
            var result = mul2.Calculate();

            Assert.AreEqual(expected, result);
        }
开发者ID:ronnycsharp,项目名称:xFunc,代码行数:12,代码来源:VectorTest.cs

示例5: MultiOpSubMul

        public void MultiOpSubMul()
        {
            // ({2, 3, 4} - {1, 2, 3}) * 4
            var vector1 = new Vector(new[] { new Number(1), new Number(2), new Number(3) });
            var vector2 = new Vector(new[] { new Number(2), new Number(3), new Number(4) });
            var sub = new Sub(vector2, vector1);
            var mul = new Mul(sub, new Number(4));

            var expected = new Vector(new[] { new Number(4), new Number(4), new Number(4) });
            var result = mul.Calculate();

            Assert.AreEqual(expected, result);
        }
开发者ID:ronnycsharp,项目名称:xFunc,代码行数:13,代码来源:VectorTest.cs


注:本文中的xFunc.Maths.Expressions.Mul.Calculate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。