theta.py 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. from .functions import defun, defun_wrapped
  2. @defun
  3. def _jacobi_theta2(ctx, z, q):
  4. extra1 = 10
  5. extra2 = 20
  6. # the loops below break when the fixed precision quantities
  7. # a and b go to zero;
  8. # right shifting small negative numbers by wp one obtains -1, not zero,
  9. # so the condition a**2 + b**2 > MIN is used to break the loops.
  10. MIN = 2
  11. if z == ctx.zero:
  12. if (not ctx._im(q)):
  13. wp = ctx.prec + extra1
  14. x = ctx.to_fixed(ctx._re(q), wp)
  15. x2 = (x*x) >> wp
  16. a = b = x2
  17. s = x2
  18. while abs(a) > MIN:
  19. b = (b*x2) >> wp
  20. a = (a*b) >> wp
  21. s += a
  22. s = (1 << (wp+1)) + (s << 1)
  23. s = ctx.ldexp(s, -wp)
  24. else:
  25. wp = ctx.prec + extra1
  26. xre = ctx.to_fixed(ctx._re(q), wp)
  27. xim = ctx.to_fixed(ctx._im(q), wp)
  28. x2re = (xre*xre - xim*xim) >> wp
  29. x2im = (xre*xim) >> (wp-1)
  30. are = bre = x2re
  31. aim = bim = x2im
  32. sre = (1<<wp) + are
  33. sim = aim
  34. while are**2 + aim**2 > MIN:
  35. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  36. (bre * x2im + bim * x2re) >> wp
  37. are, aim = (are * bre - aim * bim) >> wp, \
  38. (are * bim + aim * bre) >> wp
  39. sre += are
  40. sim += aim
  41. sre = (sre << 1)
  42. sim = (sim << 1)
  43. sre = ctx.ldexp(sre, -wp)
  44. sim = ctx.ldexp(sim, -wp)
  45. s = ctx.mpc(sre, sim)
  46. else:
  47. if (not ctx._im(q)) and (not ctx._im(z)):
  48. wp = ctx.prec + extra1
  49. x = ctx.to_fixed(ctx._re(q), wp)
  50. x2 = (x*x) >> wp
  51. a = b = x2
  52. c1, s1 = ctx.cos_sin(ctx._re(z), prec=wp)
  53. cn = c1 = ctx.to_fixed(c1, wp)
  54. sn = s1 = ctx.to_fixed(s1, wp)
  55. c2 = (c1*c1 - s1*s1) >> wp
  56. s2 = (c1 * s1) >> (wp - 1)
  57. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  58. s = c1 + ((a * cn) >> wp)
  59. while abs(a) > MIN:
  60. b = (b*x2) >> wp
  61. a = (a*b) >> wp
  62. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  63. s += (a * cn) >> wp
  64. s = (s << 1)
  65. s = ctx.ldexp(s, -wp)
  66. s *= ctx.nthroot(q, 4)
  67. return s
  68. # case z real, q complex
  69. elif not ctx._im(z):
  70. wp = ctx.prec + extra2
  71. xre = ctx.to_fixed(ctx._re(q), wp)
  72. xim = ctx.to_fixed(ctx._im(q), wp)
  73. x2re = (xre*xre - xim*xim) >> wp
  74. x2im = (xre*xim) >> (wp - 1)
  75. are = bre = x2re
  76. aim = bim = x2im
  77. c1, s1 = ctx.cos_sin(ctx._re(z), prec=wp)
  78. cn = c1 = ctx.to_fixed(c1, wp)
  79. sn = s1 = ctx.to_fixed(s1, wp)
  80. c2 = (c1*c1 - s1*s1) >> wp
  81. s2 = (c1 * s1) >> (wp - 1)
  82. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  83. sre = c1 + ((are * cn) >> wp)
  84. sim = ((aim * cn) >> wp)
  85. while are**2 + aim**2 > MIN:
  86. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  87. (bre * x2im + bim * x2re) >> wp
  88. are, aim = (are * bre - aim * bim) >> wp, \
  89. (are * bim + aim * bre) >> wp
  90. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  91. sre += ((are * cn) >> wp)
  92. sim += ((aim * cn) >> wp)
  93. sre = (sre << 1)
  94. sim = (sim << 1)
  95. sre = ctx.ldexp(sre, -wp)
  96. sim = ctx.ldexp(sim, -wp)
  97. s = ctx.mpc(sre, sim)
  98. #case z complex, q real
  99. elif not ctx._im(q):
  100. wp = ctx.prec + extra2
  101. x = ctx.to_fixed(ctx._re(q), wp)
  102. x2 = (x*x) >> wp
  103. a = b = x2
  104. prec0 = ctx.prec
  105. ctx.prec = wp
  106. c1, s1 = ctx.cos_sin(z)
  107. ctx.prec = prec0
  108. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  109. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  110. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  111. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  112. #c2 = (c1*c1 - s1*s1) >> wp
  113. c2re = (c1re*c1re - c1im*c1im - s1re*s1re + s1im*s1im) >> wp
  114. c2im = (c1re*c1im - s1re*s1im) >> (wp - 1)
  115. #s2 = (c1 * s1) >> (wp - 1)
  116. s2re = (c1re*s1re - c1im*s1im) >> (wp - 1)
  117. s2im = (c1re*s1im + c1im*s1re) >> (wp - 1)
  118. #cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  119. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  120. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  121. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  122. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  123. cnre = t1
  124. cnim = t2
  125. snre = t3
  126. snim = t4
  127. sre = c1re + ((a * cnre) >> wp)
  128. sim = c1im + ((a * cnim) >> wp)
  129. while abs(a) > MIN:
  130. b = (b*x2) >> wp
  131. a = (a*b) >> wp
  132. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  133. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  134. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  135. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  136. cnre = t1
  137. cnim = t2
  138. snre = t3
  139. snim = t4
  140. sre += ((a * cnre) >> wp)
  141. sim += ((a * cnim) >> wp)
  142. sre = (sre << 1)
  143. sim = (sim << 1)
  144. sre = ctx.ldexp(sre, -wp)
  145. sim = ctx.ldexp(sim, -wp)
  146. s = ctx.mpc(sre, sim)
  147. # case z and q complex
  148. else:
  149. wp = ctx.prec + extra2
  150. xre = ctx.to_fixed(ctx._re(q), wp)
  151. xim = ctx.to_fixed(ctx._im(q), wp)
  152. x2re = (xre*xre - xim*xim) >> wp
  153. x2im = (xre*xim) >> (wp - 1)
  154. are = bre = x2re
  155. aim = bim = x2im
  156. prec0 = ctx.prec
  157. ctx.prec = wp
  158. # cos(z), sin(z) with z complex
  159. c1, s1 = ctx.cos_sin(z)
  160. ctx.prec = prec0
  161. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  162. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  163. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  164. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  165. c2re = (c1re*c1re - c1im*c1im - s1re*s1re + s1im*s1im) >> wp
  166. c2im = (c1re*c1im - s1re*s1im) >> (wp - 1)
  167. s2re = (c1re*s1re - c1im*s1im) >> (wp - 1)
  168. s2im = (c1re*s1im + c1im*s1re) >> (wp - 1)
  169. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  170. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  171. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  172. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  173. cnre = t1
  174. cnim = t2
  175. snre = t3
  176. snim = t4
  177. n = 1
  178. termre = c1re
  179. termim = c1im
  180. sre = c1re + ((are * cnre - aim * cnim) >> wp)
  181. sim = c1im + ((are * cnim + aim * cnre) >> wp)
  182. n = 3
  183. termre = ((are * cnre - aim * cnim) >> wp)
  184. termim = ((are * cnim + aim * cnre) >> wp)
  185. sre = c1re + ((are * cnre - aim * cnim) >> wp)
  186. sim = c1im + ((are * cnim + aim * cnre) >> wp)
  187. n = 5
  188. while are**2 + aim**2 > MIN:
  189. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  190. (bre * x2im + bim * x2re) >> wp
  191. are, aim = (are * bre - aim * bim) >> wp, \
  192. (are * bim + aim * bre) >> wp
  193. #cn, sn = (cn*c1 - sn*s1) >> wp, (sn*c1 + cn*s1) >> wp
  194. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  195. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  196. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  197. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  198. cnre = t1
  199. cnim = t2
  200. snre = t3
  201. snim = t4
  202. termre = ((are * cnre - aim * cnim) >> wp)
  203. termim = ((aim * cnre + are * cnim) >> wp)
  204. sre += ((are * cnre - aim * cnim) >> wp)
  205. sim += ((aim * cnre + are * cnim) >> wp)
  206. n += 2
  207. sre = (sre << 1)
  208. sim = (sim << 1)
  209. sre = ctx.ldexp(sre, -wp)
  210. sim = ctx.ldexp(sim, -wp)
  211. s = ctx.mpc(sre, sim)
  212. s *= ctx.nthroot(q, 4)
  213. return s
  214. @defun
  215. def _djacobi_theta2(ctx, z, q, nd):
  216. MIN = 2
  217. extra1 = 10
  218. extra2 = 20
  219. if (not ctx._im(q)) and (not ctx._im(z)):
  220. wp = ctx.prec + extra1
  221. x = ctx.to_fixed(ctx._re(q), wp)
  222. x2 = (x*x) >> wp
  223. a = b = x2
  224. c1, s1 = ctx.cos_sin(ctx._re(z), prec=wp)
  225. cn = c1 = ctx.to_fixed(c1, wp)
  226. sn = s1 = ctx.to_fixed(s1, wp)
  227. c2 = (c1*c1 - s1*s1) >> wp
  228. s2 = (c1 * s1) >> (wp - 1)
  229. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  230. if (nd&1):
  231. s = s1 + ((a * sn * 3**nd) >> wp)
  232. else:
  233. s = c1 + ((a * cn * 3**nd) >> wp)
  234. n = 2
  235. while abs(a) > MIN:
  236. b = (b*x2) >> wp
  237. a = (a*b) >> wp
  238. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  239. if nd&1:
  240. s += (a * sn * (2*n+1)**nd) >> wp
  241. else:
  242. s += (a * cn * (2*n+1)**nd) >> wp
  243. n += 1
  244. s = -(s << 1)
  245. s = ctx.ldexp(s, -wp)
  246. # case z real, q complex
  247. elif not ctx._im(z):
  248. wp = ctx.prec + extra2
  249. xre = ctx.to_fixed(ctx._re(q), wp)
  250. xim = ctx.to_fixed(ctx._im(q), wp)
  251. x2re = (xre*xre - xim*xim) >> wp
  252. x2im = (xre*xim) >> (wp - 1)
  253. are = bre = x2re
  254. aim = bim = x2im
  255. c1, s1 = ctx.cos_sin(ctx._re(z), prec=wp)
  256. cn = c1 = ctx.to_fixed(c1, wp)
  257. sn = s1 = ctx.to_fixed(s1, wp)
  258. c2 = (c1*c1 - s1*s1) >> wp
  259. s2 = (c1 * s1) >> (wp - 1)
  260. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  261. if (nd&1):
  262. sre = s1 + ((are * sn * 3**nd) >> wp)
  263. sim = ((aim * sn * 3**nd) >> wp)
  264. else:
  265. sre = c1 + ((are * cn * 3**nd) >> wp)
  266. sim = ((aim * cn * 3**nd) >> wp)
  267. n = 5
  268. while are**2 + aim**2 > MIN:
  269. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  270. (bre * x2im + bim * x2re) >> wp
  271. are, aim = (are * bre - aim * bim) >> wp, \
  272. (are * bim + aim * bre) >> wp
  273. cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  274. if (nd&1):
  275. sre += ((are * sn * n**nd) >> wp)
  276. sim += ((aim * sn * n**nd) >> wp)
  277. else:
  278. sre += ((are * cn * n**nd) >> wp)
  279. sim += ((aim * cn * n**nd) >> wp)
  280. n += 2
  281. sre = -(sre << 1)
  282. sim = -(sim << 1)
  283. sre = ctx.ldexp(sre, -wp)
  284. sim = ctx.ldexp(sim, -wp)
  285. s = ctx.mpc(sre, sim)
  286. #case z complex, q real
  287. elif not ctx._im(q):
  288. wp = ctx.prec + extra2
  289. x = ctx.to_fixed(ctx._re(q), wp)
  290. x2 = (x*x) >> wp
  291. a = b = x2
  292. prec0 = ctx.prec
  293. ctx.prec = wp
  294. c1, s1 = ctx.cos_sin(z)
  295. ctx.prec = prec0
  296. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  297. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  298. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  299. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  300. #c2 = (c1*c1 - s1*s1) >> wp
  301. c2re = (c1re*c1re - c1im*c1im - s1re*s1re + s1im*s1im) >> wp
  302. c2im = (c1re*c1im - s1re*s1im) >> (wp - 1)
  303. #s2 = (c1 * s1) >> (wp - 1)
  304. s2re = (c1re*s1re - c1im*s1im) >> (wp - 1)
  305. s2im = (c1re*s1im + c1im*s1re) >> (wp - 1)
  306. #cn, sn = (cn*c2 - sn*s2) >> wp, (sn*c2 + cn*s2) >> wp
  307. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  308. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  309. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  310. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  311. cnre = t1
  312. cnim = t2
  313. snre = t3
  314. snim = t4
  315. if (nd&1):
  316. sre = s1re + ((a * snre * 3**nd) >> wp)
  317. sim = s1im + ((a * snim * 3**nd) >> wp)
  318. else:
  319. sre = c1re + ((a * cnre * 3**nd) >> wp)
  320. sim = c1im + ((a * cnim * 3**nd) >> wp)
  321. n = 5
  322. while abs(a) > MIN:
  323. b = (b*x2) >> wp
  324. a = (a*b) >> wp
  325. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  326. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  327. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  328. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  329. cnre = t1
  330. cnim = t2
  331. snre = t3
  332. snim = t4
  333. if (nd&1):
  334. sre += ((a * snre * n**nd) >> wp)
  335. sim += ((a * snim * n**nd) >> wp)
  336. else:
  337. sre += ((a * cnre * n**nd) >> wp)
  338. sim += ((a * cnim * n**nd) >> wp)
  339. n += 2
  340. sre = -(sre << 1)
  341. sim = -(sim << 1)
  342. sre = ctx.ldexp(sre, -wp)
  343. sim = ctx.ldexp(sim, -wp)
  344. s = ctx.mpc(sre, sim)
  345. # case z and q complex
  346. else:
  347. wp = ctx.prec + extra2
  348. xre = ctx.to_fixed(ctx._re(q), wp)
  349. xim = ctx.to_fixed(ctx._im(q), wp)
  350. x2re = (xre*xre - xim*xim) >> wp
  351. x2im = (xre*xim) >> (wp - 1)
  352. are = bre = x2re
  353. aim = bim = x2im
  354. prec0 = ctx.prec
  355. ctx.prec = wp
  356. # cos(2*z), sin(2*z) with z complex
  357. c1, s1 = ctx.cos_sin(z)
  358. ctx.prec = prec0
  359. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  360. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  361. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  362. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  363. c2re = (c1re*c1re - c1im*c1im - s1re*s1re + s1im*s1im) >> wp
  364. c2im = (c1re*c1im - s1re*s1im) >> (wp - 1)
  365. s2re = (c1re*s1re - c1im*s1im) >> (wp - 1)
  366. s2im = (c1re*s1im + c1im*s1re) >> (wp - 1)
  367. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  368. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  369. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  370. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  371. cnre = t1
  372. cnim = t2
  373. snre = t3
  374. snim = t4
  375. if (nd&1):
  376. sre = s1re + (((are * snre - aim * snim) * 3**nd) >> wp)
  377. sim = s1im + (((are * snim + aim * snre)* 3**nd) >> wp)
  378. else:
  379. sre = c1re + (((are * cnre - aim * cnim) * 3**nd) >> wp)
  380. sim = c1im + (((are * cnim + aim * cnre)* 3**nd) >> wp)
  381. n = 5
  382. while are**2 + aim**2 > MIN:
  383. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  384. (bre * x2im + bim * x2re) >> wp
  385. are, aim = (are * bre - aim * bim) >> wp, \
  386. (are * bim + aim * bre) >> wp
  387. #cn, sn = (cn*c1 - sn*s1) >> wp, (sn*c1 + cn*s1) >> wp
  388. t1 = (cnre*c2re - cnim*c2im - snre*s2re + snim*s2im) >> wp
  389. t2 = (cnre*c2im + cnim*c2re - snre*s2im - snim*s2re) >> wp
  390. t3 = (snre*c2re - snim*c2im + cnre*s2re - cnim*s2im) >> wp
  391. t4 = (snre*c2im + snim*c2re + cnre*s2im + cnim*s2re) >> wp
  392. cnre = t1
  393. cnim = t2
  394. snre = t3
  395. snim = t4
  396. if (nd&1):
  397. sre += (((are * snre - aim * snim) * n**nd) >> wp)
  398. sim += (((aim * snre + are * snim) * n**nd) >> wp)
  399. else:
  400. sre += (((are * cnre - aim * cnim) * n**nd) >> wp)
  401. sim += (((aim * cnre + are * cnim) * n**nd) >> wp)
  402. n += 2
  403. sre = -(sre << 1)
  404. sim = -(sim << 1)
  405. sre = ctx.ldexp(sre, -wp)
  406. sim = ctx.ldexp(sim, -wp)
  407. s = ctx.mpc(sre, sim)
  408. s *= ctx.nthroot(q, 4)
  409. if (nd&1):
  410. return (-1)**(nd//2) * s
  411. else:
  412. return (-1)**(1 + nd//2) * s
  413. @defun
  414. def _jacobi_theta3(ctx, z, q):
  415. extra1 = 10
  416. extra2 = 20
  417. MIN = 2
  418. if z == ctx.zero:
  419. if not ctx._im(q):
  420. wp = ctx.prec + extra1
  421. x = ctx.to_fixed(ctx._re(q), wp)
  422. s = x
  423. a = b = x
  424. x2 = (x*x) >> wp
  425. while abs(a) > MIN:
  426. b = (b*x2) >> wp
  427. a = (a*b) >> wp
  428. s += a
  429. s = (1 << wp) + (s << 1)
  430. s = ctx.ldexp(s, -wp)
  431. return s
  432. else:
  433. wp = ctx.prec + extra1
  434. xre = ctx.to_fixed(ctx._re(q), wp)
  435. xim = ctx.to_fixed(ctx._im(q), wp)
  436. x2re = (xre*xre - xim*xim) >> wp
  437. x2im = (xre*xim) >> (wp - 1)
  438. sre = are = bre = xre
  439. sim = aim = bim = xim
  440. while are**2 + aim**2 > MIN:
  441. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  442. (bre * x2im + bim * x2re) >> wp
  443. are, aim = (are * bre - aim * bim) >> wp, \
  444. (are * bim + aim * bre) >> wp
  445. sre += are
  446. sim += aim
  447. sre = (1 << wp) + (sre << 1)
  448. sim = (sim << 1)
  449. sre = ctx.ldexp(sre, -wp)
  450. sim = ctx.ldexp(sim, -wp)
  451. s = ctx.mpc(sre, sim)
  452. return s
  453. else:
  454. if (not ctx._im(q)) and (not ctx._im(z)):
  455. s = 0
  456. wp = ctx.prec + extra1
  457. x = ctx.to_fixed(ctx._re(q), wp)
  458. a = b = x
  459. x2 = (x*x) >> wp
  460. c1, s1 = ctx.cos_sin(ctx._re(z)*2, prec=wp)
  461. c1 = ctx.to_fixed(c1, wp)
  462. s1 = ctx.to_fixed(s1, wp)
  463. cn = c1
  464. sn = s1
  465. s += (a * cn) >> wp
  466. while abs(a) > MIN:
  467. b = (b*x2) >> wp
  468. a = (a*b) >> wp
  469. cn, sn = (cn*c1 - sn*s1) >> wp, (sn*c1 + cn*s1) >> wp
  470. s += (a * cn) >> wp
  471. s = (1 << wp) + (s << 1)
  472. s = ctx.ldexp(s, -wp)
  473. return s
  474. # case z real, q complex
  475. elif not ctx._im(z):
  476. wp = ctx.prec + extra2
  477. xre = ctx.to_fixed(ctx._re(q), wp)
  478. xim = ctx.to_fixed(ctx._im(q), wp)
  479. x2re = (xre*xre - xim*xim) >> wp
  480. x2im = (xre*xim) >> (wp - 1)
  481. are = bre = xre
  482. aim = bim = xim
  483. c1, s1 = ctx.cos_sin(ctx._re(z)*2, prec=wp)
  484. c1 = ctx.to_fixed(c1, wp)
  485. s1 = ctx.to_fixed(s1, wp)
  486. cn = c1
  487. sn = s1
  488. sre = (are * cn) >> wp
  489. sim = (aim * cn) >> wp
  490. while are**2 + aim**2 > MIN:
  491. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  492. (bre * x2im + bim * x2re) >> wp
  493. are, aim = (are * bre - aim * bim) >> wp, \
  494. (are * bim + aim * bre) >> wp
  495. cn, sn = (cn*c1 - sn*s1) >> wp, (sn*c1 + cn*s1) >> wp
  496. sre += (are * cn) >> wp
  497. sim += (aim * cn) >> wp
  498. sre = (1 << wp) + (sre << 1)
  499. sim = (sim << 1)
  500. sre = ctx.ldexp(sre, -wp)
  501. sim = ctx.ldexp(sim, -wp)
  502. s = ctx.mpc(sre, sim)
  503. return s
  504. #case z complex, q real
  505. elif not ctx._im(q):
  506. wp = ctx.prec + extra2
  507. x = ctx.to_fixed(ctx._re(q), wp)
  508. a = b = x
  509. x2 = (x*x) >> wp
  510. prec0 = ctx.prec
  511. ctx.prec = wp
  512. c1, s1 = ctx.cos_sin(2*z)
  513. ctx.prec = prec0
  514. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  515. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  516. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  517. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  518. sre = (a * cnre) >> wp
  519. sim = (a * cnim) >> wp
  520. while abs(a) > MIN:
  521. b = (b*x2) >> wp
  522. a = (a*b) >> wp
  523. t1 = (cnre*c1re - cnim*c1im - snre*s1re + snim*s1im) >> wp
  524. t2 = (cnre*c1im + cnim*c1re - snre*s1im - snim*s1re) >> wp
  525. t3 = (snre*c1re - snim*c1im + cnre*s1re - cnim*s1im) >> wp
  526. t4 = (snre*c1im + snim*c1re + cnre*s1im + cnim*s1re) >> wp
  527. cnre = t1
  528. cnim = t2
  529. snre = t3
  530. snim = t4
  531. sre += (a * cnre) >> wp
  532. sim += (a * cnim) >> wp
  533. sre = (1 << wp) + (sre << 1)
  534. sim = (sim << 1)
  535. sre = ctx.ldexp(sre, -wp)
  536. sim = ctx.ldexp(sim, -wp)
  537. s = ctx.mpc(sre, sim)
  538. return s
  539. # case z and q complex
  540. else:
  541. wp = ctx.prec + extra2
  542. xre = ctx.to_fixed(ctx._re(q), wp)
  543. xim = ctx.to_fixed(ctx._im(q), wp)
  544. x2re = (xre*xre - xim*xim) >> wp
  545. x2im = (xre*xim) >> (wp - 1)
  546. are = bre = xre
  547. aim = bim = xim
  548. prec0 = ctx.prec
  549. ctx.prec = wp
  550. # cos(2*z), sin(2*z) with z complex
  551. c1, s1 = ctx.cos_sin(2*z)
  552. ctx.prec = prec0
  553. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  554. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  555. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  556. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  557. sre = (are * cnre - aim * cnim) >> wp
  558. sim = (aim * cnre + are * cnim) >> wp
  559. while are**2 + aim**2 > MIN:
  560. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  561. (bre * x2im + bim * x2re) >> wp
  562. are, aim = (are * bre - aim * bim) >> wp, \
  563. (are * bim + aim * bre) >> wp
  564. t1 = (cnre*c1re - cnim*c1im - snre*s1re + snim*s1im) >> wp
  565. t2 = (cnre*c1im + cnim*c1re - snre*s1im - snim*s1re) >> wp
  566. t3 = (snre*c1re - snim*c1im + cnre*s1re - cnim*s1im) >> wp
  567. t4 = (snre*c1im + snim*c1re + cnre*s1im + cnim*s1re) >> wp
  568. cnre = t1
  569. cnim = t2
  570. snre = t3
  571. snim = t4
  572. sre += (are * cnre - aim * cnim) >> wp
  573. sim += (aim * cnre + are * cnim) >> wp
  574. sre = (1 << wp) + (sre << 1)
  575. sim = (sim << 1)
  576. sre = ctx.ldexp(sre, -wp)
  577. sim = ctx.ldexp(sim, -wp)
  578. s = ctx.mpc(sre, sim)
  579. return s
  580. @defun
  581. def _djacobi_theta3(ctx, z, q, nd):
  582. """nd=1,2,3 order of the derivative with respect to z"""
  583. MIN = 2
  584. extra1 = 10
  585. extra2 = 20
  586. if (not ctx._im(q)) and (not ctx._im(z)):
  587. s = 0
  588. wp = ctx.prec + extra1
  589. x = ctx.to_fixed(ctx._re(q), wp)
  590. a = b = x
  591. x2 = (x*x) >> wp
  592. c1, s1 = ctx.cos_sin(ctx._re(z)*2, prec=wp)
  593. c1 = ctx.to_fixed(c1, wp)
  594. s1 = ctx.to_fixed(s1, wp)
  595. cn = c1
  596. sn = s1
  597. if (nd&1):
  598. s += (a * sn) >> wp
  599. else:
  600. s += (a * cn) >> wp
  601. n = 2
  602. while abs(a) > MIN:
  603. b = (b*x2) >> wp
  604. a = (a*b) >> wp
  605. cn, sn = (cn*c1 - sn*s1) >> wp, (sn*c1 + cn*s1) >> wp
  606. if nd&1:
  607. s += (a * sn * n**nd) >> wp
  608. else:
  609. s += (a * cn * n**nd) >> wp
  610. n += 1
  611. s = -(s << (nd+1))
  612. s = ctx.ldexp(s, -wp)
  613. # case z real, q complex
  614. elif not ctx._im(z):
  615. wp = ctx.prec + extra2
  616. xre = ctx.to_fixed(ctx._re(q), wp)
  617. xim = ctx.to_fixed(ctx._im(q), wp)
  618. x2re = (xre*xre - xim*xim) >> wp
  619. x2im = (xre*xim) >> (wp - 1)
  620. are = bre = xre
  621. aim = bim = xim
  622. c1, s1 = ctx.cos_sin(ctx._re(z)*2, prec=wp)
  623. c1 = ctx.to_fixed(c1, wp)
  624. s1 = ctx.to_fixed(s1, wp)
  625. cn = c1
  626. sn = s1
  627. if (nd&1):
  628. sre = (are * sn) >> wp
  629. sim = (aim * sn) >> wp
  630. else:
  631. sre = (are * cn) >> wp
  632. sim = (aim * cn) >> wp
  633. n = 2
  634. while are**2 + aim**2 > MIN:
  635. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  636. (bre * x2im + bim * x2re) >> wp
  637. are, aim = (are * bre - aim * bim) >> wp, \
  638. (are * bim + aim * bre) >> wp
  639. cn, sn = (cn*c1 - sn*s1) >> wp, (sn*c1 + cn*s1) >> wp
  640. if nd&1:
  641. sre += (are * sn * n**nd) >> wp
  642. sim += (aim * sn * n**nd) >> wp
  643. else:
  644. sre += (are * cn * n**nd) >> wp
  645. sim += (aim * cn * n**nd) >> wp
  646. n += 1
  647. sre = -(sre << (nd+1))
  648. sim = -(sim << (nd+1))
  649. sre = ctx.ldexp(sre, -wp)
  650. sim = ctx.ldexp(sim, -wp)
  651. s = ctx.mpc(sre, sim)
  652. #case z complex, q real
  653. elif not ctx._im(q):
  654. wp = ctx.prec + extra2
  655. x = ctx.to_fixed(ctx._re(q), wp)
  656. a = b = x
  657. x2 = (x*x) >> wp
  658. prec0 = ctx.prec
  659. ctx.prec = wp
  660. c1, s1 = ctx.cos_sin(2*z)
  661. ctx.prec = prec0
  662. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  663. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  664. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  665. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  666. if (nd&1):
  667. sre = (a * snre) >> wp
  668. sim = (a * snim) >> wp
  669. else:
  670. sre = (a * cnre) >> wp
  671. sim = (a * cnim) >> wp
  672. n = 2
  673. while abs(a) > MIN:
  674. b = (b*x2) >> wp
  675. a = (a*b) >> wp
  676. t1 = (cnre*c1re - cnim*c1im - snre*s1re + snim*s1im) >> wp
  677. t2 = (cnre*c1im + cnim*c1re - snre*s1im - snim*s1re) >> wp
  678. t3 = (snre*c1re - snim*c1im + cnre*s1re - cnim*s1im) >> wp
  679. t4 = (snre*c1im + snim*c1re + cnre*s1im + cnim*s1re) >> wp
  680. cnre = t1
  681. cnim = t2
  682. snre = t3
  683. snim = t4
  684. if (nd&1):
  685. sre += (a * snre * n**nd) >> wp
  686. sim += (a * snim * n**nd) >> wp
  687. else:
  688. sre += (a * cnre * n**nd) >> wp
  689. sim += (a * cnim * n**nd) >> wp
  690. n += 1
  691. sre = -(sre << (nd+1))
  692. sim = -(sim << (nd+1))
  693. sre = ctx.ldexp(sre, -wp)
  694. sim = ctx.ldexp(sim, -wp)
  695. s = ctx.mpc(sre, sim)
  696. # case z and q complex
  697. else:
  698. wp = ctx.prec + extra2
  699. xre = ctx.to_fixed(ctx._re(q), wp)
  700. xim = ctx.to_fixed(ctx._im(q), wp)
  701. x2re = (xre*xre - xim*xim) >> wp
  702. x2im = (xre*xim) >> (wp - 1)
  703. are = bre = xre
  704. aim = bim = xim
  705. prec0 = ctx.prec
  706. ctx.prec = wp
  707. # cos(2*z), sin(2*z) with z complex
  708. c1, s1 = ctx.cos_sin(2*z)
  709. ctx.prec = prec0
  710. cnre = c1re = ctx.to_fixed(ctx._re(c1), wp)
  711. cnim = c1im = ctx.to_fixed(ctx._im(c1), wp)
  712. snre = s1re = ctx.to_fixed(ctx._re(s1), wp)
  713. snim = s1im = ctx.to_fixed(ctx._im(s1), wp)
  714. if (nd&1):
  715. sre = (are * snre - aim * snim) >> wp
  716. sim = (aim * snre + are * snim) >> wp
  717. else:
  718. sre = (are * cnre - aim * cnim) >> wp
  719. sim = (aim * cnre + are * cnim) >> wp
  720. n = 2
  721. while are**2 + aim**2 > MIN:
  722. bre, bim = (bre * x2re - bim * x2im) >> wp, \
  723. (bre * x2im + bim * x2re) >> wp
  724. are, aim = (are * bre - aim * bim) >> wp, \
  725. (are * bim + aim * bre) >> wp
  726. t1 = (cnre*c1re - cnim*c1im - snre*s1re + snim*s1im) >> wp
  727. t2 = (cnre*c1im + cnim*c1re - snre*s1im - snim*s1re) >> wp
  728. t3 = (snre*c1re - snim*c1im + cnre*s1re - cnim*s1im) >> wp
  729. t4 = (snre*c1im + snim*c1re + cnre*s1im + cnim*s1re) >> wp
  730. cnre = t1
  731. cnim = t2
  732. snre = t3
  733. snim = t4
  734. if(nd&1):
  735. sre += ((are * snre - aim * snim) * n**nd) >> wp
  736. sim += ((aim * snre + are * snim) * n**nd) >> wp
  737. else:
  738. sre += ((are * cnre - aim * cnim) * n**nd) >> wp
  739. sim += ((aim * cnre + are * cnim) * n**nd) >> wp
  740. n += 1
  741. sre = -(sre << (nd+1))
  742. sim = -(sim << (nd+1))
  743. sre = ctx.ldexp(sre, -wp)
  744. sim = ctx.ldexp(sim, -wp)
  745. s = ctx.mpc(sre, sim)
  746. if (nd&1):
  747. return (-1)**(nd//2) * s
  748. else:
  749. return (-1)**(1 + nd//2) * s
  750. @defun
  751. def _jacobi_theta2a(ctx, z, q):
  752. """
  753. case ctx._im(z) != 0
  754. theta(2, z, q) =
  755. q**1/4 * Sum(q**(n*n + n) * exp(j*(2*n + 1)*z), n=-inf, inf)
  756. max term for minimum (2*n+1)*log(q).real - 2* ctx._im(z)
  757. n0 = int(ctx._im(z)/log(q).real - 1/2)
  758. theta(2, z, q) =
  759. q**1/4 * Sum(q**(n*n + n) * exp(j*(2*n + 1)*z), n=n0, inf) +
  760. q**1/4 * Sum(q**(n*n + n) * exp(j*(2*n + 1)*z), n, n0-1, -inf)
  761. """
  762. n = n0 = int(ctx._im(z)/ctx._re(ctx.log(q)) - 1/2)
  763. e2 = ctx.expj(2*z)
  764. e = e0 = ctx.expj((2*n+1)*z)
  765. a = q**(n*n + n)
  766. # leading term
  767. term = a * e
  768. s = term
  769. eps1 = ctx.eps*abs(term)
  770. while 1:
  771. n += 1
  772. e = e * e2
  773. term = q**(n*n + n) * e
  774. if abs(term) < eps1:
  775. break
  776. s += term
  777. e = e0
  778. e2 = ctx.expj(-2*z)
  779. n = n0
  780. while 1:
  781. n -= 1
  782. e = e * e2
  783. term = q**(n*n + n) * e
  784. if abs(term) < eps1:
  785. break
  786. s += term
  787. s = s * ctx.nthroot(q, 4)
  788. return s
  789. @defun
  790. def _jacobi_theta3a(ctx, z, q):
  791. """
  792. case ctx._im(z) != 0
  793. theta3(z, q) = Sum(q**(n*n) * exp(j*2*n*z), n, -inf, inf)
  794. max term for n*abs(log(q).real) + ctx._im(z) ~= 0
  795. n0 = int(- ctx._im(z)/abs(log(q).real))
  796. """
  797. n = n0 = int(-ctx._im(z)/abs(ctx._re(ctx.log(q))))
  798. e2 = ctx.expj(2*z)
  799. e = e0 = ctx.expj(2*n*z)
  800. s = term = q**(n*n) * e
  801. eps1 = ctx.eps*abs(term)
  802. while 1:
  803. n += 1
  804. e = e * e2
  805. term = q**(n*n) * e
  806. if abs(term) < eps1:
  807. break
  808. s += term
  809. e = e0
  810. e2 = ctx.expj(-2*z)
  811. n = n0
  812. while 1:
  813. n -= 1
  814. e = e * e2
  815. term = q**(n*n) * e
  816. if abs(term) < eps1:
  817. break
  818. s += term
  819. return s
  820. @defun
  821. def _djacobi_theta2a(ctx, z, q, nd):
  822. """
  823. case ctx._im(z) != 0
  824. dtheta(2, z, q, nd) =
  825. j* q**1/4 * Sum(q**(n*n + n) * (2*n+1)*exp(j*(2*n + 1)*z), n=-inf, inf)
  826. max term for (2*n0+1)*log(q).real - 2* ctx._im(z) ~= 0
  827. n0 = int(ctx._im(z)/log(q).real - 1/2)
  828. """
  829. n = n0 = int(ctx._im(z)/ctx._re(ctx.log(q)) - 1/2)
  830. e2 = ctx.expj(2*z)
  831. e = e0 = ctx.expj((2*n + 1)*z)
  832. a = q**(n*n + n)
  833. # leading term
  834. term = (2*n+1)**nd * a * e
  835. s = term
  836. eps1 = ctx.eps*abs(term)
  837. while 1:
  838. n += 1
  839. e = e * e2
  840. term = (2*n+1)**nd * q**(n*n + n) * e
  841. if abs(term) < eps1:
  842. break
  843. s += term
  844. e = e0
  845. e2 = ctx.expj(-2*z)
  846. n = n0
  847. while 1:
  848. n -= 1
  849. e = e * e2
  850. term = (2*n+1)**nd * q**(n*n + n) * e
  851. if abs(term) < eps1:
  852. break
  853. s += term
  854. return ctx.j**nd * s * ctx.nthroot(q, 4)
  855. @defun
  856. def _djacobi_theta3a(ctx, z, q, nd):
  857. """
  858. case ctx._im(z) != 0
  859. djtheta3(z, q, nd) = (2*j)**nd *
  860. Sum(q**(n*n) * n**nd * exp(j*2*n*z), n, -inf, inf)
  861. max term for minimum n*abs(log(q).real) + ctx._im(z)
  862. """
  863. n = n0 = int(-ctx._im(z)/abs(ctx._re(ctx.log(q))))
  864. e2 = ctx.expj(2*z)
  865. e = e0 = ctx.expj(2*n*z)
  866. a = q**(n*n) * e
  867. s = term = n**nd * a
  868. if n != 0:
  869. eps1 = ctx.eps*abs(term)
  870. else:
  871. eps1 = ctx.eps*abs(a)
  872. while 1:
  873. n += 1
  874. e = e * e2
  875. a = q**(n*n) * e
  876. term = n**nd * a
  877. if n != 0:
  878. aterm = abs(term)
  879. else:
  880. aterm = abs(a)
  881. if aterm < eps1:
  882. break
  883. s += term
  884. e = e0
  885. e2 = ctx.expj(-2*z)
  886. n = n0
  887. while 1:
  888. n -= 1
  889. e = e * e2
  890. a = q**(n*n) * e
  891. term = n**nd * a
  892. if n != 0:
  893. aterm = abs(term)
  894. else:
  895. aterm = abs(a)
  896. if aterm < eps1:
  897. break
  898. s += term
  899. return (2*ctx.j)**nd * s
  900. @defun
  901. def jtheta(ctx, n, z, q, derivative=0):
  902. if derivative:
  903. return ctx._djtheta(n, z, q, derivative)
  904. z = ctx.convert(z)
  905. q = ctx.convert(q)
  906. # Implementation note
  907. # If ctx._im(z) is close to zero, _jacobi_theta2 and _jacobi_theta3
  908. # are used,
  909. # which compute the series starting from n=0 using fixed precision
  910. # numbers;
  911. # otherwise _jacobi_theta2a and _jacobi_theta3a are used, which compute
  912. # the series starting from n=n0, which is the largest term.
  913. # TODO: write _jacobi_theta2a and _jacobi_theta3a using fixed-point
  914. if abs(q) > ctx.THETA_Q_LIM:
  915. raise ValueError('abs(q) > THETA_Q_LIM = %f' % ctx.THETA_Q_LIM)
  916. extra = 10
  917. if z:
  918. M = ctx.mag(z)
  919. if M > 5 or (n == 1 and M < -5):
  920. extra += 2*abs(M)
  921. cz = 0.5
  922. extra2 = 50
  923. prec0 = ctx.prec
  924. try:
  925. ctx.prec += extra
  926. if n == 1:
  927. if ctx._im(z):
  928. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  929. ctx.dps += extra2
  930. res = ctx._jacobi_theta2(z - ctx.pi/2, q)
  931. else:
  932. ctx.dps += 10
  933. res = ctx._jacobi_theta2a(z - ctx.pi/2, q)
  934. else:
  935. res = ctx._jacobi_theta2(z - ctx.pi/2, q)
  936. elif n == 2:
  937. if ctx._im(z):
  938. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  939. ctx.dps += extra2
  940. res = ctx._jacobi_theta2(z, q)
  941. else:
  942. ctx.dps += 10
  943. res = ctx._jacobi_theta2a(z, q)
  944. else:
  945. res = ctx._jacobi_theta2(z, q)
  946. elif n == 3:
  947. if ctx._im(z):
  948. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  949. ctx.dps += extra2
  950. res = ctx._jacobi_theta3(z, q)
  951. else:
  952. ctx.dps += 10
  953. res = ctx._jacobi_theta3a(z, q)
  954. else:
  955. res = ctx._jacobi_theta3(z, q)
  956. elif n == 4:
  957. if ctx._im(z):
  958. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  959. ctx.dps += extra2
  960. res = ctx._jacobi_theta3(z, -q)
  961. else:
  962. ctx.dps += 10
  963. res = ctx._jacobi_theta3a(z, -q)
  964. else:
  965. res = ctx._jacobi_theta3(z, -q)
  966. else:
  967. raise ValueError
  968. finally:
  969. ctx.prec = prec0
  970. return res
  971. @defun
  972. def _djtheta(ctx, n, z, q, derivative=1):
  973. z = ctx.convert(z)
  974. q = ctx.convert(q)
  975. nd = int(derivative)
  976. if abs(q) > ctx.THETA_Q_LIM:
  977. raise ValueError('abs(q) > THETA_Q_LIM = %f' % ctx.THETA_Q_LIM)
  978. extra = 10 + ctx.prec * nd // 10
  979. if z:
  980. M = ctx.mag(z)
  981. if M > 5 or (n != 1 and M < -5):
  982. extra += 2*abs(M)
  983. cz = 0.5
  984. extra2 = 50
  985. prec0 = ctx.prec
  986. try:
  987. ctx.prec += extra
  988. if n == 1:
  989. if ctx._im(z):
  990. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  991. ctx.dps += extra2
  992. res = ctx._djacobi_theta2(z - ctx.pi/2, q, nd)
  993. else:
  994. ctx.dps += 10
  995. res = ctx._djacobi_theta2a(z - ctx.pi/2, q, nd)
  996. else:
  997. res = ctx._djacobi_theta2(z - ctx.pi/2, q, nd)
  998. elif n == 2:
  999. if ctx._im(z):
  1000. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  1001. ctx.dps += extra2
  1002. res = ctx._djacobi_theta2(z, q, nd)
  1003. else:
  1004. ctx.dps += 10
  1005. res = ctx._djacobi_theta2a(z, q, nd)
  1006. else:
  1007. res = ctx._djacobi_theta2(z, q, nd)
  1008. elif n == 3:
  1009. if ctx._im(z):
  1010. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  1011. ctx.dps += extra2
  1012. res = ctx._djacobi_theta3(z, q, nd)
  1013. else:
  1014. ctx.dps += 10
  1015. res = ctx._djacobi_theta3a(z, q, nd)
  1016. else:
  1017. res = ctx._djacobi_theta3(z, q, nd)
  1018. elif n == 4:
  1019. if ctx._im(z):
  1020. if abs(ctx._im(z)) < cz * abs(ctx._re(ctx.log(q))):
  1021. ctx.dps += extra2
  1022. res = ctx._djacobi_theta3(z, -q, nd)
  1023. else:
  1024. ctx.dps += 10
  1025. res = ctx._djacobi_theta3a(z, -q, nd)
  1026. else:
  1027. res = ctx._djacobi_theta3(z, -q, nd)
  1028. else:
  1029. raise ValueError
  1030. finally:
  1031. ctx.prec = prec0
  1032. return +res