Coverage for tests/test_Matrix3d.py: 16%
82 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 03:12 -0700
1# This file is part of sphgeom.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This software is dual licensed under the GNU General Public License and also
10# under a 3-clause BSD license. Recipients may choose which of these licenses
11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12# respectively. If you choose the GPL option then the following text applies
13# (but note that there is still no warranty even if you opt for BSD instead):
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
28import pickle
29import unittest
31from lsst.sphgeom import Matrix3d, Vector3d
34class Matrix3dTestCase(unittest.TestCase):
35 """Test 3D Matrix."""
37 def testConstruction(self):
38 self.assertEqual(Matrix3d().getSquaredNorm(), 0)
39 self.assertEqual(Matrix3d(1, 0, 0, 0, 1, 0, 0, 0, 1), Matrix3d(1))
41 def testComparison(self):
42 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
43 self.assertEqual(m, m)
44 self.assertNotEqual(m, Matrix3d(2))
46 def testIter(self):
47 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
48 r = 0
49 for row in m:
50 self.assertEqual(row, Vector3d(*range(r * 3 + 1, r * 3 + 4)))
51 r += 1
53 def testAccess(self):
54 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
55 self.assertEqual(len(m), 3)
56 self.assertEqual(m[0], Vector3d(1, 2, 3))
57 self.assertEqual(m[1], Vector3d(4, 5, 6))
58 self.assertEqual(m[2], Vector3d(7, 8, 9))
59 self.assertEqual(m[0], m.getRow(0))
60 self.assertEqual(m[1], m.getRow(1))
61 self.assertEqual(m[2], m.getRow(2))
62 n = m.transpose()
63 self.assertEqual(m.getColumn(0), n[0])
64 self.assertEqual(m.getColumn(1), n[1])
65 self.assertEqual(m.getColumn(2), n[2])
66 for r in range(3):
67 for c in range(3):
68 self.assertEqual(m[r, c], r * 3 + c + 1)
69 for i in (-4, 3):
70 with self.assertRaises(IndexError):
71 m.getRow(i)
72 with self.assertRaises(IndexError):
73 m.getColumn(i)
74 for i in [-4, 3, (-4, 1), (3, 1), (1, -4), (1, 3)]:
75 with self.assertRaises(IndexError):
76 m[i]
78 def testInner(self):
79 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
80 i = Matrix3d(1)
81 self.assertEqual(m.inner(i), i.inner(m))
82 self.assertEqual(m.inner(i), 15.0)
84 def testNorm(self):
85 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 0, 2)
86 self.assertEqual(m.getSquaredNorm(), 144.0)
87 self.assertEqual(m.getNorm(), 12.0)
89 def testArithmetic(self):
90 v = Vector3d(1, 2, 3)
91 m = Matrix3d(1, -1, 0, 1, 1, 0, 0, 0, 1)
92 n = Matrix3d(1, 1, 0, -1, 1, 0, 0, 0, 1)
93 self.assertEqual(n * (m * v), Vector3d(2, 4, 3))
94 self.assertEqual(m + m, m * Matrix3d(2))
95 self.assertEqual(m, m * Matrix3d(2) - m)
97 def testCwiseProduct(self):
98 m = Matrix3d(1, 2, 3, 4, 1, 6, 7, 8, 1)
99 self.assertEqual(m.cwiseProduct(Matrix3d(2)), Matrix3d(2))
101 def testTranspose(self):
102 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
103 n = Matrix3d(1, 4, 7, 2, 5, 8, 3, 6, 9)
104 self.assertEqual(m.transpose(), n)
105 self.assertEqual(m.transpose().transpose(), m)
107 def testInverse(self):
108 m = Matrix3d(4, 4, 4, -1, 1, 0, 1, -1, -1)
109 n = Matrix3d(0.125, 0, 0.5, 0.125, 1, 0.5, 0, -1, -1)
110 i = m.inverse()
111 self.assertEqual(i, n)
112 self.assertEqual(i.inverse(), m)
113 self.assertEqual(i * m, Matrix3d(1))
114 self.assertEqual(m * i, Matrix3d(1))
116 def testString(self):
117 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
118 self.assertEqual(str(m), "[[1.0, 2.0, 3.0],\n [4.0, 5.0, 6.0],\n [7.0, 8.0, 9.0]]")
119 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)")
120 self.assertEqual(m, eval(repr(m), {"Matrix3d": Matrix3d}))
122 def testPickle(self):
123 m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9)
124 n = pickle.loads(pickle.dumps(m))
125 self.assertEqual(m, n)
128if __name__ == "__main__":
129 unittest.main()