Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Expand this code by computing AB^T #include #include struct Matrix{ double *A; int n_rows,n_cols; }; struct Vector{ double *a; int n_dims; }; int GetPosition(int row,

Expand this code by computing AB^T

#include
#include

struct Matrix{
double *A;
int n_rows,n_cols;
};

struct Vector{
double *a;
int n_dims;
};

int GetPosition(int row, int col, int rows){ // useful, as we can always use this function to map a matrix element to where it is stored in our "long-vector"!
int pos = 0;
pos = col * rows + row; // this is the mapping of a matrix element to where this element is stored in the "long-vector"
return pos;
}

void SpecifyMatrix(Matrix &X){ // here, we pass on the address (first row in the RAM) for the structure "B"
int pos = 0;
std::cout << "*** Specify the number of rows and columns for the matrix:" << std::endl;
std::cout << "*** Number of rows : ";
std::cin >> X.n_rows; // -> ask the user for the number of rows
std::cout << "*** Number of columns : ";
std::cin >> X.n_cols; // -> ask the user for the number of columns
X.A = (double*)calloc(X.n_rows*X.n_cols,sizeof(double)); // -> allocate memory for the actual matrix, e.g. if the user specifies n_rows = 4 and n_cols = 3, there are 4*3=12 array elements that have just been allocated
std::cout << "Please specify the matrix elements now, given that it has " << X.n_rows << " rows and " << X.n_cols << " columns" << std::endl;
for (int i = 0; i < X.n_rows; i++){
for (int j = 0; j < X.n_cols; j++){
std::cout << "Specify the element A[" << i+1 << "][" << j+1 << "] = ";
pos = GetPosition(i, j, X.n_rows);
std::cin >> X.A[pos]; // -> the user specifies the matrix values storred in the ith and the jth column
}
}
}

void SpecifyVector(Vector &x){
std::cout << "*** Specify the dimension of the vector:" << std::endl;
std::cout << "*** Dimension : ";
std::cin >> x.n_dims;
x.a = (double*)calloc(x.n_dims,sizeof(double));
std::cout << "Please specify the vector elements now, given that it has " << x.n_dims << " elements" << std::endl;
for (int i = 0; i < x.n_dims; i++){
std::cout << "Specify the " << i+1 << "th element x[" << i+1 << "] = ";
std::cin >> x.a[i];
}
}


void MatrixVectorProduct(Matrix A, Vector x, Vector &y){
double temp;
int pos;
y.n_dims = A.n_rows;
y.a = (double*)calloc(y.n_dims,sizeof(double));
for (int i = 0; i < A.n_rows; i++){ // go through each row
temp = 0;
for (int j = 0; j < A.n_cols; j++){ // pick one element (column element) in the ith row at the time
pos = GetPosition(i,j,A.n_rows);
temp += A.A[pos] * x.a[j];
}
y.a[i] = temp;
}
}

void MatrixProduct(Matrix A, Matrix B, Matrix &C){
if (A.n_cols == B.n_rows){
int pos_A, pos_B, pos_C;
double temp;
C.n_rows = A.n_rows;
C.n_cols = B.n_cols;
C.A = (double*)calloc(C.n_rows*C.n_cols,sizeof(double));
for (int i = 0; i < A.n_rows; i++){
for (int j = 0; j < B.n_cols; j++){
temp = 0;
for (int k = 0; k < A.n_cols; k++){
pos_A = GetPosition(i,k,A.n_rows);
pos_B = GetPosition(j,k,B.n_rows);
temp += A.A[pos_A] * B.A[pos_B];
}
pos_C = GetPosition(i,j,C.n_rows);
C.A[pos_C] = temp;
}
}
}
else
std::cout << "*** ERROR *** - The matrix dimensions are not compatible, as A has " << A.n_cols << " columns and B has " << B.n_rows << " rows!" << std::endl;
}

void DisplayVector(Vector x){
std::cout << "The elements of this vector, containing " << x.n_dims << " elements are as follows:" << std::endl;
for (int i = 0; i < x.n_dims; i++){
std::cout << "x[" << i+1 << "] = " << x.a[i] << std::endl;
}
}

void DisplayMatrix(Matrix X){
int pos;
std::cout << "The elements of this matrix, containing " << X.n_rows << " rows and " << X.n_cols << " columns are as follows:" << std::endl;
for (int i = 0; i < X.n_rows; i++){
for (int j = 0; j < X.n_rows; j++ ){
pos = GetPosition(i,j,X.n_rows);
std::cout << "X[" << i+1 << "][" << j+1 << "] = " << X.A[pos] << "t";
}
std::cout << std::endl;
}
}

void ClearMatrix(Matrix &X){
free(X.A);
}

void ClearVector(Vector &x){
free(x.a);
}

int main() {
Matrix A, B, C;
SpecifyMatrix(A);
SpecifyMatrix(B);
MatrixProduct(A,B,C);
DisplayMatrix(C);
ClearMatrix(A);
ClearMatrix(B);
ClearMatrix(C);
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Answer To expand the code to compute ABTABT we need to modify the MatrixProduct function to handle the transpose of matrix B Heres the modified code i... 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

Global Strategy

Authors: Mike W. Peng

5th Edition

0357512367, 978-0357512364

More Books

Students also viewed these Programming questions

Question

Why has the FCPA not ended corruption in global business?

Answered: 1 week ago

Question

Evaluate the following, accurate to the nearest cent.

Answered: 1 week ago

Question

Evaluate each of the following, accurate to the nearest cent.

Answered: 1 week ago

Question

Evaluate the answers accurate to the cent.

Answered: 1 week ago