vector

package
v0.0.0-...-3b1b184 Latest Latest
Warning

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

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(v []float64) []float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, -2, -3}
	fmt.Println(vector.Abs(v))

}
Output:

[1 2 3]

func Add

func Add(v, w []float64) []float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	w := []float64{4, 5, 6}

	fmt.Println(vector.Add(v, w))

}
Output:

[5 7 9]

func Argmax

func Argmax(v []float64) int
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Argmax(v))

}
Output:

2

func Choice

func Choice(p []float64, s ...randv2.Source) int
Example
package main

import (
	"fmt"

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

func main() {
	s := rand.Const(1)
	p := []float64{0.1, 0.2, 0.3, 0.4}

	for i := 0; i < 10; i++ {
		fmt.Print(vector.Choice(p, s))
	}

}
Output:

1202321031
Example (Rand)
package main

import (
	"fmt"

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

func main() {
	p := []float64{0.1, 0.2, 0.3, 0.4}
	if vector.Choice(p) < 0 {
		fmt.Println("invalid")
	}

}
Output:

func Contains

func Contains[T comparable](v T, s []T) bool
Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.Contains(3, []int{1, 2, 3}))
	fmt.Println(vector.Contains(0, []int{1, 2, 3}))

}
Output:

true
false

func Cos

func Cos(x, y []float64) float64
Example
package main

import (
	"fmt"

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

func main() {
	x := []float64{1, 2, 3}
	y := []float64{1, 2, 4}
	fmt.Println(vector.Cos(x, y))

}
Output:

0.9914601333935124

func Div

func Div(v []float64, a float64) []float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Div(v, 2))

}
Output:

[0.5 1 1.5]

func Equals

func Equals(x, y []int) bool

Equals returns true if x and y are the same.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.Equals(
		[]int{1, 2, 3, 4, 5},
		[]int{1, 2, 3, 4, 5},
	))

	fmt.Println(vector.Equals(
		[]int{1, 2, 3, 4, 5},
		[]int{0, 2, 4, 3, 5},
	))

	fmt.Println(vector.Equals(
		[]int{1, 2, 3},
		[]int{0, 2, 4, 3, 5},
	))

}
Output:

true
false
false

func Int

func Int(v []float64) []int
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Int(v))

}
Output:

[1 2 3]

func MatchCount

func MatchCount[T comparable](x, y []T) int

MatchCount returns the number of matches.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.MatchCount(
		[]int{1, 2, 3, 4, 5},
		[]int{0, 2, 4, 3, 5},
	))

}
Output:

2

func Max

func Max[T int | float64](v []T) T
Example
package main

import (
	"fmt"

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

func main() {
	v := []int{1, 2, 3}
	fmt.Println(vector.Max(v))

}
Output:

3

func Mean

func Mean(v []float64) float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Mean(v))

}
Output:

2

func Mul

func Mul(v []float64, a float64) []float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Mul(v, 2))

}
Output:

[2 4 6]

func Pow2

func Pow2(v []float64) []float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Pow2(v))

}
Output:

[1 4 9]

func Rand

func Rand(n int, s ...randv2.Source) []float64
Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.Rand(3, rand.Const(1)))
	fmt.Println(len(vector.Rand(10)))

}
Output:

[0.23842319087387442 0.50092138792625 0.04999911180706662]
10

func Randn

func Randn(n int, s ...randv2.Source) []float64
Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.Randn(3, rand.Const(1)))
	fmt.Println(len(vector.Randn(10)))

}
Output:

[-0.8024826241110656 0.424707052949676 -0.4985070978632815]
10

func Reverse

func Reverse[T any](xs []T) []T

Reverse reverses the slice.

Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.Reverse([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}))

}
Output:

[9 8 7 6 5 4 3 2 1]

func Shuffle

func Shuffle[T any](x, t []T, s ...randv2.Source) ([]T, []T)

Shuffle shuffles the dataset.

Example
package main

import (
	"fmt"

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

func main() {
	x := [][]float64{{0, 1}, {0, 2}, {0, 3}, {0, 4}}
	t := [][]float64{{1, 0}, {2, 0}, {3, 0}, {4, 0}}

	s := rand.Const(1234)
	fmt.Println(vector.Shuffle(x, t, s))
	fmt.Println(vector.Shuffle(x, t, s))
	fmt.Println(vector.Shuffle(x, t, s))
	fmt.Println(x, t)

	fmt.Println(vector.Shuffle([][]float64{{0}}, [][]float64{{1}}))
	fmt.Println(vector.Shuffle([][]int{{1, 2, 3}, {4, 5, 6}}, [][]int{{7, 8, 9}, {10, 11, 12}}, rand.Const(2)))

}
Output:

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

func Sum

func Sum(v []float64) float64
Example
package main

import (
	"fmt"

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

func main() {
	v := []float64{1, 2, 3}
	fmt.Println(vector.Sum(v))

}
Output:

6

func T

func T[T any](v []T) [][]T
Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.T([]int{1, 2, 3}))

}
Output:

[[1] [2] [3]]

func Zero

func Zero(n int) []float64
Example
package main

import (
	"fmt"

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

func main() {
	fmt.Println(vector.Zero(3))

}
Output:

[0 0 0]

Types

This section is empty.

Jump to

Keyboard shortcuts

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