Skip to content

Commit

Permalink
[NBKCoreKit] Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 28, 2023
1 parent 7921cce commit cbabf10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ extension NBK.ProperBinaryInteger where Integer: NBKSignedInteger {
let unsigned = Magnitude.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs.magnitude, and: rhs.magnitude)
//=--------------------------------------=
return (result: unsigned.result,
lhsCoefficient: Integer(sign: lhsIsLessThanZero != unsigned.isOddLoopCount ? .minus : .plus, magnitude: unsigned.lhsCoefficient)!,
rhsCoefficient: Integer(sign: rhsIsLessThanZero == unsigned.isOddLoopCount ? .minus : .plus, magnitude: unsigned.rhsCoefficient)!,
lhsCoefficient: Integer(sign: lhsIsLessThanZero != unsigned.iterationIsOdd ? .minus : .plus, magnitude: unsigned.lhsCoefficient)!,
rhsCoefficient: Integer(sign: rhsIsLessThanZero == unsigned.iterationIsOdd ? .minus : .plus, magnitude: unsigned.rhsCoefficient)!,
lhsQuotient: Integer(sign: lhsIsLessThanZero /*----------------------*/ ? .minus : .plus, magnitude: unsigned.lhsQuotient )!,
rhsQuotient: Integer(sign: rhsIsLessThanZero /*----------------------*/ ? .minus : .plus, magnitude: unsigned.rhsQuotient )!)
}
Expand Down Expand Up @@ -144,14 +144,11 @@ extension NBK.ProperBinaryInteger where Integer: NBKUnsignedInteger {
/// ### Bézout's identity (unsigned)
///
/// ```swift
/// var x = lhs &* lhsCoefficient
/// var y = rhs &* rhsCoefficient
///
/// if isOddLoopCount {
/// swap(&x, &y)
/// if !iterationIsOdd {
/// precondition(result == (lhs &* lhsCoefficient &- rhs &* rhsCoefficient))
/// } else {
/// precondition(result == (rhs &* rhsCoefficient &- lhs &* lhsCoefficient))
/// }
///
/// precondition(result == x &- y)
/// ```
///
/// ### Quotients of dividing by GCD
Expand All @@ -165,28 +162,28 @@ extension NBK.ProperBinaryInteger where Integer: NBKUnsignedInteger {
/// ### Loop count result
///
/// ```swift
/// let lhsCoefficientSign = lhs.isLessThanZero != isOddLoopCount
/// let rhsCoefficientSign = rhs.isLessThanZero == isOddLoopCount
/// let lhsCoefficientSign = lhs.isLessThanZero != iterationIsOdd
/// let rhsCoefficientSign = rhs.isLessThanZero == iterationIsOdd
/// ```
///
@inlinable public static func greatestCommonDivisorByExtendedEuclideanAlgorithm(of lhs: Integer, and rhs: Integer)
-> (result: Integer, lhsCoefficient: Integer, rhsCoefficient: Integer, lhsQuotient: Integer, rhsQuotient: Integer, isOddLoopCount: Bool) {
-> (result: Integer, lhsCoefficient: Integer, rhsCoefficient: Integer, lhsQuotient: Integer, rhsQuotient: Integer, iterationIsOdd: Bool) {
//=--------------------------------------=
var (r0, r1) = (lhs, rhs) as (Integer,Integer)
var (s0, s1) = (001, 000) as (Integer,Integer)
var (t0, t1) = (000, 001) as (Integer,Integer)
var isOddLoopCount = ((((((((((false))))))))))
var (a, b) = (lhs, rhs) as (Integer,Integer)
var (c, d) = (001, 000) as (Integer,Integer)
var (e, f) = (000, 001) as (Integer,Integer)
var iterationIsOdd = (((((((((false)))))))))
//=--------------------------------------=
while !r1.isZero {
while !b.isZero {

isOddLoopCount.toggle()
let division = r0.quotientAndRemainder(dividingBy: r1)
iterationIsOdd.toggle()
let division = a.quotientAndRemainder(dividingBy: b)

(r0, r1) = (r1, division.remainder)
(s0, s1) = (s1, division.quotient * s1 + s0)
(t0, t1) = (t1, division.quotient * t1 + t0)
(a, b) = (b, division.remainder)
(c, d) = (d, division.quotient * d + c)
(e, f) = (f, division.quotient * f + e)
}
//=--------------------------------------=
return (result: r0, lhsCoefficient: s0, rhsCoefficient: t0, lhsQuotient: t1, rhsQuotient: s1, isOddLoopCount: isOddLoopCount)
return (result: a, lhsCoefficient: c, rhsCoefficient: e, lhsQuotient: f, rhsQuotient: d, iterationIsOdd: iterationIsOdd)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ final class NBKProperBinaryIntegerTestsOnGreatestCommonDivisorByExtendedEuclidea
for lhs in Int8.min ... Int8.max {
for rhs in Int8.min ... Int8.max {
let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs)
let identity = lhs &* extended.lhsCoefficient &+ rhs &* extended.rhsCoefficient
XCTAssertEqual(identity, Int8(bitPattern: extended.result))
XCTAssertEqual(Int8(bitPattern: extended.result), lhs &* extended.lhsCoefficient &+ rhs &* extended.rhsCoefficient)
}
}
}
Expand All @@ -131,10 +130,11 @@ final class NBKProperBinaryIntegerTestsOnGreatestCommonDivisorByExtendedEuclidea
for lhs in UInt8.min ... UInt8.max {
for rhs in UInt8.min ... UInt8.max {
let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs)
let x = lhs &* extended.lhsCoefficient
let y = rhs &* extended.rhsCoefficient
let identity = extended.isOddLoopCount ? y &- x : x &- y
XCTAssertEqual(identity, extended.result)
if !extended.iterationIsOdd {
XCTAssertEqual(extended.result, lhs &* extended.lhsCoefficient &- rhs &* extended.rhsCoefficient)
} else {
XCTAssertEqual(extended.result, rhs &* extended.rhsCoefficient &- lhs &* extended.lhsCoefficient)
}
}
}
}
Expand Down Expand Up @@ -180,7 +180,7 @@ _ lhs: T, _ rhs: T, _ result: T.Magnitude,
file: StaticString = #file, line: UInt = #line) {

func with(_ lhs: T, _ rhs: T) {
NBKAssertGreatestCommonDivisorByBinaryAlgorithm(lhs, rhs, result, false, file: file, line: line)
NBKAssertGreatestCommonDivisorByBinaryAlgorithm(lhs, rhs, result, false, file: file, line: line)
NBKAssertGreatestCommonDivisorByExtendedEuclideanAlgorithmAsUnsigned(lhs, rhs, result, file: file, line: line)
}

Expand Down Expand Up @@ -227,22 +227,22 @@ file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(1, extended.lhsQuotient, file: file, line: line)
XCTAssertEqual(0, extended.rhsQuotient, file: file, line: line)
} else {
XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line)
XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line)
XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line)
XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line)
}
}

private func NBKAssertGreatestCommonDivisorByExtendedEuclideanAlgorithmAsUnsigned<T: NBKUnsignedInteger & NBKFixedWidthInteger>(
_ lhs: T, _ rhs: T, _ result: T.Magnitude,
file: StaticString = #file, line: UInt = #line) {
//=------------------------------------------=
let extended = NBK.PBI<T>.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs)
let extended = NBK.PBI.greatestCommonDivisorByExtendedEuclideanAlgorithm(of: lhs, and: rhs)
//=------------------------------------------=
XCTAssertEqual(extended.result, T(result), file: file, line: line)

if !result.isZero {
XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line)
XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line)
XCTAssertEqual(extended.lhsQuotient, lhs / T(result), file: file, line: line)
XCTAssertEqual(extended.rhsQuotient, rhs / T(result), file: file, line: line)
} else {
XCTAssertEqual(0, lhs, file: file, line: line)
XCTAssertEqual(0, rhs, file: file, line: line)
Expand All @@ -253,7 +253,7 @@ file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(0, extended.rhsCoefficient, file: file, line: line)
}

if !extended.isOddLoopCount {
if !extended.iterationIsOdd {
XCTAssertEqual(result, lhs &* extended.lhsCoefficient &- rhs &* extended.rhsCoefficient, file: file, line: line)
} else {
XCTAssertEqual(result, rhs &* extended.rhsCoefficient &- lhs &* extended.lhsCoefficient, file: file, line: line)
Expand Down

0 comments on commit cbabf10

Please sign in to comment.