matrix

package
v0.0.0-...-25ab4ef Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flatten

func Flatten(x Matrix) []float64

func Int

func Int(m Matrix) [][]int
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	fmt.Println(matrix.Int(matrix.New([]float64{1.1, 2.2, 3.3})))

}
Output:

[[1 2 3]]

Types

type Matrix

type Matrix [][]float64

func Batch

func Batch(m Matrix, index []int) Matrix

Batch returns a matrix with rows of the specified index.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
		[]float64{5, 6},
		[]float64{7, 8},
		[]float64{9, 10},
	)
	for _, r := range matrix.Batch(x, []int{0, 2, 4}) {
		fmt.Println(r)
	}

}
Output:

[1 2]
[5 6]
[9 10]

func Column

func Column(m Matrix, j int) Matrix

Column returns a matrix with the specified column.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2, 3},
		[]float64{4, 5, 6},
	)
	fmt.Println(matrix.Column(x, 0))
	fmt.Println(matrix.Column(x, 1))
	fmt.Println(matrix.Column(x, 2))

}
Output:

[[1] [4]]
[[2] [5]]
[[3] [6]]

func Dot

func Dot(m, n Matrix) Matrix

Dot returns the dot product of m and n.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 6},
		[]float64{7, 8},
	)

	for _, r := range matrix.Dot(A, B) {
		fmt.Println(r)
	}

}
Output:

[19 22]
[43 50]

func F

func F(m Matrix, f func(a float64) float64) Matrix

F applies a function to each element of the matrix.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	for _, r := range matrix.F(A, func(v float64) float64 { return v * 2 }) {
		fmt.Println(r)
	}

}
Output:

[2 4]
[6 8]

func F2

func F2(m, n Matrix, f func(a, b float64) float64) Matrix

F2 applies a function to each element of the matrix.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 6},
		[]float64{7, 8},
	)

	for _, r := range matrix.F2(A, B, func(a, b float64) float64 { return a * b }) {
		fmt.Println(r)
	}

}
Output:

[5 12]
[21 32]

func F3

func F3(m, n, o Matrix, f func(a, b, c float64) float64) Matrix

F3 applies a function to each element of the matrix.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 6},
		[]float64{7, 8},
	)

	C := matrix.New(
		[]float64{9, 9},
		[]float64{9, 9},
	)

	for _, r := range matrix.F3(A, B, C, func(a, b, c float64) float64 { return a*b - c }) {
		fmt.Println(r)
	}

}
Output:

[-4 3]
[12 23]

func From

func From(x [][]int) Matrix

From returns a matrix from a slice of slice of T.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	fmt.Printf("%.2f", matrix.From([][]int{
		{1, 2, 3},
		{4, 5, 6},
	}))

}
Output:

[[1.00 2.00 3.00] [4.00 5.00 6.00]]

func HStack

func HStack(x ...Matrix) Matrix

HStack returns the matrix horizontally stacked.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	a := matrix.New([]float64{1, 2, 3}, []float64{4, 5, 6})
	b := matrix.New([]float64{7, 8, 9}, []float64{10, 11, 12})

	for _, r := range matrix.HStack(a, b) {
		fmt.Println(r)
	}

}
Output:

[1 2 3 7 8 9]
[4 5 6 10 11 12]

func Identity

func Identity(size int) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	fmt.Println(matrix.Identity(3))

}
Output:

[[1 0 0] [0 1 0] [0 0 1]]

func Mask

func Mask(m Matrix, f func(x float64) bool) Matrix

Mask returns a matrix with elements that 1 if f() is true and 0 otherwise.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, -0.5},
		[]float64{-2, 3},
	)
	mask := matrix.Mask(x, func(x float64) bool { return x > 0 })

	for _, r := range mask {
		fmt.Println(r)
	}

}
Output:

[1 0]
[0 1]

func New

func New(v ...[]float64) Matrix

func One

func One(m, n int) Matrix

One returns a matrix with all elements 1.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	for _, r := range matrix.One(2, 3) {
		fmt.Println(r)
	}

}
Output:

[1 1 1]
[1 1 1]

func OneHot

func OneHot(x []int, size int) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	for _, v := range matrix.OneHot([]int{0, 1, 2, 3, 4, 1, 5, 6}, 7) {
		fmt.Println(v)
	}

}
Output:

[1 0 0 0 0 0 0]
[0 1 0 0 0 0 0]
[0 0 1 0 0 0 0]
[0 0 0 1 0 0 0]
[0 0 0 0 1 0 0]
[0 1 0 0 0 0 0]
[0 0 0 0 0 1 0]
[0 0 0 0 0 0 1]

func Padding

func Padding(x Matrix, pad int) Matrix

Padding returns the padded matrix.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)
	pad := 1

	p := matrix.Padding(x, pad)
	for _, v := range p {
		fmt.Println(v)
	}
	fmt.Println()

	for _, v := range matrix.Unpadding(p, pad) {
		fmt.Println(v)
	}

}
Output:

[0 0 0 0]
[0 1 2 0]
[0 3 4 0]
[0 0 0 0]

[1 2]
[3 4]

func Rand

func Rand(m, n int, s ...randv2.Source) Matrix

Rand returns a matrix with elements that pseudo-random number in the half-open interval [0.0,1.0). m, n is the dimension of the matrix. s is the source of the pseudo-random number generator.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
	"github.com/itsubaki/neu/math/rand"
)

func main() {
	fmt.Println(matrix.Rand(2, 3).Dim())

	s := rand.Const(1)
	for _, r := range matrix.Rand(2, 3, s) {
		fmt.Println(r)
	}

}
Output:

2 3
[0.23842319087387442 0.50092138792625 0.04999911180706662]
[0.4894631469238666 0.7500167893718852 0.5725763969460762]

func Randn

func Randn(m, n int, s ...randv2.Source) Matrix

Randn returns a matrix with elements that normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution. m, n is the dimension of the matrix. s is the source of the pseudo-random number generator.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
	"github.com/itsubaki/neu/math/rand"
)

func main() {
	s := rand.Const(1)
	for _, r := range matrix.Randn(2, 3, s) {
		fmt.Println(r)
	}
	fmt.Println(matrix.Randn(2, 3).Dim())

}
Output:

[-0.8024826241110656 0.424707052949676 -0.4985070978632815]
[-0.9872764577745818 0.4770185009670911 -0.37300956589935985]
2 3

func Reshape

func Reshape(x Matrix, m, n int) Matrix

Reshape returns the matrix with the given shape.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	fmt.Println(matrix.Reshape(x, 1, 4))
	fmt.Println(matrix.Reshape(x, 4, 1))
	fmt.Println(matrix.Reshape(x, 2, 2))
	fmt.Println()

	fmt.Println(matrix.Reshape(x, 1, -1))
	fmt.Println(matrix.Reshape(x, 4, -1))
	fmt.Println(matrix.Reshape(x, 2, -1))
	fmt.Println()

	fmt.Println(matrix.Reshape(x, -1, 1))
	fmt.Println(matrix.Reshape(x, -1, 4))
	fmt.Println(matrix.Reshape(x, -1, 2))
	fmt.Println()

}
Output:

[[1 2 3 4]]
[[1] [2] [3] [4]]
[[1 2] [3 4]]

[[1 2 3 4]]
[[1] [2] [3] [4]]
[[1 2] [3 4]]

[[1] [2] [3] [4]]
[[1 2 3 4]]
[[1 2] [3 4]]

func Split

func Split(x Matrix, H int) []Matrix

Split returns the matrix split into H parts.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1.1, 2.1, 3.1, 4.1, 5.1, 6.1},
		[]float64{1.2, 2.2, 3.2, 4.2, 5.2, 6.2},
		[]float64{1.3, 2.3, 3.3, 4.3, 5.3, 6.3},
		[]float64{1.4, 2.4, 3.4, 4.4, 5.4, 6.4},
	)
	for _, v := range matrix.Split(x, 2) {
		fmt.Println(v)
	}

}
Output:

[[1.1 2.1] [1.2 2.2] [1.3 2.3] [1.4 2.4]]
[[3.1 4.1] [3.2 4.2] [3.3 4.3] [3.4 4.4]]
[[5.1 6.1] [5.2 6.2] [5.3 6.3] [5.4 6.4]]

func SubC

func SubC(c float64, m Matrix) Matrix

SubC returns c - m

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	for _, r := range matrix.SubC(1, A) {
		fmt.Println(r)
	}

}
Output:

[0 -1]
[-2 -3]

func Unpadding

func Unpadding(x Matrix, pad int) Matrix

Unpadding returns the unpadded matrix.

func Zero

func Zero(m, n int) Matrix

Zero returns a matrix with all elements 0.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	for _, r := range matrix.Zero(2, 3) {
		fmt.Println(r)
	}

}
Output:

[0 0 0]
[0 0 0]

func ZeroLike

func ZeroLike(m Matrix) Matrix

func (Matrix) Abs

func (m Matrix) Abs() Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{-1, 2},
		[]float64{3, -4},
	)

	for _, r := range A.Abs() {
		fmt.Println(r)
	}

}
Output:

[1 2]
[3 4]

func (Matrix) Add

func (m Matrix) Add(n Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 6},
		[]float64{7, 8},
	)

	for _, r := range A.Add(B) {
		fmt.Println(r)
	}

}
Output:

[6 8]
[10 12]

func (Matrix) AddC

func (m Matrix) AddC(c float64) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	for _, r := range A.AddC(2) {
		fmt.Println(r)
	}

}
Output:

[3 4]
[5 6]

func (Matrix) Argmax

func (m Matrix) Argmax() []int

Argmax returns the index of the maximum value of each row.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2, 3},
		[]float64{6, 5, 4},
	)
	fmt.Println(A.Argmax())

}
Output:

[2 0]

func (Matrix) Broadcast

func (m Matrix) Broadcast(a, b int) Matrix

Broadcast returns the broadcasted matrix.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	m := matrix.New([]float64{1})
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()

	for _, r := range m.Broadcast(3, 5) {
		fmt.Println(r)
	}

}
Output:

[1]

[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
Example (Column)
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	m := matrix.New(
		[]float64{1},
		[]float64{2},
	)
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()

	for _, r := range m.Broadcast(-1, 5) {
		fmt.Println(r)
	}

}
Output:

[1]
[2]

[1 1 1 1 1]
[2 2 2 2 2]
Example (NoEffect)
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	m := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()

	for _, r := range m.Broadcast(2, 2) {
		fmt.Println(r)
	}

}
Output:

[1 2]
[3 4]

[1 2]
[3 4]
Example (Row)
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	m := matrix.New([]float64{1, 2})
	for _, r := range m {
		fmt.Println(r)
	}
	fmt.Println()

	for _, r := range m.Broadcast(5, -1) {
		fmt.Println(r)
	}

}
Output:

[1 2]

[1 2]
[1 2]
[1 2]
[1 2]
[1 2]

func (Matrix) Dim

func (m Matrix) Dim() (int, int)
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	fmt.Println(matrix.New().Dim())
	fmt.Println(matrix.New([]float64{1, 2, 3}).Dim())

}
Output:

0 0
1 3

func (Matrix) Div

func (m Matrix) Div(n Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 2},
		[]float64{1, 8},
	)

	for _, r := range A.Div(B) {
		fmt.Println(r)
	}

}
Output:

[0.2 1]
[3 0.5]

func (Matrix) MaxAxis1

func (m Matrix) MaxAxis1() []float64

MaxAxis1 returns the maximum value of each row.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2, 3},
		[]float64{4, 5, 6},
	)
	fmt.Println(x.MaxAxis1())

}
Output:

[3 6]

func (Matrix) Mean

func (m Matrix) Mean() float64

Mean returns the average of all elements.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2, 3},
		[]float64{4, 5, 6},
	)

	fmt.Println(A.Sum())
	fmt.Println(A.Mean())

}
Output:

21
3.5

func (Matrix) MeanAxis0

func (m Matrix) MeanAxis0() []float64

MeanAxis0 returns the mean of each column.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2, 3},
		[]float64{4, 5, 6},
	)

	fmt.Println(x.MeanAxis0())

}
Output:

[2.5 3.5 4.5]

func (Matrix) Mul

func (m Matrix) Mul(n Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 6},
		[]float64{7, 8},
	)

	for _, r := range A.Mul(B) {
		fmt.Println(r)
	}

}
Output:

[5 12]
[21 32]

func (Matrix) MulC

func (m Matrix) MulC(c float64) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	for _, r := range A.MulC(2) {
		fmt.Println(r)
	}

}
Output:

[2 4]
[6 8]

func (Matrix) Pow2

func (m Matrix) Pow2() Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	for _, r := range A.Pow2() {
		fmt.Println(r)
	}

}
Output:

[1 4]
[9 16]

func (Matrix) Size

func (m Matrix) Size() int
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	fmt.Println(matrix.New().Size())
	fmt.Println(matrix.New([]float64{1, 2, 3}, []float64{1, 2, 3}).Size())

}
Output:

0
6

func (Matrix) Sqrt

func (m Matrix) Sqrt(eps float64) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	for _, r := range A.Sqrt(0) {
		fmt.Println(r)
	}

}
Output:

[1 1.4142135623730951]
[1.7320508075688772 2]

func (Matrix) Sub

func (m Matrix) Sub(n Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2},
		[]float64{3, 4},
	)

	B := matrix.New(
		[]float64{5, 6},
		[]float64{7, 8},
	)

	for _, r := range A.Sub(B) {
		fmt.Println(r)
	}

}
Output:

[-4 -4]
[-4 -4]

func (Matrix) Sum

func (m Matrix) Sum() float64

Sum returns the sum of all elements.

func (Matrix) SumAxis0

func (m Matrix) SumAxis0() []float64

SumAxis0 returns the sum of each column.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{1, 2, 3},
		[]float64{4, 5, 6},
	)
	fmt.Println(x.SumAxis0())

}
Output:

[5 7 9]

func (Matrix) SumAxis1

func (m Matrix) SumAxis1() []float64

SumAxis1 returns the sum of each row.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	x := matrix.New(
		[]float64{0, 1, 4},
		[]float64{27, 40, 55},
		[]float64{18, 28, 40},
	)
	fmt.Println(x.SumAxis1())

}
Output:

[5 122 86]

func (Matrix) T

func (m Matrix) T() Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/neu/math/matrix"
)

func main() {
	A := matrix.New(
		[]float64{1, 2, 3},
		[]float64{4, 5, 6},
	)

	for _, r := range A.T() {
		fmt.Println(r)
	}

}
Output:

[1 4]
[2 5]
[3 6]

Jump to

Keyboard shortcuts

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