Beispiel #1
0
        public static bool IsInverse(Quantity left, Quantity right)
        {
            var leftParts  = UnitParts.CreateFrom(left);
            var rightParts = UnitParts.CreateFrom(right);

            return(leftParts.Flattened.SequenceEqual(rightParts.Flattened.Select(x => x ^ -1)));
        }
Beispiel #2
0
 public MissingOverload(Quantity left, string @operator, Quantity right, UnitParts result)
 {
     this.Left     = left;
     this.Operator = @operator;
     this.Right    = right;
     this.Result   = result;
 }
Beispiel #3
0
        public static Quantity FindRight(Settings settings, Quantity left, Quantity result)
        {
            var derivedUnit = result.Unit as DerivedUnit;

            if (derivedUnit != null)
            {
                var right = UnitParts.CreateFrom(result) / UnitParts.CreateFrom(left);
                return(Find(settings, right.Flattened.ToArray()));
            }
            else
            {
                var right = UnitParts.CreateFrom(left) / UnitParts.CreateFrom(result);
                return(Find(settings, right.Flattened.ToArray()));
            }
        }
Beispiel #4
0
        /// <summary>
        /// Solves left * right^x = result
        /// Where x =±1
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        private int FindPower(Quantity left, Quantity right, Quantity result)
        {
            var leftParts   = UnitParts.CreateFrom(left);
            var rightParts  = UnitParts.CreateFrom(right);
            var resultParts = UnitParts.CreateFrom(result);

            if (leftParts * rightParts == resultParts)
            {
                return(1);
            }
            if (leftParts / rightParts == resultParts)
            {
                return(-1);
            }
            else
            {
                throw new ArgumentException(string.Format("Cound not find power for {0}*{1}^x == {2}",
                                                          left.ClassName,
                                                          right.ClassName,
                                                          result.ClassName));
            }
            //SiUnit siUnit = left.Unit as SiUnit;
            //if (siUnit != null)
            //{
            //    var unitAndPower = right.Single();
            //    if (Math.Abs(unitAndPower.Power) != 1)
            //    {
            //        throw new ArgumentException();
            //    }
            //    return unitAndPower.Power;
            //}
            //else
            //{
            //    DerivedUnit derivedUnit = (DerivedUnit)left.Unit;
            //    var unitAndPowers = derivedUnit.Parts.OrderBy(x => x.UnitName).ToArray();
            //    var andPowers = right.OrderBy(x => x.UnitName).ToArray();
            //    if (unitAndPowers.Select(x => x.Power).SequenceEqual(andPowers.Select(x => x.Power)))
            //    {
            //        return 1;
            //    }
            //    if (unitAndPowers.Select(x => x.Power).SequenceEqual(andPowers.Select(x => -1 * x.Power)))
            //    {
            //        return -1;
            //    }
            //    throw new ArgumentException("message");
            //}
        }
Beispiel #5
0
        public DerivedUnit(string name, string symbol, params UnitAndPower[] parts)
            : base(name, symbol)
        {
            _parts = new UnitParts(this);
            if (parts.Length == 0)
            {
                throw new ArgumentException("No units", "units");
            }
            if (parts.Length != parts.Select(x => x.Unit.ClassName).Distinct().Count())
            {
                throw new ArgumentException("Units must be distinct", "units");
            }
            var unitAndPowers = parts.OrderBy(x => x.UnitName).ThenBy(x => x.Power).ToList();

            foreach (var unitAndPower in unitAndPowers)
            {
                _parts.Add(unitAndPower);
            }
        }
Beispiel #6
0
        public void SetParts(IEnumerable <Conversion> subunits)
        {
            var derivedUnit = BaseUnit as DerivedUnit;

            if (derivedUnit == null)
            {
                throw new InvalidOperationException("trying to set partunits when baseunit != DerivedUnit");
            }
            double cf        = 1;
            var    unitParts = new UnitParts(derivedUnit, derivedUnit.Parts.ToArray());

            foreach (var part in subunits)
            {
                var up = unitParts.Single(x => x.UnitName == part.BaseUnit.ClassName);
                cf = cf * Math.Pow(part.Formula.ConversionFactor, up.Power);
                unitParts.Replace(up, new UnitAndPower(part, up.Power));
            }
            Formula.ConversionFactor = cf;
            ClassName = unitParts.UnitName;
            Symbol    = unitParts.Expression;
        }
Beispiel #7
0
 private bool Equals(UnitParts other)
 {
     return(this.BaseParts.Equals(other.BaseParts));
 }
Beispiel #8
0
 public MissingOverloads(UnitParts parts, IReadOnlyList <MissingOverload> missing)
 {
     this.Parts   = parts;
     this.Missing = missing;
 }
Beispiel #9
0
 protected bool Equals(UnitParts other)
 {
     return(this.BaseParts.Equals(other.BaseParts));
 }
Beispiel #10
0
 public DerivedUnit()
     : base(null, null)
 {
     _parts = new UnitParts(this);
 }