Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction to Computer Programming CIIC3011- Mini-project 2 In this project you will be implement a Python class that represents a matrix, which is a mathematical

Introduction to Computer Programming CIIC3011- Mini-project 2

In this project you will be implement a Python class that represents a matrix, which is a mathematical object consisting of rows and columns. This class will be called MyMatrix, and it will support the following operations: 1) matrix addition, 2) matrix subtraction, 3) scalar multiplication, 4) matrix multiplication, 5) identity matrix, and 6) transpose.

2. Representation

Your class will represent the matrix as a two-dimensional array of floating point numbers. The class needs to keep track of the following three data fields: 1) rows, 2) columns, and 3) elements of the matrix. The constructor will need two parameters: 1) rows, and 2) columns. A two dimensional arrays in Python is a list of lists.For example:

elements = [[1, 2], [3, 3],[4,0]] defines the following 3x2 matrix:

M= [[1,2],[3,4],[5,6]]

This matrix has 3 rows, and 2 columns, hence it is called a 3x2 matrix. A square matrix has an equal number of rows and columns. The expression elements[0] is the first row consisting of the list [1,2], and the expression elements[1] = [3,3] is the second row. To access the elements in row 0 and column 0 you will write elements[0][0], and to access element at row 1 and column 0 it will be elements[1][0]. In general, an element at

position (i,j) in a two-dimensional array elements is accessed as elements[i][j], and we specify it as element i,j of matrix M by writing Mi,j.

2.1 Constructor

Your constructor for the class MyMatrix will receive two parameters: N - the number of rows in the matrix, M - the number of columns in the matrix. For example, to create a 3 4 matrix M , you will write:

MyMatrix M = MyMatrix(3 , 4)

2.2 Setters and Getters

You need to implement two methods to write or read elements of the matrix. These methods are often referred to as getters and setters.

Getter - the purpose is to read the value of an element at position (i, j) in the matrix M. The signature of the method is: get(i,j). This will access the element Mi,j. For example,

to access the element at position (0,1) of matrix M, you will write: M.get (0, 1)

Setter - the purpose is to write the value of an element at position (i, j) in the matrix M. The signature of the method is: set(i,j, v), where v is the value to be set at position (i,j).

This will set v as the value Mi,j in matrix M. For example, to set 101 as the element at position (1,2) of matrix M, you will write:

M.set (1, 2, 101) After this step, if you write M.get(1,2) you will get the value 101.

3. Matrix Operations

Given two matrices M1 and M2 and a scalar number c, we can define the following operations:

1.Addition - the sum M1 + M2 is a new matrix P where Pi,j = M1i,j + M2i,j , provided that M1 and M2 are both n m matrices.

2.Subtraction - the difference M1 M2 is a new matrix P where Pi,j = M1i,j M2i,j, provided that both M1 and M2 are n m matrices.

3.Scalar Multiplication - the product cM1 is a new matrix P where Pi,j = cM1i,j. 4.Matrix Multiplication (bonus 5 pts) - If M1 is a np matrix, and M2 is a pm matrix,

then M1 M2 is a new nm matrix P wherePi,j = p1(M1i,k M2k,j). 5.Identity Matrix - this operation creates a new identity matrix In, which is a n n square

matrix with 1s on the main diagonal and zero elsewhere. That is, for any element Ii,j, if i = j, then Ii,j is 1, and 0 otherwise.

6.Transpose - given a nm matrix M, the transpose MT of the matrix M is a mn matrix where M T = M . In other words, the rows and columns of M are interchanged to produce MT.

4. Due Date: June 23 of 2017.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

Students also viewed these Databases questions