|
| 1 | +from graphblas import agg, op, operator |
| 2 | + |
| 3 | + |
| 4 | +def get_reduce_to_vector(key, opname, methodname): |
| 5 | + try: |
| 6 | + op_ = op.from_string(opname) |
| 7 | + except ValueError: |
| 8 | + op_ = agg.from_string(opname) |
| 9 | + op_, opclass = operator.find_opclass(op_) |
| 10 | + keybase = key[:-1] |
| 11 | + if key[-1] == "-": |
| 12 | + |
| 13 | + def get_reduction(G, mask=None): |
| 14 | + cache = G._cache |
| 15 | + if mask is not None: |
| 16 | + if key in cache: |
| 17 | + return cache[key].dup(mask=mask) |
| 18 | + elif cache.get("has_self_edges") is False and f"{keybase}+" in cache: |
| 19 | + cache[key] = cache[f"{keybase}+"] |
| 20 | + return cache[key].dup(mask=mask) |
| 21 | + elif "offdiag" in cache: |
| 22 | + return getattr(cache["offdiag"], methodname)(op_).new(mask=mask, name=key) |
| 23 | + elif ( |
| 24 | + "L-" in cache |
| 25 | + and "U-" in cache |
| 26 | + and opclass in {"BinaryOp", "Monoid"} |
| 27 | + and G.get_property("has_self_edges") |
| 28 | + ): |
| 29 | + return op_( |
| 30 | + getattr(cache["L-"], methodname)(op_).new(mask=mask) |
| 31 | + | getattr(cache["U-"], methodname)(op_).new(mask=mask) |
| 32 | + ).new(name=key) |
| 33 | + elif not G.get_property("has_self_edges"): |
| 34 | + return G.get_property(f"{keybase}+", mask=mask) |
| 35 | + else: |
| 36 | + return getattr(G.get_property("offdiag"), methodname)(op_).new( |
| 37 | + mask=mask, name=key |
| 38 | + ) |
| 39 | + if key not in cache: |
| 40 | + if cache.get("has_self_edges") is False and f"{keybase}+" in cache: |
| 41 | + cache[key] = cache[f"{keybase}+"] |
| 42 | + elif "offdiag" in cache: |
| 43 | + cache[key] = getattr(cache["offdiag"], methodname)(op_).new(name=key) |
| 44 | + elif ( |
| 45 | + "L-" in cache |
| 46 | + and "U-" in cache |
| 47 | + and opclass in {"BinaryOp", "Monoid"} |
| 48 | + and G.get_property("has_self_edges") |
| 49 | + ): |
| 50 | + cache[key] = op_( |
| 51 | + getattr(cache["L-"], methodname)(op_) |
| 52 | + | getattr(cache["U-"], methodname)(op_) |
| 53 | + ).new(name=key) |
| 54 | + elif not G.get_property("has_self_edges"): |
| 55 | + cache[key] = G.get_property(f"{keybase}+") |
| 56 | + else: |
| 57 | + cache[key] = getattr(G.get_property("offdiag"), methodname)(op_).new(name=key) |
| 58 | + if ( |
| 59 | + "has_self_edges" not in cache |
| 60 | + and f"{keybase}+" in cache |
| 61 | + and cache[key].nvals != cache[f"{keybase}+"].nvals |
| 62 | + ): |
| 63 | + cache["has_self_edges"] = True |
| 64 | + elif cache.get("has_self_edges") is False: |
| 65 | + cache[f"{keybase}+"] = cache[key] |
| 66 | + return cache[key] |
| 67 | + |
| 68 | + else: |
| 69 | + |
| 70 | + def get_reduction(G, mask=None): |
| 71 | + A = G._A |
| 72 | + cache = G._cache |
| 73 | + if mask is not None: |
| 74 | + if key in cache: |
| 75 | + return cache[key].dup(mask=mask) |
| 76 | + elif cache.get("has_self_edges") is False and f"{keybase}-" in cache: |
| 77 | + cache[key] = cache[f"{keybase}-"] |
| 78 | + return cache[key].dup(mask=mask) |
| 79 | + elif methodname == "reduce_columnwise" and "AT" in cache: |
| 80 | + return cache["AT"].reduce_rowwise(op_).new(mask=mask, name=key) |
| 81 | + else: |
| 82 | + return getattr(A, methodname)(op_).new(mask=mask, name=key) |
| 83 | + if key not in cache: |
| 84 | + if cache.get("has_self_edges") is False and f"{keybase}-" in cache: |
| 85 | + cache[key] = cache[f"{keybase}-"] |
| 86 | + elif methodname == "reduce_columnwise" and "AT" in cache: |
| 87 | + cache[key] = cache["AT"].reduce_rowwise(op_).new(name=key) |
| 88 | + else: |
| 89 | + cache[key] = getattr(A, methodname)(op_).new(name=key) |
| 90 | + if ( |
| 91 | + "has_self_edges" not in cache |
| 92 | + and f"{keybase}-" in cache |
| 93 | + and cache[key].nvals != cache[f"{keybase}-"].nvals |
| 94 | + ): |
| 95 | + cache["has_self_edges"] = True |
| 96 | + elif cache.get("has_self_edges") is False: |
| 97 | + cache[f"{keybase}-"] = cache[key] |
| 98 | + return cache[key] |
| 99 | + |
| 100 | + return get_reduction |
| 101 | + |
| 102 | + |
| 103 | +def get_reduce_to_scalar(key, opname): |
| 104 | + try: |
| 105 | + op_ = op.from_string(opname) |
| 106 | + except ValueError: |
| 107 | + op_ = agg.from_string(opname) |
| 108 | + op_, opclass = operator.find_opclass(op_) |
| 109 | + keybase = key[:-1] |
| 110 | + if key[-1] == "-": |
| 111 | + |
| 112 | + def get_reduction(G, mask=None): |
| 113 | + cache = G._cache |
| 114 | + if key not in cache: |
| 115 | + if cache.get("has_self_edges") is False and f"{keybase}+" in cache: |
| 116 | + cache[key] = cache[f"{keybase}+"] |
| 117 | + elif f"{opname}_rowwise-" in cache: |
| 118 | + cache[key] = cache[f"{opname}_rowwise-"].reduce(op_).new(name=key) |
| 119 | + elif f"{opname}_columnwise-" in cache: |
| 120 | + cache[key] = cache[f"{opname}_columnwise-"].reduce(op_).new(name=key) |
| 121 | + elif cache.get("has_self_edges") is False and f"{opname}_rowwise+" in cache: |
| 122 | + cache[key] = cache[f"{opname}_rowwise+"].reduce(op_).new(name=key) |
| 123 | + elif cache.get("has_self_edges") is False and f"{opname}_columnwise+" in cache: |
| 124 | + cache[key] = cache[f"{opname}_columnwise+"].reduce(op_).new(name=key) |
| 125 | + elif "offdiag" in cache: |
| 126 | + cache[key] = cache["offdiag"].reduce_scalar(op_).new(name=key) |
| 127 | + elif ( |
| 128 | + "L-" in cache |
| 129 | + and "U-" in cache |
| 130 | + and opclass in {"BinaryOp", "Monoid"} |
| 131 | + and G.get_property("has_self_edges") |
| 132 | + ): |
| 133 | + return op_( |
| 134 | + cache["L-"].reduce(op_)._as_vector() | cache["U-"].reduce(op_)._as_vector() |
| 135 | + )[0].new(name=key) |
| 136 | + elif not G.get_property("has_self_edges"): |
| 137 | + cache[key] = G.get_property(f"{keybase}+") |
| 138 | + else: |
| 139 | + cache[key] = G.get_property("offdiag").reduce_scalar(op_).new(name=key) |
| 140 | + if ( |
| 141 | + "has_self_edges" not in cache |
| 142 | + and f"{keybase}+" in cache |
| 143 | + and cache[key] != cache[f"{keybase}+"] |
| 144 | + ): |
| 145 | + cache["has_self_edges"] = True |
| 146 | + elif cache.get("has_self_edges") is False: |
| 147 | + cache[f"{keybase}+"] = cache[key] |
| 148 | + return cache[key] |
| 149 | + |
| 150 | + else: |
| 151 | + |
| 152 | + def get_reduction(G, mask=None): |
| 153 | + A = G._A |
| 154 | + cache = G._cache |
| 155 | + if key not in cache: |
| 156 | + if cache.get("has_self_edges") is False and f"{keybase}-" in cache: |
| 157 | + cache[key] = cache[f"{keybase}-"] |
| 158 | + elif f"{opname}_rowwise+" in cache: |
| 159 | + cache[key] = cache[f"{opname}_rowwise+"].reduce(op_).new(name=key) |
| 160 | + elif f"{opname}_columnwise+" in cache: |
| 161 | + cache[key] = cache[f"{opname}_columnwise+"].reduce(op_).new(name=key) |
| 162 | + elif cache.get("has_self_edges") is False and f"{opname}_rowwise-" in cache: |
| 163 | + cache[key] = cache[f"{opname}_rowwise-"].reduce(op_).new(name=key) |
| 164 | + elif cache.get("has_self_edges") is False and f"{opname}_columnwise-" in cache: |
| 165 | + cache[key] = cache[f"{opname}_columnwise-"].reduce(op_).new(name=key) |
| 166 | + else: |
| 167 | + cache[key] = A.reduce_scalar(op_).new(name=key) |
| 168 | + if ( |
| 169 | + "has_self_edges" not in cache |
| 170 | + and f"{keybase}-" in cache |
| 171 | + and cache[key] != cache[f"{keybase}-"] |
| 172 | + ): |
| 173 | + cache["has_self_edges"] = True |
| 174 | + elif cache.get("has_self_edges") is False: |
| 175 | + cache[f"{keybase}-"] = cache[key] |
| 176 | + return cache[key] |
| 177 | + |
| 178 | + return get_reduction |
0 commit comments