a function or a factor to scale a list of real numbers from one range to another

Solution 1:

So basically you have a list (list_sum) and you would like to scale it so that the minimum is some fixed number $a$ and the maximum is another fixed number $b$. In other words, you would like a function $f(x)$ that has the properties $$ f(\text{min})=a \quad \text{and}\quad f(\text{max})=b $$ We can make this with a linear function $f(x) = kx+c$. Plugging in the conditions, we get $$ \left\{ \begin{array}{ccc} \text{min}\cdot k &+& c &= a \\ \text{max}\cdot k &+& c &= b\\ \end{array} \right. $$ The values of $k$ and $c$ can be solved for: $$\tag{1} \left\{ \begin{array}{cl} k = & \frac{b-a}{\text{max}-\text{min}} \\ c = & \frac{a\cdot \text{max}-b\cdot \text{min}}{\text{max}-\text{min}} \end{array} \right. $$ Inserting the values and simplifying, we get $$ \tag{2} f(x) = \frac{b(x - \text{min}) + a(\text{max}-x)}{\text{max}-\text{min}} $$ A more computationally efficient way is to first calculate the coefficients from Equation ($1$) and then calculate $f(x) = kx+c$.