In [6]:
import math
# L1-norm & L2-norm distances
def l1_norm(x1, y1, x2, y2):
""" Calculate Euclidean L1 norm distance
Keyword Arguments
x1,y1 -- co-ordinates of point 1
x2,y2 -- co-ordinates of point 2
"""
distance = (x2 - x1) + (y2 - y1)
return distance
def l2_norm(x1, y1, x2, y2):
""" Calculate Euclidean L2 norm distance
Keyword Arguments
x1,y1 -- co-ordinates of point 1
x2,y2 -- co-ordinates of point 2
"""
distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return distance
In [7]:
# testing the distance functions
print 'Manhattan Distance: ', l1_norm(5,5,9,8)
print 'L2-norm Distance: ', l2_norm(5,5,9,8)
In [23]:
print 'L1-norm Distance to (0,0) : ', l1_norm(0,0,56,13)
print 'L1-norm Distance to (100,40) : ', l1_norm(100,40,56,13)
print 'L2-norm Distance to (0,0) : ', l2_norm(0,0,56,13)
print 'L2-norm Distance to (100,40) : ', l2_norm(100,40,56,13)
In [ ]: