Exemplo n.º 1
0
        public IUnit Simplify(bool simplyfyNamedUnits = false)
        {
            List <UnitPowerPair> items = new List <UnitPowerPair>();

            if (simplyfyNamedUnits)
            {
                foreach (FundamentalUnitPowerPair pair in this.GetFundamentalUnitPairs())
                {
                    items.Add(pair);
                }
            }
            else
            {
                items.AddRange(this.GetUnitPairs());
            }

            for (int i = items.Count; i < 0; i--)
            {
                if (items[i].Unit.GetType() == typeof(None))
                {
                    items.RemoveAt(i);
                    continue;
                }

                for (int j = 0; j < i; j++)
                {
                    if (items[i].Unit == items[j].Unit)
                    {
                        items[j] = new UnitPowerPair {
                            Unit = items[j].Unit, Power = items[j].Power + items[i].Power
                        };
                        items.RemoveAt(i);
                        break;
                    }
                }
            }

            UnitPowerPair[] newUnitPowerPairs = items.ToArray();

            if (newUnitPowerPairs.Length == 1 && (newUnitPowerPairs[0].Power == 1))
            {
                return((IUnit)newUnitPowerPairs[0].Unit.Clone());
            }
            else if (newUnitPowerPairs.Length == 0)
            {
                return(new None());
            }
            else
            {
                CompoundUnit newInstance = (CompoundUnit)this.Clone();
                newInstance.UnitPowerPairs = newUnitPowerPairs;
                return((IUnit)newInstance);
            }
        }
Exemplo n.º 2
0
        public override IUnit Pow(int p)
        {
            UnitPowerPair[] terms = new UnitPowerPair[UnitPowerPairs.Length];

            for (int i = 0; i < UnitPowerPairs.Length; i++)
            {
                terms[i] = new UnitPowerPair {
                    Unit = UnitPowerPairs[i].Unit, Power = UnitPowerPairs[i].Power * p
                };
            }

            return(NewCompoundUnit(terms));
        }
Exemplo n.º 3
0
        public IUnit Div(IUnit u)
        {
            List <UnitPowerPair> items = new List <UnitPowerPair>(this.GetUnitPairs());

            foreach (UnitPowerPair pair in u.GetUnitPairs())
            {
                UnitPowerPair newPair = new UnitPowerPair {
                    Unit = pair.Unit, Power = -pair.Power
                };
                items.Add(newPair);
            }

            return(CompoundUnit.NewCompoundUnit(items.ToArray()));
        }