mathematical.utils

Utilities for mathematical operations.

Classes:

FRange()

Returns a range of floating-point numbers.

Functions:

concatenate_csv(*files[, outfile])

Concatenate multiple CSV files together and return a pandas.DataFrame representing the output.

gcd(a, b)

Returns the GCD (HCF) of a and b using Euclid’s Algorithm.

gcd2(numbers)

Returns the GCD (HCF) of a list of numbers using Euclid’s Algorithm.

gcd_array(array)

Returns the GCD for an array of numbers using Euclid’s Algorithm.

hcf(a, b)

Returns the GCD (HCF) of a and b using Euclid’s Algorithm.

hcf2(numbers)

Returns the GCD (HCF) of a list of numbers using Euclid’s Algorithm.

intdiv(p, q)

Integer divsions which rounds toward zero.

isint(num)

Checks whether a float is an integer value.

lcm(numbers)

Returns the LCM of a list of numbers using Euclid’s Algorithm.

log_factorial(x)

Returns the natural logarithm of x factorial (ln(x!).

magnitude(x)

Returns the magnitude of the given value.

mod_inverse(a, m)

Returns the modular inverse of a % m, which is the number x such that a × x % m = 1.

nanmean(ls[, dtype])

Returns the mean of the given sequence, ignoring None and numpy.nan values etc.

nanrsd(ls[, dtype])

Returns the relative standard deviation of the given sequence, ignoring None and numpy.nan values etc.

nanstd(ls[, dtype])

Returns the standard deviation of the given sequence, ignoring None and numpy.nan values etc.

remove_zero(inputlist)

Remove zero values from the given list.

represents_int(s)

Checks whether a value can be converted to an int.

roman(num)

Retuns the Roman numeral represtation of the given value.

rounders(val_to_round, round_format)

Round a value to the specified number format, e.g.

strip_booleans(ls)

Remove booleans from a list.

strip_none_bool_string(ls)

Remove None, boolean and string values from a list.

strip_nonetype(ls)

Remove None from a list.

strip_strings(ls)

Remove strings from a list.

class FRange(stop: float)[source]
class FRange(start: float, stop: float, step: float = '...')

Bases: Sequence[float]

Returns a range of floating-point numbers.

The arguments to the range constructor may be integers or floats.

Parameters
  • start – Default None.

  • stop – Default None.

  • step – Default 1.0.

Raises

ValueError – If step is zero, or if any value is larger than 1×10 14.

New in version 0.2.0.

Methods:

__contains__(o)

Returns whether o is in the range.

__delattr__(key)

Implement delattr(self, name).

__eq__(other)

Return self == other.

__getitem__(item)

Returns the value in the range at index item.

__iter__()

Iterates over values in the range.

__len__()

Returns the number of values in the range.

__repr__()

Return a string representation of the FRange.

__reversed__()

Returns reversed(self).

__setattr__(key, value)

Implement setattr(self, name).

count(value)

Returns 1 if the value is within the range, 0 otherwise.

index(value)

Returns the index of value in the range.

Attributes:

start

The value of the start parameter (or 0.0 if the parameter was not supplied)

step

The value of the step parameter (or 1.0 if the parameter was not supplied)

stop

The value of the stop parameter

__contains__(o)[source]

Returns whether o is in the range.

Parameters

o (object)

Return type

bool

__delattr__(key)[source]

Implement delattr(self, name).

__eq__(other)[source]

Return self == other.

Return type

bool

__getitem__(item)[source]

Returns the value in the range at index item.

Parameters

item

Overloads
__iter__()[source]

Iterates over values in the range.

Return type

Iterator[float]

__len__()[source]

Returns the number of values in the range.

Return type

int

__repr__()[source]

Return a string representation of the FRange.

Return type

str

__reversed__()[source]

Returns reversed(self).

Return type

Iterator[float]

__setattr__(key, value)[source]

Implement setattr(self, name).

count(value)[source]

Returns 1 if the value is within the range, 0 otherwise.

Parameters

value (float)

Return type

int

index(value)[source]

Returns the index of value in the range.

Parameters

value (float)

Raises

ValueError – if the value is not in the range.

Return type

int

start

Type:    float

The value of the start parameter (or 0.0 if the parameter was not supplied)

step

Type:    float

The value of the step parameter (or 1.0 if the parameter was not supplied)

stop

Type:    float

The value of the stop parameter

concatenate_csv(*files, outfile=None)[source]

Concatenate multiple CSV files together and return a pandas.DataFrame representing the output.

Parameters
Return type

DataFrame

Returns

A pandas.DataFrame containing the concatenated CSV data.

New in version 0.3.0.

gcd(a, b)[source]

Returns the GCD (HCF) of a and b using Euclid’s Algorithm.

Parameters
Return type

int

gcd2(numbers)[source]

Returns the GCD (HCF) of a list of numbers using Euclid’s Algorithm.

Parameters

numbers (Sequence[int])

Return type

int

gcd_array(array)[source]

Returns the GCD for an array of numbers using Euclid’s Algorithm.

Based on https://www.geeksforgeeks.org/python-program-for-gcd-of-more-than-two-or-array-numbers/

Parameters

array

Return type

float

hcf(a, b)

Returns the GCD (HCF) of a and b using Euclid’s Algorithm.

Parameters
Return type

int

hcf2(numbers)

Returns the GCD (HCF) of a list of numbers using Euclid’s Algorithm.

Parameters

numbers (Sequence[int])

Return type

int

intdiv(p, q)[source]

Integer divsions which rounds toward zero.

Examples >>> intdiv(3, 2) 1 >>> intdiv(-3, 2) -1 >>> -3 // 2 -2

Return type

int

isint(num)[source]

Checks whether a float is an integer value.

Note

This function only works with floating-point numbers

Parameters

num (float) – value to check

Return type

bool

lcm(numbers)[source]

Returns the LCM of a list of numbers using Euclid’s Algorithm.

Parameters

numbers (Sequence[int])

Return type

float

log_factorial(x)[source]

Returns the natural logarithm of x factorial (ln(x!).

Parameters

x (float)

Return type

float

magnitude(x)[source]

Returns the magnitude of the given value.

Parameters

x (float) – Numerical value to find the magnitude of.

Changed in version 0.2.0: Now returns the absolute magnitude of negative numbers.

Return type

int

mod_inverse(a, m)[source]

Returns the modular inverse of a % m, which is the number x such that a × x % m = 1.

Parameters
Return type

Optional[float]

nanmean(ls, dtype=<class 'float'>)[source]

Returns the mean of the given sequence, ignoring None and numpy.nan values etc.

Similar to numpy.nanmean except it handles None.

Parameters
Return type

float

nanrsd(ls, dtype=<class 'float'>)[source]

Returns the relative standard deviation of the given sequence, ignoring None and numpy.nan values etc.

Parameters
Return type

float

nanstd(ls, dtype=<class 'float'>)[source]

Returns the standard deviation of the given sequence, ignoring None and numpy.nan values etc.

Similar to numpy.nanstd except it handles None.

Parameters
Return type

float

remove_zero(inputlist)[source]

Remove zero values from the given list.

Also removes False and None.

Parameters

inputlist (Sequence[Union[float, bool, None]]) – list to remove zero values from

Return type

List[float]

represents_int(s)[source]

Checks whether a value can be converted to an int.

Parameters

s (Any) – value to check

Return type

bool

roman(num)[source]

Retuns the Roman numeral represtation of the given value.

Examples:

>>> roman(4)
'IV'
>>> roman(17)
'XVII'
Return type

str

rounders(val_to_round, round_format)[source]

Round a value to the specified number format, e.g. "0.000" for three decimal places.

Parameters
Return type

Decimal

strip_booleans(ls)[source]

Remove booleans from a list.

Parameters

ls (Sequence[Any]) – the list to remove booleans from.

Return type

List

Returns

The list without boolean values.

strip_none_bool_string(ls)[source]

Remove None, boolean and string values from a list.

Parameters

ls (Sequence) – The list to remove values from.

Return type

List

strip_nonetype(ls)[source]

Remove None from a list.

Parameters

ls (Sequence[Any]) – the list to remove None from.

Return type

List

Returns

The list without None values.

strip_strings(ls)[source]

Remove strings from a list.

Parameters

ls (Sequence[Any]) – the list to remove strings from.

Return type

List

Returns

The list without strings.