<s> from __future__ import division , print_function , absolute_import <EOL> from numpy import float32 , float64 , complex64 , complex128 , arange , array , zeros , shape , transpose , newaxis , common_type , conjugate <EOL> from scipy . linalg import _fblas as fblas <EOL> from scipy . _lib . six import xrange <EOL> from numpy . testing import TestCase , run_module_suite , assert_array_equal , assert_allclose , assert_array_almost_equal , assert_ <EOL> accuracy = <NUM_LIT:5> <EOL> def matrixmultiply ( a , b ) : <EOL> if len ( b . shape ) == <NUM_LIT:1> : <EOL> b_is_vector = True <EOL> b = b [ : , newaxis ] <EOL> else : <EOL> b_is_vector = False <EOL> assert_ ( a . shape [ <NUM_LIT:1> ] == b . shape [ <NUM_LIT:0> ] ) <EOL> c = zeros ( ( a . shape [ <NUM_LIT:0> ] , b . shape [ <NUM_LIT:1> ] ) , common_type ( a , b ) ) <EOL> for i in xrange ( a . shape [ <NUM_LIT:0> ] ) : <EOL> for j in xrange ( b . shape [ <NUM_LIT:1> ] ) : <EOL> s = <NUM_LIT:0> <EOL> for k in xrange ( a . shape [ <NUM_LIT:1> ] ) : <EOL> s += a [ i , k ] * b [ k , j ] <EOL> c [ i , j ] = s <EOL> if b_is_vector : <EOL> c = c . reshape ( ( a . shape [ <NUM_LIT:0> ] , ) ) <EOL> return c <EOL> class BaseAxpy ( object ) : <EOL> '''<STR_LIT>''' <EOL> def test_default_a ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = arange ( <NUM_LIT> , dtype = x . dtype ) <EOL> real_y = x * <NUM_LIT:1.> + y <EOL> y = self . blas_func ( x , y ) <EOL> assert_array_equal ( real_y , y ) <EOL> def test_simple ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = arange ( <NUM_LIT> , dtype = x . dtype ) <EOL> real_y = x * <NUM_LIT> + y <EOL> y = self . blas_func ( x , y , a = <NUM_LIT> ) <EOL> assert_array_equal ( real_y , y ) <EOL> def test_x_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:3> , x . dtype ) <EOL> y = arange ( <NUM_LIT> , dtype = x . dtype ) <EOL> real_y = x [ : : <NUM_LIT:2> ] * <NUM_LIT> + y <EOL> y = self . blas_func ( x , y , a = <NUM_LIT> , n = <NUM_LIT:3> , incx = <NUM_LIT:2> ) <EOL> assert_array_equal ( real_y , y ) <EOL> def test_y_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> real_y = x * <NUM_LIT> + y [ : : <NUM_LIT:2> ] <EOL> y = self . blas_func ( x , y , a = <NUM_LIT> , n = <NUM_LIT:3> , incy = <NUM_LIT:2> ) <EOL> assert_array_equal ( real_y , y [ : : <NUM_LIT:2> ] ) <EOL> def test_x_and_y_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> real_y = x [ : : <NUM_LIT:4> ] * <NUM_LIT> + y [ : : <NUM_LIT:2> ] <EOL> y = self . blas_func ( x , y , a = <NUM_LIT> , n = <NUM_LIT:3> , incx = <NUM_LIT:4> , incy = <NUM_LIT:2> ) <EOL> assert_array_equal ( real_y , y [ : : <NUM_LIT:2> ] ) <EOL> def test_x_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> try : <EOL> self . blas_func ( x , y , n = <NUM_LIT:4> , incx = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> def test_y_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> try : <EOL> self . blas_func ( x , y , n = <NUM_LIT:3> , incy = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> try : <EOL> class TestSaxpy ( TestCase , BaseAxpy ) : <EOL> blas_func = fblas . saxpy <EOL> dtype = float32 <EOL> except AttributeError : <EOL> class TestSaxpy : <EOL> pass <EOL> class TestDaxpy ( TestCase , BaseAxpy ) : <EOL> blas_func = fblas . daxpy <EOL> dtype = float64 <EOL> try : <EOL> class TestCaxpy ( TestCase , BaseAxpy ) : <EOL> blas_func = fblas . caxpy <EOL> dtype = complex64 <EOL> except AttributeError : <EOL> class TestCaxpy : <EOL> pass <EOL> class TestZaxpy ( TestCase , BaseAxpy ) : <EOL> blas_func = fblas . zaxpy <EOL> dtype = complex128 <EOL> class BaseScal ( object ) : <EOL> '''<STR_LIT>''' <EOL> def test_simple ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> real_x = x * <NUM_LIT> <EOL> x = self . blas_func ( <NUM_LIT> , x ) <EOL> assert_array_equal ( real_x , x ) <EOL> def test_x_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> real_x = x . copy ( ) <EOL> real_x [ : : <NUM_LIT:2> ] = x [ : : <NUM_LIT:2> ] * array ( <NUM_LIT> , self . dtype ) <EOL> x = self . blas_func ( <NUM_LIT> , x , n = <NUM_LIT:3> , incx = <NUM_LIT:2> ) <EOL> assert_array_equal ( real_x , x ) <EOL> def test_x_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> try : <EOL> self . blas_func ( <NUM_LIT> , x , n = <NUM_LIT:4> , incx = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> try : <EOL> class TestSscal ( TestCase , BaseScal ) : <EOL> blas_func = fblas . sscal <EOL> dtype = float32 <EOL> except AttributeError : <EOL> class TestSscal : <EOL> pass <EOL> class TestDscal ( TestCase , BaseScal ) : <EOL> blas_func = fblas . dscal <EOL> dtype = float64 <EOL> try : <EOL> class TestCscal ( TestCase , BaseScal ) : <EOL> blas_func = fblas . cscal <EOL> dtype = complex64 <EOL> except AttributeError : <EOL> class TestCscal : <EOL> pass <EOL> class TestZscal ( TestCase , BaseScal ) : <EOL> blas_func = fblas . zscal <EOL> dtype = complex128 <EOL> class BaseCopy ( object ) : <EOL> '''<STR_LIT>''' <EOL> def test_simple ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( shape ( x ) , x . dtype ) <EOL> y = self . blas_func ( x , y ) <EOL> assert_array_equal ( x , y ) <EOL> def test_x_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:3> , x . dtype ) <EOL> y = self . blas_func ( x , y , n = <NUM_LIT:3> , incx = <NUM_LIT:2> ) <EOL> assert_array_equal ( x [ : : <NUM_LIT:2> ] , y ) <EOL> def test_y_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> y = self . blas_func ( x , y , n = <NUM_LIT:3> , incy = <NUM_LIT:2> ) <EOL> assert_array_equal ( x , y [ : : <NUM_LIT:2> ] ) <EOL> def test_x_and_y_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> y = self . blas_func ( x , y , n = <NUM_LIT:3> , incx = <NUM_LIT:4> , incy = <NUM_LIT:2> ) <EOL> assert_array_equal ( x [ : : <NUM_LIT:4> ] , y [ : : <NUM_LIT:2> ] ) <EOL> def test_x_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> try : <EOL> self . blas_func ( x , y , n = <NUM_LIT:4> , incx = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> def test_y_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> try : <EOL> self . blas_func ( x , y , n = <NUM_LIT:3> , incy = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> try : <EOL> class TestScopy ( TestCase , BaseCopy ) : <EOL> blas_func = fblas . scopy <EOL> dtype = float32 <EOL> except AttributeError : <EOL> class TestScopy : <EOL> pass <EOL> class TestDcopy ( TestCase , BaseCopy ) : <EOL> blas_func = fblas . dcopy <EOL> dtype = float64 <EOL> try : <EOL> class TestCcopy ( TestCase , BaseCopy ) : <EOL> blas_func = fblas . ccopy <EOL> dtype = complex64 <EOL> except AttributeError : <EOL> class TestCcopy : <EOL> pass <EOL> class TestZcopy ( TestCase , BaseCopy ) : <EOL> blas_func = fblas . zcopy <EOL> dtype = complex128 <EOL> class BaseSwap ( object ) : <EOL> '''<STR_LIT>''' <EOL> def test_simple ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( shape ( x ) , x . dtype ) <EOL> desired_x = y . copy ( ) <EOL> desired_y = x . copy ( ) <EOL> x , y = self . blas_func ( x , y ) <EOL> assert_array_equal ( desired_x , x ) <EOL> assert_array_equal ( desired_y , y ) <EOL> def test_x_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:3> , x . dtype ) <EOL> desired_x = y . copy ( ) <EOL> desired_y = x . copy ( ) [ : : <NUM_LIT:2> ] <EOL> x , y = self . blas_func ( x , y , n = <NUM_LIT:3> , incx = <NUM_LIT:2> ) <EOL> assert_array_equal ( desired_x , x [ : : <NUM_LIT:2> ] ) <EOL> assert_array_equal ( desired_y , y ) <EOL> def test_y_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> desired_x = y . copy ( ) [ : : <NUM_LIT:2> ] <EOL> desired_y = x . copy ( ) <EOL> x , y = self . blas_func ( x , y , n = <NUM_LIT:3> , incy = <NUM_LIT:2> ) <EOL> assert_array_equal ( desired_x , x ) <EOL> assert_array_equal ( desired_y , y [ : : <NUM_LIT:2> ] ) <EOL> def test_x_and_y_stride ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> desired_x = y . copy ( ) [ : : <NUM_LIT:2> ] <EOL> desired_y = x . copy ( ) [ : : <NUM_LIT:4> ] <EOL> x , y = self . blas_func ( x , y , n = <NUM_LIT:3> , incx = <NUM_LIT:4> , incy = <NUM_LIT:2> ) <EOL> assert_array_equal ( desired_x , x [ : : <NUM_LIT:4> ] ) <EOL> assert_array_equal ( desired_y , y [ : : <NUM_LIT:2> ] ) <EOL> def test_x_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> try : <EOL> self . blas_func ( x , y , n = <NUM_LIT:4> , incx = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> def test_y_bad_size ( self ) : <EOL> x = arange ( <NUM_LIT> , dtype = self . dtype ) <EOL> y = zeros ( <NUM_LIT:6> , x . dtype ) <EOL> try : <EOL> self . blas_func ( x , y , n = <NUM_LIT:3> , incy = <NUM_LIT:5> ) <EOL> except : <EOL> return <EOL> assert_ ( <NUM_LIT:0> ) <EOL> try : <EOL> class TestSswap ( TestCase , BaseSwap ) : <EOL> blas_func = fblas . sswap <EOL> dtype = float32 <EOL> except AttributeError : <EOL> class TestSswap : <EOL> pass <EOL> class TestDswap ( TestCase , BaseSwap ) : <EOL> blas_func = fblas . dswap <EOL> dtype = float64 <EOL> try : <EOL> class TestCswap ( TestCase , BaseSwap ) : <EOL> blas_func = fblas . cswap <EOL> dtype = complex64 <EOL> except AttributeError : <EOL> class TestCswap : <EOL> pass <EOL> class TestZswap ( TestCase , BaseSwap ) : <EOL> blas_func = fblas . zswap <EOL> dtype = complex128 <EOL> class BaseGemv ( object ) : <EOL> '''<STR_LIT>''' <EOL> def get_data ( self , x_stride = <NUM_LIT:1> , y_stride = <NUM_LIT:1> ) : <EOL> mult = array ( <NUM_LIT:1> , dtype = self . dtype ) <EOL> if self . dtype in [ complex64 , complex128 ] : <EOL> mult = array ( <NUM_LIT:1> + <NUM_LIT> , dtype = self . dtype ) <EOL> from numpy . random import normal , seed <EOL> seed ( <NUM_LIT> ) <EOL> alpha = array ( <NUM_LIT:1.> , dtype = self . dtype ) * mult <EOL> beta = array ( <NUM_LIT:1.> , dtype = self . dtype ) * mult <EOL> a = normal ( <NUM_LIT:0.> , <NUM_LIT:1.> , ( <NUM_LIT:3> , <NUM_LIT:3> ) ) . astype ( self . dtype ) * mult <EOL> x = arange ( shape ( a ) [ <NUM_LIT:0> ] * x_stride , dtype = self . dtype ) * mult <EOL> y = arange ( shape ( a ) [ <NUM_LIT:1> ] * y_stride , dtype = self . dtype ) * mult <EOL> return alpha , beta , a , x , y <EOL> def test_simple ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( ) <EOL> desired_y = alpha * matrixmultiply ( a , x ) + beta * y <EOL> y = self . blas_func ( alpha , a , x , beta , y ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_default_beta_y ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( ) <EOL> desired_y = matrixmultiply ( a , x ) <EOL> y = self . blas_func ( <NUM_LIT:1> , a , x ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_simple_transpose ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( ) <EOL> desired_y = alpha * matrixmultiply ( transpose ( a ) , x ) + beta * y <EOL> y = self . blas_func ( alpha , a , x , beta , y , trans = <NUM_LIT:1> ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_simple_transpose_conj ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( ) <EOL> desired_y = alpha * matrixmultiply ( transpose ( conjugate ( a ) ) , x ) + beta * y <EOL> y = self . blas_func ( alpha , a , x , beta , y , trans = <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_x_stride ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( x_stride = <NUM_LIT:2> ) <EOL> desired_y = alpha * matrixmultiply ( a , x [ : : <NUM_LIT:2> ] ) + beta * y <EOL> y = self . blas_func ( alpha , a , x , beta , y , incx = <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_x_stride_transpose ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( x_stride = <NUM_LIT:2> ) <EOL> desired_y = alpha * matrixmultiply ( transpose ( a ) , x [ : : <NUM_LIT:2> ] ) + beta * y <EOL> y = self . blas_func ( alpha , a , x , beta , y , trans = <NUM_LIT:1> , incx = <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_x_stride_assert ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( x_stride = <NUM_LIT:2> ) <EOL> try : <EOL> y = self . blas_func ( <NUM_LIT:1> , a , x , <NUM_LIT:1> , y , trans = <NUM_LIT:0> , incx = <NUM_LIT:3> ) <EOL> assert_ ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> try : <EOL> y = self . blas_func ( <NUM_LIT:1> , a , x , <NUM_LIT:1> , y , trans = <NUM_LIT:1> , incx = <NUM_LIT:3> ) <EOL> assert_ ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> def test_y_stride ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( y_stride = <NUM_LIT:2> ) <EOL> desired_y = y . copy ( ) <EOL> desired_y [ : : <NUM_LIT:2> ] = alpha * matrixmultiply ( a , x ) + beta * y [ : : <NUM_LIT:2> ] <EOL> y = self . blas_func ( alpha , a , x , beta , y , incy = <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_y_stride_transpose ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( y_stride = <NUM_LIT:2> ) <EOL> desired_y = y . copy ( ) <EOL> desired_y [ : : <NUM_LIT:2> ] = alpha * matrixmultiply ( transpose ( a ) , x ) + beta * y [ : : <NUM_LIT:2> ] <EOL> y = self . blas_func ( alpha , a , x , beta , y , trans = <NUM_LIT:1> , incy = <NUM_LIT:2> ) <EOL> assert_array_almost_equal ( desired_y , y ) <EOL> def test_y_stride_assert ( self ) : <EOL> alpha , beta , a , x , y = self . get_data ( y_stride = <NUM_LIT:2> ) <EOL> try : <EOL> y = self . blas_func ( <NUM_LIT:1> , a , x , <NUM_LIT:1> , y , trans = <NUM_LIT:0> , incy = <NUM_LIT:3> ) <EOL> assert_ ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> try : <EOL> y = self . blas_func ( <NUM_LIT:1> , a , x , <NUM_LIT:1> , y , trans = <NUM_LIT:1> , incy = <NUM_LIT:3> ) <EOL> assert_ ( <NUM_LIT:0> ) <EOL> except : <EOL> pass <EOL> try : <EOL> class TestSgemv ( TestCase , BaseGemv ) : <EOL> blas_func = fblas . sgemv <EOL> dtype = float32 <EOL> def test_sgemv_on_osx ( self ) : <EOL> from itertools import product <EOL> import sys <EOL> import numpy as np <EOL> if sys . platform != '<STR_LIT>' : <EOL> return <EOL> def aligned_array ( shape , align , dtype , order = '<STR_LIT:C>' ) : <EOL> d = dtype ( ) <EOL> N = np . prod ( shape ) <EOL> tmp = np . zeros ( N * d . nbytes + align , dtype = np . uint8 ) <EOL> address = tmp . __array_interface__ [ "<STR_LIT:data>" ] [ <NUM_LIT:0> ] <EOL> for offset in range ( align ) : <EOL> if ( address + offset ) % align == <NUM_LIT:0> : <EOL> break <EOL> tmp = tmp [ offset : offset + N * d . nbytes ] . view ( dtype = dtype ) <EOL> return tmp . reshape ( shape , order = order ) <EOL> def as_aligned ( arr , align , dtype , order = '<STR_LIT:C>' ) : <EOL> aligned = aligned_array ( arr . shape , align , dtype , order ) <EOL> aligned [ : ] = arr [ : ] <EOL> return aligned <EOL> def assert_dot_close ( A , X , desired ) : <EOL> assert_allclose ( self . blas_func ( <NUM_LIT:1.0> , A , X ) , desired , <EOL> rtol = <NUM_LIT> , atol = <NUM_LIT> ) <EOL> testdata = product ( ( <NUM_LIT:15> , <NUM_LIT:32> ) , ( <NUM_LIT> , ) , ( <NUM_LIT:200> , <NUM_LIT> ) , ( '<STR_LIT:C>' , '<STR_LIT:F>' ) ) <EOL> for align , m , n , a_order in testdata : <EOL> A_d = np . random . rand ( m , n ) <EOL> X_d = np . random . rand ( n ) <EOL> desired = np . dot ( A_d , X_d ) <EOL> A_f = as_aligned ( A_d , align , np . float32 , order = a_order ) <EOL> X_f = as_aligned ( X_d , align , np . float32 , order = a_order ) <EOL> assert_dot_close ( A_f , X_f , desired ) <EOL> except AttributeError : <EOL> class TestSgemv : <EOL> pass <EOL> class TestDgemv ( TestCase , BaseGemv ) : <EOL> blas_func = fblas . dgemv <EOL> dtype = float64 <EOL> try : <EOL> class TestCgemv ( TestCase , BaseGemv ) : <EOL> blas_func = fblas . cgemv <EOL> dtype = complex64 <EOL> except AttributeError : <EOL> class TestCgemv : <EOL> pass <EOL> class TestZgemv ( TestCase , BaseGemv ) : <EOL> blas_func = fblas . zgemv <EOL> dtype = complex128 <EOL> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_module_suite ( ) </s>
