standard_fit.py
Go to the documentation of this file.
1 #!/usr/bin/env nosetests -vs
2 # coding=utf-8
3 """
4 Linear algebra standard fitting module
5
6 (C) 2013 hashnote.net, Alisue
7 """
8 __author__ = 'Alisue (lambdalisue@hashnote.net)'
9 __version__ = '0.1.0'
10 __date__ = '2013-10-28'
11 __all__ = ['standard_fit', 'projection', 'distance', 'function']
12
13 import numpy as np
14
15 def standard_fit(X):
16  """
17  Find (n - 1) dimensional standard (e.g. line in 2 dimension, plane in 3
18  dimension, hyperplane in n dimension) via solving Singular Value
19  Decomposition.
20
21  The idea was explained in the following references
22
23  - http://www.caves.org/section/commelect/DUSI/openmag/pdf/SphereFitting.pdf
24  - http://www.geometrictools.com/Documentation/LeastSquaresFitting.pdf
25  - http://www.ime.unicamp.br/~marianar/MI602/material%20extra/svd-regression-analysis.pdf
26  - http://www.ling.ohio-state.edu/~kbaker/pubs/Singular_Value_Decomposition_Tutorial.pdf
27
28  Example:
29  >>> XY = [[0, 1], [3, 3]]
30  >>> XY = np.array(XY)
31  >>> C, N = standard_fit(XY)
32  >>> C
33  array([ 1.5, 2. ])
34  >>> N
35  array([-0.5547002 , 0.83205029])
36
37  Args:
38  X: n x m dimensional matrix which n indicate the number of the dimension
39  and m indicate the number of points
40
41  Returns:
42  [C, N] where C is a centroid vector and N is a normal vector
43  """
44  # Find the average of points (centroid) along the columns
45  C = np.average(X, axis=0)
46  # Create CX vector (centroid to point) matrix
47  CX = X - C
48  # Singular value decomposition
49  U, S, V = np.linalg.svd(CX)
50  # The last row of V matrix indicate the eigenvectors of
51  # smallest eigenvalues (singular values).
52  N = V[-1]
53  return C, N
54
55 def projection(x, C, N):
56  """
57  Create orthogonal projection matrix of x on the plane
58
59  Args:
60  x: n x m dimensional matrix
61  C: n dimensional vector whicn indicate the centroid of the standard
62  N: n dimensional vector which indicate the normal vector of the standard
63
64  Returns:
65  n x m dimensional matrix which indicate the orthogonal projection points
66  on the plane
67  """
68  rows, cols = x.shape
69  NN = np.tile(N, (rows, 1))
70  D = distance(x, C, N)
71  DD = np.tile(D, (cols, 1)).T
72  return x - DD * NN
73
74 def distance(x, C, N):
75  """
76  Calculate an orthogonal distance between the points and the standard
77
78  Args:
79  x: n x m dimensional matrix
80  C: n dimensional vector whicn indicate the centroid of the standard
81  N: n dimensional vector which indicate the normal vector of the standard
82
83  Returns:
84  m dimensional vector which indicate the orthogonal disntace. the value
85  will be negative if the points beside opposite side of the normal vector
86  """
87  return np.dot(x-C, N)
88
89 def function(x, C, N):
90  """
91  Calculate an orthogonal projection of the points on the standard
92
93  Args:
94  x: (n-1) x m dimensional matrix
95  C: n dimensional vector whicn indicate the centroid of the standard
96  N: n dimensional vector which indicate the normal vector of the standard
97
98  Returns:
99  m dimensional vector which indicate the last attribute value of
100  orthogonal projection
101  """
102  Ck = C[0:-1] # centroid for known parameters
103  Nk = N[0:-1] # normal for known parmeters
104  Cu = C[-1] # centroid for unknown parameter
105  Nu = N[-1] # normal for unknown parameter
106  return np.dot(x-Ck, Nk) * -1.0 / Nu + Cu
107
108 #===============================================================================
109 #
110 # Unittest
111 #
112 #===============================================================================
113 if __name__ == '__main__':
114  import doctest; doctest.testmod()
def projection(x, C, N)
Definition: standard_fit.py:55
def function(x, C, N)
Definition: standard_fit.py:89
def standard_fit(X)
Definition: standard_fit.py:15
def distance(x, C, N)
Definition: standard_fit.py:74

fiducial_slam
Author(s): Jim Vaughan
autogenerated on Sat Jun 13 2020 04:04:52