Modular matrix inversion with large number - python

I am trying to find a way for modular matrix inversion. I found the code here:
def generalizedEuclidianAlgorithm(a, b):
if b > a:
#print a, b
return generalizedEuclidianAlgorithm(b,a);
elif b == 0:
return (1, 0);
else:
#print a,b
(x, y) = generalizedEuclidianAlgorithm(b, a % b);
return (y, x - (a / b) * y)
def inversemodp(a, p):
a = a % p
if (a == 0):
print "a is 0 mod p"
return 0
(x,y) = generalizedEuclidianAlgorithm(p, a % p);
return y % p
def identitymatrix(n):
return [[long(x == y) for x in range(0, n)] for y in range(0, n)]
def inversematrix(matrix, q):
n = len(matrix)
A = np.matrix([[ matrix[j, i] for i in range(0,n)] for j in range(0, n)], dtype = long)
Ainv = np.matrix(identitymatrix(n), dtype = long)
for i in range(0, n):
factor = inversemodp(A[i,i], q)
A[i] = A[i] * factor % q
Ainv[i] = Ainv[i] * factor % q
for j in range(0, n):
if (i != j):
factor = A[j, i]
A[j] = (A[j] - factor * A[i]) % q
Ainv[j] = (Ainv[j] - factor * Ainv[i]) % q
# print A, Ainv
# print i, j, factor
return Ainv
When I test with small prime q and matrix elements, the result is correct. However, when I test with large prime q and matrix consisting of large elements (e.g, 1024 bits), it printed out the error:
A = np.matrix([[ matrix[j, i] for i in range(0,n)] for j in range(0, n)], dtype = long)
File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 257, in __new__
arr = N.array(data, dtype=dtype, copy=copy)
Is it because of dtype of np.matrix? If so, why "long" datatype cannot support this case?
Could you please show me how to solve the error. Thanks in advance

I have just figured out that np.array causes many problems. One solution is just to treat the matrix as 2-dimensional list. The following is my code
def generalizedEuclidianAlgorithm(a, b):
if b > a:
return generalizedEuclidianAlgorithm(b,a);
elif b == 0:
return (1, 0);
else:
(x, y) = generalizedEuclidianAlgorithm(b, a % b);
return (y, x - (a / b) * y)
def inversemodp(a, p):
a = a % p
if (a == 0):
print "a is 0 mod p"
return 0
(x,y) = generalizedEuclidianAlgorithm(p, a % p);
return y % p
def identitymatrix(n):
return [[long(x == y) for x in range(0, n)] for y in range(0, n)
def multiply_vector_scalar (vector, scalar, q):
kq = []
for i in range (0, len(vector)):
kq.append (vector[i] * scalar %q)
return kq
def minus_vector_scalar1(vector1, scalar, vector2, q):
kq = []
for i in range (0, len(vector1)):
kq.append ((vector1[i] - scalar * vector2[i]) %q)
return kq
def inversematrix1(matrix, q):
n = len(matrix)
A =[]
for j in range (0, n):
temp = []
for i in range (0, n):
temp.append (matrix[j][i])
A.append(temp)
Ainv = identitymatrix(n)
for i in range(0, n):
factor = inversemodp(A[i][i], q)
A[i] = multiply_vector_scalar(A[i],factor,q)
Ainv[i] = multiply_vector_scalar(Ainv[i],factor,q)
for j in range(0, n):
if (i != j):
factor = A[j][i]
A[j] = minus_vector_scalar1(A[j],factor,A[i],q)
Ainv[j] = minus_vector_scalar1(Ainv[j],factor,Ainv[i],q)
return Ainv

Related

Heat equation divide by zero issue

I'm writing a code that solves a heat equation implementing an implicit method. The problem is that the values between first and last layer of the matrix are NaNs. What could be the problem?
From my problem of view, the main issue might be with the 105th line, which represents the convrsion of original function to the one that includes the boundary function.
Boundary functions code:
def func(x, t):
return x*(1 - x)*np.exp(-2*t)
# boundary function for x = 0 and x = 1
def q0(t):
return t*np.exp(-t/0.1)*np.cos(t) # граничное условие при x = 0
def q1(t):
return t*np.exp(-t/0.5)*np.cos(t) # граничное уcловие при x = 1
def derivative(f, x0, step):
return (f(x0+step) - f(x0))/step
# boundary function that for t = 0
def u_x0(x):
return (-x + 1)*x
Function that solves the three-diagonal matrix equation
def solution(a, b):
n = len(a)
x = [0 for k in range(0, n)]
# forward
v = [0 for k in range(0, n)]
u = [0 for k in range(0, n)]
# first string (t = 0)
v[0] = a[0][1] / (-a[0][0])
u[0] = ( - b[0]) / (-a[0][0])
for i in range(1, n - 1):
v[i] = a[i][i+1] / ( -a[i][i] - a[i][i-1]*v[i-1] )
u[i] = ( a[i][i-1]*u[i-1] - b[i] ) / ( -a[i][i] - a[i][i-1]*v[i-1] )
# last string (t = 1)
v[n-1] = 0
u[n-1] = (a[n-1][n-2]*u[n-2] - b[n-1]) / (-a[n-1][n-1] - a[n-1][n-2]*v[n-2])
x[n-1] = u[n-1]
for i in range(n-1, 0, -1):
x[i-1] = v[i-1] * x[i] + u[i-1]
return x
Coefficent matrix values:
A = -t/h**2
B = 1 + 2*t/h**2
C = -t/h**2
Code that actually solves the matrix:
i = 1
X =[]
while i < 99:
X = solution(cool_array, f)
k = 0
while k < len(x_i):
#line-105
X[k] += 0.01*(func(x_i[k], x_i[i]) - (1 - x_i[i])*derivative(q0, x_i[i], 0.01) - (x_i[i])*derivative(q1, x_i[i], 0.01))
k+=1
a = 1
while a < 98:
w_h_t[i][a] = X[a]
a+=1
f = X
f[0] = w_h_t[i][0]
f[99] = w_h_t[i][99]
i+=1
print(w_h_t)
As far as I understand, the algorith solution(a, b) is written properly, so I guess the problem might be with the boundary functions or with the 105th line. The output I expect is at least an array of number, not NaNs.

How to reduce run time in my python code?

I am currently working on a project that requires me to run a complete python code base. For research purpose, I need to run the code as fast as possible. Yet I am fairly new to programming and have no idea how to reduce run time. So I hope someone can help me on that. Any advice would be appreciated. Here's part of my code base, which used a lot of nested for loops, so it might significantly increase run time.
def a_j(r, a, A): # the Claussius-Mossotti factor, determined by a symmetric (3 × 3) matrix such that (A_i)^T = A_i
alph = np.array([[0,0,0],[0,0,0],[0,0,0]],complex)
for i in range(3):
for j in range(3):
alph[i,j] = (r * a * A[i,j])
return alph
def W_ext(x, k, rho, alpha, A): # particle–particle interaction term
n = x.shape[0] # the number of x vextors
result = np.zeros([3*n,3*n],complex)
u = np.zeros((n, 3)) # u = x - x'
for i in range(n):
for j in range(n):
if i != j:
u[i] = x[i] - x[j]
block_result = a_j(rho[i], alpha, A) * G((u[i]), k) * a_j(rho[j], alpha, A)
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return result.imag
def A_ext(rho, a, A): # single-particle term
n = rho.shape[0]
result = np.zeros([3*n,3*n],complex)
for i in range(n):
for j in range(n):
if i == j:
block_result = a_j(rho[i], a, A).imag
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return result # (3 x 3) matrix
def P_ext(e, A, W, omega):
eT = np.matrix.getH(e)
mm1 = np.matmul(A, e)
mm2 = np.matmul(W, e)
extinction = (np.dot(eT, mm1) + np.dot(eT, mm2)) * (omega/2.0)
return extinction
#ABSORPTION
def W_abs(x, k, rho, alpha, A, chi): # particle–particle interaction term
n = x.shape[0]
result = np.zeros([3*n,3*n],complex)
u = np.zeros((n, 3))
for i in range(n):
for j in range(n):
if i != j:
u[i] = x[i] - x[j]
block_result = np.matrix.getH(a_j(rho[i], alpha, A)) * (1.0 / np.conjugate(chi)).imag * a_j(rho[i], alpha, A) * G((u[i]), k) * a_j(rho[j], alpha, A)
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return 2.0 * result.real # (3 x 3) matrix
def A_abs(rho, a, A, chi): # single-particle term
n = rho.shape[0]
result = np.zeros([3*n,3*n],complex)
for i in range(n):
for j in range(n):
if i == j:
block_result = np.matrix.getH(a_j(rho[i], a, A)) * (1.0 / np.conjugate(chi)).imag * a_j(rho[i], a, A)
for m in range(3):
for l in range(3):
result[3*i + m, 3*j + l] = block_result[m,l]
return result # (3 x 3) matrix

Python: TypeError: 'int' object is not subscriptable Monte Carlo

I know this is a fairly common question but looking at other answers for this has not helped me as I do not understand the problem exactly. I get the actual error itself but this problem is only occurring in one of the editions of my code where this line is the same in both that is the issue.
Here is version one:
import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
#S[i, j] = 1 (spin up)
#S[i, j] = -1 (spin down)
def InitSpins(S, N):
for i in range(N):
for j in range(N):
S[i, j] = 2*rnd.randint(2)-1
return S
def GenerateMove(S, N):
i = rnd.randint(N)
j = rnd.randint(N)
return i, j, S[i][j], - S[i][j]
def ComputeEnergy(S, N):
E = 0.0
for i in range(N):
for j in range(N):
E += -J*S[i,j]*S[(i+1)%N,j] - J*S[i,j]*S[i,(j+1)%N] - H*S[i,j]
return E
def ComputeDiffEnergy(S, i, j, old, new, N):
Eold = ComputeEnergy(S, N)
S[i,j] = new
Enew = ComputeEnergy(S, N)
S[i,j] = old
return Enew - Eold
def MCStep(S, N, E, nacc, T):
i,j,old,new = GenerateMove(S, N)
DE = ComputeDiffEnergy(S, i, j, old, new, N)
if np.any(DE <= 0) or np.any(rnd.random() < np.exp(-DE/T)):
AcceptMove(S, i, j, old, new, N)
E += DE
nacc += 1
# elif
# AcceptMove(S, i, j, old, new, N)
# E += DE
# nacc += 1
else:
RejectMove(S, i, j, old, new, N)
return E, nacc
def AcceptMove(S, i, j, old, new, N):
S[i, j] = new
def RejectMove(S, i, j, old, new, N):
S[i, j] = old
def ComputeX(S, N):
X = 0.0
for i in range(N):
for j in range(N):
X += S[i,j]
X += 1/(N**2)*np.sum(X)
return X
N = 10 #dimension of lattice
NIter = 10000 #iterations for production run
NEquil = NIter//10 #iterations in actual calculation
NT = 100 #number of time steps
T = 2.4
H = 0.0 #set outside magnetisation
J = 1.0 #set internal magnetisation
S = np.empty([N, N]) #set initlal spin array
S = InitSpins(S, N)
print(S)
print('energy',ComputeEnergy(S, N))
# Equilibration:
nacc = 0
E = ComputeEnergy(S, N)
for i in range(NEquil):
E, nacc = MCStep(S, N, E, nacc, T)
# Production run
nacc = 0
sum_E = 0.0
sum_E2 = 0.0
E = ComputeEnergy(S, N)
for i in range(NIter):
E, nacc = MCStep(S, N, E, nacc, T)
sum_E += E
sum_E2 += E**2
X = ComputeX(S, N)
def SumX(X, N):
sum_X = 0.0
sum_X2 = 0.0
for i in range(NIter):
sum_X += X
sum_X2 += X**2
return sum_X, sum_X2
sum_X, sum_X2 = SumX(X, N)
# Calculate averages
av_E = sum_E/float(NIter)
av_E2 = sum_E2/float(NIter)
av_X = sum_X/float(NIter)
av_X2 = sum_X2/float(NIter)
CV = 1/(1*(T**2))*(av_E2-av_E**2)
chi = 1/1*(T**2)*(av_X2-av_X**2)
#M =
# print results
print("acceptance ratio",nacc/float(NIter))
print("average energy",av_E)
print("heat capacity",CV)
print("magnetic susceptibility", chi)
#print("magnetisation, )
This, though incomplete, gives me results.
Then I tried to make python run through a range of T (temperature) to give me a graph of the changing heat capacity. This breaks my GenerateMove function with the error given in the title for the return i,j, S[i][j], -S[i][j].
Updated code that doesn't work is:
import numpy as np
import numpy.random as rnd
import matplotlib.pyplot as plt
#S[i, j] = 1 (spin up)
#S[i, j] = -1 (spin down)
def InitSpins(S, N):
for i in range(N):
for j in range(N):
S[i, j] = 2*rnd.randint(2)-1
return S
def GenerateMove(S, N):
i = rnd.randint(N)
j = rnd.randint(N)
return i, j, S[i][j], - S[i][j]
def ComputeEnergy(S, N):
E = 0.0
for i in range(N):
for j in range(N):
E += -J*S[i,j]*S[(i+1)%N,j] - J*S[i,j]*S[i,(j+1)%N] - H*S[i,j]
return E
def ComputeDiffEnergy(S, i, j, old, new, N):
Eold = ComputeEnergy(S, N)
S[i,j] = new
Enew = ComputeEnergy(S, N)
S[i,j] = old
return Enew - Eold
def MCStep(S, N, E, nacc, T):
i,j,old,new = GenerateMove(S, N)
DE = ComputeDiffEnergy(S, i, j, old, new, N)
if np.any(DE <= 0) or np.any(rnd.random() < np.exp(-DE/T)):
AcceptMove(S, i, j, old, new, N)
E += DE
nacc += 1
# elif
# AcceptMove(S, i, j, old, new, N)
# E += DE
# nacc += 1
else:
RejectMove(S, i, j, old, new, N)
return E, nacc
def AcceptMove(S, i, j, old, new, N):
S[i, j] = new
def RejectMove(S, i, j, old, new, N):
S[i, j] = old
def ComputeX(S, N):
X = 0.0
for i in range(N):
for j in range(N):
X += S[i,j]
X += 1/(N**2)*np.sum(X)
return X
N = 10 #dimension of lattice
NIter = 10000 #iterations for production run
NEquil = NIter//10 #iterations in actual calculation
NT = 100 #number of time steps
T = np.linspace(0.1,5.0,NT) #set temperaure range
C = np.zeros(NT) #inital heat capacity
H = 0.0 #set outside magnetisation
J = 1.0 #set internal magnetisation
S = np.empty([N, N]) #set initlal spin array
S = InitSpins(S, N)
print(S)
print('energy',ComputeEnergy(S, N))
## Equilibration:
#nacc = 0
#E = ComputeEnergy(S, N)
#for i in range(NEquil):
# E, nacc = MCStep(S, N, E, nacc, T)
#
## Production run
#nacc = 0
#sum_E = 0.0
#sum_E2 = 0.0
#E = ComputeEnergy(S, N)
#for i in range(NIter):
# E, nacc = MCStep(S, N, E, nacc, T)
# sum_E += E
# sum_E2 += E**2
X = ComputeX(S, N)
def SumX(X, N):
sum_X = 0.0
sum_X2 = 0.0
for i in range(NIter):
sum_X += X
sum_X2 += X**2
return sum_X, sum_X2
sum_X, sum_X2 = SumX(X, N)
for t in range(NT):
nacc = 0
sum_E = 0.0
sum_E2 = 0.0
S = InitSpins(S, N)
E = ComputeEnergy(S, N)
for i in range(NIter):
S = MCStep(S, N, E, nacc, T[t])
for i in range(NEquil):
S = MCStep(S, N, E, nacc, T[t])
E, nacc = MCStep(S, N, E, nacc, T)
sum_E += E
sum_E2 += E**2
av_E = sum_E/float(NIter)
av_E2 = sum_E2/float(NIter)
C[t] = (sum_E/(float(NIter*N*N)) - sum_E*sum_E/(NIter*NIter*N*N))/T[t]**2
# Calculate averages
av_X = sum_X/float(NIter)
av_X2 = sum_X2/float(NIter)
CV = 1/(1*(T**2))*(av_E2-av_E**2)
chi = 1/1*(T**2)*(av_X2-av_X**2)
#M =
# print results
print("acceptance ratio",nacc/float(NIter))
print("average energy",av_E)
print("heat capacity",CV)
print("magnetic susceptibility", chi)
#print("magnetisation, )
#plotting resultts
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(T, C)
plt.show()
EDIT: here is the full traceback
Traceback (most recent call last):
File "<ipython-input-3-52258536ecbb>", line 1, in <module>
runfile('F:/adv. numerical project graph.py', wdir='F:')
File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "F:/adv. numerical project graph.py", line 121, in <module>
S = MCStep(S, N, E, nacc, T[t])
File "F:/adv. numerical project graph.py", line 42, in MCStep
i,j,old,new = GenerateMove(S, N)
File "F:/adv. numerical project graph.py", line 24, in GenerateMove
return i, j, S[i][j], - S[i][j]
IndexError: tuple index out of range
You have unnecessarily complicated your code. The extension of your first code for a fixed temperature to a range of temperatures is quite straightforward. All you had to do was to club your code performing the lattice initialization, MC equilibration and MC production runs in a for loop running over the given temperature range. I have created lists to store the heat capacity, chi, and average energy at each temperature.
Below is how it's done. I am choosing 30 temperature values instead of 100 so as to quickly give you a working solution. I am only showing the relevant code. Rest of the code with function definitions stay the same.
I am not comfortable with the behavior of your MC predicted heat capacity. It is supposed to increase with temperature. You need to cross check your implementation. You have everything now.
NT = 30 # number of temperature steps
T_list = np.linspace(0.1, 5.0, NT) # set temperaure range
av_E_list, CV_list, chi_list = [], [], []
for T in T_list: # <--- Loop over different temperatures
print ("Performing MC for T = %.2f" %T)
S = np.empty([N, N]) #set initlal spin array
S = InitSpins(S, N)
# Equilibration:
nacc = 0
E = ComputeEnergy(S, N)
for i in range(NEquil):
E, nacc = MCStep(S, N, E, nacc, T)
# Production run
nacc = 0
sum_E = 0.0
sum_E2 = 0.0
E = ComputeEnergy(S, N)
for i in range(NIter):
E, nacc = MCStep(S, N, E, nacc, T)
sum_E += E
sum_E2 += E**2
X = ComputeX(S, N)
sum_X, sum_X2 = SumX(X, N)
# Calculate averages
av_E = sum_E/float(NIter)
av_E2 = sum_E2/float(NIter)
av_X = sum_X/float(NIter)
av_X2 = sum_X2/float(NIter)
CV = 1/(1*(T**2))*(av_E2-av_E**2)
chi = 1/1*(T**2)*(av_X2-av_X**2)
CV_list.append(CV)
chi_list.append(chi)
av_E_list.append(av_E)
# Plotting
plt.plot(T_list, CV_list, '-b*')

Efficient way to create a dense matrix from diagonal vectors in Python?

I am trying to create this matrix in Python using numpy vectors:
where the values come from a function. I have implemented it with repeatedly using numpy.diag but for large dimensions, it becomes very slow. Here is the code:
def makeS(N):
vec = np.full(N, 2*v(x_range[1]))
vec[0]*=0.5
S = np.diag(vec)
vec = np.full(N-1, v(x_range[0]))
S+= np.diag(vec, 1)
for m in xrange(1, N):
vec = np.full(N-m, 2*v(x_range[m+1]))
vec[0]*= 0.5
S += np.diag(vec, -m)
return S
where v() is the said function and x_range is a vector of x-values. Is there a way to make this more efficient?
Edit:
Here is a full example:
import numpy as np
import math
N = 5
x_range = np.linspace(0, 1, N+1)
def v(x):
return math.exp(x)
def makeS(N):
vec = np.full(N, 2*v(x_range[1]))
vec[0]*=0.5
S = np.diag(vec)
vec = np.full(N-1, v(x_range[0]))
S+= np.diag(vec, 1)
for m in xrange(1, N):
vec = np.full(N-m, 2*v(x_range[m+1]))
vec[0]*= 0.5
S += np.diag(vec, -m)
return S
print makeS(N)
which outputs
[[ 1.22140276 1. 0. 0. 0. ]
[ 1.4918247 2.44280552 1. 0. 0. ]
[ 1.8221188 2.9836494 2.44280552 1. 0. ]
[ 2.22554093 3.6442376 2.9836494 2.44280552 1. ]
[ 2.71828183 4.45108186 3.6442376 2.9836494 2.44280552]]
This is the fastest approach I could find:
def makeS(N):
values = np.array([v(x) for x in x_range])
values_doubled = 2 * values
result = np.eye(N, k=1) * values[0]
result[:, 0] = values[1:]
for i in xrange(N - 1):
result[i + 1, 1:i + 2] = values_doubled[1:i + 2][::-1]
return result
With N=2000 the original takes 26.97 seconds on my machine while the new version takes 0.02339 seconds.
Here is the complete script for evaluating timings with some additional approaches.
import numpy as np
import math
import timeit
def v(x):
return math.exp(x)
def makeS1(N, x_range):
vec = np.full(N, 2 * v(x_range[1]))
vec[0] *= 0.5
S = np.diag(vec)
vec = np.full(N - 1, v(x_range[0]))
S += np.diag(vec, 1)
for m in xrange(1, N):
vec = np.full(N - m, 2 * v(x_range[m + 1]))
vec[0] *= 0.5
S += np.diag(vec, -m)
return S
def makeS2(N, x_range):
values = np.array([v(x) for x in x_range])
values_doubled = 2 * values
def value_at_position(ai, aj):
result = np.zeros((N, N))
for i, j in zip(ai.flatten(), aj.flatten()):
if j > i + 1:
continue
elif j == i + 1:
result[i, j] = values[0]
elif j == 0:
result[i, j] = values[i + 1]
else:
result[i, j] = values_doubled[i - j + 1]
return result
return np.fromfunction(value_at_position, (N, N))
def makeS3(N, x_range):
values = np.array([v(x) for x in x_range])
values_doubled = 2 * values
result = np.zeros((N, N))
for i in xrange(N):
for j in xrange(min(i + 2, N)):
if j == i + 1:
result[i, j] = values[0]
elif j == 0:
result[i, j] = values[i + 1]
else:
result[i, j] = values_doubled[i - j + 1]
return result
def makeS4(N, x_range):
values = np.array([v(x) for x in x_range])
values_doubled = 2 * values
result = np.eye(N, k=1) * values[0]
result[:, 0] = values[1:]
for i in xrange(N - 1):
result[i + 1, 1:i + 2] = values_doubled[1:i + 2][::-1]
return result
def main():
N = 2000
x_range = np.random.randn(N + 1)
start = timeit.default_timer()
s1 = makeS1(N, x_range)
print 'makeS1', timeit.default_timer() - start
start = timeit.default_timer()
s2 = makeS2(N, x_range)
print 'makeS2', timeit.default_timer() - start
start = timeit.default_timer()
s3 = makeS3(N, x_range)
print 'makeS3', timeit.default_timer() - start
start = timeit.default_timer()
s4 = makeS4(N, x_range)
print 'makeS4', timeit.default_timer() - start
if N < 10:
print s1
print s2
print s2
print s4
assert np.allclose(s1, s2)
assert np.allclose(s2, s3)
assert np.allclose(s3, s4)
main()
On my machine, this produces the output:
makeS1 26.9707232448
makeS2 11.7728229076
makeS3 0.643742975052
makeS4 0.0233912765665

Nearest neighbors

I am writing a piece of code to print the nearest neighbors for the elements of a matrix. I get an
"invalid index" error
when I try to print the list of the neighbours (last line). Can you spot why?
Here's the code:
neighbours = ndarray((ran_x-2, ran_y-2,8),int)
for i in range(0, ran_x):
for j in range(0, ran_y):
if 1 < i < ran_x-1:
if 1 < j < ran_y-1:
neighbours = ([matrix[i-1,j-1],matrix[i-1,j],matrix[i-1,j+1],matrix[i,j-1],matrix[i,j+1],matrix[i+1,j-1],matrix[i+1,j],matrix[i+1,j+1]])
neighbours = np.array(neighbours)
for l in range(1, ran_x-1):
for m in range(1, ran_y-1):
print neighbours[l,m]
Look at the size of your array, it's a (ran_x - 2) * (ran_y - 2) elements array:
neighbours = ndarray((ran_x-2, ran_y-2,8),int)
And you try to access the elements at index ran_x-1 and ran_y-1 which are out of bound.
sliding window stride_tricks is great for this (https://stackoverflow.com/a/11000193/541038)
import numpy as np
from numpy.lib.stride_tricks import as_strided
def sliding_window(arr, window_size):
""" Construct a sliding window view of the array"""
arr = np.asarray(arr)
window_size = int(window_size)
if arr.ndim != 2:
raise ValueError("need 2-D input")
if not (window_size > 0):
raise ValueError("need a positive window size")
shape = (arr.shape[0] - window_size + 1,
arr.shape[1] - window_size + 1,
window_size, window_size)
if shape[0] <= 0:
shape = (1, shape[1], arr.shape[0], shape[3])
if shape[1] <= 0:
shape = (shape[0], 1, shape[2], arr.shape[1])
strides = (arr.shape[1]*arr.itemsize, arr.itemsize,
arr.shape[1]*arr.itemsize, arr.itemsize)
return as_strided(arr, shape=shape, strides=strides)
def cell_neighbors(arr, i, j, d):
"""Return d-th neighbors of cell (i, j)"""
w = sliding_window(arr, 2*d+1)
ix = np.clip(i - d, 0, w.shape[0]-1)
jx = np.clip(j - d, 0, w.shape[1]-1)
i0 = max(0, i - d - ix)
j0 = max(0, j - d - jx)
i1 = w.shape[2] - max(0, d - i + ix)
j1 = w.shape[3] - max(0, d - j + jx)
return w[ix, jx][i0:i1,j0:j1].ravel()
x = np.arange(8*8).reshape(8, 8)
print x
for d in [1, 2]:
for p in [(0,0), (0,1), (6,6), (8,8)]:
print "-- d=%d, %r" % (d, p)
print cell_neighbors(x, p[0], p[1], d=d)
The problem is you continuously reassign neighbours to a 1D array with length 8. Instead you should assign the neighbour data to a slice of the array you had already created:
for i in range(1, ran_x-1):
for j in range(1, ran_y-1):
neighbours[i-1,j-1,:] = [matrix[i-1,j-1],matrix[i-1,j],matrix[i-1,j+1],matrix[i,j-1],matrix[i,j+1],matrix[i+1,j-1],matrix[i+1,j],matrix[i+1,j+1]]
Note that I changed the ranges so you don't need the if statements. Your code would be faster and (arguably) neater as the following:
neighbours = np.empty((ran_x-2, ran_y-2, 8), int)
# bool array to extract outer ring from a 3x3 array:
b = np.array([[1,1,1],[1,0,1],[1,1,1]], bool)
for i in range(ran_x-2):
for j in range(ran_y-2):
neighbours[i,j,:] = matrix[i:i+3, j:j+3][b]
Of course it would be faster still to immediately print the neighbours without storing them at all, if that's all you need.

Categories