def test__setitem__(self): matrix = Matrix(2, 2) matrix[0, 0] = 1 self.assertEqual(matrix[0, 0], 1)
def compute_boundary_matrix(self): matrix = Matrix(len(self.simplicies)) for i, s in enumerate(self.simplicies): for facet in iter_facets(s): matrix.set_one(self.simplicies_indexes[facet], i) return matrix
from numpy import matrix from matrix import Matrix matrix1 = matrix([[0, 0, 0], [1, 2, 3], [2, 1, 3]]) matrix2 = matrix([[2, 0, 0], [1, 2, 3], [2, 1, 3]]) print((matrix1 @ matrix2).tolist()[0]) matrix1 = Matrix([[0, 0, 0], [1, 2, 3], [2, 1, 3]]) matrix2 = Matrix([[2, 0, 0], [1, 2, 3], [2, 1, 3]]) print(matrix1.mul(matrix2).arg[0])
from matrix import Matrix data = [[2, 3, 4], [0, 1, 3]] a = Matrix(data) print(a) print() print(Matrix((4, 2))) print() print(Matrix([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], (3, 3)))
p3 = Permutation([5, 4, 0, 2, 3, 7, 6, 1]) all_permutations = Permutation.get_permutations(8, list(range(8))) right = (p1.get_inverse_permutation() * (p2**13))**187 for permutation in all_permutations: p = Permutation(permutation) if (p * p3 * p).permutation == right.permutation: print("Результат:") print("(0 1 2 3 4 5 6 7)") print("(", " ".join(str(i) for i in p.permutation), ")", sep="") print("Задание 2.") print("Характеристичексий многочлен:") a = SquareMatrix([ [-3, -4, -1, 4], [1, -4, -3, 3], [-5, -2, 2, 2], [0, 0, 2, -2], ]) for i, a_i in enumerate(a.get_characteristic_polynomial()[:4]): print("(" + str(a_i) + ")x^" + str(4 - i) + " + ", end="") print("(" + str(a.get_characteristic_polynomial()[4]) + ")") x = SquareMatrix((a**2 + a.multiply_on_number(3) + SquareMatrix.get_e(4).multiply_on_number(2)).matrix)**2 print("Определитель посчитаю на листочке из значений характеристического") print("Задание 5") A = Matrix([[3, -3], [1, -1], [-1, 1], [4, -4], [-3, 3]]) B = Matrix([[-2, -1, 3, 2, 1], [3, 1, -2, -1, 2]]) res = SquareMatrix((A * B).matrix) for i, a_i in enumerate(res.get_characteristic_polynomial()[:5]): print("(" + str(a_i) + ")x^" + str(5 - i) + " + ", end="") print("(" + str(res.get_characteristic_polynomial()[5]) + ")")
from matrix import Matrix from matrix import Vector m1 = Matrix([[0.0, 1.0, 2.0, 3.0], [0.0, 2.0, 4.0, 6.0]]) m2 = Matrix([[0.0, 1.0], [2.0, 3.0], [4.0, 5.0], [6.0, 7.0]]) m3 = Matrix((3, 3)) m4 = Matrix([[1.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], (3, 3)) m5 = Matrix([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], (3, 3)) m6 = Matrix([[1.0, 2.0]]) m7 = Matrix((3, 4)) v1 = Vector([2.0, 3.0]) print("size m1:", m1.shape) print("size m2:", m2.shape) print("size m3:", m3.shape) print("size m4:", m4.shape) print("size m5:", m5.shape) print("size m6:", m6.shape) print("size m7:", m7.shape) print("SIZE m1:", m1.shape, "==>", m1) print("SIZE m2:", m2.shape, "==>", m2) print("SIZE m3:", m3.shape, "==>", m3) print("SIZE m4:", m4.shape, "==>", m4) print("m3+m4=", m3 + m4) print("m4+m5=", m4 + m5) print("m3+1=", m3 + 1) print("1+m3=", 1 + m3) print("m4-m5=", m4 - m5) print("m4-1=", m4 - 1) print("m4-1=", 1 - m4)
#!/usr/bin/python2 # -*- coding: utf-8 -*- # Code by Héctor Blanco Lozano from matrix import Matrix matrix_1 = Matrix([[1, 1], [1, 1]]) matrix_2 = Matrix([[4, 1], [3, 1]]) print(matrix_2) matrix_3 = matrix_1 + matrix_2 print(matrix_3) matrix_4 = matrix_1 + 5 print(matrix_4) matrix_5 = matrix_1 - matrix_2 print(matrix_3) matrix_6 = matrix_1 - 5 print(matrix_4) matrix_6 = matrix_1.prod(matrix_2) print(matrix_6) for v in matrix_2: print(v)
def test_can_extract_row_from_non_square_matrix_with_no_corresponding_column( self): matrix = Matrix("1 2 3\n4 5 6\n7 8 9\n8 7 6") self.assertEqual(matrix.row(4), [8, 7, 6])
def test_extract_column_from_one_number_matrix(self): matrix = Matrix("1") self.assertEqual(matrix.column(1), [1])
def test_can_extract_row(self): matrix = Matrix("1 2\n3 4") self.assertEqual(matrix.row(2), [3, 4])
def test_extract_row_where_numbers_have_different_widths(self): matrix = Matrix("1 2\n10 20") self.assertEqual(matrix.row(2), [10, 20])
from runge_kutta import runge_kutta from matrix import Matrix import math if __name__ == '__main__': A = Matrix([[0, 1], [-1, 0]]) B = Matrix([[0], [0]]) T = math.pi * 2 / 50 # T = 0.001 t_max = 2 x = Matrix([[1], [1]]) runge_kutta(A=A, B=B, t_max=t_max, T=T, x0=x, real_value_flag=True)
# coding: utf-8 # In[2]: # Run this cell but don't modify it. # # %get_ipython().run_line_magic('load_ext', 'autoreload') # %get_ipython().run_line_magic('autoreload', '2') from matrix import Matrix, zeroes, identity # In[3]: # some functionality already exists... here's a demo m1 = Matrix([[1, 2], [3, 4]]) m2 = Matrix([[2, 5], [6, 1]]) print("m1 is") print(m1) print("m2 is") print(m2) print("we've also provided you with a function called zeros") print("here's what happens when you call zeros(4,2)") print(zeroes(4, 2)) print("we've also provided you with a function called identity") print("here's identity(3)")
def test_scaleBy(self): matrix = Matrix(2, 2) matrix[0, 0] = 1 matrix.scaleBy(10) self.assertEqual(matrix[0, 0], 10)
def setUp(self): self.size = 6 self.ladders = [(2, 5)] matrix = Matrix(self.size, self.ladders, []) self.transitions = matrix.transitions
def test_can_extract_column(self): matrix = Matrix("1 2 3\n4 5 6\n7 8 9") self.assertEqual(matrix.column(3), [3, 6, 9])
def toGNF(self): """Convert grammar to Greibach normal form """ # 1) convert to matrix form: X * H + K, # where X is a row vector of nonterminals, # H is a matrix of productions which start with nonterminal # K is a row vector of productions which start with terminal # for example look at [1] starting on page 20 # [1] https://www.cis.upenn.edu/~jean/old511/html/cis51108sl4b.pdf X, H, K = self._toMatrix() # 2) write system of equations # { X = K * Y + K # { Y = H * Y + H # where Y is a matrix of new nonterminals with size same as H Y = [[[(('Y{}{}'.format(j, i), False),)] for i in range(H.mat.shape[0])] for j in range(H.mat.shape[1])] Y = Matrix(Y) # 3) calculate X from step 2 # substitute nonterminals in matrix H with calculated X values # and get matrix L # the new system is: # { X = K * Y + K # { Y = L * Y + L # calculate X, calculate Y # convert these matrices to one grammar xCalc = K.dot(Y).add(K) subs = {} for i, nt in enumerate(self.nonterminals): subs[nt] = xCalc.mat[0][i] L = H for i, row in enumerate(H.mat): for j, val in enumerate(row): newCell = [] for tup in val: if tup[0][0] in subs: for sub in subs[tup[0][0]]: newCell.append(sub + tup[1:]) else: newCell.append(tup) L.mat[i, j] = OrderedSet(newCell) yCalc = L.dot(Y).add(L) terminals = self.terminals nonterminals = self.nonterminals + \ ['Y{}{}'.format(j, i) for i in range(H.mat.shape[0]) for j in range(H.mat.shape[1])] productions = {} start = self.start for i, oldNT in enumerate(self.nonterminals): productions[oldNT] = xCalc.mat[0][i] for i, row in enumerate(yCalc.mat): for j, val in enumerate(row): lhs = 'Y{}{}'.format(j, i) productions[lhs] = val return Grammar(nonterminals, terminals, productions, start)
def test_can_extract_column_from_non_square_matrix_with_no_corresponding_row( self): matrix = Matrix("1 2 3 4\n5 6 7 8\n9 8 7 6") self.assertEqual(matrix.column(4), [4, 8, 6])
import sys sys.path.append('../ex02/') # noqa: E402 from vector import Vector from matrix import Matrix m = Matrix([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], (3, 2)) m2 = Matrix((3, 2)) m3 = Matrix([[10.0, 20.0, 0.0], [30.0, 40.0, 0.0]], (2, 3)) print("m", m) print("m2", m2) mv = Matrix([[1.0], [2.0]]) v = Vector([-5.0, 7.0]) print("TEST +") print("m + m2", m + m2) print("m + m", m + m) print("mv", mv) print("v", v) print("mv + v", mv + v) print("v + mv", v + mv) print("TEST -") print("m - m2", m - m2) print("m - m", m - m) print("mv", mv) print("v", v) print("mv - v", mv - v) print("TEST /") print("m", m) print("m / -4", m / -4) print("TEST *") print("m", m) print("m * -2", m * -2)
def test_extract_column_where_numbers_have_different_widths(self): matrix = Matrix("89 1903 3\n18 3 1\n9 4 800") self.assertEqual(matrix.column(2), [1903, 3, 4])
#!/usr/bin/env python import rospy from geometry_msgs.msg import Twist # sudo pip3 install adafruit-circuitpython-motorkit from adafruit_motorkit import MotorKit from matrix import Matrix kit = MotorKit() jacobian = Matrix(4, 3) direction = Matrix(3, 1) def callback(vel_msg): """ note: throttle= [-1; 1] """ global kit, jacobian, direction direction[0, 0] = vel_msg.linear.x direction[1, 0] = vel_msg.linear.y direction[2, 0] = vel_msg.angular.z print("direction\n", direction) print("jacobian\n", jacobian) motors_cmd = jacobian * direction kit.motor1.throttle = motors_cmd[0, 0] kit.motor2.throttle = motors_cmd[1, 0]
def test_extract_row_from_one_number_matrix(self): matrix = Matrix("1") self.assertEqual(matrix.row(1), [1])
import time import datetime # Configure the count of pixels, SPI Device and Port: import colors from matrix import Matrix matrix = Matrix() def draw_time(): # background color: bg_color = (0, 100, 255) # digit color: digit_color = colors.WHITE while True: matrix.clear() # get time in minutes and hours minutes = datetime.datetime.now().time().minute hours = datetime.datetime.now().time().hour sec = datetime.datetime.now().time().second micro_sec = datetime.datetime.now().time().microsecond draw_seconds(bg_color, sec, micro_sec) # set two pixels between hour and minute digits matrix.set(7, 9, digit_color) matrix.set(7, 0, digit_color)
# 4x1 col vector list_11 = [[1], [2], [3], [4]] # 1x3 row vector list_12 = [[1, 2, 3]] # 2x3 matrix list_21 = [[1, 2, 3], [4, 5, 6]] # 4x3 matrix list_22 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] # 3x3 matrix list_23 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ## Size Tests -------------------------------------- print(Matrix.get_size(list_10)) print(Matrix.get_size(list_11)) print(Matrix.get_size(list_12)) print(Matrix.get_size(list_21)) print(Matrix.get_size(list_22)) print(Matrix.get_size(list_23)) ## Multi Test -------------------------------------- print(Matrix.dim_check_for_mult(Matrix(list_11), Matrix(list_12))) print(Matrix.dim_check_for_mult(Matrix(list_22), Matrix(list_23))) print(Matrix.dim_check_for_mult(Matrix(list_23), Matrix(list_22)))
def test(stg): print(str('\nTest ' + stg + ' ').ljust(20, '-') + '\n') def print_oper(a0, op, a1, a2): lenj = max(len(str(a0)), len(str(a1)), len(str(a2))) + 3 print(str(a0).rjust(lenj, ' ')) print(op) print(str(a1).rjust(lenj, ' ')) print(''.rjust(lenj, '-')) print(str(' = ' + str(a2)).rjust(lenj, ' ')) mtx = Matrix([[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0]]) print(mtx.data) print(mtx.shape) mtx = Matrix((3, 4)) print(mtx.data) print(mtx.shape) mtx = Matrix([[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], (3, 3)) print(mtx.data) print(mtx.shape) test('mtx + vec') mtx = Matrix([[4.0], [5.0], [6.0], [7.0]]) vec = Vector([0.0, 1.0, 2.0, 3.0]) print('Matrix shape: ' + str(mtx.shape)) print('Vector shape: ' + str((vec.size, 1))) res = mtx + vec print_oper(mtx, ' + ', vec, res)
def setUp(self): self.size = 6 self.matrix = Matrix(self.size, [], []) self.transitions = self.matrix.transitions self.transition_matrix = self.matrix.transition_matrix
def __init__(self): self.transform = Matrix()
def setUp(self): self.size = 6 self.snakes = [(5, 2)] matrix = Matrix(self.size, self.snakes, []) self.transitions = matrix.transitions
from matrix import Matrix # 4x1 col vector list_11 = [[1],[2],[3],[4]] print( isinstance( Matrix(list_11), Matrix)) print( type(Matrix(list_11)) )
def test__getitem__(self): matrix = Matrix(2, 2) self.assertEqual(matrix[0, 0], 0)