예제 #1
0
 public override bool Equals(object other)
 {
     if (this == other)
     {
         return(true);
     }
     else if (other is ProductUnit)
     {
         ProductUnit p = (ProductUnit)other;
         Element[]   e = p.elements;
         if (elements.Length == e.Length)
         {
             for (int i = 0; i < elements.Length; i++)
             {
                 if (!elements[i].Equals(p.elements[i]))
                 {
                     return(false);
                 }
             }
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
예제 #2
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);
            }
        }
예제 #3
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);
        }
예제 #4
0
 /// <summary>
 /// Returns a unit equal to the given root of this unit.
 /// </summary>
 /// <param name="n"> The root's order. </param>
 /// <returns> The resultint unit. </returns>
 /// <exception cref="ArithmeticException"> if n == 0; </exception>
 public Unit Root(int n)
 {
     if (n > 0)
     {
         return(ProductUnit.GetRootInstance(this, n));
     }
     else if (n == 0)
     {
         throw new ArithmeticException("Root order cannot be 0");
     }
     else
     {
         return(ONE.Div(Root(-n)));
     }
 }
예제 #5
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);
        }
예제 #6
0
 public ProductUnit(ProductUnit other) : base(other.quantity)
 {
     elements = other.elements;
 }
예제 #7
0
 /// <summary>
 /// Returns the inverse of this unit.
 /// </summary>
 /// <returns> The unit representing 1 / this </returns>
 public Unit Inverse()
 {
     return(ProductUnit.GetQuotientInstance(ONE, this));
 }
예제 #8
0
 /// <summary>
 /// Returns the product of this unit with the one specified.
 /// <para>
 ///   Note: when this transform action is used, the resulting unit will have
 ///   this units quantity. This is important to remember if the provided unit
 ///   does NOT share the same quantity.
 /// </para>
 /// </summary>
 /// <param name="other"> The multiplicand unit. </param>
 /// <returns> The transformed unit. </returns>
 public Unit Mul(Unit other)
 {
     return(ProductUnit.GetProductInstance(this, other));
 }