A): Programming code for Matrix Addition %%%%%% Using nested loop%%%%%%%
X = [[12,7,3], [4 ,5,6], [7 ,8,9]]
Y = [[5,8,1], [6,7,3], [4,5,9]]
result = [[0,0,0], [0,0,0], [0,0,0]]
%%%%% iterate through rows
for i in range(len(X)):
%%%% iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output:
[17, 15, 4]
[10, 12, 9]
[11, 13, 18]
B):Programming code for Matrix Scalar Multiplication
%%%% product of a matrix
%%%%%Size of given matrix
N = 3
def scalarProductMat( mat, k):
%%%%%scalar element is multiplied
%%%%% by the matrix
for i in range( N):
for j in range( N):
mat[i][j] = mat[i][j] * k
%%%% Driver code
if _name_ == "__main__":
mat = [[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]
k = 4
scalarProductMat(mat, k)
%%%% to display the resultant matrix
print("Scalar Product Matrix is : ")
for i in range(N):
for j in range(N):
print(mat[i][j], end = " ")
Output:
Scalar Product Matrix is :
4 8 12
16 20 24
28 32 36
C)Programming code for Matrix Trace:
import math
%%%% Size of given matrix
MAX = 100;
Returns trace of a matrix of
%%%% size n x n
def findTrace(mat, n):
sum = 0;
for i in range(n):
sum += mat[i][i];
return sum;
%%%%%Driver Code
mat = [[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[3, 3, 3, 3, 3],
[4, 4, 4, 4, 4],
[5, 5, 5, 5, 5]];
print("Trace of Matrix =", findTrace(mat, 5));
print("Normal of Matrix =", findNormal(mat, 5));
Output:
Trace of matrix = 15
D):Programming code for Matrix Transpose:
X = [[12,7], [4 ,5], [3 ,8]]
result = [[0,0,0], [0,0,0]]
%%%%% iterate through rows
for i in range(len(X)):
%%%%iterate through columns
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
Output:
[12, 4, 3]
[7, 5, 8]
E)Programming code forMatrix Multiplication:
%%%%% Matrix of order 3x3
X = [[12,7,3], [4 ,5,6], [7 ,8,9]]
%%%% Matrix of order 3x4
Y = [[5,8,1,2], [6,7,3,0], [4,5,9,1]]
%%%% Result is of order 3x4
result = [[0,0,0,0], [0,0,0,0], [0,0,0,0]]
%%%%%% iterate through rows of X
for i in range(len(X)):
%%%%%iterate through columns of Y
for j in range(len(Y[0])):
%%%% iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Output:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...
Problem: Matrix Implementation Create a class Matrix with the following method stubs for the following methods described: 1. read: reads in a matrix from the command line 2. display: prints the matrix to the command line 3. getRows: returns the number of rows 4. getCols: returns the number of columns 5. set: sets the double value at a particular column/row position 6. get: returns the value stored at a particular column/row position 7. Plus: returns a new Matrix object that...
Recall that if A is an m times n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p. in which case C is an m × q matrix. a. Write a function M-file that takes as input two matrices A and B, and as output produces the product by rows of the two matrices. For instance, if A is 3 times 4 and B is...
Question 1 a. Define the following matrices in a script file (M-file), ? = ( 8 9 10 11; 23 9 16 15 ;11 12 3 6; 1 2 8 9 ) ? = ( 2 21 7 15; 12 4 8 22; 23 9 5 13; 23 4 21 22) ℎ = (4 9 12 15) b. Add suitable lines of codes to the M-file to do the following. Each of the following points should be coded in only...
PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...
Iterators provide a systematic way to access all of the elements in a collection. To define an iterator, we need to add the __iter__() method to the class over which we want to iterate. Consider the following lines of code: odd_numbers = Odds(9) for num in odd_numbers: print(num) This code iterates over all the odd numbers less than or equal to 9 starting from the number 1. It assumes that the Odds class is iterable, i.e., it contains an __iter__()...
Given a matrix with m rows and n columns, m adjacent numbers are chosen from m rows, where two numbers are adjacent to each other if they are directly connected vertically or diagonally and only one number is taken from one row. Design a dynamic programming algorithm to find the smallest sum of these m numbers. For example, given a 3 by 3 matrix 1 2 3 4 5 6 7 0 2 The sum of three numbers 1, 4,...
MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p
× q matrix, then the product C = AB is defined if and only if n =
p, in which case C is an m × q matrix.
5. Recall that if A is an mx n matrix and B is a px q matrix, then the product C-AB is defined if and only if n = p, in which case C is...
Finish each function python 3 LList.py class node(object): """ A version of the Node class with public attributes. This makes the use of node objects a bit more convenient for implementing LList class. Since there are no setters and getters, we use the attributes directly. This is safe because the node class is defined in this module. No one else will use this version of the class. ''' def __init__(self, data, next=None): """ Create a new node for...
Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...
Implement a C++ class to model the mathematical operations of a matrix. Your class should include the following functions. add() which adds two matrices: power() which raises the first matrix to power n: "" which returns true if both matrices are equal. You need to overload the C++ equality operator. A sample run follows. Enter the number of rows: 2 Enter the number of columns: 3 Enter the elements of matrix 1 row by row: 1 5 0 1 3...