Created
February 7, 2013 00:01
-
-
Save dfarmer/4727094 to your computer and use it in GitHub Desktop.
Torch7 Tensor version Laplace solver
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
require 'torch' | |
function calc(N, Niter) | |
u = torch.zeros(N,N) | |
u[1] = 1 | |
for i=1,Niter do | |
lua_update(u) | |
end | |
return u | |
end | |
function lua_update(u) | |
local dx = 0.1 | |
local dy = 0.1 | |
local dx2 = dx*dx | |
local dy2 = dy*dy | |
-- Assume u is square | |
nx,ny = u:size(1), u:size(2) | |
u[{ {2,nx-1}, {2,ny-1} }] = ((u[{ {3,nx},{2,ny-1} }] + u[{ {1,ny-2},{2,ny-1} }])*dy2 + (u[{ {2,nx-1},{3,nx} }] + u[{ {2,nx-1}, {1,ny-2} }]) * dx2) / (2*(dx2+dy2)) | |
end | |
timer = torch.Timer() | |
u = calc(100,8000) | |
print(timer:time().real) | |
print(u[2][2]) | |
print(torch.sum(u)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment