Ejemplo n.º 1
0
 private static NamedComposition <IUnit> GetComposition(DerivedQuantity quant)
 {
     return(NamedComposition <IUnit> .
            CreateFromExistingBaseComposition(
                quant.BaseQuantityComposition,
                baseQuantity => baseQuantity.FundamentalUnit));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// To be called only from <see cref="Quantity.GetFromBaseComposition(NamedComposition{BaseQuantity})"/>
 /// </summary>
 /// <param name="composition"></param>
 internal DerivedQuantity(NamedComposition <BaseQuantity> composition)
     : base(null, null)
 {
     BaseQuantityComposition = composition;
     FundamentalUnit         = new DerivedUnit(this);
     Init();
 }
Ejemplo n.º 3
0
        public bool Equals(NamedComposition <T>?other)
        {
            if (other is null)
            {
                return(false);
            }

            if (Composition.Count != other.Composition.Count)
            {
                return(false);
            }

            // check if any keys in this that are not in other
            // don't need to do the reverse since we already know there
            // are the same number of keys in each
            if (Composition.Keys.Except(other.Composition.Keys).Any())
            {
                return(false);
            }

            foreach (var kvp in Composition)
            {
                var(key, power) = kvp;
                if (other.Composition[key] != power)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        public static NamedComposition <IUnit> MultiplyOrDivide(bool multiplication, params IUnit[] units)
        {
            var res = units[0].UnitComposition;

            for (int i = 1; i < units.Length; ++i)
            {
                res = NamedComposition <IUnit> .MultiplyOrDivide(res, units[i].UnitComposition, multiplication);
            }

            return(res);
        }
Ejemplo n.º 5
0
 protected Unit(
     string?name,
     IUnit otherUnit,
     decimal multiplier,
     decimal offset,
     string?symbol)
     : base(name, symbol)
 {
     Quantity = otherUnit.Quantity;
     FundamentalMultiplier = otherUnit.FundamentalMultiplier * multiplier;
     FundamentalOffset     = (otherUnit.FundamentalOffset / multiplier) + offset;
     UnitComposition       = new(this);
 }
Ejemplo n.º 6
0
 // only to be called when defining fundamental units for new
 // quantities, and thus offset will always be 0
 protected Unit(
     string?name,
     Quantity quantity,
     decimal fundamentalMultiplier,
     NamedComposition <IUnit>?composition = null,
     string?symbol = null)
     : base(name, symbol)
 {
     Quantity = quantity;
     FundamentalMultiplier = fundamentalMultiplier;
     FundamentalOffset     = 0;
     UnitComposition       = composition ?? new(this);
 }
Ejemplo n.º 7
0
        public static Unit DefineFromComposition(string name, NamedComposition <IUnit> composition)
        {
            var quantity = Quantity.GetFromBaseComposition(composition);

            if (quantity is BaseQuantity)
            {
                return(new BaseUnit(name, composition));
            }
            else
            {
                return(new DerivedUnit(name, composition));
            }
        }
Ejemplo n.º 8
0
        internal static NamedComposition <T> CreateFromExistingBaseComposition <TExistingBase>(
            NamedComposition <TExistingBase> existingBaseComposition,
            Func <TExistingBase, T> convertor)

            where TExistingBase : IBase, IComparable <TExistingBase>, IEquatable <TExistingBase>
        {
            SortedDictionary <T, decimal> convertedComposition = new();

            foreach (var(existingBase, power) in existingBaseComposition.Composition)
            {
                var convertedBase = convertor(existingBase);
                convertedComposition.Add(convertedBase, power);
            }

            return(new(convertedComposition.AsReadOnly()));
        }
Ejemplo n.º 9
0
        // for defining from a chain of operations
        protected Unit(string name, NamedComposition <IUnit> composition)
            : base(name)
        {
            // TODO: notify user that offsets will be ignored
            //var offsetQuery =
            //    from baseUnit in composition.Composition.Keys
            //    where baseUnit.FundamentalOffset != 0m
            //    select baseUnit;

            UnitComposition       = composition;
            Quantity              = Quantity.GetFromBaseComposition(UnitComposition);
            FundamentalMultiplier = 1m;
            FundamentalOffset     = 0;
            foreach (var(unit, power) in UnitComposition.Composition)
            {
                var multiplier = DecimalEx.Pow(unit.FundamentalMultiplier, power);
                FundamentalMultiplier *= multiplier;
            }
        }
Ejemplo n.º 10
0
        public static NamedComposition <T> MultiplyOrDivide(
            NamedComposition <T> lhs,
            NamedComposition <T> rhs,
            bool multiplication)
        {
            var multiplyFactor = multiplication ? 1.0m : -1.0m;
            SortedDictionary <T, decimal> resultingComposition = new();

            var keysInBothSides = lhs.Composition.Keys.Intersect(rhs.Composition.Keys);

            foreach (var bothSidesKey in keysInBothSides)
            {
                var resultingPower = lhs.Composition[bothSidesKey] +
                                     (multiplyFactor * rhs.Composition[bothSidesKey]);

                if (resultingPower != 0.0m)
                {
                    resultingComposition[bothSidesKey] = resultingPower;
                }
            }

            var keysInLhs = lhs.Composition.Keys.Except(keysInBothSides);

            foreach (var lhsKey in keysInLhs)
            {
                resultingComposition[lhsKey] = lhs.Composition[lhsKey];
            }

            var keysInRhs = rhs.Composition.Keys.Except(keysInBothSides);

            foreach (var rhsKey in keysInRhs)
            {
                resultingComposition[rhsKey] = rhs.Composition[rhsKey] * multiplyFactor;
            }

            if (resultingComposition.Count == 0)
            {
                return(Empty);
            }

            return(new NamedComposition <T>(resultingComposition.AsReadOnly()));
        }
Ejemplo n.º 11
0
 private BaseQuantity(string name, string?symbol)
     : base(name, symbol)
 {
     BaseQuantityComposition = new NamedComposition <BaseQuantity>(this);
     Init();
 }
Ejemplo n.º 12
0
 // for defining from a chain of operations
 internal DerivedUnit(string name, NamedComposition <IUnit> composition)
     : base(name, composition)
 {
 }
Ejemplo n.º 13
0
 static NamedComposition()
 {
     Empty = new NamedComposition <T>(
         new Dictionary <T, decimal>().AsReadOnly());
 }