Compare matrices multiplication - c++

I must multiply a matrix by itself until the matrix in some degree would not be equal to one of the preceding matrices. Then I need to get the values of degrees in which the matrices are equal. The number of rows and columns are equal. The matrix is stored in a two-dimensional array. Values are 0 or 1. What is the best way to check for equality with the previous matrices? I tried to use vector to store matrices:
vector<int[5][5]> m;
but I got an error cannot convert from 'const int [5][5]' to 'int [5][5]'.
Waiting for an advice.

If you can use boost, look at the boost Matrix class:
It seems to be missing an == operator, but it's easy to add:
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace boost::numeric::ublas;
template<typename T>
bool operator==(const matrix<T>& m, const matrix<T>& n)
{
bool returnValue =
(m.size1() == n.size1()) &&
(m.size2() == n.size2());
if (returnValue)
{
for (unsigned int i = 0; returnValue && i < m.size1(); ++i)
{
for (unsigned int j = 0; returnValue && j < m.size2(); ++j)
{
returnValue &= m(i,j) == n(i,j);
}
}
}
return returnValue;
}
And used like so:
int main ()
{
matrix<double> m (3, 3);
for (unsigned int i = 0; i < m.size1(); ++ i)
{
for (unsigned int j = 0; j < m.size2(); ++ j)
{
m (i, j) = 3 * i + j;
}
}
std::cout << m << std::endl;
matrix<double> n (3, 3);
std::cout << (m == n) << std::endl;
std::cout << (m == m) << std::endl;
}
[Code]

If you want to do it with vector, you probably want vector < vector < int > >, i.e. a vector of vectors of ints (i.e. kind of 2-dimensional vector).
vector<int[5][5]> would (if it worked) declare a vector of 2-dimensional 5x5-int-arrays.

Related

What's wrong in this code , it's doing nothing other than taking inputs of n and m

Here in this question the function call is not executing also tell me abut can't I use array instead of vectors here.
if Possible to use array please provide me with code that how to pass arrays to a function in c++
Here in this question the function call is not executing also tell me abut can't I use array instead of vectors here.
if Possible to use array please provide me with code that how to pass arrays to a function in c++
#include <iostream>
#include <vector>
using namespace std;
int recursion(vector<vector<int>> &v, int n, int m)
{
if (n == 0 && m == 0)
{
return v[n][m];
}
int left = v[n][m] + recursion(v, n - 1, m);
int right = v[n][m] + recursion(v, n, m - 1);
return min(left, right);
}
int main()
{
int n, m;
cout << "enter the value of n and m" << endl;
cin >> n >> m;
cout << n << m;
//it's doing nothing after this point.
vector<vector<int>> vec(n, vector<int>(m));
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
vec[i][j] = (i)*m + (j + 1);
}
}
int result = recursion(vec, n, m);
cout << result;
return 0;
}
vec[i][j] = (i)*m + (j + 1);
is out-of-bounds for i = n and j = m. Same problem with calling recursion(vec, n, m);

How can I multiply two matrices if I only store non-zero elements in a list<list<pair<int,double>>>? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So I got my Data structures and algorithms project. I basically need to develop a class Matrix with following atributes:
list<list<pair<int,double>>>, (In which are only non-zero elements of a Matrix. Value of a certain element is stored as the second argument of a pair, and the first argument of a pair represents an actual column in which that element is located in the original Matrix)
vector< int> rows, (rows[i] - Represents an actual row for each list in a list of lists above, respectively)
Actual number of rows,
Actual number of columns.
In my project so far I have developed specific constructors, adding and substracting matrices etc. The only thing that I am not able to do is multiply two instances of a class Matrix, meaning I am not able to develop an operator* for class Matrix.
Explanation of the problem:
Assuming I have the following two instances of class Matrix:
Instance A:
list<list<pair<int,double>>>: {{{10,5},{40,15}},{{50,25}},{{80,35}}}
vector<int>: {2,10,70};
actual number of rows: 100
actual number of columns: 100
Instance B:
list<list<pair<int,double>>>: {{{1,6},{2,5},{3,7}},{{3,4}},{{80,1}}}
vector<int>: {1,2,33};
actual number of rows: 100
actual number of columns: 100
Obviously multiplication is defined for these matrices since the number of columns of matrix A is equal to the number of rows of matrix B.
I am having trouble with finding the correct algorithm for multiplying these matrices.
Can somebody write in short lines what could be one of the algorithms for multiplying these.
The main idea consists in having convenient primitive to set/get the value at position (i, j) of a matrix, its number of rows and its number of column.
For sake of simplicity, I replaced in the following code your top list by a vector (it's more efficient as you have random access to the i-th value):
#include <cassert>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
class Matrix {
private:
vector<list<pair<unsigned, double>>> data;
unsigned m;
unsigned n;
public:
Matrix(unsigned m, unsigned n):
m(m),
n(n),
data(m)
{}
inline unsigned num_rows() const {
return m;
}
inline unsigned num_columns() const {
return n;
}
double get(unsigned i, unsigned j) const {
for (pair<unsigned, double> p : data[i]) {
if (p.first == j) return p.second;
}
return 0.0;
}
void set(unsigned i, unsigned j, double v) {
for (pair<unsigned, double> p : data[i]) {
if (p.first == j) {
p.second = v;
return;
}
}
if (v) {
data[i].push_back(make_pair(j, v));
}
}
};
Matrix operator * (const Matrix & a, const Matrix & b) {
unsigned m = a.num_rows(),
n = b.num_columns(),
k_max = a.num_columns();
assert (a.num_columns() == b.num_rows());
Matrix c(m, n);
for (unsigned i = 0; i < m; i++) {
for (unsigned j = 0; j < n; j++) {
double value = 0;
for (unsigned k = 0; k < k_max; k++) {
value += a.get(i, k) * b.get(k, j);
}
if (value) c.set(i, j, value);
}
}
return c;
}
ostream & operator << (ostream & out, const Matrix & a) {
for (unsigned i = 0; i < a.num_rows(); i++) {
for (unsigned j = 0; j < a.num_columns(); j++) {
out << a.get(i, j) << "\t";
}
out << endl;
}
return out;
}
int main() {
Matrix a(2, 3), b(3, 1);
for (unsigned i = 0; i < a.num_rows(); i++) {
for (unsigned j = 0; j < a.num_columns(); j++) {
a.set(i, j, 10 * i + j);
}
}
for (unsigned i = 0; i < b.num_rows(); i++) {
for (unsigned j = 0; j < b.num_columns(); j++) {
b.set(i, j, 10 * (i + 1));
}
}
Matrix c = a * b;
cout << "a:" << endl << a << endl
<< "b:" << endl << b << endl
<< "c:" << endl << c << endl
;
return 0;
}
Result:
a:
0 1 2
10 11 12
b:
10
20
30
c:
80
680

Matrix multiplication (with different dimensions)

For Math class in school I need to create an application that does something (just anything) with matrices. I decided to create a matrix calculator. I have a Matrix class which contains a 2D array, an row integer and a column integer. I created the following function to multiply two matrices:
public: Matrix* multiply(Matrix* other)
{
Matrix* temp = new Matrix(other->r, other->c);
for(int i = 0; i < this->r; i++)
{
for(int j = 0; j < this->c; j++)
{
for(int k = 0; k < other->c; k++)
temp->mat[i][j] += this->mat[i][k] * other->mat[k][j];
}
}
return temp;
}
This works perfectly, but only if I multiply matrices with the same dimensions (e.g. Mat4x4*Mat4x4 or Mat2x4*Mat2x4). I understand I can't just multiply an Mat4x4 with an Mat9X2 or anything, but I do know the second matrix's columns should be equal to the first matrix's rows (so a Mat2x2 should be able to multiply with a Mat2x1) and that the answer will have the dimensions of the second matrix. How could (or should) I make the function so it will multiply the matrices with the same and with different dimensions?
Thanks in advance
A solution for your program would be to make the temp dimensions not the others dimension but this->r, other->c in order to make the dimensions valid with the outputs from the matrix multiplication.
Hope this helps.
The following code contains a Matrix class implementation meant to show a few features of C++ (like unique pointers, random numbers, and stream formatting). I often use it when I want to explain a little bit about the language. Maybe it can help you.
#include <cassert>
#include <iostream>
#include <iomanip>
#include <memory>
#include <random>
// Pedagogical implementation of matrix type.
class Matrix {
public:
// Create a rows-by-cols matrix filled with random numbers in (-1, 1).
static Matrix Random(std::size_t rows, std::size_t cols) {
Matrix m(rows, cols);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> dis(-1, 1);
for (std::size_t row = 0; row < rows; ++row) {
for (std::size_t col = 0; col < cols; ++col) {
m(row, col) = dis(gen);
}
}
return m;
}
// Build an uninitialized rows-by-cols matrix.
Matrix(std::size_t rows, std::size_t cols)
: m_data { std::make_unique<double[]>(rows * cols) },
m_rows { rows },
m_cols { cols }
{
assert(m_rows > 0);
assert(m_cols > 0);
}
// Return number of rows
std::size_t rows() const { return m_rows; }
// Return number of columns
std::size_t cols() const { return m_cols; }
// Value at (row, col)
double operator()(std::size_t row, std::size_t col) const {
assert(row < rows());
assert(col < cols());
return m_data[row * cols() + col];
}
// Reference to value at (row, col)
double& operator()(std::size_t row, std::size_t col) {
assert(row < rows());
assert(col < cols());
return m_data[row * cols() + col];
}
// Matrix multiply
Matrix operator*(const Matrix& other) const {
assert(cols() == other.rows());
Matrix out(rows(), other.cols());
for (std::size_t i = 0; i < rows(); ++i) {
for (std::size_t j = 0; j < other.cols(); ++j) {
double sum { 0 };
for (std::size_t k = 0; k < cols(); ++k) {
sum += (*this)(i, k) * other(k, j);
}
out(i, j) = sum;
}
}
return out;
}
private:
std::unique_ptr<double[]> m_data; // will cleanup after itself
const std::size_t m_rows;
const std::size_t m_cols;
};
// Pretty-print a matrix
std::ostream& operator<<(std::ostream& os, const Matrix& m) {
os << std::scientific << std::setprecision(16);
for (std::size_t row = 0; row < m.rows(); ++row) {
for (std::size_t col = 0; col < m.cols(); ++col) {
os << std::setw(23) << m(row, col) << " ";
}
os << "\n";
}
return os;
}
int main() {
Matrix A = Matrix::Random(3, 4);
Matrix B = Matrix::Random(4, 2);
std::cout << "A\n" << A
<< "B\n" << B
<< "A * B\n" << (A * B);
}
Possible output:
$ clang++ matmul.cpp -std=c++17 -Ofast -march=native -Wall -Wextra
$ ./a.out
A
1.0367049464391398e-01 7.4917987082978588e-03 -2.7966084757805687e-01 -7.2325095373639048e-01
2.2478938813996119e-01 8.4194832286446353e-01 5.3602376615184033e-01 7.1132727553003439e-01
1.9608747339865196e-01 -6.4829263198209253e-01 -2.7477471919710350e-01 1.2721104074473044e-01
B
-8.5938605801284385e-01 -6.2981285198013204e-01
-6.0333085647033191e-01 -6.8234173530317577e-01
-1.2614486249714407e-01 -3.3875904433100934e-01
-6.9618174970366520e-01 6.6785401241316045e-01
A * B
4.4517888255515814e-01 -4.5869338680118737e-01
-1.2639839804611623e+00 -4.2259184895688506e-01
1.6871952235091500e-01 4.9689953389829533e-01
It turnes out the order of the rows and columns got me heckin' bamboozled. The formula was correct. Sorry for unnecessary post.

operator overloading [][] 2d array c++

I have a 2D array and I want to define a function that returns the value of the index that the user gives me using operator overloading.
In other words:
void MyMatrix::ReturnValue()
{
int row = 0, col = 0;
cout << "Return Value From the last Matrix" << endl;
cout << "----------------------------------" << endl;
cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl;
}
The operation ((*this).matrix)[row][col] should return an int.
I have no idea how to build the operator [][].
Alternatively, I could concatenate a couple of calls to the operator [], but I didn't succeed in it, because the first call to that operaror will return int* and the second one will return int, and it compel to build another operator, and I dont want to do that.
The data matrix is defined like
int** matrix; matrix = new int*[row];
if (matrix == NULL)
{
cout << "Allocation memory - Failed";
}
for (int i = 0; i < row; i++)//Allocation memory
{
matrix[i] = new int[col];
if (matrix[i] == NULL)
{
cout << "Allocation memory - Failed";
return;
}
}
What can I do?
Thank you,
Simply, such an operator does not exist, so you can not overload it.
A possible solution is to define two classes: the Matrix and the Row.
You can define the operator[] of a Matrix so that it returns a Row, then define the same operator for the Row so that it returns an actual value (int or whatever you want, your Matrix could be also a template).
This way, the statement myMatrix[row][col] will be legal and meaningful.
The same can be done in order to assign a new Row to a Matrix or to change a value in a Row.
* EDIT *
As suggested in the comments, also you should take in consideration to use operator() instead of operator[] for such a case.
This way, there wouldn't be anymore the need for a Row class too.
You can define your own operator [] for the class. A straightforward approach can look the following way
#include <iostream>
#include <iomanip>
struct A
{
enum { Rows = 3, Cols = 4 };
int matrix[Rows][Cols];
int ( & operator []( size_t i ) )[Cols]
{
return matrix[i];
}
};
int main()
{
A a;
for ( size_t i = 0; i < a.Rows; i++ )
{
for ( size_t j = 0; j < a.Cols; j++ ) a[i][j] = a.Cols * i + j;
}
for ( size_t i = 0; i < a.Rows; i++ )
{
for ( size_t j = 0; j < a.Cols; j++ ) std::cout << std::setw( 2 ) << a[i][j] << ' ';
std::cout << std::endl;
}
}
The program output is
0 1 2 3
4 5 6 7
8 9 10 11
I have no idea how to build the operator [][].
Sometimes it is fine to use a different operator, namely ():
int& Matrix::operator () (int x, int y)
{
return matrix[x][y];
}
const int& Matrix::operator () (int x, int y) const
{
return matrix[x][y];
}
int diagonal (const Matrix& m, int x)
{
return m (x, x); // Usage.
}
Advantage:
No need to use "intermediate" class like Row or Column.
Better control than with Row& Matrix operator (int); where someone could use the Row reference to drop in a row of, say, illegal length. If Matrix should represent a rectangular thing (image, matrix in Algebra) that's a potential source of error.
Might be less tedious in higher dimensions, because operator[] needs classes for all lower dimensions.
Disadvantage:
Uncommon, different syntax.
No more easy replacement of complete rows / columns, if that's desired. However, replacing columns is not easy, anyway, provided you used rows to model (and vice versa).
In either case, there are pros and cons if the number of dimensions are not known at runtime.
I was looking for self-tested array replacement...
Improved version returns reference or NULL reference and checks boundaries inside.
#include <iostream>
#include <iomanip>
template<typename T, int cols>
class Arr1
{
public:
Arr1(T (&place)[cols]) : me(place) {};
const size_t &Cols = cols;
T &operator [](size_t i)
{
if (i < cols && this != NULL) return me[i];
else {
printf("Out of bounds !\n");
T *crash = NULL;
return *crash;
}
}
private:
T (&me)[cols];
};
template<typename T, int rows, int cols>
class Arr2
{
public:
const size_t &Rows = rows;
const size_t &Cols = cols;
Arr2() {
ret = NULL;
for (size_t i = 0; i < rows; i++) // demo - fill member array
{
for (size_t j = 0; j < cols; j++) matrix[i][j] = cols * i + j;
}
}
~Arr2() {
if (ret) delete ret;
}
Arr1<T, cols>(&operator [](size_t i))
{
if (ret != NULL) delete ret;
if (i < rows) {
ret = new Arr1<T, cols>(matrix[i]);
return *ret;
}
else {
ret = NULL;
printf("Out of bounds !\n");
return *ret;
}
}
//T(&MemberCheck)[rows][cols] = matrix;
private:
T matrix[rows][cols];
Arr1<T, cols> *ret;
};
template<typename T,int rows, int cols>
class Arr
{
public:
const size_t &Rows = rows;
const size_t &Cols = cols;
T(&operator [](size_t i))[cols]
{
if (i < rows) return matrix[i];
else {
printf("Out of bounds !\n");
T(*crash)[cols] = NULL;
return *crash;
}
}
T (&MemberCheck)[rows][cols] = matrix;
private:
T matrix[rows][cols];
};
void main2()
{
std::cout << "Single object version:" << endl;
Arr<int, 3, 4> a;
for (size_t i = 0; i <= a.Rows; i++)
{
int *x = &a[i][0];
if (!x) printf("Fill loop - %i out of bounds...\n", i);
else for (size_t j = 0; j < a.Cols; j++) a[i][j] = a.Cols * i + j;
}
for (size_t i = 0; i < a.Rows; i++)
{
for (size_t j = 0; j <= a.Cols; j++) {
std::cout << std::setw(2) << a[i][j] << ' ';
if (a.MemberCheck[i][j] != a[i][j])
printf("Internal error !");
}
std::cout << std::endl;
}
std::cout << endl << "Double object version:" << endl;
Arr2<int, 3, 4> a2;
for (size_t i = 0; i < a2.Rows; i++)
{
for (size_t j = 0; j <= a2.Cols; j++) {
int &x = a2[i][j];
if (&x)
{
x++;
std::cout << std::setw(2) << a2[i][j] << ' ';
//if (&a2.MemberCheck[i][j] != &a2[i][j])
// printf("Internal error !");
}
}
}
}
Output
Single object version:
Out of bounds !
Fill loop - 3 out of bounds...
0 1 2 3 4
4 5 6 7 8
8 9 10 11 -858993460
Double object version:
1 2 3 4 Out of bounds !
5 6 7 8 Out of bounds !
9 10 11 12 Out of bounds !
it works fine in the program below
#include<iostream>
using namespace std;
class A{
public:
int r,c;
int** val;
A()
{
r=0;c=0;val=NULL;
}
A(int row,int col)
{
r=row;c=col;
int count=0;
val=new int*[row];
for(int i=0;i<r;i++){
val[i]=new int[col];
for(int j=0;j<c;j++){
count++;
val[i][j]=count;
}
}
}
int* &operator[](int index){
return val[index];
}
};
int main(void){
A a(3,3);
cout<<a[1][2];
return 0;
}
here, a[1][2] first computes a[1]-->which returns 2nd row as (int*) type
then it's read as (int*)[2] which returns 3rd element of that row.In short,
a[1][2]------>(a[1])[2]------>(val[1])[2]------>val[1][2].

Gauss Elimination for NxM matrix

/* Program to demonstrate gaussian <strong class="highlight">elimination</strong>
on a set of linear simultaneous equations
*/
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
const double eps = 1.e-15;
/*Preliminary pivoting strategy
Pivoting function
*/
double pivot(vector<vector<double> > &a, vector<double> &b, int i)
{
int n = a.size();
int j=i;
double t=0;
for(int k=i; k<n; k+=1)
{
double aki = fabs(a[k][i]);
if(aki>t)
{
t=aki;
j=k;
}
}
if(j>i)
{
double dummy;
for(int L=0; L<n; L+=1)
{
dummy = a[i][L];
a[i][L]= a[j][L];
a[j][L]= dummy;
}
double temp = b[j];
b[i]=b[j];
b[j]=temp;
}
return a[i][i];
}
/* Forward <strong class="highlight">elimination</strong> */
void triang(vector<vector<double> > &a, vector<double> &b)
{
int n = a.size();
for(int i=0; i<n-1; i+=1)
{
double diag = pivot(a,b,i);
if(fabs(diag)<eps)
{
cout<<"zero det"<<endl;
return;
}
for(int j=i+1; j<n; j+=1)
{
double mult = a[j][i]/diag;
for(int k = i+1; k<n; k+=1)
{
a[j][k]-=mult*a[i][k];
}
b[j]-=mult*b[i];
}
}
}
/*
DOT PRODUCT OF TWO VECTORS
*/
double dotProd(vector<double> &u, vector<double> &v, int k1,int k2)
{
double sum = 0;
for(int i = k1; i <= k2; i += 1)
{
sum += u[i] * v[i];
}
return sum;
}
/*
BACK SUBSTITUTION STEP
*/
void backSubst(vector<vector<double> > &a, vector<double> &b, vector<double> &x)
{
int n = a.size();
for(int i = n-1; i >= 0; i -= 1)
{
x[i] = (b[i] - dotProd(a[i], x, i + 1, n-1))/ a[i][i];
}
}
/*
REFINED GAUSSIAN <strong class="highlight">ELIMINATION</strong> PROCEDURE
*/
void gauss(vector<vector<double> > &a, vector<double> &b, vector<double> &x)
{
triang(a, b);
backSubst(a, b, x);
}
// EXAMPLE MAIN PROGRAM
int main()
{
int n;
cin >> n;
vector<vector<double> > a;
vector<double> x;
vector<double> b;
for (int i = 0; i < n; i++) {
vector<double> temp;
for (int j = 0; j < n; j++) {
int no;
cin >> no;
temp.push_back(no);
}
a.push_back(temp);
b.push_back(0);
x.push_back(0);
}
/*
for (int i = 0; i < n; i++) {
int no;
cin >> no;
b.push_back(no);
x.push_back(0);
}
*/
gauss(a, b, x);
for (size_t i = 0; i < x.size(); i++) {
cout << x[i] << endl;
}
return 0;
}
The above gaussian eleimination algorithm works fine on NxN matrices. But I need it to work on NxM matrix. Can anyone help me to do it? I am not very good at maths. I got this code on some website and i am stuck at it.
(optional) Understand this. Do some examples on paper.
Don't write code for Gaussian elimination yourself. Without some care, the naive gauss pivoting is unstable. You have to scale the lines and take care of pivoting with the greatest element, a starting point is there. Note that this advice holds for most linear algebra algorithms.
If you want to solve systems of equations, LU decomposition, QR decomposition (stabler than LU, but slower), Cholesky decomposition (in the case the system is symmetric) or SVD (in the case the system is not square) are almost always better choices. Gaussian elimination is best for computing determinants however.
Use the algorithms from LAPACK for the problems which need Gaussian elimination (eg. solving systems, or computing determinants). Really. Don't roll your own. Since you are doing C++, you may be interested in Armadillo which takes care of a lot of things for you.
If you must roll your own for pedagogical reasons, have a look first at Numerical Recipes, version 3. Version 2 can be found online for free if you're low on budget / have no access to a library.
As a general advice, don't code algorithms you don't understand.
You just cannot apply Gaussian elimination directly to an NxM problem. If you have more equations than unknowns, the your problem is over-determined and you have no solution, which means you need to use something like the least squares method. Say that you have A*x = b, then instead of having x = inv(A)*b (when N=M), then you have to do x = inv(A^T*A)*A^T*b.
In the case where you have less equations then unknowns, then your problem is underdetermined and you have an infinity of solutions. In that case, you either pick one at random (e.g. setting some of the unknowns to an arbitrary value), or you need to use regularization, which means trying adding some extra constraints.
You can apply echelon reduction, like in this snippet
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
/*
A rectangular matrix is in echelon form(or row echelon form) if it has the following
three properties :
1. All nonzero rows are above any rows of all zeros.
2. Each leading entry of a row is in a column to the right of the leading entry of
the row above it.
3. All entries in a column below a leading entry are zeros.
If a matrix in echelon form satisfies the following additional conditions,
then it is in reduced echelon form(or reduced row echelon form) :
4. The leading entry in each nonzero row is 1.
5. Each leading 1 is the only nonzero entry in its column.
*/
template <typename C> void print(const C& c) {
for (const auto& e : c) {
cout << setw(10) << right << e;
}
cout << endl;
}
template <typename C> void print2(const C& c) {
for (const auto& e : c) {
print(e);
}
cout << endl;
}
// input matrix consists of rows, which are vectors of double
vector<vector<double>> Gauss::Reduce(const vector<vector<double>>& matrix)
{
if (matrix.size() == 0)
throw string("Empty matrix");
auto A{ matrix };
auto mima = minmax_element(A.begin(), A.end(), [](const vector<double>& a, const vector<double>& b) {return a.size() < b.size(); });
auto mi = mima.first - A.begin(), ma = mima.second - A.begin();
if (A[mi].size() != A[ma].size())
throw string("All rows shall have equal length");
size_t height = A.size();
size_t width = A[0].size();
if (width == 0)
throw string("Only empty rows");
for (size_t row = 0; row != height; row++) {
cout << "processing row " << row << endl;
// Search for maximum below current row in column row and move it to current row; skip this step on the last one
size_t col{ row }, maxRow{ 0 };
// find pivot for current row (partial pivoting)
while (col < width)
{
maxRow = distance(A.begin(), max_element(A.begin() + row, A.end(), [col](const vector<double>& rowVectorA, const vector<double>& rowVectorB) {return abs(rowVectorA[col]) < abs(rowVectorB[col]); }));
if (A[maxRow][col] != 0) // nonzero in this row and column or below found
break;
++col;
}
if (col == width) // e.g. in current row and below all entries are zero
break;
if (row != maxRow)
{
swap(A[row], A[maxRow]);
cout << "swapped " << row << " and " << maxRow;
}
cout << " => leading entry in column " << col << endl;
print2(A);
// here col >= row holds; col is the column of the leading entry e.g. first nonzero column in current row
// moreover, all entries to the left and below are zeroed
if (row+1 < height)
cout << "processing column " << col << endl;
// Make in all rows below this one 0 in current column
for (size_t rowBelow = row + 1; rowBelow < height; rowBelow++) {
// subtract product of current row by factor
double factor = A[rowBelow][col] / A[row][col];
cout << "processing row " << rowBelow << " below the current; factor is " << factor << endl;
if (factor == 0)
continue;
for (size_t colRight{ col }; colRight < width; colRight++)
{
auto d = A[rowBelow][colRight] - factor * A[row][colRight];
A[rowBelow][colRight] = abs(d) < DBL_EPSILON ? 0 : d;
}
print(A[rowBelow]);
}
}
// the matrix A is in echelon form now
cout << "matrix in echelon form" << endl;
print2(A);
// reduced echelon form follows (backward phase)
size_t row(height-1);
auto findPivot = [&row, A] () -> size_t {
do
{
auto pos = find_if(A[row].begin(), A[row].end(), [](double d) {return d != 0; });
if (pos != A[row].end())
return pos - A[row].begin();
} while (row-- > 0);
return A[0].size();
};
do
{
auto col = findPivot();
if (col == width)
break;
cout << "processing row " << row << endl;
if (A[row][col] != 1)
{
//scale row row to make element at [row][col] equal one
auto f = 1 / A[row][col];
transform(A[row].begin()+col, A[row].end(), A[row].begin()+col, [f](double d) {return d * f; });
}
auto rowAbove{ row};
while (rowAbove > 0)
{
rowAbove--;
double factor = A[rowAbove][col];
if (abs(factor) > 0)
{
for (auto colAbove{ 0 }; colAbove < width; colAbove++)
{
auto d = A[rowAbove][colAbove] - factor * A[row][colAbove];
A[rowAbove][colAbove] = abs(d) < DBL_EPSILON ? 0 : d;
}
cout << "transformed row " << rowAbove << endl;
print(A[rowAbove]);
}
}
} while (row-- > 0);
return A;
}