def getGradient(features, weight, target): """ This function calculates gradient of LSE( 2(A^T)Ax - 2(A^T)b ) """ # gradient = 2(A^T)Ax - 2(A^T)b features_T = mat.transpose(features) gradient = 2 * mat.mul(mat.mul(features_T, features), weight) gradient -= 2 * mat.mul(features_T, target) return gradient
def newtonmethod(hession_inv, gradient, weight): """ Using newton's method to optimize the answer. xn = xn-1 """ # x1 = x0 - (H^-1)(gradient) return weight - mat.mul(hession_inv, gradient)
def genb(features, y): """ input: A, b output: (A^T)(b) """ y = np.array(y).reshape(-1, 1) b = mat.mul(mat.transpose(features), y) return b
def getHessionInv(features): """ This function calculates hession matrix of LSE( 2(A^T)A ) """ # hession = 2(A^T)A features_T = mat.transpose(features) hession = 2 * mat.mul(features_T, features) return mat.inv(hession)
def genmatrix(features, rate): """ input: A, rate output: (A^T)(A) - (rate)(I) This function will return (A^T)(A) - rate * I """ matrix = mat.mul(mat.transpose(features), features) matrix -= rate * np.eye(matrix.shape[0]) return matrix
def d_hill(ciphertext, key): # your code here if len(ciphertext) == 0: print('Error(d_hill): invalid ciphertext') return '' new_key = '' if len(key) > 4: new_key += key[:4] elif len(key) == 4: new_key += key else: new_key += key counter = 0 while len(new_key) < 4: new_key += key[counter] counter += 1 baseString = utilities_A4.get_lower() key_matrix = matrix.new_matrix(2, 2, 0) count = 0 for i in range(2): for j in range(2): key_matrix[i][j] = baseString.index(new_key[count].lower()) count += 1 if mod.gcd(matrix.det(key_matrix), 26) != 1: print('Error(d_hill): key is not invertible') return '' inverse_key_matrix = matrix.inverse(key_matrix, 26) plaintext = '' non_alpha = utilities_A4.get_nonalpha(ciphertext) blocks = utilities_A4.text_to_blocks( utilities_A4.remove_nonalpha(ciphertext), 2) for block in blocks: block_m = matrix.new_matrix(2, 1, 0) block_m[0][0] = baseString.index(block[0].lower()) block_m[1][0] = baseString.index(block[1].lower()) result_m = matrix.matrix_mod(matrix.mul(inverse_key_matrix, block_m), 26) plaintext += baseString[result_m[0][0]].lower() plaintext += baseString[result_m[1][0]].lower() plaintext = utilities_A4.insert_nonalpha(plaintext, non_alpha) while plaintext[-1] == 'q': plaintext = plaintext[:-1] return plaintext
def testMul(self): lhs = OrderedSet(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']) rhs = OrderedSet(['1', '2', '3', '4', '5', '6', '7', '8']) prod = mul(lhs, rhs) result = OrderedSet(['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'g1', 'g2', 'g3', 'g4', 'g5', 'g6', 'g7', 'g8', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8']) self.assertEqual(prod, result)
def __init__(self, filename): super(PygameInterpreter, self).__init__(filename) # Font if os.path.exists(DIR_FONT): font_name = DIR_FONT else: self.error("No {0}. Falling back to pygame default font.".format( DIR_FONT)) font_name = None # pygame default self.font = pygame.font.Font(font_name, FONT_SIZE) # pixel positioning self.text_height = self.font.get_linesize() border_width = int(self.text_height * BORDER_WIDTH_MULTIPLIER) self.text_topleft = [border_width] * 2 txt_area_size = matrix.sub(self.resolution, matrix.mul(self.text_topleft, [2, 2])) self.row_length = txt_area_size[0] # text placing self.linenumber = 0 self.remaining_row = self.row_length self.text = [] self.text_states = [] # setup self.clearwait = False #~ # black filter #~ self.darkening = 0 # buttons self.buttons = [] # menu self.rmenu = None # saving # normally this should be read in from the savenumber command self.savenumber = 20 self.saves = dict()
def __init__(self, filename): super(PygameInterpreter, self).__init__(filename) # Font if os.path.exists(DIR_FONT): font_name = DIR_FONT else: self.error( "No {0}. Falling back to pygame default font.".format(DIR_FONT) ) font_name = None # pygame default self.font = pygame.font.Font(font_name, FONT_SIZE) # pixel positioning self.text_height = self.font.get_linesize() border_width = int(self.text_height*BORDER_WIDTH_MULTIPLIER) self.text_topleft = [border_width]*2 txt_area_size = matrix.sub( self.resolution, matrix.mul(self.text_topleft, [2, 2]) ) self.row_length = txt_area_size[0] # text placing self.linenumber = 0 self.remaining_row = self.row_length self.text = [] self.text_states = [] # setup self.clearwait = False #~ # black filter #~ self.darkening = 0 # buttons self.buttons = [] # menu self.rmenu = None # saving # normally this should be read in from the savenumber command self.savenumber = 20 self.saves = dict()
def test_q4(): print("-------------------------------------------") print("Testing Q4: Matrix Library") filename = 'q4_solution.txt' outFile = open(filename, 'w') print() outFile.write('1- Testing is_vector:\n') outFile.write('is_vector({}) = {}\n'.format([], matrix.is_vector([]))) outFile.write('is_vector({}) = {}\n'.format([10], matrix.is_vector([10]))) outFile.write('is_vector({}) = {}\n'.format([10, 20], matrix.is_vector([10, 20]))) outFile.write('is_vector({}) = {}\n'.format(10, matrix.is_vector(10))) outFile.write('is_vector({}) = {}\n'.format([3, 4.5], matrix.is_vector([3, 4.5]))) outFile.write('is_vector({}) = {}\n'.format([[]], matrix.is_vector([[]]))) outFile.write('is_vector({}) = {}\n'.format([[1, 2], [3, 4]], matrix.is_vector([[1, 2], [3, 4]]))) outFile.write('\n') outFile.write('2- Testing is_matrix') A = [] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = [5] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = [[1, 2], [3, 4]] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = [[1], [2], [3]] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = [[1, 2, 3], [4, 5, 6]] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = 5 outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = [5.5] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) A = [[1, 2, 3], [4, 5]] outFile.write('is_matrix({}) = {}\n'.format(A, matrix.is_matrix(A))) outFile.write('\n') print('3- Testing print_matrix') A = [] print('print_matrix({})='.format(A)) matrix.print_matrix(A) A = [10, 20, 30] print('print_matrix({})='.format(A)) matrix.print_matrix(A) A = [[10], [20], [30]] print('print_matrix({})='.format(A)) matrix.print_matrix(A) A = [[10, 20, 30], [40, 50, 60], [70, 80, 10]] print('print_matrix({})='.format(A)) matrix.print_matrix(A) A = [[10, 20, 30], [40, 50, 60], [70, 80]] print('print_matrix({})='.format(A)) print(matrix.print_matrix(A)) print() outFile.write('4/5/6- Testing size functions\n') A = [] outFile.write('get_rowCount({}) = {}\n'.format(A, matrix.get_rowCount(A))) outFile.write('get_ColumnCount({}) = {}\n'.format( A, matrix.get_columnCount(A))) outFile.write('get_size({}) = {}\n'.format(A, matrix.get_size(A))) outFile.write('\n') A = [1, 2, 3] outFile.write('get_rowCount({}) = {}\n'.format(A, matrix.get_rowCount(A))) outFile.write('get_ColumnCount({}) = {}\n'.format( A, matrix.get_columnCount(A))) outFile.write('get_size({}) = {}\n'.format(A, matrix.get_size(A))) outFile.write('\n') A = [[1, 2], [3, 4], [5, 6]] outFile.write('get_rowCount({}) = {}\n'.format(A, matrix.get_rowCount(A))) outFile.write('get_ColumnCount({}) = {}\n'.format( A, matrix.get_columnCount(A))) outFile.write('get_size({}) = {}\n'.format(A, matrix.get_size(A))) outFile.write('\n') A = [[1, 2], [3]] outFile.write('get_rowCount({}) = {}\n'.format(A, matrix.get_rowCount(A))) outFile.write('get_ColumnCount({}) = {}\n'.format( A, matrix.get_columnCount(A))) outFile.write('get_size({}) = {}\n'.format(A, matrix.get_size(A))) outFile.write('\n') outFile.write('7- Testing is_square\n') A = [] outFile.write('is_square({}) = {}\n'.format(A, matrix.is_square(A))) A = [5] outFile.write('is_square({}) = {}\n'.format(A, matrix.is_square(A))) A = [5, 6] outFile.write('is_square({}) = {}\n'.format(A, matrix.is_square(A))) A = [[1, 2], [3, 4]] outFile.write('is_square({}) = {}\n'.format(A, matrix.is_square(A))) A = [5.5] outFile.write('is_square({}) = {}\n'.format(A, matrix.is_square(A))) outFile.write('\n') outFile.write('8/9/10- Testing getter functions\n') A = [[1, 2, 3], [4, 5, 6]] i = 0 j = 1 outFile.write('get_row({},{}) = {}\n'.format(A, i, matrix.get_row(A, i))) outFile.write('get_Column({},{}) = {}\n'.format(A, j, matrix.get_column(A, j))) outFile.write('get_element({},{},{}) = {}\n'.format( A, i, j, matrix.get_element(A, i, j))) outFile.write('\n') i = 2 j = 2 outFile.write('get_row({},{}) = {}\n'.format(A, i, matrix.get_row(A, i))) outFile.write('get_Column({},{}) = {}\n'.format(A, j, matrix.get_column(A, j))) outFile.write('get_element({},{},{}) = {}\n'.format( A, i, j, matrix.get_element(A, i, j))) outFile.write('\n') i = 1 j = 3 outFile.write('get_row({},{}) = {}\n'.format(A, i, matrix.get_row(A, i))) outFile.write('get_Column({},{}) = {}\n'.format(A, j, matrix.get_column(A, j))) outFile.write('get_element({},{},{}) = {}\n'.format( A, i, j, matrix.get_element(A, i, j))) outFile.write('\n') A = [[1, 2, 3], []] outFile.write('get_row({},{}) = {}\n'.format(A, i, matrix.get_row(A, i))) outFile.write('get_Column({},{}) = {}\n'.format(A, j, matrix.get_column(A, j))) outFile.write('get_element({},{},{}) = {}\n'.format( A, i, j, matrix.get_element(A, i, j))) outFile.write('\n') outFile.write('11- Testing new_matrix\n') r = 0 c = 0 pad = 0 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) c = 1 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) r = 1 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) r = 2 c = 1 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) c = 2 r = 1 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) c = 3 r = 3 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) r = -1 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) r = 3 c = -5 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) c = 5 pad = 3.5 outFile.write('new_matrix({},{},{})=\n{}\n'.format( r, c, pad, matrix.new_matrix(r, c, pad))) outFile.write('\n') outFile.write('12- Testing get_I\n') size = -1 outFile.write('get_I({}) = {}\n'.format(size, matrix.get_I(size))) size = 0 outFile.write('get_I({}) = {}\n'.format(size, matrix.get_I(size))) size = 1 outFile.write('get_I({}) = {}\n'.format(size, matrix.get_I(size))) size = 2 outFile.write('get_I({}) = {}\n'.format(size, matrix.get_I(size))) size = 3 outFile.write('get_I({}) = {}\n'.format(size, matrix.get_I(size))) outFile.write('\n') outFile.write('13- Testing is_identity\n') A = [1] outFile.write('is_identity({}) = {}\n'.format(A, matrix.is_identity(A))) A = matrix.get_I(3) outFile.write('is_identity({}) = {}\n'.format(A, matrix.is_identity(A))) A = [[1, 0], [1, 1]] outFile.write('is_identity({}) = {}\n'.format(A, matrix.is_identity(A))) A = [[1, 0], [0, 1, 0]] outFile.write('is_identity({}) = {}\n'.format(A, matrix.is_identity(A))) outFile.write('\n') outFile.write('14- Testing scalar_mul\n') A = [[1, 2], [3, 4]] c = 10 outFile.write('scalar_mul({},{}) = {}\n'.format(A, c, matrix.scalar_mul(c, A))) A = [1, 2, 3, 4] outFile.write('scalar_mul({},{}) = {}\n'.format(A, c, matrix.scalar_mul(c, A))) A = [] outFile.write('scalar_mul({},{}) = {}\n'.format(A, c, matrix.scalar_mul(c, A))) A = [1, 2, 3, [4]] outFile.write('scalar_mul({},{}) = {}\n'.format(A, c, matrix.scalar_mul(c, A))) A = [[1, 2], [3, 4]] c = [10] outFile.write('scalar_mul({},{}) = {}\n'.format(A, c, matrix.scalar_mul(c, A))) outFile.write('\n') outFile.write('15- Testing mul\n') A = [[1, 2], [3, 4]] B = [[10, 20], [30, 40]] outFile.write('mul({},{})=\n{}\n'.format(A, c, matrix.mul(A, B))) A = [[1, 2, 3], [5, 6, 7]] B = [[10, 20], [30, 40], [50, 60]] outFile.write('mul({},{})= {}\n'.format(A, c, matrix.mul(A, B))) A = [5] B = [10] outFile.write('mul({},{})= {}\n'.format(A, B, matrix.mul(A, B))) A = [0, 1, 2] B = [[0], [1], [2]] outFile.write('mul({},{})= {}\n'.format(A, B, matrix.mul(A, B))) A = [[0], 1] B = [1, 0] outFile.write('mul({},{})= {}\n'.format(A, B, matrix.mul(A, B))) A = [1, 0] B = [[0], 1] outFile.write('mul({},{})= {}\n'.format(A, B, matrix.mul(A, B))) A = [[1, 2, 3], [5, 6, 7]] B = [[10, 20], [30, 40], [50, 60]] outFile.write('mul({},{})= {}\n'.format(B, A, matrix.mul(B, A))) A = [[1, 2, 3], [5, 6, 7]] B = [[10, 20], [30, 40]] outFile.write('mul({},{})= {}\n'.format(A, B, matrix.mul(A, B))) outFile.write('\n') outFile.write('16- Testing matrix_mod\n') A = [[1, 2], [3, 4]] m = 2 outFile.write('matrix_mod({},{})= {}\n'.format(A, m, matrix.matrix_mod(A, m))) A = [1, 2, 3, 4] m = 2 outFile.write('matrix_mod({},{})= {}\n'.format(A, m, matrix.matrix_mod(A, m))) A = [[3], [5]] m = 3 outFile.write('matrix_mod({},{})= {}\n'.format(A, m, matrix.matrix_mod(A, m))) A = [[3], [5]] m = 0 outFile.write('matrix_mod({},{})= {}\n'.format(A, m, matrix.matrix_mod(A, m))) A = [3, [5]] m = 6 outFile.write('matrix_mod({},{})= {}\n'.format(A, m, matrix.matrix_mod(A, m))) outFile.write('\n') outFile.write('17- Testing det\n') A = [[1, 2], [3, 4]] outFile.write('det({})= {}\n'.format(A, matrix.det(A))) A = [10] outFile.write('det({})= {}\n'.format(A, matrix.det(A))) A = [[1, 1, 1], [2, 2, 2], [3, 3, 3]] outFile.write('det({})= {}\n'.format(A, matrix.det(A))) A = [[1, 1, 1], [2, 2]] outFile.write('det({})= {}\n'.format(A, matrix.det(A))) outFile.write('\n') outFile.write('18- Testing inverse\n') A = [[1, 4], [8, 11]] m = 26 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) A = [[4, 3], [1, 1]] m = 5 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) A = [[1, 4], [8, 10]] m = 26 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) A = [1, 4, 8, 10] m = 15 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) A = [[4, 3], [1, 1]] m = -5 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] m = 7 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) A = [[1, 2, 3], [4, 5]] m = 7 outFile.write('inverse({},{})= {}\n'.format(A, m, matrix.inverse(A, m))) outFile.close() print('Comparing q4_solution with q4_sample:') print(utilities_A4.compare_files('q4_solution.txt', 'q4_sample.txt')) print() print("-------------------------------------------")