Coverage for tests/test_Matrix3d.py: 18%
84 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-12 20:31 -0700
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-12 20:31 -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):
30 def testConstruction(self):
31 self.assertEqual(Matrix3d().getSquaredNorm(), 0)
32 self.assertEqual(Matrix3d(1, 0, 0, 0, 1, 0, 0, 0, 1), Matrix3d(1))
34 def testComparison(self):
35 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
36 self.assertEqual(m, m)
37 self.assertNotEqual(m, Matrix3d(2))
39 def testIter(self):
40 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
41 r = 0
42 for row in m:
43 self.assertEqual(row, Vector3d(*range(r * 3 + 1, r * 3 + 4)))
44 r += 1
46 def testAccess(self):
47 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
48 self.assertEqual(len(m), 3)
49 self.assertEqual(m[0], Vector3d(1, 2, 3))
50 self.assertEqual(m[1], Vector3d(4, 5, 6))
51 self.assertEqual(m[2], Vector3d(7, 8, 9))
52 self.assertEqual(m[0], m.getRow(0))
53 self.assertEqual(m[1], m.getRow(1))
54 self.assertEqual(m[2], m.getRow(2))
55 n = m.transpose()
56 self.assertEqual(m.getColumn(0), n[0])
57 self.assertEqual(m.getColumn(1), n[1])
58 self.assertEqual(m.getColumn(2), n[2])
59 for r in range(3):
60 for c in range(3):
61 self.assertEqual(m[r, c], r * 3 + c + 1)
62 for i in (-4, 3):
63 with self.assertRaises(IndexError):
64 m.getRow(i)
65 with self.assertRaises(IndexError):
66 m.getColumn(i)
67 for i in [-4, 3, (-4, 1), (3, 1), (1, -4), (1, 3)]:
68 with self.assertRaises(IndexError):
69 m[i]
71 def testInner(self):
72 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
73 i = Matrix3d(1)
74 self.assertEqual(m.inner(i), i.inner(m))
75 self.assertEqual(m.inner(i), 15.0)
77 def testNorm(self):
78 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 0, 2)
79 self.assertEqual(m.getSquaredNorm(), 144.0)
80 self.assertEqual(m.getNorm(), 12.0)
82 def testArithmetic(self):
83 v = Vector3d(1, 2, 3)
84 m = Matrix3d(1, -1, 0, 1, 1, 0, 0, 0, 1)
85 n = Matrix3d(1, 1, 0, -1, 1, 0, 0, 0, 1)
86 self.assertEqual(n * (m * v), Vector3d(2, 4, 3))
87 self.assertEqual(m + m, m * Matrix3d(2))
88 self.assertEqual(m, m * Matrix3d(2) - m)
90 def testCwiseProduct(self):
91 m = Matrix3d(1, 2, 3, 4, 1, 6, 7, 8, 1)
92 self.assertEqual(m.cwiseProduct(Matrix3d(2)), Matrix3d(2))
94 def testTranspose(self):
95 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
96 n = Matrix3d(1, 4, 7, 2, 5, 8, 3, 6, 9)
97 self.assertEqual(m.transpose(), n)
98 self.assertEqual(m.transpose().transpose(), m)
100 def testInverse(self):
101 m = Matrix3d(4, 4, 4, -1, 1, 0, 1, -1, -1)
102 n = Matrix3d(0.125, 0, 0.5, 0.125, 1, 0.5, 0, -1, -1)
103 i = m.inverse()
104 self.assertEqual(i, n)
105 self.assertEqual(i.inverse(), m)
106 self.assertEqual(i * m, Matrix3d(1))
107 self.assertEqual(m * i, Matrix3d(1))
109 def testString(self):
110 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
111 self.assertEqual(str(m), "[[1.0, 2.0, 3.0],\n [4.0, 5.0, 6.0],\n [7.0, 8.0, 9.0]]")
112 self.assertEqual(repr(m), "Matrix3d(1.0, 2.0, 3.0,\n 4.0, 5.0, 6.0,\n 7.0, 8.0, 9.0)")
113 self.assertEqual(m, eval(repr(m), dict(Matrix3d=Matrix3d)))
115 def testPickle(self):
116 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
117 n = pickle.loads(pickle.dumps(m))
118 self.assertEqual(m, n)
121if __name__ == "__main__": 121 ↛ 122line 121 didn't jump to line 122, because the condition on line 121 was never true
122 unittest.main()