80_linalg.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from linalg import mat3x3, vec2
  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 copy
  41. test_mat_copy = test_mat.copy()
  42. assert test_mat is not test_mat_copy
  43. element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
  44. for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]):
  45. assert [getattr(test_mat, e) for e in element_name_list][i] == element
  46. # test setzeros
  47. test_mat_copy = test_mat.copy()
  48. test_mat_copy.set_zeros()
  49. assert test_mat_copy == mat3x3([[0,0,0],[0,0,0],[0,0,0]])
  50. # test set_ones
  51. test_mat_copy = test_mat.copy()
  52. test_mat_copy.set_ones()
  53. assert test_mat_copy == mat3x3([[1,1,1],[1,1,1],[1,1,1]])
  54. # test set_identity
  55. test_mat_copy = test_mat.copy()
  56. test_mat_copy.set_identity()
  57. assert test_mat_copy == mat3x3([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
  58. # test __getitem__
  59. element_name_list = [e for e in dir(test_mat) if e[:2] != '__' and e[0] == '_']
  60. for i, element in enumerate([getattr(test_mat, e) for e in element_name_list]):
  61. assert test_mat.__getitem__((int(i/3), i%3)) == element
  62. # test __setitem__
  63. test_mat_copy = test_mat.copy()
  64. element_name_list = [e for e in dir(test_mat_copy) if e[:2] != '__' and e[0] == '_']
  65. for i, element in enumerate([getattr(test_mat_copy, e) for e in element_name_list]):
  66. test_mat_copy.__setitem__((int(i/3), i%3), list(range(9))[i])
  67. assert test_mat_copy == mat3x3([[0,1,2], [3,4,5], [6,7,8]])
  68. # test __add__
  69. test_mat_copy = test_mat.copy()
  70. ones = mat3x3()
  71. ones.set_ones()
  72. result_mat = test_mat_copy.__add__(ones)
  73. correct_result_mat = test_mat_copy.copy()
  74. for i in range(3):
  75. for j in range(3):
  76. correct_result_mat[i, j] += 1
  77. assert result_mat == correct_result_mat
  78. # test __sub__
  79. test_mat_copy = test_mat.copy()
  80. ones = mat3x3()
  81. ones.set_ones()
  82. result_mat = test_mat_copy.__sub__(ones)
  83. correct_result_mat = test_mat_copy.copy()
  84. for i in range(3):
  85. for j in range(3):
  86. correct_result_mat[i, j] -= 1
  87. assert result_mat == correct_result_mat
  88. # test __mul__
  89. test_mat_copy = test_mat.copy()
  90. result_mat = test_mat_copy.__mul__(12.345)
  91. correct_result_mat = test_mat_copy.copy()
  92. for i in range(3):
  93. for j in range(3):
  94. correct_result_mat[i, j] *= 12.345
  95. # print(result_mat)
  96. # print(correct_result_mat)
  97. assert result_mat == correct_result_mat
  98. # test matmul
  99. test_mat_copy = test_mat.copy()
  100. test_mat_copy_2 = test_mat.copy()
  101. result_mat = test_mat_copy.matmul(test_mat_copy_2)
  102. correct_result_mat = mat3x3()
  103. for i in range(3):
  104. for j in range(3):
  105. 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))])
  106. assert result_mat == correct_result_mat
  107. # test determinant
  108. test_mat_copy = test_mat.copy()
  109. for i in range(3):
  110. for j in range(3):
  111. test_mat_copy[i, j] = test_mat[j, i]