faster mv xor and new merge fct

This commit is contained in:
stefan committed 2026-05-15 17:20:00 +09:00
1 parent 1f984c9bd9
commit d3d06722c1
1 file changed
+42 -12
+42 -12
View File
@@ -81,6 +81,29 @@ on a signal. ``'N'``, ``'n'``, and ``'v'`` are interpreted as ``NPULSE``.
""" """
# 0 X - 1 P R F N
_mv_xor = np.array([[0,1,1,3,4,5,6,7], # 0
[1,1,1,1,1,1,1,1], # X
[1,1,1,1,1,1,1,1], # -
[3,1,1,0,7,6,5,4], # 1
[4,1,1,7,4,5,6,7], # P
[5,1,1,6,5,4,7,6], # R
[6,1,1,5,6,7,4,5], # F
[7,1,1,4,7,6,5,4], # N
], dtype=np.uint8)
# 0 X - 1 P R F N
_mv_merge = np.array([[0,0,0,1,1,1,1,1], # 0
[0,1,1,3,4,5,6,7], # X
[0,1,2,3,4,5,6,7], # -
[1,3,3,3,1,1,1,1], # 1
[1,4,4,1,4,1,1,1], # P
[1,5,5,1,1,5,1,1], # R
[1,6,6,1,1,1,6,1], # F
[1,7,7,1,1,1,1,7], # N
], dtype=np.uint8)
def interpret(value): def interpret(value):
"""Converts characters, strings, and lists of them to lists of logic constants defined above. """Converts characters, strings, and lists of them to lists of logic constants defined above.
@@ -191,17 +214,6 @@ def mv_and(x1, x2, out=None):
return out return out
def _mv_xor(out, *ins):
any_unknown = (ins[0] == UNKNOWN) | (ins[0] == UNASSIGNED)
for inp in ins[1:]: any_unknown |= (inp == UNKNOWN) | (inp == UNASSIGNED)
out[...] = ZERO
for inp in ins:
np.bitwise_xor(out, inp & 0b011, out=out)
np.bitwise_or(out, inp & 0b100, out=out)
np.putmask(out, any_unknown, UNKNOWN)
def mv_xor(x1, x2, out=None): def mv_xor(x1, x2, out=None):
"""A multi-valued XOR operator. """A multi-valued XOR operator.
@@ -211,7 +223,25 @@ def mv_xor(x1, x2, out=None):
:return: A multi-valued array with the result. :return: A multi-valued array with the result.
""" """
out = out or np.empty(np.broadcast(x1, x2).shape, dtype=np.uint8) out = out or np.empty(np.broadcast(x1, x2).shape, dtype=np.uint8)
_mv_xor(out, x1, x2) out[...] = _mv_xor[x1, x2]
return out
def mv_merge(x1, x2, out=None):
"""A multi-valued merge operator.
Merging identical values ``a`` and ``a`` yields ``a``.
Merging ``X`` and ``-`` yields ``X``.
Merging any value ``a`` with ``X`` or ``-`` yields ``a``.
Merging any other values ``a != b`` yields ``X``.
:param x1: A multi-valued array.
:param x2: A multi-valued array.
:param out: An optional storage destination. If None, a new multi-valued array is returned.
:return: A multi-valued array with the result.
"""
out = out or np.empty(np.broadcast(x1, x2).shape, dtype=np.uint8)
out[...] = _mv_merge[x1, x2]
return out return out