Scala regex split by multiple spaces and new line - regex

I went through different regex documents for this but I'm still not getting it. I hope someone will be able to help me.
I have a table like this:
program 1 0 1 1 0 0 0 0 0 0 0 1
stmt_list 2 0 2 2 0 0 0 0 0 0 0 3
stmt 4 0 5 6 0 0 0 0 0 0 0 0
I want to read it from file and store in an array. I did the following:
val source = io.Source.fromFile("file.txt").getLines.toList.mkString.split("\\W+")
And I'm getting output like:
program
1
0
1
1
0
0
0
0
0
0
0
1stmt_list // this is problem, int and string together which I don't want.
2
0
2
2
0
0
0
0
0
0
0
3stmt
4
0
.
.
.
I learned \s matches any space, tab or newline character. But when I tried, I'm getting error on scala error: invalid escape character . I tried bunch of other steps: " +", /\W+/ etc. None is working. I would highly appreciate any help. My goal is to read the file into a 2D array with only string and int values.

Your problem isn't so much the Regex itself, but the fact that you "merge" all the lines into one String (using mkString) instead of operating on each line separately, using map:
val source = Source.fromFile("file.txt")
.getLines.toList // gets a list of file lines
.map(_.split("\\W+").toList) // maps each line into a list
source.foreach(println)
// List(program, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1)
// List(stmt_list, 2, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 3)
// List(stmt, 4, 0, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0)

Related

simple 2D array matrix in c++

im absolut new to cpp, I tried to code a simple 2D array matrix.
Here is what I'm talking about:
#include <iostream>
#include <string>
using namespace std;
void printTable()
{
int tabelle [10][10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
printf("%d ", tabelle[i][j]);
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
printTable();
return 0;
}
This is how it looks like:
1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
And this is how it should look like:
1 2 3 4 5 6 7 8 9 10
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0
Thx for any advise :D
That's not how array assignments work in C++.
Think of 2D arrays as a 1D array of 1D arrays.
int tabelle[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {3, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {5, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {6, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {7, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {8, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {9, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {10, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
your code is working correctly, 2d array is set of one dimension
array.
here 2d array tabelle[10][10]'s first one dimension array tablelle[0] is initialized with elements.
One more thing partially initialized array fills the rest of the element with
zero, that is why all other element are zero.
There are several different methods to initialize your 2D array, with the same result:
int tab[4][4] = {{1,2,3,4},{2},{3},{4}};
int bat[4][4] = {1,2,3,4,2,0,0,0,3,0,0,0,4,0,0,0};
The thing to keep in mind is that this kind of array is automatically allocated on the stack by the compiler and is stored as a linear sequential array in memory (see the second method above).
Also, for C++, the array you create in a function by automatic allocation in this manner has a scope limited to that function; after the function call ends, the array is gone. If you want to create an array in a function and pass it back to the main function for use, it has to be dynamically (heap) allocated; you then pass a pointer to that array back to main, and then you have to manage that memory by hand (assuming you didn't use a smart pointer to make it).
Note: You can also initiate your array as follows, for very large arrays:
myarray[100][100] = {}; //sets all elements to 0
for (int i = 0; i < 100; i++) {
myarray[0][i] = i + 1;
myarray[i][0] = i + 1;
}
Never do this:
myarray[100][100];
As this creates an array full of junk values loaded with whatever random bits happen to have been previously stored in that memory location, and you might forget to overwrite them later.

How to compute right kernel of a matrix with Eigen library?

I've started to implementation of an algorithm with Eigen library. I needed to calculate null space(kernel) of a matrix. I have tried with a cube's matrix that,
0, 0, 1,
0, 1, 0,
1, 0, 0,
-1, 0, 0,
0, 0, -1,
0, -1, 0
Then, I call, its source
A.transposeInPlace();
std::cout << "and after being transposed:\n" << A << std::endl;
FullPivLU<MatrixXf> lu(A);
MatrixXf A_null_space = lu.kernel();
std::cout << "Null space:\n" << A_null_space << std::endl;
A_null_space.transposeInPlace();
std::cout << "Null space Transposed_A:\n" << A_null_space;
I obtain,
0.5 0 -1 1 0 0 0 0 0 0.5
-0.5 0 -0 0 1 0 0 0 0 -0.5
0.5 0 -0 0 0 1 0 0 0 -0.5
0.5 0 -0 0 0 0 1 0 0 0.5
-1 0 1 0 0 0 0 1 0 -1
-0.5 0 1 0 0 0 0 0 1 -0.5
-0.5 1 -0 0 0 0 0 0 0 0.5
But, I realized later on that its right kernel and left kernel is same and seemingly the code snippet calculates left kernel. The code is getting crazy output on the other test case. So, how can be the right kernel be calculated? The link is also to show the difference btw right and left kernels with examples. However, if I remove first line, the output is 0 0 0
Clearly problem of the case is,
MatrixXf A{10, 3};
A <<
1, 0, 1 ,
1, 0, 0 ,
0, 1, 1 ,
0, 1, 0 ,
0, 0, 1 ,
-1, 0, 0 ,
0, 0, -1 ,
0, -1, 1 ,
0, -1, 0 ,
-1, 0, 1;
Its output is expected as,
1 0 0 0 0 0 0 -2 2 1
0 1 0 0 0 0 0 -1 1 1
0 0 1 0 0 0 0 -1 2 0
0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 0 0 -1 1 0
0 0 0 0 0 1 0 1 -1 -1
0 0 0 0 0 0 1 1 -1 0
QR factorization,
HouseholderQR<MatrixXf> qr(A);
cout << "\nQR matrix to compare \n" << qr.matrixQR().transpose();
Then I get,
-1.41421 0 0.414214
-0.707107 -0.707107 -1
-0.707107 0.707107 1
0 0 1
-0.707107 0.707107 0
0.707107 0.707107 0
0.707107 -0.707107 0
-0.707107 0.707107 -1
0 0 -1
1.19209e-07 1.41421 5.96046e-08
#Edit 2, Does Eigen calculate wrongly?
Source
#Edit 3,
I'm really but really confused because both matrix seem right! How come?
As you observed, both matrices are valid right kernels. This is because they correspond to two different basis of the same subspace. To check it, you can reduce the two matrices to the reduced row echelon form (rref function in matlab, or see this online calculator). This transformation is unique and does not change the span defined by the matrix. Your reference kernel basis is already in this form. So all you have to do is to reduce the one returned by Eigen and see that it gives you the same matrix as your reference one.

How to get a cv::Mat to 2 by 2 maximum value mean?

0, 1, 0, 0 ,0, 0
0, 0, 2, 0 ,4, 0
0, 1, 0, 5 ,0, 0
0, 0, 1, 0 ,1, 0
0, 0, 0, 0 ,1, 0
I am using opencv.
Assume that cv: mat data is the same as the above array.
I want to get the average of the maximum of 2 by 2 of the array.
As great as
2, 0
0, 5
It will be.
We will divide this value by four to get the average.
How do you get the maximum of 2 by 2?
use blur filter like
cv::gaussianblur(input_mat,output_mat,cv::size(5,5),0);

Error in inner product of vectors and index

I found a bug in my code and can't figuring out the error. I tried debugging by showing the output of each variable step by step but I can't find my error. Here is what I have and what I want to do:
I have a matrix A:
0000
0101
1010
1111
And I have a matrix B:
10000
21000
30100
41100
20010
21010
40110
41110
30001
41001
30101
41101
40011
41011
40111
41111
The matrix B has 16 rows and 5 coloumns. The matrix A has 4 rows and 4 coloumns. Now I declare a matrix C that has 4 rows and 16 coloumns.
What I want to do is to calculate the inner product of each row from B with a corresponding row from A. With corresponding I mean that the first coloumn of B shoud define the row from A that I want to multiply. So the B matrix has in fact also four-dimensional vectors and the first element corresponds to the row of A. One could say this first coloumn of B is an index for choosing the row of A. Because C++ start counting by zero I substract one for my index. Here is my code:
std::vector< std::vector<int> > C(4, std::vector<int>(16));
std::vector<int> index(4);
std::vector<int> vectorA(4);
std::vector<int> vectorB(4);
for( int y = 0; y < 16; y++)
{
for(int i=0; i<4; ++i){
vectorA[i] = A[ B[y][0]-1 ][i];
}
for( int x = 1; x < 4; x++)
{
vectorB[x -1] = B[y][x];
}
C[ B[y][0] -1][index[ B[y][0] -1] ] = inner_product(vectorA.begin(), vectorA.end(), vectorB.begin(), 0);
index[B[y][0]-1] += 1;
}
This results in my matrix C:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 3 1 2 1 2 2 3 0 0 0 0 0 0 0
The first two rows are correct but row three and four are false.
The correct solution has to be (maybe except of ordering in row 3 and 4):
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
4 3 3 2 3 2 3 2 2 0 0 0 0 0 0 0
Where is my problem? Please help, it drives me crazy :( I tried showing each variable by step but can't find why is it false.
Thanks and greetings.
I have to agree with the other comments: Your code is kind of confusing. You should really simplify the access of vectors by index.
First simple thing you should do is to change the first column of B to be zero-based. All stuff in C++ is zero-based. Adopt it. Do not start adjusting it in your code by substracting one. (This does not gain much simplicity, but is is symptomatic for your code.)
Another source of confusion is that you use the first column of B as an index into A. This might be an implication from the problem you'd like to solve, but it makes things unclear: first column of B has a totally different meaning, always code in a way that objects are seperated by their meaning.
For me the most confusing thing is, that I really do not get what you're up to. With inner product you mean dot product, right? You have 2 sets of vectors you want to calculate the dot product of. This should result in a set of scalars, a 1D vector not a 2D matrix. You do some special stuff with this index vector, which makes the result being a 2D matrix. But you haven't explained the purpose/system behind it. Why do you need a vector for index, not just a scalar??
Vector index is the most ugly/complex part of your code. Without having a clue what you are up to, I would still guess that you find out what is going wrong when you start printing out the full vector index on every iteration and check if it is changing the way you expect.
I don't know what's the rationale behind OP choices, so I can't properly comment the design of code provided, but for what I can understand there are some mistakes with the example input too.
Given A and B matrices as presented, the inner product of the lower rows of A with the corresponding in B is always 0:
B[1] { 2, 1, 0, 0, 0 },
row "2" or A[1] is { 0, 1, 0, 1 } <- B[4] { 2, 0, 0, 1, 0 },
B[5] { 2, 1, 0, 1, 0 },
The same for the succesive row. Only if swapped, the expected output can be obtained and so I did in my code.
vectorA and vectorB and the corresponding copy loops aren't really neccessary and probably are the cause of the wrong output:
for( int x = 1; x < 4; x++)
{ // ^^^^^ this should be <= to reach the last element
vectorB[x -1] = B[y][x];
}
My code, with the updated input and the direct use of A and B is:
#include <iostream>
#include <vector>
#include <numeric>
using vec_t = std::vector<int>; // I assume a C++11 compliant compiler
using mat_t = std::vector<vec_t>;
using std::cout;
int main() {
mat_t A{
{ 0, 0, 0, 0 },
{ 1, 0, 1, 0 }, // <-- those lines are swapped
{ 0, 1, 0, 1 }, // <--
{ 1, 1, 1, 1 }
};
mat_t B{
{ 1, 0, 0, 0, 0 },
{ 2, 1, 0, 0, 0 },
{ 3, 0, 1, 0, 0 },
{ 4, 1, 1, 0, 0 },
{ 2, 0, 0, 1, 0 },
{ 2, 1, 0, 1, 0 },
{ 4, 0, 1, 1, 0 },
{ 4, 1, 1, 1, 0 },
{ 3, 0, 0, 0, 1 },
{ 4, 1, 0, 0, 1 },
{ 3, 0, 1, 0, 1 },
{ 4, 1, 1, 0, 1 },
{ 4, 0, 0, 1, 1 },
{ 4, 1, 0, 1, 1 },
{ 4, 0, 1, 1, 1 },
{ 4, 1, 1, 1, 1 }
};
mat_t C(4, vec_t(16));
vec_t pos(4);
for ( int i = 0; i < 16; ++i )
{
int row = B[i][0] - 1;
int col = pos[row];
int prod = std::inner_product( A[row].begin(), A[row].end(),
++(B[i].begin()), 0 );
// ^^^ skip the first element
C[row][col] = prod;
if ( prod )
++pos[row];
}
for ( auto & r : C )
{
for ( int x : r ) {
cout << ' ' << x;
}
cout << '\n';
}
return 0;
}
The output is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 3 2 3 2 3 3 4 0 0 0 0 0 0 0
I don't know if the ordering of the last row is as expected, but it mimics the logic of OP's code.

How to set width of GDB memory examine (x) or print (p) commands?

I am trying to get a formatted print of a long 2D array, of width 8, of floats. When using the x command, I get the array printed as four-column table:
(gdb) x/16f 0x81000000
0x81000000: 0 0 1 0
0x81000010: 2 0 3 0
0x81000020: 4 0 5 0
0x81000030: 6 0 7 0
When using the p command, I get an unformatted output, the width of the terminal:
(gdb) p/f *(0x81000000)#16
$27 = {0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0}
Required output, something like:
(gdb) x/16f 0x81000000
0x81000000: 0 0 1 0 2 0 3 0
0x81000020: 4 0 5 0 6 0 7 0
or:
(gdb) p/f *(0x81000000)#16
$27 = {0, 0, 1, 0, 2, 0, 3, 0,
4, 0, 5, 0, 6, 0, 7, 0}
Is there a simple way to format the output for a specific width?
Use python scripting:
I think this is pretty close, if pretty obscure:
python print "\n".join(", ".join(gdb.execute('x/f 0x%x' % a, False, True).split()[-1] for a in range(s, s+32, 4)) for s in range(0x81000000, 0x81000040, 32))