/
multiarraymodule.c
3868 lines (3457 loc) · 102 KB
/
multiarraymodule.c
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Python Multiarray Module -- A useful collection of functions for creating and
using ndarrays
Original file
Copyright (c) 1995, 1996, 1997 Jim Hugunin, hugunin@mit.edu
Modified for numpy in 2005
Travis E. Oliphant
oliphant@ee.byu.edu
Brigham Young University
*/
/* $Id: multiarraymodule.c,v 1.36 2005/09/14 00:14:00 teoliphant Exp $ */
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "structmember.h"
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "numpy/npy_math.h"
#include "npy_config.h"
#include "numpy/npy_3kcompat.h"
NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0;
#define PyAO PyArrayObject
/* Internal APIs */
#include "arraytypes.h"
#include "arrayobject.h"
#include "hashdescr.h"
#include "descriptor.h"
#include "calculation.h"
#include "number.h"
#include "scalartypes.h"
#include "numpymemoryview.h"
#include "convert_datatype.h"
#include "nditer_pywrap.h"
/* Only here for API compatibility */
NPY_NO_EXPORT PyTypeObject PyBigArray_Type;
/*NUMPY_API
* Get Priority from object
*/
NPY_NO_EXPORT double
PyArray_GetPriority(PyObject *obj, double default_)
{
PyObject *ret;
double priority = PyArray_PRIORITY;
if (PyArray_CheckExact(obj))
return priority;
ret = PyObject_GetAttrString(obj, "__array_priority__");
if (ret != NULL) {
priority = PyFloat_AsDouble(ret);
}
if (PyErr_Occurred()) {
PyErr_Clear();
priority = default_;
}
Py_XDECREF(ret);
return priority;
}
/*NUMPY_API
* Multiply a List of ints
*/
NPY_NO_EXPORT int
PyArray_MultiplyIntList(int *l1, int n)
{
int s = 1;
while (n--) {
s *= (*l1++);
}
return s;
}
/*NUMPY_API
* Multiply a List
*/
NPY_NO_EXPORT npy_intp
PyArray_MultiplyList(npy_intp *l1, int n)
{
npy_intp s = 1;
while (n--) {
s *= (*l1++);
}
return s;
}
/*NUMPY_API
* Multiply a List of Non-negative numbers with over-flow detection.
*/
NPY_NO_EXPORT npy_intp
PyArray_OverflowMultiplyList(npy_intp *l1, int n)
{
npy_intp prod = 1;
npy_intp imax = NPY_MAX_INTP;
int i;
for (i = 0; i < n; i++) {
npy_intp dim = l1[i];
if (dim == 0) {
return 0;
}
if (dim > imax) {
return -1;
}
imax /= dim;
prod *= dim;
}
return prod;
}
/*NUMPY_API
* Produce a pointer into array
*/
NPY_NO_EXPORT void *
PyArray_GetPtr(PyArrayObject *obj, npy_intp* ind)
{
int n = obj->nd;
npy_intp *strides = obj->strides;
char *dptr = obj->data;
while (n--) {
dptr += (*strides++) * (*ind++);
}
return (void *)dptr;
}
/*NUMPY_API
* Compare Lists
*/
NPY_NO_EXPORT int
PyArray_CompareLists(npy_intp *l1, npy_intp *l2, int n)
{
int i;
for (i = 0; i < n; i++) {
if (l1[i] != l2[i]) {
return 0;
}
}
return 1;
}
/*
* simulates a C-style 1-3 dimensional array which can be accesed using
* ptr[i] or ptr[i][j] or ptr[i][j][k] -- requires pointer allocation
* for 2-d and 3-d.
*
* For 2-d and up, ptr is NOT equivalent to a statically defined
* 2-d or 3-d array. In particular, it cannot be passed into a
* function that requires a true pointer to a fixed-size array.
*/
/*NUMPY_API
* Simulate a C-array
* steals a reference to typedescr -- can be NULL
*/
NPY_NO_EXPORT int
PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd,
PyArray_Descr* typedescr)
{
PyArrayObject *ap;
npy_intp n, m, i, j;
char **ptr2;
char ***ptr3;
if ((nd < 1) || (nd > 3)) {
PyErr_SetString(PyExc_ValueError,
"C arrays of only 1-3 dimensions available");
Py_XDECREF(typedescr);
return -1;
}
if ((ap = (PyArrayObject*)PyArray_FromAny(*op, typedescr, nd, nd,
CARRAY, NULL)) == NULL) {
return -1;
}
switch(nd) {
case 1:
*((char **)ptr) = ap->data;
break;
case 2:
n = ap->dimensions[0];
ptr2 = (char **)_pya_malloc(n * sizeof(char *));
if (!ptr2) {
goto fail;
}
for (i = 0; i < n; i++) {
ptr2[i] = ap->data + i*ap->strides[0];
}
*((char ***)ptr) = ptr2;
break;
case 3:
n = ap->dimensions[0];
m = ap->dimensions[1];
ptr3 = (char ***)_pya_malloc(n*(m+1) * sizeof(char *));
if (!ptr3) {
goto fail;
}
for (i = 0; i < n; i++) {
ptr3[i] = ptr3[n + (m-1)*i];
for (j = 0; j < m; j++) {
ptr3[i][j] = ap->data + i*ap->strides[0] + j*ap->strides[1];
}
}
*((char ****)ptr) = ptr3;
}
memcpy(dims, ap->dimensions, nd*sizeof(npy_intp));
*op = (PyObject *)ap;
return 0;
fail:
PyErr_SetString(PyExc_MemoryError, "no memory");
return -1;
}
/* Deprecated --- Use PyArray_AsCArray instead */
/*NUMPY_API
* Convert to a 1D C-array
*/
NPY_NO_EXPORT int
PyArray_As1D(PyObject **op, char **ptr, int *d1, int typecode)
{
npy_intp newd1;
PyArray_Descr *descr;
char msg[] = "PyArray_As1D: use PyArray_AsCArray.";
if (DEPRECATE(msg) < 0) {
return -1;
}
descr = PyArray_DescrFromType(typecode);
if (PyArray_AsCArray(op, (void *)ptr, &newd1, 1, descr) == -1) {
return -1;
}
*d1 = (int) newd1;
return 0;
}
/*NUMPY_API
* Convert to a 2D C-array
*/
NPY_NO_EXPORT int
PyArray_As2D(PyObject **op, char ***ptr, int *d1, int *d2, int typecode)
{
npy_intp newdims[2];
PyArray_Descr *descr;
char msg[] = "PyArray_As1D: use PyArray_AsCArray.";
if (DEPRECATE(msg) < 0) {
return -1;
}
descr = PyArray_DescrFromType(typecode);
if (PyArray_AsCArray(op, (void *)ptr, newdims, 2, descr) == -1) {
return -1;
}
*d1 = (int ) newdims[0];
*d2 = (int ) newdims[1];
return 0;
}
/* End Deprecated */
/*NUMPY_API
* Free pointers created if As2D is called
*/
NPY_NO_EXPORT int
PyArray_Free(PyObject *op, void *ptr)
{
PyArrayObject *ap = (PyArrayObject *)op;
if ((ap->nd < 1) || (ap->nd > 3)) {
return -1;
}
if (ap->nd >= 2) {
_pya_free(ptr);
}
Py_DECREF(ap);
return 0;
}
static PyObject *
_swap_and_concat(PyObject *op, int axis, int n)
{
PyObject *newtup = NULL;
PyObject *otmp, *arr;
int i;
newtup = PyTuple_New(n);
if (newtup == NULL) {
return NULL;
}
for (i = 0; i < n; i++) {
otmp = PySequence_GetItem(op, i);
arr = PyArray_FROM_O(otmp);
Py_DECREF(otmp);
if (arr == NULL) {
goto fail;
}
otmp = PyArray_SwapAxes((PyArrayObject *)arr, axis, 0);
Py_DECREF(arr);
if (otmp == NULL) {
goto fail;
}
PyTuple_SET_ITEM(newtup, i, otmp);
}
otmp = PyArray_Concatenate(newtup, 0);
Py_DECREF(newtup);
if (otmp == NULL) {
return NULL;
}
arr = PyArray_SwapAxes((PyArrayObject *)otmp, axis, 0);
Py_DECREF(otmp);
return arr;
fail:
Py_DECREF(newtup);
return NULL;
}
/*NUMPY_API
* Concatenate
*
* Concatenate an arbitrary Python sequence into an array.
* op is a python object supporting the sequence interface.
* Its elements will be concatenated together to form a single
* multidimensional array. If axis is MAX_DIMS or bigger, then
* each sequence object will be flattened before concatenation
*/
NPY_NO_EXPORT PyObject *
PyArray_Concatenate(PyObject *op, int axis)
{
PyArrayObject *ret, **mps;
PyObject *otmp;
int i, n, tmp, nd = 0, new_dim;
char *data;
PyTypeObject *subtype;
double prior1, prior2;
npy_intp numbytes;
n = PySequence_Length(op);
if (n == -1) {
return NULL;
}
if (n == 0) {
PyErr_SetString(PyExc_ValueError,
"concatenation of zero-length sequences is "\
"impossible");
return NULL;
}
if ((axis < 0) || ((0 < axis) && (axis < MAX_DIMS))) {
return _swap_and_concat(op, axis, n);
}
mps = PyArray_ConvertToCommonType(op, &n);
if (mps == NULL) {
return NULL;
}
/*
* Make sure these arrays are legal to concatenate.
* Must have same dimensions except d0
*/
prior1 = PyArray_PRIORITY;
subtype = &PyArray_Type;
ret = NULL;
for (i = 0; i < n; i++) {
if (axis >= MAX_DIMS) {
otmp = PyArray_Ravel(mps[i],0);
Py_DECREF(mps[i]);
mps[i] = (PyArrayObject *)otmp;
}
if (Py_TYPE(mps[i]) != subtype) {
prior2 = PyArray_GetPriority((PyObject *)(mps[i]), 0.0);
if (prior2 > prior1) {
prior1 = prior2;
subtype = Py_TYPE(mps[i]);
}
}
}
new_dim = 0;
for (i = 0; i < n; i++) {
if (mps[i] == NULL) {
goto fail;
}
if (i == 0) {
nd = mps[i]->nd;
}
else {
if (nd != mps[i]->nd) {
PyErr_SetString(PyExc_ValueError,
"arrays must have same "\
"number of dimensions");
goto fail;
}
if (!PyArray_CompareLists(mps[0]->dimensions+1,
mps[i]->dimensions+1,
nd-1)) {
PyErr_SetString(PyExc_ValueError,
"array dimensions must "\
"agree except for d_0");
goto fail;
}
}
if (nd == 0) {
PyErr_SetString(PyExc_ValueError,
"0-d arrays can't be concatenated");
goto fail;
}
new_dim += mps[i]->dimensions[0];
}
tmp = mps[0]->dimensions[0];
mps[0]->dimensions[0] = new_dim;
Py_INCREF(mps[0]->descr);
ret = (PyArrayObject *)PyArray_NewFromDescr(subtype,
mps[0]->descr, nd,
mps[0]->dimensions,
NULL, NULL, 0,
(PyObject *)ret);
mps[0]->dimensions[0] = tmp;
if (ret == NULL) {
goto fail;
}
data = ret->data;
for (i = 0; i < n; i++) {
numbytes = PyArray_NBYTES(mps[i]);
memcpy(data, mps[i]->data, numbytes);
data += numbytes;
}
PyArray_INCREF(ret);
for (i = 0; i < n; i++) {
Py_XDECREF(mps[i]);
}
PyDataMem_FREE(mps);
return (PyObject *)ret;
fail:
Py_XDECREF(ret);
for (i = 0; i < n; i++) {
Py_XDECREF(mps[i]);
}
PyDataMem_FREE(mps);
return NULL;
}
static int
_signbit_set(PyArrayObject *arr)
{
static char bitmask = (char) 0x80;
char *ptr; /* points to the byte to test */
char byteorder;
int elsize;
elsize = arr->descr->elsize;
byteorder = arr->descr->byteorder;
ptr = arr->data;
if (elsize > 1 &&
(byteorder == PyArray_LITTLE ||
(byteorder == PyArray_NATIVE &&
PyArray_ISNBO(PyArray_LITTLE)))) {
ptr += elsize - 1;
}
return ((*ptr & bitmask) != 0);
}
/*NUMPY_API
* ScalarKind
*
* Returns the scalar kind of a type number, with an
* optional tweak based on the scalar value itself.
* If no scalar is provided, it returns INTPOS_SCALAR
* for both signed and unsigned integers, otherwise
* it checks the sign of any signed integer to choose
* INTNEG_SCALAR when appropriate.
*/
NPY_NO_EXPORT NPY_SCALARKIND
PyArray_ScalarKind(int typenum, PyArrayObject **arr)
{
NPY_SCALARKIND ret = PyArray_NOSCALAR;
if ((unsigned int)typenum < NPY_NTYPES) {
ret = _npy_scalar_kinds_table[typenum];
/* Signed integer types are INTNEG in the table */
if (ret == PyArray_INTNEG_SCALAR) {
if (!arr || !_signbit_set(*arr)) {
ret = PyArray_INTPOS_SCALAR;
}
}
} else if (PyTypeNum_ISUSERDEF(typenum)) {
PyArray_Descr* descr = PyArray_DescrFromType(typenum);
if (descr->f->scalarkind) {
ret = descr->f->scalarkind((arr ? *arr : NULL));
}
Py_DECREF(descr);
}
return ret;
}
/*NUMPY_API
*
* Determines whether the data type 'thistype', with
* scalar kind 'scalar', can be coerced into 'neededtype'.
*/
NPY_NO_EXPORT int
PyArray_CanCoerceScalar(int thistype, int neededtype,
NPY_SCALARKIND scalar)
{
PyArray_Descr* from;
int *castlist;
/* If 'thistype' is not a scalar, it must be safely castable */
if (scalar == PyArray_NOSCALAR) {
return PyArray_CanCastSafely(thistype, neededtype);
}
if ((unsigned int)neededtype < NPY_NTYPES) {
NPY_SCALARKIND neededscalar;
if (scalar == PyArray_OBJECT_SCALAR) {
return PyArray_CanCastSafely(thistype, neededtype);
}
/*
* The lookup table gives us exactly what we need for
* this comparison, which PyArray_ScalarKind would not.
*
* The rule is that positive scalars can be coerced
* to a signed ints, but negative scalars cannot be coerced
* to unsigned ints.
* _npy_scalar_kinds_table[int]==NEGINT > POSINT,
* so 1 is returned, but
* _npy_scalar_kinds_table[uint]==POSINT < NEGINT,
* so 0 is returned, as required.
*
*/
neededscalar = _npy_scalar_kinds_table[neededtype];
if (neededscalar >= scalar) {
return 1;
}
if (!PyTypeNum_ISUSERDEF(thistype)) {
return 0;
}
}
from = PyArray_DescrFromType(thistype);
if (from->f->cancastscalarkindto
&& (castlist = from->f->cancastscalarkindto[scalar])) {
while (*castlist != PyArray_NOTYPE) {
if (*castlist++ == neededtype) {
Py_DECREF(from);
return 1;
}
}
}
Py_DECREF(from);
return 0;
}
/*
* Make a new empty array, of the passed size, of a type that takes the
* priority of ap1 and ap2 into account.
*/
static PyArrayObject *
new_array_for_sum(PyArrayObject *ap1, PyArrayObject *ap2, PyArrayObject* out,
int nd, npy_intp dimensions[], int typenum)
{
PyArrayObject *ret;
PyTypeObject *subtype;
double prior1, prior2;
/*
* Need to choose an output array that can hold a sum
* -- use priority to determine which subtype.
*/
if (Py_TYPE(ap2) != Py_TYPE(ap1)) {
prior2 = PyArray_GetPriority((PyObject *)ap2, 0.0);
prior1 = PyArray_GetPriority((PyObject *)ap1, 0.0);
subtype = (prior2 > prior1 ? Py_TYPE(ap2) : Py_TYPE(ap1));
}
else {
prior1 = prior2 = 0.0;
subtype = Py_TYPE(ap1);
}
if (out) {
int d;
/* verify that out is usable */
if (Py_TYPE(out) != subtype ||
PyArray_NDIM(out) != nd ||
PyArray_TYPE(out) != typenum ||
!PyArray_ISCARRAY(out)) {
PyErr_SetString(PyExc_ValueError,
"output array is not acceptable "
"(must have the right type, nr dimensions, and be a C-Array)");
return 0;
}
for (d = 0; d < nd; ++d) {
if (dimensions[d] != PyArray_DIM(out, d)) {
PyErr_SetString(PyExc_ValueError,
"output array has wrong dimensions");
return 0;
}
}
Py_INCREF(out);
return out;
}
ret = (PyArrayObject *)PyArray_New(subtype, nd, dimensions,
typenum, NULL, NULL, 0, 0,
(PyObject *)
(prior2 > prior1 ? ap2 : ap1));
return ret;
}
/* Could perhaps be redone to not make contiguous arrays */
/*NUMPY_API
* Numeric.innerproduct(a,v)
*/
NPY_NO_EXPORT PyObject *
PyArray_InnerProduct(PyObject *op1, PyObject *op2)
{
PyArrayObject *ap1, *ap2, *ret = NULL;
PyArrayIterObject *it1, *it2;
npy_intp i, j, l;
int typenum, nd, axis;
npy_intp is1, is2, os;
char *op;
npy_intp dimensions[MAX_DIMS];
PyArray_DotFunc *dot;
PyArray_Descr *typec;
NPY_BEGIN_THREADS_DEF;
typenum = PyArray_ObjectType(op1, 0);
typenum = PyArray_ObjectType(op2, typenum);
typec = PyArray_DescrFromType(typenum);
Py_INCREF(typec);
ap1 = (PyArrayObject *)PyArray_FromAny(op1, typec, 0, 0, ALIGNED, NULL);
if (ap1 == NULL) {
Py_DECREF(typec);
return NULL;
}
ap2 = (PyArrayObject *)PyArray_FromAny(op2, typec, 0, 0, ALIGNED, NULL);
if (ap2 == NULL) {
goto fail;
}
if (ap1->nd == 0 || ap2->nd == 0) {
ret = (ap1->nd == 0 ? ap1 : ap2);
ret = (PyArrayObject *)Py_TYPE(ret)->tp_as_number->nb_multiply(
(PyObject *)ap1, (PyObject *)ap2);
Py_DECREF(ap1);
Py_DECREF(ap2);
return (PyObject *)ret;
}
l = ap1->dimensions[ap1->nd - 1];
if (ap2->dimensions[ap2->nd - 1] != l) {
PyErr_SetString(PyExc_ValueError, "matrices are not aligned");
goto fail;
}
nd = ap1->nd + ap2->nd - 2;
j = 0;
for (i = 0; i < ap1->nd - 1; i++) {
dimensions[j++] = ap1->dimensions[i];
}
for (i = 0; i < ap2->nd - 1; i++) {
dimensions[j++] = ap2->dimensions[i];
}
/*
* Need to choose an output array that can hold a sum
* -- use priority to determine which subtype.
*/
ret = new_array_for_sum(ap1, ap2, NULL, nd, dimensions, typenum);
if (ret == NULL) {
goto fail;
}
dot = (ret->descr->f->dotfunc);
if (dot == NULL) {
PyErr_SetString(PyExc_ValueError,
"dot not available for this type");
goto fail;
}
is1 = ap1->strides[ap1->nd - 1];
is2 = ap2->strides[ap2->nd - 1];
op = ret->data; os = ret->descr->elsize;
axis = ap1->nd - 1;
it1 = (PyArrayIterObject *) PyArray_IterAllButAxis((PyObject *)ap1, &axis);
axis = ap2->nd - 1;
it2 = (PyArrayIterObject *) PyArray_IterAllButAxis((PyObject *)ap2, &axis);
NPY_BEGIN_THREADS_DESCR(ap2->descr);
while (1) {
while (it2->index < it2->size) {
dot(it1->dataptr, is1, it2->dataptr, is2, op, l, ret);
op += os;
PyArray_ITER_NEXT(it2);
}
PyArray_ITER_NEXT(it1);
if (it1->index >= it1->size) {
break;
}
PyArray_ITER_RESET(it2);
}
NPY_END_THREADS_DESCR(ap2->descr);
Py_DECREF(it1);
Py_DECREF(it2);
if (PyErr_Occurred()) {
goto fail;
}
Py_DECREF(ap1);
Py_DECREF(ap2);
return (PyObject *)ret;
fail:
Py_XDECREF(ap1);
Py_XDECREF(ap2);
Py_XDECREF(ret);
return NULL;
}
/*NUMPY_API
* Numeric.matrixproduct(a,v,out)
* just like inner product but does the swapaxes stuff on the fly
*/
NPY_NO_EXPORT PyObject *
PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out)
{
PyArrayObject *ap1, *ap2, *ret = NULL;
PyArrayIterObject *it1, *it2;
npy_intp i, j, l;
int typenum, nd, axis, matchDim;
npy_intp is1, is2, os;
char *op;
npy_intp dimensions[MAX_DIMS];
PyArray_DotFunc *dot;
PyArray_Descr *typec;
NPY_BEGIN_THREADS_DEF;
typenum = PyArray_ObjectType(op1, 0);
typenum = PyArray_ObjectType(op2, typenum);
typec = PyArray_DescrFromType(typenum);
Py_INCREF(typec);
ap1 = (PyArrayObject *)PyArray_FromAny(op1, typec, 0, 0, ALIGNED, NULL);
if (ap1 == NULL) {
Py_DECREF(typec);
return NULL;
}
ap2 = (PyArrayObject *)PyArray_FromAny(op2, typec, 0, 0, ALIGNED, NULL);
if (ap2 == NULL) {
goto fail;
}
if (ap1->nd == 0 || ap2->nd == 0) {
ret = (ap1->nd == 0 ? ap1 : ap2);
ret = (PyArrayObject *)Py_TYPE(ret)->tp_as_number->nb_multiply(
(PyObject *)ap1, (PyObject *)ap2);
Py_DECREF(ap1);
Py_DECREF(ap2);
return (PyObject *)ret;
}
l = ap1->dimensions[ap1->nd - 1];
if (ap2->nd > 1) {
matchDim = ap2->nd - 2;
}
else {
matchDim = 0;
}
if (ap2->dimensions[matchDim] != l) {
PyErr_SetString(PyExc_ValueError, "objects are not aligned");
goto fail;
}
nd = ap1->nd + ap2->nd - 2;
if (nd > NPY_MAXDIMS) {
PyErr_SetString(PyExc_ValueError, "dot: too many dimensions in result");
goto fail;
}
j = 0;
for (i = 0; i < ap1->nd - 1; i++) {
dimensions[j++] = ap1->dimensions[i];
}
for (i = 0; i < ap2->nd - 2; i++) {
dimensions[j++] = ap2->dimensions[i];
}
if(ap2->nd > 1) {
dimensions[j++] = ap2->dimensions[ap2->nd-1];
}
/*
fprintf(stderr, "nd=%d dimensions=", nd);
for(i=0; i<j; i++)
fprintf(stderr, "%d ", dimensions[i]);
fprintf(stderr, "\n");
*/
is1 = ap1->strides[ap1->nd-1]; is2 = ap2->strides[matchDim];
/* Choose which subtype to return */
ret = new_array_for_sum(ap1, ap2, out, nd, dimensions, typenum);
if (ret == NULL) {
goto fail;
}
/* Ensure that multiarray.dot(<Nx0>,<0xM>) -> zeros((N,M)) */
if (PyArray_SIZE(ap1) == 0 && PyArray_SIZE(ap2) == 0) {
memset(PyArray_DATA(ret), 0, PyArray_NBYTES(ret));
}
else {
/* Ensure that multiarray.dot([],[]) -> 0 */
memset(PyArray_DATA(ret), 0, PyArray_ITEMSIZE(ret));
}
dot = ret->descr->f->dotfunc;
if (dot == NULL) {
PyErr_SetString(PyExc_ValueError,
"dot not available for this type");
goto fail;
}
op = ret->data; os = ret->descr->elsize;
axis = ap1->nd-1;
it1 = (PyArrayIterObject *)
PyArray_IterAllButAxis((PyObject *)ap1, &axis);
it2 = (PyArrayIterObject *)
PyArray_IterAllButAxis((PyObject *)ap2, &matchDim);
NPY_BEGIN_THREADS_DESCR(ap2->descr);
while (1) {
while (it2->index < it2->size) {
dot(it1->dataptr, is1, it2->dataptr, is2, op, l, ret);
op += os;
PyArray_ITER_NEXT(it2);
}
PyArray_ITER_NEXT(it1);
if (it1->index >= it1->size) {
break;
}
PyArray_ITER_RESET(it2);
}
NPY_END_THREADS_DESCR(ap2->descr);
Py_DECREF(it1);
Py_DECREF(it2);
if (PyErr_Occurred()) {
/* only for OBJECT arrays */
goto fail;
}
Py_DECREF(ap1);
Py_DECREF(ap2);
return (PyObject *)ret;
fail:
Py_XDECREF(ap1);
Py_XDECREF(ap2);
Py_XDECREF(ret);
return NULL;
}
/*NUMPY_API
*Numeric.matrixproduct(a,v)
* just like inner product but does the swapaxes stuff on the fly
*/
NPY_NO_EXPORT PyObject *
PyArray_MatrixProduct(PyObject *op1, PyObject *op2)
{
return PyArray_MatrixProduct2(op1, op2, NULL);
}
/*NUMPY_API
* Copy and Transpose
*
* Could deprecate this function, as there isn't a speed benefit over
* calling Transpose and then Copy.
*/
NPY_NO_EXPORT PyObject *
PyArray_CopyAndTranspose(PyObject *op)
{
PyArrayObject *arr, *tmp, *ret;
int i;
npy_intp new_axes_values[NPY_MAXDIMS];
PyArray_Dims new_axes;
/* Make sure we have an array */
arr = (PyArrayObject *)PyArray_FromAny(op, NULL, 0, 0, 0, NULL);
if (arr == NULL) {
return NULL;
}
if (PyArray_NDIM(arr) > 1) {
/* Set up the transpose operation */
new_axes.len = PyArray_NDIM(arr);
for (i = 0; i < new_axes.len; ++i) {
new_axes_values[i] = new_axes.len - i - 1;
}
new_axes.ptr = new_axes_values;
/* Do the transpose (always returns a view) */
tmp = (PyArrayObject *)PyArray_Transpose(arr, &new_axes);
if (tmp == NULL) {
Py_DECREF(arr);
return NULL;
}
}
else {
tmp = arr;
arr = NULL;
}
/* TODO: Change this to NPY_KEEPORDER for NumPy 2.0 */
ret = (PyArrayObject *)PyArray_NewCopy(tmp, NPY_CORDER);
Py_XDECREF(arr);
Py_DECREF(tmp);
return (PyObject *)ret;
}
/*
* Implementation which is common between PyArray_Correlate and PyArray_Correlate2
*
* inverted is set to 1 if computed correlate(ap2, ap1), 0 otherwise
*/
static PyArrayObject*
_pyarray_correlate(PyArrayObject *ap1, PyArrayObject *ap2, int typenum,
int mode, int *inverted)
{
PyArrayObject *ret;
npy_intp length;
npy_intp i, n1, n2, n, n_left, n_right;
npy_intp is1, is2, os;
char *ip1, *ip2, *op;
PyArray_DotFunc *dot;
NPY_BEGIN_THREADS_DEF;
n1 = ap1->dimensions[0];
n2 = ap2->dimensions[0];
if (n1 < n2) {
ret = ap1;
ap1 = ap2;
ap2 = ret;
ret = NULL;
i = n1;
n1 = n2;
n2 = i;
*inverted = 1;
} else {
*inverted = 0;
}
length = n1;
n = n2;
switch(mode) {
case 0:
length = length - n + 1;
n_left = n_right = 0;
break;
case 1:
n_left = (npy_intp)(n/2);
n_right = n - n_left - 1;
break;
case 2:
n_right = n - 1;
n_left = n - 1;
length = length + n - 1;
break;
default:
PyErr_SetString(PyExc_ValueError, "mode must be 0, 1, or 2");
return NULL;
}
/*
* Need to choose an output array that can hold a sum
* -- use priority to determine which subtype.
*/
ret = new_array_for_sum(ap1, ap2, NULL, 1, &length, typenum);
if (ret == NULL) {
return NULL;
}
dot = ret->descr->f->dotfunc;
if (dot == NULL) {
PyErr_SetString(PyExc_ValueError,
"function not available for this data type");
goto clean_ret;
}