tensor

package module
v0.0.0-...-302291f Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2025 License: BSD-3-Clause Imports: 12 Imported by: 2

README

Tensor package Go Reference

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

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

func Eig(eigvals, eigvecs, a *Dense, bufs [3]*Dense) error

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

func InverseIteration(q, a *Dense, mu complex64, bufs []*Dense) (complex64, error)

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

func Overlap(x, y []complex64) (z []complex64)

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

func SameArray(x, y []complex64) bool

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

func Contract(c, a, b *Dense, axes [][2]int) *Dense

Contract returns the tensor contraction of a and b over the specified axes, whose result is stored in c.

func MatMul

func MatMul(c, a, b *Dense) *Dense

MatMul returns matrix multiplication of a and b, whose result is stored in c.

func QR

func QR(q, a *Dense, bufs [2]*Dense, options ...QROptions) *Dense

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

func ReadMatrixMarket(r io.Reader) (*Dense, error)

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 Scalar

func Scalar(c complex64) *Dense

Scalar creates a scalar tensor.

func T1

func T1(slice []complex64) *Dense

T1 creates a tensor from a 1-D slice.

func T2

func T2(slice [][]complex64) *Dense

T2 creates a tensor from a 2-D slice.

func T3

func T3(slice [][][]complex64) *Dense

T3 creates a tensor from a 3-D slice.

func T4

func T4(slice [][][][]complex64) *Dense

T4 creates a tensor from a 4-D slice.

func Zeros

func Zeros(shape ...int) *Dense

Zeros returns a zero tensor with the specified shape.

func (*Dense) Add

func (a *Dense) Add(c complex64, b *Dense) *Dense

Add performs a = a + c*b.

func (*Dense) All

func (t *Dense) All() func(yield func([]int, complex64) bool)

All returns an iterator over values in t.

func (*Dense) At

func (t *Dense) At(digits ...int) complex64

At returns the value of t at position digits.

func (*Dense) Conj

func (a *Dense) Conj() *Dense

Conj returns the complex conjugate of a tensor.

func (*Dense) Equal

func (a *Dense) Equal(b *Dense, tol float32) error

Equal returns whether a and b are equal within tolerance.

func (*Dense) Eye

func (t *Dense) Eye(n, k int) *Dense

Eye creates an identity matrix with ones at the k-th diagonal.

func (*Dense) FrobeniusNorm

func (t *Dense) FrobeniusNorm() float32

FrobeniusNorm returns the FrobniusNorm of t.

func (*Dense) H

func (t *Dense) H() *Dense

H returns the Hermitian adjoint of t.

func (*Dense) InfNorm

func (a *Dense) InfNorm() float32

InfNorm returns the infinity norm of matrix a.

func (*Dense) Mul

func (x *Dense) Mul(c complex64) *Dense

Mul multiplies x with c.

func (*Dense) Reset

func (t *Dense) Reset(shape ...int) *Dense

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.

func (*Dense) Reshape

func (a *Dense) Reshape(shape ...int) *Dense

Reshape reshapes a tensor to the specified shape.

func (*Dense) Set

func (t *Dense) Set(start []int, a *Dense) *Dense

Set performs t[start0:end0, start1:end1, ...] = a, where end = start + a.shape.

func (*Dense) SetAt

func (t *Dense) SetAt(digits []int, c complex64)

SetAt sets the value of t at position digits to c.

func (*Dense) Shape

func (t *Dense) Shape() []int

Shape returns the shape of t.

func (*Dense) Slice

func (a *Dense) Slice(boundary [][2]int) *Dense

Slice returns a[b00:b01, b10:b11, b20:b21, ...].

func (*Dense) T1

func (t *Dense) T1(slice []complex64) *Dense

T1 resets t to a 1-D slice.

func (*Dense) T2

func (t *Dense) T2(slice [][]complex64) *Dense

T2 resets t to a 2-D slice.

func (*Dense) T3

func (t *Dense) T3(slice [][][]complex64) *Dense

T3 resets t to a 3-D slice.

func (*Dense) T4

func (t *Dense) T4(slice [][][][]complex64) *Dense

T4 resets t to a 4-D slice.

func (*Dense) ToSlice1

func (t *Dense) ToSlice1() []complex64

ToSlice1 returns t as a 1-D slice.

func (*Dense) ToSlice2

func (t *Dense) ToSlice2() [][]complex64

ToSlice2 returns t as a 2-D slice.

func (*Dense) Transpose

func (a *Dense) Transpose(axis ...int) *Dense

Transpose returns a tensor with axes transposed.

func (*Dense) Tril

func (t *Dense) Tril(k int) *Dense

Tril zeros elements above the k-th diagonal.

func (*Dense) Triu

func (t *Dense) Triu(k int) *Dense

Triu zeros elements below the k-th diagonal.

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.

Jump to

Keyboard shortcuts

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