[Numpy-discussion] Translation of a Matlab expresion

Charles R Harris charlesr.harris@gmail....
Mon Apr 9 12:12:14 CDT 2007


On 4/9/07, Miquel Poch <miquel.poch@gmail.com> wrote:
>
> Hi,
>
> I've got a function write in Matlab, and I need to tranlate it into
> python. I've found an expresion like this:
> ?
> BF = b0 + (b1 + (b2 + (b3 + (b4 + (b5 + b6*T).*T).*T).*T).*T).*T
> or
> dBFdT = b1 + (2 * b2 + (3 * b3 + (4 * b4 + (5 * b5 + 6*
> b6*T).*T).*T).*T).*T


It's Horner's method for evaluating polynomials and their derivatives. You
can also do this in numpy as:

In [5]: p1,p2,p3,p4,p5,p6 = [1,2,3,4,5,6]

In [6]: p = poly1d([p6,p5,p4,p3,p2,p1])

In [7]: T = array([[1,2],[3,4]])

In [8]: p(T)
Out[8]:
array([[  21,  321],
       [2005, 7737]])

In [9]: polyder(p)(T)
Out[9]:
array([[  70,  702],
       [3098, 9178]])

Note the reversal of the coefficient order. Just make sure your arrays are
floats, the example I gave is all integers.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20070409/427d71d7/attachment-0001.html 


More information about the Numpy-discussion mailing list