Skip to content

Norms¤

l1_norm

dLux.utils.norms.l1_norm(array, mask=None, axis=None, keepdims=False) ¤

Calculates the L1 norm of an array, optionally applying a mask. The L1 norm is defined as the sum of the absolute values of the elements in the array.

Parameters:

Name Type Description Default
array Array

The input array to calculate the L1 norm of.

required
mask Array | None = None

An optional boolean mask to apply to the array before calculating the norm.

None
axis int | tuple[int, ...] | None = None

Axis or axes along which the norm is computed. By default, all axes are used.

None
keepdims bool = False

If True, the reduced axes are left in the result as dimensions with size one.

False

Returns:

Name Type Description
norm float

The L1 norm of the array, optionally masked.

Source code in dLux/utils/norms.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def l1_norm(
    array: Array,
    mask: Array | None = None,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
) -> float:
    """
    Calculates the L1 norm of an array, optionally applying a mask. The L1 norm is
    defined as the sum of the absolute values of the elements in the array.

    Parameters
    ----------
    array : Array
        The input array to calculate the L1 norm of.
    mask : Array | None = None
        An optional boolean mask to apply to the array before calculating the norm.
    axis : int | tuple[int, ...] | None = None
        Axis or axes along which the norm is computed. By default, all axes are used.
    keepdims : bool = False
        If True, the reduced axes are left in the result as dimensions with size one.

    Returns
    -------
    norm : float
        The L1 norm of the array, optionally masked.
    """
    return np.nansum(
        _resolve_mask(array, mask) * np.abs(array),
        axis=axis,
        keepdims=keepdims,
    )
l2_norm

dLux.utils.norms.l2_norm(array, mask=None, axis=None, keepdims=False) ¤

Calculates the L2 norm of an array, optionally applying a mask. The L2 norm is defined as the square root of the sum of the squares of the elements in the array.

Parameters:

Name Type Description Default
array Array

The input array to calculate the L2 norm of.

required
mask Array | None = None

An optional boolean mask to apply to the array before calculating the norm.

None
axis int | tuple[int, ...] | None = None

Axis or axes along which the norm is computed. By default, all axes are used.

None
keepdims bool = False

If True, the reduced axes are left in the result as dimensions with size one.

False

Returns:

Name Type Description
norm float

The L2 norm of the array, optionally masked.

Source code in dLux/utils/norms.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def l2_norm(
    array: Array,
    mask: Array | None = None,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
) -> float:
    """
    Calculates the L2 norm of an array, optionally applying a mask. The L2 norm is
    defined as the square root of the sum of the squares of the elements in the array.

    Parameters
    ----------
    array : Array
        The input array to calculate the L2 norm of.
    mask : Array | None = None
        An optional boolean mask to apply to the array before calculating the norm.
    axis : int | tuple[int, ...] | None = None
        Axis or axes along which the norm is computed. By default, all axes are used.
    keepdims : bool = False
        If True, the reduced axes are left in the result as dimensions with size one.

    Returns
    -------
    norm : float
        The L2 norm of the array, optionally masked.
    """
    return np.sqrt(
        np.nansum(_resolve_mask(array, mask) * array**2, axis=axis, keepdims=keepdims)
    )
max_norm

dLux.utils.norms.max_norm(array, mask=None, axis=None, keepdims=False) ¤

Calculates the maximum norm of an array, optionally applying a mask. The maximum norm is defined as the maximum absolute value of the elements in the array.

Parameters:

Name Type Description Default
array Array

The input array to calculate the maximum norm of.

required
mask Array | None = None

An optional boolean mask to apply to the array before calculating the norm.

None
axis int | tuple[int, ...] | None = None

Axis or axes along which the norm is computed. By default, all axes are used.

None
keepdims bool = False

If True, the reduced axes are left in the result as dimensions with size one.

False

Returns:

Name Type Description
norm float

The maximum norm of the array, optionally masked.

Source code in dLux/utils/norms.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def max_norm(
    array: Array,
    mask: Array | None = None,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
) -> float:
    """
    Calculates the maximum norm of an array, optionally applying a mask. The maximum
    norm is defined as the maximum absolute value of the elements in the array.

    Parameters
    ----------
    array : Array
        The input array to calculate the maximum norm of.
    mask : Array | None = None
        An optional boolean mask to apply to the array before calculating the norm.
    axis : int | tuple[int, ...] | None = None
        Axis or axes along which the norm is computed. By default, all axes are used.
    keepdims : bool = False
        If True, the reduced axes are left in the result as dimensions with size one.

    Returns
    -------
    norm : float
        The maximum norm of the array, optionally masked.
    """
    resolved_mask = _resolve_mask(array, mask)
    return np.nanmax(
        np.where(resolved_mask.astype(bool), np.abs(array), -np.inf),
        axis=axis,
        keepdims=keepdims,
    )
rms_norm

dLux.utils.norms.rms_norm(array, mask=None, axis=None, keepdims=False) ¤

Calculates the root mean square (RMS) norm of an array, optionally applying a mask. The RMS norm is defined as the square root of the mean of the squares of the elements in the array.

Parameters:

Name Type Description Default
array Array

The input array to calculate the RMS norm of.

required
mask Array | None = None

An optional boolean mask to apply to the array before calculating the norm.

None
axis int | tuple[int, ...] | None = None

Axis or axes along which the norm is computed. By default, all axes are used.

None
keepdims bool = False

If True, the reduced axes are left in the result as dimensions with size one.

False

Returns:

Name Type Description
norm float

The RMS norm of the array, optionally masked.

Source code in dLux/utils/norms.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def rms_norm(
    array: Array,
    mask: Array | None = None,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
) -> float:
    """
    Calculates the root mean square (RMS) norm of an array, optionally applying a mask.
    The RMS norm is defined as the square root of the mean of the squares of the
    elements in the array.

    Parameters
    ----------
    array : Array
        The input array to calculate the RMS norm of.
    mask : Array | None = None
        An optional boolean mask to apply to the array before calculating the norm.
    axis : int | tuple[int, ...] | None = None
        Axis or axes along which the norm is computed. By default, all axes are used.
    keepdims : bool = False
        If True, the reduced axes are left in the result as dimensions with size one.

    Returns
    -------
    norm : float
        The RMS norm of the array, optionally masked.

    """
    mask = _resolve_mask(array, mask)
    n = np.sum(mask, axis=axis, keepdims=keepdims)
    return np.sqrt(np.nansum(mask * array**2, axis=axis, keepdims=keepdims) / n)
p2v_norm

dLux.utils.norms.p2v_norm(array, mask=None, axis=None, keepdims=False) ¤

Calculates the point-to-valley (P2V) norm of an array, optionally applying a mask. The P2V norm is defined as the difference between the maximum and minimum values of the elements in the array.

Parameters:

Name Type Description Default
array Array

The input array to calculate the P2V norm of.

required
mask Array | None = None

An optional boolean mask to apply to the array before calculating the norm.

None
axis int | tuple[int, ...] | None = None

Axis or axes along which the norm is computed. By default, all axes are used.

None
keepdims bool = False

If True, the reduced axes are left in the result as dimensions with size one.

False

Returns:

Name Type Description
norm float

The P2V norm of the array, optionally masked.

Source code in dLux/utils/norms.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def p2v_norm(
    array: Array,
    mask: Array | None = None,
    axis: int | tuple[int, ...] | None = None,
    keepdims: bool = False,
) -> float:
    """
    Calculates the point-to-valley (P2V) norm of an array, optionally applying a mask.
    The P2V norm is defined as the difference between the maximum and minimum values
    of the elements in the array.

    Parameters
    ----------
    array : Array
        The input array to calculate the P2V norm of.
    mask : Array | None = None
        An optional boolean mask to apply to the array before calculating the norm.
    axis : int | tuple[int, ...] | None = None
        Axis or axes along which the norm is computed. By default, all axes are used.
    keepdims : bool = False
        If True, the reduced axes are left in the result as dimensions with size one.

    Returns
    -------
    norm : float
        The P2V norm of the array, optionally masked.
    """
    resolved_mask = _resolve_mask(array, mask)
    max_val = np.nanmax(
        np.where(resolved_mask.astype(bool), array, -np.inf),
        axis=axis,
        keepdims=keepdims,
    )
    min_val = np.nanmin(
        np.where(resolved_mask.astype(bool), array, np.inf),
        axis=axis,
        keepdims=keepdims,
    )
    return max_val - min_val