From 7c8b391dddd65ff58417df80dfc79cadddf4f4e6 Mon Sep 17 00:00:00 2001 From: David Luevano <55825613+luevano@users.noreply.github.com> Date: Sun, 8 Dec 2019 21:54:19 -0700 Subject: First commit --- frob_norm.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 frob_norm.py (limited to 'frob_norm.py') 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)) -- cgit v1.2.3-70-g09d2