Norm in g'mic vs norm in c++

Why is there a disparity in here?

C:\Windows\System32>gmic echo {norm(4,3)}
[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ 5
[gmic]-0./ End G'MIC interpreter.

And this

Code:
// C++ program to demonstrate
// example of norm() function.
   
 #include <bits/stdc++.h>
using namespace std;
   
// driver function
int main ()
{
  // initializing the complex: (4.0+3.0i)
  complex<double> complexnumber (4.0, 3.0);
   
  // use of norm()function
  cout << "The norm of " << complexnumber << " is ";
  cout << norm(complexnumber) << endl;
   
  return 0;
}

Output:
The norm of (4,3) is 25

I realized this is why I couldn’t do complex-plane version of Popcorn Fractal that I long dream about.

EDIT: I found that the answer seem to be just doing a sqr(norm(vec2)) to get the same result.

Yes, I don’t know why, but the std::norm() in C++ computes the squared magnitude rather than the vector norm.
The norm() function in G’MIC just computes the Euclidean norm of a vector, which is obviously \sqrt{x^2+y^2} for a vector (x,y).

C++ definition of norm. The example given on this page is that as provided by @Reptorian

G’MIC follows norm as a mathematician would read it: the Euclidean metric — the L2 norm.

My understanding is that C++ offers its norm() as a “cheap distance metric.” If I sort a bunch of points by distance from a reference (origin), I would get the same sort order whether or not I took the square root – and taking a square root is a relatively expensive computation. So if I can avoid it and – for certain applications – get the same result, I’d drop computing the square root. Especially if all I have is @afre 's old laptop :slightly_smiling_face:

Also, I forgot to mention. norm() isn’t found on the G’MIC reference. And there should be a note that to use a*a+b*b to get the same result as in c++.

It’s there — easy to miss because it is not spelled out as such. In the math parser, it is documented as normP for P in {2, 3, 4…}. Omit the P ( norm() ) then it defaults to 2, as documented in Mathematical Expressions - Vector Calculus. See tenth bullet point. Then, for the command line parser, you can take an image’s point-wise L2 norm, tutorial at norm.

1 Like

Or just norm(a,b)^2.

Yeah, it just takes a while to find.

You cursed my laptop. :imp: At the time of your posting, it croaked. It started working an hour ago after I removed and returned some of its innards.

2 Likes

That’s very interesting. Is it also the same in terms of computational effort? I.e., does the G’MIC parser recognize the “useless” sqrt(.)^2?

Ah me. That was probably fallout of the new quantum-entanglement commands being incorporated into G’MIC. I’m finding the time-and-space inversion tutorials a bit tricky. If images of the Brooklyn Army Terminal Building in Bay Ridge show up on your laptop, don’t be (too) alarmed. They’re harmless. Mostly. I think. @David_Tschumperle , of course, has no idea what these are, since he hasn’t even conceived of them yet, so he should find the documentation interesting. That’s the beauty of temporal entanglements: I can experiment and document these commands even before they’re written, so – for once! – I am ahead of the documentation game. Always interesting to see what G’MIC 3.5 will bring to us in 2025, even if there aren’t any Feynman diagrams linking causality. I know what I am doing. I mean, I’m pretty sure of that…

1 Like