Representing mathematical equations
Rustom Mody
rustompmody at gmail.com
Mon Oct 6 19:48:39 CEST 2014
On Monday, October 6, 2014 10:52:40 PM UTC+5:30, Dave Angel wrote:
> varun7rs Wrote in message:
> > On Monday, 6 October 2014 15:03:44 UTC+2, Varun wrote:
> (Deleted all the 8-space quoting. Either use a better email client
> or remove the extra 7 lines between every line you
> quote.)
> >> Okay, I forgot to explain them. L is a set of links, dist is a number (distance), bd is the bandwidth and hc is a number as well (hopcount)...different bandwidths, hopcounts and distances for different links...
> >> b(i,x) is what i intend to calculate out of these details...here 'i' could be the head or tail of the link l and x is a node that hosts i
> > Unbelievable. I again forgot to express them
> > L = [(1,3), (5,7), .....]
> > bd = [23, 34,43.44.....]
> > dist = [3,7,5,7, ....]
> > hc = [2,3,4,1,2,2,...]
> > for every l belonging to L, i could be either 1 or 3 for L[0], similarly for L[1] it could be 5 or 7
> Now it's clear I don't have a clue. L is either a set or a list.
> Little l is an element of that set, and is either a tuple or is
> mysteriously subscripted by a boolean at the other end of the
> expression. ..
As Dave says there are too many loose ends in your spec to make much sense.
Anyway heres a small start to help you start off --
both for cleaning up your spec as well as moving towards an implementation
Take your denominator term: Σ(l∈L) l
You also say "i could be head or tail of l"
Dunno what to make of that 'or'... Toss a coin and choose??
So here are two version that will calculate the denominator for head and for tail
>>> L = [(1,3), (5,7)]
>>> sum(x for (x,y) in L)
6
>>> sum(y for (x,y) in L)
10
If you need to examine the sub-expressions (always a good idea!), use
>>> (x for (x,y) in L)
<generator object <genexpr> at 0x7fe04025c730>
Whazzat?
>>> list(x for (x,y) in L)
[1, 5]
Or just simply
>>> [x for (x,y) in L]
[1, 5]
You can use that literally in the sum
>>> sum([x for (x,y) in L])
6
>>> sum([(x,y) for (x,y) in L])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
Python just expressing that you are goofing off by trying to add tuples
rather than numbers
>>> sum([l for l in L])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
Same as above
More information about the Python-list
mailing list