-
-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Expand file tree
/
Copy pathmultiarraymodule.c
More file actions
8584 lines (7933 loc) · 229 KB
/
Copy pathmultiarraymodule.c
File metadata and controls
8584 lines (7933 loc) · 229 KB
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"
#define PyAO PyArrayObject
#include "hashdescr.c"
static PyObject *typeDict = NULL; /* Must be explicitly loaded */
static PyArray_Descr *
_arraydescr_fromobj(PyObject *obj)
{
PyObject *dtypedescr;
PyArray_Descr *new;
int ret;
dtypedescr = PyObject_GetAttrString(obj, "dtype");
PyErr_Clear();
if (dtypedescr) {
ret = PyArray_DescrConverter(dtypedescr, &new);
Py_DECREF(dtypedescr);
if (ret == PY_SUCCEED) {
return new;
}
PyErr_Clear();
}
/* Understand basic ctypes */
dtypedescr = PyObject_GetAttrString(obj, "_type_");
PyErr_Clear();
if (dtypedescr) {
ret = PyArray_DescrConverter(dtypedescr, &new);
Py_DECREF(dtypedescr);
if (ret == PY_SUCCEED) {
PyObject *length;
length = PyObject_GetAttrString(obj, "_length_");
PyErr_Clear();
if (length) {
/* derived type */
PyObject *newtup;
PyArray_Descr *derived;
newtup = Py_BuildValue("NO", new, length);
ret = PyArray_DescrConverter(newtup, &derived);
Py_DECREF(newtup);
if (ret == PY_SUCCEED) {
return derived;
}
PyErr_Clear();
return NULL;
}
return new;
}
PyErr_Clear();
return NULL;
}
/* Understand ctypes structures --
bit-fields are not supported
automatically aligns */
dtypedescr = PyObject_GetAttrString(obj, "_fields_");
PyErr_Clear();
if (dtypedescr) {
ret = PyArray_DescrAlignConverter(dtypedescr, &new);
Py_DECREF(dtypedescr);
if (ret == PY_SUCCEED) {
return new;
}
PyErr_Clear();
}
return NULL;
}
/*
* Including this file is the only way I know how to declare functions
* static in each file, and store the pointers from functions in both
* arrayobject.c and multiarraymodule.c for the C-API
*
* Declarying an external pointer-containing variable in arrayobject.c
* and trying to copy it to PyArray_API, did not work.
*
* Think about two modules with a common api that import each other...
*
* This file would just be the module calls.
*/
#include "arrayobject.c"
/* An Error object -- rarely used? */
static PyObject *MultiArrayError;
/*NUMPY_API
* Multiply a List of ints
*/
static int
PyArray_MultiplyIntList(register int *l1, register int n)
{
int s = 1;
while (n--) {
s *= (*l1++);
}
return s;
}
/*NUMPY_API
* Multiply a List
*/
static intp
PyArray_MultiplyList(register intp *l1, register int n)
{
intp s = 1;
while (n--) {
s *= (*l1++);
}
return s;
}
/*NUMPY_API
* Multiply a List of Non-negative numbers with over-flow detection.
*/
static intp
PyArray_OverflowMultiplyList(register intp *l1, register int n)
{
intp s = 1;
while (n--) {
if (*l1 == 0) {
return 0;
}
if ((s > MAX_INTP / *l1) || (*l1 > MAX_INTP / s)) {
return -1;
}
s *= (*l1++);
}
return s;
}
/*NUMPY_API
* Produce a pointer into array
*/
static void *
PyArray_GetPtr(PyArrayObject *obj, register intp* ind)
{
int n = obj->nd;
intp *strides = obj->strides;
char *dptr = obj->data;
while (n--) {
dptr += (*strides++) * (*ind++);
}
return (void *)dptr;
}
/*NUMPY_API
* Get axis from an object (possibly None) -- a converter function,
*/
static int
PyArray_AxisConverter(PyObject *obj, int *axis)
{
if (obj == Py_None) {
*axis = MAX_DIMS;
}
else {
*axis = (int) PyInt_AsLong(obj);
if (PyErr_Occurred()) {
return PY_FAIL;
}
}
return PY_SUCCEED;
}
/*NUMPY_API
* Compare Lists
*/
static int
PyArray_CompareLists(intp *l1, intp *l2, int n)
{
int i;
for (i = 0; i < n; i++) {
if (l1[i] != l2[i]) {
return 0;
}
}
return 1;
}
/*NUMPY_API
* View
* steals a reference to type -- accepts NULL
*/
static PyObject *
PyArray_View(PyArrayObject *self, PyArray_Descr *type, PyTypeObject *pytype)
{
PyObject *new = NULL;
PyTypeObject *subtype;
if (pytype) {
subtype = pytype;
}
else {
subtype = self->ob_type;
}
Py_INCREF(self->descr);
new = PyArray_NewFromDescr(subtype,
self->descr,
self->nd, self->dimensions,
self->strides,
self->data,
self->flags, (PyObject *)self);
if (new == NULL) {
return NULL;
}
Py_INCREF(self);
PyArray_BASE(new) = (PyObject *)self;
if (type != NULL) {
if (PyObject_SetAttrString(new, "dtype",
(PyObject *)type) < 0) {
Py_DECREF(new);
Py_DECREF(type);
return NULL;
}
Py_DECREF(type);
}
return new;
}
/*NUMPY_API
* Ravel
* Returns a contiguous array
*/
static PyObject *
PyArray_Ravel(PyArrayObject *a, NPY_ORDER fortran)
{
PyArray_Dims newdim = {NULL,1};
intp val[1] = {-1};
if (fortran == PyArray_ANYORDER) {
fortran = PyArray_ISFORTRAN(a);
}
newdim.ptr = val;
if (!fortran && PyArray_ISCONTIGUOUS(a)) {
return PyArray_Newshape(a, &newdim, PyArray_CORDER);
}
else if (fortran && PyArray_ISFORTRAN(a)) {
return PyArray_Newshape(a, &newdim, PyArray_FORTRANORDER);
}
else {
return PyArray_Flatten(a, fortran);
}
}
static double
power_of_ten(int n)
{
static const double p10[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8};
double ret;
if (n < 9) {
ret = p10[n];
}
else {
ret = 1e9;
while (n-- > 9) {
ret *= 10.;
}
}
return ret;
}
/*NUMPY_API
* Round
*/
static PyObject *
PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out)
{
PyObject *f, *ret = NULL, *tmp, *op1, *op2;
int ret_int=0;
PyArray_Descr *my_descr;
if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) {
PyErr_SetString(PyExc_ValueError,
"invalid output shape");
return NULL;
}
if (PyArray_ISCOMPLEX(a)) {
PyObject *part;
PyObject *round_part;
PyObject *new;
int res;
if (out) {
new = (PyObject *)out;
Py_INCREF(new);
}
else {
new = PyArray_Copy(a);
if (new == NULL) {
return NULL;
}
}
/* new.real = a.real.round(decimals) */
part = PyObject_GetAttrString(new, "real");
if (part == NULL) {
Py_DECREF(new);
return NULL;
}
part = PyArray_EnsureAnyArray(part);
round_part = PyArray_Round((PyArrayObject *)part,
decimals, NULL);
Py_DECREF(part);
if (round_part == NULL) {
Py_DECREF(new);
return NULL;
}
res = PyObject_SetAttrString(new, "real", round_part);
Py_DECREF(round_part);
if (res < 0) {
Py_DECREF(new);
return NULL;
}
/* new.imag = a.imag.round(decimals) */
part = PyObject_GetAttrString(new, "imag");
if (part == NULL) {
Py_DECREF(new);
return NULL;
}
part = PyArray_EnsureAnyArray(part);
round_part = PyArray_Round((PyArrayObject *)part,
decimals, NULL);
Py_DECREF(part);
if (round_part == NULL) {
Py_DECREF(new);
return NULL;
}
res = PyObject_SetAttrString(new, "imag", round_part);
Py_DECREF(round_part);
if (res < 0) {
Py_DECREF(new);
return NULL;
}
return new;
}
/* do the most common case first */
if (decimals >= 0) {
if (PyArray_ISINTEGER(a)) {
if (out) {
if (PyArray_CopyAnyInto(out, a) < 0) {
return NULL;
}
Py_INCREF(out);
return (PyObject *)out;
}
else {
Py_INCREF(a);
return (PyObject *)a;
}
}
if (decimals == 0) {
if (out) {
return PyObject_CallFunction(n_ops.rint, "OO", a, out);
}
return PyObject_CallFunction(n_ops.rint, "O", a);
}
op1 = n_ops.multiply;
op2 = n_ops.true_divide;
}
else {
op1 = n_ops.true_divide;
op2 = n_ops.multiply;
decimals = -decimals;
}
if (!out) {
if (PyArray_ISINTEGER(a)) {
ret_int = 1;
my_descr = PyArray_DescrFromType(NPY_DOUBLE);
}
else {
Py_INCREF(a->descr);
my_descr = a->descr;
}
out = (PyArrayObject *)PyArray_Empty(a->nd, a->dimensions,
my_descr,
PyArray_ISFORTRAN(a));
if (out == NULL) {
return NULL;
}
}
else {
Py_INCREF(out);
}
f = PyFloat_FromDouble(power_of_ten(decimals));
if (f == NULL) {
return NULL;
}
ret = PyObject_CallFunction(op1, "OOO", a, f, out);
if (ret == NULL) {
goto finish;
}
tmp = PyObject_CallFunction(n_ops.rint, "OO", ret, ret);
if (tmp == NULL) {
Py_DECREF(ret);
ret = NULL;
goto finish;
}
Py_DECREF(tmp);
tmp = PyObject_CallFunction(op2, "OOO", ret, f, ret);
if (tmp == NULL) {
Py_DECREF(ret);
ret = NULL;
goto finish;
}
Py_DECREF(tmp);
finish:
Py_DECREF(f);
Py_DECREF(out);
if (ret_int) {
Py_INCREF(a->descr);
tmp = PyArray_CastToType((PyArrayObject *)ret,
a->descr, PyArray_ISFORTRAN(a));
Py_DECREF(ret);
return tmp;
}
return ret;
}
/*NUMPY_API
* Flatten
*/
static PyObject *
PyArray_Flatten(PyArrayObject *a, NPY_ORDER order)
{
PyObject *ret;
intp size;
if (order == PyArray_ANYORDER) {
order = PyArray_ISFORTRAN(a);
}
size = PyArray_SIZE(a);
Py_INCREF(a->descr);
ret = PyArray_NewFromDescr(a->ob_type,
a->descr,
1, &size,
NULL,
NULL,
0, (PyObject *)a);
if (ret == NULL) {
return NULL;
}
if (_flat_copyinto(ret, (PyObject *)a, order) < 0) {
Py_DECREF(ret);
return NULL;
}
return ret;
}
/* For back-ward compatability -- Not recommended */
/*NUMPY_API
* Reshape
*/
static PyObject *
PyArray_Reshape(PyArrayObject *self, PyObject *shape)
{
PyObject *ret;
PyArray_Dims newdims;
if (!PyArray_IntpConverter(shape, &newdims)) {
return NULL;
}
ret = PyArray_Newshape(self, &newdims, PyArray_CORDER);
PyDimMem_FREE(newdims.ptr);
return ret;
}
/* inserts 0 for strides where dimension will be 1 */
static int
_check_ones(PyArrayObject *self, int newnd, intp* newdims, intp *strides)
{
int nd;
intp *dims;
Bool done=FALSE;
int j, k;
nd = self->nd;
dims = self->dimensions;
for (k = 0, j = 0; !done && (j < nd || k < newnd);) {
if ((j<nd) && (k<newnd) && (newdims[k] == dims[j])) {
strides[k] = self->strides[j];
j++;
k++;
}
else if ((k < newnd) && (newdims[k] == 1)) {
strides[k] = 0;
k++;
}
else if ((j<nd) && (dims[j] == 1)) {
j++;
}
else {
done = TRUE;
}
}
if (done) {
return -1;
}
return 0;
}
/*
* attempt to reshape an array without copying data
*
* This function should correctly handle all reshapes, including
* axes of length 1. Zero strides should work but are untested.
*
* If a copy is needed, returns 0
* If no copy is needed, returns 1 and fills newstrides
* with appropriate strides
*
* The "fortran" argument describes how the array should be viewed
* during the reshape, not how it is stored in memory (that
* information is in self->strides).
*
* If some output dimensions have length 1, the strides assigned to
* them are arbitrary. In the current implementation, they are the
* stride of the next-fastest index.
*/
static int
_attempt_nocopy_reshape(PyArrayObject *self, int newnd, intp* newdims,
intp *newstrides, int fortran)
{
int oldnd;
intp olddims[MAX_DIMS];
intp oldstrides[MAX_DIMS];
int oi, oj, ok, ni, nj, nk;
int np, op;
oldnd = 0;
for (oi = 0; oi < self->nd; oi++) {
if (self->dimensions[oi]!= 1) {
olddims[oldnd] = self->dimensions[oi];
oldstrides[oldnd] = self->strides[oi];
oldnd++;
}
}
/*
fprintf(stderr, "_attempt_nocopy_reshape( (");
for (oi=0; oi<oldnd; oi++)
fprintf(stderr, "(%d,%d), ", olddims[oi], oldstrides[oi]);
fprintf(stderr, ") -> (");
for (ni=0; ni<newnd; ni++)
fprintf(stderr, "(%d,*), ", newdims[ni]);
fprintf(stderr, "), fortran=%d)\n", fortran);
*/
np = 1;
for (ni = 0; ni < newnd; ni++) {
np *= newdims[ni];
}
op = 1;
for (oi = 0; oi < oldnd; oi++) {
op *= olddims[oi];
}
if (np != op) {
/* different total sizes; no hope */
return 0;
}
/* the current code does not handle 0-sized arrays, so give up */
if (np == 0) {
return 0;
}
oi = 0;
oj = 1;
ni = 0;
nj = 1;
while(ni < newnd && oi < oldnd) {
np = newdims[ni];
op = olddims[oi];
while (np != op) {
if (np < op) {
np *= newdims[nj++];
} else {
op *= olddims[oj++];
}
}
for (ok = oi; ok < oj - 1; ok++) {
if (fortran) {
if (oldstrides[ok+1] != olddims[ok]*oldstrides[ok]) {
/* not contiguous enough */
return 0;
}
}
else {
/* C order */
if (oldstrides[ok] != olddims[ok+1]*oldstrides[ok+1]) {
/* not contiguous enough */
return 0;
}
}
}
if (fortran) {
newstrides[ni] = oldstrides[oi];
for (nk = ni + 1; nk < nj; nk++) {
newstrides[nk] = newstrides[nk - 1]*newdims[nk - 1];
}
}
else {
/* C order */
newstrides[nj - 1] = oldstrides[oj - 1];
for (nk = nj - 1; nk > ni; nk--) {
newstrides[nk - 1] = newstrides[nk]*newdims[nk];
}
}
ni = nj++;
oi = oj++;
}
/*
fprintf(stderr, "success: _attempt_nocopy_reshape (");
for (oi=0; oi<oldnd; oi++)
fprintf(stderr, "(%d,%d), ", olddims[oi], oldstrides[oi]);
fprintf(stderr, ") -> (");
for (ni=0; ni<newnd; ni++)
fprintf(stderr, "(%d,%d), ", newdims[ni], newstrides[ni]);
fprintf(stderr, ")\n");
*/
return 1;
}
static int
_fix_unknown_dimension(PyArray_Dims *newshape, intp s_original)
{
intp *dimensions;
intp i_unknown, s_known;
int i, n;
static char msg[] = "total size of new array must be unchanged";
dimensions = newshape->ptr;
n = newshape->len;
s_known = 1;
i_unknown = -1;
for (i = 0; i < n; i++) {
if (dimensions[i] < 0) {
if (i_unknown == -1) {
i_unknown = i;
}
else {
PyErr_SetString(PyExc_ValueError,
"can only specify one" \
" unknown dimension");
return -1;
}
}
else {
s_known *= dimensions[i];
}
}
if (i_unknown >= 0) {
if ((s_known == 0) || (s_original % s_known != 0)) {
PyErr_SetString(PyExc_ValueError, msg);
return -1;
}
dimensions[i_unknown] = s_original/s_known;
}
else {
if (s_original != s_known) {
PyErr_SetString(PyExc_ValueError, msg);
return -1;
}
}
return 0;
}
/*
* Returns a new array
* with the new shape from the data
* in the old array --- order-perspective depends on fortran argument.
* copy-only-if-necessary
*/
/*NUMPY_API
* New shape for an array
*/
static PyObject *
PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims,
NPY_ORDER fortran)
{
intp i;
intp *dimensions = newdims->ptr;
PyArrayObject *ret;
int n = newdims->len;
Bool same, incref = TRUE;
intp *strides = NULL;
intp newstrides[MAX_DIMS];
int flags;
if (fortran == PyArray_ANYORDER) {
fortran = PyArray_ISFORTRAN(self);
}
/* Quick check to make sure anything actually needs to be done */
if (n == self->nd) {
same = TRUE;
i = 0;
while (same && i < n) {
if (PyArray_DIM(self,i) != dimensions[i]) {
same=FALSE;
}
i++;
}
if (same) {
return PyArray_View(self, NULL, NULL);
}
}
/*
* Returns a pointer to an appropriate strides array
* if all we are doing is inserting ones into the shape,
* or removing ones from the shape
* or doing a combination of the two
* In this case we don't need to do anything but update strides and
* dimensions. So, we can handle non single-segment cases.
*/
i = _check_ones(self, n, dimensions, newstrides);
if (i == 0) {
strides = newstrides;
}
flags = self->flags;
if (strides == NULL) {
/*
* we are really re-shaping not just adding ones to the shape somewhere
* fix any -1 dimensions and check new-dimensions against old size
*/
if (_fix_unknown_dimension(newdims, PyArray_SIZE(self)) < 0) {
return NULL;
}
/*
* sometimes we have to create a new copy of the array
* in order to get the right orientation and
* because we can't just re-use the buffer with the
* data in the order it is in.
*/
if (!(PyArray_ISONESEGMENT(self)) ||
(((PyArray_CHKFLAGS(self, NPY_CONTIGUOUS) &&
fortran == NPY_FORTRANORDER) ||
(PyArray_CHKFLAGS(self, NPY_FORTRAN) &&
fortran == NPY_CORDER)) && (self->nd > 1))) {
int success = 0;
success = _attempt_nocopy_reshape(self,n,dimensions,
newstrides,fortran);
if (success) {
/* no need to copy the array after all */
strides = newstrides;
flags = self->flags;
}
else {
PyObject *new;
new = PyArray_NewCopy(self, fortran);
if (new == NULL) {
return NULL;
}
incref = FALSE;
self = (PyArrayObject *)new;
flags = self->flags;
}
}
/* We always have to interpret the contiguous buffer correctly */
/* Make sure the flags argument is set. */
if (n > 1) {
if (fortran == NPY_FORTRANORDER) {
flags &= ~NPY_CONTIGUOUS;
flags |= NPY_FORTRAN;
}
else {
flags &= ~NPY_FORTRAN;
flags |= NPY_CONTIGUOUS;
}
}
}
else if (n > 0) {
/*
* replace any 0-valued strides with
* appropriate value to preserve contiguousness
*/
if (fortran == PyArray_FORTRANORDER) {
if (strides[0] == 0) {
strides[0] = self->descr->elsize;
}
for (i = 1; i < n; i++) {
if (strides[i] == 0) {
strides[i] = strides[i-1] * dimensions[i-1];
}
}
}
else {
if (strides[n-1] == 0) {
strides[n-1] = self->descr->elsize;
}
for (i = n - 2; i > -1; i--) {
if (strides[i] == 0) {
strides[i] = strides[i+1] * dimensions[i+1];
}
}
}
}
Py_INCREF(self->descr);
ret = (PyAO *)PyArray_NewFromDescr(self->ob_type,
self->descr,
n, dimensions,
strides,
self->data,
flags, (PyObject *)self);
if (ret == NULL) {
goto fail;
}
if (incref) {
Py_INCREF(self);
}
ret->base = (PyObject *)self;
PyArray_UpdateFlags(ret, CONTIGUOUS | FORTRAN);
return (PyObject *)ret;
fail:
if (!incref) {
Py_DECREF(self);
}
return NULL;
}
/*NUMPY_API
*
* return a new view of the array object with all of its unit-length
* dimensions squeezed out if needed, otherwise
* return the same array.
*/
static PyObject *
PyArray_Squeeze(PyArrayObject *self)
{
int nd = self->nd;
int newnd = nd;
intp dimensions[MAX_DIMS];
intp strides[MAX_DIMS];
int i, j;
PyObject *ret;
if (nd == 0) {
Py_INCREF(self);
return (PyObject *)self;
}
for (j = 0, i = 0; i < nd; i++) {
if (self->dimensions[i] == 1) {
newnd -= 1;
}
else {
dimensions[j] = self->dimensions[i];
strides[j++] = self->strides[i];
}
}
Py_INCREF(self->descr);
ret = PyArray_NewFromDescr(self->ob_type,
self->descr,
newnd, dimensions,
strides, self->data,
self->flags,
(PyObject *)self);
if (ret == NULL) {
return NULL;
}
PyArray_FLAGS(ret) &= ~OWNDATA;
PyArray_BASE(ret) = (PyObject *)self;
Py_INCREF(self);
return (PyObject *)ret;
}
/*NUMPY_API
* Mean
*/
static PyObject *
PyArray_Mean(PyArrayObject *self, int axis, int rtype, PyArrayObject *out)
{
PyObject *obj1 = NULL, *obj2 = NULL;
PyObject *new, *ret;
if ((new = _check_axis(self, &axis, 0)) == NULL) {
return NULL;
}
obj1 = PyArray_GenericReduceFunction((PyAO *)new, n_ops.add, axis,
rtype, out);
obj2 = PyFloat_FromDouble((double) PyArray_DIM(new,axis));
Py_DECREF(new);
if (obj1 == NULL || obj2 == NULL) {
Py_XDECREF(obj1);
Py_XDECREF(obj2);
return NULL;
}
if (!out) {
ret = PyNumber_Divide(obj1, obj2);
}
else {
ret = PyObject_CallFunction(n_ops.divide, "OOO", out, obj2, out);
}
Py_DECREF(obj1);
Py_DECREF(obj2);
return ret;
}
/*NUMPY_API
* Set variance to 1 to by-pass square-root calculation and return variance
* Std
*/
static PyObject *
PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out,
int variance)
{
return __New_PyArray_Std(self, axis, rtype, out, variance, 0);
}
static PyObject *
__New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out,
int variance, int num)
{
PyObject *obj1 = NULL, *obj2 = NULL, *obj3 = NULL, *new = NULL;
PyObject *ret = NULL, *newshape = NULL;
int i, n;
intp val;
if ((new = _check_axis(self, &axis, 0)) == NULL) {
return NULL;
}
/* Compute and reshape mean */
obj1 = PyArray_EnsureAnyArray(PyArray_Mean((PyAO *)new, axis, rtype, NULL));
if (obj1 == NULL) {
Py_DECREF(new);
return NULL;
}
n = PyArray_NDIM(new);
newshape = PyTuple_New(n);
if (newshape == NULL) {
Py_DECREF(obj1);
Py_DECREF(new);
return NULL;
}
for (i = 0; i < n; i++) {
if (i == axis) {
val = 1;
}
else {
val = PyArray_DIM(new,i);
}
PyTuple_SET_ITEM(newshape, i, PyInt_FromLong((long)val));
}
obj2 = PyArray_Reshape((PyAO *)obj1, newshape);
Py_DECREF(obj1);
Py_DECREF(newshape);
if (obj2 == NULL) {