Loren on the Art of MATLAB

February 7th, 2008

Why Does MATLAB Have the Function hypot?

Sometimes I am asked questions like "why did you introduce function xyz into MATLAB? And sometimes I have a compelling answer, even for something that looks simple on the surface. Consider the function hypot.

Contents

What Does hypot Compute?

hypot essentially computes the square root of the sum of the squares of 2 inputs. So what's the big deal, right? For "reasonable" input values, there is no big issue.

fhypot = @(a,b) sqrt(abs(a).^2+abs(b).^2);

Other Ways to Compute hypot

Let's create a set of values and compute hypot 3 ways for these: with hypot, fhypot, and sqrt(2)*x. These should all give the same answers, provide a is real and positive.

a = logspace(0,5,6)
format long
aHypot = hypot(a,a)
aFhypot = fhypot(a,a)
aSqrt2 = sqrt(2)*a
a =
           1          10         100        1000       10000      100000
aHypot =
  1.0e+005 *
  Columns 1 through 3
   0.000014142135624   0.000141421356237   0.001414213562373
  Columns 4 through 6
   0.014142135623731   0.141421356237310   1.414213562373095
aFhypot =
  1.0e+005 *
  Columns 1 through 3
   0.000014142135624   0.000141421356237   0.001414213562373
  Columns 4 through 6
   0.014142135623731   0.141421356237310   1.414213562373095
aSqrt2 =
  1.0e+005 *
  Columns 1 through 3
   0.000014142135624   0.000141421356237   0.001414213562373
  Columns 4 through 6
   0.014142135623731   0.141421356237310   1.414213562373095

Results

The results are the same, to within round-off, for the 3 methods here. But what happens if the magnitude of a is larger, near realmax perhaps?

realmax
ans =
    1.797693134862316e+308

For my computer, a 32-bit Windows machine, realmax for doubles is on the order of 10^308. Let's see what happens if a value nearly that large is used with the different versions of hypot.

a = 1e308
aHypot = hypot(a,a)
aFhypot = fhypot(a,a)
aSqrt2 = sqrt(2)*a
a =
    1.000000000000000e+308
aHypot =
    1.414213562373095e+308
aFhypot =
   Inf
aSqrt2 =
    1.414213562373095e+308

What you can see is that the straight-forward method returns Inf instead of a finite answer. And that's why hypot was added to MATLAB, to compute the hypotenuse robustly, avoiding both underflow and overflow.


Get the MATLAB code

Published with MATLAB® 7.5

5 Responses to “Why Does MATLAB Have the Function hypot?”

  1. Quan replied on :

    That was a pretty interesting post Loren. I’m not sure if it will affect the way I do things around here, but it’s something that could come in useful in the future. Thanks for the tip!

  2. Duane Hanselman replied on :

    Before hypot existed, I used a=abs(complex(a,b)); In doing so, I was able to tap the robustness of the abs() function.

  3. Loren replied on :

    Duane-

    Thanks for pointing out that abs is also implemented robustly.

    –Loren

  4. Hal K replied on :

    Loren,
    Does hypot(a,a) any different from norm([a a])?

    Hal

  5. Loren replied on :

    Hal-

    norm is also careful to scale results, like abs and hypot.

    –Loren

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Loren Shure works on design of the MATLAB language at The MathWorks. She writes here about once a week on MATLAB programming and related topics.

  • Jun: I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
  • Loren: Wow folks- Always lots of interest when there’s a quickie to try out! I will only make 2 general...
  • Loren: Jun- ismember is your friend here: >> [aa,ind] = ismember(Array2,Arra y1) aa = 1 1 1 1 1 1 1 ind = 1 2 1 4 4 3...
  • Dan: I like the first way better than the second way. Combining the arrays into one and running any is nice, although...
  • James Myatt: How about I = (a == 0 | b == 0); a(I) = []; b(I) = [];
  • Tunc: Hello Loren, love your blog because of such inspiring and challenging comments to such ’small’...
  • Pekka Kumpulainen: Here is my tradeoff. I usually want to keep the original variables as they are most probably...
  • Iain: Followup: Of course, to allow NaNs (counting them as non-zero): mask = (a~=0) & (b~=0); The mask says “a...
  • Matt Fig: I would usually go with something like this: y = a&b; x = a(y); y = b(y); But I was surprised to find...
  • kk: c=all([a;b]) a(c) a(b)

These postings are the author's and don't necessarily represent the opinions of The MathWorks.