diff options
author | David Luevano <55825613+luevano@users.noreply.github.com> | 2019-12-08 21:54:19 -0700 |
---|---|---|
committer | David Luevano <55825613+luevano@users.noreply.github.com> | 2019-12-08 21:54:19 -0700 |
commit | 7c8b391dddd65ff58417df80dfc79cadddf4f4e6 (patch) | |
tree | 0bad908f5b40848addc3307de4307b1dae4c6e7e /frob_norm.py |
First commit
Diffstat (limited to 'frob_norm.py')
-rw-r--r-- | frob_norm.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/frob_norm.py b/frob_norm.py new file mode 100644 index 000000000..8f8dc48bd --- /dev/null +++ b/frob_norm.py @@ -0,0 +1,29 @@ +import math + + +def frob_norm(array): + """ + Calculates the frobenius norm of a given array or matrix. + array: array of data. + """ + + arr_sh_len = len(array.shape) + arr_range = range(len(array)) + fn = 0.0 + + # If it is a 'vector'. + if arr_sh_len == 1: + for i in arr_range: + fn += array[i]*array[i] + + return math.sqrt(fn) + + # If it is a matrix. + elif arr_sh_len == 2: + for i in arr_range: + for j in arr_range: + fn += array[i, j]*array[i, j] + + return math.sqrt(fn) + else: + print('Error. Array size greater than 2 ({}).'.format(arr_sh_len)) |