Gaussian integer x+iy and its prime factorization
Project description
GauInt: Gaussian integers
GG(x=0, y=0):
construct gaussian integer x+iy
where x and y are ordinary integers and i is imaginary unit.
|x| and |y| can be arbitrarily large.
Addition a+b, subtraction a-b, multiplication a*b,
division a/b, remainder a%b, division with remainder divmod(a,b),
equality testing a==b, inequality testing a!=b
can be performed on two GG objects a and b. On these operations,
one of a and b can be an ordinary integer (built in python).
Division with remainder q,r = a/b,a%b is such that a = b*q + r
and norm(r) <= norm(b)/2, where norm(a) = Re(a)**2 + Im(a)**2.
Exponentiation a**e can be performed on GG object a and
non-negative ordinary integer e.
-------------------------------------------------------------
Utility functions:
real(a): real part Re(a) of a
imag(a): imaginary part Im(a) of a
norm(a): norm Re(a)**2 + Im(a)**2 of a
conj(a): conjugate Re(a) - i*Im(a) of a
IsUnit(a): test if norm(a)==1
mul_i(a): a*i
div_i(a): a/i
mul_ipow(a,e): a*i**e
complex(a):
convert GG object to floating-point complex number.
IsAssoc(a, b, exponent=False):
test if a==b*i**e for some e (e=0,1,2,3).
If exponent is False, return True or False,
else if a is associate of b, return e,
else return -1.
quadrant(a):
return -1 if a==0,
return 0 if Re(a)>0 and Im(a)>=0,
return 1 if Re(a)<=0 and Im(a)>0,
return 2 if Re(a)<0 and Im(a)<=0,
return 3 if Re(a)>=0 and Im(a)<0.
FirstQuad(a, exponent=False):
return b = a*i^e for some e (e=0,1,2,3)
in first quadrant Re(b)>0 and Im(b)>=0.
If exponent is True, return b and e.
If a is zero, then b=0 and e=0.
GCD(a,b):
return d = greatest common divisor of a and b
in first quadrant Re(d)>0 and Im(d)>=0.
If a and b are both zero, then d=0.
XGCD(a,b):
compute d = greatest common divisor of a and b
in first quadrant Re(d)>0 and Im(d)>=0,
and return d,s,t such that d = s*a + t*b.
If a and b are both zero, then d,s,t=0,1,0.
factor(a):
factorize a into gaussian primes, and
return dictionary of (prime, exponent) pair
such that product of p**e is associate of a.
Real factors are positive and
imaginary factors are in first quadrant.
IsPrime(a):
test if a is gaussian prime,
and return True or False
GenPrime(l):
generate random gaussian prime p
such that bit length of norm(p) is l and
p is in first quadrant Re(p)>0 and Im(p)>0.
l must be larger than 1.
example usage:
>>> from GG import GG,GCD,XGCD
>>> a,b,c = GG(2,3), GG(4,5), GG(6,7)
>>> a *= c; b *= c
>>> d = GCD(a,b)
>>> print(d)
(6+7j)
>>> d,s,t = XGCD(a,b)
>>> print(d == s*a + t*b)
True
example usage:
>>> from GG import IsPrime,factor
>>> IsPrime(5), IsPrime(7)
(False, True)
>>> factor(5), factor(7)
({(2+1j): 1, (1+2j): 1}, {7: 1})
example usage:
>>> from GG import GenPrime,factor,IsAssoc
>>> a,b,c = GenPrime(8), GenPrime(8), GenPrime(8)
>>> a = a**2 * b**3 * c**4
>>> print(a)
(-12320219833+13665386778j)
>>> f = factor(a)
>>> print(f)
{(11+6j): 3, (14+1j): 4, (15+4j): 2}
>>> b = 1
>>> for k in f: b *= k**f[k]
...
>>> print(IsAssoc(a,b))
True
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
GauInt-0.0.3.tar.gz
(5.1 kB
view details)
Built Distribution
File details
Details for the file GauInt-0.0.3.tar.gz
.
File metadata
- Download URL: GauInt-0.0.3.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/26.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.25.2 tqdm/4.47.0 importlib-metadata/3.7.3 keyring/21.2.1 rfc3986/1.4.0 colorama/0.4.3 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d4de76874398c5cefa31bcda5997175d5ccd500ad57971362421e45cd1c7b0c |
|
MD5 | 2725f6c4ae95aa9e1613db721bd83b65 |
|
BLAKE2b-256 | 6124e33f5b843665f6fec5ad96ba8e58d6403c39d7d31176e4d7917ab02be185 |
File details
Details for the file GauInt-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: GauInt-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/26.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.25.2 tqdm/4.47.0 importlib-metadata/3.7.3 keyring/21.2.1 rfc3986/1.4.0 colorama/0.4.3 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd70f987dc538ecfd19034237f17b292a1b1ac64cbdd6645706df5b7149aa23d |
|
MD5 | b5a9f8d707431b4a86d7e7511d8472db |
|
BLAKE2b-256 | 5e0770b6fa8df668de64f139ebf6fbb5a6dfdf1a595eb48c4f033de7e4b7fbf8 |