pypy / pypy / module / micronumpy / loop.py

The branch 'missing-ndarray-attributes' does not exist.
  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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
""" This file is the main run loop as well as evaluation loops for various
operations. This is the place to look for all the computations that iterate
over all the array elements.
"""

from pypy.interpreter.error import OperationError
from pypy.rlib.rstring import StringBuilder
from pypy.rlib import jit
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.module.micronumpy.base import W_NDimArray
from pypy.module.micronumpy.iter import PureShapeIterator
from pypy.module.micronumpy import constants
from pypy.module.micronumpy.support import int_w

call2_driver = jit.JitDriver(name='numpy_call2',
                             greens = ['shapelen', 'func', 'calc_dtype',
                                       'res_dtype'],
                             reds = ['shape', 'w_lhs', 'w_rhs', 'out',
                                     'left_iter', 'right_iter', 'out_iter'])

def call2(shape, func, calc_dtype, res_dtype, w_lhs, w_rhs, out):
    if out is None:
        out = W_NDimArray.from_shape(shape, res_dtype)
    left_iter = w_lhs.create_iter(shape)
    right_iter = w_rhs.create_iter(shape)
    out_iter = out.create_iter(shape)
    shapelen = len(shape)
    while not out_iter.done():
        call2_driver.jit_merge_point(shapelen=shapelen, func=func,
                                     calc_dtype=calc_dtype, res_dtype=res_dtype,
                                     shape=shape, w_lhs=w_lhs, w_rhs=w_rhs,
                                     out=out,
                                     left_iter=left_iter, right_iter=right_iter,
                                     out_iter=out_iter)
        w_left = left_iter.getitem().convert_to(calc_dtype)
        w_right = right_iter.getitem().convert_to(calc_dtype)
        out_iter.setitem(func(calc_dtype, w_left, w_right).convert_to(
            res_dtype))
        left_iter.next()
        right_iter.next()
        out_iter.next()
    return out

call1_driver = jit.JitDriver(name='numpy_call1',
                             greens = ['shapelen', 'func', 'calc_dtype',
                                       'res_dtype'],
                             reds = ['shape', 'w_obj', 'out', 'obj_iter',
                                     'out_iter'])

def call1(shape, func, calc_dtype, res_dtype, w_obj, out):
    if out is None:
        out = W_NDimArray.from_shape(shape, res_dtype)
    obj_iter = w_obj.create_iter(shape)
    out_iter = out.create_iter(shape)
    shapelen = len(shape)
    while not out_iter.done():
        call1_driver.jit_merge_point(shapelen=shapelen, func=func,
                                     calc_dtype=calc_dtype, res_dtype=res_dtype,
                                     shape=shape, w_obj=w_obj, out=out,
                                     obj_iter=obj_iter, out_iter=out_iter)
        elem = obj_iter.getitem().convert_to(calc_dtype)
        out_iter.setitem(func(calc_dtype, elem).convert_to(res_dtype))
        out_iter.next()
        obj_iter.next()
    return out

setslice_driver = jit.JitDriver(name='numpy_setslice',
                                greens = ['shapelen', 'dtype'],
                                reds = ['target', 'source', 'target_iter',
                                        'source_iter'])

def setslice(shape, target, source):
    # note that unlike everything else, target and source here are
    # array implementations, not arrays
    target_iter = target.create_iter(shape)
    source_iter = source.create_iter(shape)
    dtype = target.dtype
    shapelen = len(shape)
    while not target_iter.done():
        setslice_driver.jit_merge_point(shapelen=shapelen, dtype=dtype,
                                        target=target, source=source,
                                        target_iter=target_iter,
                                        source_iter=source_iter)
        target_iter.setitem(source_iter.getitem().convert_to(dtype))
        target_iter.next()
        source_iter.next()
    return target

reduce_driver = jit.JitDriver(name='numpy_reduce',
                              greens = ['shapelen', 'func', 'done_func',
                                        'calc_dtype', 'identity'],
                              reds = ['obj', 'obj_iter', 'cur_value'])

def compute_reduce(obj, calc_dtype, func, done_func, identity):
    obj_iter = obj.create_iter()
    if identity is None:
        cur_value = obj_iter.getitem().convert_to(calc_dtype)
        obj_iter.next()
    else:
        cur_value = identity.convert_to(calc_dtype)
    shapelen = len(obj.get_shape())
    while not obj_iter.done():
        reduce_driver.jit_merge_point(shapelen=shapelen, func=func,
                                      calc_dtype=calc_dtype, identity=identity,
                                      done_func=done_func, obj=obj,
                                      obj_iter=obj_iter, cur_value=cur_value)
        rval = obj_iter.getitem().convert_to(calc_dtype)
        if done_func is not None and done_func(calc_dtype, rval):
            return rval
        cur_value = func(calc_dtype, cur_value, rval)
        obj_iter.next()
    return cur_value

reduce_cum_driver = jit.JitDriver(greens = ['shapelen', 'func', 'dtype'],
                                  reds = ['obj_iter', 'out_iter'])

def compute_reduce_cumultative(obj, out, calc_dtype, func, identity):
    obj_iter = obj.create_iter()
    out_iter = out.create_iter()
    cur_value = identity.convert_to(calc_dtype)
    shapelen = len(obj.get_shape())
    while not obj_iter.done():
        reduce_cum_driver.jit_merge_point(shapelen=shapelen, func=func,
                                          dtype=calc_dtype, obj_iter=obj_iter,
                                          out_iter=out_iter)
        rval = obj_iter.getitem().convert_to(calc_dtype)
        cur_value = func(calc_dtype, cur_value, rval)
        out_iter.setitem(cur_value)
        out_iter.next()
        obj_iter.next()

def fill(arr, box):
    arr_iter = arr.create_iter()
    while not arr_iter.done():
        arr_iter.setitem(box)
        arr_iter.next()

where_driver = jit.JitDriver(name='numpy_where',
                             greens = ['shapelen', 'dtype', 'arr_dtype'],
                             reds = ['shape', 'arr', 'x', 'y','arr_iter', 'out',
                                     'x_iter', 'y_iter', 'iter', 'out_iter'])

def where(out, shape, arr, x, y, dtype):
    out_iter = out.create_iter(shape)
    arr_iter = arr.create_iter(shape)
    arr_dtype = arr.get_dtype()
    x_iter = x.create_iter(shape)
    y_iter = y.create_iter(shape)
    if x.is_scalar():
        if y.is_scalar():
            iter = arr_iter
        else:
            iter = y_iter
    else:
        iter = x_iter
    shapelen = len(shape)
    while not iter.done():
        where_driver.jit_merge_point(shapelen=shapelen, shape=shape,
                                     dtype=dtype, iter=iter, x_iter=x_iter,
                                     y_iter=y_iter, arr_iter=arr_iter,
                                     arr=arr, x=x, y=y, arr_dtype=arr_dtype,
                                     out_iter=out_iter, out=out)
        w_cond = arr_iter.getitem()
        if arr_dtype.itemtype.bool(w_cond):
            w_val = x_iter.getitem().convert_to(dtype)
        else:
            w_val = y_iter.getitem().convert_to(dtype)
        out_iter.setitem(w_val)
        out_iter.next()
        arr_iter.next()
        x_iter.next()
        y_iter.next()
    return out

axis_reduce__driver = jit.JitDriver(name='numpy_axis_reduce',
                                    greens=['shapelen', 'cumultative',
                                            'func', 'dtype',
                                            'identity'],
                                    reds=['axis', 'arr', 'out', 'shape',
                                          'out_iter', 'arr_iter',
                                          'temp_iter'])

def do_axis_reduce(shape, func, arr, dtype, axis, out, identity, cumultative,
                   temp):
    out_iter = out.create_axis_iter(arr.get_shape(), axis, cumultative)
    if cumultative:
        temp_iter = temp.create_axis_iter(arr.get_shape(), axis, False)
    else:
        temp_iter = out_iter # hack
    arr_iter = arr.create_iter()
    if identity is not None:
        identity = identity.convert_to(dtype)
    shapelen = len(shape)
    while not out_iter.done():
        axis_reduce__driver.jit_merge_point(shapelen=shapelen, func=func,
                                            dtype=dtype, identity=identity,
                                            axis=axis, arr=arr, out=out,
                                            shape=shape, out_iter=out_iter,
                                            arr_iter=arr_iter,
                                            cumultative=cumultative,
                                            temp_iter=temp_iter)
        w_val = arr_iter.getitem().convert_to(dtype)
        if out_iter.first_line:
            if identity is not None:
                w_val = func(dtype, identity, w_val)
        else:
            cur = temp_iter.getitem()
            w_val = func(dtype, cur, w_val)
        out_iter.setitem(w_val)
        if cumultative:
            temp_iter.setitem(w_val)
            temp_iter.next()
        arr_iter.next()
        out_iter.next()
    return out


def _new_argmin_argmax(op_name):
    arg_driver = jit.JitDriver(name='numpy_' + op_name,
                               greens = ['shapelen', 'dtype'],
                               reds = ['result', 'idx', 'cur_best', 'arr',
                                       'iter'])
    
    def argmin_argmax(arr):
        result = 0
        idx = 1
        dtype = arr.get_dtype()
        iter = arr.create_iter()
        cur_best = iter.getitem()
        iter.next()
        shapelen = len(arr.get_shape())
        while not iter.done():
            arg_driver.jit_merge_point(shapelen=shapelen, dtype=dtype,
                                       result=result, idx=idx,
                                       cur_best=cur_best, arr=arr, iter=iter)
            w_val = iter.getitem()
            new_best = getattr(dtype.itemtype, op_name)(cur_best, w_val)
            if dtype.itemtype.ne(new_best, cur_best):
                result = idx
                cur_best = new_best
            iter.next()
            idx += 1
        return result
    return argmin_argmax
argmin = _new_argmin_argmax('min')
argmax = _new_argmin_argmax('max')

# note that shapelen == 2 always
dot_driver = jit.JitDriver(name = 'numpy_dot',
                           greens = ['dtype'],
                           reds = ['outi', 'lefti', 'righti', 'result'])

def multidim_dot(space, left, right, result, dtype, right_critical_dim):
    ''' assumes left, right are concrete arrays
    given left.shape == [3, 5, 7],
          right.shape == [2, 7, 4]
    then
     result.shape == [3, 5, 2, 4]
     broadcast shape should be [3, 5, 2, 7, 4]
     result should skip dims 3 which is len(result_shape) - 1
        (note that if right is 1d, result should 
                  skip len(result_shape))
     left should skip 2, 4 which is a.ndims-1 + range(right.ndims)
          except where it==(right.ndims-2)
     right should skip 0, 1
    '''
    left_shape = left.get_shape()
    right_shape = right.get_shape()
    broadcast_shape = left_shape[:-1] + right_shape
    left_skip = [len(left_shape) - 1 + i for i in range(len(right_shape))
                                         if i != right_critical_dim]
    right_skip = range(len(left_shape) - 1)
    result_skip = [len(result.get_shape()) - (len(right_shape) > 1)]
    outi = result.create_dot_iter(broadcast_shape, result_skip)
    lefti = left.create_dot_iter(broadcast_shape, left_skip)
    righti = right.create_dot_iter(broadcast_shape, right_skip)
    while not outi.done():
        dot_driver.jit_merge_point(dtype=dtype, outi=outi, lefti=lefti,
                                   righti=righti, result=result)
        lval = lefti.getitem().convert_to(dtype) 
        rval = righti.getitem().convert_to(dtype) 
        outval = outi.getitem().convert_to(dtype) 
        v = dtype.itemtype.mul(lval, rval)
        value = dtype.itemtype.add(v, outval).convert_to(dtype)
        outi.setitem(value)
        outi.next()
        righti.next()
        lefti.next()
    return result

count_all_true_driver = jit.JitDriver(name = 'numpy_count',
                                      greens = ['shapelen', 'dtype'],
                                      reds = ['s', 'iter'])

def count_all_true(arr):
    s = 0
    if arr.is_scalar():
        return arr.get_dtype().itemtype.bool(arr.get_scalar_value())
    iter = arr.create_iter()
    shapelen = len(arr.get_shape())
    dtype = arr.get_dtype()
    while not iter.done():
        count_all_true_driver.jit_merge_point(shapelen=shapelen, iter=iter,
                                              s=s, dtype=dtype)
        s += iter.getitem_bool()
        iter.next()
    return s

getitem_filter_driver = jit.JitDriver(name = 'numpy_getitem_bool',
                                      greens = ['shapelen', 'arr_dtype',
                                                'index_dtype'],
                                      reds = ['res', 'index_iter', 'res_iter',
                                              'arr_iter'])

def getitem_filter(res, arr, index):
    res_iter = res.create_iter()
    index_iter = index.create_iter()
    arr_iter = arr.create_iter()
    shapelen = len(arr.get_shape())
    arr_dtype = arr.get_dtype()
    index_dtype = index.get_dtype()
    # XXX length of shape of index as well?
    while not index_iter.done():
        getitem_filter_driver.jit_merge_point(shapelen=shapelen,
                                              index_dtype=index_dtype,
                                              arr_dtype=arr_dtype,
                                              res=res, index_iter=index_iter,
                                              res_iter=res_iter,
                                              arr_iter=arr_iter)
        if index_iter.getitem_bool():
            res_iter.setitem(arr_iter.getitem())
            res_iter.next()
        index_iter.next()
        arr_iter.next()
    return res

setitem_filter_driver = jit.JitDriver(name = 'numpy_setitem_bool',
                                      greens = ['shapelen', 'arr_dtype',
                                                'index_dtype'],
                                      reds = ['index_iter', 'value_iter',
                                              'arr_iter'])

def setitem_filter(arr, index, value):
    arr_iter = arr.create_iter()
    index_iter = index.create_iter()
    value_iter = value.create_iter()
    shapelen = len(arr.get_shape())
    index_dtype = index.get_dtype()
    arr_dtype = arr.get_dtype()
    while not index_iter.done():
        setitem_filter_driver.jit_merge_point(shapelen=shapelen,
                                              index_dtype=index_dtype,
                                              arr_dtype=arr_dtype,
                                              index_iter=index_iter,
                                              value_iter=value_iter,
                                              arr_iter=arr_iter)
        if index_iter.getitem_bool():
            arr_iter.setitem(value_iter.getitem())
            value_iter.next()
        arr_iter.next()
        index_iter.next()

flatiter_getitem_driver = jit.JitDriver(name = 'numpy_flatiter_getitem',
                                        greens = ['dtype'],
                                        reds = ['step', 'ri', 'res',
                                                'base_iter'])

def flatiter_getitem(res, base_iter, step):
    ri = res.create_iter()
    dtype = res.get_dtype()
    while not ri.done():
        flatiter_getitem_driver.jit_merge_point(dtype=dtype,
                                                base_iter=base_iter,
                                                ri=ri, res=res, step=step)
        ri.setitem(base_iter.getitem())
        base_iter.next_skip_x(step)
        ri.next()
    return res

flatiter_setitem_driver = jit.JitDriver(name = 'numpy_flatiter_setitem',
                                        greens = ['dtype'],
                                        reds = ['length', 'step', 'arr_iter',
                                                'val_iter'])

def flatiter_setitem(arr, val, start, step, length):
    dtype = arr.get_dtype()
    arr_iter = arr.create_iter()
    val_iter = val.create_iter()
    arr_iter.next_skip_x(start)
    while length > 0:
        flatiter_setitem_driver.jit_merge_point(dtype=dtype, length=length,
                                                step=step, arr_iter=arr_iter,
                                                val_iter=val_iter)
        arr_iter.setitem(val_iter.getitem().convert_to(dtype))
        # need to repeat i_nput values until all assignments are done
        arr_iter.next_skip_x(step)
        length -= 1
        val_iter.next()
        # WTF numpy?
        val_iter.reset()

fromstring_driver = jit.JitDriver(name = 'numpy_fromstring',
                                  greens = ['itemsize', 'dtype'],
                                  reds = ['i', 's', 'ai'])

def fromstring_loop(a, dtype, itemsize, s):
    i = 0
    ai = a.create_iter()
    while not ai.done():
        fromstring_driver.jit_merge_point(dtype=dtype, s=s, ai=ai, i=i,
                                          itemsize=itemsize)
        val = dtype.itemtype.runpack_str(s[i*itemsize:i*itemsize + itemsize])
        ai.setitem(val)
        ai.next()
        i += 1

def tostring(space, arr):
    builder = StringBuilder()
    iter = arr.create_iter()
    res_str = W_NDimArray.from_shape([1], arr.get_dtype(), order='C')
    itemsize = arr.get_dtype().itemtype.get_element_size()
    res_str_casted = rffi.cast(rffi.CArrayPtr(lltype.Char),
                               res_str.implementation.get_storage_as_int(space))
    while not iter.done():
        res_str.implementation.setitem(0, iter.getitem())
        for i in range(itemsize):
            builder.append(res_str_casted[i])
        iter.next()
    return builder.build()

getitem_int_driver = jit.JitDriver(name = 'numpy_getitem_int',
                                   greens = ['shapelen', 'indexlen',
                                             'prefixlen', 'dtype'],
                                   reds = ['arr', 'res', 'iter', 'indexes_w',
                                           'prefix_w'])

def getitem_array_int(space, arr, res, iter_shape, indexes_w, prefix_w):
    shapelen = len(iter_shape)
    prefixlen = len(prefix_w)
    indexlen = len(indexes_w)
    dtype = arr.get_dtype()
    iter = PureShapeIterator(iter_shape, indexes_w)
    indexlen = len(indexes_w)
    while not iter.done():
        getitem_int_driver.jit_merge_point(shapelen=shapelen, indexlen=indexlen,
                                           dtype=dtype, arr=arr, res=res,
                                           iter=iter, indexes_w=indexes_w,
                                           prefix_w=prefix_w,
                                           prefixlen=prefixlen)
        # prepare the index
        index_w = [None] * indexlen
        for i in range(indexlen):
            if iter.idx_w[i] is not None:
                index_w[i] = iter.idx_w[i].getitem()
            else:
                index_w[i] = indexes_w[i]
        res.descr_setitem(space, space.newtuple(prefix_w[:prefixlen] +
                                            iter.get_index(space, shapelen)),
                          arr.descr_getitem(space, space.newtuple(index_w)))
        iter.next()
    return res

setitem_int_driver = jit.JitDriver(name = 'numpy_setitem_int',
                                   greens = ['shapelen', 'indexlen',
                                             'prefixlen', 'dtype'],
                                   reds = ['arr', 'iter', 'indexes_w',
                                           'prefix_w', 'val_arr'])

def setitem_array_int(space, arr, iter_shape, indexes_w, val_arr,
                      prefix_w):
    shapelen = len(iter_shape)
    indexlen = len(indexes_w)
    prefixlen = len(prefix_w)
    dtype = arr.get_dtype()
    iter = PureShapeIterator(iter_shape, indexes_w)
    while not iter.done():
        setitem_int_driver.jit_merge_point(shapelen=shapelen, indexlen=indexlen,
                                           dtype=dtype, arr=arr,
                                           iter=iter, indexes_w=indexes_w,
                                           prefix_w=prefix_w, val_arr=val_arr,
                                           prefixlen=prefixlen)
        # prepare the index
        index_w = [None] * indexlen
        for i in range(indexlen):
            if iter.idx_w[i] is not None:
                index_w[i] = iter.idx_w[i].getitem()
            else:
                index_w[i] = indexes_w[i]
        w_idx = space.newtuple(prefix_w[:prefixlen] + iter.get_index(space,
                                                                  shapelen))
        arr.descr_setitem(space, space.newtuple(index_w),
                          val_arr.descr_getitem(space, w_idx))
        iter.next()

copy_from_to_driver = jit.JitDriver(greens = ['dtype'],
                                    reds = ['from_iter', 'to_iter'])

def copy_from_to(from_, to, dtype):
    from_iter = from_.create_iter()
    to_iter = to.create_iter()
    while not from_iter.done():
        copy_from_to_driver.jit_merge_point(dtype=dtype, from_iter=from_iter,
                                            to_iter=to_iter)
        to_iter.setitem(from_iter.getitem().convert_to(dtype))
        to_iter.next()
        from_iter.next()

byteswap_driver = jit.JitDriver(greens = ['dtype'],
                                reds = ['from_iter', 'to_iter'])

def byteswap(from_, to):
    dtype = from_.dtype
    from_iter = from_.create_iter()
    to_iter = to.create_iter()
    while not from_iter.done():
        byteswap_driver.jit_merge_point(dtype=dtype, from_iter=from_iter,
                                        to_iter=to_iter)
        to_iter.setitem(dtype.itemtype.byteswap(from_iter.getitem()))
        to_iter.next()
        from_iter.next()

choose_driver = jit.JitDriver(greens = ['shapelen', 'mode', 'dtype'],
                              reds = ['shape', 'iterators', 'arr_iter',
                                      'out_iter'])

def choose(space, arr, choices, shape, dtype, out, mode):
    shapelen = len(shape)
    iterators = [a.create_iter(shape) for a in choices]
    arr_iter = arr.create_iter(shape)
    out_iter = out.create_iter(shape)
    while not arr_iter.done():
        choose_driver.jit_merge_point(shapelen=shapelen, dtype=dtype,
                                      mode=mode, shape=shape,
                                      iterators=iterators, arr_iter=arr_iter,
                                      out_iter=out_iter)
        index = int_w(space, arr_iter.getitem())
        if index < 0 or index >= len(iterators):
            if mode == constants.MODE_RAISE:
                raise OperationError(space.w_ValueError, space.wrap(
                    "invalid entry in choice array"))
            elif mode == constants.MODE_WRAP:
                index = index % (len(iterators))
            else:
                assert mode == constants.MODE_CLIP
                if index < 0:
                    index = 0
                else:
                    index = len(iterators) - 1
        out_iter.setitem(iterators[index].getitem().convert_to(dtype))
        for iter in iterators:
            iter.next()
        out_iter.next()
        arr_iter.next()

clip_driver = jit.JitDriver(greens = ['shapelen', 'dtype'],
                            reds = ['min_iter', 'max_iter', 'arr_iter',
                                    'out_iter'])

def clip(space, arr, shape, min, max, out):
    arr_iter = arr.create_iter(shape)
    dtype = out.get_dtype()
    shapelen = len(shape)
    min_iter = min.create_iter(shape)
    max_iter = max.create_iter(shape)
    out_iter = out.create_iter(shape)
    while not arr_iter.done():
        clip_driver.jit_merge_point(shapelen=shapelen, dtype=dtype,
                                    min_iter=min_iter, max_iter=max_iter,
                                    arr_iter=arr_iter, out_iter=out_iter)
        w_v = arr_iter.getitem().convert_to(dtype)
        w_min = min_iter.getitem().convert_to(dtype)
        w_max = max_iter.getitem().convert_to(dtype)
        if dtype.itemtype.lt(w_v, w_min):
            w_v = w_min
        elif dtype.itemtype.gt(w_v, w_max):
            w_v = w_max
        out_iter.setitem(w_v)
        arr_iter.next()
        max_iter.next()
        out_iter.next()
        min_iter.next()

diagonal_simple_driver = jit.JitDriver(greens = ['axis1', 'axis2'],
                                       reds = ['i', 'offset', 'out_iter',
                                               'arr'])

def diagonal_simple(space, arr, out, offset, axis1, axis2, size):
    out_iter = out.create_iter()
    i = 0
    index = [0] * 2
    while i < size:
        diagonal_simple_driver.jit_merge_point(axis1=axis1, axis2=axis2,
                                               out_iter=out_iter, offset=offset,
                                               i=i, arr=arr)
        index[axis1] = i
        index[axis2] = i + offset
        out_iter.setitem(arr.getitem_index(space, index))
        i += 1
        out_iter.next()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.