# checking derivatives with first-order finite differences import robot import numpy as np h = 1e-6; m = 8; for k in range(0,20): x0 = 2*np.random.rand(1,m)-1 x0 = x0.reshape(m) [f0,df] = robot.fun(x0) df_fd = np.zeros(m) for i in range(0,m): e = np.zeros(m) e[i] = 1 [step,dif] = robot.fun(x0+h*e) df_fd[i] = (step-f0)/h print("ROBOT: Norm of fd error:",np.linalg.norm(df-df_fd))
import matplotlib.pyplot as plt import robot # Set the number of parameter (m) m = 8 # Set the dimension of the active subspace (n) n = 2 # Set the number of points per dimension for Gauss Legendre Quadrature k = 7 # Compute ex0act solution for M randomly selected points M = 100000 x0 = 2*np.random.rand(M,m)-1 f = np.zeros(M) df = np.zeros((M,m)) for i in range(0,M): sample = x0[i,:].reshape(m) [out, gradout] = robot.fun(sample) f[i] = out df[i,:] = gradout.T #Gauss Legendre Quadrature of the C matrix xx = (np.ones(m)*k).astype(np.int64).tolist() [x,w] = active_subspaces.utils.quadrature.gauss_legendre(xx) C = np.zeros((m,m)) N = np.size(w) for i in range(0,N): [Out,Gradout] = robot.fun(x[i,:]) C = C + np.outer(Gradout,Gradout)*w[i] # Eigenvalue decomposition [evals,WW] = np.linalg.eig(C) # Ordering eigenvalues in decending order order = np.argsort(evals)