matrix

package
v0.0.0-...-c1fb209 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eps

func Eps(eps ...float64) float64

Types

type Matrix

type Matrix [][]complex128

Matrix type manages complex numbers in matrices.

func AntiCommutator

func AntiCommutator(m0, m1 Matrix) Matrix

func Commutator

func Commutator(m0, m1 Matrix) Matrix

func New

func New(v ...[]complex128) Matrix

New creates a new matrix

func TensorProduct

func TensorProduct(m ...Matrix) Matrix

TensorProduct implements the product for any number of matrices.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

func main() {
	m := matrix.New(
		[]complex128{1 + 1i, 2 + 2i},
		[]complex128{3 + 3i, 0 + 0i},
	)
	mM := matrix.TensorProduct(m, m)
	for _, r := range mM {
		fmt.Println(r)
	}
}
Output:

[(0+2i) (0+4i) (0+4i) (0+8i)]
[(0+6i) (0+0i) (0+12i) (0+0i)]
[(0+6i) (0+12i) (0+0i) (0+0i)]
[(0+18i) (0+0i) (0+0i) (0+0i)]

func TensorProductN

func TensorProductN(m Matrix, bit ...int) Matrix

TensorProductN returns a matrix whose elements are the product of the elements by themselves N times.

func (Matrix) Add

func (m0 Matrix) Add(m1 Matrix) Matrix

Add returns a matrix whose elements are the sum of the two matrices' elements.

func (Matrix) Apply

func (m0 Matrix) Apply(m1 Matrix) Matrix

Apply returns a matrix that is the result of aplying the two matrices together.

func (Matrix) Clone

func (m0 Matrix) Clone() Matrix

Clone returns a clone of the matrix.

func (Matrix) Conjugate

func (m0 Matrix) Conjugate() Matrix

Conjugate returns the matrix conjugated.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()
	mC := m.Conjugate()
	for _, r := range mC {
		fmt.Println(r)
	}
}
Output:

[(1+0i) (2+0i) (3+0i) (4+0i)]
[(0+0i) (1+0i) (1+1i) (2+2i)]
[(0+0i) (0+0i) (1+0i) (1+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]

[(1-0i) (2-0i) (3-0i) (4-0i)]
[(0-0i) (1-0i) (1-1i) (2-2i)]
[(0-0i) (0-0i) (1-0i) (1-0i)]
[(0-0i) (0-0i) (0-0i) (1-0i)]

func (Matrix) Dagger

func (m0 Matrix) Dagger() Matrix

Dagger returns the matrix transposed and conjugated.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()
	mD := m.Dagger()
	for _, r := range mD {
		fmt.Println(r)
	}
}
Output:

[(1+0i) (2+0i) (3+0i) (4+0i)]
[(0+0i) (1+0i) (1+1i) (2+2i)]
[(0+0i) (0+0i) (1+0i) (1+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]

[(1-0i) (0-0i) (0-0i) (0-0i)]
[(2-0i) (1-0i) (0-0i) (0-0i)]
[(3-0i) (1-1i) (1-0i) (0-0i)]
[(4-0i) (2-2i) (1-0i) (1-0i)]

func (Matrix) Dimension

func (m0 Matrix) Dimension() (int, int)

Dimension returns the dimensions of the matrix.

func (Matrix) Equals

func (m0 Matrix) Equals(m1 Matrix, eps ...float64) bool

Equals returns true if the matrices are equals.

func (Matrix) Inverse

func (m0 Matrix) Inverse() Matrix

Inverse returns the inverse of the matrix.

func (Matrix) IsHermite

func (m0 Matrix) IsHermite(eps ...float64) bool

IsHermite returns true if the matrix is equal to its conjugated transposed.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	mH := matrix.New(
		[]complex128{2 + 0i, 2 + 1i, 4 + 2i},
		[]complex128{2 - 1i, 3 + 0i, 3 + 3i},
		[]complex128{4 - 2i, 3 - 3i, 3 + 0i},
	)
	fmt.Println(m.IsHermite())
	fmt.Println(mH.IsHermite())
}
Output:

false
true

func (Matrix) IsUnitary

func (m0 Matrix) IsUnitary(eps ...float64) bool

IsUnitary returns true if the conjugate transpose of the matrix is equal to its inverse.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	fmt.Println(m.IsUnitary())
	mU := matrix.New(
		[]complex128{1, 0, 0},
		[]complex128{0, 1, 0},
		[]complex128{0, 0, 1},
	)
	mU1 := matrix.New(
		[]complex128{0.5 + 0.5i, 0.5 - 0.5i},
		[]complex128{0.4 - 0.5i, 0.5 + 0.5i},
	)
	fmt.Println(mU.IsUnitary())
	fmt.Println(mU1.IsUnitary(0.1))
}
Output:

false
true
true

func (Matrix) Mul

func (m0 Matrix) Mul(z complex128) Matrix

Mul returns a matrix whose elements are the product of the argument by the original elements.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	mMul := m.Mul(2 + 1i)
	for _, r := range mMul {
		fmt.Println(r)
	}
}
Output:

[(2+1i) (4+2i) (6+3i) (8+4i)]
[(0+0i) (2+1i) (1+3i) (2+6i)]
[(0+0i) (0+0i) (2+1i) (2+1i)]
[(0+0i) (0+0i) (0+0i) (2+1i)]

func (Matrix) Sub

func (m0 Matrix) Sub(m1 Matrix) Matrix

Sub returns a matrix whose elements are the difference between the first matrix elements and the second matrix elements.

func (Matrix) TensorProduct

func (m0 Matrix) TensorProduct(m1 Matrix) Matrix

TensorProduct returns a matrix whose elements are the tensor product of the two matrices.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

func main() {
	m := matrix.New(
		[]complex128{1 + 1i, 2 + 2i},
		[]complex128{3 + 3i, 0 + 0i},
	)
	mTP := m.TensorProduct(m)
	for _, r := range mTP {
		fmt.Println(r)
	}
}
Output:

[(0+2i) (0+4i) (0+4i) (0+8i)]
[(0+6i) (0+0i) (0+12i) (0+0i)]
[(0+6i) (0+12i) (0+0i) (0+0i)]
[(0+18i) (0+0i) (0+0i) (0+0i)]

func (Matrix) Trace

func (m0 Matrix) Trace() complex128

Trace returns the sum of matrix diagonal components.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	fmt.Println(m.Trace())
}
Output:

(4+0i)

func (Matrix) Transpose

func (m0 Matrix) Transpose() Matrix

Transpose returns the matrix transposed.

Example
package main

import (
	"fmt"

	"github.com/axamon/q/matrix"
)

var m = matrix.New(
	[]complex128{1, 2, 3, 4},
	[]complex128{0, 1, 1 + 1i, 2 + 2i},
	[]complex128{0, 0, 1, 1},
	[]complex128{0, 0, 0, 1},
)

func main() {
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()
	mt := m.Transpose()
	for _, r := range mt {
		fmt.Println(r)
	}
}
Output:

[(1+0i) (2+0i) (3+0i) (4+0i)]
[(0+0i) (1+0i) (1+1i) (2+2i)]
[(0+0i) (0+0i) (1+0i) (1+0i)]
[(0+0i) (0+0i) (0+0i) (1+0i)]

[(1+0i) (0+0i) (0+0i) (0+0i)]
[(2+0i) (1+0i) (0+0i) (0+0i)]
[(3+0i) (1+1i) (1+0i) (0+0i)]
[(4+0i) (2+2i) (1+0i) (1+0i)]

Jump to

Keyboard shortcuts

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