图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.7 KiB

  1. from sympy.core.symbol import symbols
  2. from sympy.functions import beta, Ei, zeta, Max, Min, sqrt, riemann_xi, frac
  3. from sympy.printing.cxx import CXX98CodePrinter, CXX11CodePrinter, CXX17CodePrinter, cxxcode
  4. from sympy.codegen.cfunctions import log1p
  5. from sympy.testing.pytest import warns_deprecated_sympy
  6. x, y, u, v = symbols('x y u v')
  7. def test_CXX98CodePrinter():
  8. assert CXX98CodePrinter().doprint(Max(x, 3)) in ('std::max(x, 3)', 'std::max(3, x)')
  9. assert CXX98CodePrinter().doprint(Min(x, 3, sqrt(x))) == 'std::min(3, std::min(x, std::sqrt(x)))'
  10. cxx98printer = CXX98CodePrinter()
  11. assert cxx98printer.language == 'C++'
  12. assert cxx98printer.standard == 'C++98'
  13. assert 'template' in cxx98printer.reserved_words
  14. assert 'alignas' not in cxx98printer.reserved_words
  15. def test_CXX11CodePrinter():
  16. assert CXX11CodePrinter().doprint(log1p(x)) == 'std::log1p(x)'
  17. cxx11printer = CXX11CodePrinter()
  18. assert cxx11printer.language == 'C++'
  19. assert cxx11printer.standard == 'C++11'
  20. assert 'operator' in cxx11printer.reserved_words
  21. assert 'noexcept' in cxx11printer.reserved_words
  22. assert 'concept' not in cxx11printer.reserved_words
  23. def test_subclass_print_method():
  24. class MyPrinter(CXX11CodePrinter):
  25. def _print_log1p(self, expr):
  26. return 'my_library::log1p(%s)' % ', '.join(map(self._print, expr.args))
  27. assert MyPrinter().doprint(log1p(x)) == 'my_library::log1p(x)'
  28. def test_subclass_print_method__ns():
  29. class MyPrinter(CXX11CodePrinter):
  30. _ns = 'my_library::'
  31. p = CXX11CodePrinter()
  32. myp = MyPrinter()
  33. assert p.doprint(log1p(x)) == 'std::log1p(x)'
  34. assert myp.doprint(log1p(x)) == 'my_library::log1p(x)'
  35. def test_CXX17CodePrinter():
  36. assert CXX17CodePrinter().doprint(beta(x, y)) == 'std::beta(x, y)'
  37. assert CXX17CodePrinter().doprint(Ei(x)) == 'std::expint(x)'
  38. assert CXX17CodePrinter().doprint(zeta(x)) == 'std::riemann_zeta(x)'
  39. # Automatic rewrite
  40. assert CXX17CodePrinter().doprint(frac(x)) == 'x - std::floor(x)'
  41. assert CXX17CodePrinter().doprint(riemann_xi(x)) == '(1.0/2.0)*std::pow(M_PI, -1.0/2.0*x)*x*(x - 1)*std::tgamma((1.0/2.0)*x)*std::riemann_zeta(x)'
  42. def test_cxxcode():
  43. assert sorted(cxxcode(sqrt(x)*.5).split('*')) == sorted(['0.5', 'std::sqrt(x)'])
  44. def test_cxxcode_submodule():
  45. # Test the compatibility sympy.printing.cxxcode module imports
  46. with warns_deprecated_sympy():
  47. import sympy.printing.cxxcode # noqa:F401
  48. def test_cxxcode_nested_minmax():
  49. assert cxxcode(Max(Min(x, y), Min(u, v))) \
  50. == 'std::max(std::min(u, v), std::min(x, y))'
  51. assert cxxcode(Min(Max(x, y), Max(u, v))) \
  52. == 'std::min(std::max(u, v), std::max(x, y))'