mathem

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 1 Imported by: 0

README

mathem

GitHub GitHub go.mod Go version Go Reference

A sub-package of the gophysics library providing some of the most common Mathematical constants and formulas used in Physics.

List of constants

  • GreekPi = 3.1415926535
  • Radiant = 57.2958
  • E = 2.71828
  • SquareRoot_2 = 1.41421
  • SquareRoot_3 = 1.73205

List of formulas

  • Radiant to Degrees and viceversa conversions
  • Circle Area, Sphere Volume
  • Sine Square, Cosine Square
  • Powering at, Powering 10 at
  • Summatory, Product of Sequence
  • Arithmetic Mean, Variance, Standard Deviation

Example

import (
    "fmt"
    "github.com/Gabri432/gophysics/mathem"
)

func main() {
    fmt.Println(mathem.StandardDeviation(2, 4, 4, 4, 5, 5, 7, 9)) // Using the Standard Deviation formula
}

=== Output ===
2

Notes

  • Information for "Variance" and "Standard Deviation" were taken from the following italian book: Title "Teoria degli Errori e Fondamenti di Statistica", Author: "Maurizio Loreti".

Documentation

Overview

Package mathem is a sub-package of the gophysics library.

Its porpuse is to make the most common Math calculations used for Physics.

Some of the most basic operations are radiants to degrees (and viceversa) conversions, area calculations, summatory, ecc...

Here is the documentation https://pkg.go.dev/github.com/Gabri432/gophysics/mathem

Index

Constants

View Source
const (
	//Greek Pi
	Pi, GreekPi = 3.1415926535, Pi

	//1 Radiant in degrees
	Radiant = 57.2958

	//Euler number
	E = 2.71828

	//Square root of 2
	SquareRoot_2 = 1.41421

	//Square root of 3
	SquareRoot_3 = 1.73205
)

Variables

This section is empty.

Functions

func ArithmeticMean added in v0.17.0

func ArithmeticMean(values ...float64) float64

Calculates the arithmetic mean of the user given values.

Example:

func main() {
	mathem.ArithemticMean(1,2,3,4,6) // (1+2+3+4+6)/5 => 16/5 == 3.2
}

func CircleArea

func CircleArea(radius float64) (area float64, measurementUnit string)

It calculates the circle area

func CosineSquare added in v0.15.0

func CosineSquare(angleInDeg float64) float64

It calculates cosine^2 of the angle in degrees

func DegToRad

func DegToRad(angleInDeg float64) (radiants float64, measurementUnit string)

It converts degrees to radiants

func GeometricMean added in v0.19.0

func GeometricMean(values ...float64) float64

Calculates the geometric mean of the user given values.

Example:

func main() {
	mathem.GeometricMean(1,2,3,4) // (1*2*3*4)^(1/4) == 2.213
}

func HarmonicMean added in v0.19.0

func HarmonicMean(values ...float64) float64

Calculates the Harmonic mean of the user given values.

Example:

func main() {
	mathem.HarmonicMean(1,2,3,4) // [4/(1/1 + 1/2 + 1/3 + 1/4)] == 1.92
}

func Power10 added in v0.16.0

func Power10(base float64, power int) float64

Power10 calls the math.Pow10 method to return base*10**power

func PowerAt added in v0.16.0

func PowerAt(base, power float64) float64

PowerAt calls the math.Pow method to return base**power

func ProductOfSequence added in v0.16.0

func ProductOfSequence(startingValue, endingValue int, function func(value int) float64) (product float64)

ProductOfSequence function will calculate the user defined function from the starting to the ending integer value.

Each output will be multiplied to the following one and then store into the resulting product that will be returned.

Example:

import "github.com/Gabri432/gophysics/mathem"

func main() {
	var squareFunction = func(value int) float64 {
	                       return float64(value*value) //n*n
                           }
    fmt.Println(mathem.ProductOfSequence(1, 3, squareFunction)) // [1*1] * [2*2] * [3*3] = 1 * 4 * 9 = 36
}

func RadToDeg

func RadToDeg(angleInRad float64) (degrees float64, measurementUnit string)

It converts radiants to degrees

func SineSquare added in v0.15.0

func SineSquare(angleInDeg float64) float64

It calculates sine^2 of the angle in degrees

func SphereVolume

func SphereVolume(radius float64) (volume float64, measurementUnit string)

It calculates sphere volume

func StandardDeviation added in v0.17.0

func StandardDeviation(values ...float64) float64

StandardDeviation is the square root of the Variance.

Example:

func main() {
	fmt.Println(mathem.StandardDeviation(2, 4, 4, 4, 5, 5, 7, 9)) // returns 2

	// STEP 1: Arithmetic mean = 5;
	// STEP 2: Sum of the square deviations =
	// = [(2-5)^2 + (4-5)^2 + (4-5)^2 + (4-5)^2 + (5-5)^2 + (5-5)^2 + (7-5)^2 + (9-5)^2] = 40;
	// STEP 3: Mean of those values = [(9 + 1 + 1 + 1 + 0 + 0 + 4 + 16) / 8] == 4;
	// STEP 4: Square Root of Variance = 2;
}

func Summatory added in v0.16.0

func Summatory(startingValue, endingValue int, function func(value int) float64) (sum float64)

Summatory function will calculate the user defined function from the starting to the ending integer value.

Each output will be added to the following one and then store into the resulting sum that will be returned.

Example:

import "github.com/Gabri432/gophysics/mathem"

func main() {
	var oddNumberFunction = func(value int) float64 {
	                       return float64(2*value+1) //2n+1
                           }
    fmt.Println(mathem.Summatory(1, 3, oddNumberFunction)) // [2*(1)+1] + [2*(2)+1] + [2*(3)+1] = 3 + 5 + 7 = 15
}

func Variance added in v0.17.0

func Variance(values ...float64) float64

Variance is the ratio between the square deviations of each value from the mean and the number of values.

Example:

func main() {
	fmt.Println(mathem.Variance(2, 4, 4, 4, 5, 5, 7, 9)) // returns 4

	// STEP 1: Arithmetic mean = 5;
	// STEP 2: Sum of the square deviations =
	// = [(2-5)^2 + (4-5)^2 + (4-5)^2 + (4-5)^2 + (5-5)^2 + (5-5)^2 + (7-5)^2 + (9-5)^2] = 40;
	// STEP 3: Mean of those values = [(9 + 1 + 1 + 1 + 0 + 0 + 4 + 16) / 8] == 4
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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