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)); }
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; }
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)); }
private RootSum(RootTerm a, RootTerm b) { this.a = a; this.b = b; }
public RootSum Add(RootTerm other) => RootSum.Add(this, other);