MathExt

package
v0.3.28 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AVG added in v0.2.32

func AVG(elems []float64) (float64, error)

AVG calculates the average of a slice of float64 elements.

Parameters:

  • elems: The elements to calculate the average of.

Returns:

  • float64: The average of the elements.
  • error: An error of type *ErrInvalidParameter if the slice is empty.

func Add added in v0.2.18

func Add(n1, n2 []int, base int) ([]int, error)

Add adds two numbers of the same base. Both numbers are Least Significant Digit (LSD) first.

Parameters:

  • n1: The first number to add.
  • n2: The second number to add.
  • base: The base of the numbers.

Returns:

  • []int: The sum of the two numbers.
  • error: An error if the base is invalid.

func BaseToDec added in v0.2.18

func BaseToDec(n []int, base int) (int, error)

BaseToDec converts a number of the given base to a decimal number. The number's Least Significant Digit (LSD) is at index 0.

Parameters:

  • n: The number to convert.
  • base: The base of the number.

Returns:

  • int: The decimal number.
  • error: An error if the base is invalid or the number is invalid for the given base.

func BigFloatDivision added in v0.3.19

func BigFloatDivision(numerator, denominator int) (*big.Float, error)

BigFloatDivision is a function that performs division on two integers and returns the result as a big.Float.

Parameters:

  • numerator: The numerator of the division.
  • denominator: The denominator of the division.

Returns:

  • *big.Float: The result of the division as a big.Float.
  • error: An error of type *ErrInvalidParameter if the numerator is negative or

func DecToBase added in v0.2.18

func DecToBase(n, base int) ([]int, error)

DecToBase converts a decimal number to a number of the given base. The number's Least Significant Digit (LSD) is at index 0.

Parameters:

  • n: The decimal number to convert.
  • base: The base of the result number.

Returns:

  • []int: The number in the given base.
  • error: An error if the base is invalid.

func GreatestCommonDivisor

func GreatestCommonDivisor(a, b int) int

GreatestCommonDivisor is a function that calculates the greatest common divisor (GCD) of two integers using the Euclidean algorithm.

Parameters:

  • a, b: The two integers to find the GCD of.

Returns:

  • int: The GCD of the two input numbers.

func IntToBigInt added in v0.3.19

func IntToBigInt(n int) *big.Int

func IsValidNumber added in v0.2.18

func IsValidNumber(n []int, base int) bool

IsValidNumber checks if the given number is valid for the given base.

Parameters:

  • n: The number to check.
  • base: The base of the number.

Returns:

  • bool: True if the number is valid for the given base, false otherwise.

func PrimeFactorization

func PrimeFactorization(inputNumber int) (map[int]int, error)

PrimeFactorization is a function that performs prime factorization on an input number.

Parameters:

  • inputNumber: The number to factorize.

Returns:

  • map[int]int: A map where the keys are the prime factors and the values are their respective powers.
  • error: An error of type *ErrInvalidParameter if the input number is 0.

Behaviors:

  • The input number is converted to a positive number.
  • The prime factors are sorted in ascending order.
  • -1 and 1 are represented as [1: 1].
  • The resulting map does not contain any prime factor with a value of 1.

func SQM added in v0.2.32

func SQM(elems []float64) (float64, error)

SQM calculates the Standard Quadratic Mean of a slice of float64 elements.

Parameters:

  • elems: The elements to calculate the SQM of.

Returns:

  • float64: The SQM of the elements.
  • error: An error of type *ErrInvalidParameter if the slice is empty.

func Subtract added in v0.2.18

func Subtract(n1, n2 []int, base int) ([]int, error)

Subtract subtracts two numbers of the same base. Both numbers are Least Significant Digit (LSD) first.

Parameters:

  • n1: The number to subtract from.
  • n2: The number to subtract.
  • base: The base of the numbers.

Returns:

  • []int: The result of the subtraction.
  • error: An error if the base is invalid or the subtraction resulted in a negative number.

Types

type ConvergenceResult added in v0.3.19

type ConvergenceResult struct {
	// contains filtered or unexported fields
}

ConvergenceResult is a struct that holds the values of the convergence of a series.

func CalculateConvergence added in v0.3.19

func CalculateConvergence(series Serieser, upperLimit int, delta int) (*ConvergenceResult, error)

CalculateConvergence calculates the convergence of a series. It calculates the quotient of the ith term and the (i+delta)th term.

Parameters:

  • series: The series to calculate the convergence.
  • upperLimit: The upper limit of the series to calculate the convergence.
  • delta: The difference between the terms to calculate the convergence.

Returns:

  • *ConvergenceResult: The convergence result.
  • error: An error if the calculation fails.

func (*ConvergenceResult) ApproximateConvergence added in v0.3.19

func (cr *ConvergenceResult) ApproximateConvergence(n int) (*big.Float, error)

ApproximateConvergence approximates the convergence of a series. It calculates the average of the last n values.

Parameters:

  • n: The number of values to calculate the average.

Returns:

  • *big.Float: The average of the last n values.
  • error: An error if the calculation fails.

type ErrSubtractionUnderflow added in v0.2.37

type ErrSubtractionUnderflow struct{}

ErrSubtractionUnderflow is an error that is returned when a subtraction operation results in a negative number.

func NewErrSubtractionUnderflow added in v0.2.37

func NewErrSubtractionUnderflow() *ErrSubtractionUnderflow

NewErrSubtractionUnderflow creates a new ErrSubtractionUnderflow error.

Returns:

  • *ErrSubtractionUnderflow: The new ErrSubtractionUnderflow error.

func (*ErrSubtractionUnderflow) Error added in v0.2.37

func (e *ErrSubtractionUnderflow) Error() string

Error is a method of ErrSubtractionUnderflow that returns the message: "subtraction underflow".

Returns:

  • string: The error message.

type LinearRegression added in v0.3.19

type LinearRegression struct {
	// A is the coefficient of the linear regression.
	A *big.Float

	// B is the exponent of the linear regression.
	B *big.Float
}

LinearRegression is a struct that holds the equation of a linear regression.

func NewLinearRegression added in v0.3.19

func NewLinearRegression() *LinearRegression

NewLinearRegression creates a new LinearRegression.

Returns:

  • LinearRegression: The new LinearRegression.

func (*LinearRegression) FindEquation added in v0.3.19

func (l *LinearRegression) FindEquation(cr *ConvergenceResult) error

FindEquation is a method of ConvergenceResult that finds the equation of the series that best fits the convergence values.

The equation is of the form y = a * x^b.

Returns:

  • float64: The value of a.
  • float64: The value of b.
  • error: An error if the calculation fails.

func (*LinearRegression) String added in v0.3.19

func (lr *LinearRegression) String() string

String implements the fmt.Stringer interface.

Format: y = a * x^b

type Serieser added in v0.3.19

type Serieser interface {
	// Term returns the nth term of the series.
	//
	// Parameters:
	//   - n: The term number.
	//
	// Returns:
	//   - *big.Int: The nth term of the series.
	//   - error: An error if the term cannot be calculated.
	Term(n int) (*big.Int, error)
}

Serieser is an interface for series.

Jump to

Keyboard shortcuts

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