num

package
v1.10.4 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: Apache-2.0, MIT Imports: 8 Imported by: 0

README

vek | SIMD Vector Functions

Build Status Go Reference

vek is a collection of SIMD accelerated vector functions for Go.

Most modern CPUs have special SIMD instructions (Single Instruction, Multiple Data) to process data in parallel, but there is currently no way to use them in a pure Go program. vek implements a large number of common vector operations in SIMD accelerated assembly code and wraps them in a simple Go API. vek supports most modern x86 CPUs and falls back to a pure Go implementation on unsupported platforms.

Features

  • Fast, average speedups of 10x for float32 vectors
  • Fallback to pure Go on unsupported platforms
  • Support for float64, float32 and bool vectors
  • Zero allocation variations of each function

Installation

go get -u github.com/viterin/vek

Getting Started

Simple Arithmetic Example

Vectors are represented as plain old floating point slices, there are no special data types in vek. All operations on float64 vectors reside in the vek package. It contains all the basic arithmetic operations:

package main

import (
	"fmt"
	"github.com/viterin/vek"
)

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

	// Multiply a vector by itself element-wise
	y := vek.Mul(x, x)
	fmt.Println(x, y) // [0 1 2 3 4] [0 1 4 9 16]

	// Multiply each element by a number
	y = vek.MulNumber(x, 2)
	fmt.Println(x, y) // [0 1 2 3 4] [0 2 4 6 8]
}
Working With 32-Bit Vectors

The vek32 package contains float32 versions of each operation:

package main

import (
	"fmt"
	"github.com/viterin/vek/vek32"
)

func main() {
	// Add a float32 number to each element
	x := []float32{0, 1, 2, 3, 4}
	y := vek32.AddNumber(x, 2)

	fmt.Println(x, y) // [0 1 2 3 4] [2 3 4 5 6]
}
Comparisons and Selections

Floating point vectors can be compared to other vectors or numbers. The result is a bool vector indicating where the comparison holds true. bool vectors can be used to select matching elements, count matches and more:

package main

import (
	"fmt"
	"github.com/viterin/vek"
)

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

	// []bool indicating where x < y (less than)
	m := vek.Lt(x, y)
	fmt.Println(m)            // [true true true false false false]
	fmt.Println(vek.Count(m)) // 3

	// []bool indicating where x >= 2 (greater than or equal)
	m = vek.GteNumber(x, 2)
	fmt.Println(m)          // [false false true true true true]
	fmt.Println(vek.Any(m)) // true

	// Selection of non-zero elements less than y
	z := vek.Select(x,
		vek.And(
			vek.Lt(x, y),
			vek.NeqNumber(x, 0),
		),
	)
	fmt.Println(z) // [1 2]
}
Creating and Converting Vectors

vek has a number of functions to construct new vectors and convert between vector types efficiently:

package main

import (
	"fmt"
	"github.com/viterin/vek"
	"github.com/viterin/vek/vek32"
)

func main() {
	// Vector with number repeated n times
	x := vek.Repeat(2, 5)
	fmt.Println(x) // [2 2 2 2 2]

	// Vector ranging from a to b (excl.) in steps of 1
	x = vek.Range(-2, 3)
	fmt.Println(x) // [-2 -1 0 1 2]

	// Conversion from float64 to int32
	xi32 := vek.ToInt32(x)
	fmt.Println(xi32) // [-2 -1 0 1 2]

	// Conversion from int32 to float32
	x32 := vek32.FromInt32(xi32)
	fmt.Println(x32) // [-2 -1 0 1 2]
}
Avoiding Allocations

By default, functions allocate a new array to store the result. Append _Inplace to a function to do the operation inplace, overriding the data of the first argument slice with the result. Append _Into to write the result into a target slice.

package main

import (
	"fmt"
	"github.com/viterin/vek"
)

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

	y := make([]float64, len(x))
	vek.AddNumber_Into(y, x, 2)

	fmt.Println(x, y) // [2 3 4 5 6] [4 5 6 7 8]
}
SIMD Acceleration

SIMD Acceleration is enabled by default on supported platforms, which is any x86/amd64 CPU with the AVX2 and FMA extensions. Use vek.Info() to see if hardware acceleration is enabled. Turn it off or on with vek.SetAcceleration(). Acceleration is currently disabled by default on mac as I have no machine to test it on.

package main

import (
	"fmt"
	"github.com/viterin/vek"
)

func main() {
	fmt.Printf("%+v", vek.Info())
	// {CPUArchitecture:amd64 CPUFeatures:[AVX2 FMA ..] Acceleration:true}
}

API

description
Arithmetic
vek.Add(x, y) element-wise addition
vek.AddNumber(x, a) add number to each element
vek.Sub(x, y) element-wise subtraction
vek.SubNumber(x, a) subtract number from each element
vek.Mul(x, y) element-wise multiplication
vek.MulNumber(x, a) multiply each element by number
vek.Div(x, y) element-wise division
vek.DivNumber(x, a) divide each element by number
vek.Abs(x) absolute values
vek.Neg(x) additive inverses
vek.Inv(x) multiplicative inverses
Aggregates
vek.Sum(x) sum of elements
vek.CumSum(x) cumulative sum
vek.Prod(x) product of elements
vek.CumProd(x) cumulative product
vek.Mean(x) mean
vek.Median(x) median
vek.Quantile(x, q) q-th quantile, 0 <= q <= 1
Distance
vek.Dot(x, y) dot product
vek.Norm(x) euclidean norm (length)
vek.Distance(x, y) euclidean distance
vek.ManhattanNorm(x) sum of absolute values
vek.ManhattanDistance(x, y) sum of absolute differences
vek.CosineSimilarity(x, y) cosine similarity
Matrices
vek.MatMul(x, y, n) multiply m-by-n and n-by-p matrix (row-major)
vek.Mat4Mul(x, y) specialization for 4 by 4 matrices
Special
vek.Sqrt(x) square root of each element
vek.Pow(x, y) element-wise power
vek.Round(x), Floor(x), Ceil(x) round to nearest, lesser or greater integer
Special (32-bit only)
vek32.Sin(x) sine of each element
vek32.Cos(x) cosine of each element
vek32.Exp(x) exponential function
vek32.Log(x), Log2(x), Log10(x) natural, base 2 and base 10 logarithms
Comparison
vek.Min(x) minimum value
vek.ArgMin(x) first index of the minimum value
vek.Minimum(x, y) element-wise minimum values
vek.MinimumNumber(x, a) minimum of each element and number
vek.Max(x) maximum value
vek.ArgMax(x) first index of the maximum value
vek.Maximum(x, y) element-wise maximum values
vek.MaximumNumber(x, a) maximum of each element and number
vek.Find(x, a) first index of number, -1 if not found
vek.Lt(x, y) element-wise less than
vek.LtNumber(x, a) less than number
vek.Lte(x, y) element-wise less than or equal
vek.LteNumber(x, a) less than or equal to number
vek.Gt(x, y) element-wise greater than
vek.GtNumber(x, a) greater than number
vek.Gte(x, y) element-wise greater than or equal
vek.GteNumber(x, a) greater than or equal to number
vek.Eq(x, y) element-wise equality
vek.EqNumber(x, a) equal to number
vek.Neq(x, y) element-wise non-equality
vek.NeqNumber(x, a) not equal to number
Boolean
vek.Not(x) element-wise not
vek.And(x, y) element-wise and
vek.Or(x, y) element-wise or
vek.Xor(x, y) element-wise exclusive or
vek.Select(x, y) select elements using boolean vector
vek.All(x) all bools are true
vek.Any(x) at least one bool is true
vek.None(x) none of the bools are true
vek.Count(x) number of true bools
Construction
vek.Zeros(n) vector of zeros
vek.Ones(n) vector of ones
vek.Repeat(a, n) vector with number repeated
vek.Range(a, b) vector from a to b (excl.) in steps of 1
vek.Gather(x, idx) select elements at given indices
vek.Scatter(x, idx, size) create vector with indices set to values
vek.FromBool(x), FromInt64, .. convert slice to floats
vek.ToBool(x), ToInt64, .. convert floats to other type
API Variations

vek32.xxx( .. )

The vek32 package contains identical functions for float32 vectors, e.g. vek32.Add(x, y).

vek.xxx_Inplace( .. )

Append _Inplace to the function name to mutate the argument vector inplace, e.g. vek.Add_Inplace(x, y). The first argument is the destination. It should not overlap other argument slices.

vek.xxx_Into( dst, .. )

Append _Into to the function name to write the result into a target slice, e.g. vek.Add_Into(dst, x, y). The destination should have sufficient capacity to hold the result, its length can be anything. It should not overlap other argument slices. The return value is the destination slice resized to the length of the result.

Benchmarks

Comparison of SIMD accelerated functions to the pure Go fallback version for different size slices. Times are in nanoseconds. Functions are inplace.

go test -benchmem -timeout 0 -run=^# -bench=. ./internal/...

1k, Go 1k, SIMD 100k, Go 100k, SIMD speedup
vek.Add 484 192 57,544 26,431 2x
vek32.Add 610 116 84,870 13,164 6x
vek.Mul 499 186 58,154 26,955 2x
vek32.Mul 607 126 83,486 13,056 6x
vek.Abs 794 123 120,018 19,680 6x
vek32.Abs 736 82 113,446 7,990 14x
vek.Sum 633 39 64,824 6,859 9x
vek32.Sum 631 20 65,007 3,191 20x
vek.Quantile 3,375 3,075 860,382 816,831 1x
vek32.Quantile 3,367 3,040 751,790 698,111 1x
vek.Round 1,485 161 250,316 21,622 11x
vek32.Round 1,812 102 250,035 9,722 25x
vek.Sqrt 1,900 614 326,998 85,986 4x
vek32.Sqrt 1,704 148 247,944 15,571 15x
vek.Pow 39,833 6,137 4,155,465 776,556 5x
vek32.Pow 30,386 2,091 4,070,793 292,980 14x
vek32.Exp 7,177 375 1,120,300 49,694 22x
vek32.Log 4,663 453 1,017,240 65,042 16x
vek.Max 734 62 43,412 7,568 6x
vek32.Max 731 27 44,349 3,484 13x
vek.Maximum 1,000 517 542,944 66,423 8x
vek32.Maximum 873 499 556,103 66,786 8x
vek.Find 294 77 21,989 7,256 3x
vek32.Find 223 35 21,813 3,010 7x
vek.Lt 543 195 64,136 23,548 3x
vek32.Lt 539 130 62,449 13,188 5x
vek.And 1,172 60 373,077 2,683 139x
vek.All 237 11 21,696 738 29x
vek.Range 647 59 65,403 7,889 8x
vek32.Range 633 32 65,155 3,252 20x
vek.FromInt32 335 56 33,410 11,428 3x
vek32.FromInt32 439 29 44,372 7,423 6x
m=1k,n=1k,p=1, Go m=1k,n=1k,p=1, SIMD p=1k, Go p=1k, SIMD speedup
vek.MatMul 258,418 38,835 152,726,512 20,823,962 7x
vek32.MatMul 256,453 28,403 147,474,083 10,479,834 14x
m=4,n=4,p=4, Go m=4,n=4,p=4, SIMD
vek.Mat4Mul 26 5 5x
vek32.Mat4Mul 26 5 5x

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(x []float64) []float64

Abs returns the absolute value of each slice element.

func Abs_Inplace

func Abs_Inplace(x []float64)

Abs_Inplace computes the absolute value of each slice element, inplace.

func Abs_Into

func Abs_Into(dst, x []float64) []float64

Abs_Into computes the absolute value of each slice element and stores the result in the destination slice.

func Add

func Add(x, y []float64) []float64

Add returns the result of adding two slices element-wise.

func AddNumber

func AddNumber(x []float64, a float64) []float64

AddNumber returns the result of adding a number to each slice element.

func AddNumber_Inplace

func AddNumber_Inplace(x []float64, a float64)

AddNumber_Inplace adds a number to each slice element, inplace.

func AddNumber_Into

func AddNumber_Into(dst, x []float64, a float64) []float64

AddNumber_Into adds a number to each slice element and stores the result in the destination slice.

func Add_Inplace

func Add_Inplace(x, y []float64)

Add_Inplace adds a slice element-wise to the first slice, inplace.

func Add_Into

func Add_Into(dst, x, y []float64) []float64

Add_Into adds two slices element-wise and stores the result in the destination slice.

func All

func All(x []bool) bool

All returns whether all boolean values in the slice are true.

func And

func And(x, y []bool) []bool

And returns the element-wise logical "and" operation between two slices.

func And_Inplace

func And_Inplace(x, y []bool)

And_Inplace computes the element-wise logical "and" operation between two slices, inplace.

func And_Into

func And_Into(dst, x, y []bool) []bool

And_Into stores the element-wise logical "and" operation between two slices in the destination slice.

func Any

func Any(x []bool) bool

Any returns whether at least one boolean value in the slice is true.

func ArgMax

func ArgMax(x []float64) int

ArgMax returns the (first) index of the maximum value of a slice.

func ArgMin

func ArgMin(x []float64) int

ArgMin returns the (first) index of the minimum value of a slice.

func Ceil

func Ceil(x []float64) []float64

Ceil returns the result of rounding each slice element to the nearest greater integer value.

func Ceil_Inplace

func Ceil_Inplace(x []float64)

Ceil_Inplace rounds each slice element to the nearest greater integer value, inplace.

func Ceil_Into

func Ceil_Into(dst, x []float64) []float64

Ceil_Into rounds each slice element to the nearest greater integer value and stores the result in the destination slice.

func ChangeRate added in v1.6.1

func ChangeRate[B Number, C Number](baseValue B, currentValue C) (changeRate float64)

ChangeRate 增长率

func CosineSimilarity

func CosineSimilarity(x, y []float64) float64

CosineSimilarity returns the cosine similarity of two vectors.

func Count

func Count(x []bool) int

Count returns the number of boolean values that are true.

func CumProd

func CumProd(x []float64) []float64

CumProd returns the cumulative product of a slice. The element at index i equals the product of x[:i+1].

func CumProd_Inplace

func CumProd_Inplace(x []float64)

CumProd_Inplace computes the cumulative product of a slice, inplace. The new element at index i equals the product of x[:i+1].

func CumProd_Into

func CumProd_Into(dst, x []float64) []float64

CumProd_Into computes the cumulative product of a slice and stores the result in the destination slice. The element at index i equals the product of x[:i+1].

func CumSum

func CumSum(x []float64) []float64

CumSum returns the cumulative sum of a slice. The element at index i equals the sum of x[:i+1].

func CumSum_Inplace

func CumSum_Inplace(x []float64)

CumSum_Inplace computes the cumulative sum of a slice, inplace. The new element at index i equals the sum of x[:i+1].

func CumSum_Into

func CumSum_Into(dst, x []float64) []float64

CumSum_Into computes the cumulative sum of a slice and stores the result in the destination slice. The element at index i equals the sum of x[:i+1].

func Decimal added in v1.6.1

func Decimal(value float64, digits ...int) float64

Decimal 保留小数点四舍五入

func Distance

func Distance(x, y []float64) float64

Distance returns the Euclidean distance between two vectors.

func Div

func Div(x, y []float64) []float64

Div returns the result of dividing two slices element-wise.

func DivNumber

func DivNumber(x []float64, a float64) []float64

DivNumber returns the result of dividing each slice element by a number.

func DivNumber_Inplace

func DivNumber_Inplace(x []float64, a float64)

DivNumber_Inplace divides each slice element by a number, inplace.

func DivNumber_Into

func DivNumber_Into(dst, x []float64, a float64) []float64

DivNumber_Into divides each slice element by a number and stores the result in the destination slice.

func Div_Inplace

func Div_Inplace(x, y []float64)

Div_Inplace divides the first slice element-wise by the second, inplace.

func Div_Into

func Div_Into(dst, x, y []float64) []float64

Div_Into divides two slices element-wise and stores the result in the destination slice.

func Dot

func Dot(x, y []float64) float64

Dot returns the dot product of two vectors.

func Eq

func Eq(x, y []float64) []bool

Eq returns an element-wise equality comparison between two slices.

func EqNumber

func EqNumber(x []float64, a float64) []bool

EqNumber returns an element-wise equality comparison between each slice element and a number.

func EqNumber_Into

func EqNumber_Into(dst []bool, x []float64, a float64) []bool

EqNumber_Into stores an element-wise equality comparison between each slice element and a number in the destination slice.

func Eq_Into

func Eq_Into(dst []bool, x, y []float64) []bool

Eq_Into stores an element-wise equality comparison between two slices in the destination slice.

func Find

func Find(x []float64, a float64) int

Find returns the index of the first slice element equal to the given value, or -1 if not found.

func Floor

func Floor(x []float64) []float64

Floor returns the result of rounding each slice element to the nearest lesser integer value.

func Floor_Inplace

func Floor_Inplace(x []float64)

Floor_Inplace rounds each slice element to the nearest lesser integer value, inplace.

func Floor_Into

func Floor_Into(dst, x []float64) []float64

Floor_Into rounds each slice element to the nearest lesser integer value and stores the result in the destination slice.

func FromBool

func FromBool(x []bool) []float64

FromBool creates a slice of floats from a slice of bools by converting all false values to 0 and all true values to 1.

func FromBool_Into

func FromBool_Into(dst []float64, x []bool) []float64

FromBool_Into creates a slice of floats from a slice of bools by converting all false values to 0 and all true values to 1. The result is stored in the destination slice.

func FromFloat32

func FromFloat32(x []float32) []float64

FromFloat32 creates a slice of floats from a slice of 32-bit floats. Standard conversion rules apply.

func FromFloat32_Into

func FromFloat32_Into(dst []float64, x []float32) []float64

FromFloat32_Into creates a slice of floats from a slice of 32-bit floats. Standard conversion rules apply. The result is stored in the destination slice.

func FromInt32

func FromInt32(x []int32) []float64

FromInt32 creates a slice of floats from a slice of 32-bit ints. Standard conversion rules apply.

func FromInt32_Into

func FromInt32_Into(dst []float64, x []int32) []float64

FromInt32_Into creates a slice of floats from a slice of 32-bit ints. Standard conversion rules apply. The result is stored in the destination slice.

func FromInt64

func FromInt64(x []int64) []float64

FromInt64 creates a slice of floats from a slice of 64-bit ints. Standard conversion rules apply.

func FromInt64_Into

func FromInt64_Into(dst []float64, x []int64) []float64

FromInt64_Into creates a slice of floats from a slice of 64-bit ints. Standard conversion rules apply. The result is stored in the destination slice.

func Gather

func Gather(x []float64, idx []int) []float64

Gather returns a new slice containing just the elements at the given indices.

func Gather_Into

func Gather_Into(dst, x []float64, idx []int) []float64

Gather_Into stores the slice elements at the given indices in the destination slice.

func Gt

func Gt(x, y []float64) []bool

Gt returns an element-wise "greater than" comparison between two slices.

func GtNumber

func GtNumber(x []float64, a float64) []bool

GtNumber returns an element-wise "greater than" comparison between each slice element and a number.

func GtNumber_Into

func GtNumber_Into(dst []bool, x []float64, a float64) []bool

GtNumber_Into stores an element-wise "greater than" comparison between each slice element and a number in the destination slice.

func Gt_Into

func Gt_Into(dst []bool, x, y []float64) []bool

Gt_Into stores an element-wise "greater than" comparison between two slices in the destination slice.

func Gte

func Gte(x, y []float64) []bool

Gte returns an element-wise "greater than or equal" comparison between two slices.

func GteNumber

func GteNumber(x []float64, a float64) []bool

GteNumber returns an element-wise "greater than or equal" comparison between each slice element and a number.

func GteNumber_Into

func GteNumber_Into(dst []bool, x []float64, a float64) []bool

GteNumber_Into stores an element-wise "greater than or equal" comparison between each slice element and a number in the destination slice.

func Gte_Into

func Gte_Into(dst []bool, x, y []float64) []bool

Gte_Into stores an element-wise "greater than or equal" comparison between two slices in the destination slice.

func Inv

func Inv(x []float64) []float64

Inv returns the multiplicative inverse of each slice element.

func Inv_Inplace

func Inv_Inplace(x []float64)

Inv_Inplace computes the multiplicative inverse of each slice element, inplace.

func Inv_Into

func Inv_Into(dst, x []float64) []float64

Inv_Into computes the multiplicative inverse of each slice element and stores the result in the destination slice.

func IsNaN added in v1.9.1

func IsNaN(f float64) bool

func Lt

func Lt(x, y []float64) []bool

Lt returns an element-wise "less than" comparison between two slices.

func LtNumber

func LtNumber(x []float64, a float64) []bool

LtNumber returns an element-wise "less than" comparison between each slice element and a number.

func LtNumber_Into

func LtNumber_Into(dst []bool, x []float64, a float64) []bool

LtNumber_Into stores an element-wise "less than" comparison between each slice element and a number in the destination slice.

func Lt_Into

func Lt_Into(dst []bool, x, y []float64) []bool

Lt_Into stores an element-wise "less than" comparison between two slices in the destination slice.

func Lte

func Lte(x, y []float64) []bool

Lte returns an element-wise "less than or equal" comparison between two slices.

func LteNumber

func LteNumber(x []float64, a float64) []bool

LteNumber returns an element-wise "less than or equal" comparison between each slice element and a number.

func LteNumber_Into

func LteNumber_Into(dst []bool, x []float64, a float64) []bool

LteNumber_Into stores an element-wise "less than or equal" comparison between each slice element and a number in the destination slice.

func Lte_Into

func Lte_Into(dst []bool, x, y []float64) []bool

Lte_Into stores an element-wise "less than or equal" comparison between two slices in the destination slice.

func ManhattanDistance

func ManhattanDistance(x, y []float64) float64

ManhattanDistance returns the sum of element-wise absolute differences between two slices.

func ManhattanNorm

func ManhattanNorm(x []float64) float64

ManhattanNorm returns the sum of absolute values of the slice elements.

func Mat4Mul

func Mat4Mul(x, y []float64) []float64

Mat4Mul multiplies two 4-by-4 matrices and returns the resulting 4-by-4 matrix. The matrices should be in row-major order. To multiply a matrix and a vector batch them into groups of 4.

func Mat4Mul_Into

func Mat4Mul_Into(dst, x, y []float64) []float64

Mat4Mul_Into multiplies two 4-by-4 matrices and stores the resulting 4-by-4 matrix in the destination slice. The matrices should be in row-major order. To multiply a matrix and a vector batch them into groups of 4.

func MatMul

func MatMul(x, y []float64, n int) []float64

MatMul multiplies an m-by-n and n-by-p matrix and returns the resulting m-by-p matrix. The matrices should be in row-major order. To multiply a matrix and a vector pass an n-by-1 matrix.

func MatMul_Into

func MatMul_Into(dst, x, y []float64, n int) []float64

MatMul_Into multiplies an m-by-n and n-by-p matrix and stores the resulting m-by-p matrix in the destination slice. The matrices should be in row-major order. To multiply a matrix and a vector pass an n-by-1 matrix.

func Max

func Max(x []float64) float64

Max returns the maximum value of a slice.

func Maximum

func Maximum(x, y []float64) []float64

Maximum returns the element-wise maximum values between two slices.

func MaximumNumber

func MaximumNumber(x []float64, a float64) []float64

MaximumNumber returns the maximum of a number and each slice element.

func MaximumNumber_Inplace

func MaximumNumber_Inplace(x []float64, a float64)

MaximumNumber_Inplace compares a number to each slice element and replaces the values in the slice with the maximum value.

func MaximumNumber_Into

func MaximumNumber_Into(dst, x []float64, a float64) []float64

MaximumNumber_Into compares a number to each slice element and stores the maximum values in the destination slice.

func Maximum_Inplace

func Maximum_Inplace(x, y []float64)

Maximum_Inplace compares two slices element-wise and replaces the values in the first slice with the maximum value.

func Maximum_Into

func Maximum_Into(dst, x, y []float64) []float64

Maximum_Into compares two slices element-wise and stores the maximum values in the destination slice.

func Mean

func Mean(x []float64) float64

Mean returns the arithmetic average of the slice elements.

func Median

func Median(x []float64) float64

Median returns the median value of the slice elements.

func Min

func Min(x []float64) float64

Min returns the minimum value of a slice.

func Minimum

func Minimum(x, y []float64) []float64

Minimum returns the element-wise minimum values between two slices.

func MinimumNumber

func MinimumNumber(x []float64, a float64) []float64

MinimumNumber returns the minimum of a number and each slice element.

func MinimumNumber_Inplace

func MinimumNumber_Inplace(x []float64, a float64)

MinimumNumber_Inplace compares a number to each slice element and replaces the values in the slice with the minimum value.

func MinimumNumber_Into

func MinimumNumber_Into(dst, x []float64, a float64) []float64

MinimumNumber_Into compares a number to each slice element and stores the minimum values in the destination slice.

func Minimum_Inplace

func Minimum_Inplace(x, y []float64)

Minimum_Inplace compares two slices element-wise and replaces the values in the first slice with the minimum value.

func Minimum_Into

func Minimum_Into(dst, x, y []float64) []float64

Minimum_Into compares two slices element-wise and stores the minimum values in the destination slice.

func Mul

func Mul(x, y []float64) []float64

Mul returns the result of multiplying two slices element-wise.

func MulNumber

func MulNumber(x []float64, a float64) []float64

MulNumber returns the result of multiplying each slice element by a number.

func MulNumber_Inplace

func MulNumber_Inplace(x []float64, a float64)

MulNumber_Inplace multiplies each slice element by a number, inplace.

func MulNumber_Into

func MulNumber_Into(dst, x []float64, a float64) []float64

MulNumber_Into multiplies each slice element by a number and stores the result in the destination slice.

func Mul_Inplace

func Mul_Inplace(x, y []float64)

Mul_Inplace multiplies the first slice element-wise by the second, inplace.

func Mul_Into

func Mul_Into(dst, x, y []float64) []float64

Mul_Into multiplies two slices element-wise and stores the result in the destination slice.

func Neg

func Neg(x []float64) []float64

Neg returns the additive inverse of each slice element.

func Neg_Inplace

func Neg_Inplace(x []float64)

Neg_Inplace computes the additive inverse of each slice element, inplace.

func Neg_Into

func Neg_Into(dst, x []float64) []float64

Neg_Into computes the additive inverse of each slice element and stores the result in the destination slice.

func Neq

func Neq(x, y []float64) []bool

Neq returns an element-wise "not equal" comparison between two slices.

func NeqNumber

func NeqNumber(x []float64, a float64) []bool

NeqNumber returns an element-wise "not equal" comparison between each slice element and a number.

func NeqNumber_Into

func NeqNumber_Into(dst []bool, x []float64, a float64) []bool

NeqNumber_Into stores an element-wise "not equal" comparison between each slice element and a number in the destination slice.

func Neq_Into

func Neq_Into(dst []bool, x, y []float64) []bool

Neq_Into stores an element-wise "not equal" comparison between two slices in the destination slice.

func NetChangeRate added in v1.6.1

func NetChangeRate[B Number, C Number](baseValue B, currentValue C) (changeRate float64)

NetChangeRate 净增长率, 去掉百分比

func None

func None(x []bool) bool

None returns whether none of the boolean values in the slice are true.

func Norm

func Norm(x []float64) float64

Norm returns the Euclidean norm of a vector, i.e. its length.

func Not

func Not(x []bool) []bool

Not returns the logical negation of each slice element.

func Not_Inplace

func Not_Inplace(x []bool)

Not_Inplace computes the logical negation of each slice element, inplace.

func Not_Into

func Not_Into(dst, x []bool) []bool

Not_Into stores the logical negation of each slice element in the destination slice.

func Ones

func Ones(n int) []float64

Ones returns a new slice of length n filled with ones.

func Ones_Into

func Ones_Into(dst []float64, n int) []float64

Ones_Into sets the first n elements in the destination slice to one.

func Or

func Or(x, y []bool) []bool

Or returns the element-wise logical "or" operation between two slices.

func Or_Inplace

func Or_Inplace(x, y []bool)

Or_Inplace computes the element-wise logical "or" operation between two slices, inplace.

func Or_Into

func Or_Into(dst, x, y []bool) []bool

Or_Into stores the element-wise logical "or" operation between two slices in the destination slice.

func Pow

func Pow(x, y []float64) []float64

Pow returns the elements in the first slice raised to the power in the second.

func Pow_Inplace

func Pow_Inplace(x, y []float64)

Pow_Inplace raises the elements in the first slice to the power in the second, inplace.

func Pow_Into

func Pow_Into(dst, x, y []float64) []float64

Pow_Into raises the elements in the first slice to the power in the second and stores the result in the destination slice.

func Prod

func Prod(x []float64) float64

Prod returns the product of all elements in a slice.

func Quantile

func Quantile(x []float64, q float64) float64

Quantile returns the q-th quantile of the slice elements. The value of q should be between 0 and 1 (inclusive).

func Range

func Range(a, b float64) []float64

Range returns a new slice with values incrementing from a to b (excl.) in steps of 1.

func Range_Into

func Range_Into(dst []float64, a, b float64) []float64

Range_Into writes values incrementing from a to b (excl.) in steps of 1 to the destination slice.

func Repeat

func Repeat(a float64, n int) []float64

Repeat returns a new slice of length n filled with the given value.

func Repeat_Into

func Repeat_Into(dst []float64, a float64, n int) []float64

Repeat_Into sets the first n elements in the destination slice to the given value.

func Round

func Round(x []float64) []float64

Round returns the result of rounding each slice element to the nearest integer value.

func Round_Inplace

func Round_Inplace(x []float64)

Round_Inplace rounds each slice element to the nearest integer value, inplace.

func Round_Into

func Round_Into(dst, x []float64) []float64

Round_Into rounds each slice element to the nearest integer value and stores the result in the destination slice.

func Scatter

func Scatter(x []float64, idx []int, size int) []float64

Scatter returns a slice of size elements where position idx[i] is set to x[i], for all i < len(x) = len(idx). The other elements are set to zero.

func Scatter_Into

func Scatter_Into(dst, x []float64, idx []int) []float64

Scatter_Into sets positions idx[i] to x[i] in the destination slice, for all i < len(x) = len(idx). The other elements are not modified.

func Select

func Select(x []float64, y []bool) []float64

Select returns the slice elements for which the corresponding boolean values are true.

func Select_Into

func Select_Into(dst, x []float64, y []bool) []float64

Select_Into stores the slice elements for which the corresponding boolean values are true in the destination slice. This function grows the destination slice if the selection yields more values than it has capacity for.

func SetAcceleration

func SetAcceleration(enabled bool)

SetAcceleration toggles simd acceleration. Not thread safe.

func Sqrt

func Sqrt(x []float64) []float64

Sqrt returns the square root of each slice element.

func Sqrt_Inplace

func Sqrt_Inplace(x []float64)

Sqrt_Inplace computes the square root of each slice element, inplace.

func Sqrt_Into

func Sqrt_Into(dst, x []float64) []float64

Sqrt_Into computes the square root of each slice element and stores the result in the destination slice.

func Sub

func Sub(x, y []float64) []float64

Sub returns the result of subtracting two slices element-wise.

func SubNumber

func SubNumber(x []float64, a float64) []float64

SubNumber returns the result of subtracting a number from each slice element.

func SubNumber_Inplace

func SubNumber_Inplace(x []float64, a float64)

SubNumber_Inplace subtracts a number from each slice element, inplace.

func SubNumber_Into

func SubNumber_Into(dst, x []float64, a float64) []float64

SubNumber_Into subtracts a number from each slice element and stores the result in the destination slice.

func Sub_Inplace

func Sub_Inplace(x, y []float64)

Sub_Inplace subtracts a slice element-wise from the first slice, inplace.

func Sub_Into

func Sub_Into(dst, x, y []float64) []float64

Sub_Into subtracts two slices element-wise and stores the result in the destination slice.

func Sum

func Sum(x []float64) float64

Sum returns the sum of all elements in a slice.

func ToBool

func ToBool(x []float64) []bool

ToBool converts a slice of floats to a slice of bools by setting all zero values to true and all non-zero values to false.

func ToBool_Into

func ToBool_Into(dst []bool, x []float64) []bool

ToBool_Into converts a slice of floats to a slice of bools by setting all zero values to true and all non-zero values to false. The result is stored in the destination slice.

func ToFloat32

func ToFloat32(x []float64) []float32

ToFloat32 converts a slice of floats to a slice of 32-bit floats. Standard conversion rules apply.

func ToFloat32_Into

func ToFloat32_Into(dst []float32, x []float64) []float32

ToFloat32_Into converts a slice of floats to a slice of 32-bit floats. Standard conversion rules apply. The result is stored in the destination slice.

func ToInt32

func ToInt32(x []float64) []int32

ToInt32 converts a slice of floats to a slice of 32-bit ints. Standard conversion rules apply.

func ToInt32_Into

func ToInt32_Into(dst []int32, x []float64) []int32

ToInt32_Into converts a slice of floats to a slice of 32-bit ints. Standard conversion rules apply. The result is stored in the destination slice.

func ToInt64

func ToInt64(x []float64) []int64

ToInt64 converts a slice of floats to a slice of 64-bit ints. Standard conversion rules apply.

func ToInt64_Into

func ToInt64_Into(dst []int64, x []float64) []int64

ToInt64_Into converts a slice of floats to a slice of 64-bit ints. Standard conversion rules apply. The result is stored in the destination slice.

func Xor

func Xor(x, y []bool) []bool

Xor returns the element-wise "exclusive or" operation between two slices.

func Xor_Inplace

func Xor_Inplace(x, y []bool)

Xor_Inplace computes the element-wise "exclusive or" operation between two slices, inplace.

func Xor_Into

func Xor_Into(dst, x, y []bool) []bool

Xor_Into stores the element-wise "exclusive or" operation between two slices in the destination slice.

func Zeros

func Zeros(n int) []float64

Zeros returns a new slice of length n filled with zeros.

func Zeros_Into

func Zeros_Into(dst []float64, n int) []float64

Zeros_Into sets the first n elements in the destination slice to zero.

Types

type BaseType added in v1.6.1

type BaseType interface {
	Integer | Float | ~string | ~bool
}

BaseType 基础类型

type BigFloat added in v1.6.1

type BigFloat = big.Float // 预留将来可能扩展float

type Complex added in v1.6.1

type Complex interface {
	~complex64 | ~complex128
}

Complex is a constraint that permits any complex numeric type. If future releases of Go add new predeclared complex numeric types, this constraint will be modified to include them.

type Float added in v1.6.1

type Float interface {
	~float32 | ~float64
}

Float is a constraint that permits any floating-point type. If future releases of Go add new predeclared floating-point types, this constraint will be modified to include them.

type GenericType added in v1.6.1

type GenericType interface {
	~bool | ~int32 | ~int64 | ~int | ~float32 | ~float64 | ~string
}

GenericType Series支持的所有类型 Deprecated: 不推荐使用

type Integer added in v1.6.1

type Integer interface {
	Signed | Unsigned
}

Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.

type MoveType deprecated added in v1.6.1

type MoveType interface {
	StatType | ~bool | ~string
}

Deprecated: 已弃用

type Number added in v1.6.1

type Number interface {
	Integer | Float
}

Number int和uint的长度取决于CPU是多少位

type Number16 deprecated added in v1.6.1

type Number16 interface {
	~int16 | ~uint16
}

Deprecated: 不推荐使用

type Number32 deprecated added in v1.6.1

type Number32 interface {
	~int32 | ~uint32 | float32
}

Deprecated: 不推荐使用

type Number64 deprecated added in v1.6.1

type Number64 interface {
	~int64 | ~uint64 | float64 | int | uint
}

Deprecated: 不推荐使用

type Number8 deprecated added in v1.6.1

type Number8 interface {
	~int8 | ~uint8
}

Deprecated: 不推荐使用

type NumberOfCPUBitsRelated added in v1.6.1

type NumberOfCPUBitsRelated interface {
	~int | ~uint | ~uintptr
}

NumberOfCPUBitsRelated The number of CPU bits is related Deprecated: 不推荐使用

type Ordered added in v1.6.1

type Ordered interface {
	Integer | Float | ~string
}

Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.

type Signed added in v1.6.1

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed is a constraint that permits any signed integer type. If future releases of Go add new predeclared signed integer types, this constraint will be modified to include them.

type StatType added in v1.6.1

type StatType interface {
	~int32 | ~int64 | ~float32 | ~float64
}

StatType 可以统计的类型 Deprecated: 不推荐使用

type SystemInfo

type SystemInfo struct {
	CPUArchitecture string
	CPUFeatures     []string
	Acceleration    bool
}

SystemInfo contains information about the current operating environment.

func Info

func Info() SystemInfo

Info returns information about the current operating environment.

type Unsigned added in v1.6.1

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned is a constraint that permits any unsigned integer type. If future releases of Go add new predeclared unsigned integer types, this constraint will be modified to include them.

Directories

Path Synopsis
internal
Package math32 provides basic constants and mathematical functions for float32 types.
Package math32 provides basic constants and mathematical functions for float32 types.

Jump to

Keyboard shortcuts

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