def spectral_radius_bound(X, log2_exponent): """ Returns upper bound on the largest eigenvalue of square symmetrix matrix X. log2_exponent must be a positive-valued integer. The larger it is, the slower and tighter the bound. Values up to 5 should usually suffice. The algorithm works by multiplying X by itself this many times. From V.Pan, 1990. "Estimating the Extremal Eigenvalues of a Symmetric Matrix", Computers Math Applic. Vol 20 n. 2 pp 17-22. Rq: an efficient algorithm, not used here, is defined in this paper. """ if X.type.ndim != 2: raise TypeError("spectral_radius_bound requires a matrix argument", X) if not isinstance(log2_exponent, int): raise TypeError("spectral_radius_bound requires an integer exponent", log2_exponent) if log2_exponent <= 0: raise ValueError( "spectral_radius_bound requires a strictly positive " "exponent", log2_exponent, ) XX = X for i in range(log2_exponent): XX = dot(XX, XX) return aet_pow(trace(XX), 2**(-log2_exponent))
def test_trace(): rng = np.random.RandomState(utt.fetch_seed()) x = matrix() g = trace(x) f = aesara.function([x], g) for shp in [(2, 3), (3, 2), (3, 3)]: m = rng.rand(*shp).astype(config.floatX) v = np.trace(m) assert v == f(m) xx = vector() ok = False try: trace(xx) except TypeError: ok = True except ValueError: ok = True assert ok