Math

Following extended versions of built-in functions are provided.

The function name may be sufficed with _ to avoid name conflict with Python keywords and built-in functions.

Enhanced Math Functions

calcpy.add(*args, default=0)[source]

Add all arguments together.

Parameters:
  • *args – Any number of arguments.

  • default – Default value to return if no arguments are provided.

Returns:

Sum of all arguments.

Example

>>> add()
0
>>> add(1)
1
>>> add(1, 2, 3)
6
calcpy.all_(iterable, empty=True)[source]

Return True of bool(x) is True for all x in the iterable.

If the iterable is empty, return what empty specifies.

Fully compatible with Python’s built-in all().

Parameters:
  • iterable (iterable)

  • empty – Value if iterable is empty.

Returns:

bool

Examples

>>> all_([])
True
>>> all_([False])
False
>>> all_([True])
True
>>> all_([True, False])
False
>>> all_([True, True])
True
calcpy.and_(*args, empty=True)[source]

Return True if all values are True.

Fully compatible with Python’s built-in operator.and_.

Parameters:
  • *args

  • empty – Value if args have no values.

Return type:

bool

Examples

>>> and_()
True
>>> and_(True, True)
True
>>> and_(True, True, False)
False
calcpy.any_(iterable, *, empty=False)[source]

Return True if bool(x) is True for any x in the iterable.

If the iterable is empty, return what empty specifies.

Fully compatible with Python’s built-in any().

Parameters:
  • iterable (iterable)

  • empty – Value if iterable is empty.

Returns:

bool

Examples

>>> any_([])
False
>>> any_([False])
False
>>> any_([True])
True
>>> any_([True, False])
True
>>> any_([True, True])
True
calcpy.crbt(x, /)

Return the cube root of x.

calcpy.fma(x, y, z)[source]

Calculate the fused multiply-add of three numbers.

Parameters:
  • x – First number.

  • y – Second number.

  • z – Third number.

Returns:

Result of x * y + z.

Examples

>>> fma(2, 3, 4)
10
>>> fma(2.5, 3.5, 4.5)
13.25
calcpy.gcd(*args, empty=0)[source]

Calculate the greatest common divisor of all arguments.

Parameters:
  • *args – Any number of arguments.

  • empty – Value to return if no arguments are provided.

Returns:

Greatest common divisor of all arguments.

Example

>>> gcd()
0
>>> gcd(12)
12
>>> gcd(12, 15)
3
>>> gcd(12, 15, 21)
3
calcpy.isqrt(n, /)

Return the integer part of the square root of the input.

calcpy.lcm(*integers)

Least Common Multiple.

calcpy.matmul(*args, default=1)[source]

Matrix multiplication of all arguments.

Parameters:
  • *args – Any number of arguments.

  • default – Default value to return if no arguments are provided.

Returns:

Matrix product of all arguments.

Example

>>> import numpy as np
>>> matmul(np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]]))
array([[19, 22],
       [43, 50]])
calcpy.matprod(values, start=1)[source]

Matrix product of all arguments.

Parameters:
  • *args – Any number of arguments.

  • start – Starting value.

Returns:

Matrix product of all arguments.

Example

>>> import numpy as np
>>> matprod([np.array([[1, 2], [3, 4]])])
array([[1, 2],
    [3, 4]])
>>> matprod([np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]])])
array([[19, 22],
    [43, 50]])
calcpy.minmax(*args, **kwargs)[source]

Get both min and max.

Examples

>>> minmax([1, 3, 4])
(1, 4)
>>> minmax(1, 3, 4)
(1, 4)
calcpy.mul(*args, default=1)[source]

Multiply all arguments together.

Parameters:
  • *args – Any number of arguments.

  • default – Default value to return if no arguments are provided.

Returns:

Product of all arguments.

Example

>>> mul()
1
>>> mul(2)
2
>>> mul(1, 2, 3)
6
calcpy.never(iterable, *, empty=True)[source]

Return True if bool(x) is False for all x in the iterable.

If the iterable is empty, return what empty specifies.

Parameters:
  • iterable (iterable)

  • empty – Value if iterable is empty.

Returns:

bool

Examples

>>> never([])
True
>>> never([False])
True
>>> never([True])
False
>>> never([True, False])
False
>>> never([True, True])
False
calcpy.odd(iterable, *, empty=False)[source]

Return True if an odd number of items in the iterable are True.

If the iterable is empty, return what empty specifies.

Parameters:
  • iterable (iterable)

  • empty – Value if iterable is empty.

Returns:

bool

Examples

>>> odd([])
False
>>> odd([False])
False
>>> odd([True])
True
>>> odd([True, False])
True
>>> odd([True, True])
False
calcpy.or_(*args, empty=False)[source]

Return True if any values are True.

Fully compatible with Python’s built-in operator.or_.

Parameters:
  • *args

  • empty – Value if args have no values.

Return type:

bool

Examples

>>> or_()
False
>>> or_(True, True)
True
>>> or_(True, True, False)
True
calcpy.sumprod(p, q, /)

Return the sum of products of values from two iterables p and q.

Roughly equivalent to:

sum(itertools.starmap(operator.mul, zip(p, q, strict=True)))

For float and mixed int/float inputs, the intermediate products and sums are computed with extended precision.

calcpy.xor(*args, empty=False)[source]

Return True if any values are True.

Fully compatible with Python’s built-in operator.xor.

Parameters:
  • *args

  • empty – Value if args have no values.

Return type:

bool

Examples

>>> xor()
False
>>> xor(True)
True
>>> xor(True, True, False)
False