-
-
Save Seanny123/54e5cdc6336357e71ee01030bd762e91 to your computer and use it in GitHub Desktop.
PyJulia Example Use Case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
r = np.arange(1.0, 11.0, 0.1) | |
n = len(r)**3 | |
pts = np.empty((n, 3)) | |
i = 0 | |
for x in r: | |
for y in r: | |
for z in r: | |
pts[i,:] = [x, y, z] | |
i += 1 | |
np.savetxt("points.csv", pts, delimiter=",") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function force(x, y, z) | |
r2 = x^2 + y^2 + z^2 | |
n = sqrt(r2) | |
return [x/(n*r2), y/(n*r2), z/(n*r2)] | |
end | |
function force_sum(pts) | |
s = zeros(3) | |
@time for i in 1:size(pts, 1) # ~0.3 seconds | |
s += force(pts[i,:]...) | |
end | |
return s | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time | |
import julia | |
j = julia.Julia() | |
j.include("new.jl") | |
pts = np.genfromtxt("points.csv", delimiter=',') | |
start = time.time() | |
j.force_sum(pts) | |
end = time.time() | |
print(end-start) # ideally with small overhead (~0.4 seconds) | |
print(s) | |
# do some more processing in python |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time | |
import julia | |
j = julia.Julia() | |
force_sum = j.evalfile("new.jl") | |
pts = np.genfromtxt("points.csv", delimiter=',') | |
start = time.time() | |
s = force_sum(pts) | |
end = time.time() | |
print(end-start) # ~3 seconds | |
print(s) | |
# do some more processing in python |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import time | |
def force(x, y, z): | |
r2 = x**2 + y**2 + z**2 | |
n = sqrt(r2) | |
return (x/(n*r2), y/(n*r2), z/(n*r2)) | |
pts = np.genfromtxt("points.csv", delimiter=',') | |
s = np.array([0.0, 0.0, 0.0]) | |
start = time.time() | |
for i in range(len(pts)): | |
s += force(pts[i,0], pts[i,1], pts[i,2]) | |
end = time.time() | |
print(end-start) # ~6 seconds | |
print(s) | |
# do some more processing in python |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment