13

I am trying to evaluate a formula, np is numpy:

Ds = pow(10,5)
D = np.linspace(0, pow(10,6), 100)
alpha=1.44
beta=0.44
A=alpha*(D/Ds)
L=1.65
buf2=L/4.343
buf=pow(-(alpha*[D/Ds]),beta)
value=exp(buf)

and then I will plot this data but I get:

buf=pow(-(alpha*[D/Ds]),beta)
TypeError: can't multiply sequence by non-int of type 'float'

How can I overcome this?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
y33t
  • 649
  • 4
  • 14
  • 23
  • It's worth noting that you've already computing `A=alpha*(D/Ds)` properly up above, so… why not just use `buf=pow(-A, beta)`? – abarnert Jun 08 '13 at 00:25

1 Answers1

27

Change:

buf=pow(-(alpha*[D/Ds]),beta)

to:

buf=pow(-(alpha*(D/Ds)),beta)

This:

[D/Ds]

gives you list with one element.

But this:

alpha * (D/Ds)

computes the divisions before the multiplication with alpha.

You can multiply a list by an integer:

>>> [1] * 4
[1, 1, 1, 1]

but not by a float:

[1] * 4.0
TypeError: can't multiply sequence by non-int of type 'float'

since you cannot have partial elements in a list.

Parenthesis can be used for grouping in the mathematical calculations:

>>> (1 + 2) * 4
12
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • 1
    And to make it explicit: change the square brackets around `D/Ds` (which define a list) into parenthesis. – Martijn Pieters Jun 08 '13 at 00:15
  • thanks. Interestingly now I got 'nan' as returned. Any ideas ? – y33t Jun 08 '13 at 00:19
  • @y33t That would probably be a matter for a separate question. – David Z Jun 08 '13 at 00:21
  • @y33t: Given that `alpha` and `Ds` are positive, and all of the `D` values are positive, you're raising a bunch of negative numbers to a fractional power. The solution depends on the problem: did you want complex numbers, or did you get your algorithm wrong? – abarnert Jun 08 '13 at 00:26
  • @abarnert yes I mistyped ; buf=exp(-(alpha*(D/Ds)),beta) but now I get TypeError: bad operand type for unary -: 'tuple' – y33t Jun 08 '13 at 00:37
  • @y33t: First, if you have a new question, open a new question; answering followups via comments is next to impossible. Second, if you're getting confused by a complicated expression, break it up into simple pieces by storing each intermediate result in a variable: `DDs = D/Ds`, then `aDDs = alpha*DDs`, etc. Then, the problem and its solution will likely be obvious to you—and, if it isn't, it will almost certainly be obvious to someone on SO. – abarnert Jun 08 '13 at 00:45
  • @y33t: Again, you already _did_ half of that, correctly, with `A=alpha*(D/Ds)`. So, why not actually _use_ `A` in your expression, instead of using `alpha*(D/Ds)` again? – abarnert Jun 08 '13 at 00:45