member 428835
				
				
			 
			
	
	
	
	
			
		
		
		
	Hi PF!
I followed someone's help on here and have the following code in python that performs monte carlo integration
	
	
	
    
	
		
However, in line 10 we see the integrand is typed by a user. Is there a way to import a Mathematica function into python to automate the procedure? Thanks so much for your help!
				
			I followed someone's help on here and have the following code in python that performs monte carlo integration
		Python:
	
	from math import *
from random import *
def integrate(alpha):
    #  MONTE CARLO INTEGRATION OVER NON-RECTANGULAR DOMAINS
    def f(pt):
        # RETURN INTEGRAND AS FUNCTION
        x = pt[0]
        y = pt[1]
        return x*y**2
    def testRegion(pt):
        return (pt[0]/2 > pt[1])
    def genpoint():
        # GENERATE COORDINATES IN A SQUARE
        x = (1 - math.sin(alpha))*random() + math.sin(alpha)
        y = (1 - math.sin(alpha))*random() + math.sin(alpha)
        return (x,y)
    
    # INITIALIZE
    Sum = 0.0
    Area = (1 - math.sin(alpha))**2
    N = 0
    samp_pts = 10000
    int_dist = []
    iterations = 1000
    # INTEGRATION
    for _ in range(iterations):
        for i in range(samp_pts):
            N+=1
            pt = genpoint()
            if testRegion(pt):
                Sum += f(pt)
        sol = Sum*Area/N
        int_dist.append(sol)
    return int_distHowever, in line 10 we see the integrand is typed by a user. Is there a way to import a Mathematica function into python to automate the procedure? Thanks so much for your help!
 
 
		 
 
		