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