How to generate knock-out vectors in Chapel? - chapel

I have a matrix, yes, A. Operating on her rows, I often need to create "knock-out" vectors. Basically
var v = [5, 4, 3, 2, 1];
v_{-2} = [5, 3, 2, 1]; // e.g. v[2] is removed
I don't want to remove it permanently, just for this calculation, and I want to do it along the rows of A.
var knockouts: [A.dim(1)] int; // list of knockout dims, as tall as A
for i in A.dim(1) {
var w = ||v_{-knockouts[i]}|| / ||v||
}
== UPDATE ==
More on A to keep it general. It is very large and (as is my wont) sparse. The elements knocked out are expected to be within the populated subdomain, but may not be in some cases. The entries are often probabilities, as in a stochastic matrix, so a few common row operations are
r = A[i,..]
s = r[3] / sum(r_{-3})
s = sum(r[3] log(r_{-3}))
s = sum(log (r_{-3})) / sum (log (r_{-5}))
With all the logging going on, it may not be safe to set r[3] = 0. But if that is the solution, it would still be nice to have a convenience function to do it under the covers. I don't recall seeing one but, maybe sum(r.except(3)) or another syntax.

I don't know of a good way to do this "in-place" such that no temporary arrays
are created.
For now, here's an approach that knocks out indices by creating a temporary array:
var v = [5, 4, 3, 2, 1];
writeln(exclude(v, 2)); // 5 3 2 1
writeln(exclude(v, 3)); // 5 4 2 1
/* Returns array with element at idx excluded */
proc exclude(A: [], idx) {
var v1 = A[..idx-1];
v1.push_back(A[idx+1..]); // See NOTE
return v1;
}
NOTE: Passing arrays to push_back() is not supported in Chapel 1.15. It has been added in
#7180.

Related

How to access multiple elements in c++ Eigen array?

I want to retrieve certain elements in an Eigen array and return them as a vector. I use the following code:
Eigen::ArrayXXi test;
test.resize(5,5);
test.setRandom();
Eigen::Matrix<int, 2, 3> inds;
inds<<0, 2, 3, 2, 3, 1;
auto res = test(inds.row(0), inds.row(1));
std::cout<<"test: \n"<<test <<std::endl;
std::cout<<"inds: \n"<<inds <<std::endl;
std::cout<<"res: \n"<<res <<std::endl;
The output is:
test:
730547559 -649503489 -48539462 893772102 -1038736613
-226810938 -353856438 276748203 291438716 -552146456
607950953 576018668 -290373134 466641602 -779039257
640895091 -477225175 28778235 -769652652 653214605
884005969 115899597 971155939 229713912 -737276042
inds:
0 2 3
2 3 1
res:
-48539462 893772102 -649503489
-290373134 466641602 576018668
28778235 -769652652 -477225175
The result is a matrix. I note that the diagonal of the matrix is the result I want. I could use res.diagonal() to retrieve the vector. However, I am still wondering if I can do the same thing in a more efficient way.
You can reshape the test Array to a column and then use the single-index access operator:
auto res = test.reshaped()(inds.row(0) + test.rows() * inds.row(1));
Generally, be careful when using auto with Eigen expressions (this case is fine, as long as test and inds are still valid when res is used).

Virtually defragment a fragmented memory as if it was contiguous in c++

is there a way or is it possible to take e.g 10 memory regions (e.g. pointers with given size) and create a sort of overlay such that they can be handled/treated as contiguous?
The use case would be something like reconstruct a message out of "n" frames without copying them around.
Of course the "n" frames are appended/prependend with a header which should be stripped in order to reconstruct the information. Moreover a variable could be e.g. splitted across two consecutive frames.
Few more details for future help.
Otter solution is quite nice but it lacks the possibility to lay a structure on top of multiple boost::join-ed block.
Of course a std::copy of the joined block will create a contiguous copy of all the interested and fragmented regions but in my case i would like it to be "virtual" due to performance constraints.
Regards,
boost::range::join is a great helper here - link. When working with random access ranges it will also produce random access range with quick access to elements. As the manual tells The resultant range will have the lowest common traversal of the two ranges supplied as parameters
Also when working with plain memory boost::make_iterator_range cound help.
Take a look at this short example.
int arr1[] = { 0, 1, 2, 3, 4, 5 }; // let's join these 3 plain memory arrays
int arr2[] = { 6, 7, 8, 9 };
int arr3[] = { 10, 11, 12};
int* mem1 = arr1; // let's make the example more complicated
int* mem2 = arr2; // because int arr1[] with known size will be recognized
int* mem3 = arr3; // as a range by boost::join
auto res1 = boost::range::join(boost::make_iterator_range(mem1, mem1 + 6),
boost::make_iterator_range(mem2, mem2 + 4)); // join 2 ranges by pointer arithmetics
auto res2 = boost::range::join(res1, // join previously joined range
boost::make_iterator_range(mem3, mem3 + 3));
for (auto& r : res2) // the resulted range is iterable
{
std::cout << r << "\n";
}
std::cout << res2[12]; // outputs '12', note that this result
// was eventually got by pointer arithmetics
// applyed to mem3

How to Initialize the List with the same element in flutter dart?

I have a list in dart I want to initialize the list with n number of the same element.
example:- initialize the integer list with element 5 4 times.
List<int> temp = [5,5,5,5];
what are different ways to initialize the list in dart flutter?
The easiest way I can think of is List.filled:
List.filled(int length, E fill, { bool growable: false }).
The params would be:
length - the number of elements in the list
E fill - what element should be contained in the list
growable - if you want to have a dynamic length;
So you could have:
List<int> zeros = List.filled(10, 0)
This would create a list with ten zeros in it.
One think you need to pay attention is if you're using objects to initialise the list for example:
SomeObject a = SomeObject();
List<SomeObject> objects = List.filled(10, a);
The list created above will have the same instance of object a on all positions.
If you want to have new objects on each position you could use List.generate:
List.generate(int length, E generator(int index), {bool growable:true})
Something like:
List<SomeObject> objects = List<SomeObject>.generate(10, (index) => SomeObject(index);
OR:
List<SomeObject> objects = List<SomeObject>.generate(10, (index) {
SomeOjbect obj = SomeObject(index)
obj.id= index;
return obj;
});
This will create a new instance for each position in list. The way you initialise the object is up to you.
You can try like this
List<int>.generate(4, (int index) => 5);
For more, read this
Here is a simplified version of the accepted answer. You can use a list literal, a filled list, or a generated list:
final literal = [5, 5, 5, 5];
final filled = List.filled(4, 5);
final generated = List.generate(4, (index) => 5);
print(literal); // [5, 5, 5, 5]
print(filled); // [5, 5, 5, 5]
print(generated); // [5, 5, 5, 5]
When you just want to fill the list with the same values, List.filled is good. Unless you literally want [5, 5, 5, 5]. In that case, just use the list literal. It's easy to read and understand.

Determine all square sub matrices of a given NxN matrix in C++

GIven an NxN square matrix, I would like to determine all possible square sub matrices by removing equal number of rows and columns.
In order to determine all possible 2x2 matrices I need to loop 4 times. Similarly for 3x3 matrices I need to loop 6 times and so on. Is there a way to generate code in C++ so that the code for the loops is generated dynamically? I have checked some answers related to code generation in C++, but most of them use python in it. I have no idea regarding python. So, is it possible to write code to generate code in C++?
If I get what you are saying, you mean you require M loops to choose M rows, and M loops for M columns for an M x M sub matrix, 1 <= M <= N
You don't need 2*M loops to do this. No need to dynamically generate code with an ever-increasing number of loops!
Essentially, you need to "combine" all possible combinations of i_{1}, i_{2}, ..., i_{M} and j_{1}, j_{2}, ..., j_{M} such that 1 <= i_{1} < i_{2} < ... < i_{M} <= N (and similarly for j)
If you have all possible combinations of all such i_{1}, ..., i_{M} you are essentially done.
Say for example you are working with a 10 x 10 matrix and you require 4 x 4 sub matrices.
Suppose you selected rows {1, 2, 3, 4} and columns {1, 2, 3, 4} initially. Next select column {1, 2, 3, 5}. Next {1, 2, 3, 6} and so on till {1, 2, 3, 10}. Next select {1, 2, 4, 5}, next {1, 2, 4, 6} and so on till you reach {7, 8, 9, 10}. This is one way you could generate all ("10 choose 4") combinations in a sequence.
Go ahead, write a function that generates this sequence and you are done. It can take as input M, N, current combination (as an array of M values) and return the next combination.
You need to call this sequence to select the next row and the next column.
I have put this a little loosely. If something is not clear I can edit to update my answer.
Edit:
I will be assuming loop index starts from 0 (the C++ way!). To elaborate the algorithm further, given one combination as input the next combination can be generated by treating the combination as a "counter" of sorts (except that no digit repeats).
Disclaimer : I have not run or tested the below snippet of code. But the idea is there for you to see. Also, I don't use C++ anymore. Bear with me for any mistakes.
// Requires M <= N as input, (N as in N x N matrix)
void nextCombination( int *currentCombination, int M, int N ) {
int *arr = currentCombination;
for( int i = M - 1; i >= 0; i-- ) {
if( arr[i] < N - M + i ) {
arr[i]++;
for( i = i + 1, i < M; i++ ) {
arr[i] = arr[i - 1] + 1;
}
break;
}
}
}
// Write code for Initialization: arr = [0, 1, 2, 3]
nextCombination( arr, 4, 10 );
// arr = [0, 1, 2, 4]
// You can check if the last combination has been reached by checking if arr[0] == N - M + 1. Please incorporate that into the function if you wish.
Edit:
Actually I want to check singularity of all possible sub matrices. My approach is to compute all submatrices and then find their determinants. How ever after computing the determinant of 2x2 matrices , I'll store them and use while computing determinants of 3x3 matrices. And so on. Can you suggest me a better approach. I have no space and time constraints. – vineel
A straight-forward approach using what you suggest is to index the determinants based on the the rows-columns combination that makes a sub matrix. At first store determinants for 1 x 1 sub matrices in a hash map (basically the entries themselves).
So the hash map would look like this for the 10 x 10 case
{
"0-0" : arr_{0, 0},
"0-1" : arr_{0, 1},
.
.
.
"1-0" : arr_{1, 0},
"1-1" : arr_{1, 1},
.
.
.
"9-9" : arr_{9, 9}
}
When M = 2, you can calculate determinant using the usual formula (the determinants for 1 x 1 sub matrices having been initialized) and then add to the hash map. The hash string for a 2 x 2 sub matrix would look something like 1:3-2:8 where the row indices in the original 10 x 10 matrix are 1,3 and the column indices are 2, 8. In general, for m x m sub matrix, the determinant can be determined by looking up all necessary (already) computed (m - 1) x (m - 1) determinants - this is a simple hash map lookup. Again, add the determinant to hash map once calculated.
Of course, you may need to slightly modify the nextCombination() function - it currently assumes row and column indices run from 0 to N - 1.
On another note, since all sub matrices are to be processed starting from 1 x 1, you don't need something like a nextCombination() function. Given a 2 x 2 matrix, you just need to select one more row and column to form a 3 x 3 matrix. So you need to select one row-index (that's not part of the row indices that make the 2 x 2 sub matrix) and similarly one column-index. But doing this for every 2 x 2 matrix will generate duplicate 3 x 3 matrices - you need to think of some way to eliminate duplicates. One way to avoid duplicates is by choosing only such row/column whose index is greater than the highest row/column index in the sub matrix.
Again I have loosely defined the idea. You can build upon it.

pointer to sub-row of Eigen MatrixXd that behaves like a VectorXd

I have an Eigen MatrixXd and need a pointer to some subsequent entries of some row. I would like to be able to use this pointer. I have something like this:
Eigen::MatrixXd* matrix = new MatrixXd(3, 3);
(*matrix) << 1, 2, 3,
4, 5, 6,
7, 8, 9;
Block<MatrixXd, 1, Dynamic, false, true> full_row = (*matrix).row(1);
// this gives me the full row. I am interested only in the row containing 5 6.
Block<MatrixXd> part_row = (*matrix).block(1, 1, 1, 2);
// this gives me the partial row that I want, but now i need two indices to
// access an element.
part_row(0, 1) = 3; // works
part_row(1) = 3; // gives compiler error
I would like to be able to directly access the partial row, without having to copy the values. This is really important, since it has to be done often and I cannot afford to copy vectors back and forth. (I believe I cannot expect the compiler to optimize out the copying, since the sizes of the matrices are generally unknown). Any help is greatly appreciated. Cheers!
You need to specify that your submatrix is a vector:
Block<MatrixXd,1,Dynamic> part_row(*matrix, 1, 1, 1, 2);