图片解析应用
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.

1170 lines
36 KiB

  1. from sympy.calculus.accumulationbounds import AccumBounds
  2. from sympy.core.function import (expand_mul, expand_trig)
  3. from sympy.core.numbers import (E, I, Integer, Rational, nan, oo, pi, zoo)
  4. from sympy.core.singleton import S
  5. from sympy.core.symbol import (Symbol, symbols)
  6. from sympy.functions.elementary.complexes import (im, re)
  7. from sympy.functions.elementary.exponential import (exp, log)
  8. from sympy.functions.elementary.hyperbolic import (acosh, acoth, acsch, asech, asinh, atanh, cosh, coth, csch, sech, sinh, tanh)
  9. from sympy.functions.elementary.miscellaneous import sqrt
  10. from sympy.functions.elementary.trigonometric import (acos, asin, cos, cot, sec, sin, tan)
  11. from sympy.series.order import O
  12. from sympy.core.expr import unchanged
  13. from sympy.core.function import ArgumentIndexError
  14. from sympy.testing.pytest import raises
  15. def test_sinh():
  16. x, y = symbols('x,y')
  17. k = Symbol('k', integer=True)
  18. assert sinh(nan) is nan
  19. assert sinh(zoo) is nan
  20. assert sinh(oo) is oo
  21. assert sinh(-oo) is -oo
  22. assert sinh(0) == 0
  23. assert unchanged(sinh, 1)
  24. assert sinh(-1) == -sinh(1)
  25. assert unchanged(sinh, x)
  26. assert sinh(-x) == -sinh(x)
  27. assert unchanged(sinh, pi)
  28. assert sinh(-pi) == -sinh(pi)
  29. assert unchanged(sinh, 2**1024 * E)
  30. assert sinh(-2**1024 * E) == -sinh(2**1024 * E)
  31. assert sinh(pi*I) == 0
  32. assert sinh(-pi*I) == 0
  33. assert sinh(2*pi*I) == 0
  34. assert sinh(-2*pi*I) == 0
  35. assert sinh(-3*10**73*pi*I) == 0
  36. assert sinh(7*10**103*pi*I) == 0
  37. assert sinh(pi*I/2) == I
  38. assert sinh(-pi*I/2) == -I
  39. assert sinh(pi*I*Rational(5, 2)) == I
  40. assert sinh(pi*I*Rational(7, 2)) == -I
  41. assert sinh(pi*I/3) == S.Half*sqrt(3)*I
  42. assert sinh(pi*I*Rational(-2, 3)) == Rational(-1, 2)*sqrt(3)*I
  43. assert sinh(pi*I/4) == S.Half*sqrt(2)*I
  44. assert sinh(-pi*I/4) == Rational(-1, 2)*sqrt(2)*I
  45. assert sinh(pi*I*Rational(17, 4)) == S.Half*sqrt(2)*I
  46. assert sinh(pi*I*Rational(-3, 4)) == Rational(-1, 2)*sqrt(2)*I
  47. assert sinh(pi*I/6) == S.Half*I
  48. assert sinh(-pi*I/6) == Rational(-1, 2)*I
  49. assert sinh(pi*I*Rational(7, 6)) == Rational(-1, 2)*I
  50. assert sinh(pi*I*Rational(-5, 6)) == Rational(-1, 2)*I
  51. assert sinh(pi*I/105) == sin(pi/105)*I
  52. assert sinh(-pi*I/105) == -sin(pi/105)*I
  53. assert unchanged(sinh, 2 + 3*I)
  54. assert sinh(x*I) == sin(x)*I
  55. assert sinh(k*pi*I) == 0
  56. assert sinh(17*k*pi*I) == 0
  57. assert sinh(k*pi*I/2) == sin(k*pi/2)*I
  58. assert sinh(x).as_real_imag(deep=False) == (cos(im(x))*sinh(re(x)),
  59. sin(im(x))*cosh(re(x)))
  60. x = Symbol('x', extended_real=True)
  61. assert sinh(x).as_real_imag(deep=False) == (sinh(x), 0)
  62. x = Symbol('x', real=True)
  63. assert sinh(I*x).is_finite is True
  64. assert sinh(x).is_real is True
  65. assert sinh(I).is_real is False
  66. p = Symbol('p', positive=True)
  67. assert sinh(p).is_zero is False
  68. assert sinh(0, evaluate=False).is_zero is True
  69. assert sinh(2*pi*I, evaluate=False).is_zero is True
  70. def test_sinh_series():
  71. x = Symbol('x')
  72. assert sinh(x).series(x, 0, 10) == \
  73. x + x**3/6 + x**5/120 + x**7/5040 + x**9/362880 + O(x**10)
  74. def test_sinh_fdiff():
  75. x = Symbol('x')
  76. raises(ArgumentIndexError, lambda: sinh(x).fdiff(2))
  77. def test_cosh():
  78. x, y = symbols('x,y')
  79. k = Symbol('k', integer=True)
  80. assert cosh(nan) is nan
  81. assert cosh(zoo) is nan
  82. assert cosh(oo) is oo
  83. assert cosh(-oo) is oo
  84. assert cosh(0) == 1
  85. assert unchanged(cosh, 1)
  86. assert cosh(-1) == cosh(1)
  87. assert unchanged(cosh, x)
  88. assert cosh(-x) == cosh(x)
  89. assert cosh(pi*I) == cos(pi)
  90. assert cosh(-pi*I) == cos(pi)
  91. assert unchanged(cosh, 2**1024 * E)
  92. assert cosh(-2**1024 * E) == cosh(2**1024 * E)
  93. assert cosh(pi*I/2) == 0
  94. assert cosh(-pi*I/2) == 0
  95. assert cosh((-3*10**73 + 1)*pi*I/2) == 0
  96. assert cosh((7*10**103 + 1)*pi*I/2) == 0
  97. assert cosh(pi*I) == -1
  98. assert cosh(-pi*I) == -1
  99. assert cosh(5*pi*I) == -1
  100. assert cosh(8*pi*I) == 1
  101. assert cosh(pi*I/3) == S.Half
  102. assert cosh(pi*I*Rational(-2, 3)) == Rational(-1, 2)
  103. assert cosh(pi*I/4) == S.Half*sqrt(2)
  104. assert cosh(-pi*I/4) == S.Half*sqrt(2)
  105. assert cosh(pi*I*Rational(11, 4)) == Rational(-1, 2)*sqrt(2)
  106. assert cosh(pi*I*Rational(-3, 4)) == Rational(-1, 2)*sqrt(2)
  107. assert cosh(pi*I/6) == S.Half*sqrt(3)
  108. assert cosh(-pi*I/6) == S.Half*sqrt(3)
  109. assert cosh(pi*I*Rational(7, 6)) == Rational(-1, 2)*sqrt(3)
  110. assert cosh(pi*I*Rational(-5, 6)) == Rational(-1, 2)*sqrt(3)
  111. assert cosh(pi*I/105) == cos(pi/105)
  112. assert cosh(-pi*I/105) == cos(pi/105)
  113. assert unchanged(cosh, 2 + 3*I)
  114. assert cosh(x*I) == cos(x)
  115. assert cosh(k*pi*I) == cos(k*pi)
  116. assert cosh(17*k*pi*I) == cos(17*k*pi)
  117. assert unchanged(cosh, k*pi)
  118. assert cosh(x).as_real_imag(deep=False) == (cos(im(x))*cosh(re(x)),
  119. sin(im(x))*sinh(re(x)))
  120. x = Symbol('x', extended_real=True)
  121. assert cosh(x).as_real_imag(deep=False) == (cosh(x), 0)
  122. x = Symbol('x', real=True)
  123. assert cosh(I*x).is_finite is True
  124. assert cosh(I*x).is_real is True
  125. assert cosh(I*2 + 1).is_real is False
  126. assert cosh(5*I*S.Pi/2, evaluate=False).is_zero is True
  127. assert cosh(x).is_zero is False
  128. def test_cosh_series():
  129. x = Symbol('x')
  130. assert cosh(x).series(x, 0, 10) == \
  131. 1 + x**2/2 + x**4/24 + x**6/720 + x**8/40320 + O(x**10)
  132. def test_cosh_fdiff():
  133. x = Symbol('x')
  134. raises(ArgumentIndexError, lambda: cosh(x).fdiff(2))
  135. def test_tanh():
  136. x, y = symbols('x,y')
  137. k = Symbol('k', integer=True)
  138. assert tanh(nan) is nan
  139. assert tanh(zoo) is nan
  140. assert tanh(oo) == 1
  141. assert tanh(-oo) == -1
  142. assert tanh(0) == 0
  143. assert unchanged(tanh, 1)
  144. assert tanh(-1) == -tanh(1)
  145. assert unchanged(tanh, x)
  146. assert tanh(-x) == -tanh(x)
  147. assert unchanged(tanh, pi)
  148. assert tanh(-pi) == -tanh(pi)
  149. assert unchanged(tanh, 2**1024 * E)
  150. assert tanh(-2**1024 * E) == -tanh(2**1024 * E)
  151. assert tanh(pi*I) == 0
  152. assert tanh(-pi*I) == 0
  153. assert tanh(2*pi*I) == 0
  154. assert tanh(-2*pi*I) == 0
  155. assert tanh(-3*10**73*pi*I) == 0
  156. assert tanh(7*10**103*pi*I) == 0
  157. assert tanh(pi*I/2) is zoo
  158. assert tanh(-pi*I/2) is zoo
  159. assert tanh(pi*I*Rational(5, 2)) is zoo
  160. assert tanh(pi*I*Rational(7, 2)) is zoo
  161. assert tanh(pi*I/3) == sqrt(3)*I
  162. assert tanh(pi*I*Rational(-2, 3)) == sqrt(3)*I
  163. assert tanh(pi*I/4) == I
  164. assert tanh(-pi*I/4) == -I
  165. assert tanh(pi*I*Rational(17, 4)) == I
  166. assert tanh(pi*I*Rational(-3, 4)) == I
  167. assert tanh(pi*I/6) == I/sqrt(3)
  168. assert tanh(-pi*I/6) == -I/sqrt(3)
  169. assert tanh(pi*I*Rational(7, 6)) == I/sqrt(3)
  170. assert tanh(pi*I*Rational(-5, 6)) == I/sqrt(3)
  171. assert tanh(pi*I/105) == tan(pi/105)*I
  172. assert tanh(-pi*I/105) == -tan(pi/105)*I
  173. assert unchanged(tanh, 2 + 3*I)
  174. assert tanh(x*I) == tan(x)*I
  175. assert tanh(k*pi*I) == 0
  176. assert tanh(17*k*pi*I) == 0
  177. assert tanh(k*pi*I/2) == tan(k*pi/2)*I
  178. assert tanh(x).as_real_imag(deep=False) == (sinh(re(x))*cosh(re(x))/(cos(im(x))**2
  179. + sinh(re(x))**2),
  180. sin(im(x))*cos(im(x))/(cos(im(x))**2 + sinh(re(x))**2))
  181. x = Symbol('x', extended_real=True)
  182. assert tanh(x).as_real_imag(deep=False) == (tanh(x), 0)
  183. assert tanh(I*pi/3 + 1).is_real is False
  184. assert tanh(x).is_real is True
  185. assert tanh(I*pi*x/2).is_real is None
  186. def test_tanh_series():
  187. x = Symbol('x')
  188. assert tanh(x).series(x, 0, 10) == \
  189. x - x**3/3 + 2*x**5/15 - 17*x**7/315 + 62*x**9/2835 + O(x**10)
  190. def test_tanh_fdiff():
  191. x = Symbol('x')
  192. raises(ArgumentIndexError, lambda: tanh(x).fdiff(2))
  193. def test_coth():
  194. x, y = symbols('x,y')
  195. k = Symbol('k', integer=True)
  196. assert coth(nan) is nan
  197. assert coth(zoo) is nan
  198. assert coth(oo) == 1
  199. assert coth(-oo) == -1
  200. assert coth(0) is zoo
  201. assert unchanged(coth, 1)
  202. assert coth(-1) == -coth(1)
  203. assert unchanged(coth, x)
  204. assert coth(-x) == -coth(x)
  205. assert coth(pi*I) == -I*cot(pi)
  206. assert coth(-pi*I) == cot(pi)*I
  207. assert unchanged(coth, 2**1024 * E)
  208. assert coth(-2**1024 * E) == -coth(2**1024 * E)
  209. assert coth(pi*I) == -I*cot(pi)
  210. assert coth(-pi*I) == I*cot(pi)
  211. assert coth(2*pi*I) == -I*cot(2*pi)
  212. assert coth(-2*pi*I) == I*cot(2*pi)
  213. assert coth(-3*10**73*pi*I) == I*cot(3*10**73*pi)
  214. assert coth(7*10**103*pi*I) == -I*cot(7*10**103*pi)
  215. assert coth(pi*I/2) == 0
  216. assert coth(-pi*I/2) == 0
  217. assert coth(pi*I*Rational(5, 2)) == 0
  218. assert coth(pi*I*Rational(7, 2)) == 0
  219. assert coth(pi*I/3) == -I/sqrt(3)
  220. assert coth(pi*I*Rational(-2, 3)) == -I/sqrt(3)
  221. assert coth(pi*I/4) == -I
  222. assert coth(-pi*I/4) == I
  223. assert coth(pi*I*Rational(17, 4)) == -I
  224. assert coth(pi*I*Rational(-3, 4)) == -I
  225. assert coth(pi*I/6) == -sqrt(3)*I
  226. assert coth(-pi*I/6) == sqrt(3)*I
  227. assert coth(pi*I*Rational(7, 6)) == -sqrt(3)*I
  228. assert coth(pi*I*Rational(-5, 6)) == -sqrt(3)*I
  229. assert coth(pi*I/105) == -cot(pi/105)*I
  230. assert coth(-pi*I/105) == cot(pi/105)*I
  231. assert unchanged(coth, 2 + 3*I)
  232. assert coth(x*I) == -cot(x)*I
  233. assert coth(k*pi*I) == -cot(k*pi)*I
  234. assert coth(17*k*pi*I) == -cot(17*k*pi)*I
  235. assert coth(k*pi*I) == -cot(k*pi)*I
  236. assert coth(log(tan(2))) == coth(log(-tan(2)))
  237. assert coth(1 + I*pi/2) == tanh(1)
  238. assert coth(x).as_real_imag(deep=False) == (sinh(re(x))*cosh(re(x))/(sin(im(x))**2
  239. + sinh(re(x))**2),
  240. -sin(im(x))*cos(im(x))/(sin(im(x))**2 + sinh(re(x))**2))
  241. x = Symbol('x', extended_real=True)
  242. assert coth(x).as_real_imag(deep=False) == (coth(x), 0)
  243. assert expand_trig(coth(2*x)) == (coth(x)**2 + 1)/(2*coth(x))
  244. assert expand_trig(coth(3*x)) == (coth(x)**3 + 3*coth(x))/(1 + 3*coth(x)**2)
  245. assert expand_trig(coth(x + y)) == (1 + coth(x)*coth(y))/(coth(x) + coth(y))
  246. def test_coth_series():
  247. x = Symbol('x')
  248. assert coth(x).series(x, 0, 8) == \
  249. 1/x + x/3 - x**3/45 + 2*x**5/945 - x**7/4725 + O(x**8)
  250. def test_coth_fdiff():
  251. x = Symbol('x')
  252. raises(ArgumentIndexError, lambda: coth(x).fdiff(2))
  253. def test_csch():
  254. x, y = symbols('x,y')
  255. k = Symbol('k', integer=True)
  256. n = Symbol('n', positive=True)
  257. assert csch(nan) is nan
  258. assert csch(zoo) is nan
  259. assert csch(oo) == 0
  260. assert csch(-oo) == 0
  261. assert csch(0) is zoo
  262. assert csch(-1) == -csch(1)
  263. assert csch(-x) == -csch(x)
  264. assert csch(-pi) == -csch(pi)
  265. assert csch(-2**1024 * E) == -csch(2**1024 * E)
  266. assert csch(pi*I) is zoo
  267. assert csch(-pi*I) is zoo
  268. assert csch(2*pi*I) is zoo
  269. assert csch(-2*pi*I) is zoo
  270. assert csch(-3*10**73*pi*I) is zoo
  271. assert csch(7*10**103*pi*I) is zoo
  272. assert csch(pi*I/2) == -I
  273. assert csch(-pi*I/2) == I
  274. assert csch(pi*I*Rational(5, 2)) == -I
  275. assert csch(pi*I*Rational(7, 2)) == I
  276. assert csch(pi*I/3) == -2/sqrt(3)*I
  277. assert csch(pi*I*Rational(-2, 3)) == 2/sqrt(3)*I
  278. assert csch(pi*I/4) == -sqrt(2)*I
  279. assert csch(-pi*I/4) == sqrt(2)*I
  280. assert csch(pi*I*Rational(7, 4)) == sqrt(2)*I
  281. assert csch(pi*I*Rational(-3, 4)) == sqrt(2)*I
  282. assert csch(pi*I/6) == -2*I
  283. assert csch(-pi*I/6) == 2*I
  284. assert csch(pi*I*Rational(7, 6)) == 2*I
  285. assert csch(pi*I*Rational(-7, 6)) == -2*I
  286. assert csch(pi*I*Rational(-5, 6)) == 2*I
  287. assert csch(pi*I/105) == -1/sin(pi/105)*I
  288. assert csch(-pi*I/105) == 1/sin(pi/105)*I
  289. assert csch(x*I) == -1/sin(x)*I
  290. assert csch(k*pi*I) is zoo
  291. assert csch(17*k*pi*I) is zoo
  292. assert csch(k*pi*I/2) == -1/sin(k*pi/2)*I
  293. assert csch(n).is_real is True
  294. assert expand_trig(csch(x + y)) == 1/(sinh(x)*cosh(y) + cosh(x)*sinh(y))
  295. def test_csch_series():
  296. x = Symbol('x')
  297. assert csch(x).series(x, 0, 10) == \
  298. 1/ x - x/6 + 7*x**3/360 - 31*x**5/15120 + 127*x**7/604800 \
  299. - 73*x**9/3421440 + O(x**10)
  300. def test_csch_fdiff():
  301. x = Symbol('x')
  302. raises(ArgumentIndexError, lambda: csch(x).fdiff(2))
  303. def test_sech():
  304. x, y = symbols('x, y')
  305. k = Symbol('k', integer=True)
  306. n = Symbol('n', positive=True)
  307. assert sech(nan) is nan
  308. assert sech(zoo) is nan
  309. assert sech(oo) == 0
  310. assert sech(-oo) == 0
  311. assert sech(0) == 1
  312. assert sech(-1) == sech(1)
  313. assert sech(-x) == sech(x)
  314. assert sech(pi*I) == sec(pi)
  315. assert sech(-pi*I) == sec(pi)
  316. assert sech(-2**1024 * E) == sech(2**1024 * E)
  317. assert sech(pi*I/2) is zoo
  318. assert sech(-pi*I/2) is zoo
  319. assert sech((-3*10**73 + 1)*pi*I/2) is zoo
  320. assert sech((7*10**103 + 1)*pi*I/2) is zoo
  321. assert sech(pi*I) == -1
  322. assert sech(-pi*I) == -1
  323. assert sech(5*pi*I) == -1
  324. assert sech(8*pi*I) == 1
  325. assert sech(pi*I/3) == 2
  326. assert sech(pi*I*Rational(-2, 3)) == -2
  327. assert sech(pi*I/4) == sqrt(2)
  328. assert sech(-pi*I/4) == sqrt(2)
  329. assert sech(pi*I*Rational(5, 4)) == -sqrt(2)
  330. assert sech(pi*I*Rational(-5, 4)) == -sqrt(2)
  331. assert sech(pi*I/6) == 2/sqrt(3)
  332. assert sech(-pi*I/6) == 2/sqrt(3)
  333. assert sech(pi*I*Rational(7, 6)) == -2/sqrt(3)
  334. assert sech(pi*I*Rational(-5, 6)) == -2/sqrt(3)
  335. assert sech(pi*I/105) == 1/cos(pi/105)
  336. assert sech(-pi*I/105) == 1/cos(pi/105)
  337. assert sech(x*I) == 1/cos(x)
  338. assert sech(k*pi*I) == 1/cos(k*pi)
  339. assert sech(17*k*pi*I) == 1/cos(17*k*pi)
  340. assert sech(n).is_real is True
  341. assert expand_trig(sech(x + y)) == 1/(cosh(x)*cosh(y) + sinh(x)*sinh(y))
  342. def test_sech_series():
  343. x = Symbol('x')
  344. assert sech(x).series(x, 0, 10) == \
  345. 1 - x**2/2 + 5*x**4/24 - 61*x**6/720 + 277*x**8/8064 + O(x**10)
  346. def test_sech_fdiff():
  347. x = Symbol('x')
  348. raises(ArgumentIndexError, lambda: sech(x).fdiff(2))
  349. def test_asinh():
  350. x, y = symbols('x,y')
  351. assert unchanged(asinh, x)
  352. assert asinh(-x) == -asinh(x)
  353. #at specific points
  354. assert asinh(nan) is nan
  355. assert asinh( 0) == 0
  356. assert asinh(+1) == log(sqrt(2) + 1)
  357. assert asinh(-1) == log(sqrt(2) - 1)
  358. assert asinh(I) == pi*I/2
  359. assert asinh(-I) == -pi*I/2
  360. assert asinh(I/2) == pi*I/6
  361. assert asinh(-I/2) == -pi*I/6
  362. # at infinites
  363. assert asinh(oo) is oo
  364. assert asinh(-oo) is -oo
  365. assert asinh(I*oo) is oo
  366. assert asinh(-I *oo) is -oo
  367. assert asinh(zoo) is zoo
  368. #properties
  369. assert asinh(I *(sqrt(3) - 1)/(2**Rational(3, 2))) == pi*I/12
  370. assert asinh(-I *(sqrt(3) - 1)/(2**Rational(3, 2))) == -pi*I/12
  371. assert asinh(I*(sqrt(5) - 1)/4) == pi*I/10
  372. assert asinh(-I*(sqrt(5) - 1)/4) == -pi*I/10
  373. assert asinh(I*(sqrt(5) + 1)/4) == pi*I*Rational(3, 10)
  374. assert asinh(-I*(sqrt(5) + 1)/4) == pi*I*Rational(-3, 10)
  375. # Symmetry
  376. assert asinh(Rational(-1, 2)) == -asinh(S.Half)
  377. # inverse composition
  378. assert unchanged(asinh, sinh(Symbol('v1')))
  379. assert asinh(sinh(0, evaluate=False)) == 0
  380. assert asinh(sinh(-3, evaluate=False)) == -3
  381. assert asinh(sinh(2, evaluate=False)) == 2
  382. assert asinh(sinh(I, evaluate=False)) == I
  383. assert asinh(sinh(-I, evaluate=False)) == -I
  384. assert asinh(sinh(5*I, evaluate=False)) == -2*I*pi + 5*I
  385. assert asinh(sinh(15 + 11*I)) == 15 - 4*I*pi + 11*I
  386. assert asinh(sinh(-73 + 97*I)) == 73 - 97*I + 31*I*pi
  387. assert asinh(sinh(-7 - 23*I)) == 7 - 7*I*pi + 23*I
  388. assert asinh(sinh(13 - 3*I)) == -13 - I*pi + 3*I
  389. p = Symbol('p', positive=True)
  390. assert asinh(p).is_zero is False
  391. assert asinh(sinh(0, evaluate=False), evaluate=False).is_zero is True
  392. def test_asinh_rewrite():
  393. x = Symbol('x')
  394. assert asinh(x).rewrite(log) == log(x + sqrt(x**2 + 1))
  395. def test_asinh_series():
  396. x = Symbol('x')
  397. assert asinh(x).series(x, 0, 8) == \
  398. x - x**3/6 + 3*x**5/40 - 5*x**7/112 + O(x**8)
  399. t5 = asinh(x).taylor_term(5, x)
  400. assert t5 == 3*x**5/40
  401. assert asinh(x).taylor_term(7, x, t5, 0) == -5*x**7/112
  402. def test_asinh_fdiff():
  403. x = Symbol('x')
  404. raises(ArgumentIndexError, lambda: asinh(x).fdiff(2))
  405. def test_acosh():
  406. x = Symbol('x')
  407. assert unchanged(acosh, -x)
  408. #at specific points
  409. assert acosh(1) == 0
  410. assert acosh(-1) == pi*I
  411. assert acosh(0) == I*pi/2
  412. assert acosh(S.Half) == I*pi/3
  413. assert acosh(Rational(-1, 2)) == pi*I*Rational(2, 3)
  414. assert acosh(nan) is nan
  415. # at infinites
  416. assert acosh(oo) is oo
  417. assert acosh(-oo) is oo
  418. assert acosh(I*oo) == oo + I*pi/2
  419. assert acosh(-I*oo) == oo - I*pi/2
  420. assert acosh(zoo) is zoo
  421. assert acosh(I) == log(I*(1 + sqrt(2)))
  422. assert acosh(-I) == log(-I*(1 + sqrt(2)))
  423. assert acosh((sqrt(3) - 1)/(2*sqrt(2))) == pi*I*Rational(5, 12)
  424. assert acosh(-(sqrt(3) - 1)/(2*sqrt(2))) == pi*I*Rational(7, 12)
  425. assert acosh(sqrt(2)/2) == I*pi/4
  426. assert acosh(-sqrt(2)/2) == I*pi*Rational(3, 4)
  427. assert acosh(sqrt(3)/2) == I*pi/6
  428. assert acosh(-sqrt(3)/2) == I*pi*Rational(5, 6)
  429. assert acosh(sqrt(2 + sqrt(2))/2) == I*pi/8
  430. assert acosh(-sqrt(2 + sqrt(2))/2) == I*pi*Rational(7, 8)
  431. assert acosh(sqrt(2 - sqrt(2))/2) == I*pi*Rational(3, 8)
  432. assert acosh(-sqrt(2 - sqrt(2))/2) == I*pi*Rational(5, 8)
  433. assert acosh((1 + sqrt(3))/(2*sqrt(2))) == I*pi/12
  434. assert acosh(-(1 + sqrt(3))/(2*sqrt(2))) == I*pi*Rational(11, 12)
  435. assert acosh((sqrt(5) + 1)/4) == I*pi/5
  436. assert acosh(-(sqrt(5) + 1)/4) == I*pi*Rational(4, 5)
  437. assert str(acosh(5*I).n(6)) == '2.31244 + 1.5708*I'
  438. assert str(acosh(-5*I).n(6)) == '2.31244 - 1.5708*I'
  439. # inverse composition
  440. assert unchanged(acosh, Symbol('v1'))
  441. assert acosh(cosh(-3, evaluate=False)) == 3
  442. assert acosh(cosh(3, evaluate=False)) == 3
  443. assert acosh(cosh(0, evaluate=False)) == 0
  444. assert acosh(cosh(I, evaluate=False)) == I
  445. assert acosh(cosh(-I, evaluate=False)) == I
  446. assert acosh(cosh(7*I, evaluate=False)) == -2*I*pi + 7*I
  447. assert acosh(cosh(1 + I)) == 1 + I
  448. assert acosh(cosh(3 - 3*I)) == 3 - 3*I
  449. assert acosh(cosh(-3 + 2*I)) == 3 - 2*I
  450. assert acosh(cosh(-5 - 17*I)) == 5 - 6*I*pi + 17*I
  451. assert acosh(cosh(-21 + 11*I)) == 21 - 11*I + 4*I*pi
  452. assert acosh(cosh(cosh(1) + I)) == cosh(1) + I
  453. assert acosh(1, evaluate=False).is_zero is True
  454. def test_acosh_rewrite():
  455. x = Symbol('x')
  456. assert acosh(x).rewrite(log) == log(x + sqrt(x - 1)*sqrt(x + 1))
  457. def test_acosh_series():
  458. x = Symbol('x')
  459. assert acosh(x).series(x, 0, 8) == \
  460. -I*x + pi*I/2 - I*x**3/6 - 3*I*x**5/40 - 5*I*x**7/112 + O(x**8)
  461. t5 = acosh(x).taylor_term(5, x)
  462. assert t5 == - 3*I*x**5/40
  463. assert acosh(x).taylor_term(7, x, t5, 0) == - 5*I*x**7/112
  464. def test_acosh_fdiff():
  465. x = Symbol('x')
  466. raises(ArgumentIndexError, lambda: acosh(x).fdiff(2))
  467. def test_asech():
  468. x = Symbol('x')
  469. assert unchanged(asech, -x)
  470. # values at fixed points
  471. assert asech(1) == 0
  472. assert asech(-1) == pi*I
  473. assert asech(0) is oo
  474. assert asech(2) == I*pi/3
  475. assert asech(-2) == 2*I*pi / 3
  476. assert asech(nan) is nan
  477. # at infinites
  478. assert asech(oo) == I*pi/2
  479. assert asech(-oo) == I*pi/2
  480. assert asech(zoo) == I*AccumBounds(-pi/2, pi/2)
  481. assert asech(I) == log(1 + sqrt(2)) - I*pi/2
  482. assert asech(-I) == log(1 + sqrt(2)) + I*pi/2
  483. assert asech(sqrt(2) - sqrt(6)) == 11*I*pi / 12
  484. assert asech(sqrt(2 - 2/sqrt(5))) == I*pi / 10
  485. assert asech(-sqrt(2 - 2/sqrt(5))) == 9*I*pi / 10
  486. assert asech(2 / sqrt(2 + sqrt(2))) == I*pi / 8
  487. assert asech(-2 / sqrt(2 + sqrt(2))) == 7*I*pi / 8
  488. assert asech(sqrt(5) - 1) == I*pi / 5
  489. assert asech(1 - sqrt(5)) == 4*I*pi / 5
  490. assert asech(-sqrt(2*(2 + sqrt(2)))) == 5*I*pi / 8
  491. # properties
  492. # asech(x) == acosh(1/x)
  493. assert asech(sqrt(2)) == acosh(1/sqrt(2))
  494. assert asech(2/sqrt(3)) == acosh(sqrt(3)/2)
  495. assert asech(2/sqrt(2 + sqrt(2))) == acosh(sqrt(2 + sqrt(2))/2)
  496. assert asech(2) == acosh(S.Half)
  497. # asech(x) == I*acos(1/x)
  498. # (Note: the exact formula is asech(x) == +/- I*acos(1/x))
  499. assert asech(-sqrt(2)) == I*acos(-1/sqrt(2))
  500. assert asech(-2/sqrt(3)) == I*acos(-sqrt(3)/2)
  501. assert asech(-S(2)) == I*acos(Rational(-1, 2))
  502. assert asech(-2/sqrt(2)) == I*acos(-sqrt(2)/2)
  503. # sech(asech(x)) / x == 1
  504. assert expand_mul(sech(asech(sqrt(6) - sqrt(2))) / (sqrt(6) - sqrt(2))) == 1
  505. assert expand_mul(sech(asech(sqrt(6) + sqrt(2))) / (sqrt(6) + sqrt(2))) == 1
  506. assert (sech(asech(sqrt(2 + 2/sqrt(5)))) / (sqrt(2 + 2/sqrt(5)))).simplify() == 1
  507. assert (sech(asech(-sqrt(2 + 2/sqrt(5)))) / (-sqrt(2 + 2/sqrt(5)))).simplify() == 1
  508. assert (sech(asech(sqrt(2*(2 + sqrt(2))))) / (sqrt(2*(2 + sqrt(2))))).simplify() == 1
  509. assert expand_mul(sech(asech(1 + sqrt(5))) / (1 + sqrt(5))) == 1
  510. assert expand_mul(sech(asech(-1 - sqrt(5))) / (-1 - sqrt(5))) == 1
  511. assert expand_mul(sech(asech(-sqrt(6) - sqrt(2))) / (-sqrt(6) - sqrt(2))) == 1
  512. # numerical evaluation
  513. assert str(asech(5*I).n(6)) == '0.19869 - 1.5708*I'
  514. assert str(asech(-5*I).n(6)) == '0.19869 + 1.5708*I'
  515. def test_asech_series():
  516. x = Symbol('x')
  517. t6 = asech(x).expansion_term(6, x)
  518. assert t6 == -5*x**6/96
  519. assert asech(x).expansion_term(8, x, t6, 0) == -35*x**8/1024
  520. def test_asech_rewrite():
  521. x = Symbol('x')
  522. assert asech(x).rewrite(log) == log(1/x + sqrt(1/x - 1) * sqrt(1/x + 1))
  523. def test_asech_fdiff():
  524. x = Symbol('x')
  525. raises(ArgumentIndexError, lambda: asech(x).fdiff(2))
  526. def test_acsch():
  527. x = Symbol('x')
  528. assert unchanged(acsch, x)
  529. assert acsch(-x) == -acsch(x)
  530. # values at fixed points
  531. assert acsch(1) == log(1 + sqrt(2))
  532. assert acsch(-1) == - log(1 + sqrt(2))
  533. assert acsch(0) is zoo
  534. assert acsch(2) == log((1+sqrt(5))/2)
  535. assert acsch(-2) == - log((1+sqrt(5))/2)
  536. assert acsch(I) == - I*pi/2
  537. assert acsch(-I) == I*pi/2
  538. assert acsch(-I*(sqrt(6) + sqrt(2))) == I*pi / 12
  539. assert acsch(I*(sqrt(2) + sqrt(6))) == -I*pi / 12
  540. assert acsch(-I*(1 + sqrt(5))) == I*pi / 10
  541. assert acsch(I*(1 + sqrt(5))) == -I*pi / 10
  542. assert acsch(-I*2 / sqrt(2 - sqrt(2))) == I*pi / 8
  543. assert acsch(I*2 / sqrt(2 - sqrt(2))) == -I*pi / 8
  544. assert acsch(-I*2) == I*pi / 6
  545. assert acsch(I*2) == -I*pi / 6
  546. assert acsch(-I*sqrt(2 + 2/sqrt(5))) == I*pi / 5
  547. assert acsch(I*sqrt(2 + 2/sqrt(5))) == -I*pi / 5
  548. assert acsch(-I*sqrt(2)) == I*pi / 4
  549. assert acsch(I*sqrt(2)) == -I*pi / 4
  550. assert acsch(-I*(sqrt(5)-1)) == 3*I*pi / 10
  551. assert acsch(I*(sqrt(5)-1)) == -3*I*pi / 10
  552. assert acsch(-I*2 / sqrt(3)) == I*pi / 3
  553. assert acsch(I*2 / sqrt(3)) == -I*pi / 3
  554. assert acsch(-I*2 / sqrt(2 + sqrt(2))) == 3*I*pi / 8
  555. assert acsch(I*2 / sqrt(2 + sqrt(2))) == -3*I*pi / 8
  556. assert acsch(-I*sqrt(2 - 2/sqrt(5))) == 2*I*pi / 5
  557. assert acsch(I*sqrt(2 - 2/sqrt(5))) == -2*I*pi / 5
  558. assert acsch(-I*(sqrt(6) - sqrt(2))) == 5*I*pi / 12
  559. assert acsch(I*(sqrt(6) - sqrt(2))) == -5*I*pi / 12
  560. assert acsch(nan) is nan
  561. # properties
  562. # acsch(x) == asinh(1/x)
  563. assert acsch(-I*sqrt(2)) == asinh(I/sqrt(2))
  564. assert acsch(-I*2 / sqrt(3)) == asinh(I*sqrt(3) / 2)
  565. # acsch(x) == -I*asin(I/x)
  566. assert acsch(-I*sqrt(2)) == -I*asin(-1/sqrt(2))
  567. assert acsch(-I*2 / sqrt(3)) == -I*asin(-sqrt(3)/2)
  568. # csch(acsch(x)) / x == 1
  569. assert expand_mul(csch(acsch(-I*(sqrt(6) + sqrt(2)))) / (-I*(sqrt(6) + sqrt(2)))) == 1
  570. assert expand_mul(csch(acsch(I*(1 + sqrt(5)))) / (I*(1 + sqrt(5)))) == 1
  571. assert (csch(acsch(I*sqrt(2 - 2/sqrt(5)))) / (I*sqrt(2 - 2/sqrt(5)))).simplify() == 1
  572. assert (csch(acsch(-I*sqrt(2 - 2/sqrt(5)))) / (-I*sqrt(2 - 2/sqrt(5)))).simplify() == 1
  573. # numerical evaluation
  574. assert str(acsch(5*I+1).n(6)) == '0.0391819 - 0.193363*I'
  575. assert str(acsch(-5*I+1).n(6)) == '0.0391819 + 0.193363*I'
  576. def test_acsch_infinities():
  577. assert acsch(oo) == 0
  578. assert acsch(-oo) == 0
  579. assert acsch(zoo) == 0
  580. def test_acsch_rewrite():
  581. x = Symbol('x')
  582. assert acsch(x).rewrite(log) == log(1/x + sqrt(1/x**2 + 1))
  583. def test_acsch_fdiff():
  584. x = Symbol('x')
  585. raises(ArgumentIndexError, lambda: acsch(x).fdiff(2))
  586. def test_atanh():
  587. x = Symbol('x')
  588. #at specific points
  589. assert atanh(0) == 0
  590. assert atanh(I) == I*pi/4
  591. assert atanh(-I) == -I*pi/4
  592. assert atanh(1) is oo
  593. assert atanh(-1) is -oo
  594. assert atanh(nan) is nan
  595. # at infinites
  596. assert atanh(oo) == -I*pi/2
  597. assert atanh(-oo) == I*pi/2
  598. assert atanh(I*oo) == I*pi/2
  599. assert atanh(-I*oo) == -I*pi/2
  600. assert atanh(zoo) == I*AccumBounds(-pi/2, pi/2)
  601. #properties
  602. assert atanh(-x) == -atanh(x)
  603. assert atanh(I/sqrt(3)) == I*pi/6
  604. assert atanh(-I/sqrt(3)) == -I*pi/6
  605. assert atanh(I*sqrt(3)) == I*pi/3
  606. assert atanh(-I*sqrt(3)) == -I*pi/3
  607. assert atanh(I*(1 + sqrt(2))) == pi*I*Rational(3, 8)
  608. assert atanh(I*(sqrt(2) - 1)) == pi*I/8
  609. assert atanh(I*(1 - sqrt(2))) == -pi*I/8
  610. assert atanh(-I*(1 + sqrt(2))) == pi*I*Rational(-3, 8)
  611. assert atanh(I*sqrt(5 + 2*sqrt(5))) == I*pi*Rational(2, 5)
  612. assert atanh(-I*sqrt(5 + 2*sqrt(5))) == I*pi*Rational(-2, 5)
  613. assert atanh(I*(2 - sqrt(3))) == pi*I/12
  614. assert atanh(I*(sqrt(3) - 2)) == -pi*I/12
  615. assert atanh(oo) == -I*pi/2
  616. # Symmetry
  617. assert atanh(Rational(-1, 2)) == -atanh(S.Half)
  618. # inverse composition
  619. assert unchanged(atanh, tanh(Symbol('v1')))
  620. assert atanh(tanh(-5, evaluate=False)) == -5
  621. assert atanh(tanh(0, evaluate=False)) == 0
  622. assert atanh(tanh(7, evaluate=False)) == 7
  623. assert atanh(tanh(I, evaluate=False)) == I
  624. assert atanh(tanh(-I, evaluate=False)) == -I
  625. assert atanh(tanh(-11*I, evaluate=False)) == -11*I + 4*I*pi
  626. assert atanh(tanh(3 + I)) == 3 + I
  627. assert atanh(tanh(4 + 5*I)) == 4 - 2*I*pi + 5*I
  628. assert atanh(tanh(pi/2)) == pi/2
  629. assert atanh(tanh(pi)) == pi
  630. assert atanh(tanh(-3 + 7*I)) == -3 - 2*I*pi + 7*I
  631. assert atanh(tanh(9 - I*2/3)) == 9 - I*2/3
  632. assert atanh(tanh(-32 - 123*I)) == -32 - 123*I + 39*I*pi
  633. def test_atanh_rewrite():
  634. x = Symbol('x')
  635. assert atanh(x).rewrite(log) == (log(1 + x) - log(1 - x)) / 2
  636. def test_atanh_series():
  637. x = Symbol('x')
  638. assert atanh(x).series(x, 0, 10) == \
  639. x + x**3/3 + x**5/5 + x**7/7 + x**9/9 + O(x**10)
  640. def test_atanh_fdiff():
  641. x = Symbol('x')
  642. raises(ArgumentIndexError, lambda: atanh(x).fdiff(2))
  643. def test_acoth():
  644. x = Symbol('x')
  645. #at specific points
  646. assert acoth(0) == I*pi/2
  647. assert acoth(I) == -I*pi/4
  648. assert acoth(-I) == I*pi/4
  649. assert acoth(1) is oo
  650. assert acoth(-1) is -oo
  651. assert acoth(nan) is nan
  652. # at infinites
  653. assert acoth(oo) == 0
  654. assert acoth(-oo) == 0
  655. assert acoth(I*oo) == 0
  656. assert acoth(-I*oo) == 0
  657. assert acoth(zoo) == 0
  658. #properties
  659. assert acoth(-x) == -acoth(x)
  660. assert acoth(I/sqrt(3)) == -I*pi/3
  661. assert acoth(-I/sqrt(3)) == I*pi/3
  662. assert acoth(I*sqrt(3)) == -I*pi/6
  663. assert acoth(-I*sqrt(3)) == I*pi/6
  664. assert acoth(I*(1 + sqrt(2))) == -pi*I/8
  665. assert acoth(-I*(sqrt(2) + 1)) == pi*I/8
  666. assert acoth(I*(1 - sqrt(2))) == pi*I*Rational(3, 8)
  667. assert acoth(I*(sqrt(2) - 1)) == pi*I*Rational(-3, 8)
  668. assert acoth(I*sqrt(5 + 2*sqrt(5))) == -I*pi/10
  669. assert acoth(-I*sqrt(5 + 2*sqrt(5))) == I*pi/10
  670. assert acoth(I*(2 + sqrt(3))) == -pi*I/12
  671. assert acoth(-I*(2 + sqrt(3))) == pi*I/12
  672. assert acoth(I*(2 - sqrt(3))) == pi*I*Rational(-5, 12)
  673. assert acoth(I*(sqrt(3) - 2)) == pi*I*Rational(5, 12)
  674. # Symmetry
  675. assert acoth(Rational(-1, 2)) == -acoth(S.Half)
  676. def test_acoth_rewrite():
  677. x = Symbol('x')
  678. assert acoth(x).rewrite(log) == (log(1 + 1/x) - log(1 - 1/x)) / 2
  679. def test_acoth_series():
  680. x = Symbol('x')
  681. assert acoth(x).series(x, 0, 10) == \
  682. I*pi/2 + x + x**3/3 + x**5/5 + x**7/7 + x**9/9 + O(x**10)
  683. def test_acoth_fdiff():
  684. x = Symbol('x')
  685. raises(ArgumentIndexError, lambda: acoth(x).fdiff(2))
  686. def test_inverses():
  687. x = Symbol('x')
  688. assert sinh(x).inverse() == asinh
  689. raises(AttributeError, lambda: cosh(x).inverse())
  690. assert tanh(x).inverse() == atanh
  691. assert coth(x).inverse() == acoth
  692. assert asinh(x).inverse() == sinh
  693. assert acosh(x).inverse() == cosh
  694. assert atanh(x).inverse() == tanh
  695. assert acoth(x).inverse() == coth
  696. assert asech(x).inverse() == sech
  697. assert acsch(x).inverse() == csch
  698. def test_leading_term():
  699. x = Symbol('x')
  700. assert cosh(x).as_leading_term(x) == 1
  701. assert coth(x).as_leading_term(x) == 1/x
  702. assert acosh(x).as_leading_term(x) == I*pi/2
  703. assert acoth(x).as_leading_term(x) == I*pi/2
  704. for func in [sinh, tanh, asinh, atanh]:
  705. assert func(x).as_leading_term(x) == x
  706. for func in [sinh, cosh, tanh, coth, asinh, acosh, atanh, acoth]:
  707. for arg in (1/x, S.Half):
  708. eq = func(arg)
  709. assert eq.as_leading_term(x) == eq
  710. for func in [csch, sech]:
  711. eq = func(S.Half)
  712. assert eq.as_leading_term(x) == eq
  713. def test_complex():
  714. a, b = symbols('a,b', real=True)
  715. z = a + b*I
  716. for func in [sinh, cosh, tanh, coth, sech, csch]:
  717. assert func(z).conjugate() == func(a - b*I)
  718. for deep in [True, False]:
  719. assert sinh(z).expand(
  720. complex=True, deep=deep) == sinh(a)*cos(b) + I*cosh(a)*sin(b)
  721. assert cosh(z).expand(
  722. complex=True, deep=deep) == cosh(a)*cos(b) + I*sinh(a)*sin(b)
  723. assert tanh(z).expand(complex=True, deep=deep) == sinh(a)*cosh(
  724. a)/(cos(b)**2 + sinh(a)**2) + I*sin(b)*cos(b)/(cos(b)**2 + sinh(a)**2)
  725. assert coth(z).expand(complex=True, deep=deep) == sinh(a)*cosh(
  726. a)/(sin(b)**2 + sinh(a)**2) - I*sin(b)*cos(b)/(sin(b)**2 + sinh(a)**2)
  727. assert csch(z).expand(complex=True, deep=deep) == cos(b) * sinh(a) / (sin(b)**2\
  728. *cosh(a)**2 + cos(b)**2 * sinh(a)**2) - I*sin(b) * cosh(a) / (sin(b)**2\
  729. *cosh(a)**2 + cos(b)**2 * sinh(a)**2)
  730. assert sech(z).expand(complex=True, deep=deep) == cos(b) * cosh(a) / (sin(b)**2\
  731. *sinh(a)**2 + cos(b)**2 * cosh(a)**2) - I*sin(b) * sinh(a) / (sin(b)**2\
  732. *sinh(a)**2 + cos(b)**2 * cosh(a)**2)
  733. def test_complex_2899():
  734. a, b = symbols('a,b', real=True)
  735. for deep in [True, False]:
  736. for func in [sinh, cosh, tanh, coth]:
  737. assert func(a).expand(complex=True, deep=deep) == func(a)
  738. def test_simplifications():
  739. x = Symbol('x')
  740. assert sinh(asinh(x)) == x
  741. assert sinh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1)
  742. assert sinh(atanh(x)) == x/sqrt(1 - x**2)
  743. assert sinh(acoth(x)) == 1/(sqrt(x - 1) * sqrt(x + 1))
  744. assert cosh(asinh(x)) == sqrt(1 + x**2)
  745. assert cosh(acosh(x)) == x
  746. assert cosh(atanh(x)) == 1/sqrt(1 - x**2)
  747. assert cosh(acoth(x)) == x/(sqrt(x - 1) * sqrt(x + 1))
  748. assert tanh(asinh(x)) == x/sqrt(1 + x**2)
  749. assert tanh(acosh(x)) == sqrt(x - 1) * sqrt(x + 1) / x
  750. assert tanh(atanh(x)) == x
  751. assert tanh(acoth(x)) == 1/x
  752. assert coth(asinh(x)) == sqrt(1 + x**2)/x
  753. assert coth(acosh(x)) == x/(sqrt(x - 1) * sqrt(x + 1))
  754. assert coth(atanh(x)) == 1/x
  755. assert coth(acoth(x)) == x
  756. assert csch(asinh(x)) == 1/x
  757. assert csch(acosh(x)) == 1/(sqrt(x - 1) * sqrt(x + 1))
  758. assert csch(atanh(x)) == sqrt(1 - x**2)/x
  759. assert csch(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)
  760. assert sech(asinh(x)) == 1/sqrt(1 + x**2)
  761. assert sech(acosh(x)) == 1/x
  762. assert sech(atanh(x)) == sqrt(1 - x**2)
  763. assert sech(acoth(x)) == sqrt(x - 1) * sqrt(x + 1)/x
  764. def test_issue_4136():
  765. assert cosh(asinh(Integer(3)/2)) == sqrt(Integer(13)/4)
  766. def test_sinh_rewrite():
  767. x = Symbol('x')
  768. assert sinh(x).rewrite(exp) == (exp(x) - exp(-x))/2 \
  769. == sinh(x).rewrite('tractable')
  770. assert sinh(x).rewrite(cosh) == -I*cosh(x + I*pi/2)
  771. tanh_half = tanh(S.Half*x)
  772. assert sinh(x).rewrite(tanh) == 2*tanh_half/(1 - tanh_half**2)
  773. coth_half = coth(S.Half*x)
  774. assert sinh(x).rewrite(coth) == 2*coth_half/(coth_half**2 - 1)
  775. def test_cosh_rewrite():
  776. x = Symbol('x')
  777. assert cosh(x).rewrite(exp) == (exp(x) + exp(-x))/2 \
  778. == cosh(x).rewrite('tractable')
  779. assert cosh(x).rewrite(sinh) == -I*sinh(x + I*pi/2)
  780. tanh_half = tanh(S.Half*x)**2
  781. assert cosh(x).rewrite(tanh) == (1 + tanh_half)/(1 - tanh_half)
  782. coth_half = coth(S.Half*x)**2
  783. assert cosh(x).rewrite(coth) == (coth_half + 1)/(coth_half - 1)
  784. def test_tanh_rewrite():
  785. x = Symbol('x')
  786. assert tanh(x).rewrite(exp) == (exp(x) - exp(-x))/(exp(x) + exp(-x)) \
  787. == tanh(x).rewrite('tractable')
  788. assert tanh(x).rewrite(sinh) == I*sinh(x)/sinh(I*pi/2 - x)
  789. assert tanh(x).rewrite(cosh) == I*cosh(I*pi/2 - x)/cosh(x)
  790. assert tanh(x).rewrite(coth) == 1/coth(x)
  791. def test_coth_rewrite():
  792. x = Symbol('x')
  793. assert coth(x).rewrite(exp) == (exp(x) + exp(-x))/(exp(x) - exp(-x)) \
  794. == coth(x).rewrite('tractable')
  795. assert coth(x).rewrite(sinh) == -I*sinh(I*pi/2 - x)/sinh(x)
  796. assert coth(x).rewrite(cosh) == -I*cosh(x)/cosh(I*pi/2 - x)
  797. assert coth(x).rewrite(tanh) == 1/tanh(x)
  798. def test_csch_rewrite():
  799. x = Symbol('x')
  800. assert csch(x).rewrite(exp) == 1 / (exp(x)/2 - exp(-x)/2) \
  801. == csch(x).rewrite('tractable')
  802. assert csch(x).rewrite(cosh) == I/cosh(x + I*pi/2)
  803. tanh_half = tanh(S.Half*x)
  804. assert csch(x).rewrite(tanh) == (1 - tanh_half**2)/(2*tanh_half)
  805. coth_half = coth(S.Half*x)
  806. assert csch(x).rewrite(coth) == (coth_half**2 - 1)/(2*coth_half)
  807. def test_sech_rewrite():
  808. x = Symbol('x')
  809. assert sech(x).rewrite(exp) == 1 / (exp(x)/2 + exp(-x)/2) \
  810. == sech(x).rewrite('tractable')
  811. assert sech(x).rewrite(sinh) == I/sinh(x + I*pi/2)
  812. tanh_half = tanh(S.Half*x)**2
  813. assert sech(x).rewrite(tanh) == (1 - tanh_half)/(1 + tanh_half)
  814. coth_half = coth(S.Half*x)**2
  815. assert sech(x).rewrite(coth) == (coth_half - 1)/(coth_half + 1)
  816. def test_derivs():
  817. x = Symbol('x')
  818. assert coth(x).diff(x) == -sinh(x)**(-2)
  819. assert sinh(x).diff(x) == cosh(x)
  820. assert cosh(x).diff(x) == sinh(x)
  821. assert tanh(x).diff(x) == -tanh(x)**2 + 1
  822. assert csch(x).diff(x) == -coth(x)*csch(x)
  823. assert sech(x).diff(x) == -tanh(x)*sech(x)
  824. assert acoth(x).diff(x) == 1/(-x**2 + 1)
  825. assert asinh(x).diff(x) == 1/sqrt(x**2 + 1)
  826. assert acosh(x).diff(x) == 1/sqrt(x**2 - 1)
  827. assert atanh(x).diff(x) == 1/(-x**2 + 1)
  828. assert asech(x).diff(x) == -1/(x*sqrt(1 - x**2))
  829. assert acsch(x).diff(x) == -1/(x**2*sqrt(1 + x**(-2)))
  830. def test_sinh_expansion():
  831. x, y = symbols('x,y')
  832. assert sinh(x+y).expand(trig=True) == sinh(x)*cosh(y) + cosh(x)*sinh(y)
  833. assert sinh(2*x).expand(trig=True) == 2*sinh(x)*cosh(x)
  834. assert sinh(3*x).expand(trig=True).expand() == \
  835. sinh(x)**3 + 3*sinh(x)*cosh(x)**2
  836. def test_cosh_expansion():
  837. x, y = symbols('x,y')
  838. assert cosh(x+y).expand(trig=True) == cosh(x)*cosh(y) + sinh(x)*sinh(y)
  839. assert cosh(2*x).expand(trig=True) == cosh(x)**2 + sinh(x)**2
  840. assert cosh(3*x).expand(trig=True).expand() == \
  841. 3*sinh(x)**2*cosh(x) + cosh(x)**3
  842. def test_cosh_positive():
  843. # See issue 11721
  844. # cosh(x) is positive for real values of x
  845. k = symbols('k', real=True)
  846. n = symbols('n', integer=True)
  847. assert cosh(k, evaluate=False).is_positive is True
  848. assert cosh(k + 2*n*pi*I, evaluate=False).is_positive is True
  849. assert cosh(I*pi/4, evaluate=False).is_positive is True
  850. assert cosh(3*I*pi/4, evaluate=False).is_positive is False
  851. def test_cosh_nonnegative():
  852. k = symbols('k', real=True)
  853. n = symbols('n', integer=True)
  854. assert cosh(k, evaluate=False).is_nonnegative is True
  855. assert cosh(k + 2*n*pi*I, evaluate=False).is_nonnegative is True
  856. assert cosh(I*pi/4, evaluate=False).is_nonnegative is True
  857. assert cosh(3*I*pi/4, evaluate=False).is_nonnegative is False
  858. assert cosh(S.Zero, evaluate=False).is_nonnegative is True
  859. def test_real_assumptions():
  860. z = Symbol('z', real=False)
  861. assert sinh(z).is_real is None
  862. assert cosh(z).is_real is None
  863. assert tanh(z).is_real is None
  864. assert sech(z).is_real is None
  865. assert csch(z).is_real is None
  866. assert coth(z).is_real is None
  867. def test_sign_assumptions():
  868. p = Symbol('p', positive=True)
  869. n = Symbol('n', negative=True)
  870. assert sinh(n).is_negative is True
  871. assert sinh(p).is_positive is True
  872. assert cosh(n).is_positive is True
  873. assert cosh(p).is_positive is True
  874. assert tanh(n).is_negative is True
  875. assert tanh(p).is_positive is True
  876. assert csch(n).is_negative is True
  877. assert csch(p).is_positive is True
  878. assert sech(n).is_positive is True
  879. assert sech(p).is_positive is True
  880. assert coth(n).is_negative is True
  881. assert coth(p).is_positive is True