X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=szyfrow%2Fsupport%2Fnorms.py;h=5b1cb7b3b16b4d32e2202474160022428e6d7312;hb=refs%2Fheads%2Fmain;hp=f131ad597035bbb905a1ec0b875291a49dca8aea;hpb=27c8005f6dea0026887b80a01b5f93a8f1b3c2b2;p=szyfrow.git diff --git a/szyfrow/support/norms.py b/szyfrow/support/norms.py index f131ad5..5b1cb7b 100644 --- a/szyfrow/support/norms.py +++ b/szyfrow/support/norms.py @@ -1,7 +1,10 @@ +"""Various norms, for calcuating the distances between two frequency +profiles. +""" + import collections from math import log10 - def lp(v1, v2=None, p=2): """Find the L_p norm. If passed one vector, find the length of that vector. If passed two vectors, find the length of the difference between them. @@ -10,7 +13,7 @@ def lp(v1, v2=None, p=2): vec = {k: abs(v1[k] - v2[k]) for k in (v1.keys() | v2.keys())} else: vec = v1 - return sum(v ** p for v in vec.values()) ** (1.0 / p) + return sum(v ** p for v in vec.values()) ** (1.0 / p) def l1(v1, v2=None): """Finds the distances between two frequency profiles, expressed as @@ -69,12 +72,14 @@ def l3(v1, v2=None): """ return lp(v1, v2, 3) -def linf(v1, v2=None): +def linf(v1, v2=None): + """Finds the distances between two frequency profiles, expressed as + dictionaries. Assumes every key in frequencies1 is also in frequencies2""" if v2: vec = {k: abs(v1[k] - v2[k]) for k in (v1.keys() | v2.keys())} else: vec = v1 - return max(v for v in vec.values()) + return max(v for v in vec.values()) def scale(frequencies, norm=l2):