Exemple #1
0
        private Unit GetBaseUnits()
        {
            if (standardUnit is BaseUnit)
            {
                return(standardUnit);
            }

            if (standardUnit is AlternateUnit)
            {
                return(((AlternateUnit)standardUnit).parent.GetBaseUnits());
            }

            if (standardUnit is ProductUnit)
            {
                ProductUnit productUnit = (ProductUnit)standardUnit;
                Unit        baseUnits   = ONE;

                for (int i = 0; i < productUnit.unitCount; i++)
                {
                    Unit unit = productUnit.GetUnit(i).GetBaseUnits();
                    unit      = unit.Pow(productUnit.GetUnitPow(i));
                    unit      = unit.Root(productUnit.GetUnitRoot(i));
                    baseUnits = baseUnits.Mul(unit);
                }

                return(baseUnits);
            }
            else
            {
                throw new ArithmeticException("System cannot be an instance of " + this);
            }
        }
Exemple #2
0
        /// <summary>
        /// Queries the dimension of this unit. The returned dimension depends on the current
        /// dimension model.
        /// </summary>
        /// <returns>The dimension.</returns>
        public Dimension GetDimension()
        {
            Unit standard = standardUnit;

            if (standard is BaseUnit)
            {
                return(Dimension.CURRENT_MODEL.GetDimension((BaseUnit)standard));
            }

            if (standard is AlternateUnit)
            {
                return(((AlternateUnit)standard).parent.GetDimension());
            }

            ProductUnit pu  = (ProductUnit)standard;
            Dimension   ret = Dimension.NONE;

            for (int i = 0; i < pu.unitCount; i++)
            {
                Unit      u = pu.GetUnit(i);
                Dimension d = u.GetDimension().Pow(pu.GetUnitPow(i)).Root(pu.GetUnitRoot(i));
                ret = ret.Mul(d);
            }

            return(ret);
        }
Exemple #3
0
        private static UnitConverter TransformOf(Unit unit)
        {
            if (unit is BaseUnit)
            {
                return(UnitConverter.IDENTITY);
            }

            ProductUnit   productUnit = (ProductUnit)unit;
            UnitConverter ret         = UnitConverter.IDENTITY;

            for (int i = 0; i < productUnit.unitCount; i++)
            {
                Unit          u         = productUnit.GetUnit(i);
                UnitConverter converter = TransformOf(u);

                if (!converter.isLinear)
                {
                    throw new ArithmeticException("Cannot convert: " + unit + " is non-linear.");
                }

                if (productUnit.GetUnitRoot(i) != 1)
                {
                    throw new ArithmeticException("Cannot convert: " + productUnit + " holds a base unit with a fractional exponent.");
                }

                int pow = productUnit.GetUnitPow(i);
                if (pow < 0)
                {
                    pow       = -pow;
                    converter = converter.Inverse();
                }

                for (int j = 0; j < pow; j++)
                {
                    ret = ret.Concatenate(converter);
                }
            }

            return(ret);
        }