complex

package module
Version: v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2022 License: MIT Imports: 2 Imported by: 0

README

Big Complex

Go Reference Go Report Card Coverage

Big complex number calculation library for Go (with math/big).

Currently, the library supports:

  1. Gaussian integer, complex numbers whose real and imaginary parts are both integers:

    gaussian_int

  2. Hurwitz quaternion, quaternions whose components are either all integers or all half-integers (halves of odd integers; a mixture of integers and half-integers is excluded):

    hurwitz_int

Installation

go get -u github.com/tommytim0515/go-bigcomplex

Examples

The usage is quite similar to Golang math/big package.

package main

import (
   "fmt"
   "math/big"

   bc "github.com/tommytim0515/go-bigcomplex"
)

func main() {
   // Gaussian integer calculation
   g1 := bc.NewGaussianInt(big.NewInt(5), big.NewInt(6)) // 5 + 6i
   g2 := bc.NewGaussianInt(big.NewInt(1), big.NewInt(2)) // 1 + 2i
   div := new(bc.GaussianInt).Div(g2, g1)
   fmt.Println(div) // 3 - i
   gcd := new(bc.GaussianInt).GCD(g1, g2)
   fmt.Println(gcd) // i

   // Hurwitz integer calculation
   // 1 + i + j + k
   h1 := bc.NewHurwitzInt(big.NewInt(1), big.NewInt(1), big.NewInt(1), big.NewInt(1), false)
   // 3/2 + i + j + 3k/2
   h2 := bc.NewHurwitzInt(big.NewInt(3), big.NewInt(2), big.NewInt(2), big.NewInt(3), true)
   prod := new(bc.HurwitzInt).Prod(h1, h2)
   fmt.Println(prod) // 2 + 3i + 2j + 3k
}

Why This Library?

Fan fact: Golang has native complex number types: complex64 and complex128.

c1 := complex(10, 11) // constructor init
c2 := 10 + 11i        // complex number init syntax

realPart := real(c1)    // gets real part
imagPart := imag(c1)    // gets imaginary part

complex64 represents float64 real and imaginary data, and complex128 represents float128 real and imaginary data. They are easy to use, but unfortunately they are incapable for handling very large complex numbers.

For instance, in finding the Lagrange Four Square Sum of a very large integer (1792 bits in size) for cryptographic range proof, we need to compute the Greatest Common Divisor (GCD) of Gaussian integers and the Greatest Common Right Divisor of Hurwitz integers. And the built-in complex number types can not handle such large numbers.

So I came up with the idea of building a library for large complex number calculation with Golang math/big package.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GaussianInt

type GaussianInt struct {
	R *big.Int // real part
	I *big.Int // imaginary part
}

GaussianInt implements Gaussian integer In number theory, a Gaussian integer is a complex number whose real and imaginary parts are both integers

func NewGaussianInt

func NewGaussianInt(r *big.Int, i *big.Int) *GaussianInt

NewGaussianInt declares a new Gaussian integer with the real part and imaginary part

func (*GaussianInt) Add

func (g *GaussianInt) Add(a, b *GaussianInt) *GaussianInt

Add adds two Gaussian integers

func (*GaussianInt) CmpNorm

func (g *GaussianInt) CmpNorm(a *GaussianInt) int

CmpNorm compares the norm of two Gaussian integers

func (*GaussianInt) Conj

func (g *GaussianInt) Conj(origin *GaussianInt) *GaussianInt

Conj obtains the conjugate of the original Gaussian integer

func (*GaussianInt) Copy

func (g *GaussianInt) Copy() *GaussianInt

Copy copies the Gaussian integer

func (*GaussianInt) Div

func (g *GaussianInt) Div(a, b *GaussianInt) *GaussianInt

Div performs Euclidean division of two Gaussian integers, i.e. a/b the remainder is stored in the Gaussian integer that calls the method the quotient is returned as a new Gaussian integer

func (*GaussianInt) Equals added in v0.1.2

func (g *GaussianInt) Equals(a *GaussianInt) bool

Equals checks if two Gaussian integers are equal

func (*GaussianInt) GCD

func (g *GaussianInt) GCD(a, b *GaussianInt) *GaussianInt

GCD calculates the greatest common divisor of two Gaussian integers using Euclidean algorithm the result is stored in the Gaussian integer that calls the method and returned

func (*GaussianInt) IsOne

func (g *GaussianInt) IsOne() bool

IsOne returns true if the Gaussian integer is equal to one

func (*GaussianInt) IsZero

func (g *GaussianInt) IsZero() bool

IsZero returns true if the Gaussian integer is equal to zero

func (*GaussianInt) Norm

func (g *GaussianInt) Norm() *big.Int

Norm obtains the norm of the Gaussian integer

func (*GaussianInt) Prod

func (g *GaussianInt) Prod(a, b *GaussianInt) *GaussianInt

Prod returns the products of two Gaussian integers

func (*GaussianInt) Set

func (g *GaussianInt) Set(a *GaussianInt) *GaussianInt

Set sets the Gaussian integer to the given Gaussian integer

func (*GaussianInt) String

func (g *GaussianInt) String() string

String returns the string representation of the Gaussian integer

func (*GaussianInt) Sub

func (g *GaussianInt) Sub(a, b *GaussianInt) *GaussianInt

Sub subtracts two Gaussian integers

func (*GaussianInt) Update

func (g *GaussianInt) Update(r, i *big.Int) *GaussianInt

Update updates the Gaussian integer with the given real and imaginary parts

type HurwitzInt

type HurwitzInt struct {
	// contains filtered or unexported fields
}

HurwitzInt implements Hurwitz quaternion (or Hurwitz integer) a + bi + cj + dk The set of all Hurwitz quaternion is H = {a + bi + cj + dk | a, b, c, d are all integers or all half-integers} A mixture of integers and half-integers is excluded In the struct each scalar is twice the original scalar so that all the scalars can be stored using big integers for computation efficiency

func NewHurwitzInt

func NewHurwitzInt(r, i, j, k *big.Int, doubled bool) *HurwitzInt

NewHurwitzInt declares a new integral quaternion with the real, i, j, and k parts If isDouble is true, the arguments r, i, j, k are twice the original scalars

func (*HurwitzInt) Add

func (h *HurwitzInt) Add(a, b *HurwitzInt) *HurwitzInt

Add adds two integral quaternions

func (*HurwitzInt) CmpNorm

func (h *HurwitzInt) CmpNorm(a *HurwitzInt) int

CmpNorm compares the norm of two Hurwitz integers

func (*HurwitzInt) Conj

func (h *HurwitzInt) Conj(origin *HurwitzInt) *HurwitzInt

Conj obtains the conjugate of the original integral quaternion

func (*HurwitzInt) Copy

func (h *HurwitzInt) Copy() *HurwitzInt

Copy copies the integral quaternion

func (*HurwitzInt) Div

func (h *HurwitzInt) Div(a, b *HurwitzInt) *HurwitzInt

Div performs Euclidean division of two Hurwitz integers, i.e. a/b the remainder is stored in the Hurwitz integer that calls the method the quotient is returned as a new Hurwitz integer

func (*HurwitzInt) Equals added in v0.1.2

func (h *HurwitzInt) Equals(a *HurwitzInt) bool

Equals checks if the two Hurwitz integers are equal

func (*HurwitzInt) GCRD

func (h *HurwitzInt) GCRD(a, b *HurwitzInt) *HurwitzInt

GCRD calculates the greatest common right-divisor of two Hurwitz integers using Euclidean algorithm The GCD is unique only up to multiplication by a unit (multiplication on the left in the case of a GCRD, and on the right in the case of a GCLD) the result is stored in the Hurwitz integer that calls the method and returned

func (*HurwitzInt) Init

func (h *HurwitzInt) Init() *HurwitzInt

Init initialize a Hurwitz integer

func (*HurwitzInt) IsZero

func (h *HurwitzInt) IsZero() bool

IsZero returns true if the Hurwitz integer is zero

func (*HurwitzInt) Norm

func (h *HurwitzInt) Norm() *big.Int

Norm obtains the norm of the integral quaternion

func (*HurwitzInt) Prod

func (h *HurwitzInt) Prod(a, b *HurwitzInt) *HurwitzInt

Prod returns the Hamilton product of two integral quaternions the product (a1 + b1j + c1k + d1)(a2 + b2j + c2k + d2) is determined by the products of the basis elements and the distributive law

func (*HurwitzInt) Set

func (h *HurwitzInt) Set(a *HurwitzInt) *HurwitzInt

Set sets the Hurwitz integer to the given Hurwitz integer

func (*HurwitzInt) String

func (h *HurwitzInt) String() string

String returns the string representation of the integral quaternion

func (*HurwitzInt) Sub

func (h *HurwitzInt) Sub(a, b *HurwitzInt) *HurwitzInt

Sub subtracts two integral quaternions

func (*HurwitzInt) Update

func (h *HurwitzInt) Update(r, i, j, k *big.Int, doubled bool) *HurwitzInt

Update updates the integral quaternion with the given real, i, j, and k parts

func (*HurwitzInt) Val

func (h *HurwitzInt) Val() (r, i, j, k *big.Float)

Val reveals value of a Hurwitz integer

func (*HurwitzInt) ValInt

func (h *HurwitzInt) ValInt() (r, i, j, k *big.Int)

ValInt reveals value of a Hurwitz integer in integer

func (*HurwitzInt) Zero

func (h *HurwitzInt) Zero() *HurwitzInt

Zero sets the Hurwitz integer to zero

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL