def _test_gen_schur(dtype): rand = _Random() a = rand.randmat(5, 5, dtype) b = rand.randmat(5, 5, dtype) s, t, q, z = gen_schur(a, b)[:4] assert_array_almost_equal(dtype, np.dot(np.dot(q, s), z.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q, t), z.T.conj()), b)
def _test_evecs_from_gen_schur(dtype): rand = _Random() a = rand.randmat(5, 5, dtype) b = rand.randmat(5, 5, dtype) s, t, q, z, alpha, beta = gen_schur(a, b) vl, vr = evecs_from_gen_schur(s, t, q, z, select=None, left=True, right=True) assert_array_almost_equal(dtype, np.dot(a, np.dot(vr, np.diag(beta))), np.dot(b, np.dot(vr, np.diag(alpha)))) assert_array_almost_equal( dtype, np.dot(np.dot(np.diag(beta), vl.T.conj()), a), np.dot(np.dot(np.diag(alpha), vl.T.conj()), b)) select = np.array([True, True, False, False, False], dtype=bool) vl, vr = evecs_from_gen_schur(s, t, q, z, select, left=True, right=True) assert vr.shape[1] == 2 assert vl.shape[1] == 2 assert_array_almost_equal( dtype, np.dot(a, np.dot(vr, np.diag(beta[select]))), np.dot(b, np.dot(vr, np.diag(alpha[select])))) assert_array_almost_equal( dtype, np.dot(np.dot(np.diag(beta[select]), vl.T.conj()), a), np.dot(np.dot(np.diag(alpha[select]), vl.T.conj()), b)) vl, vr = evecs_from_gen_schur(s, t, q, z, lambda i: i < 2, left=True, right=True) assert vr.shape[1] == 2 assert vl.shape[1] == 2 assert_array_almost_equal( dtype, np.dot(a, np.dot(vr, np.diag(beta[select]))), np.dot(b, np.dot(vr, np.diag(alpha[select])))) assert_array_almost_equal( dtype, np.dot(np.dot(np.diag(beta[select]), vl.T.conj()), a), np.dot(np.dot(np.diag(alpha[select]), vl.T.conj()), b))
def _test_evecs_from_gen_schur(dtype): rand = _Random() a = rand.randmat(5, 5, dtype) b = rand.randmat(5, 5, dtype) s, t, q, z, alpha, beta = gen_schur(a, b) vl, vr = evecs_from_gen_schur(s, t, q, z , select=None, left=True, right=True) assert_array_almost_equal(dtype, np.dot(a, np.dot(vr, np.diag(beta))), np.dot(b, np.dot(vr, np.diag(alpha)))) assert_array_almost_equal(dtype, np.dot(np.dot(np.diag(beta), vl.T.conj()), a), np.dot(np.dot(np.diag(alpha), vl.T.conj()), b)) select = np.array([True, True, False, False, False], dtype=bool) vl, vr = evecs_from_gen_schur(s, t, q, z, select, left=True, right=True) assert_equal(vr.shape[1], 2) assert_equal(vl.shape[1], 2) assert_array_almost_equal(dtype, np.dot(a, np.dot(vr, np.diag(beta[select]))), np.dot(b, np.dot(vr, np.diag(alpha[select])))) assert_array_almost_equal(dtype, np.dot(np.dot(np.diag(beta[select]), vl.T.conj()), a), np.dot(np.dot(np.diag(alpha[select]), vl.T.conj()), b)) vl, vr = evecs_from_gen_schur(s, t, q, z, lambda i: i<2, left=True, right=True) assert_equal(vr.shape[1], 2) assert_equal(vl.shape[1], 2) assert_array_almost_equal(dtype, np.dot(a, np.dot(vr, np.diag(beta[select]))), np.dot(b, np.dot(vr, np.diag(alpha[select])))) assert_array_almost_equal(dtype, np.dot(np.dot(np.diag(beta[select]), vl.T.conj()), a), np.dot(np.dot(np.diag(alpha[select]), vl.T.conj()), b))
def _test_convert_r2c_gen_schur(dtype): rand = _Random() a = rand.randmat(10, 10, dtype) b = rand.randmat(10, 10, dtype) s, t, q, z = gen_schur(a, b)[:4] s2, t2, q2, z2 = convert_r2c_gen_schur(s, t, q, z) assert_array_almost_equal(dtype, np.dot(np.dot(q, s), z.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q, t), z.T.conj()), b) assert_array_almost_equal(dtype, np.dot(np.dot(q2, s2), z2.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q2, t2), z2.T.conj()), b)
def _test_order_gen_schur(dtype): rand = _Random() a = rand.randmat(10, 10, dtype) b = rand.randmat(10, 10, dtype) s, t, q, z, alpha, beta = gen_schur(a, b) s2, t2, q2, z2, alpha2, beta2 = order_gen_schur( lambda i: i > 2 and i < 7, s, t, q, z) assert_array_almost_equal(dtype, np.dot(np.dot(q, s), z.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q, t), z.T.conj()), b) assert_array_almost_equal(dtype, np.dot(np.dot(q2, s2), z2.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q2, t2), z2.T.conj()), b) #Sorting here is a bit tricky: For real matrices we expect #for complex conjugated pairs identical real parts - however #that seems messed up (only an error on the order of machine precision) #in the division. The solution here is to sort and compare the real #and imaginary parts separately. The only error that would not be #catched in this comparison is if the real and imaginary parts would #be assembled differently in the two arrays - an error that is highly #unlikely. assert_array_almost_equal(dtype, np.sort((alpha / beta).real), np.sort((alpha2 / beta2).real)) assert_array_almost_equal(dtype, np.sort((alpha / beta).imag), np.sort((alpha2 / beta2).imag)) assert_array_almost_equal(dtype, np.sort( (alpha[3:7] / beta[3:7]).real), np.sort((alpha2[:4] / beta2[:4]).real)) assert_array_almost_equal(dtype, np.sort( (alpha[3:7] / beta[3:7]).imag), np.sort((alpha2[:4] / beta2[:4]).imag)) sel = [False, False, 0, True, True, True, 1, False, False, False] s3, t3, q3, z3 = order_gen_schur(sel, s, t, q, z)[:4] assert_array_almost_equal(dtype, np.dot(np.dot(q3, s3), z3.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q3, t3), z3.T.conj()), b) assert_array_almost_equal(dtype, s2, s3) assert_array_almost_equal(dtype, t2, t3) assert_array_almost_equal(dtype, q2, q3) assert_array_almost_equal(dtype, z2, z3)
def _test_order_gen_schur(dtype): rand = _Random() a = rand.randmat(10, 10, dtype) b = rand.randmat(10, 10, dtype) s, t, q, z, alpha, beta = gen_schur(a, b) s2, t2, q2, z2, alpha2, beta2 = order_gen_schur(lambda i: i>2 and i<7, s, t, q, z) assert_array_almost_equal(dtype, np.dot(np.dot(q, s), z.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q, t), z.T.conj()), b) assert_array_almost_equal(dtype, np.dot(np.dot(q2, s2), z2.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q2, t2), z2.T.conj()), b) #Sorting here is a bit tricky: For real matrices we expect #for complex conjugated pairs identical real parts - however #that seems messed up (only an error on the order of machine precision) #in the division. The solution here is to sort and compare the real #and imaginary parts separately. The only error that would not be #catched in this comparison is if the real and imaginary parts would #be assembled differently in the two arrays - an error that is highly #unlikely. assert_array_almost_equal(dtype, np.sort((alpha/beta).real), np.sort((alpha2/beta2).real)) assert_array_almost_equal(dtype, np.sort((alpha/beta).imag), np.sort((alpha2/beta2).imag)) assert_array_almost_equal(dtype, np.sort((alpha[3:7]/beta[3:7]).real), np.sort((alpha2[:4]/beta2[:4]).real)) assert_array_almost_equal(dtype, np.sort((alpha[3:7]/beta[3:7]).imag), np.sort((alpha2[:4]/beta2[:4]).imag)) sel = [False, False, 0, True, True, True, 1, False, False, False] s3, t3, q3, z3 = order_gen_schur(sel, s, t, q, z)[:4] assert_array_almost_equal(dtype, np.dot(np.dot(q3, s3), z3.T.conj()), a) assert_array_almost_equal(dtype, np.dot(np.dot(q3, t3), z3.T.conj()), b) assert_array_almost_equal(dtype, s2, s3) assert_array_almost_equal(dtype, t2, t3) assert_array_almost_equal(dtype, q2, q3) assert_array_almost_equal(dtype, z2, z3)