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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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
mhis executed, leaving sqrt(x12 + x22), on the stack.Then, x3 is pushed and
mhis 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.