<s> from __future__ import division , print_function , absolute_import <EOL> import math <EOL> import numpy as np <EOL> from scipy . _lib . six import xrange <EOL> from scipy . _lib . six import string_types <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def tri ( N , M = None , k = <NUM_LIT:0> , dtype = None ) : <EOL> """<STR_LIT>""" <EOL> if M is None : <EOL> M = N <EOL> if isinstance ( M , string_types ) : <EOL> dtype = M <EOL> M = N <EOL> m = np . greater_equal ( np . subtract . outer ( np . arange ( N ) , np . arange ( M ) ) , - k ) <EOL> if dtype is None : <EOL> return m <EOL> else : <EOL> return m . astype ( dtype ) <EOL> def tril ( m , k = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> m = np . asarray ( m ) <EOL> out = tri ( m . shape [ <NUM_LIT:0> ] , m . shape [ <NUM_LIT:1> ] , k = k , dtype = m . dtype . char ) * m <EOL> return out <EOL> def triu ( m , k = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> m = np . asarray ( m ) <EOL> out = ( <NUM_LIT:1> - tri ( m . shape [ <NUM_LIT:0> ] , m . shape [ <NUM_LIT:1> ] , k - <NUM_LIT:1> , m . dtype . char ) ) * m <EOL> return out <EOL> def toeplitz ( c , r = None ) : <EOL> """<STR_LIT>""" <EOL> c = np . asarray ( c ) . ravel ( ) <EOL> if r is None : <EOL> r = c . conjugate ( ) <EOL> else : <EOL> r = np . asarray ( r ) . ravel ( ) <EOL> vals = np . concatenate ( ( r [ - <NUM_LIT:1> : <NUM_LIT:0> : - <NUM_LIT:1> ] , c ) ) <EOL> a , b = np . ogrid [ <NUM_LIT:0> : len ( c ) , len ( r ) - <NUM_LIT:1> : - <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> indx = a + b <EOL> return vals [ indx ] <EOL> def circulant ( c ) : <EOL> """<STR_LIT>""" <EOL> c = np . asarray ( c ) . ravel ( ) <EOL> a , b = np . ogrid [ <NUM_LIT:0> : len ( c ) , <NUM_LIT:0> : - len ( c ) : - <NUM_LIT:1> ] <EOL> indx = a + b <EOL> return c [ indx ] <EOL> def hankel ( c , r = None ) : <EOL> """<STR_LIT>""" <EOL> c = np . asarray ( c ) . ravel ( ) <EOL> if r is None : <EOL> r = np . zeros_like ( c ) <EOL> else : <EOL> r = np . asarray ( r ) . ravel ( ) <EOL> vals = np . concatenate ( ( c , r [ <NUM_LIT:1> : ] ) ) <EOL> a , b = np . ogrid [ <NUM_LIT:0> : len ( c ) , <NUM_LIT:0> : len ( r ) ] <EOL> indx = a + b <EOL> return vals [ indx ] <EOL> def hadamard ( n , dtype = int ) : <EOL> """<STR_LIT>""" <EOL> if n < <NUM_LIT:1> : <EOL> lg2 = <NUM_LIT:0> <EOL> else : <EOL> lg2 = int ( math . log ( n , <NUM_LIT:2> ) ) <EOL> if <NUM_LIT:2> ** lg2 != n : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> H = np . array ( [ [ <NUM_LIT:1> ] ] , dtype = dtype ) <EOL> for i in range ( <NUM_LIT:0> , lg2 ) : <EOL> H = np . vstack ( ( np . hstack ( ( H , H ) ) , np . hstack ( ( H , - H ) ) ) ) <EOL> return H <EOL> def leslie ( f , s ) : <EOL> """<STR_LIT>""" <EOL> f = np . atleast_1d ( f ) <EOL> s = np . atleast_1d ( s ) <EOL> if f . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if s . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if f . size != s . size + <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if s . size == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> tmp = f [ <NUM_LIT:0> ] + s [ <NUM_LIT:0> ] <EOL> n = f . size <EOL> a = np . zeros ( ( n , n ) , dtype = tmp . dtype ) <EOL> a [ <NUM_LIT:0> ] = f <EOL> a [ list ( range ( <NUM_LIT:1> , n ) ) , list ( range ( <NUM_LIT:0> , n - <NUM_LIT:1> ) ) ] = s <EOL> return a <EOL> def kron ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if not a . flags [ '<STR_LIT>' ] : <EOL> a = np . reshape ( a , a . shape ) <EOL> if not b . flags [ '<STR_LIT>' ] : <EOL> b = np . reshape ( b , b . shape ) <EOL> o = np . outer ( a , b ) <EOL> o = o . reshape ( a . shape + b . shape ) <EOL> return np . concatenate ( np . concatenate ( o , axis = <NUM_LIT:1> ) , axis = <NUM_LIT:1> ) <EOL> def block_diag ( * arrs ) : <EOL> """<STR_LIT>""" <EOL> if arrs == ( ) : <EOL> arrs = ( [ ] , ) <EOL> arrs = [ np . atleast_2d ( a ) for a in arrs ] <EOL> bad_args = [ k for k in range ( len ( arrs ) ) if arrs [ k ] . ndim > <NUM_LIT:2> ] <EOL> if bad_args : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % bad_args ) <EOL> shapes = np . array ( [ a . shape if a . size > <NUM_LIT:0> else [ <NUM_LIT:0> , <NUM_LIT:0> ] for a in arrs ] ) <EOL> out_dtype = np . find_common_type ( [ arr . dtype for arr in arrs ] , [ ] ) <EOL> out = np . zeros ( np . sum ( shapes , axis = <NUM_LIT:0> ) , dtype = out_dtype ) <EOL> r , c = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for i , ( rr , cc ) in enumerate ( shapes ) : <EOL> out [ r : r + rr , c : c + cc ] = arrs [ i ] <EOL> r += rr <EOL> c += cc <EOL> return out <EOL> def companion ( a ) : <EOL> """<STR_LIT>""" <EOL> a = np . atleast_1d ( a ) <EOL> if a . ndim != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if a . size < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if a [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> first_row = - a [ <NUM_LIT:1> : ] / ( <NUM_LIT:1.0> * a [ <NUM_LIT:0> ] ) <EOL> n = a . size <EOL> c = np . zeros ( ( n - <NUM_LIT:1> , n - <NUM_LIT:1> ) , dtype = first_row . dtype ) <EOL> c [ <NUM_LIT:0> ] = first_row <EOL> c [ list ( range ( <NUM_LIT:1> , n - <NUM_LIT:1> ) ) , list ( range ( <NUM_LIT:0> , n - <NUM_LIT:2> ) ) ] = <NUM_LIT:1> <EOL> return c <EOL> def helmert ( n , full = False ) : <EOL> """<STR_LIT>""" <EOL> H = np . tril ( np . ones ( ( n , n ) ) , - <NUM_LIT:1> ) - np . diag ( np . arange ( n ) ) <EOL> d = np . arange ( n ) * np . arange ( <NUM_LIT:1> , n + <NUM_LIT:1> ) <EOL> H [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> d [ <NUM_LIT:0> ] = n <EOL> H_full = H / np . sqrt ( d ) [ : , np . newaxis ] <EOL> if full : <EOL> return H_full <EOL> else : <EOL> return H_full [ <NUM_LIT:1> : ] <EOL> def hilbert ( n ) : <EOL> """<STR_LIT>""" <EOL> values = <NUM_LIT:1.0> / ( <NUM_LIT:1.0> + np . arange ( <NUM_LIT:2> * n - <NUM_LIT:1> ) ) <EOL> h = hankel ( values [ : n ] , r = values [ n - <NUM_LIT:1> : ] ) <EOL> return h <EOL> def invhilbert ( n , exact = False ) : <EOL> """<STR_LIT>""" <EOL> from scipy . special import comb <EOL> if exact : <EOL> if n > <NUM_LIT> : <EOL> dtype = object <EOL> else : <EOL> dtype = np . int64 <EOL> else : <EOL> dtype = np . float64 <EOL> invh = np . empty ( ( n , n ) , dtype = dtype ) <EOL> for i in xrange ( n ) : <EOL> for j in xrange ( <NUM_LIT:0> , i + <NUM_LIT:1> ) : <EOL> s = i + j <EOL> invh [ i , j ] = ( ( - <NUM_LIT:1> ) ** s * ( s + <NUM_LIT:1> ) * <EOL> comb ( n + i , n - j - <NUM_LIT:1> , exact ) * <EOL> comb ( n + j , n - i - <NUM_LIT:1> , exact ) * <EOL> comb ( s , i , exact ) ** <NUM_LIT:2> ) <EOL> if i != j : <EOL> invh [ j , i ] = invh [ i , j ] <EOL> return invh <EOL> def pascal ( n , kind = '<STR_LIT>' , exact = True ) : <EOL> """<STR_LIT>""" <EOL> from scipy . special import comb <EOL> if kind not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if exact : <EOL> if n >= <NUM_LIT> : <EOL> L_n = np . empty ( ( n , n ) , dtype = object ) <EOL> L_n . fill ( <NUM_LIT:0> ) <EOL> else : <EOL> L_n = np . zeros ( ( n , n ) , dtype = np . uint64 ) <EOL> for i in range ( n ) : <EOL> for j in range ( i + <NUM_LIT:1> ) : <EOL> L_n [ i , j ] = comb ( i , j , exact = True ) <EOL> else : <EOL> L_n = comb ( * np . ogrid [ : n , : n ] ) <EOL> if kind is '<STR_LIT>' : <EOL> p = L_n <EOL> elif kind is '<STR_LIT>' : <EOL> p = L_n . T <EOL> else : <EOL> p = np . dot ( L_n , L_n . T ) <EOL> return p <EOL> def invpascal ( n , kind = '<STR_LIT>' , exact = True ) : <EOL> """<STR_LIT>""" <EOL> from scipy . special import comb <EOL> if kind not in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if kind == '<STR_LIT>' : <EOL> if exact : <EOL> if n > <NUM_LIT> : <EOL> dt = object <EOL> else : <EOL> dt = np . int64 <EOL> else : <EOL> dt = np . float64 <EOL> invp = np . empty ( ( n , n ) , dtype = dt ) <EOL> for i in range ( n ) : <EOL> for j in range ( <NUM_LIT:0> , i + <NUM_LIT:1> ) : <EOL> v = <NUM_LIT:0> <EOL> for k in range ( n - i ) : <EOL> v += comb ( i + k , k , exact = exact ) * comb ( i + k , i + k - j , <EOL> exact = exact ) <EOL> invp [ i , j ] = ( - <NUM_LIT:1> ) ** ( i - j ) * v <EOL> if i != j : <EOL> invp [ j , i ] = invp [ i , j ] <EOL> else : <EOL> invp = pascal ( n , kind = kind , exact = exact ) <EOL> if invp . dtype == np . uint64 : <EOL> invp = invp . view ( np . int64 ) <EOL> invp *= toeplitz ( ( - <NUM_LIT:1> ) ** np . arange ( n ) ) . astype ( invp . dtype ) <EOL> return invp <EOL> def dft ( n , scale = None ) : <EOL> """<STR_LIT>""" <EOL> if scale not in [ None , '<STR_LIT>' , '<STR_LIT:n>' ] : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( scale , ) ) <EOL> omegas = np . exp ( - <NUM_LIT> * np . pi * np . arange ( n ) / n ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> m = omegas ** np . arange ( n ) <EOL> if scale == '<STR_LIT>' : <EOL> m /= math . sqrt ( n ) <EOL> elif scale == '<STR_LIT:n>' : <EOL> m /= n <EOL> return m </s>
