Want to solve a equation using Python, Sympy solver is an easy solution. (I wish twenty years ago I had access to this library so I might do a better job for my school homework)

I am taking a course fundamentals of supply chain from MIT on EDX, there is a small math equation at the beginning of the course when you need to calculate the coefficients for a SKU distribution following power law. To be more specific, the question assumes the distribution follows the power law y = a * x^b where y denotes the percentage of the items sold and x denotes the number of products sold. At a high level, this is a law behind a few popular products encompass majority of your sales.

Consider the example of a store where 5% of the products account for 66.6% of the items sold, and 50% of the products account for 95% of the items sold. – Course

In this case, we substitute x and y with the real store data, we have

0.666 = a * (0.05)^b
0.95 = a * (0.5)^b

Two variables, two independent equations. You can solve this equation by cancelling any unknown variable. Say let’s get started with cancelling variable a first. We have:

(0.05)^b / 0.666 = (0.5)^b / 0.95

Then we take the natural logarithm of each side, we have:

b * ln(0.05) - ln(0.666) = b * ln(0.5) - ln(0.95)

Finally we got,

b = (ln(0.95) -ln(0.666)) / (ln(0.5) -ln(0.05)) = 0.154
a = 0.95 / (0.5^0.154) = 1.06

Double check, a * (0.05)^b = 1.06 * (0.05)^0.154 = 0.668 ~ 0.666 (pretty close, probably because of the rounding which tends to get amplified in non-linear equations).

Well, as you can see, this equation is not the end of the world but it took me about 10 minutes to write down the equation and punch many buttons on my calculator to get the intermediate steps and what if I just want to get the result.

Here is how you can use Solver to get the result in a faster, more consistent and accurate way:

Here we go, 5 lines of code, do I really need to explain what happened? I do not think so, right? 🙂

Sympy seems like to have way more functionalities other than just solving elementary school algebraic problems, it also claims to cover ordinary differential equations, partial differential equations, system of polynomial equations and more than that.

(BTW, if you just want to quickly get your hands dirty, Sympy has a live code shell where you can test out the example code or your own problem)

### Like this:

Like Loading...

*Related*