Skip to content

Instantly share code, notes, and snippets.

@dfarmer
Created February 7, 2013 00:01
Show Gist options
  • Save dfarmer/4727094 to your computer and use it in GitHub Desktop.
Save dfarmer/4727094 to your computer and use it in GitHub Desktop.
Torch7 Tensor version Laplace solver
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