vec

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: May 25, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package vec implements vector operations acting on slices.

Operations usually take two forms: for example,

  • Add(v0, v1) adds v0, v1, allocates a new vector to store the result and returns it.
  • AddAssign(v0, v1, vOut) adds v0, v1 and writes the result to pre-allocated vOut without returning.

Note that in most cases, v0, v1, and vOut can overlap. However, for operations that cannot, InPlace methods are implemented separately.

For performance reasons, most functions in this package don't implement bound checks. If length mismatch happens, it may panic or produce wrong results.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T num.Number](v0, v1 []T) []T

Add returns v0 + v1.

func AddAssign

func AddAssign[T num.Number](v0, v1, vOut []T)

AddAssign computes vOut = v0 + v1.

func BitReverseInPlace

func BitReverseInPlace[T any](v []T)

BitReverseInPlace reorders v into bit-reversal order in-place.

func Cast

func Cast[T1, T2 num.Real](v []T1) []T2

Cast casts vector v of type []T1 to []T2.

func CastAssign

func CastAssign[T1, T2 num.Real](v []T1, vOut []T2)

CastAssign casts v of type []T1 to vOut of type []T2.

func CmplxToFloat4 added in v0.3.0

func CmplxToFloat4(v []complex128) []float64

CmplxToFloat4 converts a complex128 vector to float-4 representation used in fourier polynomials.

Namely, it converts

[(r0, i0), (r1, i1), (r2, i2), (r3, i3), ...]

to

[(r0, r1, r2, r3), (i0, i1, i2, i3), ...]

The length of the input vector should be multiple of 4.

func CmplxToFloat4Assign added in v0.3.0

func CmplxToFloat4Assign(v []complex128, vOut []float64)

CmplxToFloat4Assign converts a complex128 vector to float-4 representation used in fourier polynomials and writes it to vOut.

Namely, it converts

[(r0, i0), (r1, i1), (r2, i2), (r3, i3), ...]

to

[(r0, r1, r2, r3), (i0, i1, i2, i3), ...]

The length of the input vector should be multiple of 4, and the length of vOut should be 2 times of the length of v.

func Copy

func Copy[T any](v []T) []T

Copy returns a copy of v.

func CopyAssign

func CopyAssign[T any](v0, v1 []T)

CopyAssign copies v0 to v1.

func Dot

func Dot[T num.Number](v0, v1 []T) T

Dot returns the dot product of two vectors.

func ElementWiseMul

func ElementWiseMul[T num.Number](v0, v1 []T) []T

ElementWiseMul returns v0 * v1, where * is an elementwise multiplication.

func ElementWiseMulAddAssign

func ElementWiseMulAddAssign[T num.Number](v0, v1, vOut []T)

ElementWiseMulAddAssign computes vOut += v0 * v1, where * is an elementwise multiplication.

func ElementWiseMulAssign

func ElementWiseMulAssign[T num.Number](v0, v1, vOut []T)

ElementWiseMulAssign computes vOut = v0 * v1, where * is an elementwise multiplication.

func ElementWiseMulSubAssign

func ElementWiseMulSubAssign[T num.Number](v0, v1, vOut []T)

ElementWiseMulSubAssign computes vOut -= v0 * v1, where * is an elementwise multiplication.

func Equals

func Equals[T comparable](v0, v1 []T) bool

Equals returns if two vectors are equal.

func Fill

func Fill[T any](v []T, x T)

Fill fills vector with x.

func Float4ToCmplx added in v0.3.0

func Float4ToCmplx(v []float64) []complex128

Float4ToCmplx converts a float-4 complex vector to naturally represented complex128 vector.

Namely, it converts

[(r0, r1, r2, r3), (i0, i1, i2, i3), ...]

to

[(r0, i0), (r1, i1), (r2, i2), (r3, i3), ...]

The length of the input vector should be multiple of 8.

func Float4ToCmplxAssign added in v0.3.0

func Float4ToCmplxAssign(v []float64, vOut []complex128)

Float4ToCmplxAssign converts a float-4 complex vector to naturally represented complex128 vector and writes it to vOut.

Namely, it converts

[(r0, r1, r2, r3), (i0, i1, i2, i3), ...]

to

[(r0, i0), (r1, i1), (r2, i2), (r3, i3), ...]

The length of the input vector should be multiple of 8, and the length of vOut should be half of the length of v.

func Neg

func Neg[T num.Number](v0 []T) []T

Neg returns -v0.

func NegAssign

func NegAssign[T num.Number](v0, vOut []T)

NegAssign computes vOut = -v0.

func Reverse

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

Reverse reverses v.

func ReverseAssign

func ReverseAssign[T any](v, vOut []T)

ReverseAssign reverse v and writes it to vOut.

v and vOut should not overlap. For reversing a slice inplace, use vec.ReverseInPlace.

func ReverseInPlace

func ReverseInPlace[T any](v []T)

ReverseInPlace reverses v in-place.

func Rotate

func Rotate[T any](v []T, l int) []T

Rotate rotates v l times to the right. If l < 0, then it rotates the vector l times to the left. If Abs(l) > len(s), it may panic.

func RotateAssign

func RotateAssign[T any](v []T, l int, vOut []T)

RotateAssign rotates v l times to the right and writes it to vOut. If l < 0, then it rotates the vector l times to the left.

v and vOut should not overlap. For rotating a slice inplace, use vec.RotateInPlace.

func RotateInPlace

func RotateInPlace[T any](v []T, l int)

RotateInPlace rotates v l times to the right in-place. If l < 0, then it rotates the vector l times to the left.

func ScalarMul

func ScalarMul[T num.Number](v0 []T, c T) []T

ScalarMul returns c * v0.

func ScalarMulAddAssign

func ScalarMulAddAssign[T num.Number](v0 []T, c T, vOut []T)

ScalarMulAddAssign computes vOut += c * v0.

func ScalarMulAssign

func ScalarMulAssign[T num.Number](v0 []T, c T, vOut []T)

ScalarMulAssign computes vOut = c * v0.

func ScalarMulSubAssign

func ScalarMulSubAssign[T num.Number](v0 []T, c T, vOut []T)

ScalarMulSubAssign computes vOut -= c * v0.

func Sub

func Sub[T num.Number](v0, v1 []T) []T

Sub returns v0 - v1.

func SubAssign

func SubAssign[T num.Number](v0, v1, vOut []T)

SubAssign computes vOut = v0 - v1.

Types

This section is empty.

Jump to

Keyboard shortcuts

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