Advertisement
reeps

generators_ready

Mar 30th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.25 KB | None | 0 0
  1. from ROOT import TH1F,TF1, gRandom, TStopwatch
  2. from math import sin, cos, fabs
  3. ################ const ##########################################3
  4. numb = 100000
  5. x1 = -1
  6. x2 = 1
  7. coef = 1
  8.  
  9. ############ normirovla functsii ###########################
  10.  
  11. def norm(a,b):
  12.     return 1.0 / 10.0 * (2 * b ** 5 + 15 * b + 5 * sin(b) * cos(b) - 2 * a ** 5 - 15 * a - 5 * sin(a) * cos(a))
  13.  
  14. ############### normirovka slagaemih #############
  15.  
  16. def norm3(a,b):
  17.     return  1.0 * (b ** 5 - a ** 5) / 5.0
  18.  
  19. def norm2(a,b):
  20.     return 1.0 * (b - a)
  21.  
  22. def norm1(a,b):
  23.     return (1 / 2.0) * (b + sin(b) * cos(b)) - (1 / 2.0) * (a + sin(a) * cos(a))
  24.  
  25. ################## beta ###################
  26.  
  27. def beta1(a, b):
  28.     return 1.0 * norm1(a,b) / (norm1(a,b) + norm2(a,b) + norm3(a, b))
  29.  
  30. def beta2(a, b):
  31.     return 1.0 * norm2(a,b) / (norm1(a,b) + norm2(a,b) + norm3(a, b))
  32.  
  33. def beta3(a, b):
  34.     return 1.0 * norm3(a,b) / (norm1(a,b) + norm2(a,b) + norm3(a, b))
  35.  
  36. ####################### functions of composition ###################
  37.  
  38. def func2(xm, p):
  39.     x = xm[0]
  40.     a = p[1]
  41.     b = p[2]
  42.     k = p[0]
  43.     if norm2(a,b) <= 0:
  44.         raise 'Wrong arguments in function: func1 norm1'
  45.     return k * norm2(a,b) * (1)  
  46.          
  47. def func3(xm, p):
  48.     x = xm[0]
  49.     a = p[1]
  50.     b = p[2]
  51.     k = p[0]
  52.     if norm3(a,b) <= 0:
  53.         raise 'Wrong arguments in function: func3 norm1'
  54.     return k * norm3(a,b) * (x ** 4)         
  55.    
  56. def func1(xm, p):
  57.     x = xm[0]
  58.     a = p[1]
  59.     b = p[2]
  60.     k = p[0]
  61.     if norm1(a,b) <= 0:
  62.         raise 'Wrong arguments in function: func1 norm1'
  63.     return k * norm1(a,b) * ((cos(x)) ** 2)  
  64.    
  65. ######################## main function ###################################   
  66. def func(xm, p):
  67.     #print a, b
  68.     x = xm[0]
  69.     a = p[1]
  70.     b = p[2]
  71.     k = p[0]
  72.     if norm(a, b) <= 0:
  73.     raise 'Wrong arguments in function: norm'
  74.     return k * norm(a,b) * (x ** 4 + 1 + (cos(x)) ** 2)      
  75. ################################### methods ##################################
  76. def neumann(a, b, c, d):
  77.     if d == 0:
  78.     if b <= a:
  79.             raise 'Wrong number in neumann'
  80.         fmax = func([ b ], [c, a, b])
  81.         while True:
  82.         mu = gRandom.Uniform(0, fmax)
  83.         r = gRandom.Uniform(a,b)
  84.         if mu <= func( [ r ], [c, a, b]):
  85.         return r
  86.         raise 'Could never reach this point'
  87.     else:
  88.     if b <= a:
  89.             raise 'Wrong number in neumann'
  90.         fmax = 1 * c * norm1(a,b)
  91.         while True:
  92.         mu = gRandom.Uniform(0, fmax)
  93.         r = gRandom.Uniform(a,b)
  94.         if mu <= func1( [ r ], [c, a, b]):
  95.         return r
  96.         raise 'Could never reach this point'
  97. ###############do the const#######################
  98. b1 = beta1(x1,x2)
  99. b2 = beta2(x1,x2)
  100. b3 = beta3(x1,x2)
  101. #######################################################
  102. def regression(a,b,c):
  103.     r = gRandom.Uniform(0,1)
  104.     if r < b1:
  105.     return neumann(a,b,c,1)
  106.     if (r < (b1 + b2) and r >= b1):
  107.     return fndirect(a,b,c,2)
  108.     if (r >= (b1 + b2) and r <= 1):
  109.     return fndirect(a,b,c, 3)
  110.  
  111. def fndirect(a, b, c, d):
  112.     if d == 3:
  113.     ua = 1.0 * a ** 5 / 5
  114.     ub = 1.0 * b ** 5 / 5
  115.     u = gRandom.Uniform(ua,ub)
  116.     if u < 0:
  117.         t = fabs(u)
  118.         r = -(5.0 * t) ** (1.0 / 5)
  119.     else:
  120.         r = (5.0 * u) ** (1.0 / 5) 
  121.         return r
  122.     if d == 2:
  123.     u = gRandom.Uniform(a,b)
  124.     r = u
  125.     return r   
  126. ####################################### main #############################
  127. f = TF1("f", func, x1, x2, 3)
  128. f.SetParameter(0,x1)
  129. f.SetParameter(1,x2)
  130. f.SetParameter(2,coef)
  131. f.Draw()
  132. raw_input()
  133. h1 = TH1F("h1", "neumann method",100,x1,x2 )
  134. h2 = TH1F("h2", "regression", 100, x1, x2)
  135. #zapolnenie neumann
  136. sw = TStopwatch()
  137. sw.Start()
  138. for i in range(0, numb):
  139.     r = neumann(x1, x2, coef, 0)
  140.     h1.Fill(r)
  141. sw.Stop()
  142. sw.Print()
  143. #print norm1(x1,x2), norm2(x1,x2), norm3(x1,x2), norm1(x1,x2) + norm2(x1,x2) + norm3(x1,x2)
  144. h1.Draw()
  145. print "Mean is ", h1.GetMean()
  146. print "RMS is " , h1.GetRMS()
  147. raw_input()      
  148. f.FixParameter(1,x1)
  149. f.FixParameter(2,x2)
  150. h1.Fit(f)        
  151. raw_input()
  152. sw1 = TStopwatch()
  153. sw1.Start()
  154. for i in range(0, numb):
  155.     r =regression(x1,x2,coef)
  156. #    r = fndirect(x1,x2,coef,3)
  157.     h2.Fill(r)
  158. sw1.Stop()
  159. sw1.Print()
  160. h2.Draw()
  161. print "Mean2 is ", h2.GetMean()
  162. print "RMS2 is " , h2.GetRMS()
  163. raw_input()
  164. h2.Fit(f)
  165. raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement