How to make a complete matrix with 2 loops in c++ - c++

I have a string in c++ and it represents an upper triangular matrix, What I want to do is meake a complete matrix from this string
std::string input = "1,2,1,3,6,1,4,7,9,1";
//this represents
//1 2 3 4
//2 1 6 7
//3 6 1 9
//4 7 9 1
std::replace(input.begin(), input.end(), ',', ' ');
std::vector<double> Matrix;
std::istringstream inputStream(input);
double value;
int rowNum = 0;
int colNum = 0;
while (inputStream >> value){
for (colNum = 0; colNum < 2; colNum++){
if (colNum >= rowNum){
Matrix.push_back( value );
}
else{
Matrix.push_back( Matrix[colNum * 2 + rowNum]);
}
}
rowNum++;
}
inputStream >> std::ws;
Instead of getting
1 2 3 4
2 1 6 7
3 6 1 9
4 7 9 1
But I am getting
1.0000 1.0000 1.0000 2.0000
1.0000 1.0000 2.0000 1.0000
1.0000 2.0000 1.0000 1.0000
2.0000 1.0000 1.0000 2.0000
What is it my error? I can not see it...

You should show the indexing scheme used for printing the output (i.e. how do you expect the indexes works): your choice of using a vector instead of a matrix make hard to correct the code. For sure, I see the following points that have no clear connection with the input pattern:
1) each number you read you increment the rowNum index. The row should be incremented instead at 'steps' 1, 1+2, 1+2+3,...
2) colNum should range from 0 to current rowNum, instead assumes just 0,1
3) there is no chance to fill a row (say the first) before you read (say the last). You could do if the input would be 1 2 3 4 1 6 7 1 9 1
all these points are related, and origin from the wrong data representation, that make difficult a trivial task.
In C++, a very effective way to tackle these problems is data hiding: consider how easily we can write a class that gives the correct logical representation and doesn't waste space:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
template <class T = double>
class upper_triangular_matrix
{
std::vector<T> Matrix;
public:
upper_triangular_matrix(std::string input)
{
// trade time for space: store the values, compute indexing
std::replace(input.begin(), input.end(), ',', ' ');
std::istringstream inputStream(input);
T value;
while (inputStream >> value)
Matrix.push_back(value);
// validate size: ok 1,1+2,1+2+3 etc
}
T operator()(int r, int c) const
{
// compute indexing accounting for miss duplicated
if (c > r)
std::swap(c, r);
int p = 0, n = 1;
while (r > 0)
{
p += n++;
r--;
}
return Matrix[p + c];
}
};
int main()
{
upper_triangular_matrix<> m("1,2,1,3,6,1,4,7,9,1");
for (int r = 0; r < 4; ++r)
{
for (int c = 0; c < 4; ++c)
std::cout << m(r, c) << ' ';
std::cout << std::endl;
}
}
when run, this prints
1 2 3 4
2 1 6 7
3 6 1 9
4 7 9 1

It is hard to tell exactly where the error is but here is where it starts:
std::vector<double> Matrix;
Yes, a non-empty std::vector<double> with n elements is a matrix: either a 1xn or a nx1 matrix (or both). In your context this view is, however, utterly unhelpful.
Let's look at the for-loop when you read the first element:
colNum == 0, rowNum == 0 => (1, 1) = Matrix[0] = 1
colNum == 1, rowNum == 0 => (2, 1) = Matrix[1] = 1
This start is clearly wrong. After this rowNum becomes 1:
colNum == 0, rowNum == 1 => (3, 1) = Matrix[2] = Matrix[colNum * 2 + rowNum] = Matrix[1] = 1
colNum == 1, rowNum == 1 => (4, 1) = Matrix[3] = 2
Well, I guess you can write the remainder up yourself. Of course, I could quickly write the code to solve your problem but I think this little exercise is for you. The way to do it is to fill the first row columns (where row is the current row being processed, using conventional index starting with 0) with the values from the transposed matrix and then read the remaining n - row columns (where n is the size of the matrix) from the file.

Related

how to calculate multiset of elements given probability on each element?

let say I have a total number
tN = 12
and a set of elements
elem = [1,2,3,4]
and a prob for each element to be taken
prob = [0.0, 0.5, 0.75, 0.25]
i need to get a random multiset of these elements, such as
the taken elements reflects the prob
the sum of each elem is tN
with the example above, here's some possible outcome:
3 3 2 4
2 3 2 3 2
3 4 2 3
2 2 3 3 2
3 2 3 2 2
at the moment, maxtN will be 64, and elements the one above (1,2,3,4).
is this a Knapsack problem? how would you easily resolve it? both "on the fly" or "pre-calculate" approch will be allowed (or at least, depends by the computation time). I'm doing it for a c++ app.
Mission: don't need to have exactly the % in the final seq. Just to give more possibility to an elements to be in the final seq due to its higher prob. In few words: in the example, i prefer get seq with more 3-2 rather than 4, and no 1.
Here's an attempt to select elements with its prob, on 10 takes:
Randomizer randomizer;
int tN = 12;
std::vector<int> elem = {2, 3, 4};
std::vector<float> prob = {0.5f, 0.75f, 0.25f};
float probSum = std::accumulate(begin(prob), end(prob), 0.0f, std::plus<float>());
std::vector<float> probScaled;
for (size_t i = 0; i < prob.size(); i++)
{
probScaled.push_back((i == 0 ? 0.0f : probScaled[i - 1]) + (prob[i] / probSum));
}
for (size_t r = 0; r < 10; r++)
{
float rnd = randomizer.getRandomValue();
int index = 0;
for (size_t i = 0; i < probScaled.size(); i++)
{
if (rnd < probScaled[i])
{
index = i;
break;
}
}
std::cout << elem[index] << std::endl;
}
which gives, for example, this choice:
3
3
2
2
4
2
2
4
3
3
Now i just need to build a multiset which sum up to tN. Any tips?

How to rotate outer ring of a matrix by n step in C++?

I want to rotate just the outer ring of a matrix clockwise.
Where n=number of steps to be rotate clockwise.
Suppose if I have a 4x5 matrix.
1 2 3 4 5
6 7 8 9 0
5 4 2 5 7
8 2 7 9 3
Now if n=1 the output should be :-
6 1 2 3 4
5 7 8 9 5
8 4 2 5 0
2 7 9 3 7
I have tried the logic of :
int temp = im[i][j];
im[i][j] = im[n-1-j][i];
im[n-1-j][i] = im[n-1-i][n-1-j];
im[n-1-i][n-1-j] = im[j][n-1-i];
im[j][n-1-i] = temp;
But I know this logic is completely wrong as it is moving the whole matrix.
You can do it like this:
Map and copy the border to a flat array
Apply std::rotate
Copy rotated array back into the border
Here is a sample implementation:
void rotate_border(int m[R][C], int n) {
vector<int> tmp(2*(R+C-2), -1);
for (size_t c = 0 ; c != C ; c++) {
tmp[c] = m[0][c];
tmp[c+C+R-2] = m[R-1][C-c-1];
}
for (size_t r = 1 ; r != R-1 ; r++) {
tmp[C+r-1] = m[r][C-1];
tmp[2*C+R-3+r] = m[R-r-1][0];
}
std::rotate(tmp.begin(), std::next(tmp.begin(), n), tmp.end());
for (size_t c = 0 ; c != C ; c++) {
m[0][c] = tmp[c];
m[R-1][C-c-1] = tmp[c+C+R-2];
}
for (size_t r = 1 ; r != R-1 ; r++) {
m[r][C-1] = tmp[C+r-1];
m[R-r-1][0] = tmp[2*C+R-3+r];
}
}
Demo.
Positive numbers rotate border counterclockwise. For clockwise rotation subtract the number from the size of the border. For example, 4✕5 matrix has the border of size 14, i.e. 5+(4-2)+5+(4-2). Hence, rotating by 1 clockwise requires rotating by 14-1.

A many-to-one mapping in the natural domain using discrete input variables?

I would like to find a mapping f:X --> N, with multiple discrete natural variables X of varying dimension, where f produces a unique number between 0 to the multiplication of all dimensions. For example. Assume X = {a,b,c}, with dimensions |a| = 2, |b| = 3, |c| = 2. f should produce 0 to 12 (2*3*2).
a b c | f(X)
0 0 0 | 0
0 0 1 | 1
0 1 0 | 2
0 1 1 | 3
0 2 0 | 4
0 2 1 | 5
1 0 0 | 6
1 0 1 | 7
1 1 0 | 8
1 1 1 | 9
1 2 0 | 10
1 2 1 | 11
This is easy when all dimensions are equal. Assume binary for example:
f(a=1,b=0,c=1) = 1*2^2 + 0*2^1 + 1*2^0 = 5
Using this naively with varying dimensions we would get overlapping values:
f(a=0,b=1,c=1) = 0*2^2 + 1*3^1 + 1*2^2 = 4
f(a=1,b=0,c=0) = 1*2^2 + 0*3^1 + 0*2^2 = 4
A computationally fast function is preferred as I intend to use/implement it in C++. Any help is appreciated!
Ok, the most important part here is math and algorythmics. You have variable dimensions of size (from least order to most one) d0, d1, ... ,dn. A tuple (x0, x1, ... , xn) with xi < di will represent the following number: x0 + d0 * x1 + ... + d0 * d1 * ... * dn-1 * xn
In pseudo-code, I would write:
result = 0
loop for i=n to 0 step -1
result = result * d[i] + x[i]
To implement it in C++, my advice would be to create a class where the constructor would take the number of dimensions and the dimensions itself (or simply a vector<int> containing the dimensions), and a method that would accept an array or a vector of same size containing the values. Optionaly, you could control that no input value is greater than its dimension.
A possible C++ implementation could be:
class F {
vector<int> dims;
public:
F(vector<int> d) : dims(d) {}
int to_int(vector<int> x) {
if (x.size() != dims.size()) {
throw std::invalid_argument("Wrong size");
}
int result = 0;
for (int i = dims.size() - 1; i >= 0; i--) {
if (x[i] >= dims[i]) {
throw std::invalid_argument("Value >= dimension");
}
result = result * dims[i] + x[i];
}
return result;
}
};

find a selection of elements adjacent sum = 10

Description:
Given matrix [x] [y], with x- rows and y- number of columns . Filled random numbers from 0 to 5 inclusive .
Description of finding a solution : the solution is considered to be a set of matrix elements that are adjacent to each other ( diagonal neighborhood is not taken into account ) and the sum of the number are 10. Each element of the matrix can be used 1 time for a decision . The solution may have any number of digits. The decision must end any number other than zero .
Example:
given
0 1 2 3 4 5
1 2 3 4 5 0
2 3 4 5 1 2
Solution 1 : (1 - 2 - 3 - 4)
0 **1** 2 3 4 5
1 **2** 3 4 5 0
2 **3** **4** 5 1 2
i tried to do smth like this, but it is wrong, i dont know when i must stop,
Solution it is a class which contains mair of indexes, pls help me.
void xxx(int colCount, int rowCount, int currentRow, int currentCol, int** matrix, int sum, Solution *solution, int solCount) {
sum += matrix[currentRow][currentCol];
matrix[currentRow][currentCol] = -1;
if(sum > 10){
sum - = matrix[currentRow][currentCol];
return;
} else if(sum == 10){
solution[solCount].additem(currentRow, currentCol);
return xxx(5,5,currentRow - 1, currentCol, matrix, sum, solution, solCount+1);
} else {
//UP
if( currentRow > 0 && matrix [currentRow - 1][currentCol] != -1){
xxx(5,5,currentRow - 1, currentCol, matrix, sum, solution,solCount);
}
//LEFT
if(currentCol > 0 && matrix [currentRow][currentCol-1] != -1){
xxx(5,5,currentRow, currentCol - 1, matrix, sum, solution,solCount);
}
//DOWN
if(currentRow + 1 < colCount && matrix[currentRow + 1][currentCol] != -1){
xxx(5,5,currentRow + 1, currentCol, matrix, sum, solution,solCount);
}
//RIGHT
if(currentCol + 1 < rowCount && matrix[currentRow][currentCol + 1] != -1){
xxx(5,5,currentRow, currentCol + 1, matrix, sum, solution,solCount);
}
}
}

Partitioning arrays by index

I am fairly new to C++, and am struggling through a problem that seems to have a solid solution but I just can't seem to find it. I have a contiguous array of ints starting at zero:
int i[6] = { 0, 1, 2, 3, 4, 5 }; // this is actually from an iterator
I would like to partition the array into groups of three. The design is to have two methods, j and k, such that given an i they will return the other two elements from the same group of three. For example:
i j(i) k(i)
0 1 2
1 0 2
2 0 1
3 4 5
4 3 5
5 3 4
The solution seems to involve summing the i with its value mod three and either plus or minus one, but I can't quite seem to work out the logic.
This should work:
int d = i % 3;
int j = i - d + ( d == 0 );
int k = i - d + 2 - ( d == 2 );
or following statement for k could be more readable:
int k = i - d + ( d == 2 ? 1 : 2 );
This should do it:
int j(int i)
{
int div = i / 3;
if (i%3 != 0)
return 3*div;
else
return 3*div+1;
}
int k(int i)
{
int div = i / 3;
if (i%3 != 2)
return 3*div+2;
else
return 3*div+1;
}
Test.
If you want shorter functions:
int j(int i)
{
return i/3*3 + (i%3 ? 0 : 1);
}
int k(int i)
{
return i/3*3 + (i%3-2 ? 2 : 1);
}
Well, first, notice that
j(i) == j(3+i) == j(6+i) == j(9+i) == ...
k(i) == k(3+i) == k(6+i) == k(9+i) == ...
In other words, you only need to find a formula for
j(i), i = 0, 1, 2
k(i), i = 0, 1, 2
and then for the rest of the cases simply plug in i mod 3.
From there, you'll have trouble finding a simple formula because your "rotation" isn't standard. Instead of
i j(i) k(i)
0 1 2
1 2 0
2 0 1
for which the formula would have been
j(i) = (i + 1) % 3
k(i) = (i + 2) % 3
you have
i j(i) k(i)
0 1 2
1 0 1
2 0 2
for which the only formula I can think of at the moment is
j(i) = (i == 0 ? 1 : 0)
k(i) = (i == 1 ? 1 : 2)
If the values of your array (let's call it arr, not i in order to avoid confusion with the index i) do not coincide with their respective index, you have to perform a reverse lookup to figure out their index first. I propose using an std::map<int,size_t> or an std::unordered_map<int,size_t>.
That structure reflects the inverse of arr and you can extra the index for a particular value with its subscript operator or the at member function. From then, you can operate purely on the indices, and use modulo (%) to access the previous and the next element as suggested in the other answers.