20 import math, numpy, sys
21 import general_python
as genpy
24 def __init__(self, t = 0.0, x = 0.0, v = None, a = None):
31 s =
"Scalar point:" +
'\n'
32 s +=
"t = " + str(self.
t) +
'\n'
33 s +=
"x = " + str(self.
x) +
'\n'
34 s +=
"v = " + str(self.
v) +
'\n'
35 s +=
"a = " + str(self.
a) +
'\n'
59 m = m + p.count_constraints()
62 A = numpy.zeros((m,m))
69 A[i, j] = A[i, j-1]*p.t
71 u = numpy.append(u, p.x)
77 A[i, j] = j*(p.t**(j-1))
79 u = numpy.append(u, p.v)
85 A[i, j] = j*(j-1)*(p.t**(j-2))
87 u = numpy.append(u, p.a)
90 self.
coeff = numpy.dot(numpy.linalg.inv(A), u)
92 print genpy.err_str(__name__, self.__class__.__name__, sys._getframe().f_code.co_name,
'The matrix of coefficients is singular')
93 print "Matrix of coefficients:"
96 print "Determinent = ", numpy.linalg.det(A)
102 Generates the coefficients of a spline passing through key positions and velocities specified by u and v at corresponding t
103 t, u, v must have the same length
104 if v is not specified (keep the default), only positions are considered
113 A = numpy.zeros((n,n))
116 for j
in range(1, n):
117 A[i, j] = A[i, j-1]*t[i]
119 self.
coeff = numpy.dot(numpy.linalg.inv(A), u)
122 A = numpy.zeros((2*n, 2*n))
127 for j
in range(1, 2*n):
128 A[i, j] = A[i, j-1]*t[i]
129 A[n + i, j] = j*(t[i]**(j-1))
131 self.
coeff = numpy.dot(numpy.linalg.inv(A), numpy.append(u, v))
133 print "Error: Size of input vector v is different from t and u"
137 Returns the value of the polynomial at time t
142 s = s + self.
coeff[i]*(t**i)
148 Returns the derivative of the polynomial at time t
155 for i
in range(1, n):
156 v = v + (i+1)*self.
coeff[i+1]*(t**i)
161 Returns the derivative of the polynomial at time t
168 for i
in range(1, n):
169 a = a + (i+1)*(i+2)*self.
coeff[i+2]*(t**i)
174 A polynomial of degree three in the form:
178 the coefficients are: a, b
180 each coefficient can be n-element vector or matrix (or a multi-dimensional numpy array)
188 def find_coefficients(self, total_time, start_position, end_position,start_velocity, end_velocity):
190 returns two coefficients of a linear polynomial which generates position as a function of time according to the given boundary conditions as
191 the coefficients are: a, b
192 property "total_time" indicates the total time of motion
193 each coefficient is a n-element vector (numpy array)
197 the output of the polynomial has the same structure of the coefficients
201 n = len(start_position)
205 assert len(end_position) == n
207 self.
b = start_position
208 self.
a = (end_position - start_position) / total_time
212 return "f(t)" at time: "t"
217 coefficients a, b can be any vector or multi-dimensional numpy array
218 the output has the same structure of the coefficients
222 pos = self.
a*t + self.
b
228 A polynomial of degree three in the form:
230 f(t) = a * t^2 + b * t + c
232 the coefficients are: a, b, c
234 each coefficient can be n-element vector or matrix (or a multi-dimensional numpy array)
243 def find_coefficients(self, total_time, start_position, end_position,start_velocity, end_velocity):
245 returns two coefficients of a linear polynomial which generates position as a function of time according to the given boundary conditions as
246 the coefficients are: a, b, c
247 property "total_time" indicates the total time of motion
248 each coefficient is a n-element vector (numpy array)
250 f(t) = a * t^2 + b * t + c
252 the output of the polynomial has the same structure of the coefficients
256 n = len(start_position)
260 assert len(end_position) == n
262 self.
c = numpy.copy(start_position)
263 self.
b = numpy.copy(start_velocity)
264 self.
a = (end_position - self.
c - self.
b*total_time)/(total_time**2)
268 return "f(t)" at time: "t"
271 f(t) = a * t^2 + b * t + c
273 coefficients a, b can be any vector or multi-dimensional numpy array
274 the output has the same structure of the coefficients
278 pos = self.
a*(t**2) + self.
b*t + self.
c
285 A polynomial of degree three in the form:
287 f(t) = a * t^3 + b * t^2 + c * t + d
289 the coefficients are: a, b, c, d
291 each coefficient can be n-element vector or matrix (or a multi-dimensional numpy array)
301 def find_coefficients(self, total_time, start_position, end_position, start_velocity, end_velocity):
303 returns four coefficients of a polynomial of third degree which generates position as a function of time according to the given boundary conditions as
304 the coefficients are: a, b, c, d
305 property "total_time" indicates the total time of motion
306 each coefficient is a n-element vector (numpy array)
308 f(t) = a * t^3 + b * t^2 + c * t + d
310 the output of the polynomial has the same structure of the coefficients
314 n = len(start_position)
318 assert len(end_position) == n
319 assert len(start_velocity) == n
320 assert len(end_velocity) == n
322 self.
d = start_position
323 self.
c = start_velocity
325 self.
a = numpy.zeros(n)
326 self.
b = numpy.zeros(n)
330 T = numpy.array([[total_time*t2, t2],[3*t2, 2*total_time]])
331 Tinv = numpy.linalg.inv(T)
335 B[0] = end_position[j] - self.
c[j]*total_time - self.
d[j]
336 B[1] = end_velocity[j] - self.
c[j]
338 X = numpy.dot(Tinv,B)
346 return "f(t)" at time: "t"
349 f(t) = a * t^3 + b * t^2 + c * t + d
351 coefficients a, b, c, d are given via "coeff" and can be any vector or multi-dimensional numpy array
352 the output has the same structure of the coefficients
356 pos = self.
a*t*t2 + self.
b*t2 + self.
c*t + self.
d
362 return the derivateve of "f(t)" at time: "t"
365 f(t) = a * t^3 + b * t^2 + c * t + d
366 f'(t) = 3 * a * t^2 + 2 * b * t + c
368 coefficients a, b, c, d are given via "coeff" and can be any vector or multi-dimensional numpy array
369 the output has the same structure of the coefficients. (Obviously coefficient "d" is not used)
372 vel = 3*self.a*t*t + 2*self.b*t + self.c
377 def interpolate_polynomial(A,X,Y):
380 #This function is not complete#
382 A : numpy array of n+1 elements
383 X : numpy array of m elements
384 Y : numpy array of m elements
387 This function assigns coefficients a_0 to a_n so that polynomial defined as:
389 f(x) = a_0 + a_1*x + a_2*x^2 + a_3*x^3 + ... + a_n*x^n
391 fits through points (x_1,y_1) , (x_2,y_2) , ... , (x_m,y_m)
393 The procedures follows three different algorythms in three different possible states:
397 It is not possible to define n degree polynomial to exactly pass through m points,
398 but it is possible to calculate coefficients of n degree polynomial with a minimum
399 sum of squares of distances between the function and the given points.
403 J = Sigma_{i=0}^{m}{(y_i-f(x_i))^2}
407 A very special case is n=1 when this function returns coefficients of regression line
411 The problem has only one solution. Only one n degree polynomial can be defined passing exactly through
412 the given points. This function returns the coefficients of this function.
416 Unlimited number of functions can be found to pass exactly through the given points. This
417 function selects only one of these solutions so that sum of squares of its coefficients is minimized.
421 J = Sigma_{i=0}^{n}{a_i^2}