mirror of https://github.com/Nonannet/copapy.git
volatile property from value type removed.
This commit is contained in:
parent
d71f010199
commit
a8e70cd5d6
|
|
@ -76,7 +76,6 @@ class Net:
|
||||||
def __init__(self, dtype: str, source: Node):
|
def __init__(self, dtype: str, source: Node):
|
||||||
self.dtype = dtype
|
self.dtype = dtype
|
||||||
self.source = source
|
self.source = source
|
||||||
self.volatile = False
|
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
names = get_var_name(self)
|
names = get_var_name(self)
|
||||||
|
|
@ -93,7 +92,7 @@ class value(Generic[TNum], Net):
|
||||||
Attributes:
|
Attributes:
|
||||||
dtype (str): Data type of this value.
|
dtype (str): Data type of this value.
|
||||||
"""
|
"""
|
||||||
def __init__(self, source: TNum | Node, dtype: str | None = None, volatile: bool = True):
|
def __init__(self, source: TNum | Node, dtype: str | None = None):
|
||||||
"""Instance a value.
|
"""Instance a value.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|
@ -113,7 +112,6 @@ class value(Generic[TNum], Net):
|
||||||
else:
|
else:
|
||||||
self.source = CPConstant(source, False)
|
self.source = CPConstant(source, False)
|
||||||
self.dtype = 'int'
|
self.dtype = 'int'
|
||||||
self.volatile = volatile
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __add__(self: 'value[TNum]', other: 'value[TNum] | TNum') -> 'value[TNum]': ...
|
def __add__(self: 'value[TNum]', other: 'value[TNum] | TNum') -> 'value[TNum]': ...
|
||||||
|
|
@ -225,32 +223,32 @@ class value(Generic[TNum], Net):
|
||||||
|
|
||||||
def __neg__(self: TCPNum) -> TCPNum:
|
def __neg__(self: TCPNum) -> TCPNum:
|
||||||
if self.dtype == 'float':
|
if self.dtype == 'float':
|
||||||
return cast(TCPNum, add_op('sub', [value(0.0, volatile=False), self]))
|
return cast(TCPNum, add_op('sub', [value(0.0), self]))
|
||||||
return cast(TCPNum, add_op('sub', [value(0, volatile=False), self]))
|
return cast(TCPNum, add_op('sub', [value(0), self]))
|
||||||
|
|
||||||
def __gt__(self, other: TVarNumb) -> 'value[int]':
|
def __gt__(self, other: TVarNumb) -> 'value[int]':
|
||||||
ret = add_op('gt', [self, other])
|
ret = add_op('gt', [self, other])
|
||||||
return value(ret.source, dtype='bool', volatile=False)
|
return value(ret.source, dtype='bool')
|
||||||
|
|
||||||
def __lt__(self, other: TVarNumb) -> 'value[int]':
|
def __lt__(self, other: TVarNumb) -> 'value[int]':
|
||||||
ret = add_op('gt', [other, self])
|
ret = add_op('gt', [other, self])
|
||||||
return value(ret.source, dtype='bool', volatile=False)
|
return value(ret.source, dtype='bool')
|
||||||
|
|
||||||
def __ge__(self, other: TVarNumb) -> 'value[int]':
|
def __ge__(self, other: TVarNumb) -> 'value[int]':
|
||||||
ret = add_op('ge', [self, other])
|
ret = add_op('ge', [self, other])
|
||||||
return value(ret.source, dtype='bool', volatile=False)
|
return value(ret.source, dtype='bool')
|
||||||
|
|
||||||
def __le__(self, other: TVarNumb) -> 'value[int]':
|
def __le__(self, other: TVarNumb) -> 'value[int]':
|
||||||
ret = add_op('ge', [other, self])
|
ret = add_op('ge', [other, self])
|
||||||
return value(ret.source, dtype='bool', volatile=False)
|
return value(ret.source, dtype='bool')
|
||||||
|
|
||||||
def __eq__(self, other: TVarNumb) -> 'value[int]': # type: ignore
|
def __eq__(self, other: TVarNumb) -> 'value[int]': # type: ignore
|
||||||
ret = add_op('eq', [self, other], True)
|
ret = add_op('eq', [self, other], True)
|
||||||
return value(ret.source, dtype='bool', volatile=False)
|
return value(ret.source, dtype='bool')
|
||||||
|
|
||||||
def __ne__(self, other: TVarNumb) -> 'value[int]': # type: ignore
|
def __ne__(self, other: TVarNumb) -> 'value[int]': # type: ignore
|
||||||
ret = add_op('ne', [self, other], True)
|
ret = add_op('ne', [self, other], True)
|
||||||
return value(ret.source, dtype='bool', volatile=False)
|
return value(ret.source, dtype='bool')
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
def __mod__(self: 'value[TNum]', other: 'value[TNum] | TNum') -> 'value[TNum]': ...
|
def __mod__(self: 'value[TNum]', other: 'value[TNum] | TNum') -> 'value[TNum]': ...
|
||||||
|
|
@ -332,11 +330,11 @@ class value(Generic[TNum], Net):
|
||||||
|
|
||||||
|
|
||||||
class CPConstant(Node):
|
class CPConstant(Node):
|
||||||
def __init__(self, value: int | float, constant: bool = True):
|
def __init__(self, value: int | float, anonymous: bool = True):
|
||||||
self.dtype, self.value = _get_data_and_dtype(value)
|
self.dtype, self.value = _get_data_and_dtype(value)
|
||||||
self.name = 'const_' + self.dtype
|
self.name = 'const_' + self.dtype
|
||||||
self.args = tuple()
|
self.args = tuple()
|
||||||
self.node_hash = hash(value) ^ hash(self.dtype) if constant else id(self)
|
self.node_hash = hash(value) ^ hash(self.dtype) if anonymous else id(self)
|
||||||
|
|
||||||
|
|
||||||
class Write(Node):
|
class Write(Node):
|
||||||
|
|
@ -362,7 +360,7 @@ class Op(Node):
|
||||||
|
|
||||||
def net_from_value(val: Any) -> value[Any]:
|
def net_from_value(val: Any) -> value[Any]:
|
||||||
vi = CPConstant(val)
|
vi = CPConstant(val)
|
||||||
return value(vi, vi.dtype, False)
|
return value(vi, vi.dtype)
|
||||||
|
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ class matrix(Generic[TNum]):
|
||||||
tuple(a + other for a in row)
|
tuple(a + other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(a + o if isinstance(a, value) else a + other for a in row)
|
tuple(a + o if isinstance(a, value) else a + other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
|
|
@ -117,7 +117,7 @@ class matrix(Generic[TNum]):
|
||||||
tuple(a - other for a in row)
|
tuple(a - other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(a - o if isinstance(a, value) else a - other for a in row)
|
tuple(a - o if isinstance(a, value) else a - other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
|
|
@ -140,7 +140,7 @@ class matrix(Generic[TNum]):
|
||||||
tuple(other - a for a in row)
|
tuple(other - a for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(o - a if isinstance(a, value) else other - a for a in row)
|
tuple(o - a if isinstance(a, value) else other - a for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
|
|
@ -168,7 +168,7 @@ class matrix(Generic[TNum]):
|
||||||
tuple(a * other for a in row)
|
tuple(a * other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(a * o if isinstance(a, value) else a * other for a in row)
|
tuple(a * o if isinstance(a, value) else a * other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
|
|
@ -195,7 +195,7 @@ class matrix(Generic[TNum]):
|
||||||
tuple(a / other for a in row)
|
tuple(a / other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(a / o if isinstance(a, value) else a / other for a in row)
|
tuple(a / o if isinstance(a, value) else a / other for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
|
|
@ -214,7 +214,7 @@ class matrix(Generic[TNum]):
|
||||||
tuple(other / a for a in row)
|
tuple(other / a for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(o / a if isinstance(a, value) else other / a for a in row)
|
tuple(o / a if isinstance(a, value) else other / a for a in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
|
|
@ -305,7 +305,7 @@ class matrix(Generic[TNum]):
|
||||||
"""Convert all elements to copapy values if any element is a copapy value."""
|
"""Convert all elements to copapy values if any element is a copapy value."""
|
||||||
if any(isinstance(val, value) for row in self.values for val in row):
|
if any(isinstance(val, value) for row in self.values for val in row):
|
||||||
return matrix(
|
return matrix(
|
||||||
tuple(value(val, volatile=False) if not isinstance(val, value) else val for val in row)
|
tuple(value(val) if not isinstance(val, value) else val for val in row)
|
||||||
for row in self.values
|
for row in self.values
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,6 @@ def mixed_sum(scalars: Iterable[int | float | value[Any]]) -> Any:
|
||||||
|
|
||||||
def mixed_homogenize(scalars: Iterable[T | value[T]]) -> Iterable[T] | Iterable[value[T]]:
|
def mixed_homogenize(scalars: Iterable[T | value[T]]) -> Iterable[T] | Iterable[value[T]]:
|
||||||
if any(isinstance(val, value) for val in scalars):
|
if any(isinstance(val, value) for val in scalars):
|
||||||
return (value(val, volatile=False) if not isinstance(val, value) else val for val in scalars)
|
return (value(val) if not isinstance(val, value) else val for val in scalars)
|
||||||
else:
|
else:
|
||||||
return (val for val in scalars if not isinstance(val, value))
|
return (val for val in scalars if not isinstance(val, value))
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a + b for a, b in zip(self.values, other.values))
|
return vector(a + b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a + other for a in self.values)
|
return vector(a + other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a + o if isinstance(a, value) else a + other for a in self.values)
|
return vector(a + o if isinstance(a, value) else a + other for a in self.values)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
@ -85,7 +85,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a - b for a, b in zip(self.values, other.values))
|
return vector(a - b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a - other for a in self.values)
|
return vector(a - other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a - o if isinstance(a, value) else a - other for a in self.values)
|
return vector(a - o if isinstance(a, value) else a - other for a in self.values)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
@ -100,7 +100,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(b - a for a, b in zip(self.values, other.values))
|
return vector(b - a for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(other - a for a in self.values)
|
return vector(other - a for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(o - a if isinstance(a, value) else other - a for a in self.values)
|
return vector(o - a if isinstance(a, value) else other - a for a in self.values)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
@ -117,7 +117,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a * b for a, b in zip(self.values, other.values))
|
return vector(a * b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a * other for a in self.values)
|
return vector(a * other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a * o if isinstance(a, value) else a * other for a in self.values)
|
return vector(a * o if isinstance(a, value) else a * other for a in self.values)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
@ -143,7 +143,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a ** b for a, b in zip(self.values, other.values))
|
return vector(a ** b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a ** other for a in self.values)
|
return vector(a ** other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a ** o if isinstance(a, value) else a ** other for a in self.values)
|
return vector(a ** o if isinstance(a, value) else a ** other for a in self.values)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
@ -158,7 +158,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(b ** a for a, b in zip(self.values, other.values))
|
return vector(b ** a for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(other ** a for a in self.values)
|
return vector(other ** a for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(o ** a if isinstance(a, value) else other ** a for a in self.values)
|
return vector(o ** a if isinstance(a, value) else other ** a for a in self.values)
|
||||||
|
|
||||||
def __truediv__(self, other: VecNumLike) -> 'vector[float]':
|
def __truediv__(self, other: VecNumLike) -> 'vector[float]':
|
||||||
|
|
@ -167,7 +167,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a / b for a, b in zip(self.values, other.values))
|
return vector(a / b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a / other for a in self.values)
|
return vector(a / other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a / o if isinstance(a, value) else a / other for a in self.values)
|
return vector(a / o if isinstance(a, value) else a / other for a in self.values)
|
||||||
|
|
||||||
def __rtruediv__(self, other: VecNumLike) -> 'vector[float]':
|
def __rtruediv__(self, other: VecNumLike) -> 'vector[float]':
|
||||||
|
|
@ -176,7 +176,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(b / a for a, b in zip(self.values, other.values))
|
return vector(b / a for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(other / a for a in self.values)
|
return vector(other / a for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(o / a if isinstance(a, value) else other / a for a in self.values)
|
return vector(o / a if isinstance(a, value) else other / a for a in self.values)
|
||||||
|
|
||||||
@overload
|
@overload
|
||||||
|
|
@ -220,7 +220,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a > b for a, b in zip(self.values, other.values))
|
return vector(a > b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a > other for a in self.values)
|
return vector(a > other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a > o if isinstance(a, value) else a > other for a in self.values)
|
return vector(a > o if isinstance(a, value) else a > other for a in self.values)
|
||||||
|
|
||||||
def __lt__(self, other: VecNumLike) -> 'vector[int]':
|
def __lt__(self, other: VecNumLike) -> 'vector[int]':
|
||||||
|
|
@ -229,7 +229,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a < b for a, b in zip(self.values, other.values))
|
return vector(a < b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a < other for a in self.values)
|
return vector(a < other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a < o if isinstance(a, value) else a < other for a in self.values)
|
return vector(a < o if isinstance(a, value) else a < other for a in self.values)
|
||||||
|
|
||||||
def __ge__(self, other: VecNumLike) -> 'vector[int]':
|
def __ge__(self, other: VecNumLike) -> 'vector[int]':
|
||||||
|
|
@ -238,7 +238,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a >= b for a, b in zip(self.values, other.values))
|
return vector(a >= b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a >= other for a in self.values)
|
return vector(a >= other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a >= o if isinstance(a, value) else a >= other for a in self.values)
|
return vector(a >= o if isinstance(a, value) else a >= other for a in self.values)
|
||||||
|
|
||||||
def __le__(self, other: VecNumLike) -> 'vector[int]':
|
def __le__(self, other: VecNumLike) -> 'vector[int]':
|
||||||
|
|
@ -247,7 +247,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a <= b for a, b in zip(self.values, other.values))
|
return vector(a <= b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a <= other for a in self.values)
|
return vector(a <= other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a <= o if isinstance(a, value) else a <= other for a in self.values)
|
return vector(a <= o if isinstance(a, value) else a <= other for a in self.values)
|
||||||
|
|
||||||
def __eq__(self, other: VecNumLike | Sequence[int | float]) -> 'vector[int]': # type: ignore
|
def __eq__(self, other: VecNumLike | Sequence[int | float]) -> 'vector[int]': # type: ignore
|
||||||
|
|
@ -256,7 +256,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a == b for a, b in zip(self.values, other))
|
return vector(a == b for a, b in zip(self.values, other))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a == other for a in self.values)
|
return vector(a == other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a == o if isinstance(a, value) else a == other for a in self.values)
|
return vector(a == o if isinstance(a, value) else a == other for a in self.values)
|
||||||
|
|
||||||
def __ne__(self, other: VecNumLike) -> 'vector[int]': # type: ignore
|
def __ne__(self, other: VecNumLike) -> 'vector[int]': # type: ignore
|
||||||
|
|
@ -265,7 +265,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(a != b for a, b in zip(self.values, other.values))
|
return vector(a != b for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(a != other for a in self.values)
|
return vector(a != other for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(a != o if isinstance(a, value) else a != other for a in self.values)
|
return vector(a != o if isinstance(a, value) else a != other for a in self.values)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -307,7 +307,7 @@ class vector(Generic[TNum]):
|
||||||
return vector(func(a, b) for a, b in zip(self.values, other.values))
|
return vector(func(a, b) for a, b in zip(self.values, other.values))
|
||||||
if isinstance(other, value):
|
if isinstance(other, value):
|
||||||
return vector(func(a, other) for a in self.values)
|
return vector(func(a, other) for a in self.values)
|
||||||
o = value(other, volatile=False) # Make sure a single constant is allocated
|
o = value(other) # Make sure a single constant is allocated
|
||||||
return vector(func(a, o) if isinstance(a, value) else a + other for a in self.values)
|
return vector(func(a, o) if isinstance(a, value) else a + other for a in self.values)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue