views:

449

answers:

2

This may sound like a bit of a rhetorical question, but I ask it here for two reasons:

  1. It took me a while to figure out what C++ std::norm() was doing differently from Matlab/Octave, so others may stumble upon it here.
  2. I find it odd to define the norm() function as being something different (though closely related) to what is generally considered to be the norm (or L2-norm, or euclidean norm, etc. etc.)

Specifically the C++ standard library defines norm() for complex numbers to be the square of the modulus (or absolute value), where the modulus is sqrt(a^2 + b^2) when the complex number is in the form a + i*b.

This goes against my understanding of the norm, which when specified as the euclidean norm (which corresponds to the modulus used here), is the square root of the sum of squares. I'll reference Mathworld's definition of the complex modulus.

Is this something others have run into? I found it as a result of porting some signal processing code from Octave to C++, and the only other place I found reference to this difference was on the GCC mailing list (can't post the link due to 1-link limit).

+8  A: 

The C++ usage of the word "norm" is rather confusing, since most people have only ever come across norms in the context of vector spaces. If you view the complex numbers as a vector space over the reals, this is definitely not a norm. In fairness to C++, the std::norm( ) function does compute the so-called Field Norm from the complex numbers to the reals.

Fortunately, there is the std::abs( ) function, which does what you want.

Stephen Canon
Ah, I was not aware of the field norm - that certainly makes sense, or at least provides a plausible explanation of why they chose to define norm() this way.
runexe
Probably it was just a coincidence.
quant_dev
+2  A: 

Incidentally, the Euclidean norm squared can be useful as an optimizations; if you want to compare distances (e.g. dist(p1, p2) < dist(p3, p4)) and don't care about the specific magnitude, then you can compare the squared distances rather than the actual distances, and avoid the more-expensive square-root operation.

Kevin Reid