diff --git a/src/mmath/bigint.nim b/src/mmath/bigint.nim index 8648114..4e1e406 100644 --- a/src/mmath/bigint.nim +++ b/src/mmath/bigint.nim @@ -260,3 +260,6 @@ proc `>`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) > 0 proc `==`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) == 0 proc `<=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) <= 0 proc `>=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) >= 0 +proc `!=`*(n1 : BigInt, n2 : BigInt) : bool = cmp(n1, n2) != 0 + +proc `of`*(_ : typedesc[BigInt], n : int64) : BigInt = newBigInt(n) \ No newline at end of file diff --git a/src/mmath/rational.nim b/src/mmath/rational.nim index fd4856a..bfc01da 100644 --- a/src/mmath/rational.nim +++ b/src/mmath/rational.nim @@ -1,3 +1,7 @@ +import constant + +export constant + proc gcd*[T](a : T, b : T) : T = var n1 = a var n2 = b @@ -30,38 +34,6 @@ proc simplify*[T](self : var Rational[T]) : void = proc newRational*[T](num : T) : Rational[T] = Rational[T](num: num, den: T(1)) proc newRational*[T](num : T, den: T) : Rational[T] = Rational[T](num: num, den: den) -proc one*[T : int64](_: type[T]) : T = 1.T - -proc zero*[T : int64](_: type[T]) : T = 0.T - -proc one*[T : uint64](_: type[T]) : T = 1.T - -proc zero*[T : uint64](_: type[T]) : T = 0.T - -proc one*[T : int](_: type[T]) : T = 1.T - -proc zero*[T : int](_: type[T]) : T = 0.T - -proc one*[T : uint](_: type[T]) : T = 1.T - -proc zero*[T : uint](_: type[T]) : T = 0.T - -proc one*[T : int16](_: type[T]) : T = 1.T - -proc zero*[T : int16](_: type[T]) : T = 0.T - -proc one*[T : uint16](_: type[T]) : T = 1.T - -proc zero*[T : uint16](_: type[T]) : T = 0.T - -proc one*[T : int8](_: type[T]) : T = 1.T - -proc zero*[T : int8](_: type[T]) : T = 0.T - -proc one*[T : uint8](_: type[T]) : T = 1.T - -proc zero*[T : uint8](_: type[T]) : T = 0.T - proc zero*[T](_: type[Rational[T]]): Rational[T] = newRational(T.zero, T.one) proc one*[T](_: type[Rational[T]]): Rational[T] = newRational(T.one, T.one) diff --git a/tests/test_bigint.nim b/tests/test_bigint.nim new file mode 100644 index 0000000..7c83466 --- /dev/null +++ b/tests/test_bigint.nim @@ -0,0 +1,63 @@ +import mmath/bigint +import unittest +import random +from strutils import parseBiggestUInt + +suite "Nim arbitrary precision integers (powered by GMP)": + + let str = "2347822319" + test "parse " & str: + let str = "2347822319" + let bi = BigInt.fromString(str, 10) + let ul : BiggestUInt = parseBiggestUInt(str) + check newBigInt(ul.int64) == bi + + var rng = initRand(101325) + + for i in 0..5: + let n1 = rng.rand(-1000..1000).int64 + let n2 = rng.rand(-1000..1000).int64 + let bi1 = BigInt.of(n1) + let bi2 = BigInt.of(n2) + + test $n1 & " + " & $n2: + check BigInt.of(n1 + n2) == bi1 + bi2 + + test $n1 & " - " & $n2: + check BigInt.of(n1 - n2) == bi1 - bi2 + + test $n1 & " * " & $n2: + check BigInt.of(n1 * n2) == bi1 * bi2 + + test $n1 & " div " & $n2: + check BigInt.of(n1 div n2) == bi1 div bi2 + + test $n1 & " mod " & $n2: + check BigInt.of(n1 mod n2) == bi1 mod bi2 + + test $n1 & " pow " & $3: + check BigInt.of(n1 * n1 * n1) == pow(bi1, 3) + + test "abs(" & $n1 & ")": + check BigInt.of(n1.abs()) == bi1.abs() + + test "abs(" & $n2 & ")": + check BigInt.of(n2.abs()) == bi2.abs() + + test $n1 & " cmp " & $n2: + check cmp(n1, n2) == cmp(bi1, bi2) + + test $n1 & " < " & $n2: + check (n1 < n2) == (bi1 < bi2) + + test $n1 & " > " & $n2: + check (n1 > n2) == (bi1 > bi2) + + test $n1 & " == " & $n1: + check bi1 == bi1 + + test $n2 & " == " & $n2: + check bi2 == bi2 + + test $n1 & " != " & $n2: + check bi1 != bi2 \ No newline at end of file diff --git a/tests/test_hmatrix.nim b/tests/test_hmatrix.nim index 3bd2066..f08f19a 100644 --- a/tests/test_hmatrix.nim +++ b/tests/test_hmatrix.nim @@ -78,7 +78,7 @@ suite "Nim linear algebra library": var mtx = newHMatrix[float64](100, 100, arr) var lu = mtx.clone() let pivot = lu.lup() - var b = newHVector[float64](100) + var b = newHVector[float64](100, 0.0f) for i in 0...len(b): b[i] = float64(rng.rand(b.len)) diff --git a/tests/test_smatrix.nim b/tests/test_smatrix.nim index 78ecdc2..d31129c 100644 --- a/tests/test_smatrix.nim +++ b/tests/test_smatrix.nim @@ -4,7 +4,7 @@ from mmath/smatrix import det, lu, lup, lu_det, lup, det, from_pivot, invert, id newSMatrix, SMatrix, clone, tril, triu, lu_solve, `$`, `*`, `-`, `+`, `-=`, `+=`, `==`, `*`, norm2, newSMatrixFromArray, gauss_jordan_high, gauss_jordan_low, transpose, squared_norm2 from mmath/svector import buildSVector, `-`, abs, norm, Svector -from mmath/pivot import SingularMatrixError +from mmath/error import SingularMatrixError from mmath/rational import newRational, Rational, one, zero, abs, `>`, `<`, `==`, `*`, `+`, `-`, `/`, `/=`, `+=`, `sqrt` import unittest @@ -116,3 +116,4 @@ suite "Nim linear algebra library": let mtx = newSMatrixFromArray[2, 5, int]([1,4,8,2,5,7,3,6,9,0]) let xpose = newSMatrixFromArray[5, 2, int]([1,7,4,3,8,6,2,9,5,0]) check(mtx.transpose() == xpose) +