1 """
2 Toolbox for handling projections onto linear varieties.
3
4 @author: Christophe Alexandre <ch.alexandre at bluewin dot ch>
5
6 @license:
7 Copyright(C) 2010 Christophe Alexandre
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
21 """
22 import logging
23
24 import linalg
25 import util
26
27 _h = util.NullHandler()
28 _logger = logging.getLogger('vecspace')
29 _logger.addHandler(_h)
30
32 """
33 Essentially a factory for geometrical objects.
34
35 Joining an origin to a linear variety would create a VectorSpace.
36 """
37 - def __init__(self, dimension, basis=None):
38 self.dimension = dimension
39 self.origin = linalg.zero(dimension)
40
43
46
51
53 """
54 Straight line in a n dim space given 2 points.
55 """
56 sl = StraightLine(self.dimension)
57 sl.init_points(p0, p1)
58 return sl
59
61 return SubVectorSpace(self.dimension, a)
62
64 """
65 Specific Vector space in 3D.
66 """
67
70
72 """
73 Plane in a 3D space given 3 points.
74 """
75 return self.define_hyper_plane(p1, p2, p3)
76
78 """
79 A linear variety (or linear manifold or affine subspace)
80 of a vector space V is a subset closed under affine combinations
81 of vectors in the space.
82
83 See U{http://en.wikipedia.org/wiki/Affine_space}.
84
85 In the words of mathematical physicist John Baez, "An affine space is a vector
86 space that's forgotten its origin".
87 """
88
90 """
91 """
92 self.dimension = space_dimension
93 self.variety_dimension = 0
94 self.points = []
95
97 self.points = points
98 self.variety_dimension = len(points)
99
101 """ Dimension of containing space.
102 """
103 return self.dimension
104
106 """ Dimension of linear variety.
107 """
108 return self.variety_dimension
109
111 """
112 Generalizing projection onto induced subspace.
113 """
114 vectors = []
115 for i in range(1, len(self.points)):
116 vectors.append(self.points[i].sub(self.points[0]))
117 subspace = VectorSubspace(*vectors)
118 proj = subspace.project(point.sub(self.points[0]))
119 proj.projected = proj.projected.add(self.points[0])
120 return proj
121
123 out = str(self.points)
124 return out
125
127 """
128 Defines a set on which we can project a point.
129 """
130
132 """
133 Defining a n-subspace generated by n points.
134 """
135 self.points = points
136
138 """
139 The solution implies (x - x*) perpendicular to each y[i]
140 with x* = sum( alpha[i] * y[i] )
141 and y[i]: points generating the subspace.
142 """
143 space_dim = point.get_length()
144 subspace_dim = len(self.points)
145 m = linalg.Matrix(subspace_dim)
146 for row in range(subspace_dim):
147 for column in range(row, subspace_dim):
148 value = self.points[row].product(self.points[column])
149 m.set_value(row, column, value)
150 if column != row:
151 m.set_value(column, row, value)
152 b = linalg.Vector(subspace_dim)
153 for row in range(subspace_dim):
154 value = point.product(self.points[row])
155 b.set_component(row, value)
156 alphas = util.system_solve(m.get_table(), b.get_data())
157 result = linalg.zero(space_dim)
158 for i in range(len(alphas)):
159 component = self.points[i].scale(alphas[i])
160 result = result.add(component)
161 return Projection(result, point)
162
164 """
165 Simple container gathering details about projection.
166 """
168 self.projected = projected
169 self.start = start
170 self.projector = projected.sub(start)
171
173 """
174 Linear variety of dimension 1.
175
176 It is defined by a (n-1) linear equations or 2 points.
177 """
183
185 """
186 Linear variety of dimension (n-1).
187
188 It is defined by a single linear equation.
189 """
190
196