tensor

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: BSD-3-Clause Imports: 29 Imported by: 22

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.

The Goal augmented version of the Go language directly supports NumPy-like operations on tensors. A Tensor is comparable to the NumPy ndarray type, and it provides the universal representation of a homogenous data type throughout all the packages here, from scalar to vector, matrix and beyond. All functions take and return Tensor arguments.

See the Cogent Lab Docs for full documentation.

Design discussion

The Tensor interface is implemented at the basic level with n-dimensional indexing into flat Go slices of any numeric data type (by Number), along with String, and Bool (which uses bitslice for maximum efficiency). These implementations satisfy the Values sub-interface of Tensor, which supports the most direct and efficient operations on contiguous memory data. The Shape type provides all the n-dimensional indexing with arbitrary strides to allow any ordering, although row major is the default and other orders have to be manually imposed.

In addition, there are five important "view" implementations of Tensor that wrap another "source" Tensor to provide more flexible and efficient access to the data, consistent with the NumPy functionality. See Basic and Advanced Indexing below for more info.

  • Sliced provides a sub-sliced view into the wrapped Tensor source, using an indexed list along each dimension. Thus, it can provide a reordered and filtered view onto the raw data, and it has a well-defined shape in terms of the number of indexes per dimension. This corresponds to the NumPy basic sliced indexing model.

  • Masked provides a Bool masked view onto each element in the wrapped Tensor, where the two maintain the same shape). Any cell with a false value in the bool mask returns a NaN (missing data), and Set functions are no-ops. The stats packages treat NaN as missing data, but tmath, vector, and matrix packages do not, so it is best to call .AsValues() on masked data prior to operating on it, in a basic math context (i.e., copy in Goal).

  • Indexed has a tensor of indexes into the source data, where the final, innermost dimension of the indexes is the same size as the number of dimensions in the wrapped source tensor. The overall shape of this view is that of the remaining outer dimensions of the Indexes tensor, and like other views, assignment and return values are taken from the corresponding indexed value in the wrapped source tensor.

  • Reshaped applies a different Shape to the source tensor, with the constraint that the new shape has the same length of total elements as the source tensor. It is particularly useful for aligning different tensors binary operation between them produces the desired results, for example by adding a new axis or collapsing multiple dimensions into one.

  • Rows is a specialized version of Sliced that provides a row index-based view, with the Indexes applying to the outermost row dimension, which allows sorting and filtering to operate only on the indexes, leaving the underlying Tensor unchanged. This view is returned by the table data table, which organizes multiple heterogenous Tensor columns along a common outer row dimension, and provides similar functionality to pandas and particularly xarray in Python.

Note that any view can be "stacked" on top of another, to produce more complex net views.

Each view type implements the AsValues method to create a concrete "rendered" version of the view (as a Values tensor) where the actual underlying data is organized as it appears in the view. This is like the copy function in NumPy, disconnecting the view from the original source data. Note that unlike NumPy, Masked and Indexed remain views into the underlying source data -- see Basic and Advanced Indexing below.

The float64 ("Float"), int ("Int"), and string ("String") types are used as universal input / output types, and for intermediate computation in the math functions. Any performance-critical code can be optimized for a specific data type, but these universal interfaces are suitable for misc ad-hoc data analysis.

There is also a RowMajor sub-interface for tensors (implemented by the Values and Rows types), which supports [Set]FloatRow[Cell] methods that provide optimized access to row major data. See Standard shapes for more info.

The Vectorize function and its variants provide a universal "apply function to tensor data" mechanism (often called a "map" function, but that name is already taken in Go). It takes an N function that determines how many indexes to iterate over (and this function can also do any initialization prior to iterating), a compute function that gets the current index value, and a varargs list of tensors. In general it is completely up to the compute function how to interpret the index, although we also support the "broadcasting" principles from NumPy for binary functions operating on two tensors, as discussed below. There is a Threaded version of this for parallelizable functions, and a GPU version in the gosl Go-as-a-shading-language package.

To support the best possible performance in compute-intensive code, we have written all the core tensor functions in an Out suffixed version that takes the output tensor as an additional input argument (it must be a Values type), which allows an appropriately sized tensor to be used to hold the outputs on repeated function calls, instead of requiring new memory allocations every time. These versions are used in other calls where appropriate. The function without the Out suffix just wraps the Out version, and is what is called directly by Goal, where the output return value is essential for proper chaining of operations.

To support proper argument handling for tensor functions, the goal transpiler registers all tensor package functions into the global name-to-function map (tensor.Funcs), which is used to retrieve the function by name, along with relevant arg metadata. This registry is also key for enum sets of functions, in the stats and metrics packages, for example, to be able to call the corresponding function. Goal uses symbols collected in the yaegicore package to populate the Funcs, but enums should directly add themselves to ensure they are always available even outside of Goal.

  • table organizes multiple Tensors as columns in a data Table, aligned by a common outer row dimension. 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. A Table automatically supplies a shared list of row Indexes for its Indexed columns, efficiently allowing all the heterogeneous data columns to be sorted and filtered together.

    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 plot/plotcore.

  • tensorfs provides a virtual filesystem (FS) for organizing arbitrary collections of data, supporting interactive, ad-hoc (notebook style) as well as systematic data processing. Interactive goal shell commands (cd, ls, mkdir etc) can be used to navigate the data space, with numerical expressions immediately available to operate on the data and save results back to the filesystem. Furthermore, the data can be directly copied to / from the OS filesystem to persist it, and goal can transparently access data on remote systems through ssh. Furthermore, the databrowser provides a fully interactive GUI for inspecting and plotting data.

  • tensorcore provides core widgets for graphically displaying the Tensor and Table data, which are used in tensorfs.

  • tmath implements all standard math functions on tensor.Indexed data, including the standard +, -, *, / operators. goal then calls these functions.

  • plot/plotcore supports interactive plotting of Table data.

  • bitslice is a Go slice of bytes []byte that has methods for setting individual bits, as if it was a slice of bools, while being 8x more memory efficient. This is used for encoding null entries in etensor, and as a Tensor of bool / bits there as well, and is generally very useful for binary (boolean) data.

  • stats implements a number of different ways of analyzing tensor and table data, including:

    • cluster implements agglomerative clustering of items based on metric distance / similarity matrix data.
    • convolve convolves data (e.g., for smoothing).
    • glm fits a general linear model for one or more dependent variables as a function of one or more independent variables. This encompasses all forms of regression.
    • histogram bins data into groups and reports the frequency of elements in the bins.
    • metric computes similarity / distance metrics for comparing two tensors, and associated distance / similarity matrix functions, including PCA and SVD analysis functions that operate on a covariance matrix.
    • stats provides a set of standard summary statistics on a range of different data types, including basic slices of floats, to tensor and table data. It also includes the ability to extract Groups of values and generate statistics for each group, as in a "pivot table" in a spreadsheet.

Standard shapes

There are various standard shapes of tensor data that different functions expect, listed below. The two most general-purpose functions for shaping and slicing any tensor to get it into the right shape for a given computation are:

  • Reshape returns a Reshaped view with the same total length as the source tensor, functioning like the NumPy reshape function.

  • Reslice returns a re-sliced view of a tensor, extracting or rearranging dimenstions. It supports the full NumPy basic indexing syntax. It also does reshaping as needed, including processing the NewAxis option.

  • Flat, 1D: this is the simplest data shape, and any tensor can be turned into a flat 1D list using NewReshaped(-1) or the As1D function, which either returns the tensor itself it is already 1D, or a Reshaped 1D view. The stats functions for example report summary statistics across the outermost row dimension, so converting data to this 1D view gives stats across all the data.

  • Row, Cell 2D: This is the natural shape for tabular data, and the RowMajor type and Rows view provide methods for efficiently accessing data in this way. In addition, the stats and metric packages automatically compute statistics across the outermost row dimension, aggregating results across rows for each cell. Thus, you end up with the "average cell-wise pattern" when you do stats.Mean for example. The NewRowCellsView function returns a Reshaped view of any tensor organized into this 2D shape, with the row vs. cell split specified at any point in the list of dimensions, which can be useful in obtaining the desired results.

  • Matrix 2D: For matrix algebra functions, a 2D tensor is treated as a standard row-major 2D matrix, which can be processed using gonum based matrix and vector operations, as in the matrix package.

  • Matrix 3+D: For functions that specifically process 2D matricies, a 3+D shape can be used as well, which iterates over the outer dimensions to process the inner 2D matricies.

Dynamic row sizing (e.g., for logs)

The SetNumRows function can be used to progressively increase the number of rows to fit more data, as is typically the case when logging data (often using a table). You can set the row dimension to 0 to start -- that is (now) safe. However, for greatest efficiency, it is best to set the number of rows to the largest expected size first, and then set it back to 0. The underlying slice of data retains its capacity when sized back down. During incremental increasing of the slice size, if it runs out of capacity, all the elements need to be copied, so it is more efficient to establish the capacity up front instead of having multiple incremental re-allocations.

Printing format

The following are examples of tensor printing via the Sprintf function, which is used with default values for the String() stringer method on tensors. It does a 2D projection of higher-dimensional tensors, using the Projection2D set of functions, which assume a row-wise outermost dimension in general, and pack even sets of inner dimensions into 2D row x col shapes (see examples below).

1D (vector): goes column-wise, and wraps around as needed, e.g., length = 4:

[4] 0 1 2 3 

and 40:

[40]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
     25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 

2D matrix:

[4 3]
    [0] [1] [2] 
[0]   0   1   2 
[1]  10  11  12 
[2]  20  21  22 
[3]  30  31  32 

and a column vector (2nd dimension is 1):

[4 1]
[0] 0 
[1] 1 
[2] 2 
[3] 3 

3D tensor, shape = [4 3 2] -- note the [r r c] legend below the shape which indicates which dimensions are shown on the row (r) vs column (c) axis, so you know how to interpret the indexes:

[4 3 2]
[r r c] [0] [1] 
[0 0]     0   1 
[0 1]    10  11 
[0 2]    20  21 
[1 0]   100 101 
[1 1]   110 111 
[1 2]   120 121 
[2 0]   200 201 
[2 1]   210 211 
[2 2]   220 221 
[3 0]   300 301 
[3 1]   310 311 
[3 2]   320 321 

4D tensor: note how the row, column dimensions alternate, resulting in a 2D layout of the outer 2 dimensions, with another 2D layout of the inner 2D dimensions inlaid within that:

[5 4 3 2]
[r c r c] [0 0] [0 1] [1 0] [1 1] [2 0] [2 1] [3 0] [3 1] 
[0 0]         0     1   100   101   200   201   300   301 
[0 1]        10    11   110   111   210   211   310   311 
[0 2]        20    21   120   121   220   221   320   321 
[1 0]      1000  1001  1100  1101  1200  1201  1300  1301 
[1 1]      1010  1011  1110  1111  1210  1211  1310  1311 
[1 2]      1020  1021  1120  1121  1220  1221  1320  1321 
[2 0]      2000  2001  2100  2101  2200  2201  2300  2301 
[2 1]      2010  2011  2110  2111  2210  2211  2310  2311 
[2 2]      2020  2021  2120  2121  2220  2221  2320  2321 
[3 0]      3000  3001  3100  3101  3200  3201  3300  3301 
[3 1]      3010  3011  3110  3111  3210  3211  3310  3311 
[3 2]      3020  3021  3120  3121  3220  3221  3320  3321 
[4 0]      4000  4001  4100  4101  4200  4201  4300  4301 
[4 1]      4010  4011  4110  4111  4210  4211  4310  4311 
[4 2]      4020  4021  4120  4121  4220  4221  4320  4321 

5D tensor: is treated like a 4D with the outermost dimension being an additional row dimension:

[6 5 4 3 2]
[r r c r c] [0 0] [0 1] [1 0] [1 1] [2 0] [2 1] [3 0] [3 1] 
[0 0 0]         0     1   100   101   200   201   300   301 
[0 0 1]        10    11   110   111   210   211   310   311 
[0 0 2]        20    21   120   121   220   221   320   321 
[0 1 0]      1000  1001  1100  1101  1200  1201  1300  1301 
[0 1 1]      1010  1011  1110  1111  1210  1211  1310  1311 
[0 1 2]      1020  1021  1120  1121  1220  1221  1320  1321 
[0 2 0]      2000  2001  2100  2101  2200  2201  2300  2301 
[0 2 1]      2010  2011  2110  2111  2210  2211  2310  2311 
[0 2 2]      2020  2021  2120  2121  2220  2221  2320  2321 
[0 3 0]      3000  3001  3100  3101  3200  3201  3300  3301 
[0 3 1]      3010  3011  3110  3111  3210  3211  3310  3311 
[0 3 2]      3020  3021  3120  3121  3220  3221  3320  3321 
[0 4 0]      4000  4001  4100  4101  4200  4201  4300  4301 
[0 4 1]      4010  4011  4110  4111  4210  4211  4310  4311 
[0 4 2]      4020  4021  4120  4121  4220  4221  4320  4321 
[1 0 0]     10000 10001 10100 10101 10200 10201 10300 10301 
[1 0 1]     10010 10011 10110 10111 10210 10211 10310 10311 
[1 0 2]     10020 10021 10120 10121 10220 10221 10320 10321 
[1 1 0]     11000 11001 11100 11101 11200 11201 11300 11301 
[1 1 1]     11010 11011 11110 11111 11210 11211 11310 11311 
...

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/plotcore package uses the Table to support flexible and powerful plotting functionality.

It was completely rewritten in Sept 2024 to use a single data type (tensor.Indexed) and call signature for compute functions taking these args, to provide a simple and efficient data processing framework that greatly simplified the code and enables the goal language to directly transpile simplified math expressions into corresponding tensor compute code.

Documentation

Index

Constants

View Source
const (
	// OnedRow is for onedRow arguments to Projection2D functions,
	// specifies that the 1D case goes along the row.
	OnedRow = true

	// OnedColumn is for onedRow arguments to Projection2D functions,
	// specifies that the 1D case goes along the column.
	OnedColumn = false
)
View Source
const (
	// Ascending specifies an ascending sort direction for tensor Sort routines
	Ascending = true

	// Descending specifies a descending sort direction for tensor Sort routines
	Descending = false

	// StableSort specifies using stable, original order-preserving sort, which is slower.
	StableSort = true

	// Unstable specifies using faster but unstable sorting.
	UnstableSort = false
)

Variables

View Source
var (
	// ThreadingThreshod is the threshold in number of flops (floating point ops),
	// computed as tensor N * flops per element, to engage actual parallel processing.
	// Heuristically, numbers below this threshold do not result in
	// an overall speedup, due to overhead costs. See tmath/ops_test.go for benchmark.
	ThreadingThreshold = 300

	// NumThreads is the number of threads to use for parallel threading.
	// The default of 0 causes the [runtime.GOMAXPROCS] to be used.
	NumThreads = 0
)
View Source
var Funcs map[string]*Func

Funcs is the global tensor named function registry. All functions must have a signature like this: func([opt any,] a, b, out tensor.Tensor) error i.e., taking some specific number of Tensor arguments (up to 5), with the number of output vs. input arguments registered. Functions can also take an 'any' first argument to handle other non-tensor inputs (e.g., function pointer, dirfs directory, etc). This is used to make tensor functions available to the Goal language.

View Source
var MaxPrintLineWidth = 80

MaxPrintLineWidth is the maximum line width in characters to generate for tensor Sprintf function.

View Source
var MaxSprintLength = 1000

MaxSprintLength is the default maximum length of a String() representation of a tensor, as generated by the Sprint function. Defaults to 1000.

Functions

func AddFunc

func AddFunc(name string, fun any) error

AddFunc adds given named function to the global tensor named function registry, which is used by Goal to call functions by name. See NewFunc for more informa.tion.

func AsFloat64Scalar

func AsFloat64Scalar(tsr Tensor) float64

AsFloat64Scalar returns the first value of tensor as a float64 scalar. Returns 0 if no values.

func AsFloat64Slice

func AsFloat64Slice(tsr Tensor) []float64

AsFloat64Slice returns all the tensor values as a slice of float64's. This allocates a new slice for the return values, and is not a good option for performance-critical code.

func AsIntScalar

func AsIntScalar(tsr Tensor) int

AsIntScalar returns the first value of tensor as an int scalar. Returns 0 if no values.

func AsIntSlice

func AsIntSlice(tsr Tensor) []int

AsIntSlice returns all the tensor values as a slice of ints. This allocates a new slice for the return values, and is not a good option for performance-critical code.

func AsStringScalar

func AsStringScalar(tsr Tensor) string

AsStringScalar returns the first value of tensor as a string scalar. Returns "" if no values.

func AsStringSlice

func AsStringSlice(tsr Tensor) []string

AsStringSlice returns all the tensor values as a slice of strings. This allocates a new slice for the return values, and is not a good option for performance-critical code.

func BoolFloatsFuncOut

func BoolFloatsFuncOut(fun func(a, b float64) bool, a, b Tensor, out *Bool) error

BoolFloatsFuncOut sets boolean output value based on a function involving float64 values from the two tensors.

func BoolIntsFuncOut

func BoolIntsFuncOut(fun func(a, b int) bool, a, b Tensor, out *Bool) error

BoolIntsFuncOut sets boolean output value based on a function involving int values from the two tensors.

func BoolStringsFuncOut

func BoolStringsFuncOut(fun func(a, b string) bool, a, b Tensor, out *Bool) error

BoolStringsFuncOut sets boolean output value based on a function involving string values from the two tensors.

func BoolToFloat64

func BoolToFloat64(bv bool) float64

BoolToFloat64 converts bool to float64 value.

func BoolToInt

func BoolToInt(bv bool) int

BoolToInt converts bool to int value.

func Calc

func Calc(tsr Tensor) error

Calc calls function set by SetCalcFunc to compute an updated value for given tensor. Returns an error if func not set, or any error from func itself. Function is stored as CalcFunc in Metadata.

func CellsSize

func CellsSize(sizes []int) []int

CellsSizes returns the sizes of inner cells dimensions given overall tensor sizes. It returns []int{1} for the 1D case. Used for ensuring cell-wise outputs are the right size.

func ColumnMajorStrides

func ColumnMajorStrides(sizes ...int) []int

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

func CompareAscending

func CompareAscending[T cmp.Ordered](a, b T, ascending bool) int

CompareAscending is a sort compare function that reverses direction based on the ascending bool.

func ContainsFloat

func ContainsFloat(tsr, vals Tensor) bool

ContainsFloat returns true if source tensor contains any of given vals, using Float value method for comparison.

func ContainsInt

func ContainsInt(tsr, vals Tensor) bool

ContainsInt returns true if source tensor contains any of given vals, using Int value method for comparison.

func ContainsString

func ContainsString(tsr, vals Tensor, opts StringMatch) bool

ContainsString returns true if source tensor contains any of given vals, using String value method for comparison, and given options for how to compare the strings.

func CopyFromLargerShape added in v0.1.3

func CopyFromLargerShape(tsr, from Tensor)

CopyFromLargerShape copies values from another tensor of a larger shape, using indexes in this shape. The other tensor must have at least the same or greater shape values on each dimension as the target. Uses float numbers to copy if not a string.

func DefaultNumThreads

func DefaultNumThreads() int

DefaultNumThreads returns the default number of threads to use: NumThreads if non-zero, otherwise runtime.GOMAXPROCS.

func Float64ToBool

func Float64ToBool(val float64) bool

Float64ToBool converts float64 value to bool.

func Float64ToString

func Float64ToString(val float64) string

Float64ToString converts float64 to string value using strconv, g format

func FloatAssignFunc

func FloatAssignFunc(fun func(a, b float64) float64, a, b Tensor) error

FloatAssignFunc sets a to a binary function of a and b float64 values.

func FloatBinaryFuncOut

func FloatBinaryFuncOut(flops int, fun func(a, b float64) float64, a, b Tensor, out Values) error

FloatBinaryFuncOut sets output to a binary function of a, b float64 values.

func FloatFuncOut

func FloatFuncOut(flops int, fun func(in float64) float64, in Tensor, out Values) error

FloatFuncOut sets output to a function of tensor float64 values.

func FloatPromoteType

func FloatPromoteType(tsr ...Tensor) reflect.Kind

FloatPromoteType returns the DataType for Tensor(s) that promotes the Float type if any of the elements are of that type. Otherwise it returns the type of the first tensor.

func FloatSetFunc

func FloatSetFunc(flops int, fun func(idx int) float64, a Tensor) error

FloatSetFunc sets tensor float64 values from a function, which gets the index. Must be parallel threadsafe. The flops (floating point operations) estimate is used to control parallel threading using goroutines, and should reflect number of flops in the function. See VectorizeThreaded for more information.

func IntToBool

func IntToBool(val int) bool

IntToBool converts int value to bool.

func MustBeSameShape

func MustBeSameShape(a, b Tensor) error

MustBeSameShape returns an error if the two tensors do not have the same shape.

func NFirstLen

func NFirstLen(tsr ...Tensor) int

NFirstLen is an N function for Vectorize that returns the number of elements in the tensor, taking into account the Indexes view.

func NFirstRows

func NFirstRows(tsr ...Tensor) int

NFirstRows is an N function for Vectorize that returns the number of outer-dimension rows (or Indexes) of the first tensor.

func NMinLen

func NMinLen(nt int, tsr ...Tensor) int

NMinLen is an N function for Vectorize that returns the min number of elements across given number of tensors in the list. Use a closure to call this with the nt.

func NegIndex

func NegIndex(i, n int) int

NegIndex handles negative index values as counting backward from n.

func OpenCSV

func OpenCSV(tsr Tensor, filename fsx.Filename, delim Delims) 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 OpenFS added in v0.1.3

func OpenFS(tsr Tensor, fsys fs.FS, filename string, delim Delims) error

OpenFS is the version of OpenCSV that uses an fs.FS filesystem.

func Precision

func Precision(obj any) (int, error)

Precision gets the "precision" metadata value that determines the precision to use in writing floating point numbers to files. returns an error if not set.

func Projection2DCoords

func Projection2DCoords(shp *Shape, onedRow 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). See Projection2DShape for full info.

func Projection2DIndex

func Projection2DIndex(shp *Shape, onedRow 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). See Projection2DShape for full info.

func Projection2DSet

func Projection2DSet(tsr Tensor, onedRow 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). See Projection2DShape for full info.

func Projection2DSetString

func Projection2DSetString(tsr Tensor, onedRow bool, row, col int, val string)

Projection2DSetString sets a string 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). See Projection2DShape for full info.

func Projection2DShape

func Projection2DShape(shp *Shape, onedRow 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 the 1D case, onedRow determines if the values are row-wise or not. Even multiples of inner-most dimensions are placed along the row, odd in the column. If there are an odd number of dimensions, the first dimension is row-wise, and the remaining inner dimensions use the above logic from there, as if it was even. rowEx returns the number of "extra" (outer-dimensional) rows and colEx returns the number of extra cols, to add extra spacing between these dimensions.

func Projection2DString

func Projection2DString(tsr Tensor, onedRow bool, row, col int) string

Projection2DString returns the string 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). See Projection2DShape for full info.

func Projection2DValue

func Projection2DValue(tsr Tensor, onedRow 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). See Projection2DShape for full info.

func Range

func Range(vals Values) (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 tensor api on Values.

func ReadCSV

func ReadCSV(tsr Tensor, r io.Reader, delim Delims) 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 outermost and subsequent dimensions are progressively inner.

func SaveCSV

func SaveCSV(tsr Tensor, filename fsx.Filename, delim Delims) 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 SetAllFloat64

func SetAllFloat64(tsr Tensor, val float64)

SetAllFloat64 sets all values of given tensor to given value.

func SetAllInt

func SetAllInt(tsr Tensor, val int)

SetAllInt sets all values of given tensor to given value.

func SetAllString

func SetAllString(tsr Tensor, val string)

SetAllString sets all values of given tensor to given value.

func SetCalcFunc

func SetCalcFunc(tsr Tensor, fun func() error)

SetCalcFunc sets a function to calculate updated value for given tensor, storing the function pointer in the Metadata "CalcFunc" key for the tensor. Can be called by Calc function.

func SetPrecision

func SetPrecision(obj any, prec int)

SetPrecision sets the "precision" metadata value that determines the precision to use in writing floating point numbers to files.

func SetShape

func SetShape(vals Values, sh *Shape)

SetShape sets the dimension sizes from given Shape

func SetShapeFrom

func SetShapeFrom(vals Values, from Tensor)

SetShapeFrom sets shape of given tensor from a source tensor.

func SetShapeNames

func SetShapeNames(md *metadata.Data, names ...string)

SetShapeNames sets the tensor shape dimension names into given metadata.

func SetShapeSizesFromTensor

func SetShapeSizesFromTensor(vals Values, sizes Tensor)

SetShapeSizesFromTensor sets the dimension sizes as 1D int values from given tensor. The backing storage is resized appropriately, retaining all existing data that fits.

func ShapeNames

func ShapeNames(md *metadata.Data) []string

ShapeNames gets the tensor shape dimension names from given metadata.

func SplitAtInnerDims

func SplitAtInnerDims(tsr Tensor, nInner int) []int

SplitAtInnerDims returns the sizes of the given tensor's shape with the given number of inner-most dimensions retained as is, and those above collapsed to a single dimension. If the total number of dimensions is < nInner the result is nil.

func Sprintf

func Sprintf(format string, tsr Tensor, maxLen int) string

Sprintf returns a string representation of the given tensor, with a maximum length of as given: output is terminated when it exceeds that length. If maxLen = 0, MaxSprintLength is used. The format is the per-element format string. If empty it uses general %g for number or %s for string.

func StringAssignFunc

func StringAssignFunc(fun func(a, b string) string, a, b Tensor) error

StringAssignFunc sets a to a binary function of a and b string values.

func StringBinaryFuncOut

func StringBinaryFuncOut(fun func(a, b string) string, a, b Tensor, out Values) error

StringBinaryFuncOut sets output to a binary function of a, b string values.

func StringToFloat64

func StringToFloat64(str string) float64

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

func ToBinary added in v0.1.2

func ToBinary(tsr Values) []byte

ToBinary returns a binary encoding of the tensor that includes its type, shape and all data. FromBinary makes a tensor from this binary data.

func Vectorize

func Vectorize(nfun func(tsr ...Tensor) int, fun func(idx int, tsr ...Tensor), tsr ...Tensor)

Vectorize applies given function 'fun' to tensor elements indexed by given index, with the 'nfun' providing the number of indexes to vectorize over, and initializing any output vectors. Thus the nfun is often specific to a particular class of functions. Both functions are called with the same set of Tensors passed as the final argument(s). The role of each tensor is function-dependent: there could be multiple inputs and outputs, and the output could be effectively scalar, as in a sum operation. The interpretation of the index is function dependent as well, but often is used to iterate over the outermost row dimension of the tensor. This version runs purely sequentially on on this go routine. See VectorizeThreaded and VectorizeGPU for other versions.

func VectorizeOnThreads

func VectorizeOnThreads(threads int, nfun func(tsr ...Tensor) int, fun func(idx int, tsr ...Tensor), tsr ...Tensor)

VectorizeOnThreads runs given Vectorize function on given number of threads. Use VectorizeThreaded to only use parallel threads when it is likely to be beneficial, in terms of the ThreadingThreshold. If threads is 0, then the DefaultNumThreads will be used: GOMAXPROCS subject to NumThreads constraint if non-zero.

func VectorizeThreaded

func VectorizeThreaded(flops int, nfun func(tsr ...Tensor) int, fun func(idx int, tsr ...Tensor), tsr ...Tensor)

VectorizeThreaded is a version of Vectorize that will automatically distribute the computation in parallel across multiple "threads" (goroutines) if the number of elements to be computed times the given flops (floating point operations) for the function exceeds the ThreadingThreshold. Heuristically, numbers below this threshold do not result in an overall speedup, due to overhead costs. Each elemental math operation in the function adds a flop. See estimates in [tmath] for basic math functions.

func WrapIndex1D

func WrapIndex1D(sh *Shape, i ...int) int

WrapIndex1D returns the 1d flat index for given n-dimensional index based on given shape, where any singleton dimension sizes cause the resulting index value to remain at 0, effectively causing that dimension to wrap around, while the other tensor is presumably using the full range of the values along this dimension. See AlignShapes for more info.

func WriteCSV

func WriteCSV(tsr Tensor, w io.Writer, delim Delims) 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 Arg

type Arg struct {
	// Type has full reflection type info.
	Type reflect.Type

	// IsTensor is true if it satisfies the Tensor interface.
	IsTensor bool

	// IsInt is true if Kind = Int, for shape, slice etc params.
	IsInt bool

	// IsVariadic is true if this is the last arg and has ...; type will be an array.
	IsVariadic bool
}

Arg has key information that Goal needs about each arg, for converting expressions into the appropriate type.

type Base

type Base[T any] struct {

	// Values is a flat 1D slice of the underlying data.
	Values []T

	// Meta data is used extensively for Name, Plot styles, etc.
	// Use standard Go camel-case key names, standards in [metadata].
	Meta metadata.Data
	// contains filtered or unexported fields
}

Base is the base Tensor implementation for given type.

func (*Base[T]) Bytes

func (tsr *Base[T]) Bytes() []byte

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 Bool tensor type.

func (*Base[T]) DimSize

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

DimSize returns size of given dimension.

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() *metadata.Data

Metadata returns the metadata for this tensor, which can be used to encode plotting options, etc.

func (*Base[T]) NumDims

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

NumDims returns the total number of dimensions.

func (*Base[T]) Set

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

func (*Base[T]) Set1D

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

func (*Base[T]) SetFromBytes added in v0.1.2

func (tsr *Base[T]) SetFromBytes(b []byte)

func (*Base[T]) SetNumRows

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

SetNumRows sets the number of rows (outermost dimension) in a RowMajor organized tensor. It is safe to set this to 0. For incrementally growing tensors (e.g., a log) it is best to first set the anticipated full size, which allocates the full amount of memory, and then set to 0 and grow incrementally.

func (*Base[T]) SetShapeSizes

func (tsr *Base[T]) SetShapeSizes(sizes ...int)

SetShapeSizes sets the dimension sizes of the tensor, and resizes backing storage appropriately, retaining all existing data that fits.

func (*Base[T]) Shape

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

func (*Base[T]) ShapeSizes

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

ShapeSizes returns the sizes of each dimension as a slice of ints. The returned slice is a copy and can be modified without side effects.

func (*Base[T]) Sizeof

func (tsr *Base[T]) Sizeof() int64

func (*Base[T]) String1D

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

func (*Base[T]) StringRow

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

func (*Base[T]) StringValue

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

func (*Base[T]) Value

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

func (*Base[T]) Value1D

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

func (*Base[T]) ValuePtr

func (tsr *Base[T]) ValuePtr(i ...int) *T

type Bool

type Bool struct {

	// Values is a flat 1D slice of the underlying data, using [bitslice].
	Values bitslice.Slice

	// Meta data is used extensively for Name, Plot styles, etc.
	// Use standard Go camel-case key names, standards in [metadata].
	Meta metadata.Data
	// contains filtered or unexported fields
}

Bool is a tensor of bits backed by a bitslice.Slice for efficient storage of binary, boolean data. Bool does not support [RowMajor.SubSpace] access and related methods due to the nature of the underlying data representation.

func BoolFloatsFunc

func BoolFloatsFunc(fun func(a, b float64) bool, a, b Tensor) *Bool

BoolFloatsFunc sets boolean output value based on a function involving float64 values from the two tensors.

func BoolIntsFunc

func BoolIntsFunc(fun func(a, b int) bool, a, b Tensor) *Bool

BoolIntsFunc sets boolean output value based on a function involving int values from the two tensors.

func BoolStringsFunc

func BoolStringsFunc(fun func(a, b string) bool, a, b Tensor) *Bool

BoolStringsFunc sets boolean output value based on a function involving string values from the two tensors.

func CallOut2Bool

func CallOut2Bool(fun func(a, b Tensor, out *Bool) error, a, b Tensor) *Bool

func NewBool

func NewBool(sizes ...int) *Bool

NewBool returns a new n-dimensional tensor of bit values with the given sizes per dimension (shape).

func NewBoolFromValues added in v0.1.1

func NewBoolFromValues(vals ...bool) *Bool

NewBoolFromValues returns a new 1-dimensional tensor of given value type initialized directly from the given slice values, which are not copied. The resulting Tensor thus "wraps" the given values.

func NewBoolShape

func NewBoolShape(shape *Shape) *Bool

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

func (*Bool) AppendFrom

func (tsr *Bool) AppendFrom(frm Values) Values

AppendFrom appends values from other tensor into this tensor, which must have the same cell size as this tensor. It uses and optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*Bool) AppendRow

func (tsr *Bool) AppendRow(val Values)

AppendRow not possible with Bool.

func (*Bool) AppendRowFloat

func (tsr *Bool) AppendRowFloat(val ...float64)

AppendRowFloat not possible with Bool.

func (*Bool) AppendRowInt

func (tsr *Bool) AppendRowInt(val ...int)

AppendRowInt not possible with Bool.

func (*Bool) AppendRowString

func (tsr *Bool) AppendRowString(val ...string)

AppendRowString not possible with Bool.

func (*Bool) AsValues

func (tsr *Bool) AsValues() Values

func (*Bool) Bool

func (tsr *Bool) Bool(i ...int) bool

func (*Bool) Bool1D

func (tsr *Bool) Bool1D(off int) bool

func (*Bool) Bytes

func (tsr *Bool) Bytes() []byte

func (*Bool) Clone

func (tsr *Bool) Clone() Values

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 (*Bool) CopyCellsFrom

func (tsr *Bool) CopyCellsFrom(frm Values, 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 (*Bool) CopyFrom

func (tsr *Bool) CopyFrom(frm Values)

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 (*Bool) DataType

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

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

func (*Bool) DimSize

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

DimSize returns size of given dimension

func (*Bool) Float

func (tsr *Bool) Float(i ...int) float64

func (*Bool) Float1D

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

func (*Bool) FloatRow

func (tsr *Bool) FloatRow(row, cell int) float64

func (*Bool) Int

func (tsr *Bool) Int(i ...int) int

func (*Bool) Int1D

func (tsr *Bool) Int1D(off int) int

func (*Bool) IntRow

func (tsr *Bool) IntRow(row, cell int) int

func (*Bool) IsString

func (tsr *Bool) IsString() bool

func (*Bool) Label

func (tsr *Bool) Label() string

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

func (*Bool) Len

func (tsr *Bool) Len() int

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

func (*Bool) Metadata

func (tsr *Bool) Metadata() *metadata.Data

Metadata returns the metadata for this tensor, which can be used to encode plotting options, etc.

func (*Bool) NumDims

func (tsr *Bool) NumDims() int

NumDims returns the total number of dimensions.

func (*Bool) RowTensor

func (tsr *Bool) RowTensor(row int) Values

RowTensor not possible with Bool.

func (*Bool) Set

func (tsr *Bool) Set(val bool, i ...int)

func (*Bool) Set1D

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

func (*Bool) SetBool

func (tsr *Bool) SetBool(val bool, i ...int)

func (*Bool) SetBool1D

func (tsr *Bool) SetBool1D(val bool, off int)

func (*Bool) SetFloat

func (tsr *Bool) SetFloat(val float64, i ...int)

func (*Bool) SetFloat1D

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

func (*Bool) SetFloatRow

func (tsr *Bool) SetFloatRow(val float64, row, cell int)

func (*Bool) SetFromBytes added in v0.1.2

func (tsr *Bool) SetFromBytes(b []byte)

func (*Bool) SetInt

func (tsr *Bool) SetInt(val int, i ...int)

func (*Bool) SetInt1D

func (tsr *Bool) SetInt1D(val int, off int)

func (*Bool) SetIntRow

func (tsr *Bool) SetIntRow(val int, row, cell int)

func (*Bool) SetNumRows

func (tsr *Bool) SetNumRows(rows int)

SetNumRows sets the number of rows (outermost dimension) in a RowMajor organized tensor. It is safe to set this to 0. For incrementally growing tensors (e.g., a log) it is best to first set the anticipated full size, which allocates the full amount of memory, and then set to 0 and grow incrementally.

func (*Bool) SetRowTensor

func (tsr *Bool) SetRowTensor(val Values, row int)

SetRowTensor not possible with Bool.

func (*Bool) SetShapeSizes

func (tsr *Bool) SetShapeSizes(sizes ...int)

func (*Bool) SetString

func (tsr *Bool) SetString(val string, i ...int)

func (*Bool) SetString1D

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

func (*Bool) SetStringRow

func (tsr *Bool) SetStringRow(val string, row, cell int)

func (*Bool) SetTrue

func (tsr *Bool) SetTrue()

SetTrue is simple convenience function initialize all values to 0

func (*Bool) SetZeros

func (tsr *Bool) SetZeros()

SetZeros is a convenience function initialize all values to 0 (false).

func (*Bool) Shape

func (tsr *Bool) Shape() *Shape

func (*Bool) ShapeSizes

func (tsr *Bool) ShapeSizes() []int

ShapeSizes returns the sizes of each dimension as a slice of ints. The returned slice is a copy and can be modified without side effects.

func (*Bool) Sizeof

func (tsr *Bool) Sizeof() int64

func (*Bool) String

func (tsr *Bool) String() string

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

func (*Bool) String1D

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

func (*Bool) StringRow

func (tsr *Bool) StringRow(row, cell int) string

func (*Bool) StringValue

func (tsr *Bool) StringValue(i ...int) string

func (*Bool) SubSpace

func (tsr *Bool) SubSpace(offs ...int) Values

SubSpace is not possible with Bool.

func (*Bool) Value

func (tsr *Bool) Value(i ...int) bool

func (*Bool) Value1D

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

type Byte

type Byte = Number[byte]

Byte is an alias for Number[byte].

func NewByte

func NewByte(sizes ...int) *Byte

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

type DataTypes

type DataTypes interface {
	string | bool | float32 | float64 | int | int64 | uint64 | int32 | uint32 | byte
}

DataTypes are the primary tensor data types with specific support. Any numerical type can also be used. bool is represented using an efficient bit slice.

type Delims

type Delims int32 //enums:enum

Delim are standard CSV delimiter options (Tab, Comma, Space)

const (
	// Tab is the tab rune delimiter, for TSV tab separated values
	Tab Delims = iota

	// Comma is the comma rune delimiter, for CSV comma separated values
	Comma

	// Space is the space rune delimiter, for SSV space separated value
	Space

	// Detect is used during reading a file -- reads the first line and detects tabs or commas
	Detect
)
const DelimsN Delims = 4

DelimsN is the highest valid value for type Delims, plus one.

func DelimsValues

func DelimsValues() []Delims

DelimsValues returns all possible values for the type Delims.

func (Delims) Desc

func (i Delims) Desc() string

Desc returns the description of the Delims value.

func (Delims) Int64

func (i Delims) Int64() int64

Int64 returns the Delims value as an int64.

func (Delims) MarshalText

func (i Delims) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Delims) Rune

func (dl Delims) Rune() rune

func (*Delims) SetInt64

func (i *Delims) SetInt64(in int64)

SetInt64 sets the Delims value from an int64.

func (*Delims) SetString

func (i *Delims) SetString(s string) error

SetString sets the Delims value from its string representation, and returns an error if the string is invalid.

func (Delims) String

func (i Delims) String() string

String returns the string representation of this Delims value.

func (*Delims) UnmarshalText

func (i *Delims) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Delims) Values

func (i Delims) Values() []enums.Enum

Values returns all possible values for the type Delims.

type FilterFunc

type FilterFunc func(tsr Values, row int) bool

FilterFunc is a function used for filtering that returns true if Tensor row should be included in the current filtered view of the tensor, and false if it should be removed.

type Float32

type Float32 = Number[float32]

Float32 is an alias for Number[float32].

func AsFloat32

func AsFloat32(tsr Tensor) *Float32

AsFloat32 returns the tensor as a Float32 tensor. If already is a Float32, it is returned as such. Otherwise, a new Float32 tensor is created and values are copied.

func NewFloat32

func NewFloat32(sizes ...int) *Float32

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

func NewFloat32FromValues

func NewFloat32FromValues(vals ...float32) *Float32

NewFloat32FromValues returns a new 1-dimensional tensor of given value type initialized directly from the given slice values, which are not copied. The resulting Tensor thus "wraps" the given values.

func NewFloat32Scalar

func NewFloat32Scalar(val float32) *Float32

NewFloat32Scalar is a convenience method for a Tensor representation of a single float32 scalar value.

type Float64

type Float64 = Number[float64]

Float64 is an alias for Number[float64].

func AsFloat64

func AsFloat64(tsr Tensor) *Float64

AsFloat64 returns the tensor as a Float64 tensor. If already is a Float64, it is returned as such. Otherwise, a new Float64 tensor is created and values are copied. Use this function for interfacing with gonum or other apis that only operate on float64 types.

func NewFloat64

func NewFloat64(sizes ...int) *Float64

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

func NewFloat64FromValues

func NewFloat64FromValues(vals ...float64) *Float64

NewFloat64FromValues returns a new 1-dimensional tensor of given value type initialized directly from the given slice values, which are not copied. The resulting Tensor thus "wraps" the given values.

func NewFloat64Full

func NewFloat64Full(val float64, sizes ...int) *Float64

NewFloat64Full returns a new tensor full of given scalar value, of given shape sizes.

func NewFloat64Ones

func NewFloat64Ones(sizes ...int) *Float64

NewFloat64Ones returns a new tensor full of 1s, of given shape sizes.

func NewFloat64Rand

func NewFloat64Rand(sizes ...int) *Float64

NewFloat64Rand returns a new tensor full of random numbers from global random source, of given shape sizes.

func NewFloat64Scalar

func NewFloat64Scalar(val float64) *Float64

NewFloat64Scalar is a convenience method for a Tensor representation of a single float64 scalar value.

func NewFloat64SpacedLinear

func NewFloat64SpacedLinear(start, stop Tensor, num int, endpoint bool) *Float64

NewFloat64SpacedLinear returns a new Float64 tensor with num linearly spaced numbers between start and stop values, as tensors, which must be the same length and determine the cell shape of the output. If num is 0, then a default of 50 is used. If endpoint = true, then the stop value is _inclusive_, i.e., it will be the final value, otherwise it is exclusive. This corresponds to the NumPy linspace function.

type Func

type Func struct {
	// Name is the original CamelCase Go name for function
	Name string

	// Fun is the function, which must _only_ take some number of Tensor
	// args, with an optional any first arg.
	Fun any

	// Args has parsed information about the function args, for Goal.
	Args []*Arg
}

Func represents a registered tensor function, which has In number of input Tensor arguments, and Out number of output arguments (typically 1). There can also be an 'any' first argument to support other kinds of parameters. This is used to make tensor functions available to the Goal language.

func FuncByName

func FuncByName(name string) (*Func, error)

FuncByName finds function of given name in the registry, returning an error if the function name has not been registered.

func NewFunc

func NewFunc(name string, fun any) (*Func, error)

NewFunc creates a new Func desciption of the given function, which must have a signature like this: func([opt any,] a, b, out tensor.Tensor) error i.e., taking some specific number of Tensor arguments (up to 5). Functions can also take an 'any' first argument to handle other non-tensor inputs (e.g., function pointer, dirfs directory, etc). The name should be a standard 'package.FuncName' qualified, exported CamelCase name, with 'out' indicating the number of output arguments, and an optional arg indicating an 'any' first argument. The remaining arguments in the function (automatically determined) are classified as input arguments.

func (*Func) GetArgs

func (fn *Func) GetArgs()

GetArgs gets key info about each arg, for use by Goal transpiler.

func (*Func) String

func (fn *Func) String() string

type Indexed

type Indexed struct {

	// Tensor source that we are an indexed view onto.
	Tensor Tensor

	// Indexes is the list of indexes into the source tensor,
	// with the innermost dimension providing the index values
	// (size = number of dimensions in the source tensor), and
	// the remaining outer dimensions determine the shape
	// of this [Indexed] tensor view.
	Indexes *Int
}

Indexed provides an arbitrarily indexed view onto another "source" Tensor with each index value providing a full n-dimensional index into the source. The shape of this view is determined by the shape of the [Indexed.Indexes] tensor up to the final innermost dimension, which holds the index values. Thus the innermost dimension size of the indexes is equal to the number of dimensions in the source tensor. Given the essential role of the indexes in this view, it is not usable without the indexes. This view is not memory-contiguous and does not support the RowMajor interface or efficient access to inner-dimensional subspaces. To produce a new concrete Values that has raw data actually organized according to the indexed order (i.e., the copy function of numpy), call Indexed.AsValues.

func AsIndexed

func AsIndexed(tsr Tensor) *Indexed

AsIndexed returns the tensor as a Indexed view, if it is one. Otherwise, it returns nil; there is no usable "null" Indexed view.

func NewIndexed

func NewIndexed(tsr Tensor, idx *Int) *Indexed

NewIndexed returns a new Indexed view of given tensor, with tensor of indexes into the source tensor.

func (*Indexed) AsValues

func (ix *Indexed) AsValues() Values

AsValues returns a copy of this tensor as raw Values. This "renders" the Indexed view into a fully contiguous and optimized memory representation of that view, which will be faster to access for further processing, and enables all the additional functionality provided by the Values interface.

func (*Indexed) DataType

func (ix *Indexed) DataType() reflect.Kind

func (*Indexed) DimSize

func (ix *Indexed) DimSize(dim int) int

func (*Indexed) Float

func (ix *Indexed) Float(i ...int) float64

Float returns the value of given index as a float64. The indexes are indirected through the [Indexed.Indexes].

func (*Indexed) Float1D

func (ix *Indexed) Float1D(i int) float64

Float1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Indexed) Int

func (ix *Indexed) Int(i ...int) int

Int returns the value of given index as an int. The indexes are indirected through the [Indexed.Indexes].

func (*Indexed) Int1D

func (ix *Indexed) Int1D(i int) int

Int1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Indexed) IsString

func (ix *Indexed) IsString() bool

func (*Indexed) Label

func (ix *Indexed) Label() string

func (*Indexed) Len

func (ix *Indexed) Len() int

func (*Indexed) Metadata

func (ix *Indexed) Metadata() *metadata.Data

func (*Indexed) NumDims

func (ix *Indexed) NumDims() int

func (*Indexed) SetFloat

func (ix *Indexed) SetFloat(val float64, i ...int)

SetFloat sets the value of given index as a float64 The indexes are indirected through the [Indexed.Indexes].

func (*Indexed) SetFloat1D

func (ix *Indexed) SetFloat1D(val float64, i int)

SetFloat1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Indexed) SetInt

func (ix *Indexed) SetInt(val int, i ...int)

SetInt sets the value of given index as an int The indexes are indirected through the [Indexed.Indexes].

func (*Indexed) SetInt1D

func (ix *Indexed) SetInt1D(val int, i int)

SetInt1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Indexed) SetString

func (ix *Indexed) SetString(val string, i ...int)

SetString sets the value of given index as a string The indexes are indirected through the [Indexed.Indexes].

func (*Indexed) SetString1D

func (ix *Indexed) SetString1D(val string, i int)

SetString1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Indexed) SetTensor

func (ix *Indexed) SetTensor(tsr Tensor)

SetTensor sets as indexes into given tensor with sequential initial indexes.

func (*Indexed) Shape

func (ix *Indexed) Shape() *Shape

func (*Indexed) ShapeSizes

func (ix *Indexed) ShapeSizes() []int

func (*Indexed) SourceIndexes

func (ix *Indexed) SourceIndexes(i ...int) []int

SourceIndexes returns the actual indexes into underlying source tensor based on given list of indexes into the [Indexed.Indexes] tensor, _excluding_ the final innermost dimension.

func (*Indexed) SourceIndexesFrom1D

func (ix *Indexed) SourceIndexesFrom1D(oned int) []int

SourceIndexesFrom1D returns the full indexes into source tensor based on the given 1d index, which is based on the outer dimensions, excluding the final innermost dimension.

func (*Indexed) String

func (ix *Indexed) String() string

func (*Indexed) String1D

func (ix *Indexed) String1D(i int) string

String1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Indexed) StringValue

func (ix *Indexed) StringValue(i ...int) string

StringValue returns the value of given index as a string. The indexes are indirected through the [Indexed.Indexes].

type Int

type Int = Number[int]

Int is an alias for Number[int].

func AsInt

func AsInt(tsr Tensor) *Int

AsInt returns the tensor as a Int tensor. If already is a Int, it is returned as such. Otherwise, a new Int tensor is created and values are copied.

func NewInt

func NewInt(sizes ...int) *Int

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

func NewIntFromValues

func NewIntFromValues(vals ...int) *Int

NewIntFromValues returns a new 1-dimensional tensor of given value type initialized directly from the given slice values, which are not copied. The resulting Tensor thus "wraps" the given values.

func NewIntFull

func NewIntFull(val int, sizes ...int) *Int

NewIntFull returns a new tensor full of given scalar value, of given shape sizes.

func NewIntRange

func NewIntRange(svals ...int) *Int

NewIntRange returns a new Int Tensor with given Slice range parameters, with the same semantics as NumPy arange based on the number of arguments passed:

  • 1 = stop
  • 2 = start, stop
  • 3 = start, stop, step

func NewIntScalar

func NewIntScalar(val int) *Int

NewIntScalar is a convenience method for a Tensor representation of a single int scalar value.

type Int32

type Int32 = Number[int32]

Int32 is an alias for Number[int32].

func NewInt32

func NewInt32(sizes ...int) *Int32

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

type Masked

type Masked struct {

	// Tensor source that we are a masked view onto.
	Tensor Tensor

	// Bool tensor with same shape as source tensor, providing mask.
	Mask *Bool
}

Masked is a filtering wrapper around another "source" Tensor, that provides a bit-masked view onto the Tensor defined by a Bool Values tensor with a matching shape. If the bool mask has a 'false' then the corresponding value cannot be Set, and Float access returns NaN indicating missing data (other type access returns the zero value). A new Masked view defaults to a full transparent view of the source tensor. To produce a new Values tensor with only the 'true' cases, (i.e., the copy function of numpy), call Masked.AsValues.

func AsMasked

func AsMasked(tsr Tensor) *Masked

AsMasked returns the tensor as a Masked view. If it already is one, then it is returned, otherwise it is wrapped with an initially fully transparent mask.

func NewMasked

func NewMasked(tsr Tensor, mask ...*Bool) *Masked

NewMasked returns a new Masked view of given tensor, with given Bool mask values. If no mask is provided, a default full transparent (all bool values = true) mask is used.

func (*Masked) AsValues

func (ms *Masked) AsValues() Values

AsValues returns a copy of this tensor as raw Values. This "renders" the Masked view into a fully contiguous and optimized memory representation of that view. Because the masking pattern is unpredictable, only a 1D shape is possible.

func (*Masked) DataType

func (ms *Masked) DataType() reflect.Kind

func (*Masked) DimSize

func (ms *Masked) DimSize(dim int) int

func (*Masked) Filter

func (ms *Masked) Filter(filterer func(tsr Tensor, idx int) bool) *Masked

Filter sets the mask values using given Filter function. The filter function gets the 1D index into the source tensor.

func (*Masked) Float

func (ms *Masked) Float(i ...int) float64

func (*Masked) Float1D

func (ms *Masked) Float1D(i int) float64

func (*Masked) Int

func (ms *Masked) Int(i ...int) int

func (*Masked) Int1D

func (ms *Masked) Int1D(i int) int

func (*Masked) IsString

func (ms *Masked) IsString() bool

func (*Masked) Label

func (ms *Masked) Label() string

func (*Masked) Len

func (ms *Masked) Len() int

func (*Masked) Metadata

func (ms *Masked) Metadata() *metadata.Data

func (*Masked) NumDims

func (ms *Masked) NumDims() int

func (*Masked) SetFloat

func (ms *Masked) SetFloat(val float64, i ...int)

func (*Masked) SetFloat1D

func (ms *Masked) SetFloat1D(val float64, i int)

func (*Masked) SetInt

func (ms *Masked) SetInt(val int, i ...int)

func (*Masked) SetInt1D

func (ms *Masked) SetInt1D(val int, i int)

SetInt1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Masked) SetString

func (ms *Masked) SetString(val string, i ...int)

func (*Masked) SetString1D

func (ms *Masked) SetString1D(val string, i int)

func (*Masked) SetTensor

func (ms *Masked) SetTensor(tsr Tensor)

SetTensor sets the given source tensor. If the shape does not match the current Mask, then a new transparent mask is established.

func (*Masked) Shape

func (ms *Masked) Shape() *Shape

func (*Masked) ShapeSizes

func (ms *Masked) ShapeSizes() []int

func (*Masked) SourceIndexes

func (ms *Masked) SourceIndexes(getTrue bool) *Int

SourceIndexes returns a flat Int tensor of the mask values that match the given getTrue argument state. These can be used as indexes in the Indexed view, for example. The resulting tensor is 2D with inner dimension = number of source tensor dimensions, to hold the indexes, and outer dimension = number of indexes.

func (*Masked) String

func (ms *Masked) String() string

func (*Masked) String1D

func (ms *Masked) String1D(i int) string

func (*Masked) StringValue

func (ms *Masked) StringValue(i ...int) string

func (*Masked) SyncShape

func (ms *Masked) SyncShape()

SyncShape ensures that [Masked.Mask] shape is the same as source tensor. If the Mask does not exist or is a different shape from the source, then it is created or reshaped, and all values set to true ("transparent").

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) *Number[T]

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

func NewNumberFromValues

func NewNumberFromValues[T num.Number](vals ...T) *Number[T]

NewNumberFromValues returns a new 1-dimensional tensor of given value type initialized directly from the given slice values, which are not copied. The resulting Tensor thus "wraps" the given values.

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]) AppendFrom

func (tsr *Number[T]) AppendFrom(frm Values) Values

AppendFrom appends values from other tensor into this tensor, which must have the same cell size as this tensor. It uses and optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*Number[T]) AppendRow

func (tsr *Number[T]) AppendRow(val Values)

AppendRow adds a row and sets values to given values.

func (*Number[T]) AppendRowFloat

func (tsr *Number[T]) AppendRowFloat(val ...float64)

AppendRowFloat adds a row and sets float value(s), up to number of cells.

func (*Number[T]) AppendRowInt

func (tsr *Number[T]) AppendRowInt(val ...int)

AppendRowInt adds a row and sets int value(s), up to number of cells.

func (*Number[T]) AppendRowString

func (tsr *Number[T]) AppendRowString(val ...string)

AppendRowString adds a row and sets string value(s), up to number of cells.

func (*Number[T]) AsValues

func (tsr *Number[T]) AsValues() Values

func (*Number[T]) Clone

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

Clone clones this tensor, creating a duplicate copy of itself with its own separate memory representation of all the values.

func (*Number[T]) CopyCellsFrom

func (tsr *Number[T]) CopyCellsFrom(frm Values, 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 Values)

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]) Float

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

func (*Number[T]) Float1D

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

func (*Number[T]) FloatRow

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

func (*Number[T]) Int

func (tsr *Number[T]) Int(i ...int) int

func (*Number[T]) Int1D

func (tsr *Number[T]) Int1D(i int) int

func (*Number[T]) IntRow

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

func (*Number[T]) IsString

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

func (*Number[T]) RowTensor

func (tsr *Number[T]) RowTensor(row int) Values

RowTensor is a convenience version of [RowMajor.SubSpace] to return the SubSpace for the outermost row dimension. Rows defines a version of this that indirects through the row indexes.

func (*Number[T]) SetAdd

func (tsr *Number[T]) SetAdd(val T, i ...int)

func (*Number[T]) SetDiv

func (tsr *Number[T]) SetDiv(val T, i ...int)

func (*Number[T]) SetFloat

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

func (*Number[T]) SetFloat1D

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

func (*Number[T]) SetFloatRow

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

func (*Number[T]) SetInt

func (tsr *Number[T]) SetInt(val int, i ...int)

func (*Number[T]) SetInt1D

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

func (*Number[T]) SetIntRow

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

func (*Number[T]) SetMul

func (tsr *Number[T]) SetMul(val T, i ...int)

func (*Number[T]) SetRowTensor

func (tsr *Number[T]) SetRowTensor(val Values, row int)

SetRowTensor sets the values of the SubSpace at given row to given values.

func (*Number[T]) SetString

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

func (Number[T]) SetString1D

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

func (*Number[T]) SetStringRow

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

func (*Number[T]) SetSub

func (tsr *Number[T]) SetSub(val T, i ...int)

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) Values

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 AsValues() method to separate the two.

type Reshaped

type Reshaped struct {

	// Tensor source that we are a masked view onto.
	Tensor Tensor

	// Reshape is the effective shape we use for access.
	// This must have the same Len() as the source Tensor.
	Reshape Shape
}

Reshaped is a reshaping wrapper around another "source" Tensor, that provides a length-preserving reshaped view onto the source Tensor. Reshaping by adding new size=1 dimensions (via NewAxis value) is often important for properly aligning two tensors in a computationally compatible manner; see the AlignShapes function. Reshaped.AsValues on this view returns a new Values with the view shape, calling Clone on the source tensor to get the values.

func AsReshaped

func AsReshaped(tsr Tensor) *Reshaped

AsReshaped returns the tensor as a Reshaped view. If it already is one, then it is returned, otherwise it is wrapped with an initial shape equal to the source tensor.

func NewReshaped

func NewReshaped(tsr Tensor, sizes ...int) *Reshaped

NewReshaped returns a new Reshaped view of given tensor, with given shape sizes. If no such sizes are provided, the source shape is used. A single -1 value can be used to automatically specify the remaining tensor length, as long as the other sizes are an even multiple of the total length. A single -1 returns a 1D view of the entire tensor.

func NewRowCellsView

func NewRowCellsView(tsr Tensor, split int) *Reshaped

NewRowCellsView returns a 2D Reshaped view onto the given tensor, with a single outer "row" dimension and a single inner "cells" dimension, with the given 'split' dimension specifying where the cells start. All dimensions prior to split are collapsed to form the new outer row dimension, and the remainder are collapsed to form the 1D cells dimension. This is useful for stats, metrics and other packages that operate on data in this shape.

func (*Reshaped) AsValues

func (rs *Reshaped) AsValues() Values

AsValues returns a copy of this tensor as raw Values, with the same shape as our view. This calls Clone on the source tensor to get the Values and then sets our shape sizes to it.

func (*Reshaped) DataType

func (rs *Reshaped) DataType() reflect.Kind

func (*Reshaped) DimSize

func (rs *Reshaped) DimSize(dim int) int

func (*Reshaped) Float

func (rs *Reshaped) Float(i ...int) float64

func (*Reshaped) Float1D

func (rs *Reshaped) Float1D(i int) float64

func (*Reshaped) Int

func (rs *Reshaped) Int(i ...int) int

func (*Reshaped) Int1D

func (rs *Reshaped) Int1D(i int) int

func (*Reshaped) IsString

func (rs *Reshaped) IsString() bool

func (*Reshaped) Label

func (rs *Reshaped) Label() string

func (*Reshaped) Len

func (rs *Reshaped) Len() int

func (*Reshaped) Metadata

func (rs *Reshaped) Metadata() *metadata.Data

func (*Reshaped) NumDims

func (rs *Reshaped) NumDims() int

func (*Reshaped) SetFloat

func (rs *Reshaped) SetFloat(val float64, i ...int)

func (*Reshaped) SetFloat1D

func (rs *Reshaped) SetFloat1D(val float64, i int)

func (*Reshaped) SetInt

func (rs *Reshaped) SetInt(val int, i ...int)

func (*Reshaped) SetInt1D

func (rs *Reshaped) SetInt1D(val int, i int)

func (*Reshaped) SetShapeSizes

func (rs *Reshaped) SetShapeSizes(sizes ...int) error

SetShapeSizes sets our shape sizes to the given values, which must result in the same length as the source tensor. An error is returned if not. If a different subset of content is desired, use another view such as Sliced. Note that any number of size = 1 dimensions can be added without affecting the length, and the NewAxis value can be used to semantically indicate when such a new dimension is being inserted. This is often useful for aligning two tensors to achieve a desired computation; see AlignShapes function. A single -1 can be used to specify a dimension size that takes the remaining length, as long as the other sizes are an even multiple of the length. A single -1 indicates to use the full length.

func (*Reshaped) SetString

func (rs *Reshaped) SetString(val string, i ...int)

func (*Reshaped) SetString1D

func (rs *Reshaped) SetString1D(val string, i int)

func (*Reshaped) Shape

func (rs *Reshaped) Shape() *Shape

func (*Reshaped) ShapeSizes

func (rs *Reshaped) ShapeSizes() []int

func (*Reshaped) String

func (rs *Reshaped) String() string

func (*Reshaped) String1D

func (rs *Reshaped) String1D(i int) string

func (*Reshaped) StringValue

func (rs *Reshaped) StringValue(i ...int) string

type RowMajor

type RowMajor interface {
	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 AsValues() method to separate the two. See [Slice] function to
	// extract arbitrary subspaces along ranges of each dimension.
	SubSpace(offs ...int) Values

	// RowTensor is a convenience version of [RowMajor.SubSpace] to return the
	// SubSpace for the outermost row dimension. [Rows] defines a version
	// of this that indirects through the row indexes.
	RowTensor(row int) Values

	// SetRowTensor sets the values of the [RowMajor.SubSpace] at given row to given values.
	SetRowTensor(val Values, row int)

	// AppendRow adds a row and sets values to given values.
	AppendRow(val Values)

	// FloatRow returns the value at given row and cell, where row is the outermost
	// dimension, and cell is a 1D index into remaining inner dimensions (0 for scalar).
	FloatRow(row, cell int) float64

	// SetFloatRow sets the value at given row and cell, where row is the outermost
	// dimension, and cell is a 1D index into remaining inner dimensions.
	SetFloatRow(val float64, row, cell int)

	// AppendRowFloat adds a row and sets float value(s), up to number of cells.
	AppendRowFloat(val ...float64)

	// IntRow returns the value at given row and cell, where row is the outermost
	// dimension, and cell is a 1D index into remaining inner dimensions.
	IntRow(row, cell int) int

	// SetIntRow sets the value at given row and cell, where row is the outermost
	// dimension, and cell is a 1D index into remaining inner dimensions.
	SetIntRow(val int, row, cell int)

	// AppendRowInt adds a row and sets int value(s), up to number of cells.
	AppendRowInt(val ...int)

	// StringRow returns the value at given row and cell, where row is the outermost
	// dimension, and cell is a 1D index into remaining inner dimensions.
	// [Rows] tensors index along the row, and use this interface extensively.
	// This is useful for lists of patterns, and the [table.Table] container.
	StringRow(row, cell int) string

	// SetStringRow sets the value at given row and cell, where row is the outermost
	// dimension, and cell is a 1D index into remaining inner dimensions.
	// [Rows] tensors index along the row, and use this interface extensively.
	// This is useful for lists of patterns, and the [table.Table] container.
	SetStringRow(val string, row, cell int)

	// AppendRowString adds a row and sets string value(s), up to number of cells.
	AppendRowString(val ...string)
}

RowMajor is subtype of Tensor that maintains a row major memory organization that thereby supports efficient access via the outermost 'row' dimension, with all remaining inner dimensions comprising the 'cells' of data per row (1 scalar value in the case of a 1D tensor). It is implemented by raw Values tensors, and the Rows indexed view of raw Values tensors. Other views however do not retain the underlying outer to inner row major memory structure and thus do not implement this interface.

type Rows

type Rows struct {

	// Tensor source that we are an indexed view onto.
	// Note that this must be a concrete [Values] tensor, to enable efficient
	// [RowMajor] access and subspace functions.
	Tensor Values

	// Indexes are the indexes into Tensor rows, with nil = sequential.
	// Only set if order is different from default sequential order.
	// Use the [Rows.RowIndex] method for nil-aware logic.
	Indexes []int
}

Rows is a row-indexed wrapper view around a Values Tensor that allows arbitrary row-wise ordering and filtering according to the [Rows.Indexes]. Sorting and filtering a tensor along this outermost row dimension only requires updating the indexes while leaving the underlying Tensor alone. Unlike the more general Sliced view, Rows maintains memory contiguity for the inner dimensions ("cells") within each row, and supports the RowMajor interface, with the [Set]FloatRow[Cell] methods providing efficient access. Use Rows.AsValues to obtain a concrete Values representation with the current row sorting.

func AsRows

func AsRows(tsr Tensor) *Rows

AsRows returns the tensor as a Rows view. If it already is one, then it is returned, otherwise a new Rows is created to wrap around the given tensor, which is enforced to be a Values tensor either because it already is one, or by calling [Tensor.AsValues] on it.

func NewRows

func NewRows(tsr Values, idxs ...int) *Rows

NewRows returns a new Rows view of given tensor, with optional list of indexes (none / nil = sequential).

func (*Rows) AddRows

func (rw *Rows) AddRows(n int)

AddRows adds n rows to end of underlying Tensor, and to the indexes in this view

func (*Rows) AppendRow

func (rw *Rows) AppendRow(val Values)

AppendRow adds a row and sets values to given values.

func (*Rows) AppendRowFloat

func (rw *Rows) AppendRowFloat(val ...float64)

AppendRowFloat adds a row and sets float value(s), up to number of cells.

func (*Rows) AppendRowInt

func (rw *Rows) AppendRowInt(val ...int)

AppendRowInt adds a row and sets int value(s), up to number of cells.

func (*Rows) AppendRowString

func (rw *Rows) AppendRowString(val ...string)

AppendRowString adds a row and sets string value(s), up to number of cells.

func (*Rows) AsValues

func (rw *Rows) AsValues() Values

AsValues returns this tensor as raw Values. If the row [Rows.Indexes] are nil, then the wrapped Values tensor is returned. Otherwise, it "renders" the Rows view into a fully contiguous and optimized memory representation of that view, which will be faster to access for further processing, and enables all the additional functionality provided by the Values interface.

func (*Rows) CloneIndexes

func (rw *Rows) CloneIndexes() *Rows

CloneIndexes returns a copy of the current Rows view with new indexes, with a pointer to the same underlying Tensor as the source.

func (*Rows) CopyIndexes

func (rw *Rows) CopyIndexes(oix *Rows)

CopyIndexes copies indexes from other Rows view.

func (*Rows) DataType

func (rw *Rows) DataType() reflect.Kind

func (*Rows) DeleteRows

func (rw *Rows) DeleteRows(at, n int)

DeleteRows deletes n rows of indexes starting at given index in the list of indexes

func (*Rows) DimSize

func (rw *Rows) DimSize(dim int) int

DimSize returns size of given dimension, returning NumRows() for first dimension.

func (*Rows) ExcludeMissing

func (rw *Rows) ExcludeMissing()

ExcludeMissing deletes indexes where the values are missing, as indicated by NaN. Uses first cell of higher dimensional data.

func (*Rows) Filter

func (rw *Rows) Filter(filterer func(tsr Values, row int) bool)

Filter filters the indexes using given Filter function. The Filter function operates directly on row numbers into the Tensor as these row numbers have already been projected through the indexes.

func (*Rows) FilterString

func (rw *Rows) FilterString(str string, opts StringMatch)

FilterString filters the indexes using string values compared to given string. Includes rows with matching values unless the Exclude option is set. If Contains option is set, it only checks if row contains string; if IgnoreCase, ignores case, otherwise filtering is case sensitive. Uses first cell of higher dimensional data.

func (*Rows) Float

func (rw *Rows) Float(i ...int) float64

Float returns the value of given index as a float64. The first index value is indirected through the indexes.

func (*Rows) Float1D

func (rw *Rows) Float1D(i int) float64

Float1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Rows) FloatRow

func (rw *Rows) FloatRow(row, cell int) float64

FloatRow returns the value at given row and cell, where row is outermost dim, and cell is 1D index into remaining inner dims. Row is indirected through the [Rows.Indexes]. This is the preferred interface for all Rows operations.

func (*Rows) IndexesNeeded

func (rw *Rows) IndexesNeeded()

IndexesNeeded is called prior to an operation that needs actual indexes, e.g., Sort, Filter. If Indexes == nil, they are set to all rows, otherwise current indexes are left as is. Use Sequential, then IndexesNeeded to ensure all rows are represented.

func (*Rows) InsertRows

func (rw *Rows) InsertRows(at, n int)

InsertRows adds n rows to end of underlying Tensor, and to the indexes starting at given index in this view

func (*Rows) Int

func (rw *Rows) Int(i ...int) int

Int returns the value of given index as an int. The first index value is indirected through the indexes.

func (*Rows) Int1D

func (rw *Rows) Int1D(i int) int

Int1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Rows) IntRow

func (rw *Rows) IntRow(row, cell int) int

IntRow returns the value at given row and cell, where row is outermost dim, and cell is 1D index into remaining inner dims. Row is indirected through the [Rows.Indexes]. This is the preferred interface for all Rows operations.

func (*Rows) IsString

func (rw *Rows) IsString() bool

func (*Rows) Label

func (rw *Rows) Label() string

func (*Rows) Len

func (rw *Rows) Len() int

Len returns the total number of elements in the tensor, taking into account the Indexes via Rows, as NumRows() * cell size.

func (*Rows) Metadata

func (rw *Rows) Metadata() *metadata.Data

func (*Rows) NumDims

func (rw *Rows) NumDims() int

func (*Rows) NumRows

func (rw *Rows) NumRows() int

NumRows returns the effective number of rows in this Rows view, which is the length of the index list or number of outer rows dimension of tensor if no indexes (full sequential view).

func (*Rows) Permuted

func (rw *Rows) Permuted()

Permuted sets indexes to a permuted order. If indexes already exist then existing list of indexes is permuted, otherwise a new set of permuted indexes are generated

func (*Rows) RowCellSize

func (rw *Rows) RowCellSize() (rows, cells int)

RowCellSize returns the size of the outermost Row shape dimension (via Rows.NumRows method), and the size of all the remaining inner dimensions (the "cell" size).

func (*Rows) RowIndex

func (rw *Rows) RowIndex(idx int) int

RowIndex returns the actual index into underlying tensor row based on given index value. If Indexes == nil, index is passed through.

func (*Rows) RowTensor

func (rw *Rows) RowTensor(row int) Values

RowTensor is a convenience version of Rows.SubSpace to return the SubSpace for the outermost row dimension, indirected through the indexes.

func (*Rows) Sequential

func (rw *Rows) Sequential()

Sequential sets Indexes to nil, resulting in sequential row-wise access into tensor.

func (*Rows) SetFloat

func (rw *Rows) SetFloat(val float64, i ...int)

SetFloat sets the value of given index as a float64 The first index value is indirected through the [Rows.Indexes].

func (*Rows) SetFloat1D

func (rw *Rows) SetFloat1D(val float64, i int)

SetFloat1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Rows) SetFloatRow

func (rw *Rows) SetFloatRow(val float64, row, cell int)

SetFloatRow sets the value at given row and cell, where row is outermost dim, and cell is 1D index into remaining inner dims. Row is indirected through the [Rows.Indexes]. This is the preferred interface for all Rows operations.

func (*Rows) SetInt

func (rw *Rows) SetInt(val int, i ...int)

SetInt sets the value of given index as an int The first index value is indirected through the [Rows.Indexes].

func (*Rows) SetInt1D

func (rw *Rows) SetInt1D(val int, i int)

SetInt1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Rows) SetIntRow

func (rw *Rows) SetIntRow(val int, row, cell int)

SetIntRow sets the value at given row and cell, where row is outermost dim, and cell is 1D index into remaining inner dims. Row is indirected through the [Rows.Indexes]. This is the preferred interface for all Rows operations.

func (*Rows) SetRowTensor

func (rw *Rows) SetRowTensor(val Values, row int)

SetRowTensor sets the values of the SubSpace at given row to given values, with row indirected through the indexes.

func (*Rows) SetString

func (rw *Rows) SetString(val string, i ...int)

SetString sets the value of given index as a string The first index value is indirected through the [Rows.Indexes].

func (*Rows) SetString1D

func (rw *Rows) SetString1D(val string, i int)

SetString1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Rows) SetStringRow

func (rw *Rows) SetStringRow(val string, row, cell int)

SetStringRow sets the value at given row and cell, where row is outermost dim, and cell is 1D index into remaining inner dims. Row is indirected through the [Rows.Indexes]. This is the preferred interface for all Rows operations.

func (*Rows) SetTensor

func (rw *Rows) SetTensor(tsr Values)

SetTensor sets as indexes into given Values tensor with sequential initial indexes.

func (*Rows) Shape

func (rw *Rows) Shape() *Shape

Shape() returns a Shape representation of the tensor shape (dimension sizes). If we have Indexes, this is the effective shape using the current number of indexes as the outermost row dimension size.

func (*Rows) ShapeSizes

func (rw *Rows) ShapeSizes() []int

If we have Indexes, this is the effective shape sizes using the current number of indexes as the outermost row dimension size.

func (*Rows) Sort

func (rw *Rows) Sort(ascending bool)

Sort does default alpha or numeric sort of row-wise data. Uses first cell of higher dimensional data.

func (*Rows) SortFunc

func (rw *Rows) SortFunc(cmp func(tsr Values, i, j int) int)

SortFunc sorts the row-wise indexes using given compare function. The compare function operates directly on row numbers into the Tensor as these row numbers have already been projected through the indexes. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b.

func (*Rows) SortIndexes

func (rw *Rows) SortIndexes()

SortIndexes sorts the indexes into our Tensor directly in numerical order, producing the native ordering, while preserving any filtering that might have occurred.

func (*Rows) SortStable

func (rw *Rows) SortStable(ascending bool)

SortStable does stable default alpha or numeric sort. Uses first cell of higher dimensional data.

func (*Rows) SortStableFunc

func (rw *Rows) SortStableFunc(cmp func(tsr Values, i, j int) int)

SortStableFunc stably sorts the row-wise indexes using given compare function. The compare function operates directly on row numbers into the Tensor as these row numbers have already been projected through the indexes. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b. It is *essential* that it always returns 0 when the two are equal for the stable function to actually work.

func (*Rows) String

func (rw *Rows) String() string

func (*Rows) String1D

func (rw *Rows) String1D(i int) string

String1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Rows) StringRow

func (rw *Rows) StringRow(row, cell int) string

StringRow returns the value at given row and cell, where row is outermost dim, and cell is 1D index into remaining inner dims. Row is indirected through the [Rows.Indexes]. This is the preferred interface for all Rows operations.

func (*Rows) StringValue

func (rw *Rows) StringValue(i ...int) string

StringValue returns the value of given index as a string. The first index value is indirected through the indexes.

func (*Rows) SubSpace

func (rw *Rows) SubSpace(offs ...int) Values

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. Rows version does indexed indirection of the outermost row dimension of the offsets.

func (*Rows) Swap

func (rw *Rows) Swap(i, j int)

Swap switches the indexes for i and j

func (*Rows) ValidIndexes

func (rw *Rows) ValidIndexes()

ValidIndexes deletes all invalid indexes from the list. Call this if rows (could) have been deleted from tensor.

type Shape

type Shape struct {

	// size per dimension.
	Sizes []int

	// offsets for each dimension.
	Strides []int `display:"-"`
}

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

func AddShapes

func AddShapes(shape1, shape2 *Shape) *Shape

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

func AlignForAssign

func AlignForAssign(a, b Tensor) (as, bs *Shape, err error)

AlignForAssign ensures that the shapes of two tensors, a and b have the proper alignment for assigning b into a. Alignment proceeds from the innermost dimension out, with 1s provided beyond the number of dimensions for a or b. An error is returned if the rules of alignment are violated: each dimension size must be either the same, or b is equal to 1. This corresponds to the "broadcasting" logic of NumPy.

func AlignShapes

func AlignShapes(a, b Tensor) (as, bs, os *Shape, err error)

AlignShapes aligns the shapes of two tensors, a and b for a binary computation producing an output, returning the effective aligned shapes for a, b, and the output, all with the same number of dimensions. Alignment proceeds from the innermost dimension out, with 1s provided beyond the number of dimensions for a or b. The output has the max of the dimension sizes for each dimension. An error is returned if the rules of alignment are violated: each dimension size must be either the same, or one of them is equal to 1. This corresponds to the "broadcasting" logic of NumPy.

func NewShape

func NewShape(sizes ...int) *Shape

NewShape returns a new shape with given sizes. RowMajor ordering is used by default.

func Projection2DDimShapes

func Projection2DDimShapes(shp *Shape, onedRow bool) (rowShape, colShape *Shape, rowIdxs, colIdxs []int)

Projection2DDimShapes returns the shapes and dimension indexes for a 2D projection of given tensor Shape, collapsing higher dimensions down to 2D (and 1D up to 2D). For the 1D case, onedRow determines if the values are row-wise or not. Even multiples of inner-most dimensions are placed along the row, odd in the column. If there are an odd number of dimensions, the first dimension is row-wise, and the remaining inner dimensions use the above logic from there, as if it was even. This is the main organizing function for all Projection2D calls.

func (*Shape) CopyFrom

func (sh *Shape) CopyFrom(cp *Shape)

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

func (*Shape) DimSize

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

DimSize returns the size of given dimension.

func (*Shape) IndexFrom1D

func (sh *Shape) IndexFrom1D(oned int) []int

IndexFrom1D 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) IndexTo1D

func (sh *Shape) IndexTo1D(index ...int) int

IndexTo1D returns the flat 1D index from given n-dimensional indicies. No checking is done on the length or size of the index values relative to the shape of the tensor.

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) RowCellSize

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

RowCellSize returns the size of the outermost 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) SetShapeSizes

func (sh *Shape) SetShapeSizes(sizes ...int)

SetShapeSizes sets the shape sizes from list of ints. RowMajor ordering is used by default.

func (*Shape) SetShapeSizesFromTensor

func (sh *Shape) SetShapeSizesFromTensor(sizes Tensor)

SetShapeSizesFromTensor sets the shape sizes from given tensor. RowMajor ordering is used by default.

func (*Shape) SizesAsTensor

func (sh *Shape) SizesAsTensor() *Int

SizesAsTensor returns shape sizes as an Int Tensor.

func (*Shape) String

func (sh *Shape) String() string

String satisfies the fmt.Stringer interface

type Slice

type Slice struct {
	// Start is the starting value. If 0 and Step < 0, = size-1;
	// If negative, = size+Start.
	Start int

	// Stop value. If 0 and Step >= 0, = size;
	// If 0 and Step < 0, = -1, to include whole range.
	// If negative = size+Stop.
	Stop int

	// Step increment. If 0, = 1; if negative then Start must be > Stop
	// to produce anything.
	Step int
}

Slice represents a slice of index values, for extracting slices of data, along a dimension of a given size, which is provided separately as an argument. Uses standard 'for' loop logic with a Start and _exclusive_ Stop value, and a Step increment: for i := Start; i < Stop; i += Step. The values stored in this struct are the _inputs_ for computing the actual slice values based on the actual size parameter for the dimension. Negative numbers count back from the end (i.e., size + val), and the zero value results in a list of all values in the dimension, with Step = 1 if 0. The behavior is identical to the NumPy slice.

func NewSlice

func NewSlice(start, stop, step int) Slice

NewSlice returns a new Slice with given srat, stop, step values.

func (Slice) GetStart

func (sl Slice) GetStart(size int) int

GetStart is the actual start value given the size of the dimension.

func (Slice) GetStep

func (sl Slice) GetStep() int

GetStep is the actual increment value.

func (Slice) GetStop

func (sl Slice) GetStop(size int) int

GetStop is the actual end value given the size of the dimension.

func (Slice) IntSlice

func (sl Slice) IntSlice(size int) []int

IntSlice returns []int slice with slice index values, up to given actual size.

func (Slice) IntTensor

func (sl Slice) IntTensor(size int) *Int

IntTensor returns an Int Tensor for slice, using actual size.

func (Slice) Len

func (sl Slice) Len(size int) int

Len is the number of elements in the actual slice given size of the dimension.

func (Slice) ToIntSlice

func (sl Slice) ToIntSlice(size int, ints []int)

ToIntSlice writes values to given []int slice, with given size parameter for the dimension being sliced. If slice is wrong size to hold values, not all are written: allocate ints using Len(size) to fit.

type Sliced

type Sliced struct {

	// Tensor source that we are an indexed view onto.
	Tensor Tensor

	// Indexes are the indexes for each dimension, with dimensions as the outer
	// slice (enforced to be the same length as the NumDims of the source Tensor),
	// and a list of dimension index values (within range of DimSize(d)).
	// A nil list of indexes for a dimension automatically provides a full,
	// sequential view of that dimension.
	Indexes [][]int
}

Sliced provides a re-sliced view onto another "source" Tensor, defined by a set of [Sliced.Indexes] for each dimension (must have at least 1 index per dimension to avoid a null view). Thus, each dimension can be transformed in arbitrary ways relative to the original tensor (filtered subsets, reversals, sorting, etc). This view is not memory-contiguous and does not support the RowMajor interface or efficient access to inner-dimensional subspaces. A new Sliced view defaults to a full transparent view of the source tensor. There is additional cost for every access operation associated with the indexed indirection, and access is always via the full n-dimensional indexes. See also Rows for a version that only indexes the outermost row dimension, which is much more efficient for this common use-case, and does support RowMajor. To produce a new concrete Values that has raw data actually organized according to the indexed order (i.e., the copy function of numpy), call Sliced.AsValues.

func AsSliced

func AsSliced(tsr Tensor) *Sliced

AsSliced returns the tensor as a Sliced view. If it already is one, then it is returned, otherwise it is wrapped in a new Sliced, with default full sequential ("transparent") view.

func NewSliced

func NewSliced(tsr Tensor, idxs ...[]int) *Sliced

NewSliced returns a new Sliced view of given tensor, with optional list of indexes for each dimension (none / nil = sequential). Any dimensions without indexes default to nil = full sequential view.

func (*Sliced) AsValues

func (sl *Sliced) AsValues() Values

AsValues returns a copy of this tensor as raw Values. This "renders" the Sliced view into a fully contiguous and optimized memory representation of that view, which will be faster to access for further processing, and enables all the additional functionality provided by the Values interface.

func (*Sliced) DataType

func (sl *Sliced) DataType() reflect.Kind

func (*Sliced) DimSize

func (sl *Sliced) DimSize(dim int) int

DimSize returns the effective view size of given dimension.

func (*Sliced) Filter

func (sl *Sliced) Filter(dim int, filterer func(tsr Tensor, dim, idx int) bool)

Filter filters the indexes using the given Filter function for setting the indexes for given dimension, and index into the source data.

func (*Sliced) Float

func (sl *Sliced) Float(i ...int) float64

Float returns the value of given index as a float64. The indexes are indirected through the [Sliced.Indexes].

func (*Sliced) Float1D

func (sl *Sliced) Float1D(i int) float64

Float1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Sliced) IndexesNeeded

func (sl *Sliced) IndexesNeeded(d int)

IndexesNeeded is called prior to an operation that needs actual indexes, on given dimension. If Indexes == nil, they are set to all items, otherwise current indexes are left as is. Use Sequential, then IndexesNeeded to ensure all dimension indexes are represented.

func (*Sliced) Int

func (sl *Sliced) Int(i ...int) int

Int returns the value of given index as an int. The indexes are indirected through the [Sliced.Indexes].

func (*Sliced) Int1D

func (sl *Sliced) Int1D(i int) int

Int1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Sliced) IsString

func (sl *Sliced) IsString() bool

func (*Sliced) Label

func (sl *Sliced) Label() string

func (*Sliced) Len

func (sl *Sliced) Len() int

func (*Sliced) Metadata

func (sl *Sliced) Metadata() *metadata.Data

func (*Sliced) NumDims

func (sl *Sliced) NumDims() int

func (*Sliced) Permuted

func (sl *Sliced) Permuted(dim int)

Permuted sets indexes in given dimension to a permuted order. If indexes already exist then existing list of indexes is permuted, otherwise a new set of permuted indexes are generated

func (*Sliced) Sequential

func (sl *Sliced) Sequential()

Sequential sets all Indexes to nil, resulting in full sequential access into tensor.

func (*Sliced) SetFloat

func (sl *Sliced) SetFloat(val float64, i ...int)

SetFloat sets the value of given index as a float64 The indexes are indirected through the [Sliced.Indexes].

func (*Sliced) SetFloat1D

func (sl *Sliced) SetFloat1D(val float64, i int)

SetFloat1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Sliced) SetInt

func (sl *Sliced) SetInt(val int, i ...int)

SetInt sets the value of given index as an int The indexes are indirected through the [Sliced.Indexes].

func (*Sliced) SetInt1D

func (sl *Sliced) SetInt1D(val int, i int)

SetInt1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Sliced) SetString

func (sl *Sliced) SetString(val string, i ...int)

SetString sets the value of given index as a string The indexes are indirected through the [Sliced.Indexes].

func (*Sliced) SetString1D

func (sl *Sliced) SetString1D(val string, i int)

SetString1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Sliced) SetTensor

func (sl *Sliced) SetTensor(tsr Tensor)

SetTensor sets tensor as source for this view, and initializes a full transparent view onto source (calls Sliced.Sequential).

func (*Sliced) Shape

func (sl *Sliced) Shape() *Shape

func (*Sliced) ShapeSizes

func (sl *Sliced) ShapeSizes() []int

For each dimension, we return the effective shape sizes using the current number of indexes per dimension.

func (*Sliced) SortFunc

func (sl *Sliced) SortFunc(dim int, cmp func(tsr Tensor, i, j int) int)

SortFunc sorts the indexes along given dimension using given compare function. The compare function operates directly on indexes into the source Tensor that Sliced is a view of, as these row numbers have already been projected through the indexes. That is why the tensor is passed through to the compare function, to ensure the proper tensor values are being used. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b.

func (*Sliced) SortIndexes

func (sl *Sliced) SortIndexes(dim int)

SortIndexes sorts the indexes along given dimension directly in numerical order, producing the native ordering, while preserving any filtering that might have occurred.

func (*Sliced) SortStableFunc

func (sl *Sliced) SortStableFunc(dim int, cmp func(tsr Tensor, i, j int) int)

SortStableFunc stably sorts along given dimension using given compare function. The compare function operates directly on indexes into the source Tensor that Sliced is a view of, as these row numbers have already been projected through the indexes. That is why the tensor is passed through to the compare function, to ensure the proper tensor values are being used. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b. It is *essential* that it always returns 0 when the two are equal for the stable function to actually work.

func (*Sliced) SourceIndex

func (sl *Sliced) SourceIndex(dim, idx int) int

SourceIndex returns the actual index into source tensor dimension based on given index value.

func (*Sliced) SourceIndexes

func (sl *Sliced) SourceIndexes(i ...int) []int

SourceIndexes returns the actual n-dimensional indexes into source tensor based on given list of indexes based on the Sliced view shape.

func (*Sliced) SourceIndexesFrom1D

func (sl *Sliced) SourceIndexesFrom1D(oned int) []int

SourceIndexesFrom1D returns the n-dimensional indexes into source tensor based on the given 1D index based on the Sliced view shape.

func (*Sliced) String

func (sl *Sliced) String() string

func (*Sliced) String1D

func (sl *Sliced) String1D(i int) string

String1D is somewhat expensive if indexes are set, because it needs to convert the flat index back into a full n-dimensional index and then use that api.

func (*Sliced) StringValue

func (sl *Sliced) StringValue(i ...int) string

StringValue returns the value of given index as a string. The indexes are indirected through the [Sliced.Indexes].

func (*Sliced) ValidIndexes

func (sl *Sliced) ValidIndexes()

ValidIndexes ensures that [Sliced.Indexes] are valid, removing any out-of-range values and setting the view to nil (full sequential) for any dimension with no indexes (which is an invalid condition). Call this when any structural changes are made to underlying Tensor.

type SlicesMagic

type SlicesMagic int //enums:enum

SlicesMagic are special elements in slice expressions, including NewAxis, FullAxis, and Ellipsis in NewSliced expressions.

const (
	// FullAxis indicates that the full existing axis length should be used.
	// This is equivalent to Slice{}, but is more semantic. In NumPy it is
	// equivalent to a single : colon.
	FullAxis SlicesMagic = iota

	// NewAxis creates a new singleton (length=1) axis, used to to reshape
	// without changing the size. Can also be used in [Reshaped].
	NewAxis

	// Ellipsis (...) is used in [NewSliced] expressions to produce
	// a flexibly-sized stretch of FullAxis dimensions, which automatically
	// aligns the remaining slice elements based on the source dimensionality.
	Ellipsis
)
const SlicesMagicN SlicesMagic = 3

SlicesMagicN is the highest valid value for type SlicesMagic, plus one.

func SlicesMagicValues

func SlicesMagicValues() []SlicesMagic

SlicesMagicValues returns all possible values for the type SlicesMagic.

func (SlicesMagic) Desc

func (i SlicesMagic) Desc() string

Desc returns the description of the SlicesMagic value.

func (SlicesMagic) Int64

func (i SlicesMagic) Int64() int64

Int64 returns the SlicesMagic value as an int64.

func (SlicesMagic) MarshalText

func (i SlicesMagic) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*SlicesMagic) SetInt64

func (i *SlicesMagic) SetInt64(in int64)

SetInt64 sets the SlicesMagic value from an int64.

func (*SlicesMagic) SetString

func (i *SlicesMagic) SetString(s string) error

SetString sets the SlicesMagic value from its string representation, and returns an error if the string is invalid.

func (SlicesMagic) String

func (i SlicesMagic) String() string

String returns the string representation of this SlicesMagic value.

func (*SlicesMagic) UnmarshalText

func (i *SlicesMagic) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (SlicesMagic) Values

func (i SlicesMagic) Values() []enums.Enum

Values returns all possible values for the type SlicesMagic.

type String

type String struct {
	Base[string]
}

String is a tensor of string values

func AsString

func AsString(tsr Tensor) *String

AsString returns the tensor as a String tensor. If already is a String, it is returned as such. Otherwise, a new String tensor is created and values are copied.

func NewString

func NewString(sizes ...int) *String

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

func NewStringFromValues

func NewStringFromValues(vals ...string) *String

NewStringFromValues returns a new 1-dimensional tensor of given value type initialized directly from the given slice values, which are not copied. The resulting Tensor thus "wraps" the given values.

func NewStringFull

func NewStringFull(val string, sizes ...int) *String

NewStringFull returns a new tensor full of given scalar value, of given shape sizes.

func NewStringScalar

func NewStringScalar(val string) *String

NewStringScalar is a convenience method for a Tensor representation of a single string scalar value.

func NewStringShape

func NewStringShape(shape *Shape) *String

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

func (*String) AppendFrom

func (tsr *String) AppendFrom(frm Values) Values

AppendFrom appends values from other tensor into this tensor, which must have the same cell size as this tensor. It uses and optimized implementation if the other tensor is of the same type, and otherwise it goes through appropriate standard type.

func (*String) AppendRow

func (tsr *String) AppendRow(val Values)

AppendRow adds a row and sets values to given values.

func (*String) AppendRowFloat

func (tsr *String) AppendRowFloat(val ...float64)

AppendRowFloat adds a row and sets float value(s), up to number of cells.

func (*String) AppendRowInt

func (tsr *String) AppendRowInt(val ...int)

AppendRowInt adds a row and sets int value(s), up to number of cells.

func (*String) AppendRowString

func (tsr *String) AppendRowString(val ...string)

AppendRowString adds a row and sets string value(s), up to number of cells.

func (*String) AsValues

func (tsr *String) AsValues() Values

func (*String) Bytes added in v0.1.2

func (tsr *String) Bytes() []byte

Bytes encodes the actual string values starting with the length of each string as an encoded int value. SetFromBytes decodes this format to reconstruct the contents.

func (*String) Clone

func (tsr *String) Clone() Values

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 Values, 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 Values)

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) Float

func (tsr *String) Float(i ...int) float64

func (*String) Float1D

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

func (*String) FloatRow

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

func (*String) Int

func (tsr *String) Int(i ...int) int

func (*String) Int1D

func (tsr *String) Int1D(i int) int

func (*String) IntRow

func (tsr *String) IntRow(row, cell int) int

func (*String) IsString

func (tsr *String) IsString() bool

func (*String) RowTensor

func (tsr *String) RowTensor(row int) Values

RowTensor is a convenience version of [RowMajor.SubSpace] to return the SubSpace for the outermost row dimension. Rows defines a version of this that indirects through the row indexes.

func (*String) SetFloat

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

func (*String) SetFloat1D

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

func (*String) SetFloatRow

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

func (*String) SetFromBytes added in v0.1.2

func (tsr *String) SetFromBytes(b []byte)

SetFromBytes sets string values from strings encoded using String.Bytes function.

func (*String) SetInt

func (tsr *String) SetInt(val int, i ...int)

func (*String) SetInt1D

func (tsr *String) SetInt1D(val int, i int)

func (*String) SetIntRow

func (tsr *String) SetIntRow(val int, row, cell int)

func (*String) SetRowTensor

func (tsr *String) SetRowTensor(val Values, row int)

SetRowTensor sets the values of the SubSpace at given row to given values.

func (*String) SetString

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

func (*String) SetString1D

func (tsr *String) SetString1D(val string, i int)

func (*String) SetStringRow

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

func (*String) SetZeros

func (tsr *String) SetZeros()

SetZeros is a simple convenience function initialize all values to the zero value of the type (empty strings for string type).

func (*String) String

func (tsr *String) String() string

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

func (*String) String1D

func (tsr *String) String1D(i int) string

func (*String) StringRow

func (tsr *String) StringRow(row, cell int) string

func (*String) SubSpace

func (tsr *String) SubSpace(offs ...int) Values

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.

type StringMatch

type StringMatch struct {

	// Contains means the string only needs to contain the target string,
	// with the default (false) requiring a complete match to entire string.
	Contains bool

	// IgnoreCase means that differences in case are ignored in comparing strings,
	// with the default (false) using case.
	IgnoreCase bool

	// Exclude means to exclude matches,
	// with the default (false) being to include.
	Exclude bool
}

StringMatch are options for how to compare strings.

func (*StringMatch) Match

func (sm *StringMatch) Match(trg, str string) bool

Match compares two strings according to the options. The trg is the target string that you are comparing, such that it must contain the given str string, not the other way around.

type Tensor

type Tensor interface {
	fmt.Stringer

	// Label satisfies the core.Labeler interface for a summary
	// description of the tensor, including metadata Name if set.
	Label() string

	// Metadata returns the metadata for this tensor, which can be used
	// to encode name, docs, shape dimension names, plotting options, etc.
	Metadata() *metadata.Data

	// Shape() returns a [Shape] representation of the tensor shape
	// (dimension sizes). For tensors that present a view onto another
	// tensor, this typically must be constructed.
	// In general, it is better to use the specific [Tensor.ShapeSizes],
	// [Tensor.ShapeSizes], [Tensor.DimSize] etc as neeed.
	Shape() *Shape

	// ShapeSizes returns the sizes of each dimension as a slice of ints.
	// The returned slice is a copy and can be modified without side effects.
	ShapeSizes() []int

	// Len returns the total number of elements in the tensor,
	// i.e., the product of all shape dimensions.
	// Len must always be such that the 1D() accessors return
	// values using indexes from 0..Len()-1.
	Len() int

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

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

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

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

	// AsValues returns this tensor as raw [Values]. If it already is,
	// it is returned directly. If it is a View tensor, the view is
	// "rendered" into a fully contiguous and optimized [Values] representation
	// of that view, which will be faster to access for further processing,
	// and enables all the additional functionality provided by the [Values] interface.
	AsValues() Values

	// Float returns the value of given n-dimensional index (matching Shape) as a float64.
	Float(i ...int) float64

	// SetFloat sets the value of given n-dimensional index (matching Shape) as a float64.
	SetFloat(val float64, i ...int)

	// Float1D returns the value of given 1-dimensional index (0-Len()-1) as a float64.
	// If index is negative, it indexes from the end of the list (-1 = last).
	// This can be somewhat expensive in wrapper views ([Rows], [Sliced]), which
	// convert the flat index back into a full n-dimensional index and use that api.
	// [Tensor.FloatRow] is preferred.
	Float1D(i int) float64

	// SetFloat1D sets the value of given 1-dimensional index (0-Len()-1) as a float64.
	// If index is negative, it indexes from the end of the list (-1 = last).
	// This can be somewhat expensive in the commonly-used [Rows] view;
	// [Tensor.SetFloatRow] is preferred.
	SetFloat1D(val float64, i int)

	// StringValue returns the value of given n-dimensional index (matching Shape) as a string.
	// 'String' conflicts with [fmt.Stringer], so we have to use StringValue here.
	StringValue(i ...int) string

	// SetString sets the value of given n-dimensional index (matching Shape) as a string.
	SetString(val string, i ...int)

	// String1D returns the value of given 1-dimensional index (0-Len()-1) as a string.
	// If index is negative, it indexes from the end of the list (-1 = last).
	String1D(i int) string

	// SetString1D sets the value of given 1-dimensional index (0-Len()-1) as a string.
	// If index is negative, it indexes from the end of the list (-1 = last).
	SetString1D(val string, i int)

	// Int returns the value of given n-dimensional index (matching Shape) as a int.
	Int(i ...int) int

	// SetInt sets the value of given n-dimensional index (matching Shape) as a int.
	SetInt(val int, i ...int)

	// Int1D returns the value of given 1-dimensional index (0-Len()-1) as a int.
	// If index is negative, it indexes from the end of the list (-1 = last).
	Int1D(i int) int

	// SetInt1D sets the value of given 1-dimensional index (0-Len()-1) as a int.
	// If index is negative, it indexes from the end of the list (-1 = last).
	SetInt1D(val int, i int)
}

Tensor is the most general 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 for raw Values with direct integer indexing by the Number, String, and Bool types, covering the different concrete types specified by DataTypes (see Values for additional interface methods for raw value types). For float32 and float64 values, use NaN to indicate missing values, as all of the data analysis and plot packages skip NaNs. View Tensor types provide different ways of viewing a source tensor, including Sliced for arbitrary slices of dimension indexes, Masked for boolean masked access of individual elements, and Indexed for arbitrary indexes of values, organized into the shape of the indexes, not the original source data. Reshaped provides length preserving reshaping (mostly for computational alignment purposes), and Rows provides an optimized row-indexed view for [table.Table] data.

func AnySlice added in v0.1.2

func AnySlice(tsr Tensor, idx ...any) Tensor

AnySlice returns a new Tensor view using the given index variables to be used for Sliced, Masked, or Indexed depending on what is present.

  • If a Bool tensor is provided then NewMasked is called.
  • If a single tensor is provided with Len > max(1, tsr.NumDims()), NewIndexed is called.
  • Otherwise, Reslice is called with the args.

func As1D

func As1D(tsr Tensor) Tensor

As1D returns a 1D tensor, which is either the input tensor if it is already 1D, or a new Reshaped 1D view of it. This can be useful e.g., for stats and metric functions that operate on a 1D list of values. See also Flatten.

func Cells1D

func Cells1D(tsr Tensor, row int) Tensor

Cells1D returns a flat 1D view of the innermost cells for given row index. For a RowMajor tensor, it uses the [RowTensor] subspace directly, otherwise it uses Sliced to extract the cells. In either case, As1D is used to ensure the result is a 1D tensor.

func FloatBinaryFunc

func FloatBinaryFunc(flops int, fun func(a, b float64) float64, a, b Tensor) Tensor

FloatBinaryFunc sets output to a binary function of a, b float64 values. The flops (floating point operations) estimate is used to control parallel threading using goroutines, and should reflect number of flops in the function. See VectorizeThreaded for more information.

func Mask

func Mask(tsr, mask Tensor) Tensor

Mask is the general purpose masking function, which checks if the mask arg is a Bool and uses if so. Otherwise, it logs an error.

func Reshape

func Reshape(tsr Tensor, sizes ...int) Tensor

Reshape returns a view of the given tensor with given shape sizes. A single -1 value can be used to automatically specify the remaining tensor length, as long as the other sizes are an even multiple of the total length. A single -1 returns a 1D view of the entire tensor.

func Reslice

func Reslice(tsr Tensor, sls ...any) Tensor

Reslice returns a new Sliced (and potentially Reshaped) view of given tensor, with given slice expressions for each dimension, which can be:

  • an integer, indicating a specific index value along that dimension. Can use negative numbers to index from the end. This axis will also be removed using a Reshaped.
  • a Slice object expressing a range of indexes.
  • FullAxis includes the full original axis (equivalent to `Slice{}`).
  • Ellipsis creates a flexibly-sized stretch of FullAxis dimensions, which automatically aligns the remaining slice elements based on the source dimensionality.
  • NewAxis creates a new singleton (length=1) axis, used to to reshape without changing the size. This triggers a Reshaped.
  • any remaining dimensions without indexes default to nil = full sequential view.

func Squeeze

func Squeeze(tsr Tensor) Tensor

Squeeze a Reshaped view of given tensor with all singleton (size = 1) dimensions removed (if none, just returns the tensor).

func StringBinaryFunc

func StringBinaryFunc(fun func(a, b string) string, a, b Tensor) Tensor

StringBinaryFunc sets output to a binary function of a, b string values.

func Transpose

func Transpose(tsr Tensor) Tensor

Transpose returns a new Reshaped tensor with the strides switched so that rows and column dimensions are effectively reversed.

type Uint32

type Uint32 = Number[uint32]

Uint32 is an alias for Number[uint32].

func NewUint32

func NewUint32(sizes ...int) *Uint32

NewUint32 returns a new Uint32 tensor with the given sizes per dimension (shape).

type Values

type Values interface {
	RowMajor

	// SetShapeSizes sets the dimension sizes of the tensor, and resizes
	// backing storage appropriately, retaining all existing data that fits.
	SetShapeSizes(sizes ...int)

	// SetNumRows sets the number of rows (outermost dimension).
	// It is safe to set this to 0. For incrementally growing tensors (e.g., a log)
	// it is best to first set the anticipated full size, which allocates the
	// full amount of memory, and then set to 0 and grow incrementally.
	SetNumRows(rows int)

	// Sizeof returns the number of bytes contained in the Values of this tensor.
	// For String types, this is just the string pointers, not the string content.
	Sizeof() int64

	// Bytes returns the underlying byte representation of the tensor values.
	// This is the actual underlying data, so make a copy if it can be
	// unintentionally modified or retained more than for immediate use.
	// For the [String] type, this is a len int + bytes encoding of each string.
	Bytes() []byte

	// SetFromBytes sets the values from given bytes. See [Values.Bytes] for
	// the [String] encoding.
	SetFromBytes(b []byte)

	// SetZeros is a convenience function initialize all values to the
	// zero value of the type (empty strings for string type).
	// New tensors always start out with zeros.
	SetZeros()

	// Clone clones this tensor, creating a duplicate copy of itself with its
	// own separate memory representation of all the values.
	Clone() Values

	// CopyFrom copies all 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 the appropriate standard type (Float, Int, String).
	CopyFrom(from Values)

	// 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 Values, to, start, n int)

	// AppendFrom appends all 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 the appropriate standard type (Float, Int, String).
	AppendFrom(from Values) Values
}

Values is an extended Tensor interface for raw value tensors. This supports direct setting of the shape of the underlying values, sub-space access to inner-dimensional subspaces of values, etc.

func CallOut1

func CallOut1(fun func(a Tensor, out Values) error, a Tensor) Values

CallOut1 adds output Values tensor for function.

func CallOut1Float64

func CallOut1Float64(fun func(a Tensor, out Values) error, a Tensor) Values

CallOut1Float64 adds Float64 output Values tensor for function.

func CallOut1Gen1

func CallOut1Gen1[T any](fun func(g T, a Tensor, out Values) error, g T, a Tensor) Values

func CallOut1Gen2

func CallOut1Gen2[T any, S any](fun func(g T, h S, a Tensor, out Values) error, g T, h S, a Tensor) Values

func CallOut2

func CallOut2(fun func(a, b Tensor, out Values) error, a, b Tensor) Values

func CallOut2Float64

func CallOut2Float64(fun func(a, b Tensor, out Values) error, a, b Tensor) Values

func CallOut2Gen1

func CallOut2Gen1[T any](fun func(g T, a, b Tensor, out Values) error, g T, a, b Tensor) Values

func CallOut2Gen2

func CallOut2Gen2[T any, S any](fun func(g T, h S, a, b Tensor, out Values) error, g T, h S, a, b Tensor) Values

func CallOut3

func CallOut3(fun func(a, b, c Tensor, out Values) error, a, b, c Tensor) Values

func Clone

func Clone(tsr Tensor) Values

Clone returns a copy of the given tensor. If it is raw Values then a [Values.Clone] is returned. Otherwise if it is a view, then [Tensor.AsValues] is returned. This is equivalent to the NumPy copy function.

func Flatten

func Flatten(tsr Tensor) Values

Flatten returns a copy of the given tensor as a 1D flat list of values, by calling Clone(As1D(tsr)). It is equivalent to the NumPy flatten function.

func FloatFunc

func FloatFunc(flops int, fun func(in float64) float64, in Tensor) Values

FloatFunc sets output to a function of tensor float64 values. The flops (floating point operations) estimate is used to control parallel threading using goroutines, and should reflect number of flops in the function. See VectorizeThreaded for more information.

func FromBinary added in v0.1.2

func FromBinary(b []byte) Values

FromBinary returns a Values tensor reconstructed from the binary encoding generated by ToBinary.

func MustBeValues

func MustBeValues(tsr Tensor) (Values, error)

MustBeValues returns the given tensor as a Values subtype, or nil and an error if it is not one. Typically outputs of compute operations must be values, and are reshaped to hold the results as needed.

func New

func New[T DataTypes](sizes ...int) Values

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

func NewFromValues added in v0.1.1

func NewFromValues(val ...any) Values

NewFromValues returns a new Values tensor from given list of values which must be the same type, for the supported tensor types including string, bool, and standard float, int types.

func NewOfType

func NewOfType(typ reflect.Kind, sizes ...int) Values

NewOfType returns a new n-dimensional tensor of given reflect.Kind type with the given sizes per dimension (shape). Types supported are listed in DataTypes.

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.
Package tensormpi has methods to support use of MPI with tensor and table data structures.
Package tensormpi has methods to support use of MPI with tensor and table data structures.
Package tmath provides basic math operations and functions that operate on tensor.Tensor.
Package tmath provides basic math operations and functions that operate on tensor.Tensor.

Jump to

Keyboard shortcuts

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