xmath

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

xmath is a package with math functions. Besides a few standard formulas it contains various mean algorithms.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arithmetic

func Arithmetic(val interface{}) float64

Arithmetic returns the arithmetic mean from a slice of Values as float64. The arithmetic mean or simply the mean or average when the context is clear, is the sum of a list of numbers divided by the number of numbers in the list. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Arithmetic(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 27

func Count

func Count(val interface{}) int

Count returns the length of any slice (like len()).

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func main() {
	fmt.Printf("%#v => %v", float, xmath.Count(float))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 8

func Deg2Rad

func Deg2Rad(deg float64) float64

Deg2Rad returns the rad of a deg.

func Even

func Even(number int) bool

Even tells if a number is even

func FloatRound

func FloatRound(v float64, d int) float64

Round returns a rounded float64 from a float64 with d digits after the point. It rounds via "Round half away from zero".

func Geometric

func Geometric(val interface{}) float64

Geometric returns the geometric mean from a slice of Values as float64. The geometric mean is a type of mean or average, which indicates the central tendency or typical value of a set of numbers by using the product of their values (as opposed to the arithmetic mean which uses their sum). The geometric mean is defined as the nth root of the product of n numbers. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Geometric(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 8

func Harmonic

func Harmonic(val interface{}) float64

Harmonic returns the harmonic mean from a slice of Values as float64. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Harmonic(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 2

func Max

func Max(val interface{}) float64

Max returns the greatest number from a slice of Values as float64. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Max(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 130

func Mean

func Mean(val interface{}, t Meantype) float64
Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var ints = []int{10, 12, 14, 20}

func round2(v float64) float64 {
	return float64(xmath.Round(v*100)) / 100
}

func main() {
	fmt.Printf("Arithmetic: %#v => %v\n", ints, round2(xmath.Mean(ints, xmath.ArithmeticMean)))
	fmt.Printf("Geometric: %#v => %v\n", ints, round2(xmath.Mean(ints, xmath.GeometricMean)))
	fmt.Printf("Harmonic: %#v => %v\n", ints, round2(xmath.Mean(ints, xmath.HarmonicMean)))
	fmt.Printf("Median: %#v => %v\n", ints, round2(xmath.Mean(ints, xmath.MedianMean)))
	fmt.Printf("Rootmeansquare: %#v => %v\n", ints, round2(xmath.Mean(ints, xmath.RmsMean)))
	fmt.Printf("Default: %#v => %v\n", ints, round2(xmath.Mean(ints, xmath.Default)))

}
Output:

Arithmetic: []int{10, 12, 14, 20} => 14
Geometric: []int{10, 12, 14, 20} => 13.54
Harmonic: []int{10, 12, 14, 20} => 13.13
Median: []int{10, 12, 14, 20} => 13
Rootmeansquare: []int{10, 12, 14, 20} => 14.49
Default: []int{10, 12, 14, 20} => 14

func Median

func Median(val interface{}) float64

Median returns the median from a slice of Values as float64. The median is the numerical value separating the higher half of a data sample from the lower half. The median of a list of numbers can be found by arranging all the observations from lowest value to highest value and picking the middle one. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Median(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 9

func Min

func Min(val interface{}) float64

Min returns the smallest number from a slice of Values as float64. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Min(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 1

func Odd

func Odd(number int) bool

Odd tells if a number is odd

func Prime

func Prime(n int) int

Prime returns the nth prime number as int.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

func main() {
	fmt.Printf("%#v => %v", 42, xmath.Prime(42))

}
Output:

42 => 181

func Rad2Deg

func Rad2Deg(rad float64) float64

Rad2Deg returns the deg of a rad.

func Rootmeansquare

func Rootmeansquare(val interface{}) float64

Rootmeansquare returns the root mean square from a slice of Values as float64. The root mean square is the root value of the sum of the squared value of a list of numbers divided by the number of numbers in the list. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

func Round

func Round(v float64) int

Round returns a rounded int from a float64. It rounds via "Round half away from zero".

func Sqrt

func Sqrt(n int64) int64

Sqrt calculates the square root of n.

func Sum

func Sum(val interface{}) float64

Sum returns the sum from a slice of Values as float64. It uses "as" (simonwaldherr.de/go/golibs/as) to convert given values to floats.

Example
package main

import (
	"fmt"
	"simonwaldherr.de/go/golibs/xmath"
)

var float = []float64{.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 1.3e2}

func round(v float64) int {
	return xmath.Round(v)
}

func main() {
	fmt.Printf("%#v => %v", float, round(xmath.Sum(float)))

}
Output:

[]float64{0.5, 1.33, 2.66, 3.99, 13.37, 23.42, 42.00031, 130} => 217

Types

type Meantype

type Meantype int
const (
	ArithmeticMean Meantype = iota
	GeometricMean
	HarmonicMean
	MedianMean
	RmsMean
	Default
)

Jump to

Keyboard shortcuts

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