Documentation
¶
Index ¶
- func Flatten(x Matrix) []float64
- func Int(m Matrix) [][]int
- type Matrix
- func Batch(m Matrix, index []int) Matrix
- func Column(m Matrix, j int) Matrix
- func Dot(m, n Matrix) Matrix
- func F(m Matrix, f func(a float64) float64) Matrix
- func F2(m, n Matrix, f func(a, b float64) float64) Matrix
- func F3(m, n, o Matrix, f func(a, b, c float64) float64) Matrix
- func From(x [][]int) Matrix
- func HStack(x ...Matrix) Matrix
- func Identity(size int) Matrix
- func Mask(m Matrix, f func(x float64) bool) Matrix
- func New(v ...[]float64) Matrix
- func One(m, n int) Matrix
- func OneHot(x []int, size int) Matrix
- func Padding(x Matrix, pad int) Matrix
- func Rand(m, n int, s ...randv2.Source) Matrix
- func Randn(m, n int, s ...randv2.Source) Matrix
- func Reshape(x Matrix, m, n int) Matrix
- func Split(x Matrix, H int) []Matrix
- func SubC(c float64, m Matrix) Matrix
- func Unpadding(x Matrix, pad int) Matrix
- func Zero(m, n int) Matrix
- func ZeroLike(m Matrix) Matrix
- func (m Matrix) Abs() Matrix
- func (m Matrix) Add(n Matrix) Matrix
- func (m Matrix) AddC(c float64) Matrix
- func (m Matrix) Argmax() []int
- func (m Matrix) Broadcast(a, b int) Matrix
- func (m Matrix) Dim() (int, int)
- func (m Matrix) Div(n Matrix) Matrix
- func (m Matrix) MaxAxis1() []float64
- func (m Matrix) Mean() float64
- func (m Matrix) MeanAxis0() []float64
- func (m Matrix) Mul(n Matrix) Matrix
- func (m Matrix) MulC(c float64) Matrix
- func (m Matrix) Pow2() Matrix
- func (m Matrix) Size() int
- func (m Matrix) Sqrt(eps float64) Matrix
- func (m Matrix) Sub(n Matrix) Matrix
- func (m Matrix) Sum() float64
- func (m Matrix) SumAxis0() []float64
- func (m Matrix) SumAxis1() []float64
- func (m Matrix) T() Matrix
Examples ¶
- Batch
- Column
- Dot
- F
- F2
- F3
- From
- HStack
- Identity
- Int
- Mask
- Matrix.Abs
- Matrix.Add
- Matrix.AddC
- Matrix.Argmax
- Matrix.Broadcast
- Matrix.Broadcast (Column)
- Matrix.Broadcast (NoEffect)
- Matrix.Broadcast (Row)
- Matrix.Dim
- Matrix.Div
- Matrix.MaxAxis1
- Matrix.Mean
- Matrix.MeanAxis0
- Matrix.Mul
- Matrix.MulC
- Matrix.Pow2
- Matrix.Size
- Matrix.Sqrt
- Matrix.Sub
- Matrix.SumAxis0
- Matrix.SumAxis1
- Matrix.T
- One
- OneHot
- Padding
- Rand
- Randn
- Reshape
- Split
- SubC
- Zero
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Matrix ¶
type Matrix [][]float64
func Batch ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 One ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 Zero ¶
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 (Matrix) Abs ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) SumAxis0 ¶
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]