How to get norm of a row in Eigen? - c++

I want to get the norm of a row. I've written following code but it's not true!
for (int i = 0; i < A.rows(); i++)
A.row(i) = A.row(i).array() / (A.row(i).norm());
It is worth mentioning, type of A is MatrixXcf. In your opinion, what's the problem?

I don't see any problem with your code. Anyways, what you wrote can be written more compact as either of these:
// assign result to new variable:
Eigen::MatrixXcf N = A.rowwise().normalized();
// or in-place normalization:
A.rowwise().normalize();

Related

Orthogonalization in QR Factorization outputting slightly innaccurate orthogonalized matrix

I am writing code for QR Factorization and for some reason my orthogonal method does not work as intended. Basically, my proj() method is outputting random projections. Here is the code:
apmatrix<double> proj(apmatrix<double> v, apmatrix<double> u)
//Projection of u onto v
{
//proj(v,u) = [(u dot v)/(v dot v)]*v
double a = mult(transpose(u,u),v)[0][0], b = mult(transpose(v,v),v)[0][0], c = (a/b);
apmatrix<double>k;
k.resize(v.numrows(),v.numcols());
for(int i = 0; i<v.numrows(); i++)
{
for(int j = 0; j<v.numcols(); j++)
{
k[i][j]=v[i][j]*c;
}
}
return k;
}
I tested the method by itself with manual matrix inputs, and it seems to work fine. Here is my orthogonal method:
apmatrix<double> orthogonal(apmatrix<double> A) //Orthogonal
{
/*
n = (number of columns of A)-1
x = columns of A
v0 = x0
v1 = x1 - proj(v0,x1)
vn = xn - proj(v0,xn) - proj(v1,xn) - ... - proj(v(n-1),xn)
V = {v1, v2, ..., vn} or [v0 v1 ... vn]
*/
apmatrix<double> V, x, v;
int n = A.numcols();
V.resize(A.numrows(),n);
x.resize(A.numrows(), 1);
v.resize(A.numrows(),1);
for(int i = 0; i<A.numrows(); i++)
{
x[i][0]=A[i][1];
v[i][0]=A[i][0];
V[i][0]=A[i][0];
}
for (int c = 1; c<n; c++) //Iterates through each col of A as if each was its own matrix
{
apmatrix<double>vn,vc; //vn = Orthogonalized v (avoiding matrix overwriting of v); vc = previously orthogonalized v
vn=x;
vc.resize(v.numrows(), 1);
for(int i=0; i<c; i++) //Vn = an-(sigma(t=1, n-1, proj(vt, xn))
{
for(int k = 0; k<V.numrows(); k++)
vc[k][0] = V[k][i]; //Sets vc to designated v matrix
apmatrix<double>temp = proj(vc, x);
for(int j = 0; j<A.numrows(); j++)
{
vn[j][0]-=temp[j][0]; //orthogonalize matrix
}
}
for(int k = 0; k<V.numrows(); k++)
{
V[k][c]=vn[k][0]; //Subtracts orthogonalized col to V
v[k][0]=V[k][c]; //v is redundant. more of a placeholder
}
if((c+1)<A.numcols()) //Matrix Out of Bounds Checker
{
for(int k = 0; k<A.numrows(); k++)
{
vn[k][0]=0;
vc[k][0]=0;
x[k][0]=A[k][c+1]; //Moves x onto next v
}
}
}
system("PAUSE");
return V;
}
For testing purposes, I have been using the 2D Array: [[1,1,4],[1,4,2],[1,4,2],[1,1,0]]. Each column is its own 4x1 matrix. The matrices should be outputted as: [1,1,1,1]T, [-1.5,1.5,1.5,-1.5]T, and [2,0,0,-2]T respectively. What's happening now is that the first column comes out correctly (it's the same matrix), but the second and third come out to something that is potentially similar but not equal to their intended values.
Again, each time I call on the orthogonal method, it outputs something different. I think it's due to the numbers inputted in the proj() method, but I am not fully sure.
The apmatrix is from the AP college board, back when they taught cpp. It is similar to vectors or ArrayLists in Java.
Here is a link to apmatrix.cpp and to the documentation or conditions (probably more useful), apmatrix.h.
Here is a link to the full code (I added visual markers to see what the computer is doing).
It's fair to assume that all custom methods work as intended (except maybe Matrix Regressions, but that's irrelevant). And be sure to enter the matrix using the enter method before trying to factorize. The code might be inefficient partly because I self-taught myself cpp not too long ago and I've been trying different ways to fix my code. Thank you for the help!
As said in comments:
#AhmedFasih After doing more tests today, I have found that it is in-fact some >memory issue. I found that for some reason, if a variable or an apmatrix object >is declared within a loop, initialized, then that loop is reiterated, the >memory does not entirely wipe the value stored in that variable or object. This >is noted in two places in my code. For whatever reason, I had to set the >doubles a,b, and c to 0 in the proj method and apmatrixdh to 0 in the >mult method or they would store some value in the next iteration. Thank you so >much for you help!

Swapping two rows in a matrix

I am having a problem in swapping two rows in a matrix that is a 2D-dynamic array. I wanted to know if there is a function to use directly or if there is none I would like to know how to make one. Thanks in advance.
Here is how I made the dynamic array:
int **ptrMatrix = new int*[row];
for (int i = 0; i < row; i++)
ptrMatrix[i] = new int[column];
I found the solution ... it is done simply by making a temporary variable (X) and appling the following code
for (int i=0;i<columns;i++)
{
X=matrix[row1-1][i];
matrix[row1-1][i]=matrix[row2-1][i];
matrix[row2-1][i]=X;
}

Fastest way to calculate distance between all rows in a dense eigen::matrix

I am trying to calculate the euclidean distance between every pair of rows in a 1000x1000 matrix using Eigen. What I have so far is something along these lines:
for (int i = 0; i < matrix.rows(); ++i){
VectorXd refRow = matrix.row(i);
for (int j = i+1; j < matrix.rows(); ++j){
VectorXd eleRow = matrix.row(j);
euclid_distance = (refRow - eleRow).lpNorm<2>();
...
}
}
My code includes other code here replaced with "..." but for testing the performance I have removed it.
Now I don't expect this to run at the speed of light but it is taking a lot more than I expected. Am I doing something wrong in using C++ \ the Eigen library that might be slowing this down?
Is there any other preferred method?

Matrix multiplication in a cpp file for Matlab

How would I do a matrix multiplication in cpp format that would after be compiled into a mex file?
My normal matrix multiplication in a Matlab script is as follow:
cMatrix = (1 / r) * pfMatrix * wcMatrix; %here pfMatrix is 2x3 and wcMatrix is 3x8
% Hence cMatrix is 2x8
% r is a scalar
The pfMatrix, wcMatrix and r are declared correctly in the cpp file and they have the same values as in the script. However cMatrix doesn't give me the same results. Here the implementation of the Matrix multiplication in the cpp :
int i, n, j;
for (i = 0; i<1; i++)
{
for (n = 0; n<7; n++)
{
for (j = 0; j<2; j++)
{
d->cMatrix[i][n] += (d->pfMatrix[i][j]) * (d->wcMatrix[j][n]);
}
d->cMatrix[i][n] = (1 / d->r) * d->cMatrix[i][n];
}
}
Edit:
I modified the loop following Ben Voigt answer. The results in cMatrix are still not identical to the one calculated from the Matlab script.
For example :
pfMatrix = [7937.91049469652,0,512;0,7933.81033431703,384];
wcMatrix = [-0.880633810389421,-1.04063381038942,-1.04063381038942,-0.880633810389421,-0.815633810389421,-1.10563381038942,-1.10563381038942,-0.815633810389421;-0.125,-0.125,0.125,0.125,-0.29,-0.29,0.29,0.29;100,100,100,100,100,100,100,100];
r = 100;
In this case, cMatrix(1,1) is :
(pfMatrix(1,1)*wcMatrix(1,1) + pfMatrix(1,2)*wcMatrix(2,1) + pfMatrix(1,3)*wcMatrix(3,1)) / r = 442.09
However, with the mex file the equivalent result is 959.
Edit #2:
I found the error in an element of pfMatrix that was not declared correctly (missing a division by 2). So the answer of Ben Voigt is working correctly. However, there is still a slight difference between the two results (Matlab script gives 442 and the mex gives 447, could it be a results of different data type?).
Edit #3:
Found the error and it was not related with the matrix multiplication loop.
Using your result matrix as scratch space is not a great idea. The compiler has to worry about aliasing, which means it can't optimize.
Try an explicit working variable, which also provides a convenient place to zero it:
for (int i = 0; i < 2; ++i) {
for (int n = 0; n < 8; ++n) {
double accum = 0.0;
for (int j = 0; j < 3; ++j) {
accum += (d->pfMatrix[i][j]) * (d->wcMatrix[j][n]);
}
d->cMatrix[i][n] = accum / d->r;
}
}
Your ranges were also wrong, which I've fixed.
(Also note that good performance on large matrices requires banding to get good cache behavior, however that shouldn't be an issue on a product of this size.)
A multiplication between matrices must be in this way: A[m][n] * B[n][p] = R[m][p].
The conditions that you wrote in the for loops are not correct and doesn't respect the matrix dimensions.
Look also at the Eigen libraries, which are open-source and provide a simple way to do the matrix multiplications.

lvalue in assignment too complex

Following code causes a glsl error: lvalue in assignment too complex
for(int i = 0; i < 4; i++)
{
if(Lgt.lights[i].position.w == 0.0)
{
LightDir[i] = normalize(vec3(Lgt.lights[i].position));
ViewDir[i] = normalize(cameraWorldPosition - worldPosition);
}
else
{
LightDir[i] = normalize(vec3(Lgt.lights[i].position) - worldPosition);
ViewDir[i] = normalize(cameraWorldPosition - worldPosition);
}
}
But in the other shader program an identical piece of code works fine. When the code doesn't contain if statement, for example
for(int i = 0; i < 4; i++)
{
LightDir[i] = normalize(vec3(Lgt.lights[i].position) - worldPosition);
ViewDir[i] = normalize(cameraWorldPosition - worldPosition);
}
everything is ok, but when I use multiplication:
for(int i = 0; i < 4; i++)
{
LightDir[i] = LocalMat * normalize(vec3(Lgt.lights[i].position) - worldPosition);
ViewDir[i] = LocalMat * normalize(cameraWorldPosition - worldPosition);
}
I get that error again. Can anyone tell what is going on ?
I'm guessing that you're running on a GPU/driver combination that doesn't allow indexed assignments. So in order to compile the code, it will need to completely unroll the loop, changing all the indexes in the lvalues into constants. This apparently happens in some cases, but not all.
If you're using an Nvidia GPU/driver, you might try putting #pragma optionNV unroll all in the top of your shader program to force full unrolling of all loops -- but that might cause problems if you have other loops that shouldn't be unrolled.
I also had this error, but my cause was different.
It turns out I had a hard coded maximum index in my for loop which was bigger than the array I was trying to assign to. My guess is that the compiler got confused when attempting to unfold and there were not enough indices for the lvalue.