public static void RegisterTheorems(ILibrary library) { Analysis.DerivativeTransformation.Provider.Add( new Analysis.DerivativeTransformation(_entityId, delegate(Port port, SignalSet manipulatedInputs, Signal variable, bool hasManipulatedInputs) { int cnt = manipulatedInputs.Count - 1; Signal[] multiplySignals = new Signal[cnt]; Signal[] addSignals = new Signal[cnt]; for (int i = 0; i < cnt; i++) { multiplySignals[i] = port.InputSignals[i + 1]; } Signal left = Std.Divide(manipulatedInputs[0], multiplySignals); for (int i = 0; i < cnt; i++) { for (int j = 0; j < cnt; j++) { multiplySignals[j] = i == j ? StdBuilder.Square(port.InputSignals[j + 1]) : port.InputSignals[j + 1]; } Signal num = port.InputSignals[0] * manipulatedInputs[i + 1]; addSignals[i] = Std.Divide(num, multiplySignals); } return(new SignalSet(Std.Subtract(left, addSignals))); })); Algebra.AutoSimplifyTransformation.Provider.Add( new Algebra.AutoSimplifyTransformation(_entityId, delegate(Port port) { // TODO return(ManipulationPlan.DoAlter); }, delegate(Port port, SignalSet manipulatedInputs, bool hasManipulatedInputs) { if (SimplifyFactorsForceMultiplication(manipulatedInputs) || hasManipulatedInputs) { if (manipulatedInputs.Count == 0) { return(new SignalSet(IntegerValue.ConstantMultiplicativeIdentity)); } if (manipulatedInputs.Count == 1) { return(manipulatedInputs); } return(new SignalSet(StdBuilder.Divide(manipulatedInputs))); } else { return(port.OutputSignals); } })); }
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 void Scheduler_PortTransportTest() { Signal a = Binder.CreateSignal(); Signal b = Binder.CreateSignal(); Signal c = StdBuilder.Divide(a, b); Signal d = Binder.CreateSignal(); Signal e = Binder.CreateSignal(); Signal f = Service <IBuilder> .Instance.Function("xor", d, e); a.PostNewValue(new RationalValue(1, 1)); b.PostNewValue(new IntegerValue(2)); d.PostNewValue(LogicValue.True); e.PostNewValue(LogicValue.False); Assert.IsNull(a.Value); Assert.IsNull(b.Value); Assert.IsNull(c.Value); Assert.IsNull(d.Value); Assert.IsNull(e.Value); Assert.IsNull(f.Value); Assert.AreEqual(TimeSpan.Zero, project.SimulateFor(1)); Assert.IsNotNull(a.Value); Assert.IsNotNull(b.Value); Assert.IsNull(c.Value); Assert.IsNotNull(d.Value); Assert.IsNotNull(e.Value); Assert.IsNull(f.Value); Assert.AreEqual(TimeSpan.Zero, project.SimulateFor(1)); Assert.IsNotNull(c.Value); Assert.IsInstanceOfType(typeof(RationalValue), c.Value); Assert.IsNotNull(f.Value); Assert.IsInstanceOfType(typeof(LogicValue), f.Value); Assert.AreEqual(1, ((RationalValue)c.Value).NumeratorValue); Assert.AreEqual(2, ((RationalValue)c.Value).DenominatorValue); Assert.AreEqual(ELogicX01.True, ((LogicValue)f.Value).Value); a.PostNewValue(new IntegerValue(3), new TimeSpan(0, 1, 0)); Assert.AreEqual(1, ((RationalValue)c.Value).NumeratorValue); Assert.AreEqual(2, ((RationalValue)c.Value).DenominatorValue); Assert.AreEqual(new TimeSpan(0, 1, 0), project.SimulateFor(new TimeSpan(0, 2, 0))); Assert.IsInstanceOfType(typeof(RationalValue), c.Value); Assert.AreEqual(3, ((RationalValue)c.Value).NumeratorValue); Assert.AreEqual(2, ((RationalValue)c.Value).DenominatorValue); }