Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify algebra in fast-inverse-sqrt.md #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 19 additions & 61 deletions 2024/5/29/fast-inverse-sqrt.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,100 +429,58 @@ $$

No divisions, only multiplications! The exact steps to go from the first form to this one are *numerous* to say the least, but I've included it in full for completeness. Feel free to skip over it and pick back up on the code below.

$$
y_{n+1} = y_n - \frac{\frac{1 - xy_n^2}{y_n^2}}{-\frac{2}{y_n^3}}
$$

$$
y_{n+1} = y_n - \frac{1 - xy_n^2}{y_n^2} \cdot -\frac{y_n^3}{2}
$$
Starting equation:

$$
y_{n+1} = y_n - \frac{-1 - xy_n^2}{y_n^2} \cdot \frac{y_n^3}{2}
$$

$$
y_{n+1} = y_n - \frac{-1 - xy_n^2}{\cancel{y_n^2}} \cdot \frac{\cancel{y_n^2}y_n}{2}
$$

$$
y_{n+1} = y_n - (-1 - xy_n^2 \cdot \frac{y_n}{2})
$$

$$
y_{n+1} = y_n - (-(1-xy_n^2) \cdot \frac{y_n}{2})
$$

$$
y_{n+1} = y_n - (-1\cdot(1-xy_n^2) \cdot \frac{y_n}{2})
$$

$$
y_{n+1} = y_n - (-1\cdot1 + -1\cdot(-xy_n^2) \cdot \frac{y_n}{2})
y_{n+1} = y_n - \frac{\frac{1}{y_n^2} - x}{-\frac{2}{y_n^3}}
$$

$$
y_{n+1} = y_n - (-1\frac{y_n}{2} + xy_n^2\frac{y_n}{2})
$$
Recognize that dividing by a fraction is the same as multiplying by its reciprocal:

$$
y_{n+1} = y_n - (-\frac{y_n}{2} + \frac{xy_n^2y_n}{2})
y_{n+1} = y_n - \left(\frac{1}{y_n^2} - x\right) \cdot \left(-\frac{y_n^3}{2}\right)
$$

$$
y_{n+1} = y_n - (\frac{-y_n+xy_n^3}{2})
$$
Distribute the reciprocal term to the binomial inside the parentheses:

$$
y_{n+1} = y_n - (\frac{y_n \cdot -1 +xy_n^3)}{2})
y_{n+1} = y_n - \left(\frac{1}{y_n^2} \cdot -\frac{y_n^3}{2} - x \cdot -\frac{y_n^3}{2}\right)
$$

$$
y_{n+1} = y_n - (\frac{y_n \cdot -1 + y_n(xy_n^2)}{2})
$$
Simplify each term:

$$
y_{n+1} = y_n - (\frac{y_n(-1 + xy_n^2)}{2})
y_{n+1} = y_n - \left(-\frac{y_n}{2} + \frac{x \cdot y_n^3}{2}\right)
$$

$$
y_{n+1} = y_n \cdot \frac{2}{2} - \frac{y_n(-1 + xy_n^2)}{2}
$$
Distribute the negative sign:

$$
y_{n+1} = \frac{2y_n}{2} - \frac{y_n(-1 + xy_n^2)}{2}
y_{n+1} = y_n - \left(-\frac{y_n}{2}\right) - \left(\frac{x \cdot y_n^3}{2}\right)
$$

$$
y_{n+1} = \frac{2y_n - y_n(-1 + xy_n^2)}{2}
$$
Combine like terms:

$$
y_{n+1} = \frac{2y_n + y_n(-1(-1 + xy_n^2))}{2}
y_{n+1} = y_n + \frac{y_n}{2} - \frac{x \cdot y_n^3}{2}
$$

$$
y_{n+1} = \frac{y_n(2 -1(-1 + xy_n^2))}{2}
$$
Factor out \( y_n \):

$$
y_{n+1} = \frac{y_n(2 + 1 -xy_n^2)}{2}
y_{n+1} = y_n\left(1 + \frac{1}{2} - \frac{x \cdot y_n^2}{2}\right)
$$

$$
y_{n+1} = \frac{y_n(3 -xy_n^2)}{2}
$$
Simplify the constants:

$$
y_{n+1} = y_n(\frac{3}{2} - \frac{xy_n^2}{2})
y_{n+1} = y_n\left(\frac{3}{2} - \frac{x \cdot y_n^2}{2}\right)
$$

$$
y_{n+1} = y_n(\frac{3}{2} - \frac{x}{2} y_n^2)
$$
Convert 3/2 to 1.5 and x/2 to 0.5x

$$
y_{n+1} = y_n \cdot (1.5 - (0.5x \cdot y_n \cdot y_n))
y_{n+1} = y_n\left(1.5 - 0.5x \cdot y_n^2\right)
$$

So that is the last line of the function before the return:
Expand Down Expand Up @@ -551,4 +509,4 @@ But then I stopped to actually think about it, and while there are large differe

Some people look at algorithms like CORDIC and fast inverse square root, and think them only relics of the past; A technology with no utility in the modern world. I don't think I have to tell you that I disagree with that premise.

A lot of us get into this field because, as kids, we loved to crack things open and see how they worked (even if, sometimes, we couldn't put them back together afterwards). Algorithms such as these live in that same space for me. I've tried to keep that curious spark alive, and turn it on problems and technology that aren't immediately relevant to my everyday work. And the really crazy thing is that often the underlying elements *do* help me solve real problems! Knowledge is synthesisable, who would have thought.
A lot of us get into this field because, as kids, we loved to crack things open and see how they worked (even if, sometimes, we couldn't put them back together afterwards). Algorithms such as these live in that same space for me. I've tried to keep that curious spark alive, and turn it on problems and technology that aren't immediately relevant to my everyday work. And the really crazy thing is that often the underlying elements *do* help me solve real problems! Knowledge is synthesisable, who would have thought.