<s> from __future__ import absolute_import , print_function , division <EOL> import unittest <EOL> from six . moves import xrange <EOL> import theano <EOL> from theano . tensor import * <EOL> from theano . tests import unittest_tools as utt <EOL> from numpy . testing import dec <EOL> class TestRealImag ( unittest . TestCase ) : <EOL> def test0 ( self ) : <EOL> x = zvector ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> xval = numpy . asarray ( list ( numpy . complex ( rng . randn ( ) , rng . randn ( ) ) <EOL> for i in xrange ( <NUM_LIT:10> ) ) ) <EOL> assert numpy . all ( xval . real == theano . function ( [ x ] , real ( x ) ) ( xval ) ) <EOL> assert numpy . all ( xval . imag == theano . function ( [ x ] , imag ( x ) ) ( xval ) ) <EOL> def test_on_real_input ( self ) : <EOL> x = dvector ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> xval = rng . randn ( <NUM_LIT:10> ) <EOL> numpy . all ( <NUM_LIT:0> == theano . function ( [ x ] , imag ( x ) ) ( xval ) ) <EOL> numpy . all ( xval == theano . function ( [ x ] , real ( x ) ) ( xval ) ) <EOL> x = imatrix ( ) <EOL> xval = numpy . asarray ( rng . randn ( <NUM_LIT:3> , <NUM_LIT:3> ) * <NUM_LIT:100> , dtype = '<STR_LIT>' ) <EOL> numpy . all ( <NUM_LIT:0> == theano . function ( [ x ] , imag ( x ) ) ( xval ) ) <EOL> numpy . all ( xval == theano . function ( [ x ] , real ( x ) ) ( xval ) ) <EOL> def test_cast ( self ) : <EOL> x = zvector ( ) <EOL> self . assertRaises ( TypeError , cast , x , '<STR_LIT>' ) <EOL> def test_complex ( self ) : <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> m = fmatrix ( ) <EOL> c = complex ( m [ <NUM_LIT:0> ] , m [ <NUM_LIT:1> ] ) <EOL> assert c . type == cvector <EOL> r , i = [ real ( c ) , imag ( c ) ] <EOL> assert r . type == fvector <EOL> assert i . type == fvector <EOL> f = theano . function ( [ m ] , [ r , i ] ) <EOL> mval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) , dtype = '<STR_LIT>' ) <EOL> rval , ival = f ( mval ) <EOL> assert numpy . all ( rval == mval [ <NUM_LIT:0> ] ) , ( rval , mval [ <NUM_LIT:0> ] ) <EOL> assert numpy . all ( ival == mval [ <NUM_LIT:1> ] ) , ( ival , mval [ <NUM_LIT:1> ] ) <EOL> @ dec . skipif ( True , "<STR_LIT>" ) <EOL> def test_complex_grads ( self ) : <EOL> def f ( m ) : <EOL> c = complex ( m [ <NUM_LIT:0> ] , m [ <NUM_LIT:1> ] ) <EOL> return <NUM_LIT> * real ( c ) + <NUM_LIT> * imag ( c ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> mval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) ) <EOL> utt . verify_grad ( f , [ mval ] ) <EOL> @ dec . skipif ( True , "<STR_LIT>" ) <EOL> def test_mul_mixed0 ( self ) : <EOL> def f ( a ) : <EOL> ac = complex ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] ) <EOL> return abs ( ( ac ) ** <NUM_LIT:2> ) . sum ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> aval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) ) <EOL> try : <EOL> utt . verify_grad ( f , [ aval ] ) <EOL> except utt . verify_grad . E_grad as e : <EOL> print ( e . num_grad . gf ) <EOL> print ( e . analytic_grad ) <EOL> raise <EOL> @ dec . skipif ( True , "<STR_LIT>" ) <EOL> def test_mul_mixed1 ( self ) : <EOL> def f ( a ) : <EOL> ac = complex ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] ) <EOL> return abs ( ac ) . sum ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> aval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) ) <EOL> try : <EOL> utt . verify_grad ( f , [ aval ] ) <EOL> except utt . verify_grad . E_grad as e : <EOL> print ( e . num_grad . gf ) <EOL> print ( e . analytic_grad ) <EOL> raise <EOL> @ dec . skipif ( True , "<STR_LIT>" ) <EOL> def test_mul_mixed ( self ) : <EOL> def f ( a , b ) : <EOL> ac = complex ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] ) <EOL> return abs ( ( ac * b ) ** <NUM_LIT:2> ) . sum ( ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> aval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) ) <EOL> bval = rng . randn ( <NUM_LIT:5> ) <EOL> try : <EOL> utt . verify_grad ( f , [ aval , bval ] ) <EOL> except utt . verify_grad . E_grad as e : <EOL> print ( e . num_grad . gf ) <EOL> print ( e . analytic_grad ) <EOL> raise <EOL> @ dec . skipif ( True , "<STR_LIT>" ) <EOL> def test_polar_grads ( self ) : <EOL> def f ( m ) : <EOL> c = complex_from_polar ( abs ( m [ <NUM_LIT:0> ] ) , m [ <NUM_LIT:1> ] ) <EOL> return <NUM_LIT> * real ( c ) + <NUM_LIT> * imag ( c ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> mval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) ) <EOL> utt . verify_grad ( f , [ mval ] ) <EOL> @ dec . skipif ( True , "<STR_LIT>" ) <EOL> def test_abs_grad ( self ) : <EOL> def f ( m ) : <EOL> c = complex ( m [ <NUM_LIT:0> ] , m [ <NUM_LIT:1> ] ) <EOL> return <NUM_LIT> * abs ( c ) <EOL> rng = numpy . random . RandomState ( <NUM_LIT> ) <EOL> mval = numpy . asarray ( rng . randn ( <NUM_LIT:2> , <NUM_LIT:5> ) ) <EOL> utt . verify_grad ( f , [ mval ] ) </s>
