Exemplo n.º 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)));
        }
Exemplo n.º 2
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()));
            }
        }
Exemplo n.º 3
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");
            //}
        }