def expand_Gc(Gc): """Calculate the matricized quadratic operator that operates on the full cubic Kronecker product. Parameters ---------- Gc : (r,s) ndarray The matricized quadratic tensor that operates on the compact cubic Kronecker product. Here s = r * (r+1) * (r+2) / 6. Returns ------- G : (r,r**3) ndarray The matricized quadratic tensor that operates on the full cubic Kronecker product. This is a symmetric operator in the sense that each layer of G.reshape((r,r,r,r)) is a symmetric (r,r,r) matrix. """ r,s = Gc.shape if s != r * (r+1) * (r+2) // 6: raise ValueError(f"invalid shape (r,s) = {(r,s)}" " with s != r(r+1)(r+2)/6") G = _np.empty((r,r**3)) fj = 0 for i in range(r): for j in range(i+1): for k in range(j+1): idxs = set(_permutations((i,j,k),3)) fill = Gc[:,fj] / len(idxs) for a,b,c in idxs: G[:,(a*r**2)+(b*r)+c] = fill fj += 1 # assert fj == s return G
def unique_n_digits(digits, n): """Generate a generator which yields an n-digit pandigital number. A pandigital number has no digits that are the same. 10248 is pandigital while 20883 is not. """ lower = 10 ** (n - 1) - 1 return _dropwhile(lambda num: num <= lower, map(digits_int, _permutations(digits, n)))
def p037(): primes_ = set(primes(10000)) for a in primes_: permutations = tuple(set(map(undigitize, set(_permutations(digitize(a))))).intersection(primes_)) for i in range(1, len(permutations)): b = permutations[i] for j in range(i + 1, len(permutations)): c = permutations[j] if a != 1487 and b in primes_ and c in primes_ and (a + c) // 2 == b: return str(a) + str(b) + str(c)
def forall(self,t="product"): if t.startswith("pr"): _k = list([list(i) for i in _product(self,repeat=2)]) elif t.startswith("pe"): _k = list([list(i) for i in _permutations(self,2)]) elif t.startswith("c") and t.endswith("s"): _k = list([list(i) for i in _combinations(self,2)]) else: _k = list([list(i) for i in _combinations_with_replacement(self,2)]) return lzlist(_k)
def p043(): total = 0 for permutation in _permutations("0123456789"): n = "".join(permutation) if ( int(n[1:4]) % 2 == 0 and int(n[2:5]) % 3 == 0 and int(n[3:6]) % 5 == 0 and int(n[4:7]) % 7 == 0 and int(n[5:8]) % 11 == 0 and int(n[6:9]) % 13 == 0 and int(n[7:10]) % 17 == 0 ): total += int(n) return total
def p032(): for a in range(92): digitized_a = tuple(digitize(a)) if a % 11 == 0 or a % 10 == 0: continue remaining_digits = set(range(1, 10)).difference(digitize(a)) for i in range(2, 5): for permutation in _permutations(sorted(remaining_digits), i): b = undigitize(permutation) c = a * b if c > 98765: break digitized_c = tuple(digitize(c)) if 0 in digitized_c: continue if sorted(digitized_a + tuple(digitize(b)) + digitized_c) == [1, 2, 3, 4, 5, 6, 7, 8, 9]: yield c
def pattern_table(ord): """ Return all ordinal patterns of order `ord` in their rank representation. Parameters ---------- ord : int Pattern order between 2 and 10. Returns ------- tab : ndarray of double 2D array, with each row representing an ordinal pattern in rank its representation. Patterns are sorted in lexicographic order. """ if not is_scalar_int(ord): raise TypeError("order must be a scalar integer") if ord < 1 or ord > 10: raise ValueError("order must be between 2 and 10") tmp = range(1, ord + 1) return _np.asarray([i for i in _permutations(tmp)], dtype=_np.double)
def compress_G(G): """Calculate the matricized cubic operator that operates on the compact cubic Kronecker product. Parameters ---------- G : (r,r**3) ndarray The matricized cubic tensor that operates on the full cubic Kronecker product. This should be a symmetric operator in the sense that each layer of G.reshape((r,r,r,r)) is a symmetric (r,r,r) tensor, but it is not required. Returns ------- Gc : (r,s) ndarray The matricized cubic tensor that operates on the compact cubic Kronecker product. Here s = r * (r+1) * (r+2) / 6. """ r = G.shape[0] r3 = G.shape[1] if r3 != r**3: raise ValueError(f"invalid shape (r,a) = {(r,r3)} with a != r**3") s = r * (r+1) * (r+2) // 6 Gc = _np.empty((r, s)) fj = 0 for i in range(r): for j in range(i+1): for k in range(j+1): Gc[:,fj] = _np.sum([G[:,(a*r**2)+(b*r)+c] for a,b,c in set(_permutations((i,j,k),3))], axis=0) fj += 1 # assert fj == s return Gc
def permutations(r, it): return _permutations(it, r)
def p024(): return ''.join(tuple(_permutations('0123456789'))[999999])
def p041(): for i in range(9, 0, -1): for permutation in _permutations(range(i, 0, -1)): if is_prime(undigitize(permutation)): return undigitize(permutation)