ulvis.paste.net

Paste Search Dynamic
matrix
1. from numpy import *
2. import numpy as np
3. print('This program only for 3x3 matrix')
4. A = np.array([[0,3,0],[0,0,3],[2,2,2]])
5. print('Matrix :')
6. for i in range(3):
7.     print(A[i])
8.
9.
10. def prob_generate(t):
11.     nonzero = 0
12.     for i in range(3):
13.         probability[i][0] = t & 1
14.         if(probability[i][0]):
15.             nonzero = nonzero + 1
16.         t = t>>1
17.
18.     return nonzero
19.
20. def to_test():
21.     max_exp = 0;
22.     flag = false;
23.     for i in range(3):
24.         if exp_val[i][0] > max_exp:
25.             max_exp = exp_val[i][0]
26.             flag = bool(probability[i][0])
27.
28.     return flag
29.
30. def prob_cal():
31.     mtrx = A.copy()
32.     for i in range(3):
33.         if probability[i][0] == 0:
34.             mtrx = np.delete(mtrx,i,axis = 0)
35.             mtrx = np.delete(mtrx,i,axis = 1)
36.
37.     y1 = mtrx[0][0] - mtrx[1][0]
38.     y2 = mtrx[1][1] - mtrx[0][1]
39.
40.     if y1*y2 > 0:
41.         i = 0
42.         while i<3:
43.             if probability[i][0] == 0:
44.                 i = i+1
45.                 continue
46.             probability[i][0] = abs(y2)
47.             i = i+1
48.             probability[i][0] = abs(y1)
49.             i = i+1
50.     else:
51.         probability[0][0] = probability[1][0] = probability[2][0] = 0
52.
53.
54.
55. probability = np.array([[0],[0],[0]])
56. for i in range(1,9):
57.     nonzero = prob_generate(i)
58.     if nonzero == 1:
59.         exp_val = mat(A) * mat(probability)
60.         flag = to_test()
61.     elif nonzero == 2:
62.         prob_cal()
63.         exp_val = mat(A) * mat(probability)
64.         flag = to_test()
65.     elif nonzero == 3:
66.         flag = false
67.         exp_val = mat(A) * mat(probability)
68.         if (exp_val[0][0] == exp_val[1][0]) & (exp_val[1][0] == exp_val[2][0]):
69.             flag = true
70.
71.     if flag:
72.         m = 0;
73.         for i in range(3):
74.             m += probability[i]
75.         print('Probability : ',probability[0][0]/m,probability[1][0]/m,probability[2][0]/m)
76.
Parsed in 0.016 seconds