matlib

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2019 License: MIT Imports: 8 Imported by: 0

README

matlib

Build Status

matlib is a small matrix package with a focus on data exchange and simple math functions. This is a fork of the GreenDelta/matlib with the dependencies to the native solver library removed. It would be probably good to merge this back later and create a package matsolve or similar which contains the native parts.

File format

This package provides methods for loading (goblapack.Load) and saving (goblapack.Save) matrices from files in a simple binary format:

header 8 bytes
4 bytes: uint32, number of rows, little endian order
4 bytes: uint32, number of columns, little endian order

content, rows * columns * 8 bytes:
matrix data, float64, little endian and column major order

Here is a small Python script for writing a NumPy matrix in this format:

m = numpy.load('path/to/file.npy')
rows, cols = m.shape
with open('path/to/file.bin', 'wb') as f:
    f.write(struct.pack("<i", rows))
    f.write(struct.pack("<i", cols))
    for col in range(0, cols):
        for row in range(0, rows):
            val = m[row, col]
            f.write(struct.pack("<d", val))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadColumn

func ReadColumn(file string, column int) ([]float64, error)

ReadColumn reads the given (zero-based) column from the matrix in the given file. It first reads the size of the matrix, seeks than to the position of the column in the file, and finally reads it. As matlib matrices are stored in column-major order this should be efficient.

func ReadDiag

func ReadDiag(file string) ([]float64, error)

ReadDiag reads the diagonale of the matrix stored in the given file.

func ReadRow

func ReadRow(file string, row int) ([]float64, error)

ReadRow reads the given row from the matrix in the given file. As matlib matrices are stored in column-major order this will make n file seeks for a matrix with n columns.

func WriteMatrix

func WriteMatrix(m *Matrix, file string) error

WriteMatrix writes the matrix to the given file.

Types

type Matrix

type Matrix struct {
	Rows int
	Cols int
	Data []float64
}

Matrix is a dense matrix structure that holds the data in column-major order in a linear array. Because of this lay

func Eye

func Eye(size int) *Matrix

Eye returns the identity matrix of the given size.

func MakeMatrix

func MakeMatrix(data [][]float64) *Matrix

MakeMatrix is a convenience function for creating a matrix from a 2 dimensional float slice. It is mainly used for testing purposes.

func MemMap

func MemMap(file string) (*Matrix, error)

MemMap loads the matrix from the given file using memory mapping.

func ReadMatrix

func ReadMatrix(file string) (*Matrix, error)

ReadMatrix reads a full matrix from the given file.

func Zeros

func Zeros(rows, cols int) *Matrix

Zeros creates a new matrix with all values as 0 of the give size.

func (*Matrix) Add

func (m *Matrix) Add(row, col int, value float64)

Add adds the given value to the matrix cell with the given coordinates.

func (*Matrix) Col

func (m *Matrix) Col(idx int) []float64

Col returns the values from the column with the given index in a new slice.

func (*Matrix) Copy

func (m *Matrix) Copy() *Matrix

Copy creates a copy of the matrix.

func (*Matrix) Get

func (m *Matrix) Get(row, col int) float64

Get returns the value at the given row and column.

func (*Matrix) GetPtr

func (m *Matrix) GetPtr(row, col int) *float64

GetPtr returns a pointer to the matrix cell with the given row and column.

func (*Matrix) Row

func (m *Matrix) Row(idx int) []float64

Row returns the values from the row with the given index in a new slice.

func (*Matrix) ScaleColumns

func (m *Matrix) ScaleColumns(s []float64) *Matrix

ScaleColumns scales each column i of the matrix with the factor s[i] of the given vector.

func (*Matrix) ScaledColumnSums

func (m *Matrix) ScaledColumnSums(s []float64) []float64

ScaledColumnSums calculates the sum of each column i which are saled by the factor s[i] of the given vector respectively. Thus the returned result has a length wich is equal to the number of rows of the matrix.

func (*Matrix) Set

func (m *Matrix) Set(row, col int, value float64)

Set sets the matrix cell at the given row and column to the given value.

func (*Matrix) Shape

func (m *Matrix) Shape() (int, int)

Shape returns a tuple with the number of rows and columns in the matrix, e.g.:

rows, cols := M.Shape()

func (*Matrix) Slice2d

func (m *Matrix) Slice2d() [][]float64

Slice2d converts the matrix data into a 2-dimensional slice.

func (*Matrix) Subtract

func (m *Matrix) Subtract(b *Matrix) (*Matrix, error)

Subtract calculates A - B = C where A is the matrix on which this method is called, B the method parameter, and C the return value. The matrix B can be smaller as A; C will have the same size as A.

Jump to

Keyboard shortcuts

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