Exemplo n.º 1
0
        public RootTerm Multiply(RootTerm other)
        {
            int      m = ((this.x < 0) && (other.x < 0)) ? -1 : 1;
            RootTerm r = Sqrt(this.x * other.x);

            return(new RootTerm(m * this.c * other.c * r.c, r.x));
        }
Exemplo n.º 2
0
        private RootTerm(int n)
        {
            if (n == 0)
            {
                this = Zero;
                return;
            }

            if (n == int.MinValue)
            {
                this = new RootTerm(32768, -2);
                return;
            }

            bool imag = n < 0;

            if (imag)
            {
                n = -n;
            }

            int s = (int)Math.Sqrt(n);

            if (s * s == n)
            {
                this = new RootTerm(s, imag ? -1 : 1);
                return;
            }

            this.c = 1;
            foreach (ushort p in Primes)
            {
                if (n < p)
                {
                    break;
                }

                int r;
                while (true)
                {
                    int m = Math.DivRem(n, p * p, out r);
                    if (r != 0)
                    {
                        break;
                    }

                    n       = m;
                    this.c *= p;
                }
            }

            this.x = imag ? -n : n;
        }
Exemplo n.º 3
0
        public static RootSum Add(RootTerm x, RootTerm y)
        {
            if (x.IsZero)
            {
                return(new RootSum(y, RootTerm.Zero));
            }

            if (y.IsZero)
            {
                return(new RootSum(x, RootTerm.Zero));
            }

            if (x.X == y.X)
            {
                return(new RootSum((x.C + y.C) * RootTerm.Sqrt(x.X), RootTerm.Zero));
            }

            if (x.IsReal && y.IsReal)
            {
                if (x.X < y.X)
                {
                    return(new RootSum(x, y));
                }

                return(new RootSum(y, x));
            }

            if (x.IsReal)
            {
                return(new RootSum(x, y));
            }

            if (y.IsReal)
            {
                return(new RootSum(y, x));
            }

            if (x.X > y.X)
            {
                return(new RootSum(x, y));
            }

            return(new RootSum(y, x));
        }
Exemplo n.º 4
0
 private RootSum(RootTerm a, RootTerm b)
 {
     this.a = a;
     this.b = b;
 }
Exemplo n.º 5
0
 public RootSum Add(RootTerm other) => RootSum.Add(this, other);