Module: float_array_util
Expand source code
# Copyright (C) 2023-present The Project Contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
from cl.runtime.primitive.float_util import FloatUtil
class FloatArrayUtil:
"""
This class provides helper methods for one dimensional float arrays (dtype=float).
"""
@classmethod
def equal(cls, array_1: np.ndarray, array_2: np.ndarray) -> bool:
"""Returns true if the two arrays have the same length and each element is equal within float tolerance."""
if len(array_1) != len(array_2):
raise RuntimeError("Arrays have different length of {len(array_1)} and {len(array_2)}.")
return FloatArrayUtil.is_zero(array_1 - array_2)
@classmethod
def is_zero(cls, arr: np.ndarray) -> bool:
"""Returns true if every element is zero within float tolerance."""
tolerance = FloatUtil.tolerance
return np.all([-tolerance < x < tolerance for x in arr])
@classmethod
def is_strictly_ascending(cls, arr: np.ndarray) -> bool:
"""
Returns true if the list is sorted in ascending order up to float
tolerance with equal values not permitted.
"""
tolerance: float = FloatUtil.tolerance
result = np.all(arr[:-1] < arr[1:] - tolerance)
return result
@classmethod
def is_equal_or_ascending(cls, arr: np.ndarray) -> bool:
"""
Returns true if the list is sorted in ascending order up to float
tolerance with equal values permitted.
"""
tolerance: float = FloatUtil.tolerance
result = np.all(arr[:-1] < arr[1:] + tolerance)
return result
@classmethod
def is_strictly_descending(cls, arr: np.ndarray) -> bool:
"""
Returns true if the list is sorted in descending order up to float
tolerance with equal values not permitted.
"""
tolerance: float = FloatUtil.tolerance
result = np.all(arr[:-1] > arr[1:] + tolerance)
return result
@classmethod
def is_equal_or_descending(cls, arr: np.ndarray) -> bool:
"""
Returns true if the list is sorted in descending order up to float
tolerance with equal values permitted.
"""
tolerance: float = FloatUtil.tolerance
result = np.all(arr[:-1] > arr[1:] - tolerance)
return result
Classes
class FloatArrayUtil
-
This class provides helper methods for one dimensional float arrays (dtype=float).
Expand source code
class FloatArrayUtil: """ This class provides helper methods for one dimensional float arrays (dtype=float). """ @classmethod def equal(cls, array_1: np.ndarray, array_2: np.ndarray) -> bool: """Returns true if the two arrays have the same length and each element is equal within float tolerance.""" if len(array_1) != len(array_2): raise RuntimeError("Arrays have different length of {len(array_1)} and {len(array_2)}.") return FloatArrayUtil.is_zero(array_1 - array_2) @classmethod def is_zero(cls, arr: np.ndarray) -> bool: """Returns true if every element is zero within float tolerance.""" tolerance = FloatUtil.tolerance return np.all([-tolerance < x < tolerance for x in arr]) @classmethod def is_strictly_ascending(cls, arr: np.ndarray) -> bool: """ Returns true if the list is sorted in ascending order up to float tolerance with equal values not permitted. """ tolerance: float = FloatUtil.tolerance result = np.all(arr[:-1] < arr[1:] - tolerance) return result @classmethod def is_equal_or_ascending(cls, arr: np.ndarray) -> bool: """ Returns true if the list is sorted in ascending order up to float tolerance with equal values permitted. """ tolerance: float = FloatUtil.tolerance result = np.all(arr[:-1] < arr[1:] + tolerance) return result @classmethod def is_strictly_descending(cls, arr: np.ndarray) -> bool: """ Returns true if the list is sorted in descending order up to float tolerance with equal values not permitted. """ tolerance: float = FloatUtil.tolerance result = np.all(arr[:-1] > arr[1:] + tolerance) return result @classmethod def is_equal_or_descending(cls, arr: np.ndarray) -> bool: """ Returns true if the list is sorted in descending order up to float tolerance with equal values permitted. """ tolerance: float = FloatUtil.tolerance result = np.all(arr[:-1] > arr[1:] - tolerance) return result
Static methods
def equal(array_1: numpy.ndarray, array_2: numpy.ndarray) -> bool
-
Returns true if the two arrays have the same length and each element is equal within float tolerance.
def is_equal_or_ascending(arr: numpy.ndarray) -> bool
-
Returns true if the list is sorted in ascending order up to float tolerance with equal values permitted.
def is_equal_or_descending(arr: numpy.ndarray) -> bool
-
Returns true if the list is sorted in descending order up to float tolerance with equal values permitted.
def is_strictly_ascending(arr: numpy.ndarray) -> bool
-
Returns true if the list is sorted in ascending order up to float tolerance with equal values not permitted.
def is_strictly_descending(arr: numpy.ndarray) -> bool
-
Returns true if the list is sorted in descending order up to float tolerance with equal values not permitted.
def is_zero(arr: numpy.ndarray) -> bool
-
Returns true if every element is zero within float tolerance.