9
13
None_ = "PyUFunc_None"
15
# Sentinel value to specify that the loop for the given TypeDescription uses the
16
# pointer to arrays as its func_data.
17
UsesArraysAsData = object()
11
20
class TypeDescription(object):
12
"""Type signature for a ufunc
21
"""Type signature for a ufunc.
17
type: character representing the type
26
Character representing the nominal type.
27
func_data : str or None or UsesArraysAsData, optional
28
The string representing the expression to insert into the data array, if
30
in_ : str or None, optional
31
The typecode(s) of the inputs.
32
out : str or None, optional
33
The typecode(s) of the outputs.
22
35
def __init__(self, type, f=None, in_=None, out=None):
25
38
if in_ is not None:
26
in_ = in_.replace('.', type)
39
in_ = in_.replace('P', type)
28
41
if out is not None:
29
out = out.replace('.', type)
42
out = out.replace('P', type)
32
45
def finish_signature(self, nin, nout):
94
107
for td in self.type_descriptions:
95
108
td.finish_signature(self.nin, self.nout)
110
# String-handling utilities to avoid locale-dependence.
113
UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
115
def english_upper(s):
116
""" Apply English case rules to convert ASCII strings to all upper case.
118
This is an internal utility function to replace calls to str.upper() such
119
that we can avoid changing behavior with changing locales. In particular,
120
Turkish has distinct dotted and dotless variants of the Latin letter "I" in
121
both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
133
>>> from numpy.lib.utils import english_upper
134
>>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
135
'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
136
>>> english_upper('')
139
uppered = s.translate(UPPER_TABLE)
97
143
#each entry in defdict is a Ufunc object.
99
145
#name: [string of chars for which it is defined,
124
170
'G': 'clongdouble',
126
# M is like O, but calls a method of the object instead
172
# '.' is like 'O', but calls a method of the object instead
131
177
all = '?bBhHiIlLqQfdgFDGO'
134
180
ints = 'bBhHiIlLqQ'
136
182
bints = '?' + ints
137
183
bintsO = bints + O
142
188
cmplxO = cmplx + O
144
190
inexact = flts + cmplx
145
191
noint = inexact+O
147
allM = bints+flts+cmplxM
193
allP = bints+flts+cmplxP
149
nobool_or_obj = all[1:-1]
196
nobool_or_obj = all[1:-2]
150
197
intflt = ints+flts
151
intfltcmplx = nobool_or_obj
198
intfltcmplx = ints+flts+cmplx
152
199
nocmplx = bints+flts
153
200
nocmplxO = nocmplx+O
202
notimes_or_obj = bints + inexact
204
# Find which code corresponds to int64.
208
if struct.calcsize(code) == 8:
210
uint64 = english_upper(code)
159
215
Ufunc(2, 1, Zero,
160
216
docstrings.get('numpy.core.umath.add'),
218
#[TypeDescription('M', UsesArraysAsData, 'Mm', 'M'),
219
# TypeDescription('m', UsesArraysAsData, 'mm', 'm'),
220
# TypeDescription('M', UsesArraysAsData, 'mM', 'M'),
162
222
TD(O, f='PyNumber_Add'),
165
225
Ufunc(2, 1, Zero,
166
226
docstrings.get('numpy.core.umath.subtract'),
228
#[TypeDescription('M', UsesArraysAsData, 'Mm', 'M'),
229
# TypeDescription('m', UsesArraysAsData, 'mm', 'm'),
230
# TypeDescription('M', UsesArraysAsData, 'MM', 'm'),
168
232
TD(O, f='PyNumber_Subtract'),
172
236
docstrings.get('numpy.core.umath.multiply'),
174
238
TD(O, f='PyNumber_Multiply'),
197
261
Ufunc(1, 1, None,
198
262
docstrings.get('numpy.core.umath.conjugate'),
200
TD(M, f='conjugate'),
264
TD(P, f='conjugate'),
203
267
Ufunc(2, 1, Zero,
204
268
docstrings.get('numpy.core.umath.fmod'),
206
270
TD(flts, f='fmod'),
210
274
Ufunc(1, 1, None,
211
275
docstrings.get('numpy.core.umath.square'),
213
277
TD(O, f='Py_square'),
216
280
Ufunc(1, 1, None,
217
281
docstrings.get('numpy.core.umath.reciprocal'),
219
283
TD(O, f='Py_reciprocal'),
238
302
TD(cmplx, out=('f', 'd', 'g')),
239
303
TD(O, f='PyNumber_Absolute'),
307
docstrings.get('numpy.core.umath._arg'),
308
TD(cmplx, out=('f', 'd', 'g')),
242
311
Ufunc(1, 1, None,
243
312
docstrings.get('numpy.core.umath.negative'),
285
354
docstrings.get('numpy.core.umath.logical_and'),
286
355
TD(noobj, out='?'),
287
TD(M, f='logical_and'),
356
TD(P, f='logical_and'),
290
359
Ufunc(1, 1, None,
291
360
docstrings.get('numpy.core.umath.logical_not'),
292
361
TD(noobj, out='?'),
293
TD(M, f='logical_not'),
362
TD(P, f='logical_not'),
296
365
Ufunc(2, 1, Zero,
297
366
docstrings.get('numpy.core.umath.logical_or'),
298
367
TD(noobj, out='?'),
299
TD(M, f='logical_or'),
368
TD(P, f='logical_or'),
302
371
Ufunc(2, 1, None,
303
372
docstrings.get('numpy.core.umath.logical_xor'),
304
373
TD(noobj, out='?'),
305
TD(M, f='logical_xor'),
374
TD(P, f='logical_xor'),
308
377
Ufunc(2, 1, None,
378
448
Ufunc(1, 1, None,
379
449
docstrings.get('numpy.core.umath.degrees'),
380
TD(fltsM, f='degrees'),
450
TD(fltsP, f='degrees'),
383
453
Ufunc(1, 1, None,
384
454
docstrings.get('numpy.core.umath.rad2deg'),
385
TD(fltsM, f='rad2deg'),
455
TD(fltsP, f='rad2deg'),
388
458
Ufunc(1, 1, None,
389
459
docstrings.get('numpy.core.umath.radians'),
390
TD(fltsM, f='radians'),
460
TD(fltsP, f='radians'),
393
463
Ufunc(1, 1, None,
394
464
docstrings.get('numpy.core.umath.deg2rad'),
395
TD(fltsM, f='deg2rad'),
465
TD(fltsP, f='deg2rad'),
398
468
Ufunc(1, 1, None,
399
469
docstrings.get('numpy.core.umath.arccos'),
400
470
TD(inexact, f='acos'),
404
474
Ufunc(1, 1, None,
405
475
docstrings.get('numpy.core.umath.arccosh'),
406
476
TD(inexact, f='acosh'),
410
480
Ufunc(1, 1, None,
411
481
docstrings.get('numpy.core.umath.arcsin'),
412
482
TD(inexact, f='asin'),
416
486
Ufunc(1, 1, None,
417
487
docstrings.get('numpy.core.umath.arcsinh'),
418
488
TD(inexact, f='asinh'),
422
492
Ufunc(1, 1, None,
423
493
docstrings.get('numpy.core.umath.arctan'),
424
494
TD(inexact, f='atan'),
428
498
Ufunc(1, 1, None,
429
499
docstrings.get('numpy.core.umath.arctanh'),
430
500
TD(inexact, f='atanh'),
434
504
Ufunc(1, 1, None,
435
505
docstrings.get('numpy.core.umath.cos'),
436
506
TD(inexact, f='cos'),
440
510
Ufunc(1, 1, None,
441
511
docstrings.get('numpy.core.umath.sin'),
442
512
TD(inexact, f='sin'),
446
516
Ufunc(1, 1, None,
447
517
docstrings.get('numpy.core.umath.tan'),
448
518
TD(inexact, f='tan'),
452
522
Ufunc(1, 1, None,
453
523
docstrings.get('numpy.core.umath.cosh'),
454
524
TD(inexact, f='cosh'),
458
528
Ufunc(1, 1, None,
459
529
docstrings.get('numpy.core.umath.sinh'),
460
530
TD(inexact, f='sinh'),
464
534
Ufunc(1, 1, None,
465
535
docstrings.get('numpy.core.umath.tanh'),
466
536
TD(inexact, f='tanh'),
470
540
Ufunc(1, 1, None,
471
541
docstrings.get('numpy.core.umath.exp'),
472
542
TD(inexact, f='exp'),
476
546
Ufunc(1, 1, None,
477
547
docstrings.get('numpy.core.umath.exp2'),
478
548
TD(inexact, f='exp2'),
482
552
Ufunc(1, 1, None,
483
553
docstrings.get('numpy.core.umath.expm1'),
484
554
TD(inexact, f='expm1'),
488
558
Ufunc(1, 1, None,
489
559
docstrings.get('numpy.core.umath.log'),
490
560
TD(inexact, f='log'),
494
564
Ufunc(1, 1, None,
495
565
docstrings.get('numpy.core.umath.log2'),
496
566
TD(inexact, f='log2'),
500
570
Ufunc(1, 1, None,
501
571
docstrings.get('numpy.core.umath.log10'),
502
572
TD(inexact, f='log10'),
506
576
Ufunc(1, 1, None,
507
577
docstrings.get('numpy.core.umath.log1p'),
508
578
TD(inexact, f='log1p'),
512
582
Ufunc(1, 1, None,
513
583
docstrings.get('numpy.core.umath.sqrt'),
514
584
TD(inexact, f='sqrt'),
518
588
Ufunc(1, 1, None,
519
589
docstrings.get('numpy.core.umath.ceil'),
520
590
TD(flts, f='ceil'),
524
594
Ufunc(1, 1, None,
525
595
docstrings.get('numpy.core.umath.trunc'),
526
596
TD(flts, f='trunc'),
530
600
Ufunc(1, 1, None,
531
601
docstrings.get('numpy.core.umath.fabs'),
532
602
TD(flts, f='fabs'),
536
606
Ufunc(1, 1, None,
537
607
docstrings.get('numpy.core.umath.floor'),
538
608
TD(flts, f='floor'),
542
612
Ufunc(1, 1, None,
543
613
docstrings.get('numpy.core.umath.rint'),
544
614
TD(inexact, f='rint'),
548
618
Ufunc(2, 1, None,
549
619
docstrings.get('numpy.core.umath.arctan2'),
550
620
TD(flts, f='atan2'),
554
624
Ufunc(2, 1, None,
582
652
docstrings.get('numpy.core.umath.signbit'),
583
653
TD(flts, out='?'),
657
docstrings.get('numpy.core.umath.copysign'),
662
docstrings.get('numpy.core.umath.nextafter'),
667
docstrings.get('numpy.core.umath.spacing'),
586
671
Ufunc(1, 2, None,
587
672
docstrings.get('numpy.core.umath.modf'),
617
702
# 1) create functions, data, and signature
618
703
# 2) fill in functions and data in InitOperators
619
704
# 3) add function.
621
# String-handling utilities to avoid locale-dependence.
624
UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase)
626
def english_upper(s):
627
""" Apply English case rules to convert ASCII strings to all upper case.
629
This is an internal utility function to replace calls to str.upper() such
630
that we can avoid changing behavior with changing locales. In particular,
631
Turkish has distinct dotted and dotless variants of the Latin letter "I" in
632
both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.
644
>>> from numpy.lib.utils import english_upper
645
>>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')
646
'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
647
>>> english_upper('')
650
uppered = s.translate(UPPER_TABLE)
654
706
def make_arrays(funcdict):
655
707
# functions array contains an entry for every type implemented
656
708
# NULL should be placed where PyUfunc_ style function will be filled in later
674
726
thedict = chartotype1 # one input and one output
676
728
for t in uf.type_descriptions:
677
if t.func_data is not None:
729
if t.func_data not in (None, UsesArraysAsData):
678
730
funclist.append('NULL')
679
731
astr = '%s_functions[%d] = PyUFunc_%s;' % \
680
732
(name, k, thedict[t.type])
693
745
datalist.append('(void *)NULL')
694
746
#datalist.append('(void *)%s' % t.func_data)
748
elif t.func_data is UsesArraysAsData:
749
tname = english_upper(chartoname[t.type])
750
datalist.append('(void *)NULL')
751
funclist.append('%s_%s_%s_%s' % (tname, t.in_, t.out, name))
752
code2list.append('PyUFunc_SetUsesArraysAsData(%s_data, %s);' % (name, k))
697
datalist.append('(void *)NULL');
754
datalist.append('(void *)NULL')
698
755
tname = english_upper(chartoname[t.type])
699
756
funclist.append('%s_%s' % (tname, name))