Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Last active August 29, 2015 14:08
Show Gist options
  • Save cocodrips/7b9ed7793588f616ccd7 to your computer and use it in GitHub Desktop.
Save cocodrips/7b9ed7793588f616ccd7 to your computer and use it in GitHub Desktop.
numpyでwalk
import numpy as np
"""
o-o-o-o-o
"""
m = np.matrix([
[1, 1, 0, 0, 0],
[1, 1, 1, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 1],
[0, 0, 0, 1, 1]
])
print m * m
print pow(m, 4)
"""
1-2-3
| |/
4 5-6
"""
m = np.matrix([
[1, 1, 0, 1, 0, 0],
[1, 1, 1, 0, 1, 0],
[0, 1, 1, 0, 1, 0],
[1, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 1],
])
print m * m
print pow(m, 4)
"""
Random graph
"""
import random
N = 600
array = [[random.randint(0, 1) for _ in xrange(N)] for _ in xrange(N)]
m = np.matrix(array)
print pow(m, 1000)
@cocodrips
Copy link
Author

Result

[[2 2 1 0 0]
 [2 3 2 1 0]
 [1 2 3 2 1]
 [0 1 2 3 2]
 [0 0 1 2 2]]
[[ 9 12  9  4  1]
 [12 18 16 10  4]
 [ 9 16 19 16  9]
 [ 4 10 16 18 12]
 [ 1  4  9 12  9]]
[[3 2 1 2 1 0]
 [2 4 3 1 3 1]
 [1 3 3 0 3 1]
 [2 1 0 2 0 0]
 [1 3 3 0 4 2]
 [0 1 1 0 2 2]]
[[19 22 15 12 16  5]
 [22 40 33 10 37 15]
 [15 33 29  5 33 14]
 [12 10  5  9  5  1]
 [16 37 33  5 39 18]
 [ 5 15 14  1 18 10]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment