From d3d06722c134874515e622cc12e3035c8a2a04a1 Mon Sep 17 00:00:00 2001 From: stefan Date: Fri, 15 May 2026 17:20:00 +0900 Subject: [PATCH] faster mv xor and new merge fct --- src/kyupy/logic.py | 52 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/src/kyupy/logic.py b/src/kyupy/logic.py index c850e27..644189e 100644 --- a/src/kyupy/logic.py +++ b/src/kyupy/logic.py @@ -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): """Converts characters, strings, and lists of them to lists of logic constants defined above. @@ -191,19 +214,26 @@ def mv_and(x1, x2, out=None): 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) +def mv_xor(x1, x2, out=None): + """A multi-valued XOR operator. - 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) + :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_xor[x1, x2] + return out -def mv_xor(x1, x2, out=None): - """A multi-valued XOR operator. +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. @@ -211,7 +241,7 @@ def mv_xor(x1, x2, out=None): :return: A multi-valued array with the result. """ out = out or np.empty(np.broadcast(x1, x2).shape, dtype=np.uint8) - _mv_xor(out, x1, x2) + out[...] = _mv_merge[x1, x2] return out