/// <summary> /// Returns the remainder of <c>this</c> divided by <c>G</c> in a new GF2Polynomial /// </summary> /// /// <param name="G">A GF2Polynomial != 0</param> /// /// <returns>Returns a new GF2Polynomial (<c>this</c> % <c>G</c>)</returns> public GF2Polynomial Remainder(GF2Polynomial G) { // a div b = q / r GF2Polynomial a = new GF2Polynomial(this); GF2Polynomial b = new GF2Polynomial(G); GF2Polynomial j; int i; if (b.IsZero()) throw new Exception(); a.ReduceN(); b.ReduceN(); if (a._length < b._length) return a; i = a._length - b._length; while (i >= 0) { j = b.ShiftLeft(i); a.SubtractFromThis(j); a.ReduceN(); i = a._length - b._length; } return a; }
/// <summary> /// Returns the greatest common divisor of <c>this</c> and <c>G</c> in a new GF2Polynomial /// </summary> /// /// <param name="G">A GF2Polynomial != 0</param> /// /// <returns>Returns a new GF2Polynomial gcd(<c>this</c>,<c>g</c>)</returns> public GF2Polynomial Gcd(GF2Polynomial G) { if (IsZero() && G.IsZero()) throw new ArithmeticException("Both operands of gcd equal zero."); if (IsZero()) return new GF2Polynomial(G); if (G.IsZero()) return new GF2Polynomial(this); GF2Polynomial a = new GF2Polynomial(this); GF2Polynomial b = new GF2Polynomial(G); GF2Polynomial c; while (!b.IsZero()) { c = a.Remainder(b); a = b; b = c; } return a; }
/// <summary> /// Returns the absolute quotient of <c>this</c> divided by <c>G</c> in a new GF2Polynomial /// </summary> /// /// <param name="G">A GF2Polynomial != 0</param> /// /// <returns>Returns a new GF2Polynomial |_ <c>this</c> / <c>G</c></returns> public GF2Polynomial Quotient(GF2Polynomial G) { // a div b = q / r GF2Polynomial q = new GF2Polynomial(_length); GF2Polynomial a = new GF2Polynomial(this); GF2Polynomial b = new GF2Polynomial(G); GF2Polynomial j; int i; if (b.IsZero()) throw new Exception(); a.ReduceN(); b.ReduceN(); if (a._length < b._length) return new GF2Polynomial(0); i = a._length - b._length; q.ExpandN(i + 1); while (i >= 0) { j = b.ShiftLeft(i); a.SubtractFromThis(j); a.ReduceN(); q.XorBit(i); i = a._length - b._length; } return q; }
/// <summary> /// Divides <c>this</c> by <c>G</c> and returns the quotient and remainder in a new GF2Polynomial[2], quotient in [0], remainder in [1] /// </summary> /// /// <param name="G">A GF2Polynomial != 0</param> /// /// <returns>Returns a new GF2Polynomial[2] containing quotient and remainder</returns> public GF2Polynomial[] Divide(GF2Polynomial G) { // a div b = q / r GF2Polynomial[] result = new GF2Polynomial[2]; GF2Polynomial q = new GF2Polynomial(_length); GF2Polynomial a = new GF2Polynomial(this); GF2Polynomial b = new GF2Polynomial(G); GF2Polynomial j; int i; if (b.IsZero()) throw new Exception(); a.ReduceN(); b.ReduceN(); if (a._length < b._length) { result[0] = new GF2Polynomial(0); result[1] = a; return result; } i = a._length - b._length; q.ExpandN(i + 1); while (i >= 0) { j = b.ShiftLeft(i); a.SubtractFromThis(j); a.ReduceN(); q.XorBit(i); i = a._length - b._length; } result[0] = q; result[1] = a; return result; }