How to generate this matrix with armadillo in c++? - c++

I would like to generate a matrix in C ++ using armadillo that behaves like a "truth table", for example:
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
I was thinking of a cycle of this kind, but I'm not very practical with armadillo and its data structures.
imat A = zeros<imat>(8, 3);
/* fill each row */
for(int i=0; i < 8; i++)
{
A.row(i) = (i/(pow(2, i)))%2 * ones<ivec>(3).t(); //
}
cout << "A = \n" << A << endl;
Any ideas?

If you need a large size truth table matrix (~2^30 x 30) as you said here, from the memory point of view, you should implement a function which quickly calculates the values you want rather than storing them on a matrix.
This is easily done using std::bitset as follows.
Note that N must be determined at compile-time in this method.
Then you can get the value of your A(i,j) by matrix<3>(i,j):
DEMO
#include <bitset>
template <std::size_t N>
std::size_t matrix(std::size_t i, std::size_t j)
{
return std::bitset<N>(i)[N-j-1];
}

Related

How to resize 2d vector

#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<vector<int> > matrix;
matrix.resize(3, vector<int>(4, 1));
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 4; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
matrix.resize(5, vector<int>(7, 0));
for (size_t i = 0; i < 5; i++)
{
for (size_t j = 0; j < 7; j++)
{
cout << matrix[i][j];
}
cout << endl;
}
return 0;
}
'''
As far as I know, when we are resizing a vector using "resize()" over than original capacity, values in original space will remain and new values are assigned to new space.
In the line matrix.resize(5, vector(7, 0)); If we execute that line I thought matrix would be like
1111000
1111000
1111000
0000000
0000000
something like this.
But the programs stops,
I want to know why it won't working.
matrix.resize(5, vector<int>(7, 0));
only add new vector of size 7 not modifying actual vector.
Just resize actual vectors to 7 with:
for (auto &row: matrix) row.resize(7);
so now is working:
1111000
1111000
1111000
0000000
0000000
i tested your codes using an online compiler https://onlinegdb.com/BkwcGuAAD
your columns are not resized (only the rows are resized). running your current code yields
1 1 1 1 0 0 33
1 1 1 1 0 0 33
1 1 1 1 0 0 49 << notice some columns have random numbers?
0 0 0 0 0 0 0
0 0 0 0 0 0 0
try resizing the columns too
matrix[0].resize(7,0);
matrix[1].resize(7,0);
matrix[2].resize(7,0);
matrix.resize(5, vector<int>(7, 0));
you should get something like the following
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
By the C++ Reference
(http://www.cplusplus.com/reference/vector/vector/resize/)
void resize (size_type n);
void resize (size_type n, const value_type& val);
Resizes the container so that it contains n elements.
If n is smaller than the current container size, the content is reduced to its first n elements, removing those beyond (and destroying them).
If n is greater than the current container size, the content is expanded by inserting at the end as many elements as needed to reach a size of n. If val is specified, the new elements are initialized as copies of val, otherwise, they are value-initialized.
If n is also greater than the current container capacity, an automatic reallocation of the allocated storage space takes place.
Notice that this function changes the actual content of the container by inserting or erasing elements from it.
I thought Compiler will understand if I put more longer vector inside of "val".
That is, I thought it would understand this kind of increased "n".
But Compiler will only watch whether "n" parameter itself is changed or not.
Because of this reason, my code wouldn't work properly.
if you want to increase size of vector using size() function, note that you should resize original row values on your hand.

c++11 using std::fill to fill a 2D dynamically allocated array

A discussion along this line could be found in this question and also in here, but my case is slightly different, as I am dealing with a dynamically allocated memory.
also please note, memset does not quite work with double value.
Anyway, I am trying to use std::fill to fill a dynamically allocated 2D array --
#include <iostream>
#include <algorithm>
using std::cout ; using std::endl ;
using std::fill ;
int main()
{
double **data ;
int row = 10, col = 10 ;
data = new double*[row] ;
for(int i = 0 ; i < col ; i++)
data[i] = new double[col];
// fill(&(data[0][0]),
// &(data[0][0]) + (row * col * sizeof(double)), 1); // approach 1
// fill(&(data[0][0]), &(data[0][0]) + (row * col), 1); // approach 2
// fill(data, data + (row * col * sizeof(double)), 1); // approach 3
// fill(&(data[0][0]),
// &(data[0][0]) +
// ((row * sizeof(double*)) +
// (col * sizeof(double))), 1); // approach 4
for(int i = 0 ; i < row ; i++) {
for(int j = 0 ; j < col ; j++)
cout << data[i][j] << " " ;
cout << endl ;
}
for(int i = 0 ; i < row ; i++)
delete [] data[i] ;
delete [] data ;
}
Approach 1: What I understand, the approach 1 should be the correct code, I am starting from the beginning &(data[0][0]) and the end of the array should be located at &(data[0][0]) + (row * col * sizeof(double)), but when I run, I get this error, but the array has been filled --
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
*** Error in `./test': free(): invalid next size (fast): 0x0000000000da3070 ***
Aborted (core dumped)
Approrach 2: However, according to this post, the approach 2 is recommended, but I do not quite understand this code, since sizeof(double) is missing, and I am getting this output --
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
*** Error in `./test': free(): invalid next size (fast): 0x0000000000bf5070 ***
Aborted (core dumped)
Approach 3: I am not sure why this does not compile, data and &(data[0][0]) should have the same meaning, right?
Approach 4: I am not sure if this is correct or not.
How do I do it ?
Does std::fill give any extra benefit over two nested loops?
Unlike a stack allocated 2D array, a dynamic 2D array is not guaranteed to be a contiguous range. It is however a contiguous range of pointers, but each pointer in the array may point to non-contiguous memory areas. In other words, the first element of data + i + 1 may not necessarily follow the last element of the array pointed by data + i. If you wonder why a stack-allocated 2D array is contiguous, it is because when you declare something like
double data[10][20];
then the compiler understands it as array of 10 (contiguous) elements, each element of type double[20]. The latter type is also an array, which guarantees contiguous elements, so the double[20] elements (i.e. 20 double one after the other) are stacked one after the other in the memory. double[10][20] is is strikingly different from double**.
That's why std::fill or std::memset gives you headaches, as they both assume a contiguous range. Therefore in your case a nested loop seems to be the simplest way of filling the array.
In general, it is much better to use a 1D array in which you "mimic" the 2D access, exactly for the reasons mentioned above: data locality. Data locality implies fewer cache missed and better overall performance.
Pointer arithmetic requires that a pointer be incremented only to the extent that the result still points at the same array (or one past the end).
You allocate each row as a separate array in your for loop:
for(int i = 0 ; i < col ; i++)
data[i] = new double[col]; // this creates a distinct array for each row
Since each row array you allocate is col elements, the maximum value that can legally be added to &(data[0][0]) is col. But in each of your examples of std::fill usage you add more to the pointer than you are allowed.
Given the way you are allocating the array, there's no way for you to pass raw pointers to a single call to std::fill in order to initialize the entire 2D array. Either you must use more than one call to std::fill (which defeats the purpose of using std::fill), or you must create an Iterator type that knows how to deal with the separate allocation of each row, or you must change the way you allocate the array.
I would recommend allocating the whole array at once as a single dimensional array and then writing some additional code to make it work like a two dimensional array. This has a number of benefits:
The standard library contains a convenient way of dynamically allocating a single dimensional arrays: std::vector
Using std::vector means you no longer need to use naked new and delete, which fixes the exception safety problem your code has.
A single allocation generally has better performance characteristics than many allocations (Of course, there are cases where separate allocations are better).
Here's a simple wrapper class to make a 1D array look like a 2D array:
class Matrix {
std::vector<double> data;
int cols;
public:
Matrix(int row, int col)
: data(row * col)
, cols(col)
{}
auto begin() { return data.begin(); }
auto end() { return data.end(); }
struct index_type { int row; int col; };
double &operator[](index_type i) {
return data[i.row * cols + i.col];
}
int row_count() const { return data.size()/cols; }
int column_count() const { return cols; }
};
Using this you can rewrite your code:
#include "Matrix.h"
#include <iostream>
#include <algorithm>
using std::cout ; using std::endl ;
using std::fill ;
int main()
{
Matrix data(10, 10);
fill(data.begin(), data.end(), 1.0);
for(int i = 0 ; i < data.row_count() ; i++) {
for(int j = 0 ; j < data.column_count() ; j++)
cout << data[{i, j}] << " " ;
cout << endl ;
}
}
Does std::fill give any extra benefit over two nested loops?
Using loops is less readable because loops could do lots of other things, and you have to spend more time figuring out what any particular loop is doing. For this reason one should always prefer using STL algorithms over manual loops, all else being equal.
// fill(&(data[0][0]),
// &(data[0][0]) + (row * col * sizeof(double)), 1); // approach 1
Pointer arithmetic automatically considers the size of the array elements. You don't need sizeof(double). Multiplying by sizeof(double) here is the same as multiplying by sizeof(double) inside []. You wouldn't do: data[i * sizeof(double)], so don't do data + (i * sizeof(double)).
Your example code uses &(data[0][0]). Think about if this the same or different from data[0]. Consider both the type and the value of the expressions.
I agree with the above comments. You have allocated 10 separate arrays so you can't initialize these with a single std::fill call.
Moreover, when you perform arithmetic operations on pointer of non-void types, a compiler automatically multiply your results by sizeof of a given type. However, when you use functions like memset or memcpy, you actually have to multiply number of elements by sizeof of a given types and pass it to one of these functions. It's because these function operate on bytes and they accept pointers of void type. Therefore it is impossible for compiler to take care of adjusting of sizes, because the void type has no specified size.

C++ - Can not access reverse indexing

Please forgive me if the title is wrong, or, does not explain the problem correctly.
I'm trying to calculate the LU Decomposition of a matrix. Give the matrix M:
M = [1 2 3],
[1 2 3],
[3 3 0]
Now I'm following some code that was written here: Link
It uses j i in order to calculate the lower decomposition. When I try this approach, I keep getting a "Runtime error time: 0 memory: 3276 signal:11"
The thing that I do not understand is the fact that, when I output i, j inside of the 3x3 loop, I got:
i, j =
0 0 1 0 2 0
0 1 1 1 2 1
0 2 1 2 2 2
Then when output the indexes of j,i I get:
j, i =
0 0 0 1 0 2
1 0 1 1 1 2
2 0 2 1 2 2
Which seems right in terms of the order [0][0][0][2].........[2][2] so why can I not access this, inside my vector?
The code that I have written so far is:
void lu_decomposition(const std::vector<std::vector<double> > &vals)
{
std::vector<std::vector<double> > lower(3);
for(unsigned i=0; (i < 3); i++)
{
lower[i].resize(3);
for(unsigned j=0; (j < 3); j++)
{
if (j < i)
{
lower[j][i] = 0; // This is ok
}else{
lower[j][i] = vals[j][i]; // This is the runtime error
}
}
std::cout << std::endl;
}
}
The code is also visible on ideone
Let's concentrate on these lines:
lower[i].resize(3);
//...
lower[j][i] = vals[j][i];
Is the problem clear now? You're resizing lower[i], but then you're accessing lower[j] even for j > i, which has not been resized yet. If you need to access the matrix in this fashion, you'll have to preallocate it beforehand. That means to drop the resize call and instead initialise the variable like this:
std::vector<std::vector<double> > lower(3, std::vector<double>(3));

CUSP sparse library giving strange results

I'm trying to do the following in CUSP:
A=[
1,1,0,0;
2,2,2,0;
0,3,3,3;
0,0,4,4];
B=[1,1,1,1]';
disp(mldivide(A,B));
which is
X=[0.9167,0.0833,-0.5000,0.7500]
On the other hand I get a strange answer from CUSP
#include <cusp/dia_matrix.h>
#include <cusp/krylov/cg.h>
#include <cusp/print.h>
int main()
{
cusp::dia_matrix<int,float,cusp::host_memory> A(4,4,10,3);
A.diagonal_offsets[0] = -1;
A.diagonal_offsets[1] = 0;
A.diagonal_offsets[2] = 1;
for (int i = 0;i <3;i++)
{
for (int q = 0 ;q < A.num_cols;q++)
{
A.values(q,i)=q+1;
}
}
//copy
cusp::dia_matrix<int,float,cusp::device_memory> AA = A;
cusp::array1d<float,cusp::device_memory> BB(A.num_rows,1);
cusp::array1d<float,cusp::device_memory> XX(A.num_rows,0);
cusp::print(AA);
cusp::print(XX);
cusp::print(BB);
cusp::krylov::cg(AA,XX,BB);\
cusp::print(XX);
return 0;
}
The result looks like
sparse matrix <4, 4> with 10 entries
0 0 1
0 1 1
1 0 2
1 1 2
1 2 2
2 1 3
2 2 3
2 3 3
3 2 4
3 3 4
array1d <4>
0
0
0
0
array1d <4>
1
1
1
1
array1d <4>
-39.9938
-53.436
87.9025
-30.1429
The last one doesn't look quite right. Anybody know what I'm doing wrong? Am I using the code wrong or are we supposed to have a really good guessed solution + use a preconditioner?
The conjugate gradient method is only valid for use in symmetric positive definite matrices. Your matrix isn't symmetric. That is why it isn't (and cannot) producing a valid solution. Either use an appropriate, well conditioned SPD matrix, or use a different numerical method.

Why does it cout in something that we doesn't cin in arrays?

When the following program is fead the following input (reading from cin):
1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1
The output is surprising:
1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1
#include<iostream>
using namespace std;
int main()
{
int arey[3][3];
int i,j;
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
cin>>arey[j][i];
}
}
arey[0][0]=1;
arey[3][3]=1;
i=0,j=0;
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
cout<<arey[j][i];
}
}
return 0;
}
Can someone explain what I should change to get the same output as the input?
Is the matrix 3x3 or 4x4?
you created 3x3 but the loops run for 4 elements and you also update [3][3]
Basically your indexes overflow and you overwrite a different cell in the matrix.
Update: cheecked your input, use: int arey[4][4];
Arrays use 0 based indices, so the valid range of indices for your
int arey[3][3];
are 0 <= i < 3 and 0 <= j < 3
So you need to change the condition in your for loops to be strictly < instead of <=
I really don't think I understand your question, but this is wrong:
int arey[3][3];
...
for(j=0;j<=3;j++) // <= invalid
...
array[3][3]=1; // out of bounds
arey is a 3*3 array. You can't access arey[3][?], that's out of bounds. The only valid indices are 0..2.
Once you've written past the bounds of your array, your program behavior becomes undefined.