na64

package
v0.0.0-...-f1469ca Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2015 License: BSD-3-Clause Imports: 9 Imported by: 5

Documentation

Overview

Package narray provides functions to opearate with multidimensional arrays of type float64. The NArray object is a dense, fixed-size, array of rank n.

The shape is a vector with the size of each dimension. Typical cases:

type      rank   example
--------------------------------
scalar    0      na := New()
vector    1      na := New(12)
matrix    2      na := New(5,17)
cube      3      na := New(2,3,5)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dot

func Dot(in ...*NArray) float64

Dot computes the sum of the elementwise products of the input arrays.

y = sum_{i = 0}^(N-1) x0[i]*x1[i]*...x_n-1[i]

Will panic if there are not at least two input narrays or if narray shapes don't match.

func EqualShape

func EqualShape(x *NArray, ys ...*NArray) bool

EqualShape returns true if all the arrays have equal length, and false otherwise. Returns true if there is only one input array.

func EqualValues

func EqualValues(x *NArray, y *NArray, tol float64) bool

EqualValues compares two narrays elementwise. Returns true if for all elements |x-y|/(|avg(x,y)|+1) < tol.

Types

type ApplyFunc

type ApplyFunc func(x float64) float64

ApplyFunc is a type for creating custom functions.

type Matrix

type Matrix NArray

Matrix is as an NArray of rank 2 that satisfies the gonum Matrix interfaces.

func (*Matrix) At

func (mat *Matrix) At(r, c int) float64

At returns the value of a matrix element at (r, c). It will panic if r or c are out of bounds for the matrix.

func (*Matrix) Col

func (mat *Matrix) Col(dst []float64, j int) []float64

Col returns a slice of float64 for the column specified. It will panic if the index is out of bounds. If the call requires a copy and dst is not nil it will be used and returned, if it is not nil the number of elements copied will be the minimum of the length of the slice and the number of rows in the matrix.

func (*Matrix) Dims

func (mat *Matrix) Dims() (r, c int)

Dims returns the dimensions of a Matrix.

func (*Matrix) Row

func (mat *Matrix) Row(dst []float64, i int) []float64

Row returns a slice of float64 for the row specified. It will panic if the index is out of bounds. If the call requires a copy and dst is not nil it will be used and returned, if it is not nil the number of elements copied will be the minimum of the length of the slice and the number of columns in the matrix.

func (*Matrix) Set

func (mat *Matrix) Set(r, c int, v float64)

Set alters the matrix element at (r, c) to v. It will panic if r or c are out of bounds for the matrix.

func (*Matrix) SetCol

func (mat *Matrix) SetCol(j int, src []float64) int

SetCol sets the values of the specified column to the values held in a slice of float64. It will panic if the index is out of bounds. The number of elements copied is returned and will be the minimum of the length of the slice and the number of rows in the matrix.

func (*Matrix) SetRow

func (mat *Matrix) SetRow(i int, src []float64) int

SetRow sets the values of the specified row to the values held in a slice of float64. It will panic if the index is out of bounds. The number of elements copied is returned and will be the minimum of the length of the slice and the number of columns in the matrix.

func (*Matrix) String

func (mat *Matrix) String() string

String returns vector as a printable string.

type NArray

type NArray struct {
	// The rank or order or degree of the narray is the dimensionality required to represent it. (eg. The rank of a vector is 1)
	Rank int `json:"rank"`
	// The shape is an int slice that contains the size of each dimension. Subscripts range from zero to s-1. Where s is the size of a dimension.
	Shape []int `json:"shape"`
	// The data is stored as a slice of float64 numbers.
	Data []float64 `json:"data"`
	// Strides for each dimension.
	Strides []int `json:"strides"`
}

The NArray object.

func Abs

func Abs(out, in *NArray) *NArray

Abs returns square root values of narrays elementwise. out = math.Abs(in) If out is nil a new array is created.

func Acos

func Acos(out, in *NArray) *NArray

Acos applies math.Acos() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Acosh

func Acosh(out, in *NArray) *NArray

Acosh applies math.Acosh() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Add

func Add(out *NArray, in ...*NArray) *NArray

Add adds narrays elementwise.

out = sum_i(in[i])

Will panic if there are not at least two input narrays or if narray shapes don't match. If out is nil a new array is created.

func AddConst

func AddConst(out *NArray, in *NArray, c float64) *NArray

AddConst adds const to an narray elementwise. out = in + c If out is nil a new array is created.

func AddScaled

func AddScaled(y *NArray, x *NArray, a float64) *NArray

AddScaled adds a scaled narray elementwise. y = y + a * x If y is nil a new array is created.

func Apply

func Apply(out, in *NArray, fn ApplyFunc) *NArray

Apply function of type ApplyFunc to a multidimensional array. If out is nil, a new object is allocated.

func Asin

func Asin(out, in *NArray) *NArray

Asin applies math.Asin() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Asinh

func Asinh(out, in *NArray) *NArray

Asinh applies math.Asinh() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Atan

func Atan(out, in *NArray) *NArray

Atan applies math.Atan() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Atan2

func Atan2(out, a, b *NArray) *NArray

Atan2 applies math.Atan2() elementwise to two multidimensional arrays. See math package in standard lib for details.

If out is nil a new array is created. Will panic if 'out', 'a' and 'b' shapes don't match.

func Atanh

func Atanh(out, in *NArray) *NArray

Atanh applies math.Atanh() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Cbrt

func Cbrt(out, in *NArray) *NArray

Cbrt applies math.Cbrt() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Ceil

func Ceil(out, in *NArray) *NArray

Ceil applies math.Ceil() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Copysign

func Copysign(out, a, b *NArray) *NArray

Copysign returns values with the magnitude of a and the sign of b for each element of the arrays. Will panic if narray shapes don't match. If out is nil a new array is created.

func Cos

func Cos(out, in *NArray) *NArray

Cos applies math.Cos() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Cosh

func Cosh(out, in *NArray) *NArray

Cosh applies math.Cosh() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Dim

func Dim(out, a, b *NArray) *NArray

Dim applies math.Dim() elementwise to two multidimensional arrays. See math package in standard lib for details.

If out is nil a new array is created. Will panic if 'out', 'a' and 'b' shapes don't match.

func Div

func Div(out *NArray, in ...*NArray) *NArray

Div divides narrays elementwise.

out = in[0] / in[1] / in[2] ....

Will panic if there are not at least two input narrays or if narray shapes don't match. If out is nil a new array is created.

func Erf

func Erf(out, in *NArray) *NArray

Erf applies math.Erf() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Erfc

func Erfc(out, in *NArray) *NArray

Erfc applies math.Erfc() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Exp

func Exp(out, in *NArray) *NArray

Exp applies math.Exp() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Exp2

func Exp2(out, in *NArray) *NArray

Exp2 applies math.Exp2() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Expm1

func Expm1(out, in *NArray) *NArray

Expm1 applies math.Expm1() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Floor

func Floor(out, in *NArray) *NArray

Floor applies math.Floor() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Gamma

func Gamma(out, in *NArray) *NArray

Gamma applies math.Gamma() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Hypot

func Hypot(out, a, b *NArray) *NArray

Hypot applies math.Hypot() elementwise to two multidimensional arrays. See math package in standard lib for details.

If out is nil a new array is created. Will panic if 'out', 'a' and 'b' shapes don't match.

func J0

func J0(out, in *NArray) *NArray

J0 applies math.J0() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func J1

func J1(out, in *NArray) *NArray

J1 applies math.J1() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Log

func Log(out, in *NArray) *NArray

Log applies math.Log() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Log10

func Log10(out, in *NArray) *NArray

Log10 applies math.Log10() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Log1p

func Log1p(out, in *NArray) *NArray

Log1p applies math.Log1p() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Log2

func Log2(out, in *NArray) *NArray

Log2 applies math.Log2() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Logb

func Logb(out, in *NArray) *NArray

Logb applies math.Logb() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func MaxArray

func MaxArray(out *NArray, in ...*NArray) *NArray

MaxArray compare input narrays and returns an narray containing the element-wise maxima.

out[i,j,k,...] = max(in0[i,j,k,...], in1[i,j,k,...], ...)

Will panic if there are not at least two input narray or if narray shapes don't match. If out is nil a new array is created.

func MinArray

func MinArray(out *NArray, in ...*NArray) *NArray

MinArray compare input narrays and returns an narray containing the element-wise minima.

out[i,j,k,...] = min(in0[i,j,k,...], in1[i,j,k,...], ...)

Will panic if there are not at least two input narray or if narray shapes don't match. If out is nil a new array is created.

func Mod

func Mod(out, a, b *NArray) *NArray

Mod applies math.Mod() elementwise to two multidimensional arrays. See math package in standard lib for details.

If out is nil a new array is created. Will panic if 'out', 'a' and 'b' shapes don't match.

func Mul

func Mul(out *NArray, in ...*NArray) *NArray

Mul multiplies narrays elementwise.

out = prod_i(in[i])

Will panic if there are not at least two input narrays or if narray shapes don't match. If out is nil a new array is created.

func New

func New(shape ...int) *NArray

New creates a new n-dimensional array.

func NewArray

func NewArray(a []float64, shape ...int) *NArray

NewArray creates a new n-dimensional array with content of a slice The size of the slice must match the product of the slice, otherwise a panic will occur

func Norm

func Norm(r *rand.Rand, mean, sd float64, shape ...int) *NArray

Norm creates a new n-dimensional array whose elements are drawn from a Normal probability density function.

func Pow

func Pow(out, a, b *NArray) *NArray

Pow applies math.Pow() elementwise to two multidimensional arrays. See math package in standard lib for details.

If out is nil a new array is created. Will panic if 'out', 'a' and 'b' shapes don't match.

func Rand

func Rand(r *rand.Rand, shape ...int) *NArray

Rand creates a new n-dimensional array whose elements are set using the rand.Float64 function. Values are pseudo-random numbers in [0.0,1.0).

func Rcp

func Rcp(out, in *NArray) *NArray

Rcp returns reciprocal values of narrays elementwise. out = 1.0 / in If out is nil a new array is created.

func Read

func Read(r io.Reader) (*NArray, error)

Read unmarshals json data from an io.Reader into an narray struct.

func ReadFile

func ReadFile(fn string) (*NArray, error)

ReadFile unmarshals json data from a file into an narray struct.

func Remainder

func Remainder(out, a, b *NArray) *NArray

Remainder applies math.Remainder() elementwise to two multidimensional arrays. See math package in standard lib for details.

If out is nil a new array is created. Will panic if 'out', 'a' and 'b' shapes don't match.

func Scale

func Scale(out *NArray, in *NArray, c float64) *NArray

Scale multiplies an narray by a factor elementwise. out = c * in If out is nil a new array is created.

func Sin

func Sin(out, in *NArray) *NArray

Sin applies math.Sin() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Sinh

func Sinh(out, in *NArray) *NArray

Sinh applies math.Sinh() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Sqrt

func Sqrt(out, in *NArray) *NArray

Sqrt returns square root values of narrays elementwise. out = math.Sqrt(in) If out is nil a new array is created.

func Sub

func Sub(out *NArray, in ...*NArray) *NArray

Sub subtracts narrays elementwise.

out = in[0] - in[1] - in[2] ....

Will panic if there are not at least two input narrays or if narray shapes don't match. If out is nil a new array is created.

func Tan

func Tan(out, in *NArray) *NArray

Tan applies math.Tan() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Tanh

func Tanh(out, in *NArray) *NArray

Tanh applies math.Tanh() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Trunc

func Trunc(out, in *NArray) *NArray

Trunc applies math.Trunc() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Y0

func Y0(out, in *NArray) *NArray

Y0 applies math.Y0() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func Y1

func Y1(out, in *NArray) *NArray

Y1 applies math.Y1() elementwise to a multidimensional array. See math package in standard lib for details.

If 'out' is nil a new array is created. Will panic if 'out' and 'in' shapes don't match.

func (*NArray) At

func (na *NArray) At(indices ...int) float64

At returns the value for indices.

func (*NArray) Copy

func (na *NArray) Copy() *NArray

Copy returns a copy on the narray.

func (*NArray) Decode

func (na *NArray) Decode(inf, nan []int)

Decode converts values in-place. See Encode() for details.

func (*NArray) Encode

func (na *NArray) Encode() (inf, nan []int)

Encode converts values in-place as follows:

Inf to math.MaxFloat64
-Inf to -math.MaxFloat64
NaN ro 0

Returns the indices of the modified values as follows:

Values in inf => na.Data[abs(v)] = sign(v) * Inf
Values in nan => na.Data[v] = NaN

func (*NArray) Inc

func (na *NArray) Inc(v float64, indices ...int)

Inc increments the value of an narray element.

func (*NArray) Index

func (na *NArray) Index(indices ...int) int

Index transforms a set of subscripts to a an index in the underlying one-dimensional slice.

func (*NArray) MarshalJSON

func (na *NArray) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface. The custom marshaller is needed to encode Inf/NaN values.

func (*NArray) Matrix

func (na *NArray) Matrix(query ...int) *Matrix

Matrix creates a subarray of rank 2. Equivalent to SubArray but restricted to the case where the resulting subarray has rank=2. (It will panic otherwise.) See SubArray for details.

func (*NArray) Max

func (na *NArray) Max() float64

Max returns the max value in the narray.

func (*NArray) MaxElem

func (na *NArray) MaxElem(v float64, indices ...int)

MaxElem compares value to element and replaces element if value is greater than element.

func (*NArray) MaxIdx

func (na *NArray) MaxIdx() (float64, []int)

MaxIdx returns the max value and corresponding indices.

func (*NArray) Min

func (na *NArray) Min() float64

Min returns the min value in the narray.

func (*NArray) MinElem

func (na *NArray) MinElem(v float64, indices ...int)

MinElem compares value to element and replaces element if value is less than element.

func (*NArray) MinIdx

func (na *NArray) MinIdx() (float64, []int)

MinIdx returns the min value and corresponding indices.

func (*NArray) Prod

func (na *NArray) Prod() float64

Prod returns the products of all the elements in the narray.

func (*NArray) Reshape

func (na *NArray) Reshape(dim ...int) *NArray

Reshape returns an narray with a new shape.

func (*NArray) ReverseIndex

func (na *NArray) ReverseIndex(idx int) []int

ReverseIndex converts a linear index to narray indices.

func (*NArray) Set

func (na *NArray) Set(v float64, indices ...int)

Set value for indices.

func (*NArray) SetValue

func (na *NArray) SetValue(v float64) *NArray

SetValue sets all elements to value.

func (*NArray) Sprint

func (na *NArray) Sprint(f func(na *NArray, index int) bool) string

Sprint prints narray elements when f returns true. index is the linear index of an narray.

func (*NArray) String

func (na *NArray) String() string

String prints the narray

func (*NArray) SubArray

func (na *NArray) SubArray(query ...int) *NArray

SubArray returns an narray of lower rank as follows:

Example, given an narray with shape 2x3x4 (rank=3), return the subarray of rank=2 corresponding to dim[2]=1

x := New(2,3,4)
y := x.SubArray(-1,-1,1) // use -1 to select a dimension. Put a 1 in dim=2 (third argument).
// y = {x(0,0,1), x(0,1,1), x(0,2,1), x(1,0,1), ...}

func (*NArray) Sum

func (na *NArray) Sum() float64

Sum returns the sum of all the elements in the narray.

func (*NArray) ToJSON

func (na *NArray) ToJSON() (string, error)

ToJSON returns a json string.

func (*NArray) UnmarshalJSON

func (na *NArray) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unarshaller interface. The custom unmarshaller is needed to decode Inf/NaN values.

func (*NArray) Vector

func (na *NArray) Vector(query ...int) *Vector

Vector creates a subarray of rank 1. Equivalent to SubArray but restricted to the case where the resulting subarray has rank=1. (It will panic otherwise.) See SubArray for details.

Example, given a 5x10 matrix (rank=2), return the vector of dim 10 for row idx=3:

x := New(5,10)
y := x.Vector(3,-1)
// y = {x_30, x_31, ... , x_39}

func (*NArray) Write

func (na *NArray) Write(w io.Writer) error

Write writes narray to an io.Writer.

func (*NArray) WriteFile

func (na *NArray) WriteFile(fn string) error

WriteFile writes an narray to a file.

type Vector

type Vector NArray

Vector is as an NArray of rank 1 that satisfies the gonum Vectorer interface.

Jump to

Keyboard shortcuts

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