#NEWLINE# def pade( an , m ) : #NEWLINE# #INDENT# '\n    Return Pade approximation to a polynomial as the ratio of two polynomials.\n\n    Parameters\n    ----------\n    an : (N,) array_like\n        Taylor series coefficients.\n    m : int\n        The order of the returned approximating polynomials.\n\n    Returns\n    -------\n    p, q : Polynomial class\n        The pade approximation of the polynomial defined by `an` is\n        `p(x)/q(x)`.\n\n    Examples\n    --------\n    >>> from scipy import misc\n    >>> e_exp = [1.0, 1.0, 1.0/2.0, 1.0/6.0, 1.0/24.0, 1.0/120.0]\n    >>> p, q = misc.pade(e_exp, 2)\n\n    >>> e_exp.reverse()\n    >>> e_poly = np.poly1d(e_exp)\n\n    Compare ``e_poly(x)`` and the pade approximation ``p(x)/q(x)``\n\n    >>> e_poly(1)\n    2.7166666666666668\n\n    >>> p(1)/q(1)\n    2.7179487179487181\n\n    ' #NEWLINE# from scipy import linalg #NEWLINE# an = asarray ( an ) #NEWLINE# N = ( len ( an ) - 1 ) #NEWLINE# n = ( N - m ) #NEWLINE# if ( n < 0 ) : #NEWLINE# #INDENT# raise ValueError ( 'Order of q <m> must be smaller than len(an)-1.' ) #NEWLINE# #UNINDENT# Akj = eye ( ( N + 1 ) , ( n + 1 ) ) #NEWLINE# Bkj = zeros ( ( ( N + 1 ) , m ) , 'd' ) #NEWLINE# for row in range ( 1 , ( m + 1 ) ) : #NEWLINE# #INDENT# Bkj [ row , : row ] = ( - Akj [ : row ] [ : : ( - 1 ) ] ) #NEWLINE# #UNINDENT# for row in range ( ( m + 1 ) , ( N + 1 ) ) : #NEWLINE# #INDENT# Bkj [ row , : ] = ( - an [ ( row - m ) : row ] [ : : ( - 1 ) ] ) #NEWLINE# #UNINDENT# C = hstack ( ( Akj , Bkj ) ) #NEWLINE# pq = linalg . solve ( C , an ) #NEWLINE# p = pq [ : ( n + 1 ) ] #NEWLINE# q = r_ [ ( 1.0 , pq [ ( n + 1 ) : ] ) ] #NEWLINE# return ( poly1d ( p [ : : ( - 1 ) ] ) , poly1d ( q [ : : ( - 1 ) ] ) )