Why `x = x*(1.5f-(xhalf*x*x));` can be a Newton Method iteration? - algorithm

Ok, by far, I guess many people know the famous fast inverse square root (see more on Writing your own square root function and 0x5f3759df)
Here is the code
float FastInvSqrt(float x) {
float xhalf = 0.5f * x;
int i = *(int*)&x; // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1); // what the fuck?
x = *(float*)&i;
x = x*(1.5f-(xhalf*x*x)); // one Newton Method iteration
return x;
}
Ok, i do NOT need to know more how magic 0x5f3759df is.
What I don't understand is why x*(1.5f-(xhalf*x*x)) is a Newton Method iteration?
I tried analyse but just can't get it.
So let's assume r is the real number and x is the inverse sqrt of r.
1 / (x^2) = r, then f(x) = r*(x^2)-1 and f'(x) = 2 * r * x
So one iteration should be x1 = x - f(x)/f'(x) = x / 2 + 1 / (2 * r * x), right?
How comes x * (1.5 - ((r / 2) * x * x))? (note I replaced xhalf with r / 2 here)
Edit
Ok f(x) = x^2 - 1/r is another form, let me calculate
f(x) = x^2 - 1 / r
f'(x) = 2 * x
So x1 = x - (f(x)/f'(x)) = x - (x^2 -(1 / r))/(2*x) = x / 2 + 1 / (2 * r * x), still it is quite different from the formula used in the code, right?

Wikipedia says function is (using your variable names):
f(x) = 1/x2 - r
Then we have:
f'(x) = -2/x3
And iteration is:
x - f(x)/f'(x) =
x - (1/x2 - r)/(-2 / x3) =
x + x3 /2 * (1/x2 - r) =
x + x/2 - r/2 * x3 =
x * (1.5 - r/2 * x * x)
And this is what you see in code.

Newton's method is defined in terms of the iteration
xi+1 = xi - f(xi) / f'(xi)
(where f'(x) is the first derivative of f(x)). To find the inverse root of r, one needs to find the zero of the function f(x) = x - 1/sqrt(r) (or, equivalently, f(x) = x2 - 1/r). Simply take the derivative, plug it in to the definition of the iteration step, simplify, and you have your answer.
Actually, the exact form used in the code comes from using a third equivalent form:
f(x) = x-2 - r
See this article for the detailed steps of the derivation. It's also derived in the Wikipedia article on fast inverse square root.

Related

solving of equations by the Euler method in Prolog

I wrote this mini pseudo code that solves the equation using the Euler method:
// y'= x^2 - 5y
int n = 10; // count of steps
double h = 0.5; // step
double x = 0;
double y = 1;
for (int i = 0; i < n; i++) {
y += h * (x * x - 5 * y);
x += h;
}
write << y; //result
Now I am trying to write this in Prolog language
loop2(N,H,X,Y) :-
N > 0,
write(Y), nl,
Y is Y + H * (X * X - 5 * Y),
X is X + H,
S is N - 1,
loop2(S, H, X, Y).
Here I solved the example on a piece of paper and should get 62.5
But in Prolog my Output =
?- loop2(10, 0.5, 0, 1).
1
false.
X is X + H becomes 0 is 0 + 0.5 and it is not, so case closed as far as Prolog is concerned, it has found your logical code is false, and reports that to you. You did tell it to writeln(Y) before that, so you still see 1 while it was trying.
You need to use new variable names for the results of calculations like you have used S is N - 1, e.g. Xnext is X + H.
The way you have shaped the countdown, S will eventually become 0 and then N > 0 will be false and the whole thing will fail then. You can probably get away with using this to write the values before it eventually fails, but a more normal way to end recursion is to have
loop2(0,_,_,_).
loop2(N,H,X,Y) :-
...
which says that when the call to loop2 happens, the top one is found first, if the first position is 0, then it succeeds. That ends the recursion and the whole thing succeeds.

prolog : calculating distance between two coordinates

I want to calculate the distance between two locations with their coordinates. I have seen this question and it has implementations in different languages. Since I'm a beginner in prolog, it would be really helpful if anyone can build a prolog clause that can do such operation.
I've found this python function simpler:
from math import cos, asin, sqrt
def distance(lat1, lon1, lat2, lon2):
p = 0.017453292519943295
a = 0.5 - cos((lat2 - lat1) * p)/2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
return 12742 * asin(sqrt(a))
I have made the predicate myself. It's given below:
distance(Lat1, Lon1, Lat2, Lon2, Dis):-
P is 0.017453292519943295,
A is (0.5 - cos((Lat2 - Lat1) * P) / 2 + cos(Lat1 * P) * cos(Lat2 * P) * (1 - cos((Lon2 - Lon1) * P)) / 2),
Dis is (12742 * asin(sqrt(A))).
A sample run for coordinates (23.700042,90.452103) and (23.767968, 90.425657); the output is:
?- main.
Distance is: 8.01840452822046
true.
I have given an online coordinate distance calculator's result below which shows prolog's result is sufficiently accurate.

Allowing inputs to be matrix or vectors or scalar in octave

I am doing the coursera machine learning logistic regression assignment and I wrote the top piece of code.
Instructions
function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
% J = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly
g = zeros(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
% vector or scalar).
g = (1 / (1 + exp(-1 * z)) ) .^ 1;
%g = (1 + exp(-1 * z)) .^ -1;
% =============================================================
end
My Code --- Breaks when inputting a vector or matrix
g = (1 / (1 + exp(-1 * z)) ) .^ 1;
I found this code on some github ---- Works for all cases
g = (1 + exp(-1 * z)) .^ -1;
The issue is the output works when my input is a scalar. My code breaks when it becomes a vector. May i know what am i missing as it appears to me to be the same
You need to change / which is a matrix right division to be an element-wise division (./)
g = (1 ./ (1 + exp(-1 * z)) ) .^ 1;
The use of / will work just fine for scalars, but when one of the inputs is a matrix (in this case the right side of the operator), the meaning changes and you will get either an error or unexpected results.

How to solve this equation for solving "Finding duplicate in integer array"

I was looking at the problem and the discussion here: Easy interview question got harder: given numbers 1..100, find the missing number(s)
One of the user provided a solution using following equation.
k1 + k2 = x
k1^2 + k2^2 = y
Substituting provides (x-k2)^2 + k2^2 = y
I am trying to solve this equation further and come up with a C program to solve the problem of finding duplicates.
Inspite of spending lot of time I couldn't solve this equation to get k1 or k2 one side. I always ended up with k1 or k2 on both side of equation.
Any help is appreciated.
Expand the equation
(x - k2)^2 + k2^2 = y
and get
x^2 - 2xk2 + 2k2^2 = y
or
2k2^2 - 2xk2 + x^2 - y = 0
Now use the formula for solving the quadratic equation az^2 + bz + c = 0 which is (-b +/- sqrt(b^2 - 4ac))/2a. Only that in our case z=k2. So
k2 = (2x +/- sqrt(4x^2 - 8(x^2 - y))) / 4
or
k2 = (x +/- sqrt(x^2 - 2(x^2 - y))) / 2
= (x +/- sqrt(2y - x^2)) / 2
and you can put
k2 = (x + sqrt(2y - x^2)) / 2
k1 = (x - sqrt(2y - x^2)) / 2.

How to reflect a line over another line

For a collision algorithm I am developing, I need to find out how to reflect a line over another.
Line 1:
y=ax+b
Line 2:
y=cx+d
Line 3:
(a result of reflecting line 1 over line 2) y=ex+f
Is there any algebraic way to determine e and f in terms of a, b, c, and d?
I have run over this exact same problem before. Stay with me here...
This problem involves two parts:
1. Find the point at which they intersect
to find where two lines intersect, we use the two equations of the lines:
y = M1x + B1
y = M2x + B2
Using substitution:
M1x + B1 = M2x + B2
M1x - M2x = B2 - B1
x(M1 - M2) = B2 - B1
x = (B2 - B1) / (M1 - M2)
To find the y value, just plug it in:
y = M1x + B1
2. Find the slope of the line from the other two slopes.
The second is far trickier. Using trigonometry, it is not impossible.
Let L1 be the "base line." (With a slope of M1)
Let L2 be the line that is to be reflected over the "base line." (With a slope of M2)
Let L3 be our resulting line. (With a slope of M3)
The equation I used is as follows:
double M3 = ((2 * M1) + (M2 * pow(M1, 2)) - M2) / (2 * M1 * M2 - pow(M1, 2) + 1);
Straight from my C code.
It is important to note that both slopes should be defined. You can use L'Hopital's rule to get an equation when one of the slopes is approaching infinity.
ONWARD WITH THE EXPLANATION!
Here is a crude drawing of three lines.
L2 is reflected over L1, resulting in L3. Drawing is not exact.
The angle between L1 and L2, as well as L2 and L3, is labelled as R.\
Here are the facts:
M1 = tan(A1)
M2 = tan(A2)
M3 = tan(A3)
This comes from the definition of tangent.
A3 = R + A1
This is a little trickier to see, but if you draw a horizontal line at the point of intersection it becomes obvious.
Thus, our goal is to find tan(A3). To accomplish this, we need to find R. As we can see, R can be found in a triangle with A2 and the supplement of A1 as the other angles. Thus, we know:
R + (180 - A1) + A2 = 180
R - A1 + A2 = 0
R = A1 - A2
Let's take the tangent of both sides:
tan(R) = tan(A1 - A2)
From trigonometry, we know:
tan(R) = (tan(A1) - tan(A2)) / (1 + tan(A1)tan(A2))
R = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2))
Arctan being inverse tangent. From our earlier formula, A3 = R + A1, we get:
A3 = arctan((tan(A1) - tan(A2) / (1 + tan(A1)tan(A2)) + A1
A3 = arctan((M1 - M2) / (1 + M1*M2)) + A1
But we don't want A3. We want tan(A3). So again, we take the tangent of both sides.
tan(A3) = M3 = tan(arctan((M1 - M2) / (1 + M1*M2)) + A1)
M3 = tan(arctan((M1 - M2) / (1 + M1*M2))) + tan(A1) / (1 - tan(arctan((M1 - M2) / (1 + M1*M2))) * tan(A1))
Unfortunately, that's disgustingly hideous. Replacing tangents with slopes and simplifying, we get
M3 = ((M1 - M2) / (1 + M1*M2)) + M1 / (1 - ((M1 - M2)/(1 + M1*M2)) * M1)
M3 = (M1 - M2 + M1*(1 + M1*M2)) / (1 + M1*M2 - M1*M1 + M1*M2)
M3 = (M1^2 * M2 + 2*M1 - M2) / (1 + 2*M1*M2 - M1^2)
Which is the exact same as the formula above. Sorry about all the ugly math. When M2 is completely vertical, you can use L'Hopital's rule to get
M3 = (M1^2 - 1) / 2*M1
If anyone is so inclined, check my math. But I'm tired right about now.
Assuming the two lines are not parallel to each other
Step 1:
First find the intersection of the line y = ax + b with line y = cx + d , that is by solving comes out to be
m = (d - b) / (c - a)
Step 2:
The final line has point of the form (x , ex + f) , so wjat we know is the line joining the point and the corresponding image is perpendicular to the mirror line AND the the midpoint of the first point and its image lies on the mirror line. Solving for first requirement ....
(Slope of line joining point and its image) * (Slope of the mirror line) = -1
we get ...
c * (e*pt + f - a*n - b)/( pt - n ) = -1 -----> The first equation .
Then the midpoint of the point and its image lie on the central line , i.e.
Y coordinate of the midpoint - ( c* x coordinate of midpoint + d) = 0
y coordinate of midpoint = (a*n + e*pt) / 2 and x coordinate = ( pt + n) / 2
putting it above we get...
(a*n + e*pt)c - c( pt + n) - 2d =0 ----> second equation
3.
now the point and its image make equal angles from the intersection point .... a simple way of saying that angle between mirror line , point line and image line , point line being equal ... therefore ... the tangent of angle between lines mI and mM is equal to that of mM and mP
equating we get
( mM + mP ) / ( 1 + mp*mM) =( mI - mM )/ (1 + mI*mM)
where mM = c , mI = e, and mP = a -----> third equation
put it in their respective
places you get three equations in three unknowns , pt , e and f and solve ... just x in place of n earlier and there you have your e , f in terms of a , b , c , d.
Solve it yourselves ....
However if they are parallel its simple , you have two equations in two variables use the midpoint method
Yet another method:
Matrix of affine transformation for reflection relatively to line y=ax+b (works for non-vertical lines!).
Let's pa = 1+a^2, ma = 1-a^2, then matrix is (from Nikulin's Computer Geometry book)
ma/pa 2a/pa 0
2a/pa -ma/pa 0
-2ab/pa 2b/pa 1
So we can get two arbitrary points at second line, apply this transform, and calculate new line equation

Resources