
python assignment
Matrix class
Implement a matrix class (in matrix.py).
a) The initializer should take a list of lists as an argument, where each outer list is a row, and each value in an inner list is a value in the corresponding row.
b) Implement the __str__ method to nicely format the string representation of the matrix: one line per row, two characters per number (%2d) and a space between numbers. For example:
m = Matrix([[1,0,0],[0,1,0],[0,0,1]])
print(m)
> 1 0 0
> 0 1 0
> 0 0 1
c) Implement a method scale(factor) that returns a new matrix where each value is multiplied by scale. For example:
m = Matrix([[1,2,3],[4,5,6],[7,8,9]])
n = m.scale(2)
print(n)> 2 4 6
> 8 10 12
>14 16 18
print(m)
> 1 2 3
> 4 5 6
> 7 8 9
d) Implement a method transpose() that returns a new matrix that has been transposed. Transposing flips a matrix over its diagonal: it switches rows and columns.
m = Matrix([[1,2,3],[4,5,6],[7,8,9]])
print(m)
> 1 2 3
> 4 5 6
> 7 8 9
print(m.transpose())
> 1 4 7
> 2 5 8
> 3 6 9
e) Implement a method a.multiply(b) that gives a new matrix, c, that is the result of multiplying a by b. Matrix multiplication only works if matrix a has the same number of columns as matrix b has rows. If this is not the case, please return None. The value of matrix c at row i, column j is defined as:
cij=ai1b1j+ai2b2j+...+ainbnj
Where aij is the value of matrix a at row i, column j.
e = Matrix([[1, 2, 3], [2, 1, 3], [3, 2, 1]])
print(e.multiply(e))
>14 10 12
>13 11 12
>10 10 16

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- C++ Write a ternarySearch function. A ternary search is similar to a binary search, but it divides an array into three sections instead of two - find the two points that divide the array into three (almost) equal sections, and then use these points to decide where to search for the key.arrow_forwardC++arrow_forwardC++arrow_forward
- C++arrow_forwardIn C++,define a vector object and initialize it with 3 values and then define an iterator that points to elements of this object, then ask the user to enter 3 more values and then add them to the previous vector and then print all elements in the vector.arrow_forwardPython languagearrow_forward
- C++arrow_forwardWrite a findSpelling Function -PHP Write a function findSpellings($word, $allWords) that takes a string and an array of dictionary words as parameters. The function should return an array of possible spellings for a misspelled $word. One way to approach this is to use the soundex() Words that have the same soundex are spelled similarly. Return an array of words from $allWords that match the soundex for $word. I have most of the code. How do I append the $sound to the $mathcing array?arrow_forwardC++ programming language.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





