filionQuadrature.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import numpy
  2. def Taylor(x,coef):
  3. q=1
  4. s=0
  5. for c in coef:
  6. s+=c*q
  7. q*=x
  8. return s
  9. def filionAlpha(th,th3,sth,cth):
  10. if abs(th)<0.1:
  11. return Taylor(th,[0,0,0,2/45,0,-2/315,0,2/4725])
  12. th2=th*th
  13. return (th2+sth*cth*th-2*sth*sth)/th3
  14. def filionBeta(th,th3,sth,cth):
  15. if abs(th)<0.1:
  16. return Taylor(th,[2/3,0,2/15,0,-4/105,0,2/567,0,-4/22275])
  17. return 2*((1+cth*cth)*th-2*sth*cth)/th3
  18. def filionGamma(th,th3,sth,cth):
  19. if abs(th)<0.1:
  20. return Taylor(th,[4/3,0,-2/15,0,1/210,0,-1/11340])
  21. return 4*(sth-th*cth)/th3
  22. def cEven(qs,n):
  23. idx=numpy.arange(0,2*n+1,2)
  24. return numpy.sum(qs[idx])
  25. def qFunc(qf,t,x0,h,n,func):
  26. qx=numpy.linspace(x0,x0+2*h*n,2*n+1)
  27. qs=[q*func(t*x) for x,q in zip(qx,qf)]
  28. qs[0]*=0.5
  29. qs[len(qs)-1]*=0.5
  30. return numpy.array(qs)
  31. def cOdd(qs,n):
  32. idx=numpy.arange(1,2*n+1,2)
  33. return numpy.sum(qs[idx])
  34. def filionQuadrature(qf,t,x0,h,n,func,debug=0):
  35. #calculate integral f(x) func(tx) in [x0,x0+2*n*h]
  36. #where func is either cos or sine
  37. cFunc=numpy.cos
  38. iFunc=numpy.sin
  39. sign=1
  40. if func=='sin':
  41. cFunc=numpy.sin
  42. iFunc=numpy.cos
  43. sign=-1
  44. th=t*h
  45. sth=numpy.sin(th)
  46. cth=numpy.cos(th)
  47. th3=th*th*th
  48. x2n=x0+2*n*h
  49. A=filionAlpha(th,th3,sth,cth)
  50. B=filionBeta(th,th3,sth,cth)
  51. C=filionGamma(th,th3,sth,cth)
  52. qs=qFunc(qf,t,x0,h,n,cFunc)
  53. CE=cEven(qs,n)
  54. CO=cOdd(qs,n)
  55. D=sign*(qf[2*n]*iFunc(t*x2n)-qf[0]*iFunc(t*x0))
  56. r=h*(A*D+B*CE+C*CO)
  57. if debug:
  58. print('fillon r={:.2f} A={:.2f} D={:.2f} B={:.2f} CE={:.2f} C={:.2f} CO={:.2f}'.format(r,A,D,B,CE,C,CO))
  59. return r
  60. def tabulateFunction(f,x0,n,h):
  61. qx=[x0+i*h for i in range(2*n+1)]
  62. qy=[f(x) for x in qx]
  63. return qx,qy