def rte_mu(x): mu = x.reshape(n_layer, n_frequency) rTE = rTE_forward(frequencies, lamb, sigma, mu, thicknesses) _, _, J_mu = rTE_gradient(frequencies, lamb, sigma, mu, thicknesses) def J(y): y = y.reshape(n_layer, n_frequency) # do summation over layers, broadcast over frequencies only return np.einsum('i...k,i...', J_mu, y) return rTE, J
def rte_h(x): thicknesses = x rTE = rTE_forward(frequencies, lamb, sigma, mu, thicknesses) _, J_h, _ = rTE_gradient(frequencies, lamb, sigma, mu, thicknesses) def J(y): #(J_h.T@y).T out = np.einsum('i...,i', J_h, y) # do sum over n_layer, broadcast over all others return out return rTE, J
def rte_sigma(x): sigma = x.reshape(n_layer, n_frequency) rTE = rTE_forward(frequencies, lamb, sigma, mu, thicknesses) J_sigma, _, _ = rTE_gradient(frequencies, lamb, sigma, mu, thicknesses) def J(y): y = y.reshape(n_layer, n_frequency) # do summation over layers, broadcast over frequencies only # equivalent to: # out = np.empty_like(rTE) # for i in range(n_frequency): # out[i] = J_sigma[:, i, :]).T@y[:, i] return np.einsum('i...k,i...', J_sigma, y) return rTE, J
def setUp(self): """Test to make sure numpy and compiled give same results""" n_layer = 11 n_frequency = 5 n_lambda = 8 frequencies = np.logspace(-4, -2, n_frequency) thicknesses = np.ones(n_layer-1) lamb = np.logspace(-5, -3, n_lambda) np.random.seed(123) sigma = 1E-1 * (1 + 1.0/(n_layer*n_frequency) * np.arange(n_layer*n_frequency).reshape(n_layer, n_frequency)) mu = mu_0 * (1 + 1.0/(n_layer*n_frequency) * np.arange(n_layer*n_frequency).reshape(n_layer, n_frequency)) self.rTE1 = rTE_forward(frequencies, lamb, sigma, mu, thicknesses) self.rTE2 = _rTE_forward(frequencies, lamb, sigma, mu, thicknesses) self.rTE1_dsigma, self.rTE1_dh, self.rTE1_dmu = rTE_gradient(frequencies, lamb, sigma, mu, thicknesses) self.rTE2_dsigma, self.rTE2_dh, self.rTE2_dmu = _rTE_gradient(frequencies, lamb, sigma, mu, thicknesses)
def test_rTE(self): """Test to make sure numpy and compiled give same results""" n_layer = 11 n_frequency = 5 n_lambda = 8 frequencies = np.logspace(1, 4, 5) thicknesses = np.ones(n_layer - 1) lamb = np.logspace(0, 3, n_lambda) sigma = np.random.rand(n_layer, n_frequency) mu = np.random.rand(n_layer, n_frequency) rTE1 = rTE_forward(frequencies, lamb, sigma, mu, thicknesses) rTE2 = _rTE_forward(frequencies, lamb, sigma, mu, thicknesses) assert_allclose(rTE1, rTE1) rTE1_dsigma, rTE1_dh, rTE1_dmu = rTE_gradient(frequencies, lamb, sigma, mu, thicknesses) rTE2_dsigma, rTE2_dh, rTE2_dmu = _rTE_gradient(frequencies, lamb, sigma, mu, thicknesses) assert_allclose(rTE1_dsigma, rTE2_dsigma) assert_allclose(rTE1_dh, rTE2_dh) assert_allclose(rTE1_dmu, rTE2_dmu)