Documentation
¶
Overview ¶
Package tensor implements multi-dimensional tensors on complex numbers. Tensors support contraction as well as the usual slicing, transposing, and reshaping operations. In addition, this package provides functions to:
- Find the top k eigenpairs of a large matrix
- Find all eigenpairs of a matrix
- Perform the Singular Value Decomposition
- Perform the QR Decomposition
Example ¶
package main import ( "fmt" "log" "math/cmplx" "github.com/fumin/tensor" ) func main() { a := tensor.T3([][][]complex64{ {{-6i, 5}, {-1 + 1i, -1 - 1i}, {4, -3 + 3i}}, {{0, 0}, {0, 0}, {0, 0}}, }) // Slice and reshape. b := a.Slice([][2]int{{0, 1}, {0, 3}, {0, 2}}) b = b.Reshape(3, 2) fmt.Println("Slice and reshape", b.ToSlice2()) // Transpose and update. b = b.Transpose(1, 0) b = b.Slice([][2]int{{0, 2}, {1, 3}}) b.SetAt([]int{0, 1}, -2-2i) fmt.Println("Transpose and update", b.ToSlice2()) // Calculate eigenvalues of b. bufs := [3]*tensor.Dense{tensor.Zeros(1), tensor.Zeros(1), tensor.Zeros(1)} eigvals, eigvecs := tensor.Zeros(1), tensor.Zeros(1) if err := tensor.Eig(eigvals, eigvecs, b, bufs); err != nil { log.Fatalf("%v", err) } for i, vi := range []complex64{-3 + 1i, -1 + 3i} { if abs(eigvals.At(i)-vi) < 1e-6 { fmt.Printf("Eigenvalue %d: %v\n", i, vi) } } } func abs(x complex64) float64 { return cmplx.Abs(complex128(x)) }
Output: Slice and reshape [[(0-6i) (5+0i)] [(-1+1i) (-1-1i)] [(4+0i) (-3+3i)]] Transpose and update [[(-1+1i) (-2-2i)] [(-1-1i) (-3+3i)]] Eigenvalue 0: (-3+1i) Eigenvalue 1: (-1+3i)
Index ¶
- func Arnoldi(eigvals, eigvecs, a *Dense, k int, buffers [7]*Dense, ...) error
- func Eig(eigvals, eigvecs, a *Dense, bufs [3]*Dense) error
- func InverseIteration(q, a *Dense, mu complex64, bufs []*Dense) (complex64, error)
- func Overlap(x, y []complex64) (z []complex64)
- func SameArray(x, y []complex64) bool
- type ArnoldiOptions
- type Dense
- func Contract(c, a, b *Dense, axes [][2]int) *Dense
- func MatMul(c, a, b *Dense) *Dense
- func QR(q, a *Dense, bufs [2]*Dense, options ...QROptions) *Dense
- func ReadMatrixMarket(r io.Reader) (*Dense, error)
- func SVD(u, v, s *Dense, bufs [3]*Dense, options ...SVDOptions) (*Dense, error)
- func Scalar(c complex64) *Dense
- func T1(slice []complex64) *Dense
- func T2(slice [][]complex64) *Dense
- func T3(slice [][][]complex64) *Dense
- func T4(slice [][][][]complex64) *Dense
- func Zeros(shape ...int) *Dense
- func (a *Dense) Add(c complex64, b *Dense) *Dense
- func (t *Dense) All() func(yield func([]int, complex64) bool)
- func (t *Dense) At(digits ...int) complex64
- func (a *Dense) Conj() *Dense
- func (a *Dense) Equal(b *Dense, tol float32) error
- func (t *Dense) Eye(n, k int) *Dense
- func (t *Dense) FrobeniusNorm() float32
- func (t *Dense) H() *Dense
- func (a *Dense) InfNorm() float32
- func (x *Dense) Mul(c complex64) *Dense
- func (t *Dense) Reset(shape ...int) *Dense
- func (a *Dense) Reshape(shape ...int) *Dense
- func (t *Dense) Set(start []int, a *Dense) *Dense
- func (t *Dense) SetAt(digits []int, c complex64)
- func (t *Dense) Shape() []int
- func (a *Dense) Slice(boundary [][2]int) *Dense
- func (t *Dense) T1(slice []complex64) *Dense
- func (t *Dense) T2(slice [][]complex64) *Dense
- func (t *Dense) T3(slice [][][]complex64) *Dense
- func (t *Dense) T4(slice [][][][]complex64) *Dense
- func (t *Dense) ToSlice1() []complex64
- func (t *Dense) ToSlice2() [][]complex64
- func (a *Dense) Transpose(axis ...int) *Dense
- func (t *Dense) Tril(k int) *Dense
- func (t *Dense) Triu(k int) *Dense
- type QROptions
- type SVDOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Arnoldi ¶
func Arnoldi(eigvals, eigvecs, a *Dense, k int, buffers [7]*Dense, options ...ArnoldiOptions) error
Arnoldi performs the Arnoldi iteration for finding the k eigenvalues with the smallest real part.
func Eig ¶
Eig finds the eigenvalues of matrix a, which are stored in eigvals. eigvals are sorted from small to large of their real parts. If eigvecs is not nil, it is set to the corresponding eigenvectors. Matrix a is modified upon return.
func InverseIteration ¶
InverseIteration computes the eigenvector whose eigenvalue is closest to mu. See Section 7.6.1 Selected Eigenvectors via Inverse Iteration, Matrix Computations 4th Ed., G. H. Golub, C. F. Van Loan.
func Overlap ¶
Overlap returns a slice pointing to the overlapping memory between x and y. Nil is returned if x and y do not share the same underlying array or if they do not overlap.
func SameArray ¶
SameArray returns true if x and y share the same underlying array. Sharing the same underlying array does not imply overlap, but rather that it is possible to reslice one or the other such that both point to the same memory region. For more details, please see https://groups.google.com/g/golang-nuts/c/ks1jvoyMYuc.
Types ¶
type ArnoldiOptions ¶
type ArnoldiOptions struct {
// contains filtered or unexported fields
}
ArnoldiOptions are options for performing Arnoldi iteration.
func NewArnoldiOptions ¶
func NewArnoldiOptions() ArnoldiOptions
NewArnoldiOptions returns the default Arnoldi iteration options.
func (ArnoldiOptions) KrylovSpaceDim ¶
func (opt ArnoldiOptions) KrylovSpaceDim(v int) ArnoldiOptions
KrylovSpaceDim sets the specified Krylov space dimension.
func (ArnoldiOptions) MaxIterations ¶
func (opt ArnoldiOptions) MaxIterations(v int) ArnoldiOptions
MaxIterations sets the maximum iterations.
type Dense ¶
type Dense struct {
// contains filtered or unexported fields
}
A Dense is a complex multi-dimensional tensor.
func Contract ¶
Contract returns the tensor contraction of a and b over the specified axes, whose result is stored in c.
func QR ¶
QR performs the QR decomposition of matrix a. QR returns the Upper Triangular matrix R, and q is set to the orthogonal matrix Q. Matrix a is modified upon return.
func ReadMatrixMarket ¶
ReadMatrixMarket reads a matrix in Matrix Market format.
func SVD ¶
func SVD(u, v, s *Dense, bufs [3]*Dense, options ...SVDOptions) (*Dense, error)
SVD performs the Singular Value Decomposition. SVD returns the diagonal matrix S holding the singular values, and u, v are set to the orthogonal matrices U and V. Matrix s is modified upon return.
func (*Dense) FrobeniusNorm ¶
FrobeniusNorm returns the FrobniusNorm of t.
func (*Dense) Reset ¶
Reset resets t to a zero tensor of specified shape. While the backing slice is the same, t is reset to be neither sliced nor transposed.
type QROptions ¶
type QROptions struct { // Full specifies whether to return the full orthogonal basis. Full bool }
QROptions are options for the QR decomposition.
type SVDOptions ¶
type SVDOptions struct { // Full specifies whether to return the full orthogonal basis. Full bool }
SVDOptions are options for the Singular Value Decomposition.