tensor

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

README

Tensor

Tensor and related sub-packages provide a simple yet powerful framework for representing n-dimensional data of various types, providing similar functionality to the widely used numpy and pandas libraries in python, and the commercial MATLAB framework.

  • table organizes multiple Tensors as columns in a data Table, aligned by a common row dimension as the outer-most dimension of each tensor. Because the columns are tensors, each cell (value associated with a given row) can also be n-dimensional, allowing efficient representation of patterns and other high-dimensional data. Furthermore, the entire column is organized as a single contiguous slice of data, so it can be efficiently processed. The table package also has an IndexView that provides an indexed view into the rows of the table for highly efficient filtering and sorting of data.

    Data that is encoded as a slice of structs can be bidirectionally converted to / from a Table, which then provides more powerful sorting, filtering and other functionality, including the plotview.

  • tensorview provides Core views of the Tensor and Table data.

  • stats implements a number of different ways of analyzing tensor and table data.

  • plot/plotview supports interactive plotting of Table data.

History

This package was originally developed as etable as part of the emergent software framework. It always depended on the GUI framework that became Cogent Core, and having it integrated within the Core monorepo makes it easier to integrate updates, and also makes it easier to build advanced data management and visualization applications. For example, the plot/plotview package uses the Table to support flexible and powerful plotting functionality.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToFloat64

func BoolToFloat64(bv bool) float64

func ColMajorStrides

func ColMajorStrides(sizes []int) []int

ColMajorStrides returns strides for sizes where the first dimension is inner-most and subsequent dimensions are progressively outer

func CopyDense

func CopyDense(to Tensor, dm *mat.Dense)

CopyDense copies a gonum mat.Dense matrix into given Tensor using standard Float64 interface

func EqualInts

func EqualInts(a, b []int) bool

EqualInts compares two int slices and returns true if they are equal

func Float64ToBool

func Float64ToBool(val float64) bool

func Float64ToString

func Float64ToString(val float64) string

Float64ToString converts float64 to string value using strconv, g format

func OpenCSV added in v0.1.2

func OpenCSV(tsr Tensor, filename core.Filename, delim rune) error

OpenCSV reads a tensor from a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg), using the Go standard encoding/csv reader conforming to the official CSV standard. Reads all values and assigns as many as fit.

func Projection2DCoords added in v0.1.3

func Projection2DCoords(shp *Shape, oddRow bool, row, col int) (rowCoords, colCoords []int)

Projection2DCoords returns the corresponding full-dimensional coordinates that go into the given row, col coords for a 2D projection of the given tensor, collapsing higher dimensions down to 2D (and 1D up to 2D).

func Projection2DIndex added in v0.1.3

func Projection2DIndex(shp *Shape, oddRow bool, row, col int) int

Projection2DIndex returns the flat 1D index for given row, col coords for a 2D projection of the given tensor shape, collapsing higher dimensions down to 2D (and 1D up to 2D). For any odd number of dimensions, the remaining outer-most dimension can either be multipliexed across the row or column, given the oddRow arg. Even multiples of inner-most dimensions are assumed to be row, then column. RowMajor and ColMajor layouts are handled appropriately.

func Projection2DSet added in v0.1.3

func Projection2DSet(tsr Tensor, oddRow bool, row, col int, val float64)

Projection2DSet sets a float64 value at given row, col coords for a 2D projection of the given tensor, collapsing higher dimensions down to 2D (and 1D up to 2D). For any odd number of dimensions, the remaining outer-most dimension can either be multipliexed across the row or column, given the oddRow arg. Even multiples of inner-most dimensions are assumed to be row, then column. RowMajor and ColMajor layouts are handled appropriately.

func Projection2DShape added in v0.1.3

func Projection2DShape(shp *Shape, oddRow bool) (rows, cols, rowEx, colEx int)

Projection2DShape returns the size of a 2D projection of the given tensor Shape, collapsing higher dimensions down to 2D (and 1D up to 2D). For any odd number of dimensions, the remaining outer-most dimension can either be multipliexed across the row or column, given the oddRow arg. Even multiples of inner-most dimensions are assumed to be row, then column. RowMajor and ColMajor layouts are handled appropriately. rowEx returns the number of "extra" (higher dimensional) rows and colEx returns the number of extra cols

func Projection2DValue added in v0.1.3

func Projection2DValue(tsr Tensor, oddRow bool, row, col int) float64

Projection2DValue returns the float64 value at given row, col coords for a 2D projection of the given tensor, collapsing higher dimensions down to 2D (and 1D up to 2D). For any odd number of dimensions, the remaining outer-most dimension can either be multipliexed across the row or column, given the oddRow arg. Even multiples of inner-most dimensions are assumed to be row, then column. RowMajor and ColMajor layouts are handled appropriately.

func ReadCSV added in v0.1.2

func ReadCSV(tsr Tensor, r io.Reader, delim rune) error

ReadCSV reads a tensor from a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg), using the Go standard encoding/csv reader conforming to the official CSV standard. Reads all values and assigns as many as fit.

func RowMajorStrides

func RowMajorStrides(sizes []int) []int

RowMajorStrides returns strides for sizes where the first dimension is outer-most and subsequent dimensions are progressively inner.

func SaveCSV added in v0.1.2

func SaveCSV(tsr Tensor, filename core.Filename, delim rune) error

SaveCSV writes a tensor to a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg). Outer-most dims are rows in the file, and inner-most is column -- Reading just grabs all values and doesn't care about shape.

func SetSliceLength

func SetSliceLength[S ~[]E, E any](s S, sz int) S

SetSliceLength is a utility function to set given slice to given length, reusing existing where possible and making a new one as needed.

func StringToFloat64

func StringToFloat64(str string) float64

StringToFloat64 converts string value to float64 using strconv, returning 0 if any error

func WriteCSV added in v0.1.2

func WriteCSV(tsr Tensor, w io.Writer, delim rune) error

WriteCSV writes a tensor to a comma-separated-values (CSV) file (where comma = any delimiter, specified in the delim arg). Outer-most dims are rows in the file, and inner-most is column -- Reading just grabs all values and doesn't care about shape.

Types

type Base

type Base[T any] struct {
	Shp    Shape
	Values []T
	Meta   map[string]string
}

Base is an n-dim array of float64s.

func (*Base[T]) CopyMetaData

func (tsr *Base[T]) CopyMetaData(frm Tensor)

CopyMetaData copies meta data from given source tensor

func (*Base[T]) DataType

func (tsr *Base[T]) DataType() reflect.Kind

DataType returns the type of the data elements in the tensor. Bool is returned for the Bits tensor type.

func (*Base[T]) DimSize

func (tsr *Base[T]) DimSize(dim int) int

DimSize returns size of given dimension

func (*Base[T]) Dims

func (tsr *Base[T]) Dims() (r, c int)

Dims is the gonum/mat.Matrix interface method for returning the dimensionality of the 2D Matrix. Assumes Row-major ordering and logs an error if NumDims < 2.

func (*Base[T]) Label

func (tsr *Base[T]) Label() string

Label satisfies the core.Labeler interface for a summary description of the tensor

func (*Base[T]) Len

func (tsr *Base[T]) Len() int

Len returns the number of elements in the tensor (product of shape dimensions).

func (*Base[T]) MetaData

func (tsr *Base[T]) MetaData(key string) (string, bool)

MetaData retrieves value of given key, bool = false if not set

func (*Base[T]) MetaDataMap

func (tsr *Base[T]) MetaDataMap() map[string]string

MetaDataMap returns the underlying map used for meta data

func (*Base[T]) NumDims

func (tsr *Base[T]) NumDims() int

NumDims returns the total number of dimensions.

func (*Base[T]) RowCellSize

func (tsr *Base[T]) RowCellSize() (rows, cells int)

RowCellSize returns the size of the outer-most Row shape dimension, and the size of all the remaining inner dimensions (the "cell" size). Used for Tensors that are columns in a data table.

func (*Base[T]) Set

func (tsr *Base[T]) Set(i []int, val T)

func (*Base[T]) Set1D

func (tsr *Base[T]) Set1D(i int, val T)

func (*Base[T]) SetMetaData

func (tsr *Base[T]) SetMetaData(key, val string)

SetMetaData sets a key=value meta data (stored as a map[string]string). For TensorGrid display: top-zero=+/-, odd-row=+/-, image=+/-, min, max set fixed min / max values, background=color

func (*Base[T]) SetNumRows

func (tsr *Base[T]) SetNumRows(rows int)

SetNumRows sets the number of rows (outer-most dimension) in a RowMajor organized tensor.

func (*Base[T]) SetShape

func (tsr *Base[T]) SetShape(sizes []int, names ...string)

SetShape sets the shape params, resizing backing storage appropriately

func (*Base[T]) Shape

func (tsr *Base[T]) Shape() *Shape

Shape returns a pointer to the shape that fully parameterizes the tensor shape

func (*Base[T]) String1D

func (tsr *Base[T]) String1D(off int) string

func (*Base[T]) StringRowCell

func (tsr *Base[T]) StringRowCell(row, cell int) string

func (*Base[T]) StringValue

func (tsr *Base[T]) StringValue(i []int) string

func (*Base[T]) Symmetric

func (tsr *Base[T]) Symmetric() (r int)

Symmetric is the gonum/mat.Matrix interface method for returning the dimensionality of a symmetric 2D Matrix.

func (*Base[T]) SymmetricDim

func (tsr *Base[T]) SymmetricDim() int

SymmetricDim returns the number of rows/columns in the matrix.

func (*Base[T]) Value

func (tsr *Base[T]) Value(i []int) T

func (*Base[T]) Value1D

func (tsr *Base[T]) Value1D(i int) T

type Bits

type Bits struct {
	Shp    Shape
	Values bitslice.Slice
	Meta   map[string]string
}

Bits is a tensor of bits backed by a bitslice.Slice for efficient storage of binary data

func NewBits

func NewBits(sizes []int, names ...string) *Bits

NewBits returns a new n-dimensional tensor of bit values with the given sizes per dimension (shape), and optional dimension names.

func NewBitsShape

func NewBitsShape(shape *Shape) *Bits

NewBitsShape returns a new n-dimensional tensor of bit values using given shape.

func (*Bits) At

func (tsr *Bits) At(i, j int) float64

At is the gonum/mat.Matrix interface method for returning 2D matrix element at given row, column index. Not supported for Bits -- do not call!

func (*Bits) Clone

func (tsr *Bits) Clone() Tensor

Clone clones this tensor, creating a duplicate copy of itself with its own separate memory representation of all the values, and returns that as a Tensor (which can be converted into the known type as needed).

func (*Bits) CopyCellsFrom

func (tsr *Bits) CopyCellsFrom(frm Tensor, to, start, n int)

CopyCellsFrom copies given range of values from other tensor into this tensor, using flat 1D indexes: to = starting index in this Tensor to start copying into, start = starting index on from Tensor to start copying from, and n = number of values to copy. Uses an optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*Bits) CopyFrom

func (tsr *Bits) CopyFrom(frm Tensor)

CopyFrom copies all avail values from other tensor into this tensor, with an optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*Bits) CopyMetaData

func (tsr *Bits) CopyMetaData(frm Tensor)

CopyMetaData copies meta data from given source tensor

func (*Bits) CopyShapeFrom

func (tsr *Bits) CopyShapeFrom(frm Tensor)

CopyShapeFrom copies just the shape from given source tensor calling SetShape with the shape params from source (see for more docs).

func (*Bits) DataType

func (tsr *Bits) DataType() reflect.Kind

DataType returns the type of the data elements in the tensor. Bool is returned for the Bits tensor type.

func (*Bits) DimSize

func (tsr *Bits) DimSize(dim int) int

DimSize returns size of given dimension

func (*Bits) Dims

func (tsr *Bits) Dims() (r, c int)

Dims is the gonum/mat.Matrix interface method for returning the dimensionality of the 2D Matrix. Not supported for Bits -- do not call!

func (*Bits) Float

func (tsr *Bits) Float(i []int) float64

func (*Bits) Float1D

func (tsr *Bits) Float1D(off int) float64

func (*Bits) FloatRowCell

func (tsr *Bits) FloatRowCell(row, cell int) float64

func (*Bits) Floats

func (tsr *Bits) Floats(flt *[]float64)

func (*Bits) IsString

func (tsr *Bits) IsString() bool

func (*Bits) Label

func (tsr *Bits) Label() string

Label satisfies the core.Labeler interface for a summary description of the tensor

func (*Bits) Len

func (tsr *Bits) Len() int

Len returns the number of elements in the tensor (product of shape dimensions).

func (*Bits) MetaData

func (tsr *Bits) MetaData(key string) (string, bool)

MetaData retrieves value of given key, bool = false if not set

func (*Bits) MetaDataMap

func (tsr *Bits) MetaDataMap() map[string]string

MetaDataMap returns the underlying map used for meta data

func (*Bits) NumDims

func (tsr *Bits) NumDims() int

NumDims returns the total number of dimensions.

func (*Bits) Range

func (tsr *Bits) Range() (min, max float64, minIndex, maxIndex int)

Range is not applicable to Bits tensor

func (*Bits) RowCellSize

func (tsr *Bits) RowCellSize() (rows, cells int)

RowCellSize returns the size of the outer-most Row shape dimension, and the size of all the remaining inner dimensions (the "cell" size). Used for Tensors that are columns in a data table.

func (*Bits) Set

func (tsr *Bits) Set(i []int, val bool)

func (*Bits) Set1D

func (tsr *Bits) Set1D(i int, val bool)

func (*Bits) SetFloat

func (tsr *Bits) SetFloat(i []int, val float64)

func (*Bits) SetFloat1D

func (tsr *Bits) SetFloat1D(off int, val float64)

func (*Bits) SetFloatRowCell

func (tsr *Bits) SetFloatRowCell(row, cell int, val float64)

func (*Bits) SetFloats

func (tsr *Bits) SetFloats(vals []float64)

SetFloats sets tensor values from a []float64 slice (copies values).

func (*Bits) SetMetaData

func (tsr *Bits) SetMetaData(key, val string)

SetMetaData sets a key=value meta data (stored as a map[string]string). For TensorGrid display: top-zero=+/-, odd-row=+/-, image=+/-, min, max set fixed min / max values, background=color

func (*Bits) SetNumRows

func (tsr *Bits) SetNumRows(rows int)

SetNumRows sets the number of rows (outer-most dimension) in a RowMajor organized tensor.

func (*Bits) SetShape

func (tsr *Bits) SetShape(sizes []int, names ...string)

SetShape sets the shape params, resizing backing storage appropriately

func (*Bits) SetString

func (tsr *Bits) SetString(i []int, val string)

func (*Bits) SetString1D

func (tsr *Bits) SetString1D(off int, val string)

func (*Bits) SetStringRowCell

func (tsr *Bits) SetStringRowCell(row, cell int, val string)

func (*Bits) SetZeros

func (tsr *Bits) SetZeros()

SetZeros is simple convenience function initialize all values to 0

func (*Bits) Shape

func (tsr *Bits) Shape() *Shape

Shape returns a pointer to the shape that fully parameterizes the tensor shape

func (*Bits) String

func (tsr *Bits) String() string

String satisfies the fmt.Stringer interface for string of tensor data

func (*Bits) String1D

func (tsr *Bits) String1D(off int) string

func (*Bits) StringRowCell

func (tsr *Bits) StringRowCell(row, cell int) string

func (*Bits) StringValue

func (tsr *Bits) StringValue(i []int) string

func (*Bits) SubSpace

func (tsr *Bits) SubSpace(offs []int) Tensor

SubSpace is not possible with Bits

func (*Bits) T

func (tsr *Bits) T() mat.Matrix

T is the gonum/mat.Matrix transpose method. Not supported for Bits -- do not call!

func (*Bits) Value

func (tsr *Bits) Value(i []int) bool

Value returns value at given tensor index

func (*Bits) Value1D

func (tsr *Bits) Value1D(i int) bool

Value1D returns value at given tensor 1D (flat) index

type Byte

type Byte = Number[byte]

Byte is an alias for Number[byte].

func NewByte added in v0.1.2

func NewByte(sizes []int, names ...string) *Byte

NewByte returns a new Byte tensor with the given sizes per dimension (shape), and optional dimension names.

type Float32

type Float32 = Number[float32]

Float32 is an alias for Number[float32].

func NewFloat32 added in v0.1.2

func NewFloat32(sizes []int, names ...string) *Float32

NewFloat32 returns a new Float32 tensor with the given sizes per dimension (shape), and optional dimension names.

type Float64

type Float64 = Number[float64]

Float64 is an alias for Number[float64].

func NewFloat64 added in v0.1.2

func NewFloat64(sizes []int, names ...string) *Float64

NewFloat64 returns a new Float64 tensor with the given sizes per dimension (shape), and optional dimension names.

type Int

type Int = Number[int]

Int is an alias for Number[int].

func NewInt added in v0.1.2

func NewInt(sizes []int, names ...string) *Int

NewInt returns a new Int tensor with the given sizes per dimension (shape), and optional dimension names.

type Int32

type Int32 = Number[int32]

Int32 is an alias for Number[int32].

func NewInt32 added in v0.1.2

func NewInt32(sizes []int, names ...string) *Int32

NewInt32 returns a new Int32 tensor with the given sizes per dimension (shape), and optional dimension names.

type Number

type Number[T num.Number] struct {
	Base[T]
}

Number is a tensor of numerical values

func NewNumber

func NewNumber[T num.Number](sizes []int, names ...string) *Number[T]

NewNumber returns a new n-dimensional tensor of numerical values with the given sizes per dimension (shape), and optional dimension names.

func NewNumberShape

func NewNumberShape[T num.Number](shape *Shape) *Number[T]

NewNumberShape returns a new n-dimensional tensor of numerical values using given shape.

func (*Number[T]) AddScalar

func (tsr *Number[T]) AddScalar(i []int, val float64) float64

func (*Number[T]) At

func (tsr *Number[T]) At(i, j int) float64

At is the gonum/mat.Matrix interface method for returning 2D matrix element at given row, column index. Assumes Row-major ordering and logs an error if NumDims < 2.

func (*Number[T]) Clone

func (tsr *Number[T]) Clone() Tensor

Clone clones this tensor, creating a duplicate copy of itself with its own separate memory representation of all the values, and returns that as a Tensor (which can be converted into the known type as needed).

func (*Number[T]) CopyCellsFrom

func (tsr *Number[T]) CopyCellsFrom(frm Tensor, to, start, n int)

CopyCellsFrom copies given range of values from other tensor into this tensor, using flat 1D indexes: to = starting index in this Tensor to start copying into, start = starting index on from Tensor to start copying from, and n = number of values to copy. Uses an optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*Number[T]) CopyFrom

func (tsr *Number[T]) CopyFrom(frm Tensor)

CopyFrom copies all avail values from other tensor into this tensor, with an optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*Number[T]) CopyShapeFrom

func (tsr *Number[T]) CopyShapeFrom(frm Tensor)

CopyShapeFrom copies just the shape from given source tensor calling SetShape with the shape params from source (see for more docs).

func (*Number[T]) Float

func (tsr *Number[T]) Float(i []int) float64

func (*Number[T]) Float1D

func (tsr *Number[T]) Float1D(i int) float64

func (*Number[T]) FloatRowCell

func (tsr *Number[T]) FloatRowCell(row, cell int) float64

func (*Number[T]) Floats

func (tsr *Number[T]) Floats(flt *[]float64)

Floats sets []float64 slice of all elements in the tensor (length is ensured to be sufficient). This can be used for all of the gonum/floats methods for basic math, gonum/stats, etc.

func (*Number[T]) IsString

func (tsr *Number[T]) IsString() bool

func (*Number[T]) MulScalar

func (tsr *Number[T]) MulScalar(i []int, val float64) float64

func (*Number[T]) Range

func (tsr *Number[T]) Range() (min, max float64, minIndex, maxIndex int)

Range returns the min, max (and associated indexes, -1 = no values) for the tensor. This is needed for display and is thus in the core api in optimized form Other math operations can be done using gonum/floats package.

func (*Number[T]) SetFloat

func (tsr *Number[T]) SetFloat(i []int, val float64)

func (*Number[T]) SetFloat1D

func (tsr *Number[T]) SetFloat1D(i int, val float64)

func (*Number[T]) SetFloatRowCell

func (tsr *Number[T]) SetFloatRowCell(row, cell int, val float64)

func (*Number[T]) SetFloats

func (tsr *Number[T]) SetFloats(flt []float64)

SetFloats sets tensor values from a []float64 slice (copies values).

func (*Number[T]) SetString

func (tsr *Number[T]) SetString(i []int, val string)

func (Number[T]) SetString1D

func (tsr Number[T]) SetString1D(off int, val string)

func (*Number[T]) SetStringRowCell

func (tsr *Number[T]) SetStringRowCell(row, cell int, val string)

func (*Number[T]) SetZeros

func (tsr *Number[T]) SetZeros()

SetZeros is simple convenience function initialize all values to 0

func (*Number[T]) String

func (tsr *Number[T]) String() string

String satisfies the fmt.Stringer interface for string of tensor data

func (*Number[T]) SubSpace

func (tsr *Number[T]) SubSpace(offs []int) Tensor

SubSpace returns a new tensor with innermost subspace at given offset(s) in outermost dimension(s) (len(offs) < NumDims). The new tensor points to the values of the this tensor (i.e., modifications will affect both), as its Values slice is a view onto the original (which is why only inner-most contiguous supsaces are supported). Use Clone() method to separate the two.

func (*Number[T]) T

func (tsr *Number[T]) T() mat.Matrix

T is the gonum/mat.Matrix transpose method. It performs an implicit transpose by returning the receiver inside a Transpose.

type Shape

type Shape struct {

	// size per dimension
	Sizes []int

	// offsets for each dimension
	Strides []int `view:"-"`

	// names of each dimension
	Names []string `view:"-"`
}

Shape manages a tensor's shape information, including strides and dimension names and can compute the flat index into an underlying 1D data storage array based on an n-dimensional index (and vice-versa). Per C / Go / Python conventions, indexes are Row-Major, ordered from outer to inner left-to-right, so the inner-most is right-most.

func AddShapes added in v0.1.2

func AddShapes(shape1, shape2 *Shape) *Shape

AddShapes returns a new shape by adding two shapes one after the other.

func NewShape

func NewShape(sizes []int, names ...string) *Shape

NewShape returns a new shape with given sizes and optional dimension names. RowMajor ordering is used by default.

func (*Shape) CopyShape

func (sh *Shape) CopyShape(cp *Shape)

CopyShape copies the shape parameters from another Shape struct. copies the data so it is not accidentally subject to updates.

func (*Shape) DimByName

func (sh *Shape) DimByName(name string) int

DimByName returns the index of the given dimension name. returns -1 if not found.

func (*Shape) DimName

func (sh *Shape) DimName(i int) string

DimName returns the name of given dimension.

func (*Shape) DimSize added in v0.1.2

func (sh *Shape) DimSize(i int) int

DimSize returns the size of given dimension.

func (*Shape) DimSizeByName added in v0.1.2

func (sh *Shape) DimSizeByName(name string) int

DimSizeByName returns the size of given dimension, specified by name. will crash if name not found.

func (*Shape) Index

func (sh *Shape) Index(offset int) []int

Index returns the n-dimensional index from a "flat" 1D array index.

func (*Shape) IndexIsValid

func (sh *Shape) IndexIsValid(idx []int) bool

IndexIsValid() returns true if given index is valid (within ranges for all dimensions)

func (*Shape) IsEqual

func (sh *Shape) IsEqual(oth *Shape) bool

IsEqual returns true if this shape is same as other (does not compare names)

func (*Shape) Len

func (sh *Shape) Len() int

Len returns the total length of elements in the tensor (i.e., the product of the shape sizes)

func (*Shape) NumDims

func (sh *Shape) NumDims() int

NumDims returns the total number of dimensions.

func (*Shape) Offset

func (sh *Shape) Offset(index []int) int

Offset returns the "flat" 1D array index into an element at the given n-dimensional index. No checking is done on the length or size of the index values relative to the shape of the tensor.

func (*Shape) RowCellSize

func (sh *Shape) RowCellSize() (rows, cells int)

RowCellSize returns the size of the outer-most Row shape dimension, and the size of all the remaining inner dimensions (the "cell" size). Used for Tensors that are columns in a data table.

func (*Shape) SetShape

func (sh *Shape) SetShape(sizes []int, names ...string)

SetShape sets the shape size and optional names RowMajor ordering is used by default.

func (*Shape) String

func (sh *Shape) String() string

String satisfies the fmt.Stringer interface

type String

type String struct {
	Base[string]
}

String is a tensor of string values

func NewString

func NewString(sizes []int, names ...string) *String

NewString returns a new n-dimensional tensor of string values with the given sizes per dimension (shape), and optional dimension names.

func NewStringShape

func NewStringShape(shape *Shape) *String

NewStringShape returns a new n-dimensional tensor of string values using given shape.

func (*String) AddScalar

func (tsr *String) AddScalar(i []int, val float64) float64

func (*String) At

func (tsr *String) At(i, j int) float64

At is the gonum/mat.Matrix interface method for returning 2D matrix element at given row, column index. Assumes Row-major ordering and logs an error if NumDims < 2.

func (*String) Clone

func (tsr *String) Clone() Tensor

Clone clones this tensor, creating a duplicate copy of itself with its own separate memory representation of all the values, and returns that as a Tensor (which can be converted into the known type as needed).

func (*String) CopyCellsFrom

func (tsr *String) CopyCellsFrom(frm Tensor, to, start, n int)

CopyCellsFrom copies given range of values from other tensor into this tensor, using flat 1D indexes: to = starting index in this Tensor to start copying into, start = starting index on from Tensor to start copying from, and n = number of values to copy. Uses an optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*String) CopyFrom

func (tsr *String) CopyFrom(frm Tensor)

CopyFrom copies all avail values from other tensor into this tensor, with an optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*String) CopyShapeFrom

func (tsr *String) CopyShapeFrom(frm Tensor)

CopyShapeFrom copies just the shape from given source tensor calling SetShape with the shape params from source (see for more docs).

func (*String) Float

func (tsr *String) Float(i []int) float64

func (*String) Float1D

func (tsr *String) Float1D(off int) float64

func (*String) FloatRowCell

func (tsr *String) FloatRowCell(row, cell int) float64

func (*String) Floats

func (tsr *String) Floats(flt *[]float64)

Floats sets []float64 slice of all elements in the tensor (length is ensured to be sufficient). This can be used for all of the gonum/floats methods for basic math, gonum/stats, etc.

func (*String) IsString

func (tsr *String) IsString() bool

func (*String) MulScalar

func (tsr *String) MulScalar(i []int, val float64) float64

func (*String) Range

func (tsr *String) Range() (min, max float64, minIndex, maxIndex int)

Range returns the min, max (and associated indexes, -1 = no values) for the tensor. This is needed for display and is thus in the core api in optimized form Other math operations can be done using gonum/floats package.

func (*String) SetFloat

func (tsr *String) SetFloat(i []int, val float64)

func (*String) SetFloat1D

func (tsr *String) SetFloat1D(off int, val float64)

func (*String) SetFloatRowCell

func (tsr *String) SetFloatRowCell(row, cell int, val float64)

func (*String) SetFloats

func (tsr *String) SetFloats(flt []float64)

SetFloats sets tensor values from a []float64 slice (copies values).

func (*String) SetString

func (tsr *String) SetString(i []int, val string)

func (String) SetString1D

func (tsr String) SetString1D(off int, val string)

func (*String) SetStringRowCell

func (tsr *String) SetStringRowCell(row, cell int, val string)

func (*String) SetZeros

func (tsr *String) SetZeros()

SetZeros is simple convenience function initialize all values to 0

func (*String) String

func (tsr *String) String() string

String satisfies the fmt.Stringer interface for string of tensor data

func (*String) SubSpace

func (tsr *String) SubSpace(offs []int) Tensor

SubSpace returns a new tensor with innermost subspace at given offset(s) in outermost dimension(s) (len(offs) < NumDims). The new tensor points to the values of the this tensor (i.e., modifications will affect both), as its Values slice is a view onto the original (which is why only inner-most contiguous supsaces are supported). Use Clone() method to separate the two.

func (*String) T

func (tsr *String) T() mat.Matrix

T is the gonum/mat.Matrix transpose method. It performs an implicit transpose by returning the receiver inside a Transpose.

type Tensor

type Tensor interface {
	fmt.Stringer
	mat.Matrix

	// Shape returns a pointer to the shape that fully parameterizes the tensor shape
	Shape() *Shape

	// Len returns the number of elements in the tensor (product of shape dimensions).
	Len() int

	// NumDims returns the total number of dimensions.
	NumDims() int

	// DimSize returns size of given dimension
	DimSize(dim int) int

	// RowCellSize returns the size of the outer-most Row shape dimension,
	// and the size of all the remaining inner dimensions (the "cell" size).
	// Used for Tensors that are columns in a data table.
	RowCellSize() (rows, cells int)

	// DataType returns the type of the data elements in the tensor.
	// Bool is returned for the Bits tensor type.
	DataType() reflect.Kind

	// returns true if the data type is a String. otherwise is numeric.
	IsString() bool

	// Float returns the value of given index as a float64.
	Float(i []int) float64

	// SetFloat sets the value of given index as a float64
	SetFloat(i []int, val float64)

	// StringValue returns the value of given index as a string
	StringValue(i []int) string

	// SetString sets the value of given index as a string
	SetString(i []int, val string)

	// Float1D returns the value of given 1-dimensional index (0-Len()-1) as a float64
	Float1D(i int) float64

	// SetFloat1D sets the value of given 1-dimensional index (0-Len()-1) as a float64
	SetFloat1D(i int, val float64)

	// FloatRowCell returns the value at given row and cell, where row is outer-most dim,
	// and cell is 1D index into remaining inner dims. For Table columns.
	FloatRowCell(row, cell int) float64

	// SetFloatRowCell sets the value at given row and cell, where row is outer-most dim,
	// and cell is 1D index into remaining inner dims. For Table columns.
	SetFloatRowCell(row, cell int, val float64)

	// Floats sets []float64 slice of all elements in the tensor
	// (length is ensured to be sufficient).
	// This can be used for all of the gonum/floats methods
	// for basic math, gonum/stats, etc.
	Floats(flt *[]float64)

	// SetFloats sets tensor values from a []float64 slice (copies values).
	SetFloats(vals []float64)

	// String1D returns the value of given 1-dimensional index (0-Len()-1) as a string
	String1D(i int) string

	// SetString1D sets the value of given 1-dimensional index (0-Len()-1) as a string
	SetString1D(i int, val string)

	// StringRowCell returns the value at given row and cell, where row is outer-most dim,
	// and cell is 1D index into remaining inner dims. For Table columns
	StringRowCell(row, cell int) string

	// SetStringRowCell sets the value at given row and cell, where row is outer-most dim,
	// and cell is 1D index into remaining inner dims. For Table columns
	SetStringRowCell(row, cell int, val string)

	// SubSpace returns a new tensor with innermost subspace at given
	// offset(s) in outermost dimension(s) (len(offs) < NumDims).
	// The new tensor points to the values of the this tensor (i.e., modifications
	// will affect both), as its Values slice is a view onto the original (which
	// is why only inner-most contiguous supsaces are supported).
	// Use Clone() method to separate the two.
	SubSpace(offs []int) Tensor

	// Range returns the min, max (and associated indexes, -1 = no values) for the tensor.
	// This is needed for display and is thus in the core api in optimized form
	// Other math operations can be done using gonum/floats package.
	Range() (min, max float64, minIndex, maxIndex int)

	// SetZeros is simple convenience function initialize all values to 0
	SetZeros()

	// Clone clones this tensor, creating a duplicate copy of itself with its
	// own separate memory representation of all the values, and returns
	// that as a Tensor (which can be converted into the known type as needed).
	Clone() Tensor

	// CopyFrom copies all avail values from other tensor into this tensor, with an
	// optimized implementation if the other tensor is of the same type, and
	// otherwise it goes through appropriate standard type.
	CopyFrom(from Tensor)

	// CopyShapeFrom copies just the shape from given source tensor
	// calling SetShape with the shape params from source (see for more docs).
	CopyShapeFrom(from Tensor)

	// CopyCellsFrom copies given range of values from other tensor into this tensor,
	// using flat 1D indexes: to = starting index in this Tensor to start copying into,
	// start = starting index on from Tensor to start copying from, and n = number of
	// values to copy.  Uses an optimized implementation if the other tensor is
	// of the same type, and otherwise it goes through appropriate standard type.
	CopyCellsFrom(from Tensor, to, start, n int)

	// SetShape sets the sizes parameters of the tensor, and resizes backing storage appropriately.
	// existing names will be preserved if not presented.
	SetShape(sizes []int, names ...string)

	// SetNumRows sets the number of rows (outer-most dimension).
	SetNumRows(rows int)

	// SetMetaData sets a key=value meta data (stored as a map[string]string).
	// For TensorGrid display: top-zero=+/-, odd-row=+/-, image=+/-,
	// min, max set fixed min / max values, background=color
	SetMetaData(key, val string)

	// MetaData retrieves value of given key, bool = false if not set
	MetaData(key string) (string, bool)

	// MetaDataMap returns the underlying map used for meta data
	MetaDataMap() map[string]string

	// CopyMetaData copies meta data from given source tensor
	CopyMetaData(from Tensor)
}

Tensor is the interface for n-dimensional tensors. Per C / Go / Python conventions, indexes are Row-Major, ordered from outer to inner left-to-right, so the inner-most is right-most. It is implemented by the Base and Number generic types specialized by different concrete types: float64, float32, int, int32, byte, string, bits (bools). For float32 and float64 values, use NaN to indicate missing values. All of the data analysis and plot packages skip NaNs.

func New

func New[T string | bool | float32 | float64 | int | int32 | byte](sizes []int, names ...string) Tensor

New returns a new n-dimensional tensor of given value type with the given sizes per dimension (shape), and optional dimension names.

func NewOfType

func NewOfType(typ reflect.Kind, sizes []int, names ...string) Tensor

NewOfType returns a new n-dimensional tensor of given reflect.Kind type with the given sizes per dimension (shape), and optional dimension names. Supported types are string, bool (for Bits), float32, float64, int, int32, and byte.

Directories

Path Synopsis
Package bitslice implements a simple slice-of-bits using a []byte slice for storage, which is used for efficient storage of boolean data, such as projection connectivity patterns.
Package bitslice implements a simple slice-of-bits using a []byte slice for storage, which is used for efficient storage of boolean data, such as projection connectivity patterns.
cmd
examples
Package agg provides aggregation functions operating on IndexView indexed views of table.Table data, along with standard AggFunc functions that can be used at any level of aggregation from tensor on up.
Package agg provides aggregation functions operating on IndexView indexed views of table.Table data, along with standard AggFunc functions that can be used at any level of aggregation from tensor on up.
metric
Package metric provides various similarity / distance metrics for comparing floating-point vectors.
Package metric provides various similarity / distance metrics for comparing floating-point vectors.
norm
Package norm provides normalization and norm metric computations e.g., L2 = sqrt of sum of squares of a vector.
Package norm provides normalization and norm metric computations e.g., L2 = sqrt of sum of squares of a vector.
pca
Package pca performs principal component's analysis and associated covariance matrix computations, operating on table.Table or tensor.Tensor data.
Package pca performs principal component's analysis and associated covariance matrix computations, operating on table.Table or tensor.Tensor data.
simat
Package simat provides similarity / distance matrix functions that create a SimMat matrix from Tensor or Table data.
Package simat provides similarity / distance matrix functions that create a SimMat matrix from Tensor or Table data.
split
Package split provides GroupBy, Agg, Permute and other functions that create and populate Splits of table.Table data.
Package split provides GroupBy, Agg, Permute and other functions that create and populate Splits of table.Table data.
Package tensormpi wraps the Message Passing Interface for distributed memory data sharing across a collection of processors (procs).
Package tensormpi wraps the Message Passing Interface for distributed memory data sharing across a collection of processors (procs).
Package tensorview provides GUI Views of table Table and Tensor structures using the Cogent Core Views framework.
Package tensorview provides GUI Views of table Table and Tensor structures using the Cogent Core Views framework.

Jump to

Keyboard shortcuts

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