Starter
Starter

Reputation: 417

Evaluating algebraic expressions with combinations

I need help with evaluating the expression. I just started it but at a loss for what next plus all the for loops I am using seem unnecessary. it has sum, products and combinations:

a complicated mathematical equations

What I tried is incomplete and in my opinion not accurate. I tried several but all I can come up with for now. I don't have the denominator yet.

i = 10
N = 3.1
j = []
for x in range(1, i + 1):
    for y in range(1, i):
        for z in range(1, n - i):            
            l = N * y * z
            j.append(l)
            ll = sum(j)

Any help is appreciated. I want to be able to understand it so I can do more complex examples.

Upvotes: 0

Views: 194

Answers (1)

Rory Daulton
Rory Daulton

Reputation: 22544

Here are some hints. If you try them and are still stuck, ask for more help.

First, you know that the expression involves "combinations," also called "binomial coefficients." So you will need a routine that calculates those. Here is a question with multiple answers on how to calculate these numbers. Briefly, you can use the scipy package or make your own routine that uses Python's factorial function or that uses iteration.

Next, you see that the expression involves sums and products and is written as a single expression. Python has a sum function which works on generator expressions (as well as list and set generators and other iterables). Your conversion from math to Python will be easier if you know how to set up such expressions. If you do not understand these generators/iterables and how to sum them, do research on this topic. This approach is not necessary, since you could use loops rather than the generators, but this approach will be easier. Study until you can understand an expression (including why the final number in the range has 1 added to it) such as

sum(N * f(x) for x in range(1, 5+1))

Last, your expression has products, but Python has no built-in way to take the product of an iterable. Here is such a function in Python 3.

from operator import mul
from functools import reduce

def prod(iterable):
    """Return the product of the numbers in an iterable."""
    return reduce(mul, iterable, 1)

With all of that, your desired expression will look like this (you will need to finish the job by replacing the ... with something more useful):

numerator = sum(N * prod(... for y in range(1, 1+1)) for x in range(1, 5+1))
denominator = prod(y + N for y in range(1, 5+1))
result = numerator / denominator

Note that your final result is a function of N.

Upvotes: 1

Related Questions