Finding all submatrixes of a given matrix - c++

I've got a 2D vector which holds a matrix of integers, which looks like this:
vector<vector<int>> Members;
What I am trying to find is a way on how to extract every possible sub matrix of a NxN matrix.
For example if I had a 2x2 matrix:
0 -2
9 2
It would output:
0
-2
9
2
0
9
-2
2
0 -2
9 2

Sub-Matrix depends on the left-up point and right-bottom point, so you can give all possible locations and print them one by one like this:
//data stored in mat[max][max]
int max=5;//size of matrix
int i,j,m,n;
for(i=0;i<=max-1;i++)
for(j=0;j<=max-1;j++)
for(m=i;m<=max-1;m++)
for(n=j;n<=max-1;j++)
print(i,j,m,n);//a simple function

Related

Armadillo C++ : Linear Combination with modulus calculations

I want to extract linear combinations from matrices but by performing combinations in modulus.
Let us consider the calculation modulus 5, we then have the following for the addition:
+ | 0 1 2 3 4
--+-----------
0 | 0 1 2 3 4
1 | 1 2 3 4 0
2 | 2 3 4 0 1
3 | 3 4 0 1 2
4 | 4 0 1 2 3
and this table for the multiplication:
* | 0 1 2 3 4
--+-----------
0 | 0 0 0 0 0
1 | 0 1 2 3 4
2 | 0 2 4 1 3
3 | 0 3 1 4 2
4 | 0 4 3 2 1
So let us take an example:
Let us consider the following matrix:
E = 2 1 3 2 0
4 3 0 1 1
Then we can obtain the triangulation matrix by applying a LU decomposition (https://en.wikipedia.org/wiki/LU_decomposition) (or a Gaussian Elimination), which is the following:
T = 1 0 0 0 0
2 1 0 0 0
and finally, the matrix that I want to extract, which is the one storing the linear combinations:
CL = 3 2 3 0 3
0 1 1 3 4
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
So basically, the algorithm should work as follows:
Input: a matrix E with n rows and m columns, and p, a prime number.
* We perform a Gaussian elimination/LU decomposition to obtain the lower-triangulation matrix T.
But all the calculus are done modulo 'p'.
Output: T (with the same size as E, n rows m columns).
CL (with a size m rows, m columns),
which is basically the identity matrix on which we
applied all the modifications that were performed on E to obtain T.
Alright, so now we have the context, let me explain the problem.
I started to do it using the Armadillo library (http://arma.sourceforge.net/), but I did not find any solution on the library to perform the calculus on a mathematical Field p. I easily found the LU method to obtain the lower-triangle matrix, but the calculations are performed in the real.
#include <iostream>
#include <armadillo>
using namespace arma;
using namespace std;
int main(int argc,char** argv)
{
mat A = mat({{2,1,3,2,0},{4,3,0,1,1}});
mat L, U, P;
lu(L, U, P, A);
cout << L << endl;
return 0;
}
With the following, you obtain the lower-triangle matrix 'L' but in the real calculus. Thus you obtain:
T' = 1 0
1/2 1
Is there any technique to perform the computation in a modulus way?
EDIT The Armadillo library is not able to do it. I developed my own LU decomposition in modulus but there is still a bug there. I asked a new question here Linear Combination C++ in modulus, hoping to solve it.
First of all: drop the using namespaces, code can become completely unreadable if you do that.
I haven't used Armadillo yet. But I have looked at the documentation, and it seems templated to me.
Now things are getting a bit wild. The type you use, arma::mat seems to be a typedef on arma::Mat<double>.
The high-level function arma::lu isn't properly documented. It obviously does an LU-decomposition, but I don't know if the function is templated. If it is, i.e., you cannot just call it with double mats but also other types, you might have a shot using a custom type representing the field (since 5 is prime, otherwise you'd be completely lost) of calculations modulo 5. Meaning you write a class, let's call it IntMod5 and define all required operators for this class, meaning all operators that IntMod5 uses. For example, you'd need to define operator/(), e.g. by making a table of inverses of 4 of the 5 elements of the field (0 has none), i.e. 1->1, 2->3, 3->2, 4->4, and define
IntMod5 operator/(const IntMod5& o) const
{
return IntMod5((this->value*inverse(o.value))%5);
}
This is just one example, you likely need to define all arithmetic operators, binary and unary, and possibly more such as comparison (LU decomposition might use finding good pivot elements). If you're lucky and the library is written in a way that it works for any field, not just floating point, you have a chance.
Before you go through all the work, you should use a trivial class simply wrapping double and check if arma::Mat or arma::lu do any type checks blocking you out.
If either of these fails, you'll likely have to write your own LU decomposition modulo 5 or find another library that supports it.

Is there a way to insert row/column in matrix (Eigen C++)?

I am using the Eigen library in C++.
I need to insert a row and column to an existing matrix at specific index.
For example, say I need to insert a 0 row and 0 column at the 2nd index...
ORIGINAL MATRIX (A)
1 2 3
1 2 3
1 2 3
NEW MATRIX (B)
1 2 0 3
1 2 0 3
0 0 0 0
1 2 0 3
Thanks for the help in advance!
The new matrix B can be constructed from the original matrix A by using the block operations .topRows() and .bottomRows():
MatrixXd B = MatrixXd::Zero(4, 3);
B.topRows(2) = A.topRows(2);
B.bottomRows(1) = A.bottomRows(1);
This will insert a row of zeros between the second and third row. Analogous operations with .rightCols() and .leftCols() can be used to insert a column of zeros.

How to rotate a matrix in counterclockwise

I use a 5*5 matrix as a moving Marine team. Input an ā€œLā€, the matrix which has been rotated in counterclockwise way. And input the second ā€œLā€, the matrix has been rotated again. Implement it with 2-dimension array.
Input: We will give you 5 rows of numbers, and for each row there will have 5 numbers. Input is between 1~9, reuse and separated by a space.
Output: Output the final matrix which has been rotated in counterclockwise, when you got the EOF (End of File). Each number has to be separated by a space.
For 3 rows as example
5 9 8
7 2 1
4 3 6
change to :
8 1 6
9 2 3
5 7 4

Explore a matrix using Chebyshev distance

What is the fastest way of exploring an array from a point (i,j) , using Chebysev distance?
My aproach:
I am currently defining 2 one dimensional arrays that store the directions for the start then compute with a For what is left when radius > 1 ( radius is the "radius" of the chebysev circle I wanna explore the array) . I am finding I am exploring some elements twice . Is there an algorithm that shows what is the best aproach ?
Be 0 the distance between (i,j) and himself . I would want the matrix to be explored like this ( the numbers represent the distance between i,j and them). Ofcourse i,j is not always it the middle , it must be any point I choose .
2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2
Thank you and excuse my english :)
You can use the BFS algorithm. It is just a simple loop with a queue.
Your "edges" on are links between the position (i, j) and its 8 neighbors:
(i-1,j)
(i-1,j-1)
...
(i+1,j+1)

Transposing a sparse matrix using linked lists (Traversal problems)

I'm trying to transpose a sparse matrix in c++. I'm struggling with the traversal of the new transposed matrix. I want to enter everything from the first row of the matrix to the first column of the new matrix.
Each row has the column index the number should be in and the number itself.
Input:
colInd num colInd num colInd num
Input:
1 1 2 2 3 3
1 4 2 5 3 6
1 7 2 8 3 9
Output:
1 1 2 4 3 7
1 2 2 5 3 8
1 3 2 6 3 9
How do I make the list traverse down the first column inserting the first element as it goes then go back to the top inserting down the second column. Apologies if this is two hard to follow. But all I want help with is traversing the Transposed matrix to be in the right place at the right time inserting a nz(non zero) object in the right place.
Here is my code
list<singleRow> tran;
//Finshed reading so transpose
for (int i = 0; i < rows.size(); i++){ // Initialize transposed matrix
singleRow trow;
tran.push_back(trow);
}
list<singleRow>::const_iterator rit;
list<singleRow>::const_iterator trowit;
int rowind;
for (rit = rows.begin(), rowind = 1; rit != rows.end(); rit++, rowind++){//rit = row iterator
singleRow row = *rit;
singleRow::const_iterator nzit;
trowit = tran.begin(); //Start at the beginning of the list of rows
trow = *trowit;
for (nzit = row.begin(); nzit != row.end(); nzit++){//nzit = non zero iterator
int col = nzit->getCol();
double val = nzit->getVal();
trow.push_back(nz(rowind,val)); //How do I attach this to tran so that it goes in the right place?
trowit++;
}
}
Your representation of the matrix is inefficient: it doesn't use the fact that the matrix is sparse. I say so because it includes all the rows of the matrix, even if most of them are zero (empty), like it usually happens with sparse matrices.
Your representation is also hard to work with. So i suggest converting the representation first (to a regular 2-D array), transposing the matrix, and convert back.
(Edited:)
Alternatively, you can change the representation, for example, like this:
Input: rowInd colInd num
1 1 1
1 2 2
1 2 3
2 1 4
2 2 5
2 3 6
3 1 7
3 2 8
3 3 9
Output:
1 1 1
2 1 2
3 1 3
1 2 4
2 2 5
3 2 6
1 3 7
2 3 8
3 3 9
The code would be something like this:
struct singleElement {int row, col; double val;};
list<singleElement> matrix_input, matrix_output;
...
// Read input matrix from file or some such
list<singleElement>::const_iterator i;
for (i = matrix_input.begin(); i != matrix_input.end(); ++i)
{
singleElement e = *i;
std::swap(e.row, e.col);
matrix_output.push_back(e);
}
Your choice of list-of-list representation for a sparse matrix is poor for transposition. Sometimes, when considering algorithms and data structures, the best thing to do is to take the hit for transforming your data structure into one better suited for your algorithm than to mangle your algorithm to work with the wrong data structure.
In this case you could, for example, read your matrix into a coordinate list representation which would be very easy to transpose, then write into whatever representation you like. If space is a challenge, then you might need to do this chunk by chunk, allocating new columns in your target representation 1 by 1 and deallocating columns in your old representation as you go.