Coverage for tests/test_Matrix3d.py: 18%
84 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-02 18:10 -0700
1#
2# LSST Data Management System
3# See COPYRIGHT file at the top of the source tree.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
23import pickle
24import unittest
26from lsst.sphgeom import Matrix3d, Vector3d
29class Matrix3dTestCase(unittest.TestCase):
31 def testConstruction(self):
32 self.assertEqual(Matrix3d().getSquaredNorm(), 0)
33 self.assertEqual(Matrix3d(1, 0, 0,
34 0, 1, 0,
35 0, 0, 1),
36 Matrix3d(1))
38 def testComparison(self):
39 m = Matrix3d(1, 2, 3,
40 4, 5, 6,
41 7, 8, 9)
42 self.assertEqual(m, m)
43 self.assertNotEqual(m, Matrix3d(2))
45 def testIter(self):
46 m = Matrix3d(1, 2, 3,
47 4, 5, 6,
48 7, 8, 9)
49 r = 0
50 for row in m:
51 self.assertEqual(row, Vector3d(*range(r*3 + 1, r*3 + 4)))
52 r += 1
54 def testAccess(self):
55 m = Matrix3d(1, 2, 3,
56 4, 5, 6,
57 7, 8, 9)
58 self.assertEqual(len(m), 3)
59 self.assertEqual(m[0], Vector3d(1, 2, 3))
60 self.assertEqual(m[1], Vector3d(4, 5, 6))
61 self.assertEqual(m[2], Vector3d(7, 8, 9))
62 self.assertEqual(m[0], m.getRow(0))
63 self.assertEqual(m[1], m.getRow(1))
64 self.assertEqual(m[2], m.getRow(2))
65 n = m.transpose()
66 self.assertEqual(m.getColumn(0), n[0])
67 self.assertEqual(m.getColumn(1), n[1])
68 self.assertEqual(m.getColumn(2), n[2])
69 for r in range(3):
70 for c in range(3):
71 self.assertEqual(m[r, c], r*3 + c + 1)
72 for i in (-4, 3):
73 with self.assertRaises(IndexError):
74 m.getRow(i)
75 with self.assertRaises(IndexError):
76 m.getColumn(i)
77 for i in [-4, 3, (-4, 1), (3, 1), (1, -4), (1, 3)]:
78 with self.assertRaises(IndexError):
79 m[i]
81 def testInner(self):
82 m = Matrix3d(1, 2, 3,
83 4, 5, 6,
84 7, 8, 9)
85 i = Matrix3d(1)
86 self.assertEqual(m.inner(i), i.inner(m))
87 self.assertEqual(m.inner(i), 15.0)
89 def testNorm(self):
90 m = Matrix3d(1, 2, 3,
91 4, 5, 6,
92 7, 0, 2)
93 self.assertEqual(m.getSquaredNorm(), 144.0)
94 self.assertEqual(m.getNorm(), 12.0)
96 def testArithmetic(self):
97 v = Vector3d(1, 2, 3)
98 m = Matrix3d(1, -1, 0,
99 1, 1, 0,
100 0, 0, 1)
101 n = Matrix3d(1, 1, 0,
102 -1, 1, 0,
103 0, 0, 1)
104 self.assertEqual(n * (m * v), Vector3d(2, 4, 3))
105 self.assertEqual(m + m, m * Matrix3d(2))
106 self.assertEqual(m, m * Matrix3d(2) - m)
108 def testCwiseProduct(self):
109 m = Matrix3d(1, 2, 3,
110 4, 1, 6,
111 7, 8, 1)
112 self.assertEqual(m.cwiseProduct(Matrix3d(2)), Matrix3d(2))
114 def testTranspose(self):
115 m = Matrix3d(1, 2, 3,
116 4, 5, 6,
117 7, 8, 9)
118 n = Matrix3d(1, 4, 7,
119 2, 5, 8,
120 3, 6, 9)
121 self.assertEqual(m.transpose(), n)
122 self.assertEqual(m.transpose().transpose(), m)
124 def testInverse(self):
125 m = Matrix3d(4, 4, 4,
126 -1, 1, 0,
127 1, -1, -1)
128 n = Matrix3d(0.125, 0, 0.5,
129 0.125, 1, 0.5,
130 0, -1, -1)
131 i = m.inverse()
132 self.assertEqual(i, n)
133 self.assertEqual(i.inverse(), m)
134 self.assertEqual(i * m, Matrix3d(1))
135 self.assertEqual(m * i, Matrix3d(1))
137 def testString(self):
138 m = Matrix3d(1, 2, 3,
139 4, 5, 6,
140 7, 8, 9)
141 self.assertEqual(str(m), '[[1.0, 2.0, 3.0],\n'
142 ' [4.0, 5.0, 6.0],\n'
143 ' [7.0, 8.0, 9.0]]')
144 self.assertEqual(repr(m), 'Matrix3d(1.0, 2.0, 3.0,\n'
145 ' 4.0, 5.0, 6.0,\n'
146 ' 7.0, 8.0, 9.0)')
147 self.assertEqual(m, eval(repr(m), dict(Matrix3d=Matrix3d)))
149 def testPickle(self):
150 m = Matrix3d(1, 2, 3,
151 4, 5, 6,
152 7, 8, 9)
153 n = pickle.loads(pickle.dumps(m))
154 self.assertEqual(m, n)
157if __name__ == '__main__': 157 ↛ 158line 157 didn't jump to line 158, because the condition on line 157 was never true
158 unittest.main()