Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn], n > 1 on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

If n = 1 and x1 < 0, the above code will produce an incorrect result. :mhz works unconditionally. (Thanks to @MartinBüttner for pointing that out.)

I've used this trick for the first time in this answer this answer.

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn], n > 1 on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

If n = 1 and x1 < 0, the above code will produce an incorrect result. :mhz works unconditionally. (Thanks to @MartinBüttner for pointing that out.)

I've used this trick for the first time in this answer.

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn], n > 1 on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

If n = 1 and x1 < 0, the above code will produce an incorrect result. :mhz works unconditionally. (Thanks to @MartinBüttner for pointing that out.)

I've used this trick for the first time in this answer.

added 129 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn], n > 1 on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

If n = 1 and x1 < 0, the above code will produce an incorrect result. :mhz works unconditionally. (Thanks to @MartinBüttner for pointing that out.)

I've used this trick for the first time in this answer.

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn] on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

I've used this trick for the first time in this answer.

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn], n > 1 on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

If n = 1 and x1 < 0, the above code will produce an incorrect result. :mhz works unconditionally. (Thanks to @MartinBüttner for pointing that out.)

I've used this trick for the first time in this answer.

Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Euclidean norms

The straightforward way of calculating the Euclidean norm of a vector, i.e., the square root of the sum of the squares of its elements, is

2f#:+mq

However, there's a much shorter way.

mh, the hypotenuse operator, pops two integers a and b from the stack and pushes sqrt(a2 + b2).

If we have a vector x := [x1 ... xn] on the stack, :mh (reduce by hypotenuse) will achieve the following:

  • First x1 and x2 are pushed and mh is executed, leaving sqrt(x12 + x22), on the stack.

  • Then, x3 is pushed and mh is executed again, leaving
    sqrt(sqrt(x12 + x22)2 + x32) = sqrt(x12 + x22 + x32) on the stack.

  • After xn has been processed, we're left with sqrt(x12 + ... xn2), the Euclidean norm of x.

I've used this trick for the first time in this answer.

AltStyle によって変換されたページ (->オリジナル) /