matrix

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2015 License: MIT Imports: 1 Imported by: 5

README

Matrix

License GoDoc Version Wercker Coverage

An experimental library for matrix manipulation implemented in Golang.

Motivations

  1. Portability - Implement in pure Golang to achieve cgo-free.
  2. Efficiency - Pursue performance as possible without highly optimized back-ends like blas.
  3. Simplicity - Provide clean API.

Installation

For installation, execute the following command:

$ go get github.com/mitsuse/matrix-go

Features

Matrix Types

Currently, the following types are implemented:

  • mutable dense matrix
Creation

Use dense.New to create a new dense matrix with given elements.

// Create a 2 x 3 matrix.
m := dense.New(2, 3)(
    0, 1, 2,
    3, 4, 5,
)

To create zero matrix, call dense.Zeros instead.

// Create a 2 x 3 zero matrix.
m := dense.Zeros(2, 3)
Operations
Addition & Subtraction

Add a matrix to other with (Matrix).Add:

m := dense.New(2, 3)(
    0, 1, 2,
    3, 4, 5,
)

n := dense.New(2, 3)(
    5, 4, 3,
    2, 1, 0,
)

r := dense.New(2, 3)(
    5, 5, 5,
    5, 5, 5,
)

// true
m.Add(n).Equal(r)

Similarly, (Matrix).Subtract is used for subtraction on two matrix.

When the receiver is mutable, (Matrix).Add and (Matrix).Subtract return the receiver itself, the elements of which is rewritten.

Matrix Multiplication

The product of two matrices can be calculated by (Matrix).Multiply.

m := dense.New(3, 2)(
    0, 1,
    2, 3,
    4, 5,
)

n := dense.New(2, 1)(
    0,
    -1,
)

r := dense.New(3, 1)(
    -1,
    -3,
    -5,
)

m.Multiply(n).Equal(r)

Matrix multiplication always create a new matrix. The type of the result matrix is same as the type of the receiver.

Scalar Multiplication

(Matrix).Scalar is available for Scalar multiplication (scalar-left multiplication).

m := dense.New(2, 2)(
    0, 1,
    2, 3,
)

r := dense.New(2, 2)(
    0, -1,
    -2, -3,
)

// true
m.Scalar(-1).Equal(r)

For scalar-right multiplication, use (Scalar).Multiply.

m := dense.New(2, 2)(
    0, 1,
    2, 3,
)

r := dense.New(2, 2)(
    0, -1,
    -2, -3,
)

// true
Scalar(-1).Multiply(m).Equal(r)

When the matrix used for scalar multiplication is mutable, (Matrix).Scalar and (Scalar).Multiply rewrite elements of the matrix.

Cursor

Matrix has several methods to iterate elements. They return a value typed as Cursor which is a reference to the element to visit.

m := dense.New(2, 3)(
    0, 1, 2,
    3, 4, 5,
)

// Create a cursor to iterate all elements of matrix m.
c := m.All()

// Check whether the element to visit exists or not.
for c.HasNext() {
    element, row, column := c.Get()

    fmt.Printf(
        "element = %d, row = %d, column = %d\n",
        element,
        row,
        column,
    )
}

Currently, three methods are implemented which return a cursor:

  • (Matrix).All
  • (Matrix).NonZeros
  • (Matrix).Diagonal

For details, please read the documentation of types.Matrix.

More Details

Please read the documentation.

License

Please read LICENSE.txt.

Documentation

Overview

Package "matrix" is an experimental library for matrix manipulation implemented in Golang.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDiagonal

func IsDiagonal(m Matrix) bool

Check whether "m" is diagonal matrix or not.

func IsIdentity

func IsIdentity(m Matrix) bool

Check whether "m" is identity matrix or not.

func IsScalar

func IsScalar(m Matrix) bool

Check whether "m" is scalar matrix or not.

func IsSquare

func IsSquare(m Matrix) bool

Check whether "m" is square matrix or not.

func IsZeros

func IsZeros(m Matrix) bool

Check whether "m" is zero matrix or not.

Types

type Cursor

type Cursor interface {
	types.Cursor
}

"Cursor" is the interface for iterator for elements of matrix. Some implementations of "Cursor" iterate all elements, and others iterates elements satisfying conditions. For more details, refer "types.Cursor".

type Matrix

type Matrix interface {
	types.Matrix
}

"Matrix" is the interface for implementations of various matrix types. For more details, refer "types.Matrix".

type Scalar

type Scalar float64

"Scalar" is used to calculate the product with scalar-left multiplication. This type is underlying float64.

func (Scalar) Multiply

func (s Scalar) Multiply(m Matrix) Matrix

Directories

Path Synopsis
Package "dense" provides an implementation of mutable dense matrix.
Package "dense" provides an implementation of mutable dense matrix.
internal
rewriters
Package "rewriters" provides functions to rewrite the indexes or shape of matrix.
Package "rewriters" provides functions to rewrite the indexes or shape of matrix.
types
Package "types" provides types which represent matrix and iterator of elements.
Package "types" provides types which represent matrix and iterator of elements.

Jump to

Keyboard shortcuts

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