X *= 4 X **= 2 return X def alloc_norm_mul_opt(A, B): """Take two matrices, A and B, - normalize their rows by dividing with their sums - do a polynomial transformation on each - multiply them and return the result. """ A = A.copy() B = B.copy() A /= A.sum(axis=1)[:, np.newaxis] B /= B.sum(axis=1)[: ,np.newaxis] A = _poly_opt(A) B = _poly_opt(B) return A.dot(B) if __name__ == '__main__': from alloc_norm_mul import alloc_norm_mul A = np.random.random((500, 20)) B = np.random.random((20, 300)) np.testing.assert_allclose(alloc_norm_mul(A, B), alloc_norm_mul_opt(A, B))
X *= 4 X **= 2 return X def alloc_norm_mul_opt(A, B): """Take two matrices, A and B, - normalize their rows by dividing with their sums - do a polynomial transformation on each - multiply them and return the result. """ A = A.copy() B = B.copy() A /= A.sum(axis=1)[:, np.newaxis] B /= B.sum(axis=1)[:, np.newaxis] A = _poly_opt(A) B = _poly_opt(B) return A.dot(B) if __name__ == '__main__': from alloc_norm_mul import alloc_norm_mul A = np.random.random((500, 20)) B = np.random.random((20, 300)) np.testing.assert_allclose(alloc_norm_mul(A, B), alloc_norm_mul_opt(A, B))
import numpy as np from alloc_norm_mul import alloc_norm_mul rng = np.random.RandomState(0) # Sorry, Josh, -1 doesn't work A = rng.uniform(size=((500, 100))) * 100 B = rng.uniform(size=((100, 60))) * 500 alloc_norm_mul(A, B)