matrix

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

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

Go to latest
Published: Jun 30, 2018 License: MIT Imports: 12 Imported by: 0

README

Matrix

GoDoc Build Status Coverage Status

This package provides a matrix library for Go.

Currenly, float64 and float32 are the only supported types, but we will add support for int64, and perhaps interface{} down the line

Documentation

Overview

Package matrix implements a "mat" object, which behaves like a 2D array or list in other programming languages. Under the hood, the mat object is a flat slice, which provides for optimal performance in Go, while the methods and constructors provide for a higher level of performance and abstraction when compared to the "2D" slices of go (slices of slices). Due to it's internal representation, row or column vectors can also be easily created by the Matf32 object, without a performance hit, by setting the number of rows or columns to one.

All errors encountered in this package, such as attempting to access an element out of bounds are treated as critical error, and thus, the code immediately exits with signal 1. In such cases, the function/method in which the error was encountered is printed to the screen, in addition to the full stack trace, in order to help fix the issue rapidly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matf32

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

Matf32 is the main struct of this library. Matf32 is a essentially a 1D slice (a []float32) that contains two integers, representing rows and columns, which allow it to behave as if it was a 2D slice. This allows for higher performance and flexibility for the users of this library, at the expense of some bookkeeping that is done here.

The fields of this struct are not directly accessible, and they may only change by the use of the various methods in this library.

func Eyef32

func Eyef32(x int) *Matf32

Eyef32 returns the identity matrix

func Matf32FromData

func Matf32FromData(oneOrTwoDSlice interface{}) *Matf32

Matf32FromData creates a mat object from a []float32 or a [][]float32 slice. This function is designed to do the "right thing" based on the type of the slice passed to it. The "right thing" based on each possible case is as follows:

Assume that s is a [][]float32, and v is a []float32 for the examples below.

x := matrix.Matf32FromData(v)

In this case, x.Dims() is (1, len(v)), and the values in x are the same as the values in v. x is essentially a row vector. To convert to a column vector or a matrix of any other shape, use the Reshape() method.

This function can also be invoked with data that is stored in a 2D slice ([][]float32).

x := matrix.Matf32FromData(s)

In this case, x.Dims() is (len(s), len(s[0])), and the values in x are the same as the values in s. It is assumed that s is not jagged.

func Newf32

func Newf32(dims ...int) *Matf32

Newf32 is the primary constructor for the "Matf32" object. New is a variadic function, expecting 0 to 2 integers, with differing behavior as follows:

m := matrix.Newf32()

m is now an empty &Matf32{}, where the number of rows, columns and the length and capacity of the underlying slice are all zero. This is mostly for internal use.

m := matrix.Newf32(x)

m is a x by x (square) matrix, with the underlying slice of length x, and capacity 2x.

m := matrix.Newf32(x, y)

m is an x by y matrix, with the underlying slice of length xy, and capacity of 2xy.

func RandMatf32

func RandMatf32(r, c int) *Matf32

RandMatf32 returns a Matf32 whose elements have random values. For example:

m := matrix.RandMatf32(2, 3)

m is a 2X3 Matf32 whose elements have values randomly selected from the range (0, 1], (includes 0, but excludes 1).

func (*Matf32) Add

func (m *Matf32) Add(float64OrMatf32 interface{}) *Matf32

Add carries the addition operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float32, then it is added to each element:

m := matrix.Newf32(2, 3).SetAll(5.0)
m.Add(2.0)

This will result in all values of m being 7.0. The passed Object can also be a Matf32, in which case each element of the element of the passed Matf32 is added to the corresponding element of the receiver. Note that the passed Matf32 must have the same shape as the receiver.

m := matrix.Newf32(2, 3).SetAll(10.0)
n := m.Copy()
m.Add(n)

This will result in each element of m being 20.0.

func (*Matf32) All

func (m *Matf32) All(f func(*float32) bool) bool

All checks if a supplied function is true for all elements of a mat object. For instance, consider

m.All(matrix.Positive)

will return true if and only if all elements in m are positive.

func (*Matf32) Any

func (m *Matf32) Any(f func(*float32) bool) bool

Any checks if a supplied function is true for one elements of a mat object. For instance,

m.Any(matrix.Positive)

would be true if at least one element of the mat object is positive.

func (*Matf32) Append

func (m *Matf32) Append(n *Matf32) *Matf32

Append merges a passed mat to the botton of the receiver. The passed mat must therefore have the same number of columns as the receiver. For example:

m := matrix.Newf32(1, 2).SetAll(2.0) // [[2.0, 2.0]]
n := matrix.Newf32(2, 2).SetAll(3.0) // [[3.0, 3.0], [3.0, 3.0]]
m.Append(n)
fmt.Println(m) // [[2.0, 2.0], [3.0, 3.0], [3.0, 3.0]]

Note that in the current implementation this is a somewhat expensive function.

func (*Matf32) AppendCol

func (m *Matf32) AppendCol(v []float32) *Matf32

AppendCol appends a column to the right side of a Matf32.

func (*Matf32) AppendRow

func (m *Matf32) AppendRow(v []float32) *Matf32

AppendRow appends a row to the bottom of a Matf32.

func (*Matf32) Avg

func (m *Matf32) Avg(args ...int) float32

Avg takes the average of the elements of a Matf32. It can be called in one of two ways:

m.Avg()

This will return the average of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Avg(0, 2) // Returns the average of the 3rd row
m.Avg(1, 0) // Returns the average of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf32) Col

func (m *Matf32) Col(x int) *Matf32

Col returns a new mat object whose values are equal to a column of the original mat object. The number of Rows of the returned mat object is equal to the number of rows of the original mat, and the number of columns is equal to 1.

This function supports negative indexing. For example,

v := m.Col(-1)

returns the last column of m.

func (*Matf32) Concat

func (m *Matf32) Concat(n *Matf32) *Matf32

Concat merges a passed mat to the right side of the receiver. The passed mat must therefore have the same number of rows as the receiver. For example:

m := matrix.Newf32(1, 2).SetAll(2.0) // [[2.0, 2.0]]
n := matrix.Newf32(1, 3).SetAll(3.0) // [[3.0, 3.0, 3.0]]
m.Concat(n)
fmt.Println(m) // [[2.0, 2.0, 3.0, 3.0, 3.0]]

Note that in the current implementation this is a somewhat expensive function.

func (*Matf32) Copy

func (m *Matf32) Copy() *Matf32

Copy returns a duplicate of a mat object. The returned copy is "deep", meaning that the object can be manipulated without effecting the original mat object.

func (*Matf32) Div

func (m *Matf32) Div(float64OrMatf32 interface{}) *Matf32

Div carries the division operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float32, then each element of the receiver is devided by it:

m := Newf32(2, 3).SetAll(5.0)
m.Div(2.0)

This will result in all values of m being 2.5. Note that the passed float32 cannot be 0.0.

The passed Object can also be a Matf32, in which case each element of the passed Matf32 is subtracted from the corresponding element of the receiver. Note that the passed Matf32 must have the same shape as the receiver, and it cannot contains any elements which are 0.0.

m := matrix.Newf32(2, 3).SetAll(10.0)
n := m.Copy()
m.Div(n)

This will result in each element of m being 1.0.

func (*Matf32) Dot

func (m *Matf32) Dot(n *Matf32) *Matf32

Dot is the matrix multiplication of two mat objects. Consider the following two mats:

m := matrix.Newf32(5, 6)
n := matrix.Newf32(6, 10)

then

o := m.Dot(n)

is a 5 by 10 mat whose element at row i and column j is given by:

Sum(m.Row(i).Mul(n.col(j))

func (*Matf32) Equals

func (m *Matf32) Equals(n *Matf32) bool

Equals checks to see if two mat objects are equal. That mean that the two mats have the same number of rows, same number of columns, and have the same float32 in each entry at a given index.

func (*Matf32) Get

func (m *Matf32) Get(r, c int) float32

Get returns a pointer to the float32 stored in the given row and column.

func (*Matf32) Map

func (m *Matf32) Map(f func(*float32)) *Matf32

Map applies a given function to each element of a mat object. The given function must take a pointer to a float32, and return nothing. For eaxmple, lets say that we wish to take the error function of each element of a Matf32. The following would do this:

m.Map(func(i *float32) {
	*i = math.Erf(*i)
})

func (*Matf32) Max

func (m *Matf32) Max(args ...int) (index int, maxVal float32)

Max returns the index and the value of the biggest float32 in a Matf32. This method can be called in one of two ways:

idx, val := m.Max()

will return the index, and value of the biggest float32 in m. We can also specify the exact row and column for which we want the minimum index and values:

idx, val := m.Max(0, 3) // Get the max index and value of the 4th row
idx, val := m.Max(1, 2) // Get the max index and value of the 3rd column

Note that negative index values are not supported at this time. Also note that in the case where multiple values are the maximum, the index of the first encountered value is returned.

TODO: support negative index, and fix errors.

func (*Matf32) Min

func (m *Matf32) Min(args ...int) (index int, minVal float32)

Min returns the index and the value of the smallest float32 in a Matf32. This method can be called in one of two ways:

idx, val := m.Min()

will return the index, and value of the smallest float32 in m. We can also specify the exact row and column for which we want the minimum index and values:

idx, val := m.Min(0, 3) // Get the min index and value of the 4th row
idx, val := m.Min(1, 2) // Get the min index and value of the 3rd column

Note that negative index values are not supported at this time. Also note that in the case where multiple values are the maximum, the index of the first encountered value is returned.

TODO: support negative index, and fix errors.

func (*Matf32) Mul

func (m *Matf32) Mul(float64OrMatf32 interface{}) *Matf32

Mul carries the multiplication operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float32, then each element is multiplied by it:

m := matrix.Newf32(2, 3).SetAll(5.0)
m.Mul(2.0)

This will result in all values of m being 10.0. The passed Object can also be a Matf32, in which case each element of the receiver are multiplied by the corresponding element of the passed Matf32. Note that the passed Matf32 must have the same shape as the receiver.

m := matrix.Newf32(2, 3).SetAll(10.0)
n := m.Copy()
m.Mul(n)

This will result in each element of m being 100.0.

Note: For the matrix cross product see the Dot() method.

func (*Matf32) Prd

func (m *Matf32) Prd(args ...int) float32

Prd takes the product of the elements of a Matf32. It can be called in one of two ways:

m.Prd()

This will return the product of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Prd(0, 2) // Returns the product of the 3rd row
m.Prd(1, 0) // Returns the product of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf32) Reshape

func (m *Matf32) Reshape(rows, cols int) *Matf32

Reshape changes the row and the columns of the mat object as long as the total number of values contained in the mat object remains constant. The order and the values of the mat does not change with this function.

func (*Matf32) Row

func (m *Matf32) Row(x int) *Matf32

Row returns a new mat object whose values are equal to a row of the original mat object. The number of Rows of the returned mat object is equal to 1, and the number of columns is equal to the number of columns of the original mat.

This function supports negative indexing. For example,

v := m.Row(-1)

returns the last row of m.

func (*Matf32) Set

func (m *Matf32) Set(r, c int, val float64) *Matf32

Set sets the value of a mat at a given row and column to a given value.

func (*Matf32) SetAll

func (m *Matf32) SetAll(val float64) *Matf32

SetAll sets all values of a mat to the passed float64 value.

func (*Matf32) SetCol

func (m *Matf32) SetCol(col int, floatOrSlice interface{}) *Matf32

SetCol Sets all elements in a given column to the passed value(s). Negative index values are allowed. For example:

m.SetCol(-1, 2.0)

sets all values of m's last column to 2.0. It is also possible to pass a slice of float32 to this function, all the elements of the chosen column will be set to the corresponding values in the slice. For example:

m := Newf32(2, 2).SetCol(0, []float32{1.0, 2.0})

sets to values in the first column of m to 1.0 and 2.0 respectively. Note that in this case, the length of the passed slice must match exactly the number of elements in m's column, i.e. the number of rows of m.

func (*Matf32) SetRow

func (m *Matf32) SetRow(row int, floatOrSlice interface{}) *Matf32

SetRow Sets all elements in a given column to the passed value(s). Negative index values are allowed. For example:

m.SetRow(-1, 2.0)

sets all values of m's last row to 2.0. It is also possible to pass a slice of float32 to this function, all the elements of the chosen row will be set to the corresponding values in the slice. For example:

m := Newf32(2, 2).SetRow(0, []float32{1.0, 2.0})

sets to values in the first row of m to 1.0 and 2.0 respectively. Note that in this case, the length of the passed slice must match exactly the number of elements in m's row, i.e. the number of cols of m.

func (*Matf32) Shape

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

Shape returns the number of rows and columns of a mat object.

func (*Matf32) Std

func (m *Matf32) Std(args ...int) float32

Std takes the standard deviation of the elements of a Matf32. It can be called in one of two ways:

m.Std()

This will return the std. div. of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Std(0, 2) // Returns the standard deviation of the 3rd row
m.Std(1, 0) // Returns the standard deviation of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf32) Sub

func (m *Matf32) Sub(float64OrMatf32 interface{}) *Matf32

Sub carries the subtraction operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float32, then it is subtracted from each element:

m := matrix.Newf32(2, 3).SetAll(5.0)
m.Sub(2.0)

This will result in all values of m being 3.0. The passed Object can also be a Matf32, in which case each element of the passed Matf32 is subtracted from the corresponding element of the receiver. Note that the passed Matf32 must have the same shape as the receiver.

m := matrix.Newf32(2, 3).SetAll(10.0)
n := m.Copy()
m.Sub(n)

This will result in each element of m being 0.0.

func (*Matf32) Sum

func (m *Matf32) Sum(args ...int) float32

Sum takes the sum of the elements of a Matf32. It can be called in one of two ways:

m.Sum()

This will return the sum of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Sum(0, 2) // Returns the sum of the 3rd row
m.Sum(1, 0) // Returns the sum of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf32) T

func (m *Matf32) T() *Matf32

T returns the transpose of the original matrix. The transpose of a mat object is defined in the usual manner, where every value at row x, and column y is placed at row y, and column x. The number of rows and column of the transposed mat are equal to the number of columns and rows of the original matrix, respectively.

func (*Matf32) ToSlice1D

func (m *Matf32) ToSlice1D() []float32

ToSlice1D returns the values contained in a mat object as a 1D slice of float32s.

func (*Matf32) ToSlice1Df64

func (m *Matf32) ToSlice1Df64() []float64

ToSlice1Df64 returns the values contained in a mat object as a 1D slice of float64s.

func (*Matf32) ToSlice2D

func (m *Matf32) ToSlice2D() [][]float32

ToSlice2D returns the values of a mat object as a 2D slice of float32s.

func (*Matf32) ToSlice2Df64

func (m *Matf32) ToSlice2Df64() [][]float64

ToSlice2Df64 returns the values of a mat object as a 2D slice of float64s.

type Matf64

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

Matf64 is the main struct of this library. Matf64 is a essentially a 1D slice (a []float64) that contains two integers, representing rows and columns, which allow it to behave as if it was a 2D slice. This allows for higher performance and flexibility for the users of this library, at the expense of some bookkeeping that is done here.

The fields of this struct are not directly accessible, and they may only change by the use of the various methods in this library.

func Eyef64

func Eyef64(x int) *Matf64

Eyef64 returns the identity matrix

func Matf64FromCSV

func Matf64FromCSV(filename string) *Matf64

Matf64FromCSV creates a mat object from a CSV (comma separated values) file. Here, we assume that the number of rows of the resultant mat object is equal to the number of lines, and the number of columns is equal to the number of entries in each line. As before, we make sure that each line contains the same number of elements.

The file to be read is assumed to be very large, and hence it is read one line at a time. This results in some major inefficiencies, and it is recommended that this function be used sparingly, and not as a major component of your library/executable.

Unlike other mat creation functions in this package, the capacity of the mat object created here is the same as its length since we assume the mat to be very large.

func Matf64FromData

func Matf64FromData(oneOrTwoDSlice interface{}, dims ...int) *Matf64

Matf64FromData creates a mat object from a []float64 or a [][]float64 slice. This function is designed to do the "right thing" based on the type of the slice passed to it. The "right thing" based on each possible case is as follows:

Assume that s is a [][]float64, and v is a []float64 for the examples below.

x := matrix.Matf64FromData(v)

In this case, x.Dims() is (1, len(v)), and the values in x are the same as the values in v. x is essentially a row vector.

Alternatively, this function can be invoked as:

x := matrix.Matf64FromData(v, a)

In this case, x.Dims() is (a, 1), and the values in x are the same as the values in v. x is essentially a column vector. Note that a must be equal to len(v).

Finally for the case where the data is a []float64, the function can be invoked as:

x := matrix.Matf64FromData(v, a, b)

In this case, x.Dims() is (a, b), and the values in x are the same as the values in v. Note that a*b must be equal to len(v). Also note that this is equivalent to:

x := matrix.Matf64FromData(v).reshape(a,b)

This function can also be invoked with data that is stored in a 2D slice ([][]float64). Just as the []float64 case, there are three possibilities:

x := matrix.Matf64FromData(s)

In this case, x.Dims() is (len(s), len(s[0])), and the values in x are the same as the values in s. It is assumed that s is not jagged.

Another form to call this function with a 2D slice of data is:

x := matrix.Matf64FromData(s, a)

In this case, x.Dims() is (a, a), and the values in x are the same as the values in s. Note that the total number of elements in s must be exactly a*a.

Finally, this function can be called as:

x := matrix.Matf64FromData(s, a, b)

In this case, x.Dims() is (a, b), and the values in x are the same as the values in s. Note that the total number of elements in s must be exactly a*b. Also note that this is equivalent to:

x := matrix.Matf64FromData(s).Reshape(a, b)

Choose the format that suits your needs, as there is no performance difference between the two forms.

func Newf64

func Newf64(dims ...int) *Matf64

Newf64 is the primary constructor for the "Matf64" object. New is a variadic function, expecting 0 to 2 integers, with differing behavior as follows:

m := matrix.Newf64()

m is now an empty &Matf64{}, where the number of rows, columns and the length and capacity of the underlying slice are all zero. This is mostly for internal use.

m := matrix.Newf64(x)

m is a x by x (square) matrix, with the underlying slice of length x, and capacity 2x.

m := matrix.Newf64(x, y)

m is an x by y matrix, with the underlying slice of length xy, and capacity of 2xy.

func RandMatf64

func RandMatf64(r, c int, args ...float64) *Matf64

RandMatf64 returns a Matf64 whose elements have random values. There are 3 ways to call RandMatf64:

m := matrix.RandMatf64(2, 3)

With this call, m is a 2X3 Matf64 whose elements have values randomly selected from the range (0, 1], (includes 0, but excludes 1).

m := matrix.RandMatf64(2, 3, x)

With this call, m is a 2X3 Matf64 whose elements have values randomly selected from the range (0, x], (includes 0, but excludes x).

m := matrix.RandMatf64(2, 3, x, y)

With this call, m is a 2X3 Matf64 whose elements have values randomly selected from the range (x, y], (includes x, but excludes y). In this case, x must be strictly less than y.

func (*Matf64) Add

func (m *Matf64) Add(float64OrMatf64 interface{}) *Matf64

Add carries the addition operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float64, then it is added to each element:

m := matrix.Newf64(2, 3).SetAll(5.0)
m.Add(2.0)

This will result in all values of m being 7.0. The passed Object can also be a Matf64, in which case each element of the element of the passed Matf64 is added to the corresponding element of the receiver. Note that the passed Matf64 must have the same shape as the receiver.

m := matrix.Newf64(2, 3).SetAll(10.0)
n := m.Copy()
m.Add(n)

This will result in each element of m being 20.0.

func (*Matf64) All

func (m *Matf64) All(f func(*float64) bool) bool

All checks if a supplied function is true for all elements of a mat object. For instance, consider

m.All(matrix.Positivef64)

will return true if and only if all elements in m are positive.

func (*Matf64) Any

func (m *Matf64) Any(f func(*float64) bool) bool

Any checks if a supplied function is true for one elements of a mat object. For instance,

m.Any(matrix.Positivef64)

would be true if at least one element of the mat object is positive.

func (*Matf64) Append

func (m *Matf64) Append(n *Matf64) *Matf64

Append merges a passed mat to the botton of the receiver. The passed mat must therefore have the same number of columns as the receiver. For example:

m := matrix.Newf64(1, 2).SetAll(2.0) // [[2.0, 2.0]]
n := matrix.Newf64(2, 2).SetAll(3.0) // [[3.0, 3.0], [3.0, 3.0]]
m.Append(n)
fmt.Println(m) // [[2.0, 2.0], [3.0, 3.0], [3.0, 3.0]]

Note that in the current implementation this is a somewhat expensive function.

func (*Matf64) AppendCol

func (m *Matf64) AppendCol(v []float64) *Matf64

AppendCol appends a column to the right side of a Matf64.

func (*Matf64) AppendRow

func (m *Matf64) AppendRow(v []float64) *Matf64

AppendRow appends a row to the bottom of a Matf64.

func (*Matf64) Avg

func (m *Matf64) Avg(args ...int) float64

Avg takes the average of the elements of a Matf64. It can be called in one of two ways:

m.Avg()

This will return the average of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Avg(0, 2) // Returns the average of the 3rd row
m.Avg(1, 0) // Returns the average of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf64) Col

func (m *Matf64) Col(x int) *Matf64

Col returns a new mat object whose values are equal to a column of the original mat object. The number of Rows of the returned mat object is equal to the number of rows of the original mat, and the number of columns is equal to 1.

This function supports negative indexing. For example,

v := m.Col(-1)

returns the last column of m.

func (*Matf64) Concat

func (m *Matf64) Concat(n *Matf64) *Matf64

Concat merges a passed mat to the right side of the receiver. The passed mat must therefore have the same number of rows as the receiver. For example:

m := matrix.Newf64(1, 2).SetAll(2.0) // [[2.0, 2.0]]
n := matrix.Newf64(1, 3).SetAll(3.0) // [[3.0, 3.0, 3.0]]
m.Concat(n)
fmt.Println(m) // [[2.0, 2.0, 3.0, 3.0, 3.0]]

Note that in the current implementation this is a somewhat expensive function.

func (*Matf64) Copy

func (m *Matf64) Copy() *Matf64

Copy returns a duplicate of a mat object. The returned copy is "deep", meaning that the object can be manipulated without effecting the original mat object.

func (*Matf64) Div

func (m *Matf64) Div(float64OrMatf64 interface{}) *Matf64

Div carries the division operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float64, then each element of the receiver is devided by it:

m := Newf64(2, 3).SetAll(5.0)
m.Div(2.0)

This will result in all values of m being 2.5. Note that the passed float64 cannot be 0.0.

The passed Object can also be a Matf64, in which case each element of the passed Matf64 is subtracted from the corresponding element of the receiver. Note that the passed Matf64 must have the same shape as the receiver, and it cannot contains any elements which are 0.0.

m := matrix.Newf64(2, 3).SetAll(10.0)
n := m.Copy()
m.Div(n)

This will result in each element of m being 1.0.

func (*Matf64) Dot

func (m *Matf64) Dot(n *Matf64) *Matf64

Dot is the matrix multiplication of two mat objects. Consider the following two mats:

m := matrix.Newf64(5, 6)
n := matrix.Newf64(6, 10)

then

o := m.Dot(n)

is a 5 by 10 mat whose element at row i and column j is given by:

Sum(m.Row(i).Mul(n.col(j))

func (*Matf64) Equals

func (m *Matf64) Equals(n *Matf64) bool

Equals checks to see if two mat objects are equal. That mean that the two mats have the same number of rows, same number of columns, and have the same float64 in each entry at a given index.

func (*Matf64) Get

func (m *Matf64) Get(r, c int) float64

Get returns a pointer to the float64 stored in the given row and column.

func (*Matf64) Map

func (m *Matf64) Map(f func(*float64)) *Matf64

Map applies a given function to each element of a mat object. The given function must take a pointer to a float64, and return nothing. For eaxmple, lets say that we wish to take the error function of each element of a Matf64. The following would do this:

m.Map(func(i *float64) {
	*i = math.Erf(*i)
})

func (*Matf64) Max

func (m *Matf64) Max(args ...int) (index int, maxVal float64)

Max returns the index and the value of the biggest float64 in a Matf64. This method can be called in one of two ways:

idx, val := m.Max()

will return the index, and value of the biggest float64 in m. We can also specify the exact row and column for which we want the minimum index and values:

idx, val := m.Max(0, 3) // Get the max index and value of the 4th row
idx, val := m.Max(1, 2) // Get the max index and value of the 3rd column

Note that negative index values are not supported at this time. Also note that in the case where multiple values are the maximum, the index of the first encountered value is returned.

func (*Matf64) Min

func (m *Matf64) Min(args ...int) (index int, minVal float64)

Min returns the index and the value of the smallest float64 in a Matf64. This method can be called in one of two ways:

idx, val := m.Min()

will return the index, and value of the smallest float64 in m. We can also specify the exact row and column for which we want the minimum index and values:

idx, val := m.Min(0, 3) // Get the min index and value of the 4th row
idx, val := m.Min(1, 2) // Get the min index and value of the 3rd column

Note that negative index values are not supported at this time. Also note that in the case where multiple values are the maximum, the index of the first encountered value is returned.

func (*Matf64) Mul

func (m *Matf64) Mul(float64OrMatf64 interface{}) *Matf64

Mul carries the multiplication operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float64, then each element is multiplied by it:

m := matrix.Newf64(2, 3).SetAll(5.0)
m.Mul(2.0)

This will result in all values of m being 10.0. The passed Object can also be a Matf64, in which case each element of the receiver are multiplied by the corresponding element of the passed Matf64. Note that the passed Matf64 must have the same shape as the receiver.

m := matrix.Newf64(2, 3).SetAll(10.0)
n := m.Copy()
m.Mul(n)

This will result in each element of m being 100.0.

Note: For the matrix cross product see the Dot() method.

func (*Matf64) Prd

func (m *Matf64) Prd(args ...int) float64

Prd takes the product of the elements of a Matf64. It can be called in one of two ways:

m.Prd()

This will return the product of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Prd(0, 2) // Returns the product of the 3rd row
m.Prd(1, 0) // Returns the product of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf64) Reshape

func (m *Matf64) Reshape(rows, cols int) *Matf64

Reshape changes the row and the columns of the mat object as long as the total number of values contained in the mat object remains constant. The order and the values of the mat does not change with this function.

func (*Matf64) Row

func (m *Matf64) Row(x int) *Matf64

Row returns a new mat object whose values are equal to a row of the original mat object. The number of Rows of the returned mat object is equal to 1, and the number of columns is equal to the number of columns of the original mat.

This function supports negative indexing. For example,

v := m.Row(-1)

returns the last row of m.

func (*Matf64) Set

func (m *Matf64) Set(r, c int, val float64) *Matf64

Set sets the value of a mat at a given row and column to a given value.

func (*Matf64) SetAll

func (m *Matf64) SetAll(val float64) *Matf64

SetAll sets all values of a mat to the passed float64 value.

func (*Matf64) SetCol

func (m *Matf64) SetCol(col int, floatOrSlice interface{}) *Matf64

SetCol Sets all elements in a given column to the passed value(s). Negative index values are allowed. For example:

m.SetCol(-1, 2.0)

sets all values of m's last column to 2.0. It is also possible to pass a slice of float64 to this function, all the elements of the chosen column will be set to the corresponding values in the slice. For example:

m := Newf64(2, 2).SetCol(0, []float64{1.0, 2.0})

sets to values in the first column of m to 1.0 and 2.0 respectively. Note that in this case, the length of the passed slice must match exactly the number of elements in m's column, i.e. the number of rows of m.

func (*Matf64) SetRow

func (m *Matf64) SetRow(row int, floatOrSlice interface{}) *Matf64

SetRow Sets all elements in a given column to the passed value(s). Negative index values are allowed. For example:

m.SetRow(-1, 2.0)

sets all values of m's last row to 2.0. It is also possible to pass a slice of float64 to this function, all the elements of the chosen row will be set to the corresponding values in the slice. For example:

m := Newf64(2, 2).SetRow(0, []float64{1.0, 2.0})

sets to values in the first row of m to 1.0 and 2.0 respectively. Note that in this case, the length of the passed slice must match exactly the number of elements in m's row, i.e. the number of cols of m.

func (*Matf64) Shape

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

Shape returns the number of rows and columns of a mat object.

func (*Matf64) Std

func (m *Matf64) Std(args ...int) float64

Std takes the standard deviation of the elements of a Matf64. It can be called in one of two ways:

m.Std()

This will return the std. div. of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Std(0, 2) // Returns the standard deviation of the 3rd row
m.Std(1, 0) // Returns the standard deviation of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf64) String

func (m *Matf64) String() string

String returns the string representation of a mat. This is done by putting every row into a line, and separating the entries of that row by a space. note that the last line does not contain a newline.

func (*Matf64) Sub

func (m *Matf64) Sub(float64OrMatf64 interface{}) *Matf64

Sub carries the subtraction operation between each element of the receiver and an object passed to it. Based on the type of the passed object, the results of this method changes:

If the passed object is a float64, then it is subtracted from each element:

m := matrix.Newf64(2, 3).SetAll(5.0)
m.Sub(2.0)

This will result in all values of m being 3.0. The passed Object can also be a Matf64, in which case each element of the passed Matf64 is subtracted from the corresponding element of the receiver. Note that the passed Matf64 must have the same shape as the receiver.

m := matrix.Newf64(2, 3).SetAll(10.0)
n := m.Copy()
m.Sub(n)

This will result in each element of m being 0.0.

func (*Matf64) Sum

func (m *Matf64) Sum(args ...int) float64

Sum takes the sum of the elements of a Matf64. It can be called in one of two ways:

m.Sum()

This will return the sum of all elements in m. This method can also be called by passing 2 integers: 0 or 1 for row or column, and another int specifying the row or column. For example:

m.Sum(0, 2) // Returns the sum of the 3rd row
m.Sum(1, 0) // Returns the sum of the first column.

Note that second passed integer cannot be less than 0, or greater that the length of the matrix in that dimension.

func (*Matf64) T

func (m *Matf64) T() *Matf64

T returns the transpose of the original matrix. The transpose of a mat object is defined in the usual manner, where every value at row x, and column y is placed at row y, and column x. The number of rows and column of the transposed mat are equal to the number of columns and rows of the original matrix, respectively. This method creates a new mat object, and the original is left intact.

func (*Matf64) ToCSV

func (m *Matf64) ToCSV(fileName string)

ToCSV creates a file with the passed name, and writes the content of a mat object to it, by putting each row in a single comma separated line. The number of entries in each line is equal to the columns of the mat object.

func (*Matf64) ToSlice1D

func (m *Matf64) ToSlice1D() []float64

ToSlice1D returns the values contained in a mat object as a 1D slice of float64s.

func (*Matf64) ToSlice2D

func (m *Matf64) ToSlice2D() [][]float64

ToSlice2D returns the values of a mat object as a 2D slice of float64s.

Jump to

Keyboard shortcuts

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