16 import sys, math, numpy
as np
18 import general_python
as genpy
19 from math_tools
import general_math
as gen
21 import matplotlib.pyplot
as plt
25 returns the maximums of each column in a vector
32 cm.append(max(M[:,j]))
38 returns the minimums of each column in a vector
45 cm.append(min(M[:,j]))
49 def plot_matrix(M, xlabel = None, ylabels = None, annotate = False, legend = True, annx = None, anny = None):
52 if annotate
and (annx ==
None):
57 max_y = max(
colmax(M[:,1:n]))
58 min_y = min(
colmin(M[:,1:n]))
67 print 'Annotate ', i, ylabels[j] ,
' at: ', annx[j], anny[j]
69 for j
in range(n - 1):
70 plt.plot(M[:,0], M[:,j+1])
72 plt.annotate(ylabels[j], xy=(annx[j], anny[j]), xytext=(annx[j]+0.1*hx, anny[j]+0.1*hy), arrowprops=dict(facecolor=
'black', shrink=0.2), )
73 plt.plot(annx, anny,
'x', color =
'black')
76 I should work on that ...
91 returns a numpy vector of length n containing a (all elements of the vector will be a)
100 gets vector v with n^2 elements and returns n*n matrix
103 n = int(math.sqrt(n_2))
107 print "n*i + j = ", n*i + j
108 print "R = ", v, n_2, n, R
115 if int(value) == value :
118 return str(format%value)
122 parametrized usage of sprintf
125 for v
in list_of_vals :
131 previous name: format_vector
132 parametrized usage of sprintf
135 formatted = np.zeros((n))
138 formatted[i] = format%vector[i]
139 return str(formatted)
143 parametrized usage of sprintf
147 formatted = np.zeros((m,n))
151 formatted[i,j] = format%matrix[i,j]
152 return str(formatted)
156 Returns the positions of those elements of vector v which satisfy the given condition with the given value
157 condition is a string and must be in: '>', '>=', '<', '=='
161 for i
in range(len(v)):
163 flag = (v[i] < value)
164 elif condition ==
'>':
165 flag = (v[i] > value)
166 elif condition ==
'==':
167 flag = (v[i] == value)
168 elif condition ==
'<=':
169 flag = (v[i] <= value)
170 elif condition ==
'>=':
171 flag = (v[i] >= value)
173 assert False,
"Error from vectors_and_matrices.which(): "+ condition+
" is an unknown condition."
181 Removes the items from v whose positions are specified in given array "positions" and returns the filtered vector
184 for i
in range(len(v)):
185 if not (i
in positions):
191 equivalent to np.dot( A, np.diag(v) )
193 multiplies each column of matrix A by the corresponding element in vector v.
194 This is equivalent to A*diag(v) but requires less calculations
196 return np.dot( A, np.diag(v) )
201 Adiagv = np.zeros((m,n))
204 Adiagv[i,j] = A[i,j]*v[j]
209 Returns the unit vector parallel to the given vector.
211 l = np.linalg.norm(v)
212 if gen.equal(l, 0.0):
219 return the linear mapping of vector q by coefficients f and g
223 This code should be implemented before:
225 for i in range(0,len(f[i])):
226 if abs(f[i]) < mgvlib.epsilon:
227 print 'Error 01: Division by zero occured'
228 print 'Something is wrong with the joint limits or the joint limit multipliers'
229 print 'Make sure that the joint limits are well defined, and method "initialize" of the manipulator configuration has been implemented'
238 returns the inner product of the two vectors v1 and v2
244 Returns a numpy array equal to v. Input argument v must be a normal array of numbers
248 def vectors_angle(v1, v2, in_degrees = False, positive_direction = np.zeros(3)):
250 Returns the angle between two vectors. The positive direction, specifies which halfspace is used as positive vectors for the cross product of u and v
251 if not specified, the returned angle will be always positive.
252 If specified, then if the sign of the returned angle is the same as the inner product of positive_direction and crossproduct(v1,v2)
255 l_v1 = np.linalg.norm(v1)
256 l_v2 = np.linalg.norm(v2)
257 assert not general.equal(l_v1, 0.0)
258 assert not general.equal(l_v2, 0.0)
262 theta = trigonometry.arccos(cos_theta)
263 if general.equal(np.linalg.norm(positive_direction), 0.0):
266 return math.copysign(theta,
inner_product(np.cross(v1, v2), positive_direction))
270 return the inverse linear mapping of vector u by coefficients f and g
279 return an square diagonal matrix whose diagonal elements are elements of vector v
282 return np.diag(v, k = 0 )
287 elementwise multiplication of two vectors.
289 !!! http://www.scipy.org/NumPy_for_Matlab_Users !!!
296 returns a three element vector containing the first three elements of v4
302 get a three element vector and add one element to its end. Return a four element vector
310 def equal(v1,v2, epsilon = gen.epsilon):
312 Returns 1 if two vectors or matrices are equal otherwise returns 0
314 return (np.linalg.norm(v1-v2) < epsilon)
318 uvect is a shorted phrase of "unit vector"
320 Returns a vector (3 X 1) containing the first three elements of the m-th column of transformation matrix TRM.
321 (TRM is the 4*4 transformation matrix or a 3X3 rotation matrix)
322 If m is in [0,1,2] the m-th unit vector is returned. If m is 3, then the function returns the position vector
328 Returns the rotation matrix (3 X 3) extracted from transformation matrix TRM. TRM is a 4*4 matrix
330 return TRM[ 0:3, 0:3 ]
334 Return the cross product of two vectors u and v. u and v are (3 element) vectors
340 Convert a (3 X 3) rotation matrix into a (4 X 4) transformation matrix. The added elements will be zero
352 Return the right pseudo-inverse of matrix J
354 --> take a look at ! np.linalg.pinv(a) !
357 Jinv = np.dot(J.T,np.linalg.inv(A))
362 Return the left pseudo-inverse of matrix J
366 Jinv = np.dot(np.linalg.inv(A), J.T)
371 returns the right side damped least square inverse of matrix M. k is the damping factor:
373 M^T * [M*M^T + (k^2)*I]^(-1)
379 Minv = np.dot(M.T, np.linalg.inv(A + k*k*np.eye(m)))
384 returns the left side damped least square inverse of matrix M. k is the damping factor:
386 [M^T*M + (k^2)*I]^(-1) * M^T
388 --> take a look at ! np.linalg.pinv(a, rcond=1.0000000000000001e-15) !
393 Minv = np.dot(np.linalg.inv(A + k*k*np.eye(m)), M.T)
397 return np.dot(np.dot(W,M.T),np.linalg.inv(np.dot(np.dot(M,W),M.T)))
404 return np.dot(np.dot(W,M.T),np.linalg.inv(k*k*np.eye(m) + np.dot(np.dot(M,W),M.T)))
409 if the magnitude(norm) of the given vector is smaller than max_norm, the given vctor is returned
410 otherwise a vector parallel to v with norm max_norm is returned
412 l = np.linalg.norm(v)
423 while permit
and (i < n):
424 permit = permit
and (v[i] >= 0)
and (
not (non_zero
and gen.equal(v[i], 0.0)))
430 when the correction of vector x is restricted by limits
431 If you want to change vector x in a desired direction, and there are lower and upper bounds for x,
432 how much are you allowed to change?
433 This function returns a feasible stepsize for the given direction.
434 The direction of change must be multiplied by a scalar value lower or equal to this feasible stepsize
435 so that the applied changes are feasible.
437 valtyp = [np.ndarray, list, tuple]
438 direction = genpy.check_type(direction, valtyp, __name__, function_name = sys._getframe().f_code.co_name, var_name =
'direction', shape_length = 1)
440 [x, x_min, x_max] = genpy.check_types(variables = [x,x_min,x_max], type_lists = [valtyp,valtyp,valtyp],
441 file_path = __name__, function_name = sys._getframe().f_code.co_name,
442 var_names = [
'x',
'x_min',
'x_max'], shape_lengths = [1,1,1], array_lengths = [n,n,n])
446 if not gen.equal(direction[i], 0.0, epsilon = 0.000000001):
452 a = (x_min[i] - x[i])/direction[i]
453 b = (x_max[i] - x[i])/direction[i]
454 etta.append(gen.sign_choice(b, a, direction[i]))
458 if etta[len(etta)-1] < 0:
459 print 'x_min: ', x_min
460 print 'x_max: ', x_max
462 print 'dir: ', direction
472 for i
in range(n - 1):
473 s += vectarray[i + 1]
478 for i
in range(n - 1):
479 vectarray[i] = vectarray[i+1]
480 vectarray[n - 1] = v_new