Exemplo n.º 1
0
 ExpFactor ParseFactor(string s, ExpFactor f)
 {
     if (s[0] == '2')
     {
         f = f.Multiply(new ExpFactor(Int16.Parse(s.Substring(2)), 0));
     }
     else
     {
         f = f.Multiply(new ExpFactor(0, Int16.Parse(s.Substring(3))));
     }
     return(f);
 }
Exemplo n.º 2
0
 public void Add(AtomicMeasureUnit u, int exp)
 {
     if (u.AtomicMeasureUnit.Normalization == None)
     {
         var fp = u.AtomicMeasureUnit.NormalizationFactor.ExpFactor.Power(exp);
         _dimensionLessFactor = _dimensionLessFactor.Multiply(fp);
     }
     else
     {
         for (int i = 0; i < _normM.Count; ++i)
         {
             if (_normM[i] == u)
             {
                 _normE[i] += exp;
                 return;
             }
         }
         _normM.Add(u);
         _normE.Add(exp);
     }
 }
Exemplo n.º 3
0
        bool TryParseExponent(Match match, out ExponentMeasureUnit u, bool skipFirstLookup = false)
        {
            if (!skipFirstLookup && TryGetExistingExponent(match.ToString(), out u))
            {
                return(true);
            }
            u = null;
            // Handling dimensionless unit.
            var factor = match.Groups[4].Value;

            if (factor.Length > 0)
            {
                var f = ParseFactor(factor, ExpFactor.Neutral);
                u = RegisterPrefixed(f, MeasureStandardPrefix.None, MeasureUnit.None);
                return(true);
            }
            string sExp = match.Groups[3].Value;
            int    exp  = sExp.Length > 0 ? Int32.Parse(sExp) : 1;

            if (exp == 0)
            {
                u = MeasureUnit.None;
                return(true);
            }
            string withoutExp = match.Groups[1].Value + match.Groups[2].Value;

            if (_allUnits.TryGetValue(withoutExp, out var unit))
            {
                if (exp == 1 && unit is ExponentMeasureUnit direct)
                {
                    u = direct;
                    return(true);
                }
                if (!(unit is AtomicMeasureUnit atomic))
                {
                    return(false);
                }
                u = RegisterExponent(exp, atomic);
                return(true);
            }
            ExpFactor adjustment = ExpFactor.Neutral;

            foreach (Capture f in match.Groups[1].Captures)
            {
                adjustment = ParseFactor(f.Value, adjustment);
            }
            string withoutAdjustment = match.Groups[2].Value;

            if (_allUnits.TryGetValue(withoutAdjustment, out unit))
            {
                if (!(unit is AtomicMeasureUnit basic))
                {
                    return(false);
                }
                if (basic is PrefixedMeasureUnit prefix)
                {
                    adjustment = adjustment.Multiply(prefix.AdjustmentFactor).Multiply(prefix.Prefix.Factor);
                    basic      = prefix.AtomicMeasureUnit;
                }
                u = CreateExponentPrefixed(exp, adjustment, basic);
                return(true);
            }
            var p = MeasureStandardPrefix.FindPrefix(withoutAdjustment);

            if (p == MeasureStandardPrefix.None)
            {
                return(false);
            }
            withoutAdjustment = withoutAdjustment.Substring(p.Abbreviation.Length);
            if (withoutAdjustment.Length == 0)
            {
                return(false);
            }
            if (_allUnits.TryGetValue(withoutAdjustment, out unit))
            {
                if (!(unit is AtomicMeasureUnit basic))
                {
                    return(false);
                }
                if (basic is PrefixedMeasureUnit prefix)
                {
                    return(false);
                }
                adjustment = adjustment.Multiply(p.Factor);
                u          = CreateExponentPrefixed(exp, adjustment, basic);
                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Returns this factor multiplied by another one.
 /// </summary>
 /// <param name="x">The factor to multiply with.</param>
 /// <returns>The resulting full factor.</returns>
 public FullFactor Multiply(FullFactor x) => new FullFactor(Factor * x.Factor, ExpFactor.Multiply(x.ExpFactor));