是否有标准 Python class 对浮点范围进行布尔运算?
Is there standard Python class to do boolean operations on float ranges?
我需要收集关于浮点变量近似值的多条信息。信息看起来像“39 < x < 41”或 "x < 14.4"。此外,该变量有其最终的最小值和最大值。
我想要一个 class 以浮点间隔的形式包含此类信息。我还想在这样的时间间隔上执行布尔运算,这样它看起来像:
float_interval(1,5) and float_interval(2,6) == float_interval(2,5)
float_interval(1,2) and float_interval(3,4) == None
float_interval(1,2) or float_interval(3,4) == i_do_not_know_yet
我是在描述一些著名的class还是我自己写的?
如果有人感兴趣,变量是怀孕期间胎儿的胎龄(以周为单位)。在许多病例对照研究中,并不总是直接提及胎儿年龄,而是提供有关妊娠三个月或分娩是足月还是早产等信息。因此,我可以推断出大概的胎龄并将其归入相应的类别。
这是将布尔函数应用于有限区间并集的逻辑的简单概念验证实现。区间的有限并集表示为(开始,结束)对列表。
import functools
import heapq
import itertools
def iter_intervals(tag, interval_set):
for start, end in interval_set:
yield start, tag, True
yield end, tag, False
def apply_boolean_function(f, *interval_sets):
states = [False] * len(interval_sets)
result_state = False
result = []
for boundary, index, new_state in heapq.merge(*itertools.starmap(
iter_intervals, enumerate(interval_sets))):
states[index] = new_state
new_result_state = f(states)
if new_result_state != result_state:
result_state = new_result_state
if new_result_state and result and result[-1] == boundary:
result.pop()
else:
result.append(boundary)
return zip(*[iter(result)] * 2)
union = functools.partial(apply_boolean_function, any)
intersection = functools.partial(apply_boolean_function, all)
complement = functools.partial(apply_boolean_function,
lambda states: not states[0] and states[1])
示例(Python 2):
>>> union([(2, 4), (6, 8)], [(5, 7)])
[(2, 4), (5, 8)]
>>> intersection([(1, 5)], [(2, 6)])
[(2, 5)]
在 Python 3 中,return 值将是惰性 zip()
对象而不是列表。您可以将对 list()
的调用添加到 apply_boolean_function()
中的 return 语句以获取列表。
非常强大的 sympy 包有一个 Interval
class 内置。示例代码:
import sympy
I1 = sympy.Interval(1, 5)
I2 = sympy.Interval(2, 6)
I3 = I1 & I2
print(I3)
我需要收集关于浮点变量近似值的多条信息。信息看起来像“39 < x < 41”或 "x < 14.4"。此外,该变量有其最终的最小值和最大值。
我想要一个 class 以浮点间隔的形式包含此类信息。我还想在这样的时间间隔上执行布尔运算,这样它看起来像:
float_interval(1,5) and float_interval(2,6) == float_interval(2,5)
float_interval(1,2) and float_interval(3,4) == None
float_interval(1,2) or float_interval(3,4) == i_do_not_know_yet
我是在描述一些著名的class还是我自己写的?
如果有人感兴趣,变量是怀孕期间胎儿的胎龄(以周为单位)。在许多病例对照研究中,并不总是直接提及胎儿年龄,而是提供有关妊娠三个月或分娩是足月还是早产等信息。因此,我可以推断出大概的胎龄并将其归入相应的类别。
这是将布尔函数应用于有限区间并集的逻辑的简单概念验证实现。区间的有限并集表示为(开始,结束)对列表。
import functools
import heapq
import itertools
def iter_intervals(tag, interval_set):
for start, end in interval_set:
yield start, tag, True
yield end, tag, False
def apply_boolean_function(f, *interval_sets):
states = [False] * len(interval_sets)
result_state = False
result = []
for boundary, index, new_state in heapq.merge(*itertools.starmap(
iter_intervals, enumerate(interval_sets))):
states[index] = new_state
new_result_state = f(states)
if new_result_state != result_state:
result_state = new_result_state
if new_result_state and result and result[-1] == boundary:
result.pop()
else:
result.append(boundary)
return zip(*[iter(result)] * 2)
union = functools.partial(apply_boolean_function, any)
intersection = functools.partial(apply_boolean_function, all)
complement = functools.partial(apply_boolean_function,
lambda states: not states[0] and states[1])
示例(Python 2):
>>> union([(2, 4), (6, 8)], [(5, 7)])
[(2, 4), (5, 8)]
>>> intersection([(1, 5)], [(2, 6)])
[(2, 5)]
在 Python 3 中,return 值将是惰性 zip()
对象而不是列表。您可以将对 list()
的调用添加到 apply_boolean_function()
中的 return 语句以获取列表。
非常强大的 sympy 包有一个 Interval
class 内置。示例代码:
import sympy
I1 = sympy.Interval(1, 5)
I2 = sympy.Interval(2, 6)
I3 = I1 & I2
print(I3)