NAME
Sidef::Types::Number::Gauss - Gaussian integer arithmetic in Sidef
DESCRIPTION
This class implements Gaussian integers, which are complex numbers of the form a + bi where both a and b are integers. Gaussian integers form a unique factorization domain and are useful in number theory, particularly in studying prime factorizations and solving Diophantine equations.
A Gaussian integer can be created using the Gauss(a, b) constructor, which represents the complex number a + bi.
SYNOPSIS
var a = Gauss(17, 19) #=> 17+19i
var b = Gauss(43, 97) #=> 43+97i
say a+b #=> 60+116i
say a-b #=> -26-78i
say a*b #=> -1112+2466i
say a/b #=> 99/433-32i/433
say a.norm #=> 650 (17² + 19²)
say a.conj #=> 17-19i
var g = Gauss(120, 84)
say g.factor #=> [Gauss(-1,1), Gauss(1,1), Gauss(1,1), Gauss(1,1), Gauss(2,1), Gauss(3,0)]
var p = Gauss(3, 2)
say p.is_prime #=> true
INHERITS
Inherits methods from:
* Sidef::Types::Number::Number
METHODS
!=
a != b
Returns true if two Gaussian integers are not equal (their real or imaginary parts differ).
Aliases: ne
%
a % b
Returns the remainder when dividing Gaussian integer a by b, using the Euclidean division algorithm for Gaussian integers.
Aliases: mod
&
a & b
Performs bitwise AND operation on the real and imaginary parts separately.
Aliases: and
*
a * b
Returns the product of two Gaussian integers. If a = (p+qi) and b = (r+si), then a*b = (pr-qs) + (ps+qr)i.
Aliases: mul
**
a ** b
Returns the Gaussian integer a raised to the power b.
Aliases: pow
+
a + b
Returns the sum of two Gaussian integers by adding their real and imaginary parts separately.
Aliases: add
++
a++
Increments the Gaussian integer by 1 (adds 1 to the real part).
Aliases: inc
-
a - b
Returns the difference of two Gaussian integers by subtracting their real and imaginary parts separately.
Aliases: sub
--
a--
Decrements the Gaussian integer by 1 (subtracts 1 from the real part).
Aliases: dec
/
a / b
Returns the quotient when dividing Gaussian integer a by b. The result may have rational real and imaginary parts.
Aliases: ÷, div
<
a < b
Compares two Gaussian integers by their norms. Returns true if the norm of a is less than the norm of b.
Aliases: lt
<<
a << b
Performs left bitwise shift on both real and imaginary parts by b positions.
Aliases: lsft, shift_left
<=>
a <=> b
Compares two Gaussian integers by their norms. Returns -1 if norm(a) < norm(b), 0 if equal, and 1 if norm(a) > norm(b).
Aliases: cmp
==
a == b
Returns true if two Gaussian integers are equal (both real and imaginary parts match).
Aliases: eq
>
a > b
Compares two Gaussian integers by their norms. Returns true if the norm of a is greater than the norm of b.
Aliases: gt
>>
a >> b
Performs right bitwise shift on both real and imaginary parts by b positions.
Aliases: rsft, shift_right
^
a ^ b
Performs bitwise XOR operation on the real and imaginary parts separately.
Aliases: xor
|
a | b
Performs bitwise OR operation on the real and imaginary parts separately.
Aliases: or
≤
a ≤ b
Returns true if the norm of a is less than or equal to the norm of b.
Aliases: <=, le
≥
a ≥ b
Returns true if the norm of a is greater than or equal to the norm of b.
Aliases: >=, ge
a
self.a
Returns the real part of the Gaussian integer.
Aliases: re, real
abs
x.abs
Returns the absolute value (magnitude) of the Gaussian integer, which is sqrt(a² + b²) where the Gaussian integer is a+bi.
b
self.b
Returns the imaginary part of the Gaussian integer.
Aliases: im, imag
ceil
x.ceil
Returns a new Gaussian integer with the ceiling function applied to both the real and imaginary parts.
conj
x.conj
Returns the complex conjugate of the Gaussian integer. If x = a+bi, returns a-bi.
divisors
n.divisors
Returns an array of all Gaussian integer divisors of n.
dump
x.dump
Returns a string representation of the Gaussian integer suitable for debugging, showing its internal structure.
eval
x.eval(v)
Evaluates the Gaussian integer in some context, replacing symbolic variables with the value v.
factor
z.factor
Returns an array of Gaussian prime factors of z. Each factor is itself a Gaussian integer.
Aliases: factors
factor_exp
z.factor_exp
Returns an array of pairs [prime, exponent] representing the prime factorization of the Gaussian integer z.
float
x.float
Converts the Gaussian integer to a complex number with floating-point real and imaginary parts.
floor
x.floor
Returns a new Gaussian integer with the floor function applied to both the real and imaginary parts.
gcd
n.gcd(k)
Returns the greatest common divisor of two Gaussian integers using the Euclidean algorithm.
gcd_norm
n.gcd_norm(k)
Returns the norm (squared magnitude) of the greatest common divisor of two Gaussian integers.
i
x.i
Returns the imaginary unit multiplied by the Gaussian integer, effectively rotating it 90 degrees in the complex plane.
iabs
x.iabs
Returns the squared absolute value (norm) of the Gaussian integer, which is a² + b² for a+bi.
inv
x.inv
Returns the multiplicative inverse of the Gaussian integer, equal to conj(x)/norm(x).
invmod
x.invmod(m)
Returns the modular multiplicative inverse of x modulo m in the Gaussian integers, if it exists.
is_coprime
n.is_coprime(k)
Returns true if Gaussian integers n and k are coprime (their GCD is a unit: ±1 or ±i).
is_div
x.is_div(y)
Returns true if Gaussian integer x is divisible by y (i.e., x/y is also a Gaussian integer).
Aliases: is_divisible
is_imag
x.is_imag
Returns true if the Gaussian integer is purely imaginary (real part is zero).
is_mone
x.is_mone
Returns true if the Gaussian integer equals -1.
is_one
x.is_one
Returns true if the Gaussian integer equals 1.
is_prime
x.is_prime
Returns true if the Gaussian integer is a Gaussian prime. A Gaussian integer is prime if it cannot be factored into non-unit Gaussian integers.
is_real
x.is_real
Returns true if the Gaussian integer is purely real (imaginary part is zero).
is_zero
x.is_zero
Returns true if the Gaussian integer equals 0+0i.
lift
x.lift
Converts the Gaussian integer to a standard complex number representation.
neg
x.neg
Returns the negation of the Gaussian integer (multiplies by -1).
new
self.new(a, b)
Gauss(a, b)
Creates a new Gaussian integer with real part a and imaginary part b.
Aliases: call
norm
x.norm
Returns the norm of the Gaussian integer, which is a² + b² for x = a+bi. This is the squared magnitude.
parts
self.parts
Returns an array containing the real and imaginary parts: [a, b] for a+bi.
powmod
x.powmod(n, m)
Returns x raised to the power n modulo m in the Gaussian integers.
pretty
x.pretty
Returns a human-readable string representation of the Gaussian integer in the form "a+bi" or "a-bi".
Aliases: stringify
reals
self.reals
Returns an array containing both the real and imaginary parts as real numbers.
round
x.round(r)
Rounds both the real and imaginary parts to r decimal places, returning a new Gaussian integer.
sgn
x.sgn
Returns the sign of the Gaussian integer as a unit Gaussian integer (one of 1, -1, i, -i) with the same argument.
sqr
x.sqr
Returns the square of the Gaussian integer (x * x).
to_c
self.to_c
Converts the Gaussian integer to a complex number.
Aliases: to_n
to_s
x.to_s
Converts the Gaussian integer to a string representation.
MATHEMATICAL BACKGROUND
Gaussian integers form a unique factorization domain, meaning every Gaussian integer can be uniquely factored into Gaussian primes (up to units and order). The units in the Gaussian integers are {1, -1, i, -i}.
A Gaussian integer a+bi is a Gaussian prime if and only if:
a = 0 and |b| is a prime number of the form 4k+3, or
b = 0 and |a| is a prime number of the form 4k+3, or
Both a and b are nonzero and a²+b² is a rational prime
The norm function N(a+bi) = a²+b² is multiplicative: N(zw) = N(z)N(w).
SEE ALSO
AUTHOR
Daniel "Trizen" Șuteu
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Sidef itself.