80_linalg.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. from linalg import mat3x3, vec2, vec3
  2. import random
  3. import sys
  4. def mat_round(mat, pos):
  5. '''
  6. 对mat的副本的每一个元素执行round(element, pos),返回副本
  7. 用于校对元素是浮点数的矩阵
  8. '''
  9. ret = mat.copy()
  10. for i, row in enumerate(ret):
  11. for j, element in enumerate(row):
  12. row[j] = round(element, pos)
  13. ret[i] = row
  14. return ret
  15. def get_row(mat, row_index):
  16. '''
  17. 返回mat的row_index行元素构成的列表
  18. '''
  19. ret = []
  20. for i in range(3):
  21. ret.append(mat[row_index, i])
  22. return ret
  23. def get_col(mat, col_index):
  24. '''
  25. 返回mat的col_index列元素构成的列表
  26. '''
  27. ret = []
  28. for i in range(3):
  29. ret.append(mat[i, col_index])
  30. return ret
  31. # 生成随机测试目标
  32. min_num = -10.0
  33. max_num = 10.0
  34. test_mat = mat3x3([[random.uniform(min_num, max_num) for _ in range(3)] for _ in range(3)])
  35. # test_mat = mat3x3([
  36. # [1, 2, 3],
  37. # [4, 5, 6],
  38. # [7, 8, 9]]
  39. # )
  40. # test incorrect number of parameters is passed
  41. for i in range(20):
  42. if i in [0, 9]:
  43. continue
  44. try:
  45. test_mat_copy = mat3x3(*tuple([e+0.1 for e in range(i)]))
  46. # 既然参数数量不是合法的0个或9个,并且这里也没有触发TypeError,那么引发测试失败
  47. print(f'When there are {i} arguments, no TypeError is triggered')
  48. exit(1)
  49. except TypeError:
  50. pass
  51. # test 9 floating parameters is passed
  52. test_mat_copy = test_mat.copy()
  53. element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
  54. element_value_list = [getattr(test_mat, attr) for attr in element_name_list]
  55. assert mat3x3(*tuple(element_value_list)) == test_mat
  56. # test copy
  57. test_mat_copy = test_mat.copy()
  58. assert test_mat is not test_mat_copy
  59. element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
  60. for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]):
  61. assert [getattr(test_mat, e) for e in element_name_list][i] == element
  62. # test setzeros
  63. test_mat_copy = test_mat.copy()
  64. test_mat_copy.set_zeros()
  65. assert test_mat_copy == mat3x3([[0,0,0],[0,0,0],[0,0,0]])
  66. # test set_ones
  67. test_mat_copy = test_mat.copy()
  68. test_mat_copy.set_ones()
  69. assert test_mat_copy == mat3x3([[1,1,1],[1,1,1],[1,1,1]])
  70. # test set_identity
  71. test_mat_copy = test_mat.copy()
  72. test_mat_copy.set_identity()
  73. assert test_mat_copy == mat3x3([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
  74. # test __getitem__
  75. element_name_list = [e for e in dir(test_mat) if e[:2] != '__' and e[0] == '_']
  76. for i, element in enumerate([getattr(test_mat, e) for e in element_name_list]):
  77. assert test_mat.__getitem__((int(i/3), i%3)) == element
  78. # test __setitem__
  79. test_mat_copy = test_mat.copy()
  80. element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
  81. for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]):
  82. test_mat_copy.__setitem__((int(i/3), i%3), list(range(9))[i])
  83. assert test_mat_copy == mat3x3([[0,1,2], [3,4,5], [6,7,8]])
  84. # test __add__
  85. test_mat_copy = test_mat.copy()
  86. ones = mat3x3()
  87. ones.set_ones()
  88. result_mat = test_mat_copy.__add__(ones)
  89. correct_result_mat = test_mat_copy.copy()
  90. for i in range(3):
  91. for j in range(3):
  92. correct_result_mat[i, j] += 1
  93. assert result_mat == correct_result_mat
  94. # test __sub__
  95. test_mat_copy = test_mat.copy()
  96. ones = mat3x3()
  97. ones.set_ones()
  98. result_mat = test_mat_copy.__sub__(ones)
  99. correct_result_mat = test_mat_copy.copy()
  100. for i in range(3):
  101. for j in range(3):
  102. correct_result_mat[i, j] -= 1
  103. assert result_mat == correct_result_mat
  104. # test __mul__
  105. test_mat_copy = test_mat.copy()
  106. result_mat = test_mat_copy.__mul__(12.345)
  107. correct_result_mat = test_mat_copy.copy()
  108. for i in range(3):
  109. for j in range(3):
  110. correct_result_mat[i, j] *= 12.345
  111. # print(result_mat)
  112. # print(correct_result_mat)
  113. assert result_mat == correct_result_mat
  114. # test matmul
  115. test_mat_copy = test_mat.copy()
  116. test_mat_copy_2 = test_mat.copy()
  117. result_mat = test_mat_copy.matmul(test_mat_copy_2)
  118. correct_result_mat = mat3x3()
  119. for i in range(3):
  120. for j in range(3):
  121. correct_result_mat[i, j] = sum([e1*e2 for e1, e2 in zip(get_row(test_mat_copy, i), get_col(test_mat_copy_2, j))])
  122. assert result_mat == correct_result_mat
  123. # test determinant
  124. test_mat_copy = test_mat.copy()
  125. for i in range(3):
  126. for j in range(3):
  127. test_mat_copy[i, j] = test_mat[j, i]
  128. # # test __repr__
  129. # test_mat_copy = test_mat.copy()
  130. # print(test_mat_copy[0,0])
  131. # assert test_mat_copy.__repr__() == f'mat3x3([[{test_mat_copy[0,0].round(4)}, {test_mat_copy[1,0].round(4)}, {test_mat_copy[2,0].round(4)}],\n [{test_mat_copy[0,1].round(4)}, {test_mat_copy[1,1].round(4)}, {test_mat_copy[2,1].round(4)}],\n [{test_mat_copy[0,2].round(4)}, {test_mat_copy[1,2].round(4)}, {test_mat_copy[2,2].round(4)}]])'