shmensor

package
v0.0.0-...-277eea6 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Copyright 2019 Google LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Apply

type Apply struct {
	Func Function
	E    Evaluator
}

Apply contains a function and an Evaluator interface to apply it to.

func (Apply) Eval

func (as Apply) Eval() (Tensor, error, *Profiler)

type Evaluator

type Evaluator interface {
	Eval() (Tensor, error, *Profiler)
}

type Expression

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

An Expression is not a Tensor. It's an Expression symbolizing a desired combination of tensor products and contractions on Tensors. When you evaluate it, you get a Tensor. Abstract index notation with Einstein summation.

func (Expression) D

func (e Expression) D(indices string) Expression

func (Expression) Eval

func (e Expression) Eval() (Tensor, error, *Profiler)

More pedestrian eval functions

func (Expression) U

func (e Expression) U(indices string) Expression

type Function

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

Function is a function that can be applied to every element of a Tensor. As of now, a caller can't make their own. They have to use a constructor from the types file which ensures types are aligned.

func NewRealFunction

func NewRealFunction(f func(r float64) float64) Function

func NewRealScalar

func NewRealScalar(f float64) Function

func NewStringFunction

func NewStringFunction(f func(s string) string) Function

type Plus

type Plus struct {
	A, B Evaluator
}

Plus contains the sum of two Terms.

func (Plus) Eval

func (ps Plus) Eval() (Tensor, error, *Profiler)

type Profiler

type Profiler struct {
	Multiplies int
	Adds       int
	Mutex      bool
	TraceCache int
}

Profiler collects metrics about Tensor evaluation for debugging and be

func (*Profiler) String

func (p *Profiler) String() string

Pretty printing.

type Tensor

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

A Tensor is not a matrix. It's a tensor. A Tensor is a multi-dimensional array that transforms according to the co and contravariant. tensor transformation laws.

func NewComplexTensor

func NewComplexTensor(f func(i ...int) complex128, signature string, dim []int) Tensor

func NewIntTensor

func NewIntTensor(f func(i ...int) int, signature string, dim []int) Tensor

func NewRealTensor

func NewRealTensor(f func(i ...int) float64, signature string, dim []int) Tensor

func NewStringTensor

func NewStringTensor(f func(i ...int) string, signature string, dim []int) Tensor

func Product

func Product(t1, t2 Tensor, profiler *Profiler) Tensor

func Trace

func Trace(t Tensor, a, b int, profiler *Profiler) (Tensor, error)

Trace is a contraction on two indices. eventually should forbid callers from accessing directly and/or verify indices exist to contract and are same dimensions.

func Transpose

func Transpose(t Tensor, a, b int) (Tensor, error)

Transpose swaps two tensor indices.

func (Tensor) ContravariantIndices

func (t Tensor) ContravariantIndices() []int

func (Tensor) CovariantIndices

func (t Tensor) CovariantIndices() []int

func (*Tensor) D

func (t *Tensor) D(indices string) Expression

func (Tensor) Dimension

func (t Tensor) Dimension() []int

func (Tensor) Eval

func (t Tensor) Eval() (Tensor, error, *Profiler)

func (Tensor) Reify

func (t Tensor) Reify() [][]interface{}

This method is the one most badly in need of testing. "Reify" is one of *many* possible ways to visualize a tensor as a matrix. The horizontal and vertical indices correspond to walking the covariant and contravariant indices of the tensor.

E.g. If a tensor has 3 contravariant indices of dimension 1 | 2 \ 3 and 1 covariant index of dimension 4, Reify will produce a 4 by (1*2*3)=6 matrix, with the indices sorted. Do not treat this as a real matrix it's merely for convenience.

func (*Tensor) Reshape

func (t *Tensor) Reshape(signature string)

func (Tensor) Signature

func (t Tensor) Signature() string

Some getters.

func (Tensor) String

func (t Tensor) String() string

Pretty printing.

func (*Tensor) U

func (t *Tensor) U(indices string) Expression

Like t.U("ij").D("k").U("a").D("b") Like t1.U("jk").U("arb").D("xyz") Eval(t, t1) Eval(t.U("ij").D("k").U("a").D("b"), t1.U("jk").U("arb").D("xyz")) Mat multiply example Eval(t1.U("i"), t2.D("j")) Transpose like Eval(t1.I().U("j").D("i"))

type Term

type Term struct {
	List []Expression
}

A Term is a list of Expressions. It represents a single term of tensor products and contractions in abstract index notation.

func E

func E(i ...Expression) Term

E wraps up a bunch of expressions into a term.

func (Term) Eval

func (term Term) Eval() (Tensor, error, *Profiler)

Eval takes a list of expressions representing a solo or product term of tensors in abstract index notation and returns the resulting Tensor.

Note that shmensor Tensors are lazy, so computation is only performed when you call Reify().

Consider verbose mode boolean to explore what's happening.

type Type

type Type interface {
	// Take two things, like numbers, get a new thing.
	Multiply(interface{}, interface{}) interface{}
	// Take two things, like numbers, get a new thing.
	Add(interface{}, interface{}) interface{}
}

Type defines a ring element. To implement the interface define multiplication and addition of the ring elements.

Several default Tensor types and initializer functions are defined at the end of the file.

Note that most applications expect the inputs and output to be of the same type. You have to enforce that at object construction.

Jump to

Keyboard shortcuts

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