Ejemplo n.º 1
0
        public static RadicalSumRatio Add(
            RadicalSumRatio left,
            RadicalSumRatio right)
        {
            // n1   n2   (n1 * d2) + (n2 * d1)
            // -- + -- = ---------------------
            // d1   d2          d1 * d2
            RadicalSum numerator =
                (left.Numerator * right.Denominator)
                + (right.Numerator * left.Denominator);
            RadicalSum denominator = left.Denominator * right.Denominator;

            return(new RadicalSumRatio(numerator, denominator));
        }
Ejemplo n.º 2
0
 public RadicalSumRatio(RadicalSum numerator, RadicalSum denominator)
 {
     if (RadicalSum.Zero == denominator)
     {
         throw new ArgumentException("Denominator cannot be zero", nameof(denominator));
     }
     if (denominator < RadicalSum.Zero)
     {
         ToSimplestForm(-numerator, -denominator, out _numerator, out _denominator);
     }
     else
     {
         ToSimplestForm(numerator, denominator, out _numerator, out _denominator);
     }
 }
Ejemplo n.º 3
0
        public static RadicalSumRatio Multiply(
            RadicalSumRatio left,
            RadicalSumRatio right)
        {
            //               C1n   C2n   commonFactorLeft * C1n_reduced   commonFactorRight * C2n_reduced
            // R = R1 * R2 = --- * --- = ------------------------------ * -------------------------------
            //               C1d   C2d                      C1d                               C2d
            //
            //     commonFactor * c1n_reduced * c2n_reduced
            //   = ----------------------------------------
            //                            c2d * c1d
            //
            // ci_reduced === ci / commonFactori
            // commonFactor === commonFactorLeft * commonFactorRight;
            // Simplest form puts all common factors in numerator
            var commonFactorLeft  = Utilities.GetCommonFactor(left.Numerator.Coefficients);
            var commonFactorRight = Utilities.GetCommonFactor(right.Numerator.Coefficients);
            var commonFactor      = commonFactorLeft * commonFactorRight;
            var c_left_n_reduced  = left.Numerator / commonFactorLeft;
            var c_right_n_reduced = right.Numerator / commonFactorRight;
            var d_left            = left.Denominator;
            var d_right           = right.Denominator;

            if (c_left_n_reduced == d_right)
            {
                c_left_n_reduced = RadicalSum.One;
                d_right          = RadicalSum.One;
            }
            if (c_right_n_reduced == d_left)
            {
                c_right_n_reduced = RadicalSum.One;
                d_left            = RadicalSum.One;
            }

            RadicalSum numerator   = commonFactor * c_left_n_reduced * c_right_n_reduced;
            RadicalSum denominator = d_left * d_right;

            var result = new RadicalSumRatio(numerator, denominator);

            return(result);
        }
Ejemplo n.º 4
0
        private static void ToSimplestForm(
            RadicalSum n_in,
            RadicalSum d_in,
            out RadicalSum n_out,
            out RadicalSum d_out)
        {
            // First extract common factors from numerator and denominator:
            // N = common_factor_n * (N/common_factor_n); n_reduced === N/common_factor_n
            // D = common_factor_d * (D/common_factor_d); d_reduced === D/common_factor_d
            Rational common_factor_n = Utilities.GetCommonFactor(n_in.Coefficients);
            Rational common_factor_d = Utilities.GetCommonFactor(d_in.Coefficients);
            var      n_reduced       = n_in * Rational.Invert(common_factor_n);
            var      d_reduced       = d_in * Rational.Invert(common_factor_d);

            //     N   common_factor_n * (N/common_factor_n)   common_factor_n * n_reduced                   n_reduced
            // C = - = ------------------------------------- = --------------------------- = common_factor * ---------
            //     D   common_factor_d * (D/common_factor_d)   common_factor_d * d_reduced                   d_reduced
            //
            // common_factor === common_factor_n / common_factor_d
            var common_factor = common_factor_n / common_factor_d;

            if (n_reduced == d_reduced)
            {
                n_out = common_factor * RadicalSum.One;
                d_out = RadicalSum.One;
            }
            else if (d_reduced.Radicals.Length == 1)
            {
                n_out = common_factor * n_reduced / d_reduced.Radicals[0];
                d_out = RadicalSum.One;
            }
            else
            {
                n_out = common_factor * n_reduced;
                d_out = d_reduced;
            }
        }
Ejemplo n.º 5
0
 public RadicalSumRatio(RadicalSum numerator)
     : this(numerator, RadicalSum.One)
 {
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Converts to a sum of radicals by multiplying the numerator by the multiplicative inverse of the denominator.
 /// WARNING: Finding the inverse could take a very long time and consume considerable memory and CPU. Use with caution.
 /// </summary>
 /// <param name="left"></param>
 /// <param name="right"></param>
 /// <returns></returns>
 public RadicalSum ToRadicalSum()
 {
     return(RadicalSum.GroupDivide(Numerator, Denominator));
 }