# Reaktor/Tutorials/Applied algebra for Reaktor users
## What you should know

Algebra is by far the most important form of mathematics needed when it comes to working with Reaktor. Ideally one should have an understanding of high school algebra, along with at least some peripheral knowledge of trigonometry. One generally does not encounter circumstances in Reaktor where things such as the quadratic equation are needed, but generally speaking you should be comfortable with solving single variable linear equations (i.e. solving for x if: $2x+10=4x$) and double variable linear equations (i.e. solving for y if: $3y=2x+6y+20$). Polynomials with terms to powers greater than one are not encountered too often (i.e. $y=x^{2}+3x+4$), but you may encounter them when doing such things as shaping linear controller data into a curve.

## Resources

* http://www.mathleague.com/help/algebra/algebra.htm -- A basic introduction to algebra with a focus on turning sentences into equations (you can ignore the section on sequences)
* http://www.ifigure.com/math/algebra/algebra.htm -- Contains several dozen useful web-based algebraic calculators

## Useful formulas
### Scaling data from one range to another

$f(x)={\frac {M'-m'}{M-m}}\cdot (x-m)+m'$
* x = input
* f(x) = output
* m = min value of input
* M = max value of input
* m' = min value of output
* M' = max value of output
As an example, say you have an LFO which is outputting in a range from -1 to 1. However, you want the LFO to be outputting in a range from 30 to 50. We can use the above formula to construct a function which transforms our input from the old range to the new range: We simply plug in our values and then simplify.
* $f(x)={\frac {(50)-(30)}{(1)-(-1)}}\cdot (x-(-1))+(30)$
* $f(x)={\frac {20}{1+1}}\cdot (x+1)+30$
* $f(x)=10\cdot (x+1)+30$
NOTE: The dot ( $\cdot$ ) signifies multiplication. Subtracting a negative is the same as adding.
Taking into account order of operations (from highest priority to lowest: parenthesis, exponents, multiplication and division, addition and subtraction), we see that we can transform our input via three easy steps:
* Add 1 to the input -- This shifts the range (-1..1) to (0..2).
* Multiply the result of that by 10 -- This scales the range of (0..2) to (0..20).
* Add 30 to the result of the multiplication -- This shifts the range of (0..20) to (30..50), our goal.
Here is one more example, where we transform our input with a range of -300 to 24 into data with a range of 100 to 10000.
* $f(x)={\frac {(10000)-(100)}{(24)-(-300)}}\cdot (x-(-300))+(100)$
* $f(x)={\frac {9900}{324}}\cdot (x+300)+(100)$
NOTE: We do not reduce the fraction because 9900 ÷ 324 produces a repeating decimal (30.55555...).
Hence we get the following steps:
* Divide 9900 by 324 -- This gives us 30.555555...
* Add 300 to the input -- This shifts the range (-300..24) to (0..324).
* Multiply the result of step 1 by the result of step 2 -- This scales the range (0..324) to (0..9900).
* Add 100 to the result of step 3 -- This shifts the range of (0..9900) to (100..10000), our goal.
As you can see, linearly scaling data is as simple as shift, scale, shift.

### MIDI Pitch => Frequency (Hz)

$f(x)=440\cdot 2^{\frac {x-69}{12}}\approx 440\cdot e^{0.057762265\cdot (x-69)}$
* $x$ = pitch
* $f(x)$ = frequency
* $e\approx 2.71828183$

### Frequency (Hz) => MIDI Pitch

$f(x)=69+12\cdot {\frac {\log \left({\frac {x}{440}}\right)}{\log(2)}}\approx {\frac {\ln \left({\frac {x}{8.175798}}\right)}{\ln 1.0594633}}$
* $x$ = frequency
* $f(x)$ = pitch
* $\ln$ = the natural log

### How many times MIDI pitch A is slower than MIDI pitch B (A < B)

$f(A,B)=2^{\frac {B-A}{12}}\approx e^{0.057762265\cdot (B-A)}$
* A = MIDI pitch A
* B = MIDI pitch B
* f(x) = output
* $e\approx 2.71828183$