cmplxs

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: BSD-3-Clause Imports: 5 Imported by: 3

README

Gonum cmplxs

go.dev reference GoDoc

Package cmplxs provides a set of helper routines for dealing with slices of complex128. The functions avoid allocations to allow for use within tight loops without garbage collection overhead.

Documentation

Overview

Package cmplxs provides a set of helper routines for dealing with slices of complex128. The functions avoid allocations to allow for use within tight loops without garbage collection overhead.

The convention used is that when a slice is being modified in place, it has the name dst.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(dst []float64, s []complex128)

Abs calculates the absolute values of the elements of s, and stores them in dst. It panics if the argument lengths do not match.

func Add

func Add(dst, s []complex128)

Add adds, element-wise, the elements of s and dst, and stores the result in dst. It panics if the argument lengths do not match.

Example (Newslice)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/cmplxs"
)

func main() {
	// If one wants to store the result in a
	// new container, just make a new slice
	s1 := []complex128{1 + 1i, 2 + 2i, 3 + 3i, 4 + 4i}
	s2 := []complex128{5 + 7i, 6 + 7i, 7 + 7i, 8 + 8i}
	s3 := []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}
	dst := make([]complex128, len(s1))

	cmplxs.AddTo(dst, s1, s2)
	cmplxs.Add(dst, s3)

	fmt.Println("dst =", dst)
	fmt.Println("s1 =", s1)
	fmt.Println("s2 =", s2)
	fmt.Println("s3 =", s3)

}
Output:

dst = [(7+10i) (9+11i) (11+12i) (13+14i)]
s1 = [(1+1i) (2+2i) (3+3i) (4+4i)]
s2 = [(5+7i) (6+7i) (7+7i) (8+8i)]
s3 = [(1+2i) (1+2i) (1+2i) (1+2i)]
Example (Simple)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/cmplxs"
)

func main() {
	// Adding three slices together. Note that
	// the result is stored in the first slice
	s1 := []complex128{1 + 1i, 2 + 2i, 3 + 3i, 4 + 4i}
	s2 := []complex128{5 + 7i, 6 + 7i, 7 + 7i, 8 + 8i}
	s3 := []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}
	cmplxs.Add(s1, s2)
	cmplxs.Add(s1, s3)

	fmt.Println("s1 =", s1)
	fmt.Println("s2 =", s2)
	fmt.Println("s3 =", s3)

}
Output:

s1 = [(7+10i) (9+11i) (11+12i) (13+14i)]
s2 = [(5+7i) (6+7i) (7+7i) (8+8i)]
s3 = [(1+2i) (1+2i) (1+2i) (1+2i)]
Example (Unequallengths)
package main

import (
	"fmt"

	"gonum.org/v1/gonum/cmplxs"
)

func main() {
	// If the lengths of the slices are unknown,
	// use EqualLengths to check
	s1 := []complex128{1 + 1i, 2 + 2i, 3 + 3i}
	s2 := []complex128{5 + 5i, 6 + 6i, 7 + 7i, 8 + 8i}

	eq := cmplxs.EqualLengths(s1, s2)
	if eq {
		cmplxs.Add(s1, s2)
	} else {
		fmt.Println("Unequal lengths")
	}

}
Output:

Unequal lengths

func AddConst

func AddConst(c complex128, dst []complex128)

AddConst adds the scalar c to all of the values in dst.

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/cmplxs"
)

func main() {
	s := []complex128{1 - 1i, -2 - 1i, 3 - 1i, -4 - 1i}
	c := 5 + 1i

	cmplxs.AddConst(c, s)

	fmt.Println("s =", s)

}
Output:

s = [(6+0i) (3+0i) (8+0i) (1+0i)]

func AddScaled

func AddScaled(dst []complex128, alpha complex128, s []complex128)

AddScaled performs dst = dst + alpha * s. It panics if the slice argument lengths do not match.

func AddScaledTo

func AddScaledTo(dst, y []complex128, alpha complex128, s []complex128) []complex128

AddScaledTo performs dst = y + alpha * s, where alpha is a scalar, and dst, y and s are all slices. It panics if the slice argument lengths do not match.

At the return of the function, dst[i] = y[i] + alpha * s[i]

func AddTo

func AddTo(dst, s, t []complex128) []complex128

AddTo adds, element-wise, the elements of s and t and stores the result in dst. It panics if the argument lengths do not match.

func Complex

func Complex(dst []complex128, real, imag []float64) []complex128

Complex fills each of the elements of dst with the complex number constructed from the corresponding elements of real and imag. It panics if the argument lengths do not match.

func Count

func Count(f func(complex128) bool, s []complex128) int

Count applies the function f to every element of s and returns the number of times the function returned true.

func CumProd

func CumProd(dst, s []complex128) []complex128

CumProd finds the cumulative product of elements of s and store it in place into dst so that

dst[i] = s[i] * s[i-1] * s[i-2] * ... * s[0]

It panics if the argument lengths do not match.

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/cmplxs"
)

func main() {
	s := []complex128{1 + 1i, -2 - 2i, 3 + 3i, -4 - 4i}
	dst := make([]complex128, len(s))

	cmplxs.CumProd(dst, s)

	fmt.Println("dst =", dst)
	fmt.Println("s =", s)

}
Output:

dst = [(1+1i) (0-4i) (12-12i) (-96+0i)]
s = [(1+1i) (-2-2i) (3+3i) (-4-4i)]

func CumSum

func CumSum(dst, s []complex128) []complex128

CumSum finds the cumulative sum of elements of s and stores it in place into dst so that

dst[i] = s[i] + s[i-1] + s[i-2] + ... + s[0]

It panics if the argument lengths do not match.

Example
package main

import (
	"fmt"

	"gonum.org/v1/gonum/cmplxs"
)

func main() {
	s := []complex128{1 + 1i, -2 - 2i, 3 + 3i, -4 - 4i}
	dst := make([]complex128, len(s))

	cmplxs.CumSum(dst, s)

	fmt.Println("dst =", dst)
	fmt.Println("s =", s)

}
Output:

dst = [(1+1i) (-1-1i) (2+2i) (-2-2i)]
s = [(1+1i) (-2-2i) (3+3i) (-4-4i)]

func Distance

func Distance(s, t []complex128, L float64) float64

Distance computes the L-norm of s - t. See Norm for special cases. It panics if the slice argument lengths do not match.

func Div

func Div(dst, s []complex128)

Div performs element-wise division dst / s and stores the result in dst. It panics if the argument lengths do not match.

func DivTo

func DivTo(dst, s, t []complex128) []complex128

DivTo performs element-wise division s / t and stores the result in dst. It panics if the argument lengths do not match.

func Dot

func Dot(s1, s2 []complex128) complex128

Dot computes the dot product of s1 and s2, i.e. sum_{i = 1}^N conj(s1[i])*s2[i]. It panics if the argument lengths do not match.

func Equal

func Equal(s1, s2 []complex128) bool

Equal returns true when the slices have equal lengths and all elements are numerically identical.

func EqualApprox

func EqualApprox(s1, s2 []complex128, tol float64) bool

EqualApprox returns true when the slices have equal lengths and all element pairs have an absolute tolerance less than tol or a relative tolerance less than tol.

func EqualFunc

func EqualFunc(s1, s2 []complex128, f func(complex128, complex128) bool) bool

EqualFunc returns true when the slices have the same lengths and the function returns true for all element pairs.

func EqualLengths

func EqualLengths(slices ...[]complex128) bool

EqualLengths returns true when all of the slices have equal length, and false otherwise. It also returns true when there are no input slices.

func Find

func Find(inds []int, f func(complex128) bool, s []complex128, k int) ([]int, error)

Find applies f to every element of s and returns the indices of the first k elements for which the f returns true, or all such elements if k < 0. Find will reslice inds to have 0 length, and will append found indices to inds. If k > 0 and there are fewer than k elements in s satisfying f, all of the found elements will be returned along with an error. At the return of the function, the input inds will be in an undetermined state.

func HasNaN

func HasNaN(s []complex128) bool

HasNaN returns true when the slice s has any values that are NaN and false otherwise.

func Imag

func Imag(dst []float64, src []complex128) []float64

Imag places the imaginary components of src into dst. It panics if the argument lengths do not match.

func LogSpan

func LogSpan(dst []complex128, l, u complex128) []complex128

LogSpan returns a set of n equally spaced points in log space between, l and u where N is equal to len(dst). The first element of the resulting dst will be l and the final element of dst will be u. Panics if len(dst) < 2 Note that this call will return NaNs if either l or u are negative, and will return all zeros if l or u is zero. Also returns the mutated slice dst, so that it can be used in range, like:

for i, x := range LogSpan(dst, l, u) { ... }

func MaxAbs

func MaxAbs(s []complex128) complex128

MaxAbs returns the maximum absolute value in the input slice. It panics if s is zero length.

func MaxAbsIdx

func MaxAbsIdx(s []complex128) int

MaxAbsIdx returns the index of the maximum absolute value in the input slice. If several entries have the maximum absolute value, the first such index is returned. It panics if s is zero length.

func MinAbs

func MinAbs(s []complex128) complex128

MinAbs returns the minimum absolute value in the input slice. It panics if s is zero length.

func MinAbsIdx

func MinAbsIdx(s []complex128) int

MinAbsIdx returns the index of the minimum absolute value in the input slice. If several entries have the minimum absolute value, the first such index is returned. It panics if s is zero length.

func Mul

func Mul(dst, s []complex128)

Mul performs element-wise multiplication between dst and s and stores the result in dst. It panics if the argument lengths do not match.

func MulConj added in v0.11.0

func MulConj(dst, s []complex128)

MulConj performs element-wise multiplication between dst and the conjugate of s and stores the result in dst. It panics if the argument lengths do not match.

func MulConjTo added in v0.11.0

func MulConjTo(dst, s, t []complex128) []complex128

MulConjTo performs element-wise multiplication between s and the conjugate of t and stores the result in dst. It panics if the argument lengths do not match.

func MulTo

func MulTo(dst, s, t []complex128) []complex128

MulTo performs element-wise multiplication between s and t and stores the result in dst. It panics if the argument lengths do not match.

func NearestIdx

func NearestIdx(s []complex128, v complex128) int

NearestIdx returns the index of the element in s whose value is nearest to v. If several such elements exist, the lowest index is returned. It panics if s is zero length.

func Norm

func Norm(s []complex128, L float64) float64

Norm returns the L-norm of the slice S, defined as (sum_{i=1}^N abs(s[i])^L)^{1/L} Special cases: L = math.Inf(1) gives the maximum absolute value. Does not correctly compute the zero norm (use Count).

func Prod

func Prod(s []complex128) complex128

Prod returns the product of the elements of the slice. Returns 1 if len(s) = 0.

func Real

func Real(dst []float64, src []complex128) []float64

Real places the real components of src into dst. It panics if the argument lengths do not match.

func Reverse

func Reverse(s []complex128)

Reverse reverses the order of elements in the slice.

func Same

func Same(s, t []complex128) bool

Same returns true when the input slices have the same length and all elements have the same value with NaN treated as the same.

func Scale

func Scale(c complex128, dst []complex128)

Scale multiplies every element in dst by the scalar c.

func ScaleReal added in v0.11.0

func ScaleReal(f float64, dst []complex128)

ScaleReal multiplies every element in dst by the real scalar f.

func ScaleRealTo added in v0.11.0

func ScaleRealTo(dst []complex128, f float64, s []complex128) []complex128

ScaleRealTo multiplies the elements in s by the real scalar f and stores the result in dst. It panics if the slice argument lengths do not match.

func ScaleTo

func ScaleTo(dst []complex128, c complex128, s []complex128) []complex128

ScaleTo multiplies the elements in s by c and stores the result in dst. It panics if the slice argument lengths do not match.

func Span

func Span(dst []complex128, l, u complex128) []complex128

Span returns a set of N equally spaced points between l and u, where N is equal to the length of the destination. The first element of the destination is l, the final element of the destination is u. It panics if the length of dst is less than 2.

Span also returns the mutated slice dst, so that it can be used in range expressions, like:

for i, x := range Span(dst, l, u) { ... }

func Sub

func Sub(dst, s []complex128)

Sub subtracts, element-wise, the elements of s from dst. It panics if the argument lengths do not match.

func SubTo

func SubTo(dst, s, t []complex128) []complex128

SubTo subtracts, element-wise, the elements of t from s and stores the result in dst. It panics if the argument lengths do not match.

func Sum

func Sum(s []complex128) complex128

Sum returns the sum of the elements of the slice.

Types

This section is empty.

Directories

Path Synopsis
Package cscalar provides a set of helper routines for dealing with complex128 values.
Package cscalar provides a set of helper routines for dealing with complex128 values.

Jump to

Keyboard shortcuts

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