numf32

package
v0.27.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(x float32, y float32) (max float32, min float32)

Compares and returns maximum and minimum of two floats taking NaNs into account.

func Contains

func Contains(slice []float32, s float32) bool

Checks if given float exists in the slice.

func Cumsum

func Cumsum(slice []float32) []float32

Calculates a slice of cumulative sum from given slice.

func Delta

func Delta(slice []float32) []float32

Returns the delta between all consecutive floats. Returned slice length is one item shorter.

func DropNan

func DropNan(slice []float32) []float32

Creates a new slice from given input slice without NaNs

func DropNanInplace

func DropNanInplace(slice *[]float32)

Drops NaNs from a slice in place.

func FillNan

func FillNan(slice []float32, method string, prefill bool, validTime int) []float32

Fills NaN values with a value based on given method:

"previous" // fills the NaNs with previous value
"linear"   // fills the NaNs with linear interpolation

Filling starts from first valid value, thus leaving any preceding NaNs untouched. By setting prefill = true, first valid value is used to replace also the preceding NaNs.

One sample's lifetime can be set by setting validTime value to > 0. Filling is then performed for validTime samples. Using validTime overrides prefill = true -setting

func FillNanInplace

func FillNanInplace(slice *[]float32, method string, prefill bool, validTime int)

Fills NaN values inplace with a value based on given method:

"previous" // fills the NaNs with previous value
"linear"   // fills the NaNs with linear interpolation

Filling starts from first valid value, thus leaving any preceding NaNs untouched. By setting prefill = true, first valid value is used to replace also the preceding NaNs.

One sample's lifetime can be set by setting validTime value to > 0. Filling is then performed for validTime samples. Using validTime overrides prefill = true -setting

func FindIndex

func FindIndex(slice []float32, val float32) (int, bool)

Finds the index of first occurrence of the given value.

func GetIndex

func GetIndex(slice []float32, val float32) int

Finds the index of first occurrence of the given value and returns the index. If not found index = -1.

func Insert

func Insert(slice []float32, idx int, val float32) []float32

Inserts given value to given index into a slice.

func IsEqualSlice

func IsEqualSlice(s1, s2 []float32) bool

Checks if two slices are equal, element-wise. NaN and Inf are also handled.

func IsSameSign

func IsSameSign(x float32, y float32) bool

Checks if two values have same sign. Zero sign is ambiguous and is considered to be both positive and negative.

func IsValid

func IsValid(value float32) bool

Checks whether the given float is a valid number instead of NaN or Inf.

func Max

func Max(a, b float32) float32

Faster Max

func MaxFrom

func MaxFrom(s []float32) (max float32)

func Median

func Median(data []float32) float32

Finds the middle value or mean of middle values from dataset using Quickselect Note: Contents of data is altered

func MedianFromSorted

func MedianFromSorted(data []float32) float32

Median from sorted data

func Min

func Min(a, b float32) float32

Faster Min

func MinFrom

func MinFrom(s []float32) (min float32)

func MulSlices

func MulSlices(s1, s2 []float32) []float32

Multiplies two slices of same length element-wise.

func NanSlice

func NanSlice(size int) (nanslice []float32)

Creates a slice of NaNs of given size.

func NumRange

func NumRange(start int, end int, step int) (numberrange []float32)

Returns an evenly spaced slice of floats, between <start> and <end> with spacing <step>.

func PrettyPerc

func PrettyPerc(v1, v2 float32, decimals int) float32

Calculate ratio v1/v2 as percentage value with defined amount of decimals

func QuickSelect

func QuickSelect(data []float32, k int) float32

Finds kth largest value from data. NOTE: Will crash if len(data)=0

func QuickSelects

func QuickSelects(data []float32, k int) (float32, float32)

Finds kth and (k+1) th largest value from data. NOTE: Will crash if len(data)=0

func Quicksort

func Quicksort(data []float32)

Sorts data into ascending order using Quicksort. Note: Not safe to use with NaNs. If data is already sorted, the algorithm will be very slow compared to regular sort.

func RemoveFrom

func RemoveFrom(slice []float32, s int) []float32

Removes an integer from given index

func ReplaceNan

func ReplaceNan(value float32, replacewith float32) float32

Replaces given value with a new one, if given value is NaN or Inf.

func ReplaceNans

func ReplaceNans(values []float32, replacewith float32) []float32

Replaces all NaNs in a slice with given value and returns a new slice.

func ReplaceNansInplace

func ReplaceNansInplace(values *[]float32, replacewith float32)

Replaces all NaNs in a slice in place with given value.

func RoundTo

func RoundTo(val float32, digits int) float32

Rounds the number to given significant digits.

func RoundToNearest

func RoundToNearest(value float32, nearest float32) float32

Rounds <value> to <nearest> value

func Select

func Select(condition bool, i1, i2 float32) float32

Returns i1 if condition == TRUE, else returns i2

func SetDefault

func SetDefault(defaultval float32, confval float32) float32

Replaces default value for a parameter, if the configuration value is zero (ie. initialized empty variable). Note that the function does not make any difference, if the configuration is set to zero on purpose.

func SliceOf

func SliceOf(value float32, size int) []float32

Creates a slice of given size filled with given value.

func Sum

func Sum(s []float32) (sum float32)

func SwitchIf

func SwitchIf(condition bool, i1, i2 float32) (float32, float32)

Returns the given values without changes, if condition == FALSE. Returns the values in switched order, if condition == TRUE.

Types

type Rounded

type Rounded struct {
	Rawvalue       float32 // raw value with given digits but no prefix
	Prefix         string  // prefix string
	Value          float32 // rounded value with given digits and prefix
	Response       string  // response string as <Value> <Prefix><unit>
	Prettyvalue    float32 // rounded value with one decimal
	Prettyresponse string  // response string as <Value> <Prefix><unit> ; if value is <0.1 , shows "less than 0.1"
}

func RoundWithPrefix

func RoundWithPrefix(v float32, digits int, unit string, prefix string) Rounded

Returns the rounded value to given digits and correct prefix (M for Megas, k for Kilos etc.) Special case is abs(value) between 1000....10000, which is not converted to kilos (because it looks nicer) set prefix to force certain prefix, otherwise the function figures it out on its' own. These units are excluded from having a prefix

noprefixUnits := []string{"%", "cycles", "years", "°c", "°lon", "°lat", "events", "", " "}

Jump to

Keyboard shortcuts

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