Created
October 7, 2014 15:22
-
-
Save IainNZ/c7dd570ffedbf629a81d to your computer and use it in GitHub Desktop.
genspmat2
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 genspmat2(N) | |
iseven(N) && error("even N not allowed") | |
# Determine memory usage | |
k = N^2 | |
for i in 1:N^2 | |
if i > N | |
k += 2 | |
elseif i > 1 | |
k += 1 | |
end | |
if i <= (N-1)*N | |
k += 2 | |
elseif i <= (N-1)*(N+1) | |
k += 1 | |
end | |
end | |
# Preallocate | |
I = Array(Int64,k) | |
J = Array(Int64,k) | |
V = Array(Int64,k) | |
k = 0 | |
for i in 1:N^2 | |
m = getm(i,N) | |
n = getn(i,N) | |
k += 1 | |
J[k] = i; I[k] = i; V[k] = m*m+n*n | |
if i > N | |
k += 1 | |
J[k] = i-N; I[k] = i; V[k] = 1 | |
k += 1 | |
J[k] = i-1; I[k] = i; V[k] = m | |
elseif i > 1 | |
k += 1 | |
J[k] = i-1; I[k] = i; V[k] = m | |
end | |
if i <= (N-1)*N | |
k += 1 | |
J[k] = i+1; I[k] = i; V[k] = m | |
k += 1 | |
J[k] = i+N; I[k] = i; V[k] = 1 | |
elseif i <= (N-1)*(N+1) | |
k += 1 | |
J[k] = i+1; I[k] = i; V[k] = m | |
end | |
end | |
return sparse(I,J,V) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment