m2m模型翻译
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.

139 lines
4.6 KiB

6 months ago
  1. from sympy.core.numbers import (Float, I, Rational, pi)
  2. from sympy.core.relational import Eq
  3. from sympy.core.symbol import (Symbol, symbols)
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.functions.elementary.piecewise import Piecewise
  6. from sympy.functions.elementary.trigonometric import sin
  7. from sympy.integrals.integrals import Integral
  8. from sympy.matrices.dense import Matrix
  9. from mpmath import mnorm, mpf
  10. from sympy.solvers import nsolve
  11. from sympy.utilities.lambdify import lambdify
  12. from sympy.testing.pytest import raises, XFAIL
  13. from sympy.utilities.decorator import conserve_mpmath_dps
  14. @XFAIL
  15. def test_nsolve_fail():
  16. x = symbols('x')
  17. # Sometimes it is better to use the numerator (issue 4829)
  18. # but sometimes it is not (issue 11768) so leave this to
  19. # the discretion of the user
  20. ans = nsolve(x**2/(1 - x)/(1 - 2*x)**2 - 100, x, 0)
  21. assert ans > 0.46 and ans < 0.47
  22. def test_nsolve_denominator():
  23. x = symbols('x')
  24. # Test that nsolve uses the full expression (numerator and denominator).
  25. ans = nsolve((x**2 + 3*x + 2)/(x + 2), -2.1)
  26. # The root -2 was divided out, so make sure we don't find it.
  27. assert ans == -1.0
  28. def test_nsolve():
  29. # onedimensional
  30. x = Symbol('x')
  31. assert nsolve(sin(x), 2) - pi.evalf() < 1e-15
  32. assert nsolve(Eq(2*x, 2), x, -10) == nsolve(2*x - 2, -10)
  33. # Testing checks on number of inputs
  34. raises(TypeError, lambda: nsolve(Eq(2*x, 2)))
  35. raises(TypeError, lambda: nsolve(Eq(2*x, 2), x, 1, 2))
  36. # multidimensional
  37. x1 = Symbol('x1')
  38. x2 = Symbol('x2')
  39. f1 = 3 * x1**2 - 2 * x2**2 - 1
  40. f2 = x1**2 - 2 * x1 + x2**2 + 2 * x2 - 8
  41. f = Matrix((f1, f2)).T
  42. F = lambdify((x1, x2), f.T, modules='mpmath')
  43. for x0 in [(-1, 1), (1, -2), (4, 4), (-4, -4)]:
  44. x = nsolve(f, (x1, x2), x0, tol=1.e-8)
  45. assert mnorm(F(*x), 1) <= 1.e-10
  46. # The Chinese mathematician Zhu Shijie was the very first to solve this
  47. # nonlinear system 700 years ago (z was added to make it 3-dimensional)
  48. x = Symbol('x')
  49. y = Symbol('y')
  50. z = Symbol('z')
  51. f1 = -x + 2*y
  52. f2 = (x**2 + x*(y**2 - 2) - 4*y) / (x + 4)
  53. f3 = sqrt(x**2 + y**2)*z
  54. f = Matrix((f1, f2, f3)).T
  55. F = lambdify((x, y, z), f.T, modules='mpmath')
  56. def getroot(x0):
  57. root = nsolve(f, (x, y, z), x0)
  58. assert mnorm(F(*root), 1) <= 1.e-8
  59. return root
  60. assert list(map(round, getroot((1, 1, 1)))) == [2.0, 1.0, 0.0]
  61. assert nsolve([Eq(
  62. f1, 0), Eq(f2, 0), Eq(f3, 0)], [x, y, z], (1, 1, 1)) # just see that it works
  63. a = Symbol('a')
  64. assert abs(nsolve(1/(0.001 + a)**3 - 6/(0.9 - a)**3, a, 0.3) -
  65. mpf('0.31883011387318591')) < 1e-15
  66. def test_issue_6408():
  67. x = Symbol('x')
  68. assert nsolve(Piecewise((x, x < 1), (x**2, True)), x, 2) == 0.0
  69. def test_issue_6408_integral():
  70. x, y = symbols('x y')
  71. assert nsolve(Integral(x*y, (x, 0, 5)), y, 2) == 0.0
  72. @conserve_mpmath_dps
  73. def test_increased_dps():
  74. # Issue 8564
  75. import mpmath
  76. mpmath.mp.dps = 128
  77. x = Symbol('x')
  78. e1 = x**2 - pi
  79. q = nsolve(e1, x, 3.0)
  80. assert abs(sqrt(pi).evalf(128) - q) < 1e-128
  81. def test_nsolve_precision():
  82. x, y = symbols('x y')
  83. sol = nsolve(x**2 - pi, x, 3, prec=128)
  84. assert abs(sqrt(pi).evalf(128) - sol) < 1e-128
  85. assert isinstance(sol, Float)
  86. sols = nsolve((y**2 - x, x**2 - pi), (x, y), (3, 3), prec=128)
  87. assert isinstance(sols, Matrix)
  88. assert sols.shape == (2, 1)
  89. assert abs(sqrt(pi).evalf(128) - sols[0]) < 1e-128
  90. assert abs(sqrt(sqrt(pi)).evalf(128) - sols[1]) < 1e-128
  91. assert all(isinstance(i, Float) for i in sols)
  92. def test_nsolve_complex():
  93. x, y = symbols('x y')
  94. assert nsolve(x**2 + 2, 1j) == sqrt(2.)*I
  95. assert nsolve(x**2 + 2, I) == sqrt(2.)*I
  96. assert nsolve([x**2 + 2, y**2 + 2], [x, y], [I, I]) == Matrix([sqrt(2.)*I, sqrt(2.)*I])
  97. assert nsolve([x**2 + 2, y**2 + 2], [x, y], [I, I]) == Matrix([sqrt(2.)*I, sqrt(2.)*I])
  98. def test_nsolve_dict_kwarg():
  99. x, y = symbols('x y')
  100. # one variable
  101. assert nsolve(x**2 - 2, 1, dict = True) == \
  102. [{x: sqrt(2.)}]
  103. # one variable with complex solution
  104. assert nsolve(x**2 + 2, I, dict = True) == \
  105. [{x: sqrt(2.)*I}]
  106. # two variables
  107. assert nsolve([x**2 + y**2 - 5, x**2 - y**2 + 1], [x, y], [1, 1], dict = True) == \
  108. [{x: sqrt(2.), y: sqrt(3.)}]
  109. def test_nsolve_rational():
  110. x = symbols('x')
  111. assert nsolve(x - Rational(1, 3), 0, prec=100) == Rational(1, 3).evalf(100)
  112. def test_issue_14950():
  113. x = Matrix(symbols('t s'))
  114. x0 = Matrix([17, 23])
  115. eqn = x + x0
  116. assert nsolve(eqn, x, x0) == -x0
  117. assert nsolve(eqn.T, x.T, x0.T) == -x0