public static bool SimplifySummands(ISignalSet signals) { bool changed = CollectSummands(signals); if (signals.Count < 2) { return(changed); } IAccumulator acc = null; for (int i = signals.Count - 1; i >= 0; i--) { Signal s = signals[i]; if (Std.IsConstantComplex(s)) { if (acc == null) { acc = Accumulator <IntegerValue, RationalValue> .Create(IntegerValue.AdditiveIdentity); } signals.RemoveAt(i); changed = true; acc = acc.Add(s.Value); } } if (acc != null && !acc.Value.Equals(IntegerValue.AdditiveIdentity)) { signals.Insert(0, Std.DefineConstant(acc.Value)); } return(changed); }
public static bool CollectSummands(ISignalSet signals) { bool changed = false; for(int i = 0; i < signals.Count; i++) { Signal s = signals[i]; if(!s.BehavesAsBeingDriven(false)) continue; Port p = s.DrivenByPort; if(p.Entity.EntityId.Equals(AdditionArchitectures.EntityIdentifier)) { signals.RemoveAt(i); ISignalSet inputs = p.InputSignals; for(int j = 0; j < inputs.Count; j++) signals.Insert(i + j, inputs[j]); i--; changed = true; continue; } if(p.Entity.EntityId.Equals(SubtractionArchitectures.EntityIdentifier)) { ISignalSet inputs = p.InputSignals; signals[i] = inputs[0]; i--; for(int j = 1; j < inputs.Count; j++) signals.Insert(i + j, Std.Negate(inputs[j])); changed = true; continue; } } return changed; }
public static bool SimplifyFactors(ISignalSet signals) { bool changed = CollectFactors(signals); if (signals.Count < 2) { return(changed); } IAccumulator acc = null; for (int i = signals.Count - 1; i > 0; i--) //don't touch first item! { Signal s = signals[i]; if (Std.IsConstantComplex(s)) { if (acc == null) { acc = Accumulator <IntegerValue, RationalValue> .Create(IntegerValue.MultiplicativeIdentity); } signals.RemoveAt(i); changed = true; if (Std.IsConstantAdditiveIdentity(s)) { signals.Clear(); signals.Add(UndefinedSymbol.Constant); return(true); } acc = acc.Multiply(s.Value); } } if (acc != null && !acc.Value.Equals(IntegerValue.MultiplicativeIdentity)) { Signal first = signals[0]; if (Std.IsConstantComplex(first)) { acc = acc.Divide(first.Value).Invert(); signals[0] = Std.DefineConstant(acc.Value); changed = true; } else { signals.Insert(1, Std.DefineConstant(acc.Value)); } } return(changed); }
public static bool CollectFactors(ISignalSet signals) { bool changed = false; for (int i = 0; i < signals.Count; i++) { Signal s = signals[i]; if (!s.BehavesAsBeingDriven(false)) { continue; } Port p = s.DrivenByPort; if (p.Entity.EntityId.Equals(MultiplicationArchitectures.EntityIdentifier)) { signals.RemoveAt(i); ISignalSet inputs = p.InputSignals; for (int j = 0; j < inputs.Count; j++) { signals.Insert(i + j, inputs[j]); } i--; changed = true; continue; } if (p.Entity.EntityId.Equals(DivisionArchitectures.EntityIdentifier)) { ISignalSet inputs = p.InputSignals; signals[i] = inputs[0]; i--; for (int j = 1; j < inputs.Count; j++) { signals.Insert(i + j, Std.Invert(inputs[j])); } changed = true; continue; } } return(changed); }
public static bool CollectSummands(ISignalSet signals) { bool changed = false; for (int i = 0; i < signals.Count; i++) { Signal s = signals[i]; if (!s.BehavesAsBeingDriven(false)) { continue; } Port p = s.DrivenByPort; if (p.Entity.EntityId.Equals(AdditionArchitectures.EntityIdentifier)) { signals.RemoveAt(i); ISignalSet inputs = p.InputSignals; for (int j = 0; j < inputs.Count; j++) { signals.Insert(i + j, (i == 0 && j != 0) ? Std.Negate(inputs[j]) : inputs[j]); } i--; changed = true; continue; } if (p.Entity.EntityId.Equals(SubtractionArchitectures.EntityIdentifier)) { ISignalSet inputs = p.InputSignals; signals[i] = inputs[0]; i--; for (int j = 1; j < inputs.Count; j++) { signals.Insert(i + j, (i == 0) ? inputs[j] : Std.Negate(inputs[j])); } changed = true; continue; } } return(changed); }
public static bool SimplifySummands(ISignalSet signals) { bool changed = CollectSummands(signals); if (signals.Count < 2) { return(changed); } IAccumulator acc = null; for (int i = signals.Count - 1; i > 0; i--) //don't touch first item! { Signal s = signals[i]; if (Std.IsConstantComplex(s)) { if (acc == null) { acc = Accumulator <IntegerValue, RationalValue> .Create(IntegerValue.AdditiveIdentity); } signals.RemoveAt(i); changed = true; acc = acc.Add(s.Value); } } if (acc != null && !acc.Value.Equals(IntegerValue.AdditiveIdentity)) { Signal first = signals[0]; if (Std.IsConstantComplex(first)) { acc = acc.Subtract(first.Value).Negate(); signals[0] = Std.DefineConstant(acc.Value); changed = true; } else { signals.Insert(1, Std.DefineConstant(acc.Value)); } } return(changed); }
public static bool SimplifySummands(ISignalSet signals) { bool changed = CollectSummands(signals); if(signals.Count < 2) return changed; IAccumulator acc = null; for(int i = signals.Count - 1; i >= 0; i--) { Signal s = signals[i]; if(Std.IsConstantComplex(s)) { if(acc == null) acc = Accumulator<IntegerValue, RationalValue>.Create(IntegerValue.AdditiveIdentity); signals.RemoveAt(i); changed = true; acc = acc.Add(s.Value); } } if(acc != null && !acc.Value.Equals(IntegerValue.AdditiveIdentity)) { signals.Insert(0, Std.DefineConstant(acc.Value)); } return changed; }
public static bool CollectFactors(ISignalSet signals) { bool changed = false; for(int i = 0; i < signals.Count; i++) { Signal s = signals[i]; if(!s.BehavesAsBeingDriven(false)) continue; Port p = s.DrivenByPort; if(p.Entity.EntityId.Equals(MultiplicationArchitectures.EntityIdentifier)) { signals.RemoveAt(i); ISignalSet inputs = p.InputSignals; for(int j = 0; j < inputs.Count; j++) signals.Insert(i + j, (i == 0 && j != 0) ? Std.Invert(inputs[j]) : inputs[j]); i--; changed = true; continue; } if(p.Entity.EntityId.Equals(DivisionArchitectures.EntityIdentifier)) { ISignalSet inputs = p.InputSignals; signals[i] = inputs[0]; i--; for(int j = 1; j < inputs.Count; j++) signals.Insert(i + j, (i == 0) ? inputs[j] : Std.Invert(inputs[j])); changed = true; continue; } } return changed; }
public static bool SimplifyFactors(ISignalSet signals) { bool changed = CollectFactors(signals); if(signals.Count < 2) return changed; IAccumulator acc = null; for(int i = signals.Count - 1; i > 0; i--) //don't touch first item! { Signal s = signals[i]; if(Std.IsConstantComplex(s)) { if(acc == null) acc = Accumulator<IntegerValue, RationalValue>.Create(IntegerValue.MultiplicativeIdentity); signals.RemoveAt(i); changed = true; if(Std.IsConstantAdditiveIdentity(s)) { signals.Clear(); signals.Add(UndefinedSymbol.Constant); return true; } acc = acc.Multiply(s.Value); } } if(acc != null && !acc.Value.Equals(IntegerValue.MultiplicativeIdentity)) { Signal first = signals[0]; if(Std.IsConstantComplex(first)) { acc = acc.Divide(first.Value).Invert(); signals[0] = Std.DefineConstant(acc.Value); changed = true; } else { signals.Insert(1, Std.DefineConstant(acc.Value)); } } return changed; }
public static bool SimplifyOperands(ISignalSet signals) { if (signals.Count < 2) { return(false); } bool changed = false; Signal radix = signals[0]; if (Std.IsConstantAdditiveIdentity(radix) && Std.IsConstant(signals[1])) { if (Std.IsAlwaysNegative(signals[1])) { signals.Clear(); signals.Add(UndefinedSymbol.Constant); return(true); } signals.Clear(); signals.Add(radix); return(true); } if (Std.IsConstantMultiplicativeIdentity(radix)) { signals.Clear(); signals.Add(radix); return(true); } // Evaluate all but the first two operands -> nonnegative integers IntegerValue value = null; for (int i = signals.Count - 1; i > 1; i--) { Signal s = signals[i]; if (!Std.IsConstantNonnegativeInteger(s)) { if (changed) { if (!value.IsMultiplicativeIdentity) { signals.Add(Std.DefineConstant(value)); } return(true); } return(false); } signals.RemoveAt(i); value = ValueConverter <IntegerValue> .ConvertFrom(s.Value).PositiveIntegerPower((int)value.Value); changed = true; } // Evaluate the second operand -> integer (can also be negative) Signal exponent = signals[1]; if (!Std.IsConstantInteger(exponent)) { if (changed) { if (!value.IsMultiplicativeIdentity) { signals.Add(Std.DefineConstant(value)); } return(true); } return(false); } IntegerValue exponentValue = ValueConverter <IntegerValue> .ConvertFrom(exponent.Value); if (changed) { exponentValue = exponentValue.PositiveIntegerPower((int)value.Value); } // Special case: first operand is a power if (radix.IsDrivenByPortEntity(PowerArchitectures.EntityIdentifier) && radix.DrivenByPort.InputSignalCount == 2 && Std.IsConstantInteger(radix.DrivenByPort.InputSignals[1])) { exponentValue = exponentValue.Multiply(ValueConverter <IntegerValue> .ConvertFrom(radix.DrivenByPort.InputSignals[1].Value)); signals[0] = radix = radix.DrivenByPort.InputSignals[0]; changed = true; } // Evaluate the first operand if (!Std.IsConstantComplex(radix)) { if (changed) { if (!exponentValue.IsMultiplicativeIdentity) { signals[1] = Std.DefineConstant(exponentValue); } else { signals.RemoveAt(1); } return(true); } return(false); } IAccumulator acc = Accumulator <IntegerValue, RationalValue> .Create(radix.Value); acc = acc.IntegerPower((int)exponentValue.Value); signals.Clear(); signals.Add(Std.DefineConstant(acc.Value)); return(true); }
public static bool SimplifySummands(ISignalSet signals) { bool changed = CollectSummands(signals); if(signals.Count < 2) return changed; IAccumulator acc = null; for(int i = signals.Count - 1; i > 0; i--) //don't touch first item! { Signal s = signals[i]; if(Std.IsConstantComplex(s)) { if(acc == null) acc = Accumulator<IntegerValue, RationalValue>.Create(IntegerValue.AdditiveIdentity); signals.RemoveAt(i); changed = true; acc = acc.Add(s.Value); } } if(acc != null && !acc.Value.Equals(IntegerValue.AdditiveIdentity)) { Signal first = signals[0]; if(Std.IsConstantComplex(first)) { acc = acc.Subtract(first.Value).Negate(); signals[0] = Std.DefineConstant(acc.Value); changed = true; } else { signals.Insert(1, Std.DefineConstant(acc.Value)); } } return changed; }