コード例 #1
0
		public void RightNestedComponentsAreInlined()
		{
			// x*(y*z) = x*y*z
			var innerComponent = new DualFactorComponent(new AlphabeticFactor("y"), new AlphabeticFactor("z"), true);
			var outerComponent = new DualFactorComponent(new AlphabeticFactor("x"), Factorizer.ToFactor(innerComponent), true);
			var componentList = new ComponentList(outerComponent);

			Assert.AreEqual(3, componentList.Factors.Count);
			Assert.IsTrue(componentList.Factors[0].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[0].IsInNumerator);
			Assert.IsTrue(componentList.Factors[1].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[1].IsInNumerator);
			Assert.IsTrue(componentList.Factors[2].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[2].IsInNumerator);

			Assert.AreEqual("x*y*z", componentList.ToString());
		}
コード例 #2
0
		public void DoubleNestedComponentsAreInlined()
		{
			// (w*x)*(y*z) = w*x*y*z
			var leftInnerComponent = new DualFactorComponent(new AlphabeticFactor("w"), new AlphabeticFactor("x"), true);
			var rightInnerComponent = new DualFactorComponent(new AlphabeticFactor("y"), new AlphabeticFactor("z"), true);
			var outerComponent = new DualFactorComponent(Factorizer.ToFactor(leftInnerComponent), Factorizer.ToFactor(rightInnerComponent), true);
			var componentList = new ComponentList(outerComponent);

			Assert.AreEqual(4, componentList.Factors.Count);
			Assert.IsTrue(componentList.Factors[0].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[0].IsInNumerator);
			Assert.IsTrue(componentList.Factors[1].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[1].IsInNumerator);
			Assert.IsTrue(componentList.Factors[2].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[2].IsInNumerator);
			Assert.IsTrue(componentList.Factors[3].Factor is AlphabeticFactor);
			Assert.IsTrue(componentList.Factors[3].IsInNumerator);

			Assert.AreEqual("w*x*y*z", componentList.ToString());
		}
コード例 #3
0
		private static Expression ToExpression(ComponentList componentList)
		{
			if (componentList.Factors.Count == 1)
			{
				if (componentList.Factors[0].IsInNumerator)
				{
					return ToExpression(componentList.Factors[0].Factor);
				}

				if (componentList.Factors[0].Factor is NumericFactor && !componentList.Factors[0].IsInNumerator)
				{
					var nf = (NumericFactor)componentList.Factors[0].Factor;
					if (nf.Number is Integer)
					{
						return new SingleComponentExpression(new IntegerFraction(new Integer(1), (Integer)nf.Number));
					}
				}
			}

			return new SingleComponentExpression(componentList);
		}
コード例 #4
0
ファイル: PrimeFactor.cs プロジェクト: andrewsarnold/Titanium
		protected override Expression InnerEvaluate(params Expression[] parameters)
		{
			var parameter = parameters[0].Evaluate();
			var numericFactor = Factorizer.ToFactor(parameter) as NumericFactor;
			if (numericFactor == null)
			{
				return parameter;
			}
			if (numericFactor.Number.IsZero)
			{
				return Expressionizer.ToExpression(numericFactor);
			}
			if (numericFactor.Number.IsOne)
			{
				return Expressionizer.ToExpression(new NumericFactor(new Integer(1)));
			}

			var number = Math.Abs(numericFactor.Number.ValueAsFloat());

			// TODO: If number is fractional and greater than 1,
			// convert to an integer fraction and go from there.
			if (number > 1 && number % 1 > 0)
			{
				return Expressionizer.ToExpression(numericFactor);
			}

			var isFractional = false;
			if (number > 0 && number < 1)
			{
				number = 1 / number;
				isFractional = true;
			}

			var primeCounts = Factorize(number);

			var useFloats = numericFactor.Number is Float;
			var unevaluatedExponentFunctions = primeCounts.Select(pc => pc.Value > 1
				? new FunctionComponent(new Exponent(), new List<Expression>
				{
					Expressionizer.ToExpression(new NumericFactor(useFloats ? (Number)new Float(pc.Key) : new Integer(pc.Key))),
					Expressionizer.ToExpression(new NumericFactor(new Integer(pc.Value)))
				})
				: Componentizer.ToComponent(new NumericFactor(useFloats ? (Number)new Float(pc.Key) : new Integer(pc.Key))));
			var componentList = new ComponentList(unevaluatedExponentFunctions.Select(uef => new ComponentListFactor(Factorizer.ToFactor(uef))).ToList());

			if (isFractional)
			{
				foreach (var componentListFactor in componentList.Factors)
				{
					componentListFactor.IsInNumerator = false;
				}

				if (!numericFactor.Number.IsNegative)
				{
					componentList.Factors.Insert(0, new ComponentListFactor(new NumericFactor(new Integer(1))));
				}
			}
			
			if (numericFactor.Number.IsNegative)
			{
				componentList.Factors.Insert(0, new ComponentListFactor(new NumericFactor(useFloats
					? (Number)new Float(-1)
					: new Integer(-1))));
			}

			return Expressionizer.ToExpression(componentList);
		}