Question: Programming in C++ Inverse of a Matrix, use 1D array format to represent the Matrix array (do not use 2D array format) Write a program

Programming in C++

Inverse of a Matrix, use 1D array format to represent the Matrix array (do not use 2D array format)

Write a program to invert a 3x3 matrix

2 1 1

3 2 1

2 1 2

Utilizing structured programming write a new module to invert a matrix

A*A^-1= IdentMatrix

Declare two matrices A and InvA ( do not declare ident matrix)

Figure out how to appended matrix C with A and Identity Matrix

Run steps to convert the fist half of matrix into identity matrix to get inverse matrix

Push values back form Matrix C into InvA and Display Matrix InvA similar to what is done above?

Please help with generating this code in C++

I have how to do standard Gauss Jordan, I just don't understand how to make an invers by making an identity matrix. Here is the Code I have.

#include

struct Matrix{ //declaring a structure for Matrix

double *X;

int n_rows, n_columns;

};

struct Vector{ //declaring a structure for Vector

double *x;

int dimension;

};

void DefineVector(Vector &z, int dimension){

z.dimension = dimension;

z.x = (double*)calloc(z.dimension, sizeof(double));

}

void DefineMatrix(Matrix &Z, int rows, int columns){

Z.n_rows = rows;

Z.n_columns = columns;

Z.X = (double*)calloc(Z.n_rows*Z.n_columns, sizeof(double));

}

void SpecifyVectorElements(Vector &z){

for (int i = 0; i < z.dimension; i++){

std::cout <<"specify vector element x[" << i << "] " << std::endl;

std::cin >> z.x[i];

}

}

void DisplayVector(Vector z){

std::cout << "The elements of the vector are" << std::endl;

for (int i = 0; i < z.dimension; i++){

std::cout <<"x[" << i << "] = " << z.x[i] << std::endl;

} //Displays Vector Elements

}

void SpecifyMatrixElements(Matrix &Z){

for (int i = 0; i < Z.n_rows; i++){

for (int k = 0; k < Z.n_columns; k++){

std::cout <<"specify Matrix element X[" << i << "][" << k << "] " <

std::cin >> Z.X[k*Z.n_rows+i];//in takes the Matrix elements

}

}

}

void DisplayMatrix(Matrix Z){

std::cout << "The elements of the Matrix are" << std::endl;

for (int i = 0; i < Z.n_rows; i++){

for (int k = 0; k < Z.n_columns; k++){

std::cout <<"X[" << i << "][" << k << "] = " << Z.X[k*Z.n_rows+i] << "\t"; //displays Matrix elements of a row with tab spacing

}

std::cout << std::endl; //adds a new line for next row elements

}

}

void MatrixVectorProduct(Matrix Z, Vector x, Vector &y){

if (Z.n_columns == x.dimension){

for(int i=0; i < Z.n_rows; i++){

y.x[i] = 0;

for (int k = 0; k < Z.n_columns; k++){

y.x[i] += Z.X[k*Z.n_rows + i] * x.x[k];

}

}

}

else std::cout<<"Matrix-Vector Multiplication not supported due to dimensions mismatch"<

}

void MatrixMatrixProduct(Matrix A, Matrix B, Matrix &Y){

if (A.n_columns == B.n_rows){

for (int i = 0; i < A.n_rows; i++){

for(int j = 0; j < B.n_columns; j++){

Y.X[j*Y.n_rows +i] = 0;

for (int k = 0; k < A.n_columns; k++){ //B.n_rows can also be used

Y.X[j*Y.n_rows +i] += A.X[k*A.n_rows+i]* B.X[k+ j*B.n_rows];

}

}

}

}

else std::cout<<"Matrix-Vector Multiplication not supported due to dimensions mismatch"<

}

void Convert_ith_DiagonalElementToOne(Matrix C, int x){

double cii = 0;

cii = C.X[x*(C.n_rows+1)]; //Diagonal element

for (int i = 0; i < C.n_columns; i++){

C.X[i*C.n_rows+ x] /= cii;

}

}

void ReadRow(Matrix C, Vector &c, int x){

for ( int i = 0; i < C.n_columns; i++){

c.x[i] = C.X[i*C.n_rows + x];

}

}

void convertColumnElements_ij_ToZero_ForwardPath(Matrix &C, int x, int y){

double Cij = 0;

Vector c;

DefineVector(c, C.n_columns);

ReadRow(C, c, x);

Cij = C.X[x*C.n_rows + y];

for (int i = x+1; i< C.n_columns; i++){

C.X[i*C.n_rows +y] -= Cij*c.x[i];

} C.X[x*C.n_rows +y] = 0;

}

void ConverColumElements_ij_ToZero_BackwardPath(Matrix &C, int x, int y){

double Cij = 0;

int size = C.n_columns - C.n_rows;

Cij = C.X[x*C.n_rows +y];

for(int i = 0; C.n_columns; i++){

C.X[C.n_rows*C.n_rows + i *C.n_rows +y] -= Cij*C.X[C.n_rows*C.n_rows + i * C.n_rows +x];

} C.X[x*C.n_rows +y] =0;

}

void GaussJordanElimination(Matrix A, Vector y, Vector &x){

Matrix C;

DefineMatrix (C, A.n_rows, A.n_columns +1);

for (int i = 0; i < A.n_rows; i++ ){

for (int k = 0; k < A.n_columns; k++){

C.X[k*A.n_rows + i] = A.X[k*A.n_rows + i];

}

C.X[A.n_rows*A.n_columns + i] = y.x[i];

}

DisplayMatrix(C);

for (int i =0; i< A.n_rows; i ++){

Convert_ith_DiagonalElementToOne(C, i);

for (int j= i+1; j < A.n_rows; j++){

convertColumnElements_ij_ToZero_ForwardPath(C, i, j);

}

} DisplayMatrix(C);

for (int i = A.n_rows -1; i > 0; i--){

for (int j = i-1; j>= 0; j--){

convertColumnElements_ij_ToZero_ForwardPath(C, i, j);

}

} DisplayMatrix(C);

for (int i = 0; i < A.n_columns; i ++){

for (int j=i -1; j>= 0 ; j--){

ConverColumElements_ij_ToZero_BackwardPath(C, i, j);

}

} DisplayMatrix(C);

for (int i = 0; i < C.n_rows; i++){

x.x[i] = C.X[C.n_rows*C.n_rows+i];

}

}

int main() {

Matrix A, B, Y;

Vector x, y;

int A_rows = 3, A_columns =3;// B_rows = 3, B_columns = 3;

int x_dimension =3;//can ask user for inputs as well

DefineVector (x, A_columns); //DMA for x vector

DefineVector (y, A_rows); //y = A*x //DMA for y vector

DefineMatrix (A, A_rows, A_columns); //DMA for A Matrix

SpecifyMatrixElements(A); //User inputs Matrix A elements

DisplayMatrix (A); // User displays Matrix A elements

SpecifyVectorElements(y);//User inputs vector elements

DisplayVector(y); // Displays vector y

GaussJordanElimination(A, y, x);

DisplayVector(x);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!