test_matrix.py
Go to the documentation of this file.
1 #!/usr/env python
2 
3 import unittest, sys, os
4 sys.path.insert(0, os.path.abspath(".."))
5 from gnsstk.test_utils import args, assertSequenceAlmostEqual, run_unit_tests, vec_to_list
6 
7 import gnsstk
8 import math
9 
10 # TODO: need additional tests for:
11 # * rowRef, colRef, row, col tests
12 # * Operators +=, -=, *=, /=, and unary negation
13 # * ident, trace, normF, normCol, slowDet
14 
15 
16 def assertMatricesEqual(test_case, a, b):
17  """
18  Naming scheme attempts to match assertions names from unittest module.
19  """
20  # Check if the number of columns and rows are equal
21  test_case.assertEqual(a.rows(), b.rows())
22  test_case.assertEqual(a.cols(), b.cols())
23 
24  # Check if each element equals each other
25  for i in range(a.rows()):
26  for j in range(a.cols()):
27  test_case.assertEqual(a.get_value(i, j), b.get_value(i, j))
28 
29 
30 
31 class TestMatrix(unittest.TestCase):
32 
33  def test_constructor(self):
34  # Matrix Constructor tests
35  a = gnsstk.Matrix_double()
36  self.assertEqual(0, a.size())
37  self.assertTrue(a.empty())
38  self.assertEqual(0, a.rows())
39  self.assertEqual(0, a.cols())
40 
41  # Constructor with rows/columns
42  a = gnsstk.Matrix_int(34, 67)
43  self.assertEqual(2278, a.size())
44  self.assertFalse(a.empty())
45  self.assertEqual(34, a.rows())
46  self.assertEqual(67, a.cols())
47 
48  # Constructor with rows/columns and a default fill value
49  a = gnsstk.Matrix_int(34, 67, 5)
50  self.assertEqual(2278, a.size())
51  self.assertFalse(a.empty())
52  self.assertEqual(34, a.rows())
53  self.assertEqual(67, a.cols())
54  self.assertEqual(5, a.get_value(23, 34))
55 
56  def test_accessors(self):
57  # Testing accessors of size, num rows, num cols, empty, and max_size
58  a = gnsstk.Matrix_double()
59  self.assertTrue(a.empty())
60 
61  a = gnsstk.Matrix_double(12, 23, 5)
62  self.assertFalse(a.empty())
63  self.assertEqual(12, a.rows())
64  self.assertEqual(23, a.cols())
65  self.assertEqual(276, a.size())
66  self.assertEqual(276, a.max_size())
67 
68  def test_resize(self):
69  a = gnsstk.Matrix_double(12, 34, 5)
70  self.assertEqual(12, a.rows())
71  self.assertEqual(34, a.cols())
72  self.assertEqual(408, a.size())
73 
74  # This "works" but all of the data is scrapped in the resize due to the implementation of Vector.resize()
75  _ = a.resize(34, 56)
76  self.assertEqual(34, a.rows())
77  self.assertEqual(56, a.cols())
78  self.assertEqual(1904, a.size())
79 
80  # Resize and replace all values with given default value
81  _ = a.resize(1, 1, 13)
82  self.assertEqual(1, a.rows())
83  self.assertEqual(1, a.cols())
84  self.assertEqual(1, a.size())
85  self.assertEqual(13, a.get_value(0, 0))
86 
87  def test_zeroize(self):
88  # Zeroize results depends on the value of RefVectorBaseHelper::zeroTolerance
89  # Zeroize will set a value to zero if it is less than RefVectorBaseHelper::zeroTolerance
90 
91  # Attempt to zeroize, but nothing is less than RefVectorBaseHelper::zeroTolerance
92  a = gnsstk.Matrix_double(12, 34, 10**-9)
93  a.zeroizeRow(7)
94  assertSequenceAlmostEqual(self, [10**-9] * 34, vec_to_list(a[7]))
95 
96  # Zeroize and all values in row are below RefVectorBaseHelper::zeroTolerance
97  a = gnsstk.Matrix_double(12, 34, 10**-11)
98  a.zeroizeRow(7)
99  self.assertSequenceEqual([0] * 34, vec_to_list(a[7]))
100 
101  a = gnsstk.Matrix_double(12, 34, 10**-11)
102  a.zeroizeCol(30)
103  self.assertSequenceEqual([0] * 12, vec_to_list(gnsstk.MatrixColSlice_double(a, 30)))
104 
105  a = gnsstk.Matrix_double(12, 34, 10**-11)
106  a.zeroize()
107  ref = gnsstk.Matrix_double(12, 34, 0)
108  assertMatricesEqual(self, ref, a)
109 
111  # TODO: should these tests be expanded on, or is it sufficient to simply exercise the swig calls?
112  a = gnsstk.Matrix_double(45, 45, 1)
113  b = gnsstk.Matrix_double(45, 33, 0)
114  c = gnsstk.Matrix_double(45, 45, 0)
115 
116  self.assertTrue(a.isSquare())
117  self.assertFalse(b.isSquare())
118  self.assertTrue(c.isSquare())
119 
120  self.assertFalse(a.isUT())
121  self.assertFalse(b.isUT())
122  self.assertTrue(c.isUT())
123 
124  self.assertFalse(a.isLT())
125  self.assertFalse(b.isLT())
126  self.assertTrue(c.isLT())
127 
128  self.assertFalse(a.isDiagonal())
129  self.assertFalse(b.isDiagonal())
130  self.assertTrue(c.isDiagonal())
131 
132  self.assertTrue(a.isSymmetric())
133  self.assertFalse(b.isSymmetric())
134  self.assertTrue(c.isSymmetric())
135 
136  def test_slice_copy(self):
137  # ColCopy, rowCopy, diagCopy
138  a = gnsstk.Matrix_double(34, 12, 1)
139  vec1 = a.colCopy(5, 23)
140  self.assertEqual(11, vec1.size())
141 
142  vec2 = a.rowCopy(33, 7)
143  self.assertEqual(5, vec2.size())
144 
145  vec3 = a.diagCopy()
146  self.assertEqual(12, vec3.size())
147 
148  def test_swap_rows(self):
149  a = gnsstk.Matrix_double(34, 34)
150 
151  # Setting values of matrix to their row number
152  for i in range(a.rows()):
153  for j in range(a.cols()):
154  a[i][j] = i
155 
156  # Asserting current state before swapping rows
157  self.assertSequenceEqual([1] * 34, vec_to_list(a[1]))
158  self.assertSequenceEqual([20] * 34, vec_to_list(a[20]))
159 
160  a.swapRows(1, 20)
161 
162  # Rows should be swapped
163  self.assertSequenceEqual([20] * 34, vec_to_list(a[1]))
164  self.assertSequenceEqual([1] * 34, vec_to_list(a[20]))
165 
166  def test_swap_cols(self):
167  a = gnsstk.Matrix_double(34, 34)
168 
169  # Settings values of matrix to their col number for e
170  for i in range(a.rows()):
171  for j in range(a.cols()):
172  a[i][j] = j
173 
174  # Asserting current state before swapping cols
175  self.assertSequenceEqual([1] * 34, vec_to_list(gnsstk.MatrixColSlice_double(a, 1)))
176  self.assertSequenceEqual([20] * 34, vec_to_list(gnsstk.MatrixColSlice_double(a, 20)))
177 
178  a.swapCols(1, 20)
179 
180  # Cols should be swapped
181  self.assertSequenceEqual([20] * 34, vec_to_list(gnsstk.MatrixColSlice_double(a, 1)))
182  self.assertSequenceEqual([1] * 34, vec_to_list(gnsstk.MatrixColSlice_double(a, 20)))
183 
185  a = gnsstk.Matrix_int(5, 5)
186  for i in range(a.rows()):
187  for j in range(a.cols()):
188  a[i][j] = i + j
189 
190  a.swapRows(0, 4)
191  a.swapCols(1, 2)
192 
193  self.assertSequenceEqual([6, 3, 4, 5, 2], vec_to_list(gnsstk.MatrixColSlice_int(a, 1)))
194  self.assertSequenceEqual([0, 2, 1, 3, 4], vec_to_list(a[4]))
195 
196  def test_assign_from(self):
197  # AssignFrom
198  a = gnsstk.Matrix_double(5, 5)
199  b = gnsstk.Matrix_double(5, 5, 10)
200  ref = gnsstk.Matrix_double(5, 5, 10)
201  a.assignFromMatrix(b)
202 
203  assertMatricesEqual(self, a, ref)
204 
205  # a and b should not be linked in any way. Changing b should not result in a change of a
206  b[2][3] = -3
207  assertMatricesEqual(self, a, ref)
208 
209  # The vector represents all elements of the matrix. i.e. it is a flattened matrix
210  a = gnsstk.Matrix_int(5, 5, 0)
211  ref = gnsstk.Matrix_int(5, 5, -78)
212  v = gnsstk.Vector_int(25, -78)
213 
214  a.assignFromVector(v)
215  v[2] = 30 # A change to `v` should not propogate to `a`
216  assertMatricesEqual(self, a, ref)
217 
218  # This test is the only place that array_double is used, and the
219  # array_double declarations cause downstream errors and have been
220  # commented out. See gnsstk.i
221  # double_array = gnsstk.array_double(25)
222  # for i in range(25):
223  # double_array[i] = 4
224  # a.assignFrom(double_array) # AssignFrom array
225  # actual.append(matrices_are_equal(a, gnsstk.Matrix_double(5, 5, 4)))
226 
227  a = gnsstk.Matrix_int(5, 5, 0)
228  ref = gnsstk.Matrix_int(5, 5, 42)
229 
230  a.assignFrom(42)
231  assertMatricesEqual(self, a, ref)
232 
233 
234 if __name__ == "__main__":
tests.test_matrix.TestMatrix.test_swap_cols
def test_swap_cols(self)
Definition: test_matrix.py:166
tests.test_matrix.TestMatrix.test_swap_rows_and_cols
def test_swap_rows_and_cols(self)
Definition: test_matrix.py:184
tests.test_matrix.TestMatrix.test_accessors
def test_accessors(self)
Definition: test_matrix.py:56
tests.test_matrix.TestMatrix.test_assign_from
def test_assign_from(self)
Definition: test_matrix.py:196
test_utils.vec_to_list
def vec_to_list(s)
Definition: test_utils.py:88
tests.test_matrix.TestMatrix.test_matrix_properties
def test_matrix_properties(self)
Definition: test_matrix.py:110
test_utils.assertSequenceAlmostEqual
def assertSequenceAlmostEqual(test_case, l1, l2, **kwargs)
Definition: test_utils.py:76
tests.test_matrix.TestMatrix.test_swap_rows
def test_swap_rows(self)
Definition: test_matrix.py:148
tests.test_matrix.TestMatrix.test_resize
def test_resize(self)
Definition: test_matrix.py:68
tests.test_matrix.TestMatrix.test_slice_copy
def test_slice_copy(self)
Definition: test_matrix.py:136
tests.test_matrix.assertMatricesEqual
def assertMatricesEqual(test_case, a, b)
Definition: test_matrix.py:16
tests.test_matrix.TestMatrix
Definition: test_matrix.py:31
gnsstk::range
double range(const Position &A, const Position &B)
Definition: Position.cpp:1273
tests.test_matrix.TestMatrix.test_zeroize
def test_zeroize(self)
Definition: test_matrix.py:87
tests.test_matrix.TestMatrix.test_constructor
def test_constructor(self)
Definition: test_matrix.py:33
test_utils.run_unit_tests
def run_unit_tests()
Definition: test_utils.py:51


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:41