def test_block_matrix_5(self): # I identity matrix la_str = """B = [ A C I ] where A: ℝ ^ (2 × 2): a matrix C: ℝ ^ (2 × 2): a matrix """ func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) B = np.array([[1, 2, 1, 2, 1, 0], [3, 4, 3, 4, 0, 1]]) self.assertDMatrixEqual(func_info.numpy_func(A, A).B, B) # MATLAB test # if TEST_MATLAB: # mat_func = getattr(mat_engine, func_info.mat_func_name, None) # self.assertDMatrixEqual(np.array(mat_func(matlab.double(A.tolist()), matlab.double(A.tolist()))['B']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 6> B;", " B << 1, 2, 1, 2, 1, 0, 3, 4, 3, 4, 0, 1;", " Eigen::Matrix<double, 2, 6> C = {}(A, A).B;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_elementwise_matrix(self): # scalar value la_str = """B_i,j = A_j,i where A: ℝ^(2 × 3) """ func_info = self.gen_func_info(la_str) A = np.array([[1, 3, 4], [2, 0, 1]]) B = np.array([[1, 2], [3, 0], [4, 1]]) self.assertDMatrixEqual(func_info.numpy_func(A).B, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(A.tolist()))['B']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 3> A;", " A << 1, 3, 4, 2, 0, 1;", " Eigen::Matrix<double, 3, 2> B;", " B << 1, 2, 3, 0, 4, 1;", " Eigen::Matrix<double, 3, 2> C = {}(A).B;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_backtick_function(self): la_str = """`Output` = `Parameters` `Minimize`(`Parameters`) where `Parameters`: ℝ ^ (2 × 2): a matrix `Minimize`: ℝ^(2 × 2) -> ℝ^(2 × 2): a function """ func_info = self.gen_func_info(la_str) P = np.array([[1, 2], [4, 3]]) f = lambda p: p + p A = np.array([[18, 16], [32, 34]]) self.assertDMatrixEqual(func_info.numpy_func(P, f).Output, A) # MATLAB test # if TEST_MATLAB: # mat_func = getattr(mat_engine, func_info.mat_func_name, None) # self.assertDMatrixEqual(np.array(mat_func(matlab.double(P.tolist()))['Output']), A) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> P;", " P << 1, 2, 4, 3;", " Eigen::Matrix<double, 2, 2> A;", " A << 18, 16, 32, 34;", " std::function<Eigen::Matrix<double, 2, 2>(Eigen::Matrix<double, 2, 2>)> f;" " f = [](Eigen::Matrix<double, 2, 2> p)->Eigen::Matrix<double, 2, 2>{", " return p + p;" " };", " Eigen::Matrix<double, 2, 2> B = {}(P, f).Output;".format( func_info.eig_func_name), " return ((B - A).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_block_matrix_2(self): # expression as item la_str = """C = [A+B A-B] where A: ℝ ^ (2 × 2): a matrix B: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) C = np.array([[6, 8, -4, -4], [10, 12, -4, -4]]) self.assertDMatrixEqual(func_info.numpy_func(A, B).C, C) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array( mat_func(matlab.double(A.tolist()), matlab.double(B.tolist()))['C']), C) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << 5, 6, 7, 8;", " Eigen::Matrix<double, 2, 4> C;", " C << 6, 8, -4, -4, 10, 12, -4, -4;", " Eigen::Matrix<double, 2, 4> D = {}(A, B).C;".format( func_info.eig_func_name), " return ((D - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_sparse_block_matrix_1(self): # sparse matrix in block matrix la_str = """[ A 0₂,₂ 0_2,2 I] where A: ℝ ^ (2 × 2) sparse """ func_info = self.gen_func_info(la_str) t = [(0, 0), (1, 1)] t2 = [(0, 0), (1, 1), (2, 2), (3, 3)] value = np.array([3, 4]) value2 = np.array([3, 4, 1, 1]) A = scipy.sparse.coo_matrix((value, np.asarray(t).T), shape=(2, 2)) B = scipy.sparse.coo_matrix((value2, np.asarray(t2).T), shape=(4, 4)) self.assertSMatrixEqual(func_info.numpy_func(A).ret, B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " std::vector<Eigen::Triplet<double> > t1;", " t1.push_back(Eigen::Triplet<double>(0, 0, 3));", " t1.push_back(Eigen::Triplet<double>(1, 1, 4));", " Eigen::SparseMatrix<double> A(2, 2);", " A.setFromTriplets(t1.begin(), t1.end());" " std::vector<Eigen::Triplet<double> > t2;", " t2.push_back(Eigen::Triplet<double>(0, 0, 3));", " t2.push_back(Eigen::Triplet<double>(1, 1, 4));", " t2.push_back(Eigen::Triplet<double>(2, 2, 1));", " t2.push_back(Eigen::Triplet<double>(3, 3, 1));", " Eigen::SparseMatrix<double> C(4, 4);", " C.setFromTriplets(t2.begin(), t2.end());" " Eigen::SparseMatrix<double> B = {}(A).ret;".format( func_info.eig_func_name), " return C.isApprox(B);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test01_kdcraw(self): """Doc strings for KDcrawIface (used to crash).""" import cppyy, pydoc # TODO: run a find for these paths qtpath = "/usr/include/qt5" kdcraw_h = "/usr/include/KF5/KDCRAW/kdcraw/kdcraw.h" if not os.path.isdir(qtpath) or not os.path.exists(kdcraw_h): py.test.skip("no KDE/Qt found, skipping test01_kdcraw") # need to resolve qt_version_tag for the incremental compiler; since # it's not otherwise used, just make something up cppyy.cppdef("int qt_version_tag = 42;") cppyy.add_include_path(qtpath) cppyy.include(kdcraw_h) # bring in some symbols to resolve the class cppyy.load_library("libQt5Core.so") cppyy.load_library("libKF5KDcraw.so") from cppyy.gbl import KDcrawIface self.__class__.helpout = [] pydoc.doc(KDcrawIface.KDcraw) helptext = ''.join(self.__class__.helpout) assert 'KDcraw' in helptext assert 'CPPInstance' in helptext
def test_gallery_3(self): # sequence la_str = """[A⁻¹+A⁻¹BS⁻¹BᵀA⁻¹ -A⁻¹BS⁻¹ -S⁻¹BᵀA⁻¹ S⁻¹] where A: ℝ^(2×2) B: ℝ^(2×2) S: ℝ^(2×2)""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) B = np.array([[9, 12], [15, 16]]) S = np.array([[0, 2], [5, 8]]) b = np.array([[-9.2, -3.8, 1.6, 0.6], [2.4, 4.6, -0.2, -1.2], [3.6, 0.4, -0.8, 0.2], [-2.25, -0.75, 0.5, 0]]) self.assertDMatrixApproximateEqual(func_info.numpy_func(A, B, S).ret, b) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << 9, 12, 15, 16;", " Eigen::Matrix<double, 2, 2> S;", " S << 0, 2, 5, 8;", " Eigen::Matrix<double, 4, 4> D;", " D << -9.2, -3.8, 1.6, 0.6," " 2.4, 4.6, -0.2, -1.2," " 3.6, 0.4, -0.8, 0.2," " -2.25, -0.75, 0.5, 0;", " Eigen::Matrix<double, 4, 4> C = {}(A, B, S).ret;".format(func_info.eig_func_name), " return ((C - D).norm() < {});".format(self.eps), "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_matrix_pow(self): la_str = """C = A^2 where A: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) C = np.array([[7, 10], [15, 22]]) self.assertDMatrixEqual(func_info.numpy_func(A).C, C) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(A.tolist()))['C']), C) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << 7, 10, 15, 22;", " Eigen::Matrix<double, 2, 2> C = {}(A).C;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def setup_class(cls): cls.disable = False import cppyy # TODO: better error handling cppyy.include('boost/any.hpp') if not hasattr(cppyy.gbl, 'boost'): cls.disable = True
def test_inverse(self): # -1 la_str = """B = A^(-1) where A: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[4, 0], [0, 0.5]]) B = np.array([[0.25, 0], [0, 2]]) self.assertDMatrixEqual(func_info.numpy_func(A).B, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(A.tolist()))['B']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 4, 0, 0, 0.5;", " Eigen::Matrix<double, 2, 2> B;", " B << 0.25, 0, 0, 2;", " Eigen::Matrix<double, 2, 2> C = {}(A).B;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_cond_lt(self): la_str = """Q = A Q_ii = sum_(j for j < 3 ) Q_ij where A: ℝ ^ (3 × 3): a matrix""" func_info = self.gen_func_info(la_str) Q = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) P = np.array([[3, 2, 3], [4, 9, 6], [7, 8, 15]]) self.assertDMatrixEqual(func_info.numpy_func(Q).Q, P) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(Q.tolist()))['Q']), P) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 3, 3> Q;", " Q << 1, 2, 3, 4, 5, 6, 7, 8, 9;", " Eigen::Matrix<double, 3, 3> P;", " P << 3, 2, 3, 4, 9, 6, 7, 8, 15;", " Eigen::Matrix<double, 3, 3> C = {}(Q).Q;".format( func_info.eig_func_name), " return ((C - P).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_frobenius_product(self): la_str = """A = T : P where T: ℝ ^ (2×2): a sequence P: ℝ ^ (2×2): a sequence""" func_info = self.gen_func_info(la_str) T = np.array([[1, 2], [3, 4]]) P = np.array([[5, 6], [7, 8]]) self.assertEqual(func_info.numpy_func(T, P).A, 70) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertEqual( np.array( mat_func(matlab.double(T.tolist()), matlab.double(P.tolist()))['A']), 70) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> T;", " T << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> P;", " P << 5, 6, 7, 8;", " double B = {}(T, P).A;".format(func_info.eig_func_name), " return (B == 70);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_inner_product_subscript(self): la_str = """A = <T , P>_M where T: ℝ ^ 2: a sequence P: ℝ ^ 2: a sequence M: ℝ ^ (2×2): a sequence""" func_info = self.gen_func_info(la_str) T = np.array([1, 2]) P = np.array([3, 4]) M = np.array([[5, 6], [7, 8]]) self.assertEqual(func_info.numpy_func(T, P, M).A, 143) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertEqual( np.array( mat_func(matlab.double(T.tolist()), matlab.double(P.tolist()), matlab.double(M.tolist()))['A']), 143) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 1> T;", " T << 1, 2;", " Eigen::Matrix<double, 2, 1> P;", " P << 3, 4;", " Eigen::Matrix<double, 2, 2> M;", " M << 5, 6, 7, 8;", " double B = {}(T, P, M).A;".format(func_info.eig_func_name), " return (B == 143);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_sparse_diagonal_matrix(self): # sparse matrix: = la_str = """D_ii = sum_j A_ij where A: ℝ^(n × n) """ func_info = self.gen_func_info(la_str) A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) E = [(0, 0), (1, 1), (2, 2)] value = np.array([6, 15, 24]) B = scipy.sparse.coo_matrix((value, np.asarray(E).T), shape=(3, 3), dtype=np.float64) self.assertSMatrixEqual(func_info.numpy_func(A).D, B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 3, 3> A;", " A << 1, 2, 3, 4, 5, 6, 7, 8, 9;", " std::vector<Eigen::Triplet<double> > t1;", " t1.push_back(Eigen::Triplet<double>(0, 0, 6));", " t1.push_back(Eigen::Triplet<double>(1, 1, 15));", " t1.push_back(Eigen::Triplet<double>(2, 2, 24));", " Eigen::SparseMatrix<double> C(3, 3);", " C.setFromTriplets(t1.begin(), t1.end());" " Eigen::SparseMatrix<double> B = {}(A).D;".format( func_info.eig_func_name), " return C.isApprox(B);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_gallery_10(self): # sequence la_str = """`T₁` = 1/sqrt(2)U[0 0 0 0 0 -1 0 1 0]Vᵀ where U: ℝ^(3×3) V: ℝ^(3×3)""" func_info = self.gen_func_info(la_str) U = np.array([[1, 2, 3], [2, 3, 5], [8, 10, 6]]) V = np.array([[10, 2, 2], [1, 3, 1], [7, 4, 8]]) B = np.array([[1.41421356, 4.94974747, -2.82842712], [2.82842712, 8.48528137, -2.82842712], [-5.65685425, 5.65685425, -39.59797975]]) self.assertDMatrixApproximateEqual(func_info.numpy_func(U, V).T1, B) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 3, 3> U;", " U << 1, 2, 3, 2, 3, 5, 8, 10, 6;", " Eigen::Matrix<double, 3, 3> V;", " V << 10, 2, 2, 1, 3, 1, 7, 4, 8;", " Eigen::Matrix<double, 3, 3> B;", " B << 1.41421356, 4.94974747, -2.82842712, 2.82842712, 8.48528137, -2.82842712, -5.65685425, 5.65685425, -39.59797975;", " Eigen::Matrix<double, 3, 3> C = {}(U, V).T₁;".format(func_info.eig_func_name), " return ((B - C).norm() < {});".format(self.eps), "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_unary_matrix_2(self): la_str = """B = -[2 2; 2 2]-A where A: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) B = np.array([[-3, -4], [-5, -6]]) self.assertDMatrixEqual(func_info.numpy_func(A).B, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(A.tolist()))['B']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << -3, -4, -5, -6;", " Eigen::Matrix<double, 2, 2> C = {}(A).B;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_gallery_13(self): # sequence la_str = """`C(x,y)` = (∑_n ∑_i c_n,i w_n,i R̂_n) / (∑_n ∑_i w_n,i R̂_n) where c ∈ ℝ^(f×s): the value of the Bayer pixel w ∈ ℝ^(f×s): the local sample weight R̂ ∈ ℝ^f: the local robustness""" func_info = self.gen_func_info(la_str) c = np.array([[1, 2], [3, 6]]) w = np.array([[2, 2], [2, 4]]) R̂ = np.array([2, 4]) self.assertTrue(np.isclose(func_info.numpy_func(c, w, R̂).C_left_parenthesis_x_comma_y_right_parenthesis, 4.125)) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> c;", " c << 1, 2, 3, 6;", " Eigen::Matrix<double, 2, 2> w;", " w << 2, 2, 2, 4;", " Eigen::Matrix<double, 2, 1> R;", " R << 2, 4;", " double C = {}(c, w, R).C_left_parenthesis_x_comma_y_right_parenthesis;".format(func_info.eig_func_name), " return (abs(4.125 - C) < {});".format(self.eps), "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test01_kdcraw(self): """Doc strings for KDcrawIface (used to crash).""" import cppyy, pydoc # TODO: run a find for these paths qtpath = "/usr/include/qt5" kdcraw_h = "/usr/include/KF5/KDCRAW/kdcraw/kdcraw.h" if not os.path.isdir(qtpath) or not os.path.exists(kdcraw_h): import warnings warnings.warn("no KDE/Qt found, skipping test01_kdcraw") return # need to resolve qt_version_tag for the incremental compiler; since # it's not otherwise used, just make something up cppyy.cppdef("int qt_version_tag = 42;") cppyy.add_include_path(qtpath) cppyy.include(kdcraw_h) from cppyy.gbl import KDcrawIface self.__class__.helpout = [] pydoc.doc(KDcrawIface.KDcraw) helptext = ''.join(self.__class__.helpout) assert 'KDcraw' in helptext assert 'CPPInstance' in helptext
def test_gallery_14(self): # sequence la_str = """Ω = [`e₁` `e₂`][`k₁` 0 0 `k₂`] [`e₁`ᵀ `e₂`ᵀ] where `k₁`: ℝ : control the desired kernel variance in either edge or orthogonal direction `k₂`: ℝ : control the desired kernel variance in either edge or orthogonal direction `e₁`: ℝ ^ 3: orthogonal direction vectors `e₂`: ℝ ^ 3: orthogonal direction vectors""" func_info = self.gen_func_info(la_str) k1 = 2 k2 = 3 e1 = np.array([4, 2, 3]) e2 = np.array([1, 5, 2]) B = np.array([[35, 31, 30], [31, 83, 42], [30, 42, 30]]) self.assertDMatrixApproximateEqual(func_info.numpy_func(k1, k2, e1, e2).Ω, B) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 3, 1> e1;", " e1 << 4, 2, 3;", " Eigen::Matrix<double, 3, 1> e2;", " e2 << 1, 5, 2;", " Eigen::Matrix<double, 3, 3> B;", " B << 35, 31, 30, 31, 83, 42, 30, 42, 30;", " Eigen::Matrix<double, 3, 3> C = {}(2, 3, e1, e2).Ω;".format(func_info.eig_func_name), " return ((B - C).norm() < {});".format(self.eps), "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def __call__(self): for length, headers in self.known_lengths: if length not in LengthRegistrar.REGISTERED_LENGTHS: if length == "intervalxt::cppyy::Lengths<std::vector<__gmp_expr<__mpz_struct[1],__mpz_struct[1]> > >": import logging logging.warning( "GMP does not provide a cereal interface yet. Therefore serialization of mpz lengths is not supported yet." ) continue if length == "intervalxt::cppyy::Lengths<std::vector<__gmp_expr<__mpq_struct[1],__mpq_struct[1]> > >": import logging logging.warning( "GMP does not provide a cereal interface yet. Therefore serialization of mpq lengths is not supported yet." ) continue for header in headers: cppyy.include(header) cppyy.cppdef( 'LIBINTERVALXT_ERASED_REGISTER((::intervalxt::Lengths), (%s));' % (length, )) LengthRegistrar.REGISTERED_LENGTHS.add(length)
def test_gallery_2(self): # sequence la_str = """y_i = (a_i)ᵀ x + w_i x̂ = (∑_i a_i(a_i)ᵀ)⁻¹ ∑_i y_i a_i where a_i: ℝ^n: the measurement vectors w_i: ℝ: measurement noise x: ℝ^n: measurement noise """ func_info = self.gen_func_info(la_str) a = np.array([[10, 4], [8, 6]]) w = np.array([3, 2]) x = np.array([2, 3]) b = np.array([2.35714286, 2.85714286]) self.assertDMatrixApproximateEqual(func_info.numpy_func(a, w, x).x̂ , b) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " std::vector<Eigen::VectorXd > a;", " Eigen::VectorXd a1(2);", " a1 << 10, 4;", " Eigen::VectorXd a2(2);", " a2 << 8, 6;", " a.push_back(a1);", " a.push_back(a2);", " std::vector<double> w = {3, 2};", " Eigen::VectorXd x(2);", " x << 2, 3;", " Eigen::VectorXd B(2);", " B << 2.35714286, 2.85714286;", " Eigen::Matrix<double, 2, 1> C = {}(a, w, x).x̂ ;".format(func_info.eig_func_name), " return ((C - B).norm() < {});".format(self.eps), "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_backtick_sub(self): la_str = """A = sum_`j_i` `Energy`_`j_i` where `Energy`_`index`: ℝ ^ (2 × 2): a sequence""" func_info = self.gen_func_info(la_str) A = np.array([[[1, 2], [4, 3]], [[1, 2], [4, 3]]]) B = np.array([[2, 4], [8, 6]]) self.assertDMatrixEqual(func_info.numpy_func(A).A, B) # MATLAB test # if TEST_MATLAB: # mat_func = getattr(mat_engine, func_info.mat_func_name, None) # self.assertDMatrixEqual(np.array(mat_func(matlab.double(A.tolist()))['A']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A1;", " A1 << 1, 2, 4, 3;", " Eigen::Matrix<double, 2, 2> A2;", " A2 << 1, 2, 4, 3;", " Eigen::Matrix<double, 2, 2> B;", " B << 2, 4, 8, 6;", " std::vector<Eigen::Matrix<double, 2, 2> > A;", " A.push_back(A1);", " A.push_back(A2);", " Eigen::Matrix<double, 2, 2> C = {}(A).A;".format( func_info.eig_func_name), " return ((C - B).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_gallery_21(self): # sequence la_str = """`L(x,v)` = xᵀWx + ∑_i v_i(x_i²-1) where x: ℝ^n W: ℝ^(n×n) v: ℝ^n""" func_info = self.gen_func_info(la_str) x = np.array([3, 1, 4, 2]) W = np.array([[1, 2, 3, 4], [3, 4, 5, 6], [5, 3, 5, 6], [9, 1, 3, 2]]) v = np.array([2, 1, 5, 7]) self.assertTrue(np.isclose(func_info.numpy_func(x, W, v).L_left_parenthesis_x_comma_v_right_parenthesis, 520)) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::VectorXd x(4);" " x << 3, 1, 4, 2;", " Eigen::MatrixXd W(4,4);", " W << 1, 2, 3, 4, 3, 4, 5, 6, 5, 3, 5, 6, 9, 1, 3, 2;", " Eigen::VectorXd v(4);" " v << 2, 1, 5, 7;", " double C = {}(x, W, v).L_left_parenthesis_x_comma_v_right_parenthesis;".format(func_info.eig_func_name), " return (abs(520 - C) < {});".format(self.eps), "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_square_matrix(self): la_str = """A = [a 2; b 3] where a: scalar b: scalar""" func_info = self.gen_func_info(la_str) a = 1 b = 4 B = np.array([[1, 2], [4, 3]]) self.assertDMatrixEqual(func_info.numpy_func(a, b).A, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual(np.array(mat_func(a, b)['A']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> B;", " B << 1, 2, 4, 3;", " Eigen::Matrix<double, 2, 2> C = {}(1, 4).A;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_laplacian(self): # sparse matrix la_str = """from trigonometry: cot L_i,j = { cot(α_ij) + cot(β_ij) if j ∈ N(i) L_i,i = -sum_(k for k != i) L_i,k where L: ℝ^(n×n) α: ℝ^(n×n) β: ℝ^(n×n) N: ℤ -> {ℤ} """ func_info = self.gen_func_info(la_str) α = np.array([[1, 2, 3], [4, 7, 6], [5, 3, 2]]) β = np.array([[2, 5, 4], [3, 5, 6], [9, 3, 2]]) def N(p0): if p0 == 1: return [2] elif p0 == 2: return [1] return [1, 2] G = [(0, 1), (1, 0), (2, 0), (2, 1), (0, 0), (1, 1), (2, 2)] value = np.array([-0.75347046989, -6.151561396983, -2.506658326531, -14.0305051028690, 0.7534704698930, 6.15156139698, 16.5371634294]) B = scipy.sparse.coo_matrix((value, np.asarray(G).T), shape=(3, 3)) self.assertSMatrixEqual(func_info.numpy_func(α, β, N).L, B) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 3, 3> α;", " α << 1, 2, 3, 4, 7, 6, 5, 3, 2;", " Eigen::Matrix<double, 3, 3> β;", " β << 2, 5, 4, 3, 5, 6, 9, 3, 2;", " std::function<std::set<int >(int)> N = [](int p)->std::set<int >{", " std::set<int > tmp;", " if(p == 1){", " tmp.insert(2);", " }", " else if(p == 2){", " tmp.insert(1);", " }", " else{", " tmp.insert(1);", " tmp.insert(2);", " }", " return tmp;", " };", " std::vector<Eigen::Triplet<double> > t1;", " t1.push_back(Eigen::Triplet<double>(0, 1, -0.75347046989));", " t1.push_back(Eigen::Triplet<double>(1, 0, -6.151561396983));", " t1.push_back(Eigen::Triplet<double>(2, 0, -2.506658326531));", " t1.push_back(Eigen::Triplet<double>(2, 1, -14.0305051028690));", " t1.push_back(Eigen::Triplet<double>(0, 0, 0.7534704698930));", " t1.push_back(Eigen::Triplet<double>(1, 1, 6.15156139698));", " t1.push_back(Eigen::Triplet<double>(2, 2, 16.5371634294));", " Eigen::SparseMatrix<double> A(3, 3);", " A.setFromTriplets(t1.begin(), t1.end());" " Eigen::SparseMatrix<double> B = {}(α, β, N).L;".format(func_info.eig_func_name), " return A.isApprox(B);", "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_block_matrix_4(self): # I identity matrix la_str = """C = [A 1; 0 I_2] where A: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) C = np.array([[1, 2, 1, 1], [3, 4, 1, 1], [0, 0, 1, 0], [0, 0, 0, 1]]) self.assertDMatrixEqual(func_info.numpy_func(A).C, C) # MATLAB testA # if TEST_MATLAB: # mat_func = getattr(mat_engine, func_info.mat_func_name, None) # print(np.array(mat_func((A.tolist()))['C'])) # self.assertDMatrixEqual(np.array(mat_func((A.tolist()))['C']), C) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 4, 4> B;", " B << 1, 2, 1, 1, 3, 4, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1;", " Eigen::Matrix<double, 4, 4> C = {}(A).C;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_laplacian_1(self): # sparse matrix la_str = """L_i,j = { w_i,j if (i,j) ∈ E L_i,i = -sum_(l for l != i) L_i,l where L: ℝ^(n×n) w: ℝ^(n×n): edge weight matrix E: {ℤ²} index: edges """ func_info = self.gen_func_info(la_str) w = np.array([[-6, 9, -1, -11], [17, 14, 0, 0], [6, -2, 11, 0], [2, -9, -2, -9]]) E = [(0, 1), (0, 2), (1, 2), (2, 0), (3, 0), (3, 1), (3, 2)] F = [(0, 1), (0, 2), (1, 2), (2, 0), (3, 0), (3, 1), (3, 2), (0, 0), (1, 1), (2, 2), (3, 3)] value = np.array([9, -1, 0, 6, 2, -9, -2, -8, 0, -6, 9]) B = scipy.sparse.coo_matrix((value, np.asarray(F).T), shape=(4, 4), dtype=np.float64) self.assertSMatrixEqual(func_info.numpy_func(w, E).L, B) # eigen test cppyy.include(func_info.eig_file_name) func_list = ["bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 4, 4> w;", " w << -6, 9, -1, -11, 17, 14, 0, 0, 6, -2, 11, 0, 2, -9, -2, -9;", " std::set< std::tuple< int, int > > E;", " E.insert(std::make_tuple(0, 1));", " E.insert(std::make_tuple(0, 2));", " E.insert(std::make_tuple(1, 2));", " E.insert(std::make_tuple(2, 0));", " E.insert(std::make_tuple(3, 0));", " E.insert(std::make_tuple(3, 1));", " E.insert(std::make_tuple(3, 2));", " std::set< std::tuple< int, int > > F;", " F.insert(std::make_tuple(0, 1));", " F.insert(std::make_tuple(0, 2));", " F.insert(std::make_tuple(1, 2));", " F.insert(std::make_tuple(2, 0));", " F.insert(std::make_tuple(3, 0));", " F.insert(std::make_tuple(3, 1));", " F.insert(std::make_tuple(3, 2));", " F.insert(std::make_tuple(0, 0));", " F.insert(std::make_tuple(1, 1));", " F.insert(std::make_tuple(2, 2));", " F.insert(std::make_tuple(3, 3));", " std::vector<Eigen::Triplet<double> > t1;", " t1.push_back(Eigen::Triplet<double>(0, 1, 9));", " t1.push_back(Eigen::Triplet<double>(0, 2, -1));", " t1.push_back(Eigen::Triplet<double>(1, 2, 0));", " t1.push_back(Eigen::Triplet<double>(2, 0, 6));", " t1.push_back(Eigen::Triplet<double>(3, 0, 2));", " t1.push_back(Eigen::Triplet<double>(3, 1, -9));", " t1.push_back(Eigen::Triplet<double>(3, 2, -2));", " t1.push_back(Eigen::Triplet<double>(0, 0, -8));", " t1.push_back(Eigen::Triplet<double>(1, 1, 0));", " t1.push_back(Eigen::Triplet<double>(2, 2, -6));", " t1.push_back(Eigen::Triplet<double>(3, 3, 9));", " Eigen::SparseMatrix<double> A(4, 4);", " A.setFromTriplets(t1.begin(), t1.end());" " Eigen::SparseMatrix<double> B = {}(w, E).L;".format(func_info.eig_func_name), " return A.isApprox(B);", "}"] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_block_matrix_7(self): # scalar value la_str = """B = [ A ; 1 ] where A: ℝ ^ 2: a matrix""" func_info = self.gen_func_info(la_str) A = np.array([3, 4]) B = np.array([[3], [4], [1]]) self.assertDMatrixEqual(func_info.numpy_func(A).B, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(A.tolist()))['B']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 1> A;", " A << 3, 4;", " Eigen::Matrix<double, 3, 1> B;", " B << 3, 4, 1;", " Eigen::Matrix<double, 3, 1> C = {}(A).B;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_solver(self): # matrix la_str = """y = A \ C where A: ℝ ^ (2 × 2): a matrix C: ℝ ^ (2 × 2): a matrix """ func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) C = np.array([[19, 22], [43, 50]]) B = np.array([[5., 6.], [7., 8.]]) B = np.asarray(B, dtype=np.float64) self.assertDMatrixEqual( func_info.numpy_func(A, C).y, np.linalg.solve(A, C)) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array( mat_func(matlab.double(A.tolist()), matlab.double(C.tolist()))['y']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << 5, 6, 7, 8;", " Eigen::Matrix<double, 2, 2> C;", " C << 19, 22, 43, 50;", " Eigen::Matrix<double, 2, 2> D = {}(A, C).y;".format( func_info.eig_func_name), " return ((A.colPivHouseholderQr().solve(C) - D).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_identity_matrix_1(self): # I used as symbol rather than identity matrix la_str = """I = A B = I + A where A: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) B = np.array([[2, 4], [6, 8]]) self.assertDMatrixEqual(func_info.numpy_func(A).B, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(matlab.double(A.tolist()))['B']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << 2, 4, 6, 8;", " Eigen::Matrix<double, 2, 2> C = {}(A).B;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test_multiplication_2(self): # matrix la_str = """c = a ⋅ A where a: scalar A: ℝ ^ (2 × 2): a matrix""" func_info = self.gen_func_info(la_str) A = np.array([[1, 2], [3, 4]]) B = np.array([[2, 4], [6, 8]]) self.assertDMatrixEqual(func_info.numpy_func(2, A).c, B) # MATLAB test if TEST_MATLAB: mat_func = getattr(mat_engine, func_info.mat_func_name, None) self.assertDMatrixEqual( np.array(mat_func(2, matlab.int64(A.tolist()))['c']), B) # eigen test cppyy.include(func_info.eig_file_name) func_list = [ "bool {}(){{".format(func_info.eig_test_name), " Eigen::Matrix<double, 2, 2> A;", " A << 1, 2, 3, 4;", " Eigen::Matrix<double, 2, 2> B;", " B << 2, 4, 6, 8;", " Eigen::Matrix<double, 2, 2> C = {}(2, A).c;".format( func_info.eig_func_name), " return ((B - C).norm() == 0);", "}" ] cppyy.cppdef('\n'.join(func_list)) self.assertTrue(getattr(cppyy.gbl, func_info.eig_test_name)())
def test02_set_iterators(self): """Access to set iterators and their comparisons""" import cppyy cppyy.include("iterator") s = cppyy.gbl.std.set[int]() assert s.begin() == s.end() assert s.rbegin() == s.rend() val = 42 s.insert(val) assert len(s) == 1 assert s.begin().__deref__() == val assert s.rbegin().__deref__() == val assert s.begin() != s.end() assert s.begin().__preinc__() == s.end() assert s.rbegin() != s.rend() assert s.rbegin().__preinc__() == s.rend()
def setup_class(cls): import cppyy cppyy.include('boost/any.hpp')
import cppyy cppyy.include("ball.hpp") from cppyy.gbl import Ball b = Ball(150, 200) print(b.get_x(), b.get_y()) for i in range(10): b.move() print(b.get_x(), b.get_y())
#!/usr/bin/env python3 import cppyy cppyy.include("lightsout.hpp") cppyy.include("fifteen.hpp") from cppyy.gbl import LightsOut, Fifteen ##from boardgames import LightsOut, Fifteen import sys sys.path.append('../../examples/') from boardgame_g2d import gui_play ##game = LightsOut(4, 5, 5) game = Fifteen(3, 3) gui_play(game)
#!/usr/bin/env python3 import cppyy cppyy.include("bounce.hpp") from cppyy.gbl import Size, Point, Rect, Arena, Ball, Ghost, Turtle import sys; sys.path.append('../../examples/') import g2d def make(typ, **attrs): obj = typ() for k, v in attrs.items(): setattr(obj, k, v) return obj def vals(obj, attrs=list("xywhrgb")): return tuple(getattr(obj, a) for a in attrs if hasattr(obj, a)) arena = Arena(make(Size, w=320, h=240)) b1 = Ball(arena, make(Point, x=40, y=80)) b2 = Ball(arena, make(Point, x=80, y=40)) g = Ghost(arena, make(Point, x=120, y=80)) turtle = Turtle(arena, make(Point, x=80, y=80)) sprites = g2d.load_image("sprites.png") def update(): arena.move_all() # Game logic g2d.clear_canvas() for a in arena.actors(): g2d.draw_image_clip(sprites, vals(a.symbol()), vals(a.position()))