public void AutoSimplify_RationalNumberExpression() { // A Signal a = StdBuilder.Add( StdBuilder.Divide(IntegerValue.Constant(2), IntegerValue.Constant(3)), RationalValue.Constant(3, 4)); Assert.AreEqual("2/3+3/4", _f.Format(a, FormattingOptions.Compact), "A1"); Signal aS = Std.AutoSimplify(a); Assert.AreEqual("17/12", _f.Format(aS, FormattingOptions.Compact), "A2"); // B Signal b = StdBuilder.Power( StdBuilder.Divide(IntegerValue.Constant(4), IntegerValue.Constant(2)), IntegerValue.Constant(3)); Assert.AreEqual("(4/2)^3", _f.Format(b, FormattingOptions.Compact), "B1"); Signal bS = Std.AutoSimplify(b); Assert.AreEqual("8", _f.Format(bS, FormattingOptions.Compact), "B2"); // C Signal c = StdBuilder.Divide( IntegerValue.ConstantOne, StdBuilder.Subtract( RationalValue.Constant(2, 4), RationalValue.ConstantHalf)); Assert.AreEqual("1/(1/2-1/2)", _f.Format(c, FormattingOptions.Compact), "C1"); Signal cS = Std.AutoSimplify(c); Assert.AreEqual("Std.Undefined", _f.Format(cS, FormattingOptions.Compact), "C2"); // D Signal d = StdBuilder.Power( StdBuilder.Power( IntegerValue.Constant(5), IntegerValue.Constant(2)), StdBuilder.Power( IntegerValue.Constant(3), IntegerValue.Constant(1))); Assert.AreEqual("(5^2)^(3^1)", _f.Format(d, FormattingOptions.Compact), "D1"); Signal dS = Std.AutoSimplify(d); Assert.AreEqual("15625", _f.Format(dS, FormattingOptions.Compact), "D2"); }
public static void RegisterTheorems(ILibrary library) { //Analysis.DerivativeTransformation.Provider.Add( // new Analysis.DerivativeTransformation(_entityId, // delegate(Port port, Signal[] derivedInputSignals) // { // Signal innerA = derivedInputSignals[1] * StdBuilder.NaturalLogarithm(port.InputSignals[0]); // Signal innerB = (port.InputSignals[1] * derivedInputSignals[0]) / port.InputSignals[0]; // return port.OutputSignals[0] * (innerA + innerB); // })); Algebra.AutoSimplifyTransformation.Provider.Add( new Algebra.AutoSimplifyTransformation(_entityId, delegate(Port port) { // TODO return(ManipulationPlan.DoAlter); }, delegate(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs) { if (SimplifyOperands(manipulatedInputs) || hasManipulatedInputs) { if (manipulatedInputs.Count == 0) { return(new SignalSet(IntegerValue.ConstantMultiplicativeIdentity)); } if (manipulatedInputs.Count == 1) { return(manipulatedInputs); } if (Std.IsConstantMultiplicativeIdentity(manipulatedInputs[0])) { return(new SignalSet(manipulatedInputs[0])); } if (Std.IsConstantMultiplicativeIdentity(manipulatedInputs[1])) { return(new SignalSet(manipulatedInputs[0])); } if (Std.IsConstantAdditiveIdentity(manipulatedInputs[1])) { return(new SignalSet(IntegerValue.ConstantOne)); } return(new SignalSet(StdBuilder.Power(manipulatedInputs))); } else { return(port.OutputSignals); } })); }
public void Pattern_Conditions() { Project p = new Project(); MathSystem s = p.CurrentSystem; // sin(x^2) Signal x = Binder.CreateSignal(); x.Label = "x"; Std.ConstrainAlwaysReal(x); Signal x2 = StdBuilder.Power(x, IntegerValue.ConstantTwo); x2.Label = "x2"; Signal sinx2 = StdBuilder.Sine(x2); sinx2.Label = "sinx2"; AlwaysTrueCondition ctrue = AlwaysTrueCondition.Instance; Assert.AreEqual(true, ctrue.FulfillsCondition(x, x.DrivenByPort), "A01"); EntityCondition centity = new EntityCondition(new MathIdentifier("Sine", "Std")); Assert.AreEqual(true, centity.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A02"); Assert.AreEqual(false, centity.FulfillsCondition(x2, x2.DrivenByPort), "A03"); //InputSignalsPropertyCondition cinputconst = new InputSignalsPropertyCondition(new MathIdentifier("Constant", "Std"), CombinationMode.AtLeastOne); InputSignalsFlagCondition cinputconst = new InputSignalsFlagCondition(StdAspect.ConstantFlag, FlagState.Enabled, CombinationMode.AtLeastOne); Assert.AreEqual(true, cinputconst.FulfillsCondition(x2, x2.DrivenByPort), "A04"); Assert.AreEqual(false, cinputconst.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A05"); OrCondition cor = new OrCondition(centity, cinputconst); Assert.AreEqual(true, cor.FulfillsCondition(x2, x2.DrivenByPort), "A06"); Assert.AreEqual(true, cor.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A07"); Assert.AreEqual(false, cor.FulfillsCondition(x, x.DrivenByPort), "A08"); AndCondition cand = new AndCondition(ctrue, centity); Assert.AreEqual(true, cand.FulfillsCondition(sinx2, sinx2.DrivenByPort), "A09"); Assert.AreEqual(false, cand.FulfillsCondition(x2, x2.DrivenByPort), "A10"); }
public void StdPolynomial_SingleVariable() { Signal x = Binder.CreateSignal(); Signal a0 = IntegerValue.ConstantZero; Signal a1 = IntegerValue.ConstantOne; Signal a2 = RationalValue.ConstantHalf; Signal a3 = IntegerValue.Constant(2); Signal a4 = IntegerValue.Constant(3); Signal badX = StdBuilder.Sine(x); Signal badA2 = StdBuilder.Tangent(a2); Signal badA3 = RealValue.ConstantPI; Signal x2 = StdBuilder.Power(x, a3); Signal x3 = StdBuilder.Power(x, a4); Signal a3x3 = a3 * x3; Signal a2x2 = a2 * x2; Assert.IsTrue(Polynomial.IsMonomial(x, x), "x: is SVM(x)"); Assert.IsTrue(Polynomial.IsMonomial(a0, x), "0: is SVM(x)"); Assert.IsTrue(Polynomial.IsMonomial(a2, x), "1/2: is SVM(x)"); Assert.IsFalse(Polynomial.IsMonomial(badX, x), "sin(x): is not SVM(x)"); Assert.IsFalse(Polynomial.IsMonomial(badA2, x), "tan(1/2): is not SVM(x)"); Assert.IsFalse(Polynomial.IsMonomial(badA3, x), "pi is not SVM(x)"); Assert.IsTrue(Polynomial.IsMonomial(x3, x), "x^3: is SVM(x)"); Assert.IsTrue(Polynomial.IsMonomial(a2x2, x), "1/2*x^2: is SVM(x)"); Assert.AreEqual("Std.Integer(1)", Polynomial.MonomialDegree(x, x).ToString(), "x: SVM deg(x)=1"); Assert.AreEqual("Std.Integer(0)", Polynomial.MonomialDegree(a2, x).ToString(), "1/2: SVM deg(x)=0"); Assert.AreEqual("Std.NegativeInfinity", Polynomial.MonomialDegree(a0, x).ToString(), "0: SVM deg(x)=-inf"); Assert.AreEqual("Std.Integer(3)", Polynomial.MonomialDegree(x3, x).ToString(), "x^3: SVM deg(x)=3"); Assert.AreEqual("Std.Integer(2)", Polynomial.MonomialDegree(a2x2, x).ToString(), "1/2*x^2: SVM deg(x)=2"); IValueStructure vs; Signal test = Polynomial.MonomialCoefficient(x, x, out vs); Assert.AreEqual("Std.Integer(1)", vs.ToString(), "x: SVM coeff deg=1 "); Assert.IsTrue(test.Value.Equals(a1.Value), "x: SVM coeff = 1"); test = Polynomial.MonomialCoefficient(a2, x, out vs); Assert.AreEqual("Std.Integer(0)", vs.ToString(), "1/2: SVM coeff deg=0 "); Assert.IsTrue(test.Value.Equals(a2.Value), "1/2: SVM coeff = 1/2"); test = Polynomial.MonomialCoefficient(a3x3, x, out vs); Assert.AreEqual("Std.Integer(3)", vs.ToString(), "2*x^3: SVM coeff deg=3 "); Assert.IsTrue(test.Value.Equals(a3.Value), "2*x^3: SVM coeff = 2"); Signal a3x3_a2x2_a4 = StdBuilder.Add(a3x3, a2x2, a4); Assert.IsFalse(Polynomial.IsMonomial(a3x3_a2x2_a4, x), "2*x^3+1/2*x^2+3: is not SVM(x)"); Assert.IsTrue(Polynomial.IsPolynomial(a3x3_a2x2_a4, x), "2*x^3+1/2*x^2+3: is SVP(x)"); Assert.AreEqual("Std.Integer(3)", Polynomial.PolynomialDegree(a3x3_a2x2_a4, x).ToString(), "2*x^3+1/2*x^2+3: SVP deg(x)=3"); test = Polynomial.PolynomialCoefficient(a3x3_a2x2_a4, x, 1); Assert.IsTrue(test.Value.Equals(a0.Value), "2*x^3+1/2*x^2+3: SVP coeff(1) = 0"); test = Polynomial.PolynomialCoefficient(a3x3_a2x2_a4, x, 2); Assert.IsTrue(test.Value.Equals(a2.Value), "2*x^3+1/2*x^2+3: SVP coeff(2) = 1/2"); Signal[] coefficients = Polynomial.PolynomialCoefficients(a3x3_a2x2_a4, x); Assert.AreEqual(4, coefficients.Length, "2*x^3+1/2*x^2+3: SVP coeffs: len(coeffs) = 4 (-> deg=3)"); Assert.IsTrue(coefficients[0].Value.Equals(a4.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[0] = 3"); Assert.IsTrue(coefficients[1].Value.Equals(a0.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[1] = 0"); Assert.IsTrue(coefficients[2].Value.Equals(a2.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[2] = 1/2"); Assert.IsTrue(coefficients[3].Value.Equals(a3.Value), "2*x^3+1/2*x^2+3: SVP coeffs: coeffs[3] = 2"); }