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.

731 lines
25 KiB

7 months ago
  1. from sympy.assumptions.refine import refine
  2. from sympy.calculus.accumulationbounds import AccumBounds
  3. from sympy.concrete.products import Product
  4. from sympy.concrete.summations import Sum
  5. from sympy.core.function import expand_log
  6. from sympy.core.numbers import (E, Float, I, Rational, nan, oo, pi, zoo)
  7. from sympy.core.power import Pow
  8. from sympy.core.singleton import S
  9. from sympy.core.symbol import (Symbol, symbols)
  10. from sympy.functions.elementary.complexes import (adjoint, conjugate, re, sign, transpose)
  11. from sympy.functions.elementary.exponential import (LambertW, exp, exp_polar, log)
  12. from sympy.functions.elementary.hyperbolic import (cosh, sinh, tanh)
  13. from sympy.functions.elementary.miscellaneous import sqrt
  14. from sympy.functions.elementary.trigonometric import (cos, sin)
  15. from sympy.matrices.expressions.matexpr import MatrixSymbol
  16. from sympy.polys.polytools import gcd
  17. from sympy.series.order import O
  18. from sympy.simplify.simplify import simplify
  19. from sympy.core.parameters import global_parameters
  20. from sympy.functions.elementary.exponential import match_real_imag
  21. from sympy.abc import x, y, z
  22. from sympy.core.expr import unchanged
  23. from sympy.core.function import ArgumentIndexError
  24. from sympy.testing.pytest import raises, XFAIL, _both_exp_pow
  25. @_both_exp_pow
  26. def test_exp_values():
  27. if global_parameters.exp_is_pow:
  28. assert type(exp(x)) is Pow
  29. else:
  30. assert type(exp(x)) is exp
  31. k = Symbol('k', integer=True)
  32. assert exp(nan) is nan
  33. assert exp(oo) is oo
  34. assert exp(-oo) == 0
  35. assert exp(0) == 1
  36. assert exp(1) == E
  37. assert exp(-1 + x).as_base_exp() == (S.Exp1, x - 1)
  38. assert exp(1 + x).as_base_exp() == (S.Exp1, x + 1)
  39. assert exp(pi*I/2) == I
  40. assert exp(pi*I) == -1
  41. assert exp(pi*I*Rational(3, 2)) == -I
  42. assert exp(2*pi*I) == 1
  43. assert refine(exp(pi*I*2*k)) == 1
  44. assert refine(exp(pi*I*2*(k + S.Half))) == -1
  45. assert refine(exp(pi*I*2*(k + Rational(1, 4)))) == I
  46. assert refine(exp(pi*I*2*(k + Rational(3, 4)))) == -I
  47. assert exp(log(x)) == x
  48. assert exp(2*log(x)) == x**2
  49. assert exp(pi*log(x)) == x**pi
  50. assert exp(17*log(x) + E*log(y)) == x**17 * y**E
  51. assert exp(x*log(x)) != x**x
  52. assert exp(sin(x)*log(x)) != x
  53. assert exp(3*log(x) + oo*x) == exp(oo*x) * x**3
  54. assert exp(4*log(x)*log(y) + 3*log(x)) == x**3 * exp(4*log(x)*log(y))
  55. assert exp(-oo, evaluate=False).is_finite is True
  56. assert exp(oo, evaluate=False).is_finite is False
  57. @_both_exp_pow
  58. def test_exp_period():
  59. assert exp(I*pi*Rational(9, 4)) == exp(I*pi/4)
  60. assert exp(I*pi*Rational(46, 18)) == exp(I*pi*Rational(5, 9))
  61. assert exp(I*pi*Rational(25, 7)) == exp(I*pi*Rational(-3, 7))
  62. assert exp(I*pi*Rational(-19, 3)) == exp(-I*pi/3)
  63. assert exp(I*pi*Rational(37, 8)) - exp(I*pi*Rational(-11, 8)) == 0
  64. assert exp(I*pi*Rational(-5, 3)) / exp(I*pi*Rational(11, 5)) * exp(I*pi*Rational(148, 15)) == 1
  65. assert exp(2 - I*pi*Rational(17, 5)) == exp(2 + I*pi*Rational(3, 5))
  66. assert exp(log(3) + I*pi*Rational(29, 9)) == 3 * exp(I*pi*Rational(-7, 9))
  67. n = Symbol('n', integer=True)
  68. e = Symbol('e', even=True)
  69. assert exp(e*I*pi) == 1
  70. assert exp((e + 1)*I*pi) == -1
  71. assert exp((1 + 4*n)*I*pi/2) == I
  72. assert exp((-1 + 4*n)*I*pi/2) == -I
  73. @_both_exp_pow
  74. def test_exp_log():
  75. x = Symbol("x", real=True)
  76. assert log(exp(x)) == x
  77. assert exp(log(x)) == x
  78. if not global_parameters.exp_is_pow:
  79. assert log(x).inverse() == exp
  80. assert exp(x).inverse() == log
  81. y = Symbol("y", polar=True)
  82. assert log(exp_polar(z)) == z
  83. assert exp(log(y)) == y
  84. @_both_exp_pow
  85. def test_exp_expand():
  86. e = exp(log(Rational(2))*(1 + x) - log(Rational(2))*x)
  87. assert e.expand() == 2
  88. assert exp(x + y) != exp(x)*exp(y)
  89. assert exp(x + y).expand() == exp(x)*exp(y)
  90. @_both_exp_pow
  91. def test_exp__as_base_exp():
  92. assert exp(x).as_base_exp() == (E, x)
  93. assert exp(2*x).as_base_exp() == (E, 2*x)
  94. assert exp(x*y).as_base_exp() == (E, x*y)
  95. assert exp(-x).as_base_exp() == (E, -x)
  96. # Pow( *expr.as_base_exp() ) == expr invariant should hold
  97. assert E**x == exp(x)
  98. assert E**(2*x) == exp(2*x)
  99. assert E**(x*y) == exp(x*y)
  100. assert exp(x).base is S.Exp1
  101. assert exp(x).exp == x
  102. @_both_exp_pow
  103. def test_exp_infinity():
  104. assert exp(I*y) != nan
  105. assert refine(exp(I*oo)) is nan
  106. assert refine(exp(-I*oo)) is nan
  107. assert exp(y*I*oo) != nan
  108. assert exp(zoo) is nan
  109. x = Symbol('x', extended_real=True, finite=False)
  110. assert exp(x).is_complex is None
  111. @_both_exp_pow
  112. def test_exp_subs():
  113. x = Symbol('x')
  114. e = (exp(3*log(x), evaluate=False)) # evaluates to x**3
  115. assert e.subs(x**3, y**3) == e
  116. assert e.subs(x**2, 5) == e
  117. assert (x**3).subs(x**2, y) != y**Rational(3, 2)
  118. assert exp(exp(x) + exp(x**2)).subs(exp(exp(x)), y) == y * exp(exp(x**2))
  119. assert exp(x).subs(E, y) == y**x
  120. x = symbols('x', real=True)
  121. assert exp(5*x).subs(exp(7*x), y) == y**Rational(5, 7)
  122. assert exp(2*x + 7).subs(exp(3*x), y) == y**Rational(2, 3) * exp(7)
  123. x = symbols('x', positive=True)
  124. assert exp(3*log(x)).subs(x**2, y) == y**Rational(3, 2)
  125. # differentiate between E and exp
  126. assert exp(exp(x + E)).subs(exp, 3) == 3**(3**(x + E))
  127. assert exp(exp(x + E)).subs(exp, sin) == sin(sin(x + E))
  128. assert exp(exp(x + E)).subs(E, 3) == 3**(3**(x + 3))
  129. assert exp(3).subs(E, sin) == sin(3)
  130. def test_exp_adjoint():
  131. assert adjoint(exp(x)) == exp(adjoint(x))
  132. def test_exp_conjugate():
  133. assert conjugate(exp(x)) == exp(conjugate(x))
  134. @_both_exp_pow
  135. def test_exp_transpose():
  136. assert transpose(exp(x)) == exp(transpose(x))
  137. @_both_exp_pow
  138. def test_exp_rewrite():
  139. assert exp(x).rewrite(sin) == sinh(x) + cosh(x)
  140. assert exp(x*I).rewrite(cos) == cos(x) + I*sin(x)
  141. assert exp(1).rewrite(cos) == sinh(1) + cosh(1)
  142. assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
  143. assert exp(1).rewrite(sin) == sinh(1) + cosh(1)
  144. assert exp(x).rewrite(tanh) == (1 + tanh(x/2))/(1 - tanh(x/2))
  145. assert exp(pi*I/4).rewrite(sqrt) == sqrt(2)/2 + sqrt(2)*I/2
  146. assert exp(pi*I/3).rewrite(sqrt) == S.Half + sqrt(3)*I/2
  147. if not global_parameters.exp_is_pow:
  148. assert exp(x*log(y)).rewrite(Pow) == y**x
  149. assert exp(log(x)*log(y)).rewrite(Pow) in [x**log(y), y**log(x)]
  150. assert exp(log(log(x))*y).rewrite(Pow) == log(x)**y
  151. n = Symbol('n', integer=True)
  152. assert Sum((exp(pi*I/2)/2)**n, (n, 0, oo)).rewrite(sqrt).doit() == Rational(4, 5) + I*2/5
  153. assert Sum((exp(pi*I/4)/2)**n, (n, 0, oo)).rewrite(sqrt).doit() == 1/(1 - sqrt(2)*(1 + I)/4)
  154. assert (Sum((exp(pi*I/3)/2)**n, (n, 0, oo)).rewrite(sqrt).doit().cancel()
  155. == 4*I/(sqrt(3) + 3*I))
  156. @_both_exp_pow
  157. def test_exp_leading_term():
  158. assert exp(x).as_leading_term(x) == 1
  159. assert exp(2 + x).as_leading_term(x) == exp(2)
  160. assert exp((2*x + 3) / (x+1)).as_leading_term(x) == exp(3)
  161. # The following tests are commented, since now SymPy returns the
  162. # original function when the leading term in the series expansion does
  163. # not exist.
  164. # raises(NotImplementedError, lambda: exp(1/x).as_leading_term(x))
  165. # raises(NotImplementedError, lambda: exp((x + 1) / x**2).as_leading_term(x))
  166. # raises(NotImplementedError, lambda: exp(x + 1/x).as_leading_term(x))
  167. @_both_exp_pow
  168. def test_exp_taylor_term():
  169. x = symbols('x')
  170. assert exp(x).taylor_term(1, x) == x
  171. assert exp(x).taylor_term(3, x) == x**3/6
  172. assert exp(x).taylor_term(4, x) == x**4/24
  173. assert exp(x).taylor_term(-1, x) is S.Zero
  174. def test_exp_MatrixSymbol():
  175. A = MatrixSymbol("A", 2, 2)
  176. assert exp(A).has(exp)
  177. def test_exp_fdiff():
  178. x = Symbol('x')
  179. raises(ArgumentIndexError, lambda: exp(x).fdiff(2))
  180. def test_log_values():
  181. assert log(nan) is nan
  182. assert log(oo) is oo
  183. assert log(-oo) is oo
  184. assert log(zoo) is zoo
  185. assert log(-zoo) is zoo
  186. assert log(0) is zoo
  187. assert log(1) == 0
  188. assert log(-1) == I*pi
  189. assert log(E) == 1
  190. assert log(-E).expand() == 1 + I*pi
  191. assert unchanged(log, pi)
  192. assert log(-pi).expand() == log(pi) + I*pi
  193. assert unchanged(log, 17)
  194. assert log(-17) == log(17) + I*pi
  195. assert log(I) == I*pi/2
  196. assert log(-I) == -I*pi/2
  197. assert log(17*I) == I*pi/2 + log(17)
  198. assert log(-17*I).expand() == -I*pi/2 + log(17)
  199. assert log(oo*I) is oo
  200. assert log(-oo*I) is oo
  201. assert log(0, 2) is zoo
  202. assert log(0, 5) is zoo
  203. assert exp(-log(3))**(-1) == 3
  204. assert log(S.Half) == -log(2)
  205. assert log(2*3).func is log
  206. assert log(2*3**2).func is log
  207. def test_match_real_imag():
  208. x, y = symbols('x,y', real=True)
  209. i = Symbol('i', imaginary=True)
  210. assert match_real_imag(S.One) == (1, 0)
  211. assert match_real_imag(I) == (0, 1)
  212. assert match_real_imag(3 - 5*I) == (3, -5)
  213. assert match_real_imag(-sqrt(3) + S.Half*I) == (-sqrt(3), S.Half)
  214. assert match_real_imag(x + y*I) == (x, y)
  215. assert match_real_imag(x*I + y*I) == (0, x + y)
  216. assert match_real_imag((x + y)*I) == (0, x + y)
  217. assert match_real_imag(Rational(-2, 3)*i*I) == (None, None)
  218. assert match_real_imag(1 - 2*i) == (None, None)
  219. assert match_real_imag(sqrt(2)*(3 - 5*I)) == (None, None)
  220. def test_log_exact():
  221. # check for pi/2, pi/3, pi/4, pi/6, pi/8, pi/12; pi/5, pi/10:
  222. for n in range(-23, 24):
  223. if gcd(n, 24) != 1:
  224. assert log(exp(n*I*pi/24).rewrite(sqrt)) == n*I*pi/24
  225. for n in range(-9, 10):
  226. assert log(exp(n*I*pi/10).rewrite(sqrt)) == n*I*pi/10
  227. assert log(S.Half - I*sqrt(3)/2) == -I*pi/3
  228. assert log(Rational(-1, 2) + I*sqrt(3)/2) == I*pi*Rational(2, 3)
  229. assert log(-sqrt(2)/2 - I*sqrt(2)/2) == -I*pi*Rational(3, 4)
  230. assert log(-sqrt(3)/2 - I*S.Half) == -I*pi*Rational(5, 6)
  231. assert log(Rational(-1, 4) + sqrt(5)/4 - I*sqrt(sqrt(5)/8 + Rational(5, 8))) == -I*pi*Rational(2, 5)
  232. assert log(sqrt(Rational(5, 8) - sqrt(5)/8) + I*(Rational(1, 4) + sqrt(5)/4)) == I*pi*Rational(3, 10)
  233. assert log(-sqrt(sqrt(2)/4 + S.Half) + I*sqrt(S.Half - sqrt(2)/4)) == I*pi*Rational(7, 8)
  234. assert log(-sqrt(6)/4 - sqrt(2)/4 + I*(-sqrt(6)/4 + sqrt(2)/4)) == -I*pi*Rational(11, 12)
  235. assert log(-1 + I*sqrt(3)) == log(2) + I*pi*Rational(2, 3)
  236. assert log(5 + 5*I) == log(5*sqrt(2)) + I*pi/4
  237. assert log(sqrt(-12)) == log(2*sqrt(3)) + I*pi/2
  238. assert log(-sqrt(6) + sqrt(2) - I*sqrt(6) - I*sqrt(2)) == log(4) - I*pi*Rational(7, 12)
  239. assert log(-sqrt(6-3*sqrt(2)) - I*sqrt(6+3*sqrt(2))) == log(2*sqrt(3)) - I*pi*Rational(5, 8)
  240. assert log(1 + I*sqrt(2-sqrt(2))/sqrt(2+sqrt(2))) == log(2/sqrt(sqrt(2) + 2)) + I*pi/8
  241. assert log(cos(pi*Rational(7, 12)) + I*sin(pi*Rational(7, 12))) == I*pi*Rational(7, 12)
  242. assert log(cos(pi*Rational(6, 5)) + I*sin(pi*Rational(6, 5))) == I*pi*Rational(-4, 5)
  243. assert log(5*(1 + I)/sqrt(2)) == log(5) + I*pi/4
  244. assert log(sqrt(2)*(-sqrt(3) + 1 - sqrt(3)*I - I)) == log(4) - I*pi*Rational(7, 12)
  245. assert log(-sqrt(2)*(1 - I*sqrt(3))) == log(2*sqrt(2)) + I*pi*Rational(2, 3)
  246. assert log(sqrt(3)*I*(-sqrt(6 - 3*sqrt(2)) - I*sqrt(3*sqrt(2) + 6))) == log(6) - I*pi/8
  247. zero = (1 + sqrt(2))**2 - 3 - 2*sqrt(2)
  248. assert log(zero - I*sqrt(3)) == log(sqrt(3)) - I*pi/2
  249. assert unchanged(log, zero + I*zero) or log(zero + zero*I) is zoo
  250. # bail quickly if no obvious simplification is possible:
  251. assert unchanged(log, (sqrt(2)-1/sqrt(sqrt(3)+I))**1000)
  252. # beware of non-real coefficients
  253. assert unchanged(log, sqrt(2-sqrt(5))*(1 + I))
  254. def test_log_base():
  255. assert log(1, 2) == 0
  256. assert log(2, 2) == 1
  257. assert log(3, 2) == log(3)/log(2)
  258. assert log(6, 2) == 1 + log(3)/log(2)
  259. assert log(6, 3) == 1 + log(2)/log(3)
  260. assert log(2**3, 2) == 3
  261. assert log(3**3, 3) == 3
  262. assert log(5, 1) is zoo
  263. assert log(1, 1) is nan
  264. assert log(Rational(2, 3), 10) == log(Rational(2, 3))/log(10)
  265. assert log(Rational(2, 3), Rational(1, 3)) == -log(2)/log(3) + 1
  266. assert log(Rational(2, 3), Rational(2, 5)) == \
  267. log(Rational(2, 3))/log(Rational(2, 5))
  268. # issue 17148
  269. assert log(Rational(8, 3), 2) == -log(3)/log(2) + 3
  270. def test_log_symbolic():
  271. assert log(x, exp(1)) == log(x)
  272. assert log(exp(x)) != x
  273. assert log(x, exp(1)) == log(x)
  274. assert log(x*y) != log(x) + log(y)
  275. assert log(x/y).expand() != log(x) - log(y)
  276. assert log(x/y).expand(force=True) == log(x) - log(y)
  277. assert log(x**y).expand() != y*log(x)
  278. assert log(x**y).expand(force=True) == y*log(x)
  279. assert log(x, 2) == log(x)/log(2)
  280. assert log(E, 2) == 1/log(2)
  281. p, q = symbols('p,q', positive=True)
  282. r = Symbol('r', real=True)
  283. assert log(p**2) != 2*log(p)
  284. assert log(p**2).expand() == 2*log(p)
  285. assert log(x**2).expand() != 2*log(x)
  286. assert log(p**q) != q*log(p)
  287. assert log(exp(p)) == p
  288. assert log(p*q) != log(p) + log(q)
  289. assert log(p*q).expand() == log(p) + log(q)
  290. assert log(-sqrt(3)) == log(sqrt(3)) + I*pi
  291. assert log(-exp(p)) != p + I*pi
  292. assert log(-exp(x)).expand() != x + I*pi
  293. assert log(-exp(r)).expand() == r + I*pi
  294. assert log(x**y) != y*log(x)
  295. assert (log(x**-5)**-1).expand() != -1/log(x)/5
  296. assert (log(p**-5)**-1).expand() == -1/log(p)/5
  297. assert log(-x).func is log and log(-x).args[0] == -x
  298. assert log(-p).func is log and log(-p).args[0] == -p
  299. def test_log_exp():
  300. assert log(exp(4*I*pi)) == 0 # exp evaluates
  301. assert log(exp(-5*I*pi)) == I*pi # exp evaluates
  302. assert log(exp(I*pi*Rational(19, 4))) == I*pi*Rational(3, 4)
  303. assert log(exp(I*pi*Rational(25, 7))) == I*pi*Rational(-3, 7)
  304. assert log(exp(-5*I)) == -5*I + 2*I*pi
  305. @_both_exp_pow
  306. def test_exp_assumptions():
  307. r = Symbol('r', real=True)
  308. i = Symbol('i', imaginary=True)
  309. for e in exp, exp_polar:
  310. assert e(x).is_real is None
  311. assert e(x).is_imaginary is None
  312. assert e(i).is_real is None
  313. assert e(i).is_imaginary is None
  314. assert e(r).is_real is True
  315. assert e(r).is_imaginary is False
  316. assert e(re(x)).is_extended_real is True
  317. assert e(re(x)).is_imaginary is False
  318. assert Pow(E, I*pi, evaluate=False).is_imaginary == False
  319. assert Pow(E, 2*I*pi, evaluate=False).is_imaginary == False
  320. assert Pow(E, I*pi/2, evaluate=False).is_imaginary == True
  321. assert Pow(E, I*pi/3, evaluate=False).is_imaginary is None
  322. assert exp(0, evaluate=False).is_algebraic
  323. a = Symbol('a', algebraic=True)
  324. an = Symbol('an', algebraic=True, nonzero=True)
  325. r = Symbol('r', rational=True)
  326. rn = Symbol('rn', rational=True, nonzero=True)
  327. assert exp(a).is_algebraic is None
  328. assert exp(an).is_algebraic is False
  329. assert exp(pi*r).is_algebraic is None
  330. assert exp(pi*rn).is_algebraic is False
  331. assert exp(0, evaluate=False).is_algebraic is True
  332. assert exp(I*pi/3, evaluate=False).is_algebraic is True
  333. assert exp(I*pi*r, evaluate=False).is_algebraic is True
  334. @_both_exp_pow
  335. def test_exp_AccumBounds():
  336. assert exp(AccumBounds(1, 2)) == AccumBounds(E, E**2)
  337. def test_log_assumptions():
  338. p = symbols('p', positive=True)
  339. n = symbols('n', negative=True)
  340. z = symbols('z', zero=True)
  341. x = symbols('x', infinite=True, extended_positive=True)
  342. assert log(z).is_positive is False
  343. assert log(x).is_extended_positive is True
  344. assert log(2) > 0
  345. assert log(1, evaluate=False).is_zero
  346. assert log(1 + z).is_zero
  347. assert log(p).is_zero is None
  348. assert log(n).is_zero is False
  349. assert log(0.5).is_negative is True
  350. assert log(exp(p) + 1).is_positive
  351. assert log(1, evaluate=False).is_algebraic
  352. assert log(42, evaluate=False).is_algebraic is False
  353. assert log(1 + z).is_rational
  354. def test_log_hashing():
  355. assert x != log(log(x))
  356. assert hash(x) != hash(log(log(x)))
  357. assert log(x) != log(log(log(x)))
  358. e = 1/log(log(x) + log(log(x)))
  359. assert e.base.func is log
  360. e = 1/log(log(x) + log(log(log(x))))
  361. assert e.base.func is log
  362. e = log(log(x))
  363. assert e.func is log
  364. assert x.func is not log
  365. assert hash(log(log(x))) != hash(x)
  366. assert e != x
  367. def test_log_sign():
  368. assert sign(log(2)) == 1
  369. def test_log_expand_complex():
  370. assert log(1 + I).expand(complex=True) == log(2)/2 + I*pi/4
  371. assert log(1 - sqrt(2)).expand(complex=True) == log(sqrt(2) - 1) + I*pi
  372. def test_log_apply_evalf():
  373. value = (log(3)/log(2) - 1).evalf()
  374. assert value.epsilon_eq(Float("0.58496250072115618145373"))
  375. def test_log_nseries():
  376. assert log(x - 1)._eval_nseries(x, 4, None, I) == I*pi - x - x**2/2 - x**3/3 + O(x**4)
  377. assert log(x - 1)._eval_nseries(x, 4, None, -I) == -I*pi - x - x**2/2 - x**3/3 + O(x**4)
  378. assert log(I*x + I*x**3 - 1)._eval_nseries(x, 3, None, 1) == I*pi - I*x + x**2/2 + O(x**3)
  379. assert log(I*x + I*x**3 - 1)._eval_nseries(x, 3, None, -1) == -I*pi - I*x + x**2/2 + O(x**3)
  380. assert log(I*x**2 + I*x**3 - 1)._eval_nseries(x, 3, None, 1) == I*pi - I*x**2 + O(x**3)
  381. assert log(I*x**2 + I*x**3 - 1)._eval_nseries(x, 3, None, -1) == I*pi - I*x**2 + O(x**3)
  382. def test_log_expand():
  383. w = Symbol("w", positive=True)
  384. e = log(w**(log(5)/log(3)))
  385. assert e.expand() == log(5)/log(3) * log(w)
  386. x, y, z = symbols('x,y,z', positive=True)
  387. assert log(x*(y + z)).expand(mul=False) == log(x) + log(y + z)
  388. assert log(log(x**2)*log(y*z)).expand() in [log(2*log(x)*log(y) +
  389. 2*log(x)*log(z)), log(log(x)*log(z) + log(y)*log(x)) + log(2),
  390. log((log(y) + log(z))*log(x)) + log(2)]
  391. assert log(x**log(x**2)).expand(deep=False) == log(x)*log(x**2)
  392. assert log(x**log(x**2)).expand() == 2*log(x)**2
  393. x, y = symbols('x,y')
  394. assert log(x*y).expand(force=True) == log(x) + log(y)
  395. assert log(x**y).expand(force=True) == y*log(x)
  396. assert log(exp(x)).expand(force=True) == x
  397. # there's generally no need to expand out logs since this requires
  398. # factoring and if simplification is sought, it's cheaper to put
  399. # logs together than it is to take them apart.
  400. assert log(2*3**2).expand() != 2*log(3) + log(2)
  401. @XFAIL
  402. def test_log_expand_fail():
  403. x, y, z = symbols('x,y,z', positive=True)
  404. assert (log(x*(y + z))*(x + y)).expand(mul=True, log=True) == y*log(
  405. x) + y*log(y + z) + z*log(x) + z*log(y + z)
  406. def test_log_simplify():
  407. x = Symbol("x", positive=True)
  408. assert log(x**2).expand() == 2*log(x)
  409. assert expand_log(log(x**(2 + log(2)))) == (2 + log(2))*log(x)
  410. z = Symbol('z')
  411. assert log(sqrt(z)).expand() == log(z)/2
  412. assert expand_log(log(z**(log(2) - 1))) == (log(2) - 1)*log(z)
  413. assert log(z**(-1)).expand() != -log(z)
  414. assert log(z**(x/(x+1))).expand() == x*log(z)/(x + 1)
  415. def test_log_AccumBounds():
  416. assert log(AccumBounds(1, E)) == AccumBounds(0, 1)
  417. @_both_exp_pow
  418. def test_lambertw():
  419. k = Symbol('k')
  420. assert LambertW(x, 0) == LambertW(x)
  421. assert LambertW(x, 0, evaluate=False) != LambertW(x)
  422. assert LambertW(0) == 0
  423. assert LambertW(E) == 1
  424. assert LambertW(-1/E) == -1
  425. assert LambertW(-log(2)/2) == -log(2)
  426. assert LambertW(oo) is oo
  427. assert LambertW(0, 1) is -oo
  428. assert LambertW(0, 42) is -oo
  429. assert LambertW(-pi/2, -1) == -I*pi/2
  430. assert LambertW(-1/E, -1) == -1
  431. assert LambertW(-2*exp(-2), -1) == -2
  432. assert LambertW(2*log(2)) == log(2)
  433. assert LambertW(-pi/2) == I*pi/2
  434. assert LambertW(exp(1 + E)) == E
  435. assert LambertW(x**2).diff(x) == 2*LambertW(x**2)/x/(1 + LambertW(x**2))
  436. assert LambertW(x, k).diff(x) == LambertW(x, k)/x/(1 + LambertW(x, k))
  437. assert LambertW(sqrt(2)).evalf(30).epsilon_eq(
  438. Float("0.701338383413663009202120278965", 30), 1e-29)
  439. assert re(LambertW(2, -1)).evalf().epsilon_eq(Float("-0.834310366631110"))
  440. assert LambertW(-1).is_real is False # issue 5215
  441. assert LambertW(2, evaluate=False).is_real
  442. p = Symbol('p', positive=True)
  443. assert LambertW(p, evaluate=False).is_real
  444. assert LambertW(p - 1, evaluate=False).is_real is None
  445. assert LambertW(-p - 2/S.Exp1, evaluate=False).is_real is False
  446. assert LambertW(S.Half, -1, evaluate=False).is_real is False
  447. assert LambertW(Rational(-1, 10), -1, evaluate=False).is_real
  448. assert LambertW(-10, -1, evaluate=False).is_real is False
  449. assert LambertW(-2, 2, evaluate=False).is_real is False
  450. assert LambertW(0, evaluate=False).is_algebraic
  451. na = Symbol('na', nonzero=True, algebraic=True)
  452. assert LambertW(na).is_algebraic is False
  453. assert LambertW(p).is_zero is False
  454. n = Symbol('n', negative=True)
  455. assert LambertW(n).is_zero is False
  456. def test_issue_5673():
  457. e = LambertW(-1)
  458. assert e.is_comparable is False
  459. assert e.is_positive is not True
  460. e2 = 1 - 1/(1 - exp(-1000))
  461. assert e2.is_positive is not True
  462. e3 = -2 + exp(exp(LambertW(log(2)))*LambertW(log(2)))
  463. assert e3.is_nonzero is not True
  464. def test_log_fdiff():
  465. x = Symbol('x')
  466. raises(ArgumentIndexError, lambda: log(x).fdiff(2))
  467. def test_log_taylor_term():
  468. x = symbols('x')
  469. assert log(x).taylor_term(0, x) == x
  470. assert log(x).taylor_term(1, x) == -x**2/2
  471. assert log(x).taylor_term(4, x) == x**5/5
  472. assert log(x).taylor_term(-1, x) is S.Zero
  473. def test_exp_expand_NC():
  474. A, B, C = symbols('A,B,C', commutative=False)
  475. assert exp(A + B).expand() == exp(A + B)
  476. assert exp(A + B + C).expand() == exp(A + B + C)
  477. assert exp(x + y).expand() == exp(x)*exp(y)
  478. assert exp(x + y + z).expand() == exp(x)*exp(y)*exp(z)
  479. @_both_exp_pow
  480. def test_as_numer_denom():
  481. n = symbols('n', negative=True)
  482. assert exp(x).as_numer_denom() == (exp(x), 1)
  483. assert exp(-x).as_numer_denom() == (1, exp(x))
  484. assert exp(-2*x).as_numer_denom() == (1, exp(2*x))
  485. assert exp(-2).as_numer_denom() == (1, exp(2))
  486. assert exp(n).as_numer_denom() == (1, exp(-n))
  487. assert exp(-n).as_numer_denom() == (exp(-n), 1)
  488. assert exp(-I*x).as_numer_denom() == (1, exp(I*x))
  489. assert exp(-I*n).as_numer_denom() == (1, exp(I*n))
  490. assert exp(-n).as_numer_denom() == (exp(-n), 1)
  491. @_both_exp_pow
  492. def test_polar():
  493. x, y = symbols('x y', polar=True)
  494. assert abs(exp_polar(I*4)) == 1
  495. assert abs(exp_polar(0)) == 1
  496. assert abs(exp_polar(2 + 3*I)) == exp(2)
  497. assert exp_polar(I*10).n() == exp_polar(I*10)
  498. assert log(exp_polar(z)) == z
  499. assert log(x*y).expand() == log(x) + log(y)
  500. assert log(x**z).expand() == z*log(x)
  501. assert exp_polar(3).exp == 3
  502. # Compare exp(1.0*pi*I).
  503. assert (exp_polar(1.0*pi*I).n(n=5)).as_real_imag()[1] >= 0
  504. assert exp_polar(0).is_rational is True # issue 8008
  505. def test_exp_summation():
  506. w = symbols("w")
  507. m, n, i, j = symbols("m n i j")
  508. expr = exp(Sum(w*i, (i, 0, n), (j, 0, m)))
  509. assert expr.expand() == Product(exp(w*i), (i, 0, n), (j, 0, m))
  510. def test_log_product():
  511. from sympy.abc import n, m
  512. i, j = symbols('i,j', positive=True, integer=True)
  513. x, y = symbols('x,y', positive=True)
  514. z = symbols('z', real=True)
  515. w = symbols('w')
  516. expr = log(Product(x**i, (i, 1, n)))
  517. assert simplify(expr) == expr
  518. assert expr.expand() == Sum(i*log(x), (i, 1, n))
  519. expr = log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))
  520. assert simplify(expr) == expr
  521. assert expr.expand() == Sum(i*log(x) + j*log(y), (i, 1, n), (j, 1, m))
  522. expr = log(Product(-2, (n, 0, 4)))
  523. assert simplify(expr) == expr
  524. assert expr.expand() == expr
  525. assert expr.expand(force=True) == Sum(log(-2), (n, 0, 4))
  526. expr = log(Product(exp(z*i), (i, 0, n)))
  527. assert expr.expand() == Sum(z*i, (i, 0, n))
  528. expr = log(Product(exp(w*i), (i, 0, n)))
  529. assert expr.expand() == expr
  530. assert expr.expand(force=True) == Sum(w*i, (i, 0, n))
  531. expr = log(Product(i**2*abs(j), (i, 1, n), (j, 1, m)))
  532. assert expr.expand() == Sum(2*log(i) + log(j), (i, 1, n), (j, 1, m))
  533. @XFAIL
  534. def test_log_product_simplify_to_sum():
  535. from sympy.abc import n, m
  536. i, j = symbols('i,j', positive=True, integer=True)
  537. x, y = symbols('x,y', positive=True)
  538. assert simplify(log(Product(x**i, (i, 1, n)))) == Sum(i*log(x), (i, 1, n))
  539. assert simplify(log(Product(x**i*y**j, (i, 1, n), (j, 1, m)))) == \
  540. Sum(i*log(x) + j*log(y), (i, 1, n), (j, 1, m))
  541. def test_issue_8866():
  542. assert simplify(log(x, 10, evaluate=False)) == simplify(log(x, 10))
  543. assert expand_log(log(x, 10, evaluate=False)) == expand_log(log(x, 10))
  544. y = Symbol('y', positive=True)
  545. l1 = log(exp(y), exp(10))
  546. b1 = log(exp(y), exp(5))
  547. l2 = log(exp(y), exp(10), evaluate=False)
  548. b2 = log(exp(y), exp(5), evaluate=False)
  549. assert simplify(log(l1, b1)) == simplify(log(l2, b2))
  550. assert expand_log(log(l1, b1)) == expand_log(log(l2, b2))
  551. def test_log_expand_factor():
  552. assert (log(18)/log(3) - 2).expand(factor=True) == log(2)/log(3)
  553. assert (log(12)/log(2)).expand(factor=True) == log(3)/log(2) + 2
  554. assert (log(15)/log(3)).expand(factor=True) == 1 + log(5)/log(3)
  555. assert (log(2)/(-log(12) + log(24))).expand(factor=True) == 1
  556. assert expand_log(log(12), factor=True) == log(3) + 2*log(2)
  557. assert expand_log(log(21)/log(7), factor=False) == log(3)/log(7) + 1
  558. assert expand_log(log(45)/log(5) + log(20), factor=False) == \
  559. 1 + 2*log(3)/log(5) + log(20)
  560. assert expand_log(log(45)/log(5) + log(26), factor=True) == \
  561. log(2) + log(13) + (log(5) + 2*log(3))/log(5)
  562. def test_issue_9116():
  563. n = Symbol('n', positive=True, integer=True)
  564. assert log(n).is_nonnegative is True