this function calculates the minkowski distance for a given order between two ndimensional points represented as lists for the case of order 1 the minkowski distance degenerates to the manhattan distance for order 2 the usual euclidean distance is obtained https en wikipedia orgwikiminkowskidistance note due to floating point calculation errors the output of this function may be inaccurate minkowskidistance1 0 1 0 2 0 2 0 1 2 0 minkowskidistance1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 2 8 0 import numpy as np np isclose5 0 minkowskidistance5 0 0 0 3 true minkowskidistance1 0 2 0 1 traceback most recent call last valueerror the order must be greater than or equal to 1 minkowskidistance1 0 1 0 2 0 1 traceback most recent call last valueerror both points must have the same dimension this function calculates the minkowski distance for a given order between two n dimensional points represented as lists for the case of order 1 the minkowski distance degenerates to the manhattan distance for order 2 the usual euclidean distance is obtained https en wikipedia org wiki minkowski_distance note due to floating point calculation errors the output of this function may be inaccurate minkowski_distance 1 0 1 0 2 0 2 0 1 2 0 minkowski_distance 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 2 8 0 import numpy as np np isclose 5 0 minkowski_distance 5 0 0 0 3 true minkowski_distance 1 0 2 0 1 traceback most recent call last valueerror the order must be greater than or equal to 1 minkowski_distance 1 0 1 0 2 0 1 traceback most recent call last valueerror both points must have the same dimension
def minkowski_distance(
    point_a: list[float],
    point_b: list[float],
    order: int,
) -> float:
    
    if order < 1:
        raise ValueError("The order must be greater than or equal to 1.")

    if len(point_a) != len(point_b):
        raise ValueError("Both points must have the same dimension.")

    return sum(abs(a - b) ** order for a, b in zip(point_a, point_b)) ** (1 / order)


if __name__ == "__main__":
    import doctest

    doctest.testmod()