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 ¶
- func Dot(in ...*NArray) float64
- func EqualShape(x *NArray, ys ...*NArray) bool
- func EqualValues(x *NArray, y *NArray, tol float64) bool
- type ApplyFunc
- type Matrix
- func (mat *Matrix) At(r, c int) float64
- func (mat *Matrix) Col(dst []float64, j int) []float64
- func (mat *Matrix) Dims() (r, c int)
- func (mat *Matrix) Row(dst []float64, i int) []float64
- func (mat *Matrix) Set(r, c int, v float64)
- func (mat *Matrix) SetCol(j int, src []float64) int
- func (mat *Matrix) SetRow(i int, src []float64) int
- func (mat *Matrix) String() string
- type NArray
- func Abs(out, in *NArray) *NArray
- func Acos(out, in *NArray) *NArray
- func Acosh(out, in *NArray) *NArray
- func Add(out *NArray, in ...*NArray) *NArray
- func AddConst(out *NArray, in *NArray, c float64) *NArray
- func AddScaled(y *NArray, x *NArray, a float64) *NArray
- func Apply(out, in *NArray, fn ApplyFunc) *NArray
- func Asin(out, in *NArray) *NArray
- func Asinh(out, in *NArray) *NArray
- func Atan(out, in *NArray) *NArray
- func Atan2(out, a, b *NArray) *NArray
- func Atanh(out, in *NArray) *NArray
- func Cbrt(out, in *NArray) *NArray
- func Ceil(out, in *NArray) *NArray
- func Copysign(out, a, b *NArray) *NArray
- func Cos(out, in *NArray) *NArray
- func Cosh(out, in *NArray) *NArray
- func Dim(out, a, b *NArray) *NArray
- func Div(out *NArray, in ...*NArray) *NArray
- func Erf(out, in *NArray) *NArray
- func Erfc(out, in *NArray) *NArray
- func Exp(out, in *NArray) *NArray
- func Exp2(out, in *NArray) *NArray
- func Expm1(out, in *NArray) *NArray
- func Floor(out, in *NArray) *NArray
- func Gamma(out, in *NArray) *NArray
- func Hypot(out, a, b *NArray) *NArray
- func J0(out, in *NArray) *NArray
- func J1(out, in *NArray) *NArray
- func Log(out, in *NArray) *NArray
- func Log10(out, in *NArray) *NArray
- func Log1p(out, in *NArray) *NArray
- func Log2(out, in *NArray) *NArray
- func Logb(out, in *NArray) *NArray
- func MaxArray(out *NArray, in ...*NArray) *NArray
- func MinArray(out *NArray, in ...*NArray) *NArray
- func Mod(out, a, b *NArray) *NArray
- func Mul(out *NArray, in ...*NArray) *NArray
- func New(shape ...int) *NArray
- func NewArray(a []float64, shape ...int) *NArray
- func Norm(r *rand.Rand, mean, sd float64, shape ...int) *NArray
- func Pow(out, a, b *NArray) *NArray
- func Rand(r *rand.Rand, shape ...int) *NArray
- func Rcp(out, in *NArray) *NArray
- func Read(r io.Reader) (*NArray, error)
- func ReadFile(fn string) (*NArray, error)
- func Remainder(out, a, b *NArray) *NArray
- func Scale(out *NArray, in *NArray, c float64) *NArray
- func Sin(out, in *NArray) *NArray
- func Sinh(out, in *NArray) *NArray
- func Sqrt(out, in *NArray) *NArray
- func Sub(out *NArray, in ...*NArray) *NArray
- func Tan(out, in *NArray) *NArray
- func Tanh(out, in *NArray) *NArray
- func Trunc(out, in *NArray) *NArray
- func Y0(out, in *NArray) *NArray
- func Y1(out, in *NArray) *NArray
- func (na *NArray) At(indices ...int) float64
- func (na *NArray) Copy() *NArray
- func (na *NArray) Decode(inf, nan []int)
- func (na *NArray) Encode() (inf, nan []int)
- func (na *NArray) Inc(v float64, indices ...int)
- func (na *NArray) Index(indices ...int) int
- func (na *NArray) MarshalJSON() ([]byte, error)
- func (na *NArray) Matrix(query ...int) *Matrix
- func (na *NArray) Max() float64
- func (na *NArray) MaxElem(v float64, indices ...int)
- func (na *NArray) MaxIdx() (float64, []int)
- func (na *NArray) Min() float64
- func (na *NArray) MinElem(v float64, indices ...int)
- func (na *NArray) MinIdx() (float64, []int)
- func (na *NArray) Prod() float64
- func (na *NArray) Reshape(dim ...int) *NArray
- func (na *NArray) ReverseIndex(idx int) []int
- func (na *NArray) Set(v float64, indices ...int)
- func (na *NArray) SetValue(v float64) *NArray
- func (na *NArray) Sprint(f func(na *NArray, index int) bool) string
- func (na *NArray) String() string
- func (na *NArray) SubArray(query ...int) *NArray
- func (na *NArray) Sum() float64
- func (na *NArray) ToJSON() (string, error)
- func (na *NArray) UnmarshalJSON(b []byte) error
- func (na *NArray) Vector(query ...int) *Vector
- func (na *NArray) Write(w io.Writer) error
- func (na *NArray) WriteFile(fn string) error
- type Vector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dot ¶
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 ¶
EqualShape returns true if all the arrays have equal length, and false otherwise. Returns true if there is only one input array.
Types ¶
type Matrix ¶
type Matrix NArray
Matrix is as an NArray of rank 2 that satisfies the gonum Matrix interfaces.
func (*Matrix) At ¶
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 ¶
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) Row ¶
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 ¶
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 ¶
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.
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 ¶
Abs returns square root values of narrays elementwise. out = math.Abs(in) If out is nil a new array is created.
func Acos ¶
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 ¶
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 ¶
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 ¶
AddConst adds const to an narray elementwise. out = in + c If out is nil a new array is created.
func AddScaled ¶
AddScaled adds a scaled narray elementwise. y = y + a * x If y is nil a new array is created.
func Apply ¶
Apply function of type ApplyFunc to a multidimensional array. If out is nil, a new object is allocated.
func Asin ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 NewArray ¶
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 ¶
Norm creates a new n-dimensional array whose elements are drawn from a Normal probability density function.
func Pow ¶
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 ¶
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 ¶
Rcp returns reciprocal values of narrays elementwise. out = 1.0 / in If out is nil a new array is created.
func Remainder ¶
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 ¶
Scale multiplies an narray by a factor elementwise. out = c * in If out is nil a new array is created.
func Sin ¶
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 ¶
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 ¶
Sqrt returns square root values of narrays elementwise. out = math.Sqrt(in) If out is nil a new array is created.
func Sub ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) Encode ¶
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) Index ¶
Index transforms a set of subscripts to a an index in the underlying one-dimensional slice.
func (*NArray) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface. The custom marshaller is needed to encode Inf/NaN values.
func (*NArray) 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) MaxElem ¶
MaxElem compares value to element and replaces element if value is greater than element.
func (*NArray) MinElem ¶
MinElem compares value to element and replaces element if value is less than element.
func (*NArray) ReverseIndex ¶
ReverseIndex converts a linear index to narray indices.
func (*NArray) Sprint ¶
Sprint prints narray elements when f returns true. index is the linear index of an narray.
func (*NArray) SubArray ¶
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) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unarshaller interface. The custom unmarshaller is needed to decode Inf/NaN values.
func (*NArray) 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}