def cross(X, Y, axis=1): ''' Cross product axis=1 Numpy default axis=0 MATLAB, Octave, SciLab default ''' if axis == 0: X = X.T Y = Y.T if (X.n in (2, 3)) and (Y.n in (2, 3)): if X.m == Y.m: Z = [] for k in range(min(X.m, Y.m)): z = X[k, 0] * Y[k, 1] - X[k, 1] * Y[k, 0] if (X.n == 3) and (Y.n == 3): Z.append([X[k, 1] * Y[k, 2] - X[k, 2] * Y[k, 1], X[k, 2] * Y[k, 0] - X[k, 0] * Y[k, 2], z]) else: Z.append([z]) if axis == 0: return umatrix.matrix(Z).T else: return umatrix.matrix(Z) else: raise ValueError('shape mismatch') else: raise ValueError('incompatible dimensions for cross product' ' (must be 2 or 3)')
def __init__(self, w_or_q, x=None, y=None, z=None): """ Initializes a Quaternion object :param w_or_q: A scalar representing the real part of the quaternion, another Quaternion object or a four-element array containing the quaternion values :param x: The first imaginary part if w_or_q is a scalar :param y: The second imaginary part if w_or_q is a scalar :param z: The third imaginary part if w_or_q is a scalar """ self._q = umatrix.matrix([1, 0, 0, 0], cstride=1, rstride=4, dtype=float) if x is not None and y is not None and z is not None: w = w_or_q q = umatrix.matrix([w, x, y, z], cstride=1, rstride=4, dtype=float) elif isinstance(w_or_q, Quaternion): q = umatrix.matrix(w_or_q.q, cstride=1, rstride=4, dtype=float) elif isinstance(w_or_q, umatrix.matrix): q = w_or_q else: q = umatrix.matrix(w_or_q, cstride=1, rstride=4, dtype=float) if len(q) != 4: raise ValueError("Expecting a 4-element array or w x y z as parameters") self._set_q(q)
def equality(): result = {} x10 = umatrix.matrix([[0.03, 1.2, 2.45], [4.5, 5.45, 6.98], [8, 9.0001, 10.2], [12.123, 13.45, 14.0]]) x11 = umatrix.matrix([[0.03, 1.2, 2.45], [4.5, 5.45, 6.98], [8, 9.0001, 10.2], [12.123, 13.45, 14.0]]) x12 = umatrix.matrix([[0.03, 1.2, 2.451], [4.5, 5.45, 6.98], [9, 9.0002, 10.2], [12.123, 13.45 + eps, 14.0]]) x13 = umatrix.matrix([[0.03, 1.2, 2.451], [4.5, 5.45, 6.98], [9, 9.0003, 10.2], [12.123, 13.450001, 14.0]]) result['x == y and x.__eq__(y)'] = (x10 == x11) and (x10.__eq__(x11)) result['umatrix.matrix_isclose(x, y) True'] = matrix_compare(x10, x11) result['umatrix.matrix_isclose(x, y) False'] = matrix_compare(x10, x12) == False result['umatrix.matrix_isclose(x, y, tol) False tol'] = matrix_compare( x12, x13, tol=eps / 2) == False result['umatrix.matrix_isclose(x, y, tol) True tol'] = matrix_compare( x12, x13, tol=0.001) try: result['umatrix.matrix_equal(x, y)'] = umatrix.matrix_equal( x10, x12) == False except Exception as e: result['umatrix.matrix_equal(x, y)'] = (False, e) result['umatrix.matrix_equiv(x, y) same shape'] = umatrix.matrix_equiv( x10, x11) result['umatrix.matrix_equiv(x, y.reshape) shape'] = umatrix.matrix_equiv( x10, x11.reshape((3, 4))) return result
def cross(X, Y, axis=1): """ Cross product axis=1 Numpy default axis=0 MATLAB, Octave, SciLab default """ if axis == 0: X = X.T Y = Y.T if (X.n in (2, 3)) and (Y.n in (2, 3)): if X.m == Y.m: Z = [] for k in range(min(X.m, Y.m)): z = X[k, 0] * Y[k, 1] - X[k, 1] * Y[k, 0] if (X.n == 3) and (Y.n == 3): Z.append([ X[k, 1] * Y[k, 2] - X[k, 2] * Y[k, 1], X[k, 2] * Y[k, 0] - X[k, 0] * Y[k, 2], z, ]) else: Z.append([z]) if axis == 0: return umatrix.matrix(Z).T else: return umatrix.matrix(Z) else: raise ValueError("shape mismatch") else: raise ValueError("incompatible dimensions for cross product" " (must be 2 or 3)")
def __init__(self, lbda): self.std_pos = 0.00448355705097 * 10 self.std_vel = 0.0014239412603 self.lbda = lbda moy = pi / 180.0 * umatrix.matrix([[-5.0, 4.0]]).T cho = umatrix.matrix([[self.std_pos, 0.0], [0.0, self.std_vel]]) ACapteur.__init__(self, [u"pos_mes", "vel_mes"], moy, cho)
def list_ops(): result = {} X = umatrix.matrix([[0, 1, 2], [4, 5, 6], [8, 9, 10], [12, 13, 14]]) Ylist = [1, 2, 3] Zlist = [1, 2, 3, 4] Ymatrix = umatrix.matrix([[1, 2, 3]]) Zmatrix = umatrix.matrix([[1, 2, 3, 4]]) try: result['matrix + list row defualt'] = matrix_compare( X + Ylist, umatrix.matrix([[1, 3, 5], [5, 7, 9], [9, 11, 13], [13, 15, 17]])) except Exception as e: result['matrix + list row default'] = (False, e) try: result['matrix + list row default broadcast error'] = matrix_compare( X + Zlist, umatrix.matrix([[1, 3, 5], [5, 7, 9], [9, 11, 13], [13, 15, 17]])) except Exception as e: result['matrix + list row default broadcast error'] = True try: result['matrix col + list'] = matrix_compare( X[:, 1] + Zlist, umatrix.matrix([[2, 7, 12, 17]]).T) except Exception as e: result['matrix col + list'] = (False, e) try: result['matrix col + list broadcast error'] = matrix_compare( X[:, 1] + Ylist, umatrix.matrix([[2, 7, 12, 17]]).T) except Exception as e: result['matrix col + list broadcast error'] = True try: result['matrix + row matrix'] = matrix_compare( X + Ymatrix, umatrix.matrix([[1, 3, 5], [5, 7, 9], [9, 11, 13], [13, 15, 17]])) except Exception as e: result['matrix + row matrix'] = (False, e) try: result['matrix + col matrix'] = matrix_compare( X + Zmatrix.T, umatrix.matrix([[1, 2, 3], [6, 7, 8], [11, 12, 13], [16, 17, 18]])) except Exception as e: result['matrix + col matrix'] = (False, e) try: result['matrix + col broadcast error'] = matrix_compare( X + Ymatrix.T, umatrix.matrix([[1, 3, 5], [5, 7, 9], [9, 11, 13], [13, 15, 17]])) except Exception as e: result['matrix + col broadcast error'] = True return result
def iteration(): result = {} x10 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) Z = [i for i in x10] result['iteration over matrix'] = Z == [umatrix.matrix([[0, 1, 2]]), umatrix.matrix([[4, 5, 6]]), umatrix.matrix([[8 , 9 , 10]]), umatrix.matrix([[12, 13, 14]])] Z = [i for i in x10[1,:]] result['iteration over row slice'] = Z == [4, 5, 6] Z = [i for i in x10[:,1]] result['iteration over col slice'] = Z == [1, 5, 9, 13] Z = [i for i in x10[1:3,1:2]] result['iteration over submatrix'] = Z == [5, 9] return result
def main(): # Cadence de calcul fs = 100.0 # Init système lbda = 5.0 sys = SystemeTest(lbda) sys.set_state(umatrix.matrix([[-pi / 4]]).T, 0.0) # Init Kalman A = umatrix.matrix([[exp(-lbda / fs), 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]) B = umatrix.matrix([[1.0 - exp(-lbda / fs), 0.0, 0.0]]).T C = umatrix.matrix([[1.0, 1.0, 0.0], [-lbda, 0.0, 1.0]]) D = umatrix.matrix([[0.0, lbda]]).T Q = 0 * ulinalg.eye(3) std_pos = 0.00448355705097 std_vel = 0.0014239412603 R = umatrix.matrix([[std_pos**2, 0.0], [0.0, std_vel**2]]) kal = FiltreKalman(["pos_est", "biais_pos_est", "biais_vel_est"]) kal.set_matrices(A, B, C, D, Q, R, 1.0 / fs) kal.set_state( umatrix.matrix([[0.0, 0.0, 0.0]]).T, ulinalg.eye(3) / 10.0, 0.0) c = Capteur(lbda) ctrl = Controller(1.0 / fs, sys) sim = Simulation(1.0 / fs, ctrl, sys, c, kal) sim.simule(20.0) log = sim.get_loggeur()
def update_imu(self, gyroscope, accelerometer): """ Perform one update step with data from a IMU sensor array :param gyroscope: A three-element array containing the gyroscope data in radians per second. :param accelerometer: A three-element array containing the accelerometer data. Can be any unit since a normalized value is used. """ q = self.quaternion # Normalise accelerometer measurement if ulinalg.norm(accelerometer) is 0: print("accelerometer is zero") return accelerometer = accelerometer / ulinalg.norm(accelerometer) # Gradient descent algorithm corrective step f = umatrix.matrix([ 2 * (q[1] * q[3] - q[0] * q[2]) - accelerometer[0, 0], 2 * (q[0] * q[1] + q[2] * q[3]) - accelerometer[0, 1], 2 * (0.5 - q[1]**2 - q[2]**2) - accelerometer[0, 2] ], cstride=1, rstride=1, dtype=float) j = umatrix.matrix([ -2 * q[2], 2 * q[3], -2 * q[0], 2 * q[1], 2 * q[1], 2 * q[0], 2 * q[3], 2 * q[2], 0, -4 * q[1], -4 * q[2], 0 ], cstride=1, rstride=4, dtype=float) step = ulinalg.dot(j.transpose(), f) step = step / ulinalg.norm(step) # normalise step magnitude # Compute rate of change of quaternion qdot = (q * Quaternion(0, gyroscope[0, 0], gyroscope[0, 1], gyroscope[0, 2])) * 0.5 - Quaternion( step.T * self.beta) # Integrate to yield quaternion q = q + qdot * (self.samplePeriod) self.quaternion = q * (1 / ulinalg.norm(q._q)) # normalise quaternion
def dot(X, Y): """ Dot product """ if X.size(2) == Y.size(1): Z = [] for k in range(X.size(1)): for j in range(Y.size(2)): Z.append(sum([X[k, i] * Y[i, j] for i in range(Y.size(1))])) return umatrix.matrix(Z, cstride=1, rstride=Y.size(2)) else: raise ValueError("shapes not aligned")
def dot(X, Y): ''' Dot product ''' if X.size(2) == Y.size(1): Z = [] for k in range(X.size(1)): for j in range(Y.size(2)): Z.append(sum([X[k, i] * Y[i, j] for i in range(Y.size(1))])) return umatrix.matrix(Z, cstride=1, rstride=Y.size(2)) else: raise ValueError('shapes not aligned')
def slicing(): result = {} x10 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) result['extract single element'] = x10[1,2] == 6 result['extract a row'] = matrix_compare(x10[1,:], umatrix.matrix([[4, 5, 6]])) result['extract a col'] = matrix_compare(x10[:,1], umatrix.matrix([1, 5, 9, 13], cstride=1, rstride=1)) result['extract rows'] = matrix_compare(x10[1:4,:], umatrix.matrix([[4, 5, 6],[8, 9, 10],[12, 13, 14]])) result['extract columns'] = matrix_compare(x10[:,1:3], umatrix.matrix([[1, 2],[5, 6],[9, 10],[13, 14]])) result['extract sub'] = matrix_compare(x10[1:3,1:3], umatrix.matrix([[5, 6],[9, 10]])) result['extract row from transpose'] = matrix_compare(x10.T[0], umatrix.matrix([[0, 4, 8, 12]])) result['extract col from transpose'] = matrix_compare(x10.T[:,1], umatrix.matrix([4, 5, 6], cstride=1, rstride=1)) result['extract sub from transpose'] = matrix_compare(x10.T[1:3,1:3], umatrix.matrix([[5, 9],[6, 10]])) return result
def element_wise(): result = {} X = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10]]) try: result['matrix - matrix'] = matrix_compare(X-X, umatrix.matrix([[0,0,0],[0,0,0],[0,0,0]])) except Exception as e: result['matrix + list row default'] = (False, e) try: result['matrix - matrix view'] = matrix_compare(X-X.T, umatrix.matrix([[0,-3,-6],[3,0,-3],[6,3,0]])) except Exception as e: result['matrix + list row default'] = (False, e) try: result['matrix view - matrix'] = matrix_compare(X.T-X, umatrix.matrix([[0,-3,-6],[3,0,-3],[6,3,0]]).T) except Exception as e: result['matrix + list row default'] = (False, e) return result
def equality(): result = {} x10 = umatrix.matrix([[0.03,1.2,2.45],[4.5,5.45,6.98],[8,9.0001,10.2],[12.123,13.45,14.0]]) x11 = umatrix.matrix([[0.03,1.2,2.45],[4.5,5.45,6.98],[8,9.0001,10.2],[12.123,13.45,14.0]]) x12 = umatrix.matrix([[0.03,1.2,2.451],[4.5,5.45,6.98],[9,9.0002,10.2],[12.123,13.45+eps,14.0]]) x13 = umatrix.matrix([[0.03,1.2,2.451],[4.5,5.45,6.98],[9,9.0003,10.2],[12.123,13.450001,14.0]]) result['x == y and x.__eq__(y)'] = (x10 == x11) and (x10.__eq__(x11)) result['umatrix.matrix_isclose(x, y) True'] = matrix_compare(x10, x11) result['umatrix.matrix_isclose(x, y) False'] = matrix_compare(x10, x12) == False result['umatrix.matrix_isclose(x, y, tol) False tol'] = matrix_compare(x12, x13, tol=eps/2) == False result['umatrix.matrix_isclose(x, y, tol) True tol'] = matrix_compare(x12, x13, tol=0.001) try: result['umatrix.matrix_equal(x, y)'] = umatrix.matrix_equal(x10, x12) == False except Exception as e: result['umatrix.matrix_equal(x, y)'] = (False, e) result['umatrix.matrix_equiv(x, y) same shape'] = umatrix.matrix_equiv(x10, x11) result['umatrix.matrix_equiv(x, y.reshape) shape'] = umatrix.matrix_equiv(x10, x11.reshape((3,4))) return result
def det_inv_test(): result = {} # first test g = umatrix.matrix([[0., 0., 0., 0., 0., 1.], [0.03125, 0.0625, 0.125, 0.25, 0.5, 1.], [0., 0., 0., 0., 1., 0.], [0.3125, 0.5, 0.75, 1., 1., 0.], [0., 0., 0., 2., 0., 0.], [2.5, 3., 3., 2., 0., 0.]]) g_inv = umatrix.matrix([[-192., 192., -48., -48., -4., 4.], [240., -240., 64., 56., 6., -4.], [-80., 80., -24., -16., -3., 1.], [0., 0., 0., 0., 0.5, 0.], [0., 0., 1., 0., 0., 0.], [1., 0., 0., 0., 0., 0.]]) (det, inv) = ulinalg.det_inv(g) result['determinant 1'] = (abs(det - 0.0078125) < 0.000001) result['inverse 1'] = umatrix.matrix_equal(inv, g_inv, 0.000001) # second test x = umatrix.matrix([[3., 2., 0., 1.], [4., 0., 1., 2.], [3., 0., 2., 1.], [9., 2., 3., 1.]]) (det, inv) = ulinalg.det_inv(x) det_res = det == 24.0 f = umatrix.matrix([[-0.25, 0.25, -0.5, 0.25], [ 0.66666666666666667, -0.49999999999999999, 0.50000000000000001, -0.16666666666666667 ], [ 0.16666666666666667, -0.49999999999999999, 1.00000000000000002, -0.16666666666666667 ], [ 0.41666666666666667, 0.25, 0.50000000000000001, -0.41666666666666667 ]]) result['inverse 2'] = matrix_compare(inv, f, tol=eps * 2) #, tol=0.000000000000001) f[3, 3] = -0.416668 result['determinant 2'] = det_res result['matrix_equal True'] = umatrix.matrix_equal(inv, f, tol=0.0001) result['matrix_equal False'] = umatrix.matrix_equal(inv, f) == False x1 = umatrix.matrix([[0.71, -0.71, 0.7], [0.71, 0.71, 0.5], [0, 0, 1]]) z = ulinalg.pinv(x1) result['psuedo inverse'] = matrix_compare( z, umatrix.matrix([ [0.7042253521126759, 0.704225352112676, -0.8450704225352109], [-0.704225352112676, 0.704225352112676, 0.1408450704225352], [1.110223024625157e-16, 5.551115123125783e-17, 0.9999999999999998] ]), tol=2 * eps) return result
def list_ops(): result = {} X = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) Ylist = [1,2,3] Zlist = [1,2,3,4] Ymatrix = umatrix.matrix([[1,2,3]]) Zmatrix = umatrix.matrix([[1,2,3,4]]) try: result['matrix + list row defualt'] = matrix_compare(X+Ylist, umatrix.matrix([[1,3,5],[5,7,9],[9,11,13],[13,15,17]])) except Exception as e: result['matrix + list row default'] = (False, e) try: result['matrix + list row default broadcast error'] = matrix_compare(X+Zlist, umatrix.matrix([[1,3,5],[5,7,9],[9,11,13],[13,15,17]])) except Exception as e: result['matrix + list row default broadcast error'] = True try: result['matrix col + list'] = matrix_compare(X[:,1]+Zlist, umatrix.matrix([[2,7,12,17]]).T) except Exception as e: result['matrix col + list'] = (False, e) try: result['matrix col + list broadcast error'] = matrix_compare(X[:,1]+Ylist, umatrix.matrix([[2,7,12,17]]).T) except Exception as e: result['matrix col + list broadcast error'] = True try: result['matrix + row matrix'] = matrix_compare(X+Ymatrix, umatrix.matrix([[1,3,5],[5,7,9],[9,11,13],[13,15,17]])) except Exception as e: result['matrix + row matrix'] = (False, e) try: result['matrix + col matrix'] = matrix_compare(X+Zmatrix.T, umatrix.matrix([[1,2,3],[6,7,8],[11,12,13],[16,17,18]])) except Exception as e: result['matrix + col matrix'] = (False, e) try: result['matrix + col broadcast error'] = matrix_compare(X+Ymatrix.T, umatrix.matrix([[1,3,5],[5,7,9],[9,11,13],[13,15,17]])) except Exception as e: result['matrix + col broadcast error'] = True return result
def mesure(self, x, u, t): z = self.comportement(x, u, t) n = len(self._name_of_mes) n2 = int((n + 1) / 2) * 2 # Vecteur de nombre aleatoires uniformes entre 0 et 1 vu = [urandom.getrandbits(8 * 4) / float(2 ** 32)] * n2 for i in range(n2 / 2): vu[2 * i + 0], vu[2 * i + 1] = ( sqrt(-log(vu[2 * i + 0])) * cos(2 * pi * vu[2 * i + 1]), sqrt(-log(vu[2 * i + 0])) * sin(2 * pi * vu[2 * i + 1]), ) bn = umatrix.matrix([vu[:n]]).T bg = self._cho * bn + self._moy self._mes = z + bg
def construct(): result = {} x11 = umatrix.matrix([[0, 1, 2], [4, 5, 6], [8, 9, 10], [12, 13, 14]]) x10 = umatrix.matrix([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]) result['square test 1'] = x10.is_square result['square test 2'] = not x11.is_square result['transpose column vector'] = matrix_compare( umatrix.matrix([1, 2, 3, 4], cstride=1, rstride=1).T, umatrix.matrix([[1, 2, 3, 4]])) result['transpose test square'] = matrix_compare( x10.transpose(), umatrix.matrix([[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]])) result['transpose property'] = matrix_compare( x10.T, umatrix.matrix([[0, 4, 8, 12], [1, 5, 9, 13], [2, 6, 10, 14], [3, 7, 11, 15]])) # check for shape change view x12 = x11 result['shape'] = x11.shape == (4, 3) x11.shape = (3, 4) result['shape change'] = matrix_compare( x11, umatrix.matrix([[0, 1, 2, 4], [5, 6, 8, 9], [10, 12, 13, 14] ])) and (x11 == x12) x11 = umatrix.matrix([[0, 1, 2], [4, 5, 6], [8, 9, 10], [12, 13, 14]]) # check for shape change copy x12 = x11.reshape((3, 4)) result['shape copy'] = matrix_compare( x12, umatrix.matrix([[0, 1, 2, 4], [5, 6, 8, 9], [10, 12, 13, 14] ])) and (x12.shape != x11.shape) return result
def construct(): result = {} x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) result['square test 1'] = x10.is_square result['square test 2'] = not x11.is_square result['transpose column vector'] = matrix_compare(umatrix.matrix([1,2,3,4], cstride=1, rstride=1).T, umatrix.matrix([[1,2,3,4]])) result['transpose test square'] = matrix_compare(x10.transpose(), umatrix.matrix([[0, 4, 8, 12],[1, 5, 9, 13],[2, 6, 10, 14],[3, 7, 11, 15]])) result['transpose property'] = matrix_compare(x10.T, umatrix.matrix([[0, 4, 8, 12],[1, 5, 9, 13],[2, 6, 10, 14],[3, 7, 11, 15]])) # check for shape change view x12 = x11 result['shape'] = x11.shape == (4, 3) x11.shape=(3, 4) result['shape change'] = matrix_compare(x11, umatrix.matrix([[0,1,2,4],[5,6,8,9],[10,12,13,14]])) and (x11 == x12) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) # check for shape change copy x12 = x11.reshape((3,4)) result['shape copy'] = matrix_compare(x12, umatrix.matrix([[0,1,2,4],[5,6,8,9],[10,12,13,14]])) and (x12.shape != x11.shape) return result
def det_inv_test(): result = {} # first test g = umatrix.matrix([[ 0. , 0. , 0. , 0. , 0. , 1. ], [ 0.03125, 0.0625 , 0.125 , 0.25 , 0.5 , 1. ], [ 0. , 0. , 0. , 0. , 1. , 0. ], [ 0.3125 , 0.5 , 0.75 , 1. , 1. , 0. ], [ 0. , 0. , 0. , 2. , 0. , 0. ], [ 2.5 , 3. , 3. , 2. , 0. , 0. ]]) g_inv = umatrix.matrix([[-192. , 192. , -48. , -48. , -4. , 4. ], [ 240. , -240. , 64. , 56. , 6. , -4. ], [ -80. , 80. , -24. , -16. , -3. , 1. ], [ 0. , 0. , 0. , 0. , 0.5, 0. ], [ 0. , 0. , 1. , 0. , 0. , 0. ], [ 1. , 0. , 0. , 0. , 0. , 0. ]]) (det,inv) = ulinalg.det_inv(g) result['determinant 1'] = (abs(det - 0.0078125) < 0.000001) result['inverse 1'] = umatrix.matrix_equal(inv, g_inv, 0.000001) # second test x = umatrix.matrix([[3.,2.,0.,1.],[4.,0.,1.,2.],[3.,0.,2.,1.],[9.,2.,3.,1.]]) (det,inv) = ulinalg.det_inv(x) det_res = det == 24.0 f = umatrix.matrix([[-0.25 , 0.25 , -0.5 , 0.25 ], [0.66666666666666667 , -0.49999999999999999, 0.50000000000000001 , -0.16666666666666667], [0.16666666666666667 , -0.49999999999999999, 1.00000000000000002 , -0.16666666666666667], [0.41666666666666667 , 0.25 , 0.50000000000000001 , -0.41666666666666667]]) result['inverse 2'] = matrix_compare(inv, f, tol=eps*2)#, tol=0.000000000000001) f[3,3] = -0.416668 result['determinant 2'] = det_res result['matrix_equal True'] = umatrix.matrix_equal(inv, f, tol=0.0001) result['matrix_equal False'] = umatrix.matrix_equal(inv, f) == False x1 = umatrix.matrix([[0.71,-0.71,0.7],[0.71,0.71,0.5],[0,0,1]]) z = ulinalg.pinv(x1) result['psuedo inverse'] = matrix_compare(z, umatrix.matrix([[0.7042253521126759 , 0.704225352112676 , -0.8450704225352109 ], [-0.704225352112676 , 0.704225352112676 , 0.1408450704225352 ], [1.110223024625157e-16, 5.551115123125783e-17, 0.9999999999999998 ]]), tol=2*eps) return result
def comportement(self, x, t): return umatrix.matrix([[pi / 180.0 * 20]])
def comportement(self, x, u, t): th = x[0] dth = self.A * th + self.K * u[0, 0] return umatrix.matrix([[dth]])
y = int(round(rd * math.sin(math.radians(azm_i[index] - 90)), 0)) xm = xc + x ym = yc + y graphics.fill_circle(xm, ym, 2, 1) # show current location if visible if visible_i[index] == 'Yes': rd = int(round(rad * math.cos(math.radians(alc_i[index])), 0)) x = int(round(rd * math.cos(math.radians(azc_i[index] - 90)), 0)) y = int(round(rd * math.sin(math.radians(azc_i[index] - 90)), 0)) xcu = xc + x ycu = yc + y graphics.circle(xcu, ycu, 2, 1) # find path of transit A = matrix.matrix([[xr**2, xr, 1], [xm**2, xm, 1], [xs**2, xs, 1]]) B = matrix.matrix([[yr], [ym], [ys]]) d_i = linalg.det_inv(A) invA = d_i[1] X = linalg.dot(invA, B) for i in range(xr - xs): x = xs + i oled.pixel(x, yc - int((X[0] * x**2)) + int( (X[1] * x)) + int(X[2]), 1) oled.show() utime.sleep(10) # collect garbage just in case that is causing the crashes gc.collect()
def comportement(self, x, u, t): th = x[0, 0] dth = -self.lbda * (th - u[0, 0]) return umatrix.matrix([[th, dth]]).T
def skyChart(name): # display stuff oled.text(name, 0, 0) oled.text("Visible:", 0, 10) oled.text(visible_i[names.index(name)], 80, 10) oled.show() # sky chart xc = 94 yc = 31 rad = 29 graphics.circle(xc, yc, rad, 1) # show planet rise azimuth x = int( round(rad * math.cos(math.radians(azr_i[names.index(name)] - 90)), 0)) y = int( round(rad * math.sin(math.radians(azr_i[names.index(name)] - 90)), 0)) xr = xc + x yr = yc + y graphics.fill_circle(xr, yr, 2, 1) # show planet set azimuth x = int( round(rad * math.cos(math.radians(azs_i[names.index(name)] - 90)), 0)) y = int( round(rad * math.sin(math.radians(azs_i[names.index(name)] - 90)), 0)) xs = xc + x ys = yc + y graphics.fill_circle(xs, ys, 2, 1) # show location at maximum altitude rd = int(round(rad * math.cos(math.radians(alm_i[names.index(name)])), 0)) x = int( round(rd * math.cos(math.radians(azm_i[names.index(name)] - 90)), 0)) y = int( round(rd * math.sin(math.radians(azm_i[names.index(name)] - 90)), 0)) xm = xc + x ym = yc + y graphics.fill_circle(xm, ym, 2, 1) # show current location if visible if visible_i[index] == 'Yes': rd = int( round(rad * math.cos(math.radians(alc_i[names.index(name)])), 0)) x = int( round(rd * math.cos(math.radians(azc_i[names.index(name)] - 90)), 0)) y = int( round(rd * math.sin(math.radians(azc_i[names.index(name)] - 90)), 0)) xcu = xc + x ycu = yc + y graphics.circle(xcu, ycu, 2, 1) # find path of transit A = matrix.matrix([[xr**2, xr, 1], [xm**2, xm, 1], [xs**2, xs, 1]]) B = matrix.matrix([[yr], [ym], [ys]]) d_i = linalg.det_inv(A) invA = d_i[1] X = linalg.dot(invA, B) for i in range(xr - xs): x = xs + i oled.pixel(x, yc - int((X[0] * x**2)) + int((X[1] * x)) + int(X[2]), 1) oled.show() # collect garbage just in case that is causing the crashes gc.collect()
def zeros(m, n, dtype=umatrix.ddtype): return umatrix.matrix([[0 for i in range(n)] for j in range(m)], dtype=dtype)
import umatrix, ulinalg import data as d import show A = umatrix.matrix(d.A) B = umatrix.matrix(d.B).transpose() print('=== Multiply Integer matrix ...') X = show.time(ulinalg.dot)(A, B) print(X) C = umatrix.matrix(d.C) D = umatrix.matrix(d.D).transpose() print('=== Multiply Float matrix ...') X = show.time(ulinalg.dot)(C, D) print(X)
def scaler(): result = {} x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) try: result['scaler * matrix'] = matrix_compare(2*x10, umatrix.matrix([[0, 2, 4, 6],[8, 10, 12, 14],[16, 18, 20, 22],[24, 26, 28, 30]])) except TypeError: result['scaler * matrix'] = (False, 'TypeError') try: result['scaler + matrix'] = matrix_compare(2.1+x10, umatrix.matrix([[2.1 , 3.1 , 4.1 , 5.1 ],[6.1 , 7.1 , 8.1 , 9.1 ],[10.1, 11.1, 12.1, 13.1],[14.1, 15.1, 16.1, 17.1]])) except TypeError: result['scaler + matrix'] = (False, 'TypeError') try: result['scaler - matrix'] = matrix_compare(1-x10, umatrix.matrix([[1, 0, -1 , -2],[-3, -4, -5, -6],[-7, -8, -9, -10],[-11, -12, -13, -14]])) except TypeError: result['scaler - matrix'] = (False, 'TypeError') result['matrix + scaler'] = matrix_compare(x10+2.1, umatrix.matrix([[2.1 , 3.1 , 4.1 , 5.1 ],[6.1 , 7.1 , 8.1 , 9.1 ],[10.1, 11.1, 12.1, 13.1],[14.1, 15.1, 16.1, 17.1]])) result['matrix - scaler'] = matrix_compare(x10-1.4, umatrix.matrix([[-1.4, -0.3999999999999999, 0.6000000000000001 , 1.6],[2.6, 3.6, 4.6, 5.6],[6.6, 7.6, 8.6, 9.6],[10.6, 11.6, 12.6, 13.6]]), tol=eps) result['matrix * scaler'] = matrix_compare(x10*3, umatrix.matrix([[0, 3, 6, 9],[12, 15, 18, 21],[24, 27, 30, 33],[36, 39, 42, 45]])) result['matrix / scaler'] = matrix_compare(x10/3, umatrix.matrix([[0.0, 0.3333333333333333, 0.6666666666666667, 1.0], [1.3333333333333333 , 1.6666666666666667 , 2.0, 2.3333333333333333 ], [2.66666666666666667 , 3.0, 3.3333333333333333 , 3.6666666666666667 ], [4.0, 4.3333333333333333 , 4.6666666666666667 , 5.0]]), tol=2*eps) result['matrix // scaler'] = matrix_compare(x10//3, umatrix.matrix([[0, 0, 0, 1],[1, 1, 2, 2],[2, 3, 3, 3],[4, 4, 4, 5]])) result['negate matrix'] = matrix_compare(-x10, umatrix.matrix([[0, -1, -2, -3],[-4, -5, -6, -7],[-8, -9, -10, -11],[-12, -13, -14, -15]])) try: result['matrix ** scaler'] = matrix_compare(x10**2, umatrix.matrix([[ 0, 1, 4, 9],[16 , 25, 36, 49],[64, 81, 100, 121],[144, 169, 196, 225]])) except: result['matrix ** scaler'] = (False, 'NotImplemented') return result
def assignment(): result = {} x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x11[1,1] = 20 result['matrix element <- value'] = matrix_compare(x11, umatrix.matrix([[0,1,2],[4,20,6],[8,9,10],[12,13,14]])) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x11[1,1] = [21, 23] result['matrix element <- list'] = matrix_compare(x11, umatrix.matrix([[0,1,2],[4,21,6],[8,9,10],[12,13,14]])) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x11[1,:] = 21 result['matrix row <- value'] = matrix_compare(x11, umatrix.matrix([[0,1,2],[21,21,21],[8,9,10],[12,13,14]])) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x11[1,:] = [20, 21, 22] result['matrix row <- list'] = matrix_compare(x11, umatrix.matrix([[0,1,2],[20,21,22],[8,9,10],[12,13,14]])) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x11[:,1] = 20 result['matrix col <- value'] = matrix_compare(x11, umatrix.matrix([[0,20,2],[4,20,6],[8,20,10],[12,20,14]])) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x11[:,1] = [20, 21, 22, 23] result['matrix col <- list'] = matrix_compare(x11, umatrix.matrix([[0,20,2],[4,21,6],[8,22,10],[12,23,14]])) x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) x10[1:3,1:3] = 20 result['submatrix <- value'] = matrix_compare(x10, umatrix.matrix([[0,1,2,3],[4,20,20,7],[8,20,20,11],[12,13,14,15]])) x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) x10[1:3,1:3] = [20,21,22,23] result['submatrix <- list'] = matrix_compare(x10, umatrix.matrix([[0,1,2,3],[4,20,21,7],[8,22,23,11],[12,13,14,15]])) x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) x10[1:3,1:3] = umatrix.matrix([[20,21],[22,23]]) result['submatrix <- matrix'] = matrix_compare(x10, umatrix.matrix([[0,1,2,3],[4,20,21,7],[8,22,23,11],[12,13,14,15]])) x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) try: x11[1] = [18,19] result['matrix non-splice <- matrix/vector'] = not matrix_compare(x11, umatrix.matrix([[0,1,2],[18,19,6],[8,9,10],[12,13,14]])) except NotImplementedError: result['matrix non-splice <- matrix/vector'] = True return result
def __init__(self, _name_of_cmd, dt): self._name_of_cmd = _name_of_cmd self._dt = dt self._u = umatrix.matrix([[0.0] * len(self._name_of_cmd)])
def products(): result = {} x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) x1 = umatrix.matrix([[0.71,-0.71,0.7],[0.71,0.71,0.5],[0,0,1]]) y_row = umatrix.matrix([[1,0,1]]) y_col = umatrix.matrix([1, 0, 1], cstride=1, rstride=1) result['matrix * scaler'] = matrix_compare(x10*2, umatrix.matrix([[0, 2, 4, 6],[8, 10, 12, 14],[16, 18, 20, 22],[24, 26, 28, 30]])) result['matrix * matrix elementwise'] = matrix_compare(x11*x11, umatrix.matrix([[0, 1, 4],[16, 25, 36],[64, 81, 100],[144, 169, 196]])) result['row dot matrix 1x3 . 3x3'] = matrix_compare(ulinalg.dot(y_row,x1), umatrix.matrix([[ 0.71, -0.71, 1.7 ]])) try: result['matrix dot col 3x3 . 3x1'] = matrix_compare(ulinalg.dot(x1,y_col), umatrix.matrix([1.41, 1.21, 1.0], cstride=1, rstride=1), tol=eps) except ValueError: result['matrix dot col 3x3 . 3x1'] = False x = umatrix.matrix([[ 3., -2., -2.]]) y = umatrix.matrix([[-1., 0., 5.]]) x1 = umatrix.matrix([[ 3., -2.]]) y1 = umatrix.matrix([[-1., 0.]]) result['cross product (x,y)'] = matrix_compare(ulinalg.cross(x,y), umatrix.matrix([[-10.0 , -13.0 , -2.0]])) result['cross product (y,x)'] = matrix_compare(ulinalg.cross(y,x), umatrix.matrix([[10.0 , 13.0 , 2.0]])) result['cross product 2 (x,y)'] = matrix_compare(ulinalg.cross(x1,y1), umatrix.matrix([[-2.0]])) x = umatrix.matrix([[ 3., -2., -2.],[-1., 0., 5.]]) y = umatrix.matrix([[-1., 0., 5.]]) try: result['cross product shape mismatch (x,y)'] = matrix_compare(ulinalg.cross(x,y), umatrix.matrix([[-10.0 , -13.0 , -2.0]])) except ValueError: result['cross product shape mismatch (x,y)'] = True return result
FREQUENCY = 50 # Hz ROTATIONAL_RANGE_100 = math.pi PIN0 = Pin.exp_board.G24 PIN1 = Pin.exp_board.G14 servo0 = Servo(PIN0, 0, FREQUENCY, ROTATIONAL_RANGE_100, PULSE_MIN, PULSE_MAX) servo1 = Servo(PIN1, 1, FREQUENCY, ROTATIONAL_RANGE_100, PULSE_MIN, PULSE_MAX) acc = [] gyro = [] while True: #start = utime.ticks_ms() accelerometer = sensor.accel gyrometer = sensor.gyro acc = umatrix.matrix([accelerometer.x, accelerometer.y, accelerometer.z], cstride=1, rstride=3, dtype=float) gyro = umatrix.matrix([gyrometer.x, gyrometer.y, gyrometer.z], cstride=1, rstride=3, dtype=float) #print("acceleration [X:%0.2f, Y:%0.2f, Z:%0.2f] gyro [X:%0.2f, Y:%0.2f, Z:%0.2f] \n" # %(accelerometer.x, accelerometer.y, accelerometer.z, gyrometer.x * math.pi/180, gyrometer.y * math.pi/180, gyrometer.z * math.pi/180)) #print("temperature [T:%0.2f]\n" %(sensor.temperature)) ahrs.update_imu(gyro * math.pi / 180.0, acc) (roll, pitch, yaw) = ahrs.quaternion.to_euler()
import umatrix A = umatrix.matrix([[1, 2], [0, 3]]) B = umatrix.matrix([[1, 2, 3], [5, 6, 7]]) print(A) print(B) print(A * B)
import umatrix import data as d import show A = umatrix.matrix(*d.A) B = umatrix.matrix(*d.B).transpose print('=== Multiply Integer matrix ...') X = show.time(lambda: A * B)() print(X) C = umatrix.matrix(*d.C) D = umatrix.matrix(*d.D).transpose print('=== Multiply Float matrix ...') X = show.time(lambda: C * D)() print(X)
def products(): result = {} x11 = umatrix.matrix([[0,1,2],[4,5,6],[8,9,10],[12,13,14]]) x10 = umatrix.matrix([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]]) x1 = umatrix.matrix([[0.71,-0.71,0.7],[0.71,0.71,0.5],[0,0,1]]) y_row = umatrix.matrix([[1,0,1]]) y_col = umatrix.matrix([1, 0, 1], cstride=1, rstride=1) result['matrix * scaler'] = matrix_compare(x10*2, umatrix.matrix([[0, 2, 4, 6],[8, 10, 12, 14],[16, 18, 20, 22],[24, 26, 28, 30]])) result['matrix * matrix elementwise'] = matrix_compare(x11*x11, umatrix.matrix([[0, 1, 4],[16, 25, 36],[64, 81, 100],[144, 169, 196]])) result['row dot matrix 1x3 . 3x3'] = matrix_compare(ulinalg.dot(y_row,x1), umatrix.matrix([[ 0.71, -0.71, 1.7 ]])) try: result['matrix dot col 3x3 . 3x1'] = matrix_compare(ulinalg.dot(x1,y_col), umatrix.matrix([1.41, 1.21, 1.0], cstride=1, rstride=1)) except ValueError: result['matrix dot col 3x3 . 3x1'] = False x = umatrix.matrix([[ 3., -2., -2.]]) y = umatrix.matrix([[-1., 0., 5.]]) x1 = umatrix.matrix([[ 3., -2.]]) y1 = umatrix.matrix([[-1., 0.]]) result['cross product (x,y)'] = matrix_compare(ulinalg.cross(x,y), umatrix.matrix([[-10.0 , -13.0 , -2.0]])) result['cross product (y,x)'] = matrix_compare(ulinalg.cross(y,x), umatrix.matrix([[10.0 , 13.0 , 2.0]])) result['cross product 2 (x,y)'] = matrix_compare(ulinalg.cross(x1,y1), umatrix.matrix([[-2.0]])) x = umatrix.matrix([[ 3., -2., -2.],[-1., 0., 5.]]) y = umatrix.matrix([[-1., 0., 5.]]) try: result['cross product shape mismatch (x,y)'] = matrix_compare(ulinalg.cross(x,y), umatrix.matrix([[-10.0 , -13.0 , -2.0]])) except ValueError: result['cross product shape mismatch (x,y)'] = True return result
14, ] # G set_B = [3, 7, 11, 15] #B R_val = 0 G_val = 0 B_val = 0 num_total_packets = 960 packet_count = 0 Q = umatrix.matrix([[16, 11, 10, 16, 24, 40, 51, 61], [12, 12, 14, 19, 26, 58, 60, 55], [14, 13, 16, 24, 40, 57, 69, 56], [14, 17, 22, 29, 51, 87, 80, 62], [18, 22, 37, 56, 68, 109, 103, 77], [24, 35, 55, 64, 81, 104, 113, 92], [49, 64, 78, 87, 103, 121, 120, 101], [72, 92, 95, 98, 112, 100, 103, 99]]) # Quantization matrix for quality = 50 for i in range(num_total_packets): packet_count = packet_count + 1 with open('byte_array_to_send' + str(packet_count) + '_of_960' + '.txt', 'rb') as f: byte_array_five_tiles = f.read() print(i, byte_array_five_tiles) #matches for j in range(5): # nuber of tiles per packet count = 0 B = ulinalg.zeros(8, 8) G = ulinalg.zeros(8, 8)
def getEcartsTypes(self, num_var=None): if num_var is None: return umatrix.matrix([[sqrt(self._P[i, i]) for i in range(self._n)]]) else: return sqrt(self._P[num_var, num_var])