예제 #1
0
 /// <summary>Get the unit of measure's symbol in the fundamental units for that
 /// system. For example a Newton is a kg.m/s2.</summary>
 ///
 /// <returns> Base symbol</returns>
 public string GetBaseSymbol()
 {
     lock (new object())
     {
         if (BaseSymbol == null)
         {
             Reducer powerMap = GetReducer();
             BaseSymbol = powerMap.BuildBaseString();
         }
         return(BaseSymbol);
     }
 }
예제 #2
0
        private UnitOfMeasure MultiplyOrDivide(UnitOfMeasure other, bool invert)
        {
            if (other == null)
            {
                throw new Exception(MeasurementSystem.GetMessage("unit.cannot.be.null"));
            }

            CheckOffset(this);
            CheckOffset(other);

            // this base symbol map
            Reducer thisReducer = GetReducer();
            Dictionary <UnitOfMeasure, int> thisMap = thisReducer.Terms;

            // other base symbol map
            Reducer otherReducer = other.GetReducer();
            Dictionary <UnitOfMeasure, int> otherMap = otherReducer.Terms;

            // create a map of the unit of measure powers
            Dictionary <UnitOfMeasure, int> resultMap = new Dictionary <UnitOfMeasure, int>();

            // iterate over the multiplier's unit map
            foreach (KeyValuePair <UnitOfMeasure, int> thisEntry in thisMap)
            {
                UnitOfMeasure thisUOM   = thisEntry.Key;
                int           thisPower = thisEntry.Value;

                if (otherMap.TryGetValue(thisUOM, out int otherPower))
                {
                    if (!invert)
                    {
                        // add to multiplier's power
                        thisPower += otherPower;
                    }
                    else
                    {
                        // subtract from dividend's power
                        thisPower -= otherPower;
                    }

                    // remove multiplicand or divisor UOM
                    otherMap.Remove(thisUOM);
                }

                if (thisPower != 0)
                {
                    resultMap[thisUOM] = thisPower;
                }
            }

            // add any remaining multiplicand terms and invert any remaining divisor
            // terms
            foreach (KeyValuePair <UnitOfMeasure, int> otherEntry in otherMap)
            {
                UnitOfMeasure otherUOM   = otherEntry.Key;
                int           otherPower = otherEntry.Value;

                if (!invert)
                {
                    resultMap[otherUOM] = otherPower;
                }
                else
                {
                    resultMap[otherUOM] = -otherPower;
                }
            }

            // get the base symbol and possibly base UOM
            Reducer resultReducer = new Reducer();

            resultReducer.Terms = resultMap;

            // product or quotient
            UnitOfMeasure result = new UnitOfMeasure();

            if (!invert)
            {
                result.SetProductUnits(this, other);
            }
            else
            {
                result.SetQuotientUnits(this, other);
            }

            if (!invert)
            {
                result.Symbol = GenerateProductSymbol(result.GetMultiplier(), result.GetMultiplicand());
            }
            else
            {
                result.Symbol = GenerateQuotientSymbol(result.GetDividend(), result.GetDivisor());
            }

            // constrain to a maximum length
            if (result.Symbol.Length > MAX_SYMBOL_LENGTH)
            {
                result.Symbol = GenerateIntermediateSymbol();
            }

            String        baseSymbol = resultReducer.BuildBaseString();
            UnitOfMeasure baseUOM    = MeasurementSystem.GetSystem().GetBaseUOM(baseSymbol);

            if (baseUOM != null)
            {
                // there is a conversion to the base UOM
                double thisFactor  = thisReducer.MapScalingFactor;
                double otherFactor = otherReducer.MapScalingFactor;

                double resultFactor = 0;
                if (!invert)
                {
                    resultFactor = thisFactor * otherFactor;
                }
                else
                {
                    resultFactor = thisFactor / otherFactor;
                }
                result.ScalingFactor = resultFactor;
                result.AbscissaUnit  = baseUOM;
                result.UOMType       = baseUOM.UOMType;
            }

            return(result);
        }