File: test_util_binary_new.py

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (245 lines) | stat: -rw-r--r-- 11,927 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

##############################################################################
#
# Copyright (c) 2003-2018 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################

from __future__ import print_function, division

__copyright__="""Copyright (c) 2003-2018 by The University of Queensland
http://www.uq.edu.au
Primary Business: Queensland, Australia"""
__license__="""Licensed under the Apache License, version 2.0
http://www.apache.org/licenses/LICENSE-2.0"""
__url__="https://launchpad.net/escript-finley"

"""
test for non-overloaded binary operations

:remark: use see `test_util`
:var __author__: name of author
:var __copyright__: copyrights
:var __license__: licence agreement
:var __url__: url entry point on documentation
:var __version__: version
:var __date__: date of the version
"""

__author__="Joel Fenwick, joelfenwick@uq.edu.au"

import esys.escriptcore.utestselect as unittest
import numpy
from esys.escript import *
from test_util_base import Test_util_values




        
    

class Test_util_binary_new(Test_util_values):
    
   def generate_indices(self, shape):
        res=[0]*len(shape)
        l=len(shape)
        done=False
        while not done:
            yield tuple(res)
            res[0]+=1
            for i in range(l-1):
                if res[i]>=shape[i]:
                    res[i]=0
                    res[i+1]+=1
                else:
                    break
            # now we check the last digit
            if res[l-1]>=shape[l-1]:
                done=True
        

   def subst_outer(self, a, b):
        if isinstance(a,float) or isinstance(a, complex):
            a=(a,)
        if isinstance(b,float) or isinstance(b, complex):
            b=(b,)            
        sa=getShape(a)
        sb=getShape(b)
        a=numpy.array(a)
        b=numpy.array(b)
        targettype=a.dtype if a.dtype.kind=='c' else b.dtype
        if sa==():
            if sb==():
                return a*b
            resshape=sb
            res=numpy.zeros(resshape, dtype=targettype)            
            for xb in self.generate_indices(sb):
                res.itemset(xb,a*b.item(xb))  
            return res
        elif sb==():
            resshape=sa
            res=numpy.zeros(resshape, dtype=targettype)            
            for xa in self.generate_indices(sa):
                res.itemset(xa,a.item(xa)*b)            
            return res
        else:
            resshape=sa+sb
            res=numpy.zeros(resshape, dtype=targettype)            
        for xa in self.generate_indices(sa):
            for xb in self.generate_indices(sb):
                res.itemset(xa+xb,a.item(xa)*b.item(xb))
        return res    
    
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_add_combined(self):
       opstring='a+b'
       misccheck='isinstance(res, Data) if isinstance(a, Data) or isinstance(b, Data) else True' # doesn't cover all cases;
       oraclecheck="refa+refb"
       opname="+ operator"
       noshapemismatch=True
       permitscalarmismatch=True
       permit_array_op_data=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, permit_array_op_data=permit_array_op_data)   
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_div_combined(self):
       opstring='a/b'
       misccheck='isinstance(res, Data) if isinstance(a, Data) or isinstance(b, Data) else True' # doesn't cover all cases;
       oraclecheck="refa/refb"
       opname="/ operator"
       no_second_arg_zeros=True
       noshapemismatch=True
       permitscalarmismatch=True
       permit_array_op_data=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, permit_array_op_data=permit_array_op_data, no_second_arg_zeros=no_second_arg_zeros)     
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_mul_combined(self):
       opstring='a*b'
       misccheck='isinstance(res, Data) if isinstance(a, Data) or isinstance(b, Data) else True' # doesn't cover all cases;
       oraclecheck="refa*refb"
       opname="* operator"
       noshapemismatch=True
       permitscalarmismatch=True
       permit_array_op_data=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, permit_array_op_data=permit_array_op_data)          
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_pow_combined(self):
       opstring='a**b'
       misccheck='isinstance(res, Data) if isinstance(a, Data) or isinstance(b, Data) else True' # doesn't cover all cases;
       oraclecheck="refa**refb"
       opname="** operator"
       noshapemismatch=True
       permitscalarmismatch=True
       permit_array_op_data=False
       no_first_arg_negative=True
       no_first_arg_zeros=False
       second_large_args=False
       first_large_args=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, permit_array_op_data=permit_array_op_data, no_first_arg_negative=no_first_arg_negative, no_first_arg_zeros=no_first_arg_zeros, 
                                                 second_large_args=second_large_args, first_large_args=first_large_args)          
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_sub_combined(self):
       opstring='a-b'
       misccheck='isinstance(res, Data) if isinstance(a, Data) or isinstance(b, Data) else True' # doesn't cover all cases;
       oraclecheck="refa-refb"
       opname="- operator"
       noshapemismatch=True
       permitscalarmismatch=True
       permit_array_op_data=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, permit_array_op_data=permit_array_op_data)          
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_inner_combined(self):
       opstring='inner(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.tensordot(refa, refb, axes=getRank(refa))"
       opname="inner"
       noshapemismatch=True
       permitscalarmismatch=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch)
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_outer_combined(self):
       opstring='outer(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="self.subst_outer(refa,refb)"
       opname="outer"
       noshapemismatch=True
       permitscalarmismatch=True
       capcombinedrank=True
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, cap_combined_rank=capcombinedrank)           
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_matrix_minimum_combined(self):
       opstring='minimum(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.minimum(refa,refb)"
       opname="minimum"
       noshapemismatch=True
       permitscalarmismatch=True
       cplx=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, support_cplx=cplx)         
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_matrix_maximum_combined(self):
       opstring='maximum(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.maximum(refa,refb)"
       opname="maximum"
       noshapemismatch=True
       permitscalarmismatch=True
       cplx=False
       self.generate_binary_operation_test_batch(opstring, misccheck, oraclecheck, opname, no_shape_mismatch=noshapemismatch, permit_scalar_mismatch=permitscalarmismatch, support_cplx=cplx)         
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_matrix_mult_combined(self):
       opstring='matrix_mult(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.dot(refa,refb)"
       opname="matrix_mult"
       aranks=(2,)
       self.generate_binary_matrixlike_operation_test_batch_large(opstring, misccheck, oraclecheck, opname, aranks=aranks)
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_transpose_matrix_mult_combined(self):
       opstring='transposed_matrix_mult(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.dot(numpy.transpose(refa),refb)"
       opname="transposed_matrix_mult"
       aranks=(2,)
       self.generate_binary_matrixlike_operation_test_batch_large(opstring, misccheck, oraclecheck, opname, aranks=aranks)
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_matrix_transposed_mult_combined(self):
       opstring='matrix_transposed_mult(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.dot(refa,numpy.transpose(refb))"
       opname="matrix_transposed_mult"
       aranks=(2,)
       self.generate_binary_matrixlike_operation_test_batch_large(opstring, misccheck, oraclecheck, opname, aranks=aranks)       
   #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_tensor_mult_combined(self):
       opstring='tensor_mult(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.dot(refa,refb) if getRank(refa)==2 else numpy.tensordot(refa,refb)"
       opname="tensor_mult"
       aranks=(2,4)
       self.generate_binary_matrixlike_operation_test_batch_large(opstring, misccheck, oraclecheck, opname, aranks=aranks)
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_transposed_tensor_mult_combined(self):
       opstring='transposed_tensor_mult(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.dot(transpose(refa),refb) if getRank(refa)==2 else numpy.tensordot(transpose(refa),refb)"
       opname="transposed_tensor_mult"
       aranks=(2,4)
       self.generate_binary_matrixlike_operation_test_batch_large(opstring, misccheck, oraclecheck, opname, aranks=aranks) 
    #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   def test_tensor_transposed_mult_combined(self):
       opstring='tensor_transposed_mult(a,b)'
       misccheck=None   # How to work out what the result of type should be
       oraclecheck="numpy.dot(refa,transpose(refb)) if getRank(refa)==2 else numpy.tensordot(refa,transpose(refb))"
       opname="tensor_tranposed_mult"
       aranks=(2,4)
       self.generate_binary_matrixlike_operation_test_batch_large(opstring, misccheck, oraclecheck, opname, aranks=aranks)