def test_Transfer_to_array(): G = Transfer(1, [1, 1]) H = Transfer(2, 10) with assert_raises(ValueError): G.to_array() assert_equal(H.to_array(), np.array([[.2]])) assert_equal(Transfer(np.arange(9, 90, 9).reshape(3, 3), 9*np.ones((3, 3))).to_array(), np.arange(1, 10).reshape(3, 3))
def test_Transfer_neg_add_radd_static_simo_miso(): # See gh-68 tf1 = Transfer([[[0.0], [1.0]]], 1., 0.02) tf2 = Transfer([[[1.0], [0.5]]], 1., 0.02) tf = tf1 + tf2 assert_allclose(tf.to_array(), np.array([[1., 1.5]])) tf1 = Transfer([[[0.0]], [[1.0]]], 1., 0.02) tf2 = Transfer([[[1.0]], [[0.5]]], 1., 0.02) tf = tf1 + tf2 assert_allclose(tf.to_array(), np.array([[1.], [1.5]]))
def test_Transfer_algebra_matmul_rmatmul(): G = Transfer([[1, [1, 1]]], [[[1, 2, 1], [1, 1]]]) H = Transfer([[[1, 3]], [1]], [1, 2, 1]) F = G @ H assert_almost_equal(F.num, np.array([[1, 3, 4]])) assert_almost_equal(F.den, np.array([[1, 4, 6, 4, 1]])) F = H @ G assert_almost_equal(F.num[0][0], np.array([[1, 3]])) assert_almost_equal(F.num[0][1], np.array([[1, 4, 3]])) assert_almost_equal(F.num[1][0], np.array([[1]])) assert_almost_equal(F.num[1][1], np.array([[1, 1]])) assert_almost_equal(F.den[0][0], np.array([[1, 4, 6, 4, 1]])) assert_almost_equal(F.den[0][1], np.array([[1, 3, 3, 1]])) assert_almost_equal(F.den[1][0], F.den[0][0]) assert_almost_equal(F.den[1][1], F.den[0][1]) F = Transfer(2) @ Transfer(np.eye(2)) @ Transfer(2) assert_equal(F.to_array(), 4*np.eye(2)) G = Transfer([[1, 2]], [1, 1]) H = np.array([[2], [1]]) @ G assert_array_equal(H.num[0][0], np.array([[2.]])) assert_array_equal(H.num[0][1], np.array([[4.]])) assert_array_equal(H.num[1][0], np.array([[1.]])) assert_array_equal(H.num[1][1], np.array([[2.]])) G = Transfer([[1, 2]], [1, 1]) H = G @ np.array([[2], [1]]) assert H._isSISO assert_array_almost_equal(H.num, np.array([[4.]])) assert_array_almost_equal(H.den, np.array([[1., 1.]])) H = np.array([[2]]) @ G assert_array_equal(H.num[0][0], np.array([[2.]])) assert_array_equal(H.num[0][1], np.array([[4.]])) with assert_raises(ValueError): H = np.array([2+1j, 1]) * G J = H*0. assert_array_equal(J.num[0][0], np.array([[0.]])) assert_array_equal(J.num[0][1], np.array([[0.]])) assert_array_equal(J.den[0][0], np.array([[1.]])) assert_array_equal(J.den[0][1], np.array([[1.]])) G = Transfer(1, [1, 1]) H = G*0. assert_array_equal(H.num, np.array([[0.]])) assert_array_equal(H.den, np.array([[1.]]))
def test_verbosity_Transfer(capsys): _ = Transfer.validate_arguments(-2*np.eye(2), [1, 2, 3], verbose=True) out, err = capsys.readouterr() assert not err.strip() assert out == ("========================================\n" "Handling numerator\n" "========================================\n" "I found a numpy array\n" "The array has multiple elements\n" "========================================\n" "Handling denominator\n" "========================================\n" "I found a list\n" "I found a list that has only scalars\n" "==================================================\n" "Handling raw entries are done.\n" "Now checking the SISO/MIMO context and regularization.\n" "==================================================\n" "One of the MIMO flags are true\n" "Numerator is MIMO, Denominator is something else\n" "Numerator is MIMO, Denominator is SISO\n")
def test_basic_pole_properties(): G = Transfer(0.5, [1, 4, 3]) + 5 zzz = G.pole_properties() assert_array_almost_equal(zzz, np.array([[-1.+0.j, 1.+0.j, 1.+0.j], [-3.+0.j, 3.+0.j, 1.+0.j]]))
def test_Transfer_property(): G = Transfer([1, 1], [1, 1, 1]) assert G.DiscretizedWith is None G.SamplingPeriod = 0.1 G.DiscretizedWith = 'zoh' assert G.DiscretizedWith == 'zoh' G = Transfer([1, 1], [1, 1, 1]) G.num = [1, 2, 1] with assert_raises(IndexError): G.num = [[1, [1, 2]]] G.den = [1, 2, 1] with assert_raises(IndexError): G.den = [[[1, 2, 3], [1, 2, 5]]] with assert_raises(ValueError): G.DiscretizedWith = 'zoh' with assert_raises(ValueError): G.DiscretizationMatrix = 1. G = Transfer([0.1, 0.1, -0.5], [1, 1.3, 0.43], 0.1) with assert_raises(ValueError): G.DiscretizedWith = 'some dummy method' G.DiscretizedWith = 'lft' G.DiscretizationMatrix = np.array([[1, 2], [1.5, 5.]]) # dummy array assert_array_equal(G.DiscretizationMatrix, np.array([[1, 2], [1.5, 5.]])) with assert_raises(ValueError): G.DiscretizationMatrix = [1., 1.] with assert_raises(ValueError): G.PrewarpFrequency = 200 G = Transfer([1, 1], [1, 1, 1], dt=0.1) G.DiscretizedWith = 'tustin' G.PrewarpFrequency = 0.02 assert G.PrewarpFrequency == 0.02