def test_propagate_err_unequal_shape_z_dz(): """Tests for ValueError when using unequal arrays for z and dz.""" z = np.array([0, 1, 2, 3, 4, 5]) dz = np.random.normal(size=z.size-1, scale=0.1) msg = r'shape of z:' with pytest.raises(ValueError, match=msg): propagate_err(z, dz, option='abs')
def test_propagate_err_invalid_option(): """Tests for ValueError when using an unsupported option.""" z = np.array([0, 1, 2, 3, 4, 5]) dz = np.random.normal(size=z.size, scale=0.1) option = 'unknown' msg = r'Invalid option' with pytest.raises(ValueError, match=msg): propagate_err(z, dz, option)
def test_propagate_err(option): """Tests for ValueError when using an unsupported option.""" np.random.seed(2020) z = np.array([1.0, 1.0+10j, 2.0, 2.0+20j], dtype='complex') dz = np.random.normal(z.size, scale=0.1)*z # if `z` is real, assume that `dz` is also real and return it as-is err = propagate_err(np.real(z), np.real(dz), option) assert_allclose(err, np.real(dz)) # if `z` is complex, but `dz` is real apply the err to both real/imag err_complex_real = propagate_err(z, np.real(dz), option) assert np.all(np.isreal(err_complex_real)) dz_used = np.real(dz)+1j*np.real(dz) if option == 'real': assert_allclose(err_complex_real, np.real(dz_used)) elif option == 'imag': assert_allclose(err_complex_real, np.imag(dz_used)) elif option == 'abs': assert_allclose(err_complex_real, [3.823115, 3.823115, 7.646231, 7.646231], rtol=1.0e-5) elif option == 'angle': assert_allclose(err_complex_real, [3.823115, 0.380414, 3.823115, 0.380414], rtol=1.0e-5) # both `z` and `dz` are complex err_complex_complex = propagate_err(z, dz, option) assert np.all(np.isreal(err_complex_complex)) if option == 'real': assert_allclose(err_complex_complex, np.real(dz)) elif option == 'imag': assert_allclose(err_complex_complex, np.imag(dz)) elif option == 'abs': assert_allclose(err_complex_complex, [3.823115, 38.043322, 7.646231, 76.086645], rtol=1.0e-5) elif option == 'angle': assert_allclose(err_complex_complex, [0., 0.535317, 0., 0.535317], rtol=1.0e-5)