Package pod :: Module vecspace
[frames] | no frames]

Source Code for Module pod.vecspace

  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   
31 -class VectorSpace(object):
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
41 - def get_dimension(self):
42 return self.dimension
43
44 - def define_point(self, *coordinates):
45 return linalg.Point(*coordinates)
46
47 - def define_hyper_plane(self, *points):
48 hp = HyperPlane(self.dimension) 49 hp.init_points(*points) 50 return hp
51
52 - def define_line(self, p0, p1):
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
60 - def define_subspace(self, a):
61 return SubVectorSpace(self.dimension, a)
62
63 -class VectorSpace3D(VectorSpace):
64 """ 65 Specific Vector space in 3D. 66 """ 67
68 - def __init__(self):
69 VectorSpace.__init__(self, 3)
70
71 - def define_plane(self, p1, p2, p3):
72 """ 73 Plane in a 3D space given 3 points. 74 """ 75 return self.define_hyper_plane(p1, p2, p3)
76
77 -class LinearVariety(object):
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
89 - def __init__(self, space_dimension):
90 """ 91 """ 92 self.dimension = space_dimension 93 self.variety_dimension = 0 94 self.points = []
95
96 - def init_points(self, *points):
97 self.points = points 98 self.variety_dimension = len(points)
99
100 - def get_dimension(self):
101 """ Dimension of containing space. 102 """ 103 return self.dimension
104
105 - def get_variety_dimension(self):
106 """ Dimension of linear variety. 107 """ 108 return self.variety_dimension
109
110 - def project(self, point):
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
122 - def __repr__(self):
123 out = str(self.points) 124 return out
125
126 -class VectorSubspace(object):
127 """ 128 Defines a set on which we can project a point. 129 """ 130
131 - def __init__(self, *points):
132 """ 133 Defining a n-subspace generated by n points. 134 """ 135 self.points = points
136
137 - def project(self, point):
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
163 -class Projection(object):
164 """ 165 Simple container gathering details about projection. 166 """
167 - def __init__(self, projected, start):
168 self.projected = projected 169 self.start = start 170 self.projector = projected.sub(start)
171
172 -class StraightLine(LinearVariety):
173 """ 174 Linear variety of dimension 1. 175 176 It is defined by a (n-1) linear equations or 2 points. 177 """
178 - def __init__(self, dim):
179 """ 180 @param dim: space dimension 181 """ 182 LinearVariety.__init__(self, dim)
183
184 -class HyperPlane(LinearVariety):
185 """ 186 Linear variety of dimension (n-1). 187 188 It is defined by a single linear equation. 189 """ 190
191 - def __init__(self, dim):
192 """ 193 @param dim: space dimension 194 """ 195 LinearVariety.__init__(self, dim)
196