Solve Matrix w/ C++: Row-Echelon Form

  • C/C++
  • Thread starter greeniguana00
  • Start date
  • Tags
    C++ Matrix
In summary, the conversation is about a code that helps to solve a matrix by getting it into row-echelon form. The code avoids global variables and is designed to be expandable. The code is posted on the internet, but the expert recommends using a more established library like GSL for such tasks.
  • #1
greeniguana00
53
0
I just thought I'd post this here. It will help you solve a matrix by getting it into row-echelon form (well, close to it anyway). I avoided global variables and tried to make it as expandable as possible.

Code:
#include <iostream>
using namespace std;

void output(int height, int width, long int * matrix)
{
	cout<<"__________________"<<endl;
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int row_temp;
	int column_temp;
	for (row_temp=0;row_temp<height;row_temp++) {
		for (column_temp=0;column_temp<width;column_temp++) {
			cout<<matrix2[row_temp][column_temp]<<" ";
		}
		cout<<endl;
	}
}

void switch_rows(int row1, int row2, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int temp;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		temp=matrix2[row1][column_temp];
		matrix2[row1][column_temp]=matrix2[row2][column_temp];
		matrix2[row2][column_temp]=temp;
	}
}

void multiply_row(int row, long int multiple, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		matrix2[row][column_temp]=matrix2[row][column_temp]*multiple;
	}
}

void add_rows(int row1, int row2, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int temp;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		matrix2[row1][column_temp]=matrix2[row1][column_temp]+matrix2[row2][column_temp];
	}
}

void add_multiple_of_row(int row1, int row2, long int multiple, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int temp;
	int column_temp;
	for (column_temp=0;column_temp<width;column_temp++) {
		matrix2[row1][column_temp]=matrix2[row1][column_temp]+(matrix2[row2][column_temp]*multiple);
	}
}

int reduce_column(int column, int block_rows_until, int height, int width, long int * matrix)
{
	long int (*matrix2)[width];
	matrix2=(long int (*)[width]) matrix;
	int row_temp;
	int column_temp;
	int temp;
	int selected_row=-1;
	for (row_temp=(height-1);row_temp>=block_rows_until;row_temp--) {
		if (matrix2[row_temp][column]!=0) {
			selected_row=row_temp;
		}
	}
	if (selected_row==-1) {
		return block_rows_until;
	}
	switch_rows(block_rows_until, selected_row, width, matrix);
	for (row_temp=block_rows_until+1;row_temp<height;row_temp++) {
		if (matrix2[row_temp][column]!=0) {			
			output(height, width, matrix);
			temp=matrix2[row_temp][column];
			multiply_row(row_temp, matrix2[block_rows_until][column], width, matrix);
			output(height, width, matrix);
			add_multiple_of_row(row_temp, block_rows_until, -(temp), width, matrix);
		}
	}
	block_rows_until++;
	return block_rows_until;
	
}

int main()
{
	int width;
	int height;
	cout<<"Height? ";
	cin>>height;
	cout<<"Width? ";
	cin>>width;
	cout<<"Enter in the matrix row by row with entries separated by space:"<<endl;
	long int matrix[height][width];
	int row_temp;
	int column_temp;
	for (row_temp=0;row_temp<height;row_temp++) {
        	for (column_temp=0;column_temp<width;column_temp++) {
			cin>>matrix[row_temp][column_temp];
		}
	}
	long int * matrix_transfer;
	matrix_transfer=(long int *) matrix;
	int current_row=0;
	int current_column=0;
	int solved=0;
	while (solved!=1) {
		current_row=reduce_column(current_column, current_row, height, width, matrix_transfer);
		current_column++;
		if (current_row==(height-1)) {solved=1;}
		if (current_column==(width-1)) {solved=1;}
	}
	output(height, width, matrix_transfer);
	return 0;
}
 
Technology news on Phys.org
  • #2
I'm not sure I would use such code posted randomly on the internet. As this code been rigorously tested?

I would rather use an established library, such as GSL.
https://www.gnu.org/software/gsl/
 

What is a matrix in C++?

A matrix in C++ is a two-dimensional array with rows and columns. It can be used to represent data in a tabular format, and it is often used in mathematical operations such as solving systems of equations.

What is the purpose of converting a matrix into row-echelon form?

The purpose of converting a matrix into row-echelon form is to simplify the matrix and make it easier to solve. This form eliminates any zeros below the leading coefficient in each row, making it easier to identify the solutions to the system of equations represented by the matrix.

How can I convert a matrix into row-echelon form in C++?

To convert a matrix into row-echelon form in C++, you can use the Gauss-Jordan elimination method. This involves performing elementary row operations such as swapping rows, multiplying rows by a scalar, and adding multiples of one row to another until the matrix is in row-echelon form.

What are the benefits of using C++ to solve a matrix in row-echelon form?

C++ is a high-level programming language that is known for its speed and efficiency. Using C++ to solve a matrix in row-echelon form allows for faster computation compared to other languages. It also provides more control and flexibility in the coding process, allowing for customization and optimization of the solution.

Are there any limitations to using C++ to solve a matrix in row-echelon form?

One limitation of using C++ to solve a matrix in row-echelon form is that it requires some knowledge of programming and algorithms. This may be a barrier for those who are not familiar with C++ or coding in general. Additionally, C++ does not have built-in functions for matrix operations, so the code must be written from scratch or using external libraries.

Similar threads

  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
25
Views
1K
  • Programming and Computer Science
Replies
3
Views
702
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
Back
Top