24 lines
No EOL
642 B
Python
24 lines
No EOL
642 B
Python
import numpy as np
|
|
|
|
def converge(v,p):
|
|
i = 0
|
|
while (np.max(np.abs(v - np.mean(v))) > 1):
|
|
v = np.matmul(p, v)
|
|
i += 1
|
|
return i
|
|
|
|
def create_vector(max):
|
|
np.random.seed(0) # For reproducibility
|
|
vector = np.random.randint(0, 1000, size=(max, 1)) # Random integers
|
|
while np.any(np.abs(np.diff(vector.flatten())) < 100): # Ensure the difference is at least 100
|
|
vector = np.random.randint(0, 1000, size=(max, 1))
|
|
return vector
|
|
|
|
v = create_vector(3)
|
|
p = np.array([
|
|
[0.500000, 0.250000, 0.250000],
|
|
[0.250000, 0.375000, 0.375000],
|
|
[0.000000, 0.250000, 0.750000]
|
|
])
|
|
|
|
print(converge(v,p)) |