summaryrefslogtreecommitdiff
path: root/frob_norm.py
blob: 8f8dc48bdc9bf066961aeee55b9d12d120a8bb12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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))