matrix

package
v0.0.0-...-a3b61a5 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Argmax

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.Argmax(A))

}
Output:

[2 1 0]

func Broadcast

func Broadcast(m, n Matrix) (Matrix, Matrix)
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

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

func Dim

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	A := matrix.Zero(2, 3)
	fmt.Println(matrix.Dim(A))

}
Output:

2 3

func Flatten

func Flatten(m Matrix) []float64

func Max

func Max(m Matrix) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.Max(A))

}
Output:

5

func Mean

func Mean(m Matrix) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.Mean(A))

}
Output:

5.5

func Min

func Min(m Matrix) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.Min(A))

}
Output:

-5

func Shape

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	A := matrix.Zero(2, 3)
	fmt.Println(matrix.Shape(A))

}
Output:

[2 3]

func Size

func Size(m Matrix) int
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	A := matrix.Zero(2, 3)
	fmt.Println(matrix.Size(A))

}
Output:

6

func Sum

func Sum(m Matrix) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.Sum(A))

}
Output:

10

Types

type Matrix

type Matrix [][]float64

func Add

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

import (
	"fmt"

	"github.com/itsubaki/autograd/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.Add(A, B) {
		fmt.Println(r)
	}

}
Output:

[6 8]
[10 12]

func AddC

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[2 3]
[4 5]

func BroadcastTo

func BroadcastTo(shape []int, m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	for _, r := range matrix.BroadcastTo([]int{3, 5}, A) {
		fmt.Println(r)
	}

}
Output:

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	for _, r := range matrix.BroadcastTo([]int{-1, 5}, A) {
		fmt.Println(r)
	}

}
Output:

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	for _, r := range matrix.BroadcastTo([]int{2, 2}, A) {
		fmt.Println(r)
	}

}
Output:

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	for _, r := range matrix.BroadcastTo([]int{5, -1}, A) {
		fmt.Println(r)
	}

}
Output:

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

func Clip

func Clip(m Matrix, min, max float64) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	A := matrix.New(
		[]float64{-3, -2, -1, 0, 1, 2, 3, 4, 5, 6},
		[]float64{7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
	)

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

}
Output:

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

func Cos

func Cos(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[0.5403023058681398 -0.4161468365471424]
[-0.9899924966004454 -0.6536436208636119]

func Div

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

import (
	"fmt"

	"github.com/itsubaki/autograd/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.Div(A, B) {
		fmt.Println(r)
	}

}
Output:

[0.2 0.3333333333333333]
[0.42857142857142855 0.5]

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/autograd/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 Exp

func Exp(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[2.718281828459045 7.38905609893065]
[20.085536923187668 54.598150033144236]

func F

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

func F2

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

func From

func From(x [][]int) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	for _, r := range matrix.From([][]int{{1, 2}, {3, 4}}) {
		fmt.Printf("%.2f\n", r)
	}

}
Output:

[1.00 2.00]
[3.00 4.00]

func Log

func Log(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[0 0.6931471805599453]
[1.0986122886681096 1.3862943611198906]

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/autograd/matrix"
)

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

	for _, r := range matrix.Mask(A, func(v float64) bool { return v > 0 }) {
		fmt.Println(r)
	}

}
Output:

[0 1]
[1 0]

func MaxAxis1

func MaxAxis1(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[2]
[4]
[5]

func Mul

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

import (
	"fmt"

	"github.com/itsubaki/autograd/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.Mul(A, B) {
		fmt.Println(r)
	}

}
Output:

[5 12]
[21 32]

func MulC

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[2 4]
[6 8]

func New

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

func OneLike

func OneLike(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

}
Output:

[1 1 1]
[1 1 1]

func Pow

func Pow(c float64, m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[1 4]
[9 16]

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.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	shape := matrix.Shape(matrix.Rand(2, 3))
	fmt.Println(shape)

}
Output:

[2 3]
Example (Nil)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	shape := matrix.Shape(matrix.Rand(2, 3, nil))
	fmt.Println(shape)

}
Output:

[2 3]
Example (Seed)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
	"github.com/itsubaki/autograd/rand"
)

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

}
Output:

[0.9999275824802834 0.8856419373528862 0.38147752771154886]
[0.4812673234167829 0.44417259544314847 0.5210016660132573]

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.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

func main() {
	shape := matrix.Shape(matrix.Randn(2, 3))
	fmt.Println(shape)

}
Output:

[2 3]
Example (Seed)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
	"github.com/itsubaki/autograd/rand"
)

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

}
Output:

[0.5665360716030388 -0.6123972949371448 0.5898947122637695]
[-0.3678242340302933 1.0919575041640825 -0.4438344619606553]

func Reshape

func Reshape(shape []int, m Matrix) Matrix

Reshape returns the matrix with the given shape.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.Reshape([]int{1, 4}, A))
	fmt.Println(matrix.Reshape([]int{4, 1}, A))
	fmt.Println(matrix.Reshape([]int{2, 2}, A))
	fmt.Println()

	fmt.Println(matrix.Reshape([]int{1, -1}, A))
	fmt.Println(matrix.Reshape([]int{4, -1}, A))
	fmt.Println(matrix.Reshape([]int{2, -1}, A))
	fmt.Println()

	fmt.Println(matrix.Reshape([]int{-1, 1}, A))
	fmt.Println(matrix.Reshape([]int{-1, 4}, A))
	fmt.Println(matrix.Reshape([]int{-1, 2}, A))
	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 Sin

func Sin(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[0.8414709848078965 0.9092974268256816]
[0.1411200080598672 -0.7568024953079282]

func Sub

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	B := matrix.New(
		[]float64{10, 20},
		[]float64{30, 40},
	)

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

}
Output:

[-9 -18]
[-27 -36]

func SubC

func SubC(c float64, m Matrix) Matrix

SubC returns c - m

Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/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 SumAxis0

func SumAxis0(m Matrix) Matrix

SumAxis0 returns the sum of each column.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[4 6]

func SumAxis1

func SumAxis1(m Matrix) Matrix

SumAxis1 returns the sum of each row.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[3]
[7]

func SumTo

func SumTo(shape []int, m Matrix) Matrix
Example (Axis0)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.SumTo([]int{1, 2}, A))

}
Output:

[[4 6]]
Example (Axis1)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.SumTo([]int{2, 1}, A))

}
Output:

[[3] [7]]
Example (Noeffect)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.SumTo([]int{2, 3}, A))

}
Output:

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

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

	fmt.Println(matrix.SumTo([]int{1, 1}, A))

}
Output:

[[10]]

func Tanh

func Tanh(m Matrix) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

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

}
Output:

[0.7615941559557649 0.9640275800758169]
[0.9950547536867305 0.999329299739067]

func Transpose

func Transpose(m Matrix) Matrix

func Zero

func Zero(m, n int) Matrix
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/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
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/matrix"
)

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

}
Output:

[0 0 0]
[0 0 0]

Jump to

Keyboard shortcuts

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