Module: float_list_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.
from typing import List
import numpy as np
from cl.runtime.primitive.float_util import FloatUtil
from cl.runtime.primitive.list_util import ListUtil
class FloatListUtil(ListUtil):
"""Extends ListUtil to provide helper methods for float lists."""
@classmethod
def is_strictly_ascending(cls, values: List[float]) -> bool:
"""
Returns true if the list is sorted in ascending order up to float
tolerance with equal values not permitted.
"""
tolerance: float = FloatUtil.tolerance
arr: np.ndarray = np.array(values)
result = np.all(arr[:-1] < arr[1:] - tolerance)
return result
@classmethod
def is_equal_or_ascending(cls, values: List[float]) -> bool:
"""
Returns true if the list is sorted in ascending order up to float
tolerance with equal values permitted.
"""
tolerance: float = FloatUtil.tolerance
arr: np.ndarray = np.array(values)
result = np.all(arr[:-1] < arr[1:] + tolerance)
return result
@classmethod
def is_strictly_descending(cls, values: List[float]) -> bool:
"""
Returns true if the list is sorted in descending order up to float
tolerance with equal values not permitted.
"""
tolerance: float = FloatUtil.tolerance
arr: np.ndarray = np.array(values)
result = np.all(arr[:-1] > arr[1:] + tolerance)
return result
@classmethod
def is_equal_or_descending(cls, values: List[float]) -> bool:
"""
Returns true if the list is sorted in descending order up to float
tolerance with equal values permitted.
"""
tolerance: float = FloatUtil.tolerance
arr: np.ndarray = np.array(values)
result = np.all(arr[:-1] > arr[1:] - tolerance)
return result
Classes
class FloatListUtil
-
Extends ListUtil to provide helper methods for float lists.
Expand source code
class FloatListUtil(ListUtil): """Extends ListUtil to provide helper methods for float lists.""" @classmethod def is_strictly_ascending(cls, values: List[float]) -> bool: """ Returns true if the list is sorted in ascending order up to float tolerance with equal values not permitted. """ tolerance: float = FloatUtil.tolerance arr: np.ndarray = np.array(values) result = np.all(arr[:-1] < arr[1:] - tolerance) return result @classmethod def is_equal_or_ascending(cls, values: List[float]) -> bool: """ Returns true if the list is sorted in ascending order up to float tolerance with equal values permitted. """ tolerance: float = FloatUtil.tolerance arr: np.ndarray = np.array(values) result = np.all(arr[:-1] < arr[1:] + tolerance) return result @classmethod def is_strictly_descending(cls, values: List[float]) -> bool: """ Returns true if the list is sorted in descending order up to float tolerance with equal values not permitted. """ tolerance: float = FloatUtil.tolerance arr: np.ndarray = np.array(values) result = np.all(arr[:-1] > arr[1:] + tolerance) return result @classmethod def is_equal_or_descending(cls, values: List[float]) -> bool: """ Returns true if the list is sorted in descending order up to float tolerance with equal values permitted. """ tolerance: float = FloatUtil.tolerance arr: np.ndarray = np.array(values) result = np.all(arr[:-1] > arr[1:] - tolerance) return result
Ancestors
Static methods
def is_empty(values: Optional[List]) -> bool
-
Inherited from:
ListUtil
.is_empty
Returns true if the list is None or has zero length.
def is_equal_or_ascending(values: List[float]) -> bool
-
Returns true if the list is sorted in ascending order up to float tolerance with equal values permitted.
def is_equal_or_descending(values: List[float]) -> bool
-
Returns true if the list is sorted in descending order up to float tolerance with equal values permitted.
def is_strictly_ascending(values: List[float]) -> bool
-
Returns true if the list is sorted in ascending order up to float tolerance with equal values not permitted.
def is_strictly_descending(values: List[float]) -> bool
-
Returns true if the list is sorted in descending order up to float tolerance with equal values not permitted.