matrix

package module
v0.0.0-...-63837e2 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: MIT Imports: 7 Imported by: 3

README

matrix

GoDoc Build Status License

Matrix is a simple implementation of a general purpose matrix with concurrency in go.

Usage

If you use version 1.11 or older just add the following line inside your module files.

import "github.com/jmpargana/matrix"

If you use an older version run this on your terminal:

go get github.com/jmpargana/matrix

License

License

Documentation

Overview

Package matrix is a go package that implements a simple matrix type. It has both methods for the type as well as functions for the most important operations: Mult, Add, Sub, MultScalar, AddScalar and HadamardProd. It uses concurrency only in the matrix multiplication, since its the only place where the overhead might actually be worth it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(rhs, lhs Matrix) bool

Equal compares to matrices for equality.

Types

type Matrix

type Matrix struct {
	NumRows int
	NumCols int
	// contains filtered or unexported fields
}

Matrix is a matrix of floats. The size of the matrix is publicly available, while its data only via getters.

func Add

func Add(lhs, rhs Matrix) (Matrix, error)

func HadamardProd

func HadamardProd(lhs, rhs Matrix) (Matrix, error)

HadamardProd performs the hadamard product of matrices. It is an elementwise multiplication of each of the elements in both matrices, therefore they must have the exact same dimensions.

func Mult

func Mult(lhs, rhs Matrix) (Matrix, error)

func New

func New(rows, cols int) Matrix

New creates a new empty matrix of a given size.

func NewFrom

func NewFrom(data [][]float64) Matrix

NewFrom creates a matrix instance from a slice of slices

func NewFromVec

func NewFromVec(rows, cols int, data []float64) Matrix

NewFromVector creates a matrix from long vector. It just makes sure it can have a rectangular shape

func NewRandom

func NewRandom(rows, cols int) Matrix

NewRandom creates a matrix of a given size and fills the vectors with random floating point number ranging from 0..1

func NewSquare

func NewSquare(size int) Matrix

NewSquare creates an empty square matrix of a given size.

func Sub

func Sub(lhs, rhs Matrix) (Matrix, error)

func Trans

func Trans(m Matrix) (Matrix, error)

Trans returns a new matrix which represents the transposed version of the first.

func (*Matrix) Add

func (m *Matrix) Add(other Matrix) error

Add adds a value to first matrix and saves result in its data. Both matrices should have the same dimensions.

func (*Matrix) AddScalar

func (m *Matrix) AddScalar(val float64)

AddScalar adds a value to all elements in matrix.

func (*Matrix) Col

func (m *Matrix) Col(col int) ([]float64, error)

Col returns a slice of the column on the nth index of the matrix.

func (*Matrix) Equal

func (m *Matrix) Equal(other Matrix) bool

Equal compares the instance matrix with another.

func (*Matrix) Get

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

func (*Matrix) IsSquare

func (m *Matrix) IsSquare() bool

IsSquare compares the number of columns with the number of rows.

func (*Matrix) MarshalBinary

func (m *Matrix) MarshalBinary() ([]byte, error)

MarshalBinary is the method needed to implement the BinaryMarshaler interface. With it one can call gob.Encode on the Matrix struct.

func (*Matrix) Mult

func (m *Matrix) Mult(other Matrix) error

Mult multiplies two matrices and saves result in first one. The two matrices must look the same, otherwise the shape of the first needs to change.

func (*Matrix) MultScalar

func (m *Matrix) MultScalar(val float64)

MultScalar multiplies all elements of matrix by value.

func (*Matrix) Row

func (m *Matrix) Row(row int) ([]float64, error)

Row returns a slice of the row on the nth index of the matrix.

func (*Matrix) Set

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

func (Matrix) String

func (m Matrix) String() (s string)

func (*Matrix) Sub

func (m *Matrix) Sub(other Matrix) error

Add subtracts a value to first matrix and saves result in its data. Both matrices should have the same dimensions.

func (*Matrix) Trans

func (m *Matrix) Trans() error

Trans transposes the matrix in place.

func (*Matrix) UnmarshalBinary

func (m *Matrix) UnmarshalBinary(data []byte) error

MarshalBinary is the method needed to implement the BinaryUnmarshaler interface. With it one can call gob.Decode on the Matrix struct.

Jump to

Keyboard shortcuts

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