本文整理匯總了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);
}