Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Super simple matrix -> inverse matrix c++ code

Posted on 2009-02-19
6
Medium Priority
?
4,253 Views
Last Modified: 2013-12-14
I'm trying to write coding to take in a matrix and return it's inverse. So far I've got all the reading in and output coded and an Identity matrix of the same size as the other matrix and  I can get 1s in the main diagonal but where from there? Everywhere I've looked the code involves classes or including math libraries etc, etc. I think I can do this with a couple of nested for loops. Can anyone figure this out?

I've included what I have
0
Comment
Question by:danwoods
  • 4
  • 2
6 Comments
 

Author Comment

by:danwoods
ID: 23684240
I don't see my code, so I'm reposting it...

//***************************Lab 1*************************************
//FILE: matrix.cc
//AUTHOR: Written by Daniel Woodson
//INSTRUCTOR: Dr. Pettey
//COURSE: Discrete Structures
//DUE DATE: Feb 19, 2008
//DESCRIPTION: Program takes in matrix size, then matrix elements.
//             Program returns inverse matrix
//INPUTS:    matrix size,  matrix elements
//OUTPUTS:   inverse matrix
//*********************************************************************

#include <string>
#include <iostream>

using namespace std;

int main(){
      //function declarations

      //variable declarations
      int size;                                 //used to hold the size of the square matrix
      float inMatrix[10][10];            //used to hold the user inputed matrix
      float outMatrix[10][10];           //used to hold the program generated matrix
      float idMatrix[10][10];            //used to hold the an identity matrix
      int row;                           //used to hold the current row
      int col;                           //used to hold the current column
      
      //initalize idMatrix
      for(row = 0; row < size; row++){
            for(col = 0; col < size; col++){
                  if(row == col){
                        idMatrix[row][col] = 1;
                  }//end if
                  else{
                        idMatrix[row][col] = 0;
                  }//end else
            }//end for (col)
      }//end for (row)
      
      //prompt user for size
      cout << "Input the size of the matrix: ";
      //read in size
      cin >> size;
      
      //prompt the user for the matrix
      cout << "Input the " << size << " X " << size << " matrix: " << endl;
      
      //read in matrix
      for(row = 0; row < size; row++){
            for(col = 0; col < size; col++){
                  cin >> inMatrix[row][col];
            }//end for (col)
      }//end for (row)
            
  //perform opperations
  for(row = 0; row < size; row++){
    for(col = 0; col < size; col++){
      //divide all the elements in a row by the element in the rows identity element
      inMatrix[row][col] = (inMatrix[row][col])/(inMatrix[row][row]);
      //and divide the same row of the identity matrix by the same denominator
      idMatrix[row][col] = (idMatrix[row][col])/(inMatrix[row][row]);
    }//end for (col)
  }//end for (row)
 
  //return results
  //Display Message
  cout << "The inverse matrix is:" << endl;
 
  //display inverse matrix
      for(row = 0; row < size; row++){
            for(col = 0; col < size; col++){
                  cout << idMatrix[row][col] << ", ";
    }//end for (col)
    cout << endl;
      }//end for (row)

  return 0;
        
}//end main

//***************************Lab 1*************************************
//FILE: matrix.cc
//AUTHOR: Written by Daniel Woodson
//INSTRUCTOR: Dr. Pettey
//COURSE: Discrete Structures
//DUE DATE: Feb 19, 2008
//DESCRIPTION: Program takes in matrix size, then matrix elements.
//             Program returns inverse matrix
//INPUTS:    matrix size,  matrix elements
//OUTPUTS:   inverse matrix
//*********************************************************************
 
#include <string>
#include <iostream>
 
using namespace std;
 
int main(){
	//function declarations
 
	//variable declarations
	int size;					   //used to hold the size of the square matrix
	float inMatrix[10][10];            //used to hold the user inputed matrix
	float outMatrix[10][10];           //used to hold the program generated matrix
	float idMatrix[10][10];            //used to hold the an identity matrix
	int row;                           //used to hold the current row
	int col;                           //used to hold the current column
	
	//initalize idMatrix
	for(row = 0; row < size; row++){
		for(col = 0; col < size; col++){
			if(row == col){
				idMatrix[row][col] = 1;
			}//end if
			else{
				idMatrix[row][col] = 0;
			}//end else
		}//end for (col)
	}//end for (row)
	
	//prompt user for size
	cout << "Input the size of the matrix: ";
	//read in size
	cin >> size;
	
	//prompt the user for the matrix
	cout << "Input the " << size << " X " << size << " matrix: " << endl;
	
	//read in matrix
	for(row = 0; row < size; row++){
		for(col = 0; col < size; col++){
			cin >> inMatrix[row][col];
		}//end for (col)
	}//end for (row)
		
  //perform opperations
  for(row = 0; row < size; row++){
    for(col = 0; col < size; col++){
      //divide all the elements in a row by the element in the rows identity element
      inMatrix[row][col] = (inMatrix[row][col])/(inMatrix[row][row]);
      //and divide the same row of the identity matrix by the same denominator
      idMatrix[row][col] = (idMatrix[row][col])/(inMatrix[row][row]);
    }//end for (col)
  }//end for (row)
  
  //return results
  //Display Message
  cout << "The inverse matrix is:" << endl;
  
  //display inverse matrix
	for(row = 0; row < size; row++){
		for(col = 0; col < size; col++){
			cout << idMatrix[row][col] << ", ";
    }//end for (col)
    cout << endl;
	}//end for (row)
 
  return 0;
	  
}//end main

Open in new window

0
 

Author Comment

by:danwoods
ID: 23684468
So, the experts are stumped huh? Point value incresed...
0
 

Author Comment

by:danwoods
ID: 23684592
Found a really good explanation at http://www.experts-exchange.com/Programming/Languages/C/Q_24144576.html?sfQueryTermInfo=1+algorithm+c+code+invers+matric

However, it's in c. Anyone feel like translating?

Point value increased
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 53

Accepted Solution

by:
Infinity08 earned 1050 total points
ID: 23684808
>> So, the experts are stumped huh?

You need to give us a little more time than that heh. Not all of us are on-line 24/24 lol.


Now :

You didn't initialize size. You probably want to set it to 10 before you use it to initialize the identity matrix.

Next, you let the user choose a size. You might want to make sure that that size is between 1 and 10. I'll assume the user gives valid input.

Finally, the algorithm you implemented is incomplete. You simply divide all rows by the diagonal value (note that you need to be careful doing that - what if the diagonal value is 0 ?).
The steps you need to add are for making the other items 0 (all items except for the diagonal items), since it seems you're implementing the Gauss-Jordan elimination method :

        http://mathworld.wolfram.com/Gauss-JordanElimination.html

(click through to Gaussian elimination to find the steps you need to add).

There exist implementations for Gauss-Jordan elimination - a simple search on the internet will turn up many ;)
0
 

Author Closing Comment

by:danwoods
ID: 31548879
This is a billed service, actual code would've been great...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23691595
>> This is a billed service, actual code would've been great...

You pay for the right to ask questions. This is not a rent-a-coder like service.
Every expert on this site is a volunteer. We don't get paid for our time, and generally don't have the time to come up with a fully coded solution for you. We do however give advice, and help you wherever needed.

If you need further assistance with this, then I'll be glad to do that. Expecting to get full code however, will usually result in a disappointment for you :) Having more realistic expectations that are in the line of this site is likely to be more rewarding for you and the experts working with you.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question