13

Suppose I have a MatrixXcf called A. I want to replace elements of every column by normalized one relative to the corresponding column. I've written following code but it's not true!

for (int i = 0; i < A.cols(); i++)
    A.col(i).real.array() = A.col(i).real().array()/A.col(i).real().norm();

and another question, what's difference between norm(), normalize() and normalized() in Eigen?

Saeid
  • 508
  • 1
  • 4
  • 20

2 Answers2

20

Firstly, you can normalize in place with normalize, so your code should be:

for (int i = 0; i < A.cols(); i++)
    A.col(i).normalize();

Secondly:

  1. normalize - Normalizes a compile time known vector (as in a vector that is known to be a vector at compile time) in place, returns nothing.
  2. normalized - Returns the above as a constructed copy, doesnt affect the class. You can use it to assign - Vector normCopy = vect.normalized().
  3. norm - Returns the norm value of the matrix. Ie, the square root of the sum of the square of all the matrix entries.

So the difference is really, what each function returns for you.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
10

The answer to your question can be found in the manual. In summary:

  • norm() is the Frobenius norm, the square root of the sum of squares of the components.

  • .normalized() returns a copy to the original object divided by this norm (i.e. the original object is not changed).

  • .normalize() divides an object in-place by this norm (i.e. the original object itself is modified).

With this example you can convince yourself:

#include <Eigen/Eigen>
#include <iostream>

int main()
{

  Eigen::VectorXd A(3);

  A(0) = 1.;
  A(1) = 2.;
  A(2) = 3.;

  std::cout << "A.norm() = " << std::endl;
  std::cout << A.norm() << std::endl;

  Eigen::VectorXd B = A.normalized();

  std::cout << "B = " << std::endl;
  std::cout << B << std::endl;
  std::cout << "A = " << std::endl;
  std::cout << A << std::endl;

  A.normalize();

  std::cout << "A = " << std::endl;
  std::cout << A << std::endl;

}

I compiled with:

clang++ `pkg-config --cflags eigen3` so.cpp 

but that varies from system to system.

The output:

A.norm() =
3.74166

B =
0.267261
0.534522
0.801784

A =
1
2
3

A =
0.267261
0.534522
0.801784
Tom de Geus
  • 5,625
  • 2
  • 33
  • 77