def test_naca_grad(): nprocesses = max(1, multiprocessing.cpu_count() - 1) try: travis = os.environ['TRAVIS'] == 'true' except KeyError: travis = False if travis: # We limit number of dimensions and iteration count # so entire test takes less than 10min on Travis-CI naca = psdr.demos.NACA0012(n_upper=1, n_lower=1, verbose=True, maxiter=200, nprocesses=1) x = 0.2 * np.ones(len(naca.domain)) err = check_jacobian(x, naca.eval, naca.grad, hvec=[1e-4]) print('maximum error', err) assert err < 5e-2 else: naca = psdr.demos.NACA0012(n_upper=1, n_lower=1, verbose=True, maxiter=1000, nprocesses=nprocesses) x = 0.2 * np.ones(len(naca.domain)) err = check_jacobian(x, naca.eval, naca.grad, hvec=[1e-4]) print('maximum error', err) assert err < 5e-4
def test_der(m=3, p=5, M=10): X = np.random.randn(M, m) bases = [ MonomialTensorBasis(m, p), LegendreTensorBasis(m, p), ChebyshevTensorBasis(m, p), LaguerreTensorBasis(m, p), HermiteTensorBasis(m, p), ] for basis in bases: obj = lambda x: basis.V(x.reshape(1, -1)).reshape(-1) grad = lambda x: basis.DV(x.reshape(1, -1)).reshape(-1, m) assert check_jacobian(X[0], obj, grad) < 1e-7 basis.set_scale(X) obj = lambda x: basis.V(x.reshape(1, -1)).reshape(-1) grad = lambda x: basis.DV(x.reshape(1, -1)).reshape(-1, m) assert check_jacobian(X[0], obj, grad) < 1e-7
def test_jacobian(m, n, nout, Basis, degree): np.random.seed(0) M = 1000 U = np.random.randn(m, n) X = np.random.uniform(-1, 1, size=(M, m)) fX = np.random.randn(M, nout) r = _varpro_residual(U, X, fX, Basis, degree) J = _varpro_jacobian(U, X, fX, Basis, degree) res = lambda u: _varpro_residual(u.reshape(m, n), X, fX, Basis, degree) jac = lambda u: _varpro_jacobian(u.reshape(m, n), X, fX, Basis, degree) err = check_jacobian(U.flatten(), res, jac, hvec=[1e-6]) assert err < 1e-5
def test_jacobian_fixed(m, n, nf, nout, Basis, degree): np.random.seed(0) M = 1000 A = np.random.randn(m, m) U, _ = np.linalg.qr(A, mode='complete') Uf = np.copy(U[:, :nf]) Uc = np.copy(U[:, nf:]) mm = Uc.shape[1] U, _ = np.linalg.qr(np.random.randn(mm, n)) X = np.random.uniform(-1, 1, size=(M, m)) fX = np.random.randn(M, nout) res = lambda u: _varpro_residual_fixed(u.reshape(mm, n), X, fX, Basis, degree, Uf, Uc) jac = lambda u: _varpro_jacobian_fixed(u.reshape(mm, n), X, fX, Basis, degree, Uf, Uc) J = jac(U.flatten()) err = check_jacobian(U.flatten(), res, jac, hvec=[1e-6]) assert err < 1e-6