本文整理汇总了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);
}
示例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;
}
示例3: CalculateTest
public void CalculateTest()
{
IExpression exp = new Mul(new Number(2), new Number(2));
Assert.AreEqual(4.0, exp.Calculate());
}
示例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);
}
示例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);
}