cmat

package module
v0.0.0-...-0fe7e44 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2014 License: LGPL-3.0 Imports: 9 Imported by: 4

README

CMAT - Column major MATrix

Another implementation of a column major matrix package. Similiar to the MATRIX package but with more restricted interface. Linear algebra operations available in GOMAS or NETLIB package.

FloatMatrix

Creating instances

NewMatrix(r, c)                Create a new FloatMatrix of size r rows, c columns
NewCopy(A)                     Create a new FloatMatrix as copy of A
NewJoin(join, mlist...)        Create a new compound FloatMatrix from argument matrices
MakeMatrix(r, c, buf)          Create a new FloatMatrix, use buf as element store

Basic attributes

A.Size() (int,int)             Get size of A as (rows, cols)
A.Len() int                    Get number of element in A
A.Stride() int                 Get row stride of A
A.Data() []float64             Get raw data elements

Getting/setting elements

A.Get(i, j) float64            Get value at [i,j]
A.Set(i, j, v)                 Set element at [i,j] to value v
A.GetAt(i) float64             Get i'th element 
A.SetAt(i, v)                  Set i'th element to value v

Strings

A.ToString(format) string      Convert to string, format is element format
A.String() string              Convert to string

Testing

A.AllClose(B, tols...) bool    Test if A is close to B, within tolerance

Copying

A.Copy(B)                      Copy B to A.
A.Transpose(B)                 Copy B.T to A

Matrix views

A.SubMatrix(B, r, c, nr, nc)   Make A submatrix of B starting at [r,c]
A.Column(B, col)               Make A column vector of B
A.Row(B, row)                  Make A row vector of B
A.Diag(B)                      Make A diagonal row vector of B

Transforming and setting

A.SetFrom(src, bits)           Get values for A from source
A.Map(mapping)                 Apply mapping to all elements of A

Data sources

interface FloatSource         
   Get(i, j) float64

NewFloatConstSource(val)           Create source that provides const value
NewFloatNormSource(stddev,mean)    Create source of normally distributed floats
NewFloatUniformSource(scale,shift) Create source of uniformly distributed floats

Mapping types

interface FloatMapping
   Eval(i, j, v) float64

Following types implement FloatMapping interface for Map() method.


type FloatEvaluator {       Generic from value v at [i,j] to new value
     Callable func(i, j, v) float64  
}

type FloatFunction {        Mapping to new value Callable(v)
     Callable func(v) float64  
}

type Float2Function {       Mapping to new value Callable(v, Const)
     Callable func(v, c) float64  
 Const float64
}

See file mapping.go for some examples.

Documentation

Index

Constants

View Source
const (
	STACK = iota
	AUGMENT
)
View Source
const (
	LOWER  = 0x1
	UPPER  = 0x2
	SYMM   = 0x4
	HERM   = 0x8
	UNIT   = 0x10
	NONE   = 0
	STRICT = UNIT
)

align values with bit values in gomas-package

View Source
const ABSTOL = 1e-8

Absolute tolerance. Values v1, v2 are equal within tolerance if ABS(v1-v2) < ABSTOL + RELTOL*ABS(v2)

View Source
const RELTOL = 1.0000000000000001e-05

Relative tolerance

Variables

This section is empty.

Functions

This section is empty.

Types

type FlagBits

type FlagBits int

type Float2Function

type Float2Function struct {
	// float valued callable function
	Callable func(float64, float64) float64
	// value of second argument for callable
	Constant float64
}

Two parameter mapping from float value to new ie. newval = fn(oldval, const)

func (*Float2Function) Eval

func (t *Float2Function) Eval(i, j int, val float64) float64

Evaluate two parameter callable as Callable(val, Constant)

type FloatConstSource

type FloatConstSource struct {
	Const float64
}

Source that produces const values.

func NewFloatConstSource

func NewFloatConstSource(val float64) *FloatConstSource

func (*FloatConstSource) Get

func (s *FloatConstSource) Get(i, j int) float64

Get

type FloatDiagonalSource

type FloatDiagonalSource struct {
	Const float64
}

Source that provides constant for diagonal, zero otherwise

func NewFloatDiagonalSource

func NewFloatDiagonalSource(val float64) *FloatDiagonalSource

func (*FloatDiagonalSource) Get

func (s *FloatDiagonalSource) Get(i, j int) float64

type FloatEvaluator

type FloatEvaluator struct {
	Callable func(int, int, float64) float64
}

Simple wrapper for element location dependent mapping.

func (*FloatEvaluator) Eval

func (t *FloatEvaluator) Eval(i, j int, val float64) float64

type FloatFunction

type FloatFunction struct {
	// float valued callable
	Callable func(float64) float64
}

Single parameter mapping from float value to another float value.

func (*FloatFunction) Eval

func (t *FloatFunction) Eval(i, j int, val float64) float64

Evaluate callable with argument.

type FloatMapping

type FloatMapping interface {
	Eval(i, j int, val float64) float64
}

Interface for mapping element values to new values.

type FloatMatrix

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

Column majoe double precision matrix.

func MakeMatrix

func MakeMatrix(rows, cols int, ebuf []float64) *FloatMatrix

Make a new matrix and use ebuf as element storage. cap(ebuf) must not be less than rows*cols.

func NewCopy

func NewCopy(A *FloatMatrix) *FloatMatrix

Make a new copy of matrix

func NewJoin

func NewJoin(how JoinType, mlist ...*FloatMatrix) *FloatMatrix

Create a new matrix by joining argument matrices. If join type is AUGMENT then matrices are joined on increasing column numbers ie. horizontally. Result matrix size is rowmax(mlist) by sum(m in mlist; cols(m)) If join type is STACK then matrices are joined on increasing row numbers ie. vertically and result matrix size is sum(m in mlist: rows(m) by colmax(mlist).

func NewMatrix

func NewMatrix(r, s int) *FloatMatrix

Make new matrix of size r rows, c cols.

func TriL

func TriL(A *FloatMatrix, bits int) *FloatMatrix

Make matrix LOWER triangular matrix ie. set strictly upper part to zero.

func TriU

func TriU(A *FloatMatrix, bits int) *FloatMatrix

Make matrix UPPER triangular matrix ie. set strictly lower part to zero.

func (*FloatMatrix) Add

func (A *FloatMatrix) Add(val float64)

Element-wise adding

func (*FloatMatrix) AllClose

func (A *FloatMatrix) AllClose(B *FloatMatrix, tols ...float64) bool

Test if matrix A is equal to B within given tolenrances. Tolerances are given as tuple (abstol, reltol). If no tolerances are given default constants ABSTOL and RELTOL are used.

func (*FloatMatrix) Column

func (C *FloatMatrix) Column(A *FloatMatrix, col int, sizes ...int) *FloatMatrix

Make C column of A. C = A[:,col]. Parameter sizes is singleton (row) and column vector starts at `row` and extends to the last element of the column. Alternatively sizes can be tuple of (row, numelems) and column vector starts at `row` and extends `numelems` elements. Function returns C.

func (*FloatMatrix) Copy

func (A *FloatMatrix) Copy(B *FloatMatrix) *FloatMatrix

Make A copy of B.

func (*FloatMatrix) Data

func (A *FloatMatrix) Data() []float64

Return raw element array.

func (*FloatMatrix) Diag

func (D *FloatMatrix) Diag(A *FloatMatrix, n ...int) *FloatMatrix

Return matrix diagonal as row vector. If optional parameter n < 0 returns n'th sub-diagonal. If n > 0 returns n'th super-diagonal and if n == 0 returns main diagonal

func (*FloatMatrix) Get

func (A *FloatMatrix) Get(i, j int) float64

Get element at [i, j]. Returns NaN if indexes are invalid. Negative indexes counted from end.

func (*FloatMatrix) GetAt

func (A *FloatMatrix) GetAt(i int) float64

Get element at index i. Returns NaN if index is invalid.

func (*FloatMatrix) GetAtUnsafe

func (A *FloatMatrix) GetAtUnsafe(i int) float64

Get element at index i. Unsafe vesrsion

func (*FloatMatrix) GetUnsafe

func (A *FloatMatrix) GetUnsafe(i, j int) float64

Get element at [i, j]. Unsafe version without checks and negative indexes

func (*FloatMatrix) GobDecode

func (A *FloatMatrix) GobDecode(buf []byte) (err error)

Decode a matrix.

func (*FloatMatrix) GobEncode

func (A *FloatMatrix) GobEncode() ([]byte, error)

GobEncode matrix. If A is a submatrix elements outside submatrix are not included.

func (*FloatMatrix) IsVector

func (A *FloatMatrix) IsVector() bool

func (*FloatMatrix) Len

func (A *FloatMatrix) Len() int

Get number of elements in matrix.

func (*FloatMatrix) Log

func (A *FloatMatrix) Log()

Element-wise logarithm.

func (*FloatMatrix) Map

func (A *FloatMatrix) Map(t FloatMapping, bits ...int)

Change matrix elements with a Mapping. If flag bit UPPER is set then strictly lower part of the matrix is not touched. If flag bit LOWER is set then strictly upper part of the matrix is not touched. If bit SYMM is set then transformer is evaluated only for upper part indexes and strictly lower part is set symmetrically.

func (*FloatMatrix) MarshalJSON

func (A *FloatMatrix) MarshalJSON() ([]byte, error)

func (*FloatMatrix) Row

func (R *FloatMatrix) Row(A *FloatMatrix, row int, sizes ...int) *FloatMatrix

Make R a row vector of A i.e. R = A[row,:]

func (*FloatMatrix) Scale

func (A *FloatMatrix) Scale(val float64)

Element-wise scaling.

func (*FloatMatrix) Set

func (A *FloatMatrix) Set(i, j int, v float64)

Set element at [i, j]

func (*FloatMatrix) SetAt

func (A *FloatMatrix) SetAt(i int, v float64)

Set element at index i.

func (*FloatMatrix) SetAtUnsafe

func (A *FloatMatrix) SetAtUnsafe(i int, v float64)

Set element at index i.

func (*FloatMatrix) SetBuf

func (A *FloatMatrix) SetBuf(rows, cols, stride int, ebuf []float64) *FloatMatrix

Set matrix size and storage. Minimum size for ebuf is stride*cols. If stride zero or negative then rows is used as row stride. Returns nil if buffer capasity too small. Otherwise returns A.

func (*FloatMatrix) SetFrom

func (m *FloatMatrix) SetFrom(source FloatSource, bits ...int)

Set matrix elements from source. Optional bits define which part of the matrix is accessed. Default is to set all entries.

Bits

UPPER        set upper triangular/trapezoidal part
UPPER|UNIT   set strictly upper triangular/trapezoidal part
LOWER        set lower triangular/trapezoidal part
LOWER|UNIT   set strictly lower triangular/trapezoidal part
SYMM         set lower part symmetrically to upper part

To set strictly lower part of a matrix: A.SetFrom(src, LOWER|UNIT)

func (*FloatMatrix) SetUnsafe

func (A *FloatMatrix) SetUnsafe(i, j int, v float64)

Set element at [i, j]

func (*FloatMatrix) Size

func (A *FloatMatrix) Size() (int, int)

Get size of the matrix as tuple (rows, cols).

func (*FloatMatrix) Stride

func (A *FloatMatrix) Stride() int

Get row stride of the matrix.

func (*FloatMatrix) String

func (A *FloatMatrix) String() string

func (*FloatMatrix) SubMatrix

func (A *FloatMatrix) SubMatrix(B *FloatMatrix, row, col int, sizes ...int) *FloatMatrix

Make A submatrix of B. Returns A.

func (*FloatMatrix) SubVector

func (X *FloatMatrix) SubVector(Y *FloatMatrix, offset, nlen int) *FloatMatrix

Make X subvector of Y, X = Y[offset:offset+nlen]

func (*FloatMatrix) ToString

func (A *FloatMatrix) ToString(format string) string

func (*FloatMatrix) ToStringPartial

func (A *FloatMatrix) ToStringPartial(format string, rowpart, colpart int) string

Convert matrix to string with spesific element format.

func (*FloatMatrix) Transpose

func (A *FloatMatrix) Transpose(B *FloatMatrix) *FloatMatrix

Transpose matrix, A = B.T

func (*FloatMatrix) UnmarshalJSON

func (A *FloatMatrix) UnmarshalJSON(buf []byte) (err error)

type FloatNormSource

type FloatNormSource struct {
	// mean of the distribution
	Mean float64
	// required standard deviation
	StdDev float64
	Rnd    *rand.Rand
}

Float value source for normally distributed values with mean `Mean` and standard deviation `StdDev`.

func NewFloatNormSource

func NewFloatNormSource(params ...float64) *FloatNormSource

Create a new source of Normally distributed float values. Optional parameters is either StdDev or tuple of (StdDev, Mean). Initial random number is multiplied with StdDev and Mean is added to the result. Default values for (StdDev,Mean) is (1.0, 0.0)

func (*FloatNormSource) Get

func (s *FloatNormSource) Get(i, j int) float64

Fetch a new value from distribution.

type FloatSink

type FloatSink interface {
	Put(i, j int, val float64)
}

Interface for pushing out elements.

type FloatSource

type FloatSource interface {
	Get(i, j int) float64
}

Interface for providing float values.

type FloatTableSource

type FloatTableSource struct {
	Data    [][]float64
	Default float64
}

Source for retrieving matrix elements from a table. Default is returned if element index outside table.

func NewFloatTableSource

func NewFloatTableSource(data [][]float64, defval float64) *FloatTableSource

Create a new table source.

func (*FloatTableSource) Get

func (ts *FloatTableSource) Get(i, j int) float64

func (*FloatTableSource) Size

func (ts *FloatTableSource) Size() (int, int)

Return source table dimensions.

type FloatUniformSource

type FloatUniformSource struct {
	// shifting factor
	Shift float64
	// scaling factor
	Scale float64
	Rnd   *rand.Rand
}

Float value source for uniformly distributed in range [0.0,1.0) shifted and scaled and returning value Scale*(Uniform()-Shift)

func NewFloatUniformSource

func NewFloatUniformSource(params ...float64) *FloatUniformSource

Create a new source of uniformly distributed float values. Optional parameters is either shift or tuple of (shift, scale). Value of shift is added to initial value which is then multiplied with the scaling factor. Default values for shift and scale are (0.0, 1.0) that creates source for random numbers from [0.0, 1.0)

func (*FloatUniformSource) Get

func (s *FloatUniformSource) Get(i, j int) float64

Fetch a new value from distribution.

type JoinType

type JoinType int

Jump to

Keyboard shortcuts

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