~jtaylor/ubuntu/precise/python-numpy/multiarch-fix-818867

« back to all changes in this revision

Viewing changes to numpy/core/src/arraymethods.c

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik, Riku Voipio, Tiziano Zito, Carlos Galisteo, Ondrej Certik
  • Date: 2008-07-08 15:08:16 UTC
  • mfrom: (0.1.21 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080708150816-ekf992jcp2k1eua3
Tags: 1:1.1.0-3
[ Riku Voipio ]
* debian/control: atlas is not available on armel, and after a quick look
  neither on alpha. I'd also suggest dropping
  libatlas-sse-dev|libatlas-sse2-dev|libatlas-3dnow-dev alternative combo
  away, these are potentially dangerous on buildd's. Ondrej: dropped.
  (Closes: #489568)

[ Tiziano Zito ]
* patch: build _dotblas.c when ATLAS is not installed, build-conflict with
  atlas, build-depend on blas+lapack only, as it used to be (Closes: #489726)

[ Carlos Galisteo ]
* debian/control
  - Added Homepage field.

[ Ondrej Certik ]
* Checked the package on i386 and amd64, both with and without atlas, all
  tests run and the numpy package is faster if atlas is around. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
/* Should only be used if x is known to be an nd-array */
3
2
#define _ARET(x) PyArray_Return((PyArrayObject *)(x))
4
3
 
5
4
static PyObject *
6
5
array_take(PyArrayObject *self, PyObject *args, PyObject *kwds)
7
6
{
8
 
        int dimension=MAX_DIMS;
9
 
        PyObject *indices;
10
 
        PyArrayObject *out=NULL;
11
 
        NPY_CLIPMODE mode=NPY_RAISE;
12
 
        static char *kwlist[] = {"indices", "axis", "out", "mode", NULL};
13
 
 
14
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&O&", kwlist,
15
 
                                         &indices, PyArray_AxisConverter,
16
 
                                         &dimension,
17
 
                                         PyArray_OutputConverter,
18
 
                                         &out,
19
 
                                         PyArray_ClipmodeConverter,
20
 
                                         &mode))
21
 
                return NULL;
22
 
 
23
 
        return _ARET(PyArray_TakeFrom(self, indices, dimension, out, mode));
 
7
    int dimension=MAX_DIMS;
 
8
    PyObject *indices;
 
9
    PyArrayObject *out=NULL;
 
10
    NPY_CLIPMODE mode=NPY_RAISE;
 
11
    static char *kwlist[] = {"indices", "axis", "out", "mode", NULL};
 
12
 
 
13
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&O&", kwlist,
 
14
                                     &indices, PyArray_AxisConverter,
 
15
                                     &dimension,
 
16
                                     PyArray_OutputConverter,
 
17
                                     &out,
 
18
                                     PyArray_ClipmodeConverter,
 
19
                                     &mode))
 
20
        return NULL;
 
21
 
 
22
    return _ARET(PyArray_TakeFrom(self, indices, dimension, out, mode));
24
23
}
25
24
 
26
25
static PyObject *
27
26
array_fill(PyArrayObject *self, PyObject *args)
28
27
{
29
 
        PyObject *obj;
30
 
        if (!PyArg_ParseTuple(args, "O", &obj))
31
 
                return NULL;
32
 
        if (PyArray_FillWithScalar(self, obj) < 0) return NULL;
33
 
        Py_INCREF(Py_None);
34
 
        return Py_None;
 
28
    PyObject *obj;
 
29
    if (!PyArg_ParseTuple(args, "O", &obj))
 
30
        return NULL;
 
31
    if (PyArray_FillWithScalar(self, obj) < 0) return NULL;
 
32
    Py_INCREF(Py_None);
 
33
    return Py_None;
35
34
}
36
35
 
37
36
static PyObject *
38
37
array_put(PyArrayObject *self, PyObject *args, PyObject *kwds)
39
38
{
40
 
        PyObject *indices, *values;
41
 
        NPY_CLIPMODE mode=NPY_RAISE;
42
 
        static char *kwlist[] = {"indices", "values", "mode", NULL};
 
39
    PyObject *indices, *values;
 
40
    NPY_CLIPMODE mode=NPY_RAISE;
 
41
    static char *kwlist[] = {"indices", "values", "mode", NULL};
43
42
 
44
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O&", kwlist,
45
 
                                         &indices, &values,
46
 
                                         PyArray_ClipmodeConverter,
47
 
                                         &mode))
48
 
                return NULL;
49
 
        return PyArray_PutTo(self, values, indices, mode);
 
43
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O&", kwlist,
 
44
                                     &indices, &values,
 
45
                                     PyArray_ClipmodeConverter,
 
46
                                     &mode))
 
47
        return NULL;
 
48
    return PyArray_PutTo(self, values, indices, mode);
50
49
}
51
50
 
52
51
static PyObject *
53
52
array_reshape(PyArrayObject *self, PyObject *args, PyObject *kwds)
54
53
{
55
 
        PyArray_Dims newshape;
56
 
        PyObject *ret;
57
 
        PyArray_ORDER order=PyArray_CORDER;
58
 
        int n;
59
 
 
60
 
        if (kwds != NULL) {
61
 
                PyObject *ref;
62
 
                ref = PyDict_GetItemString(kwds, "order");
63
 
                if (ref == NULL) {
64
 
                        PyErr_SetString(PyExc_TypeError, 
65
 
                                        "invalid keyword argument");
66
 
                        return NULL;
67
 
                }
68
 
                if ((PyArray_OrderConverter(ref, &order) == PY_FAIL))
69
 
                        return NULL;
70
 
        }
71
 
 
72
 
        n = PyTuple_Size(args);
73
 
        if (n <= 1) {
74
 
                if (PyTuple_GET_ITEM(args, 0) == Py_None)
75
 
                        return PyArray_View(self, NULL, NULL);
76
 
                if (!PyArg_ParseTuple(args, "O&", PyArray_IntpConverter,
77
 
                                      &newshape)) return NULL;
78
 
        }
79
 
        else {
80
 
                if (!PyArray_IntpConverter(args, &newshape)) {
81
 
                        if (!PyErr_Occurred()) {
82
 
                                PyErr_SetString(PyExc_TypeError,
83
 
                                                "invalid shape");
84
 
                        }
85
 
                        goto fail;
86
 
                }
87
 
        }
88
 
        ret = PyArray_Newshape(self, &newshape, order);
89
 
        PyDimMem_FREE(newshape.ptr);
90
 
        return ret;
 
54
    PyArray_Dims newshape;
 
55
    PyObject *ret;
 
56
    PyArray_ORDER order=PyArray_CORDER;
 
57
    int n;
 
58
 
 
59
    if (kwds != NULL) {
 
60
        PyObject *ref;
 
61
        ref = PyDict_GetItemString(kwds, "order");
 
62
        if (ref == NULL) {
 
63
            PyErr_SetString(PyExc_TypeError,
 
64
                            "invalid keyword argument");
 
65
            return NULL;
 
66
        }
 
67
        if ((PyArray_OrderConverter(ref, &order) == PY_FAIL))
 
68
            return NULL;
 
69
    }
 
70
 
 
71
    n = PyTuple_Size(args);
 
72
    if (n <= 1) {
 
73
        if (PyTuple_GET_ITEM(args, 0) == Py_None)
 
74
            return PyArray_View(self, NULL, NULL);
 
75
        if (!PyArg_ParseTuple(args, "O&", PyArray_IntpConverter,
 
76
                              &newshape)) return NULL;
 
77
    }
 
78
    else {
 
79
        if (!PyArray_IntpConverter(args, &newshape)) {
 
80
            if (!PyErr_Occurred()) {
 
81
                PyErr_SetString(PyExc_TypeError,
 
82
                                "invalid shape");
 
83
            }
 
84
            goto fail;
 
85
        }
 
86
    }
 
87
    ret = PyArray_Newshape(self, &newshape, order);
 
88
    PyDimMem_FREE(newshape.ptr);
 
89
    return ret;
91
90
 
92
91
 fail:
93
 
        PyDimMem_FREE(newshape.ptr);
94
 
        return NULL;
 
92
    PyDimMem_FREE(newshape.ptr);
 
93
    return NULL;
95
94
}
96
95
 
97
96
static PyObject *
98
97
array_squeeze(PyArrayObject *self, PyObject *args)
99
98
{
100
 
        if (!PyArg_ParseTuple(args, "")) return NULL;
101
 
        return PyArray_Squeeze(self);
 
99
    if (!PyArg_ParseTuple(args, "")) return NULL;
 
100
    return PyArray_Squeeze(self);
102
101
}
103
102
 
104
103
static PyObject *
105
 
array_view(PyArrayObject *self, PyObject *args)
 
104
array_view(PyArrayObject *self, PyObject *args, PyObject *kwds)
106
105
{
107
 
        PyObject *otype=NULL;
108
 
        PyArray_Descr *type=NULL;
109
 
 
110
 
        if (!PyArg_ParseTuple(args, "|O", &otype)) return NULL;
111
 
 
112
 
        if (otype) {
113
 
                if (PyType_Check(otype) &&                      \
114
 
                    PyType_IsSubtype((PyTypeObject *)otype,
115
 
                                     &PyArray_Type)) {
116
 
                        return PyArray_View(self, NULL,
117
 
                                            (PyTypeObject *)otype);
118
 
                }
119
 
                else {
120
 
                        if (PyArray_DescrConverter(otype, &type) == PY_FAIL)
121
 
                                return NULL;
122
 
                }
123
 
        }
124
 
        return PyArray_View(self, type, NULL);
 
106
    PyObject *out_dtype=NULL;
 
107
    PyObject *out_type=NULL;
 
108
    PyArray_Descr *dtype=NULL;
 
109
 
 
110
    static char *kwlist[] = {"dtype", "type", NULL};
 
111
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO", kwlist,
 
112
                                     &out_dtype,
 
113
                                     &out_type))
 
114
        return NULL;
 
115
 
 
116
    /* If user specified a positional argument, guess whether it
 
117
       represents a type or a dtype for backward compatibility. */
 
118
    if (out_dtype) {
 
119
        /* type specified? */
 
120
        if (PyType_Check(out_dtype) &&
 
121
            PyType_IsSubtype((PyTypeObject *)out_dtype,
 
122
                             &PyArray_Type)) {
 
123
            if (out_type) {
 
124
                PyErr_SetString(PyExc_ValueError,
 
125
                                "Cannot specify output type twice.");
 
126
                return NULL;
 
127
            }
 
128
            out_type = out_dtype;
 
129
            out_dtype = NULL;
 
130
        }
 
131
    }
 
132
 
 
133
    if ((out_type) && (!PyType_Check(out_type) ||
 
134
                       !PyType_IsSubtype((PyTypeObject *)out_type,
 
135
                                         &PyArray_Type))) {
 
136
        PyErr_SetString(PyExc_ValueError,
 
137
                        "Type must be a sub-type of ndarray type");
 
138
        return NULL;
 
139
    }
 
140
 
 
141
    if ((out_dtype) &&
 
142
        (PyArray_DescrConverter(out_dtype, &dtype) == PY_FAIL)) {
 
143
        PyErr_SetString(PyExc_ValueError,
 
144
                        "Dtype must be a numpy data-type");
 
145
        return NULL;
 
146
    }
 
147
 
 
148
    return PyArray_View(self, dtype, (PyTypeObject*)out_type);
125
149
}
126
150
 
127
151
static PyObject *
128
152
array_argmax(PyArrayObject *self, PyObject *args, PyObject *kwds)
129
153
{
130
 
        int axis=MAX_DIMS;
131
 
        PyArrayObject *out=NULL;
132
 
        static char *kwlist[] = {"axis", "out", NULL};
133
 
 
134
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
135
 
                                         PyArray_AxisConverter,
136
 
                                         &axis,
137
 
                                         PyArray_OutputConverter,
138
 
                                         &out))
139
 
                return NULL;
140
 
 
141
 
        return _ARET(PyArray_ArgMax(self, axis, out));
 
154
    int axis=MAX_DIMS;
 
155
    PyArrayObject *out=NULL;
 
156
    static char *kwlist[] = {"axis", "out", NULL};
 
157
 
 
158
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
159
                                     PyArray_AxisConverter,
 
160
                                     &axis,
 
161
                                     PyArray_OutputConverter,
 
162
                                     &out))
 
163
        return NULL;
 
164
 
 
165
    return _ARET(PyArray_ArgMax(self, axis, out));
142
166
}
143
167
 
144
168
static PyObject *
145
169
array_argmin(PyArrayObject *self, PyObject *args, PyObject *kwds)
146
170
{
147
 
        int axis=MAX_DIMS;
148
 
        PyArrayObject *out=NULL;
149
 
        static char *kwlist[] = {"axis", "out", NULL};
150
 
 
151
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
152
 
                                         PyArray_AxisConverter,
153
 
                                         &axis,
154
 
                                         PyArray_OutputConverter,
155
 
                                         &out))
156
 
                return NULL;
157
 
 
158
 
        return _ARET(PyArray_ArgMin(self, axis, out));
 
171
    int axis=MAX_DIMS;
 
172
    PyArrayObject *out=NULL;
 
173
    static char *kwlist[] = {"axis", "out", NULL};
 
174
 
 
175
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
176
                                     PyArray_AxisConverter,
 
177
                                     &axis,
 
178
                                     PyArray_OutputConverter,
 
179
                                     &out))
 
180
        return NULL;
 
181
 
 
182
    return _ARET(PyArray_ArgMin(self, axis, out));
159
183
}
160
184
 
161
185
static PyObject *
162
186
array_max(PyArrayObject *self, PyObject *args, PyObject *kwds)
163
187
{
164
 
        int axis=MAX_DIMS;
165
 
        PyArrayObject *out=NULL;
166
 
        static char *kwlist[] = {"axis", "out", NULL};
167
 
 
168
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
169
 
                                         PyArray_AxisConverter,
170
 
                                         &axis,
171
 
                                         PyArray_OutputConverter,
172
 
                                         &out))
173
 
                return NULL;
174
 
 
175
 
        return PyArray_Max(self, axis, out);
 
188
    int axis=MAX_DIMS;
 
189
    PyArrayObject *out=NULL;
 
190
    static char *kwlist[] = {"axis", "out", NULL};
 
191
 
 
192
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
193
                                     PyArray_AxisConverter,
 
194
                                     &axis,
 
195
                                     PyArray_OutputConverter,
 
196
                                     &out))
 
197
        return NULL;
 
198
 
 
199
    return PyArray_Max(self, axis, out);
176
200
}
177
201
 
178
202
static PyObject *
179
203
array_ptp(PyArrayObject *self, PyObject *args, PyObject *kwds)
180
204
{
181
 
        int axis=MAX_DIMS;
182
 
        PyArrayObject *out=NULL;
183
 
        static char *kwlist[] = {"axis", "out", NULL};
184
 
 
185
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
186
 
                                         PyArray_AxisConverter,
187
 
                                         &axis,
188
 
                                         PyArray_OutputConverter,
189
 
                                         &out))
190
 
                return NULL;
191
 
 
192
 
        return PyArray_Ptp(self, axis, out);
 
205
    int axis=MAX_DIMS;
 
206
    PyArrayObject *out=NULL;
 
207
    static char *kwlist[] = {"axis", "out", NULL};
 
208
 
 
209
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
210
                                     PyArray_AxisConverter,
 
211
                                     &axis,
 
212
                                     PyArray_OutputConverter,
 
213
                                     &out))
 
214
        return NULL;
 
215
 
 
216
    return PyArray_Ptp(self, axis, out);
193
217
}
194
218
 
195
219
 
196
220
static PyObject *
197
221
array_min(PyArrayObject *self, PyObject *args, PyObject *kwds)
198
222
{
199
 
        int axis=MAX_DIMS;
200
 
        PyArrayObject *out=NULL;
201
 
        static char *kwlist[] = {"axis", "out", NULL};
202
 
 
203
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
204
 
                                         PyArray_AxisConverter,
205
 
                                         &axis,
206
 
                                         PyArray_OutputConverter,
207
 
                                         &out))
208
 
                return NULL;
209
 
 
210
 
        return PyArray_Min(self, axis, out);
 
223
    int axis=MAX_DIMS;
 
224
    PyArrayObject *out=NULL;
 
225
    static char *kwlist[] = {"axis", "out", NULL};
 
226
 
 
227
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
228
                                     PyArray_AxisConverter,
 
229
                                     &axis,
 
230
                                     PyArray_OutputConverter,
 
231
                                     &out))
 
232
        return NULL;
 
233
 
 
234
    return PyArray_Min(self, axis, out);
211
235
}
212
236
 
213
237
static PyObject *
214
238
array_swapaxes(PyArrayObject *self, PyObject *args)
215
239
{
216
 
        int axis1, axis2;
217
 
 
218
 
        if (!PyArg_ParseTuple(args, "ii", &axis1, &axis2)) return NULL;
219
 
 
220
 
        return PyArray_SwapAxes(self, axis1, axis2);
 
240
    int axis1, axis2;
 
241
 
 
242
    if (!PyArg_ParseTuple(args, "ii", &axis1, &axis2)) return NULL;
 
243
 
 
244
    return PyArray_SwapAxes(self, axis1, axis2);
221
245
}
222
246
 
223
247
 
224
248
/* steals typed reference */
225
249
/*OBJECT_API
226
 
 Get a subset of bytes from each element of the array
 
250
  Get a subset of bytes from each element of the array
227
251
*/
228
252
static PyObject *
229
253
PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset)
230
254
{
231
 
        PyObject *ret=NULL;
232
 
 
233
 
        if (offset < 0 || (offset + typed->elsize) > self->descr->elsize) {
234
 
                PyErr_Format(PyExc_ValueError,
235
 
                             "Need 0 <= offset <= %d for requested type "  \
236
 
                             "but received offset = %d",
237
 
                             self->descr->elsize-typed->elsize, offset);
238
 
                Py_DECREF(typed);
239
 
                return NULL;
240
 
        }
241
 
        ret = PyArray_NewFromDescr(self->ob_type,
242
 
                                   typed,
243
 
                                   self->nd, self->dimensions,
244
 
                                   self->strides,
245
 
                                   self->data + offset,
246
 
                                   self->flags, (PyObject *)self);
247
 
        if (ret == NULL) return NULL;
248
 
        Py_INCREF(self);
249
 
        ((PyArrayObject *)ret)->base = (PyObject *)self;
250
 
 
251
 
        PyArray_UpdateFlags((PyArrayObject *)ret, UPDATE_ALL);
252
 
        return ret;
 
255
    PyObject *ret=NULL;
 
256
 
 
257
    if (offset < 0 || (offset + typed->elsize) > self->descr->elsize) {
 
258
        PyErr_Format(PyExc_ValueError,
 
259
                     "Need 0 <= offset <= %d for requested type "  \
 
260
                     "but received offset = %d",
 
261
                     self->descr->elsize-typed->elsize, offset);
 
262
        Py_DECREF(typed);
 
263
        return NULL;
 
264
    }
 
265
    ret = PyArray_NewFromDescr(self->ob_type,
 
266
                               typed,
 
267
                               self->nd, self->dimensions,
 
268
                               self->strides,
 
269
                               self->data + offset,
 
270
                               self->flags, (PyObject *)self);
 
271
    if (ret == NULL) return NULL;
 
272
    Py_INCREF(self);
 
273
    ((PyArrayObject *)ret)->base = (PyObject *)self;
 
274
 
 
275
    PyArray_UpdateFlags((PyArrayObject *)ret, UPDATE_ALL);
 
276
    return ret;
253
277
}
254
278
 
255
279
static PyObject *
256
280
array_getfield(PyArrayObject *self, PyObject *args, PyObject *kwds)
257
281
{
258
282
 
259
 
        PyArray_Descr *dtype;
260
 
        int offset = 0;
261
 
        static char *kwlist[] = {"dtype", "offset", 0};
262
 
 
263
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist,
264
 
                                         PyArray_DescrConverter,
265
 
                                         &dtype, &offset)) return NULL;
266
 
 
267
 
        return _ARET(PyArray_GetField(self, dtype, offset));
 
283
    PyArray_Descr *dtype=NULL;
 
284
    int offset = 0;
 
285
    static char *kwlist[] = {"dtype", "offset", 0};
 
286
 
 
287
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|i", kwlist,
 
288
                                     PyArray_DescrConverter,
 
289
                                     &dtype, &offset)) {
 
290
        Py_XDECREF(dtype);
 
291
        return NULL;
 
292
    }
 
293
 
 
294
    return PyArray_GetField(self, dtype, offset);
268
295
}
269
296
 
270
297
 
271
298
/*OBJECT_API
272
 
 Set a subset of bytes from each element of the array
 
299
  Set a subset of bytes from each element of the array
273
300
*/
274
301
static int
275
302
PyArray_SetField(PyArrayObject *self, PyArray_Descr *dtype,
276
 
                 int offset, PyObject *val)
 
303
                 int offset, PyObject *val)
277
304
{
278
 
        PyObject *ret=NULL;
279
 
        int retval = 0;
280
 
 
281
 
        if (offset < 0 || (offset + dtype->elsize) > self->descr->elsize) {
282
 
                PyErr_Format(PyExc_ValueError,
283
 
                             "Need 0 <= offset <= %d for requested type "  \
284
 
                             "but received offset = %d",
285
 
                             self->descr->elsize-dtype->elsize, offset);
286
 
                Py_DECREF(dtype);
287
 
                return -1;
288
 
        }
289
 
        ret = PyArray_NewFromDescr(self->ob_type,
290
 
                                   dtype, self->nd, self->dimensions,
291
 
                                   self->strides, self->data + offset,
292
 
                                   self->flags, (PyObject *)self);
293
 
        if (ret == NULL) return -1;
294
 
        Py_INCREF(self);
295
 
        ((PyArrayObject *)ret)->base = (PyObject *)self;
296
 
 
297
 
        PyArray_UpdateFlags((PyArrayObject *)ret, UPDATE_ALL);
298
 
        retval = PyArray_CopyObject((PyArrayObject *)ret, val);
299
 
        Py_DECREF(ret);
300
 
        return retval;
 
305
    PyObject *ret=NULL;
 
306
    int retval = 0;
 
307
 
 
308
    if (offset < 0 || (offset + dtype->elsize) > self->descr->elsize) {
 
309
        PyErr_Format(PyExc_ValueError,
 
310
                     "Need 0 <= offset <= %d for requested type "  \
 
311
                     "but received offset = %d",
 
312
                     self->descr->elsize-dtype->elsize, offset);
 
313
        Py_DECREF(dtype);
 
314
        return -1;
 
315
    }
 
316
    ret = PyArray_NewFromDescr(self->ob_type,
 
317
                               dtype, self->nd, self->dimensions,
 
318
                               self->strides, self->data + offset,
 
319
                               self->flags, (PyObject *)self);
 
320
    if (ret == NULL) return -1;
 
321
    Py_INCREF(self);
 
322
    ((PyArrayObject *)ret)->base = (PyObject *)self;
 
323
 
 
324
    PyArray_UpdateFlags((PyArrayObject *)ret, UPDATE_ALL);
 
325
    retval = PyArray_CopyObject((PyArrayObject *)ret, val);
 
326
    Py_DECREF(ret);
 
327
    return retval;
301
328
}
302
329
 
303
330
static PyObject *
304
331
array_setfield(PyArrayObject *self, PyObject *args, PyObject *kwds)
305
332
{
306
 
        PyArray_Descr *dtype;
307
 
        int offset = 0;
308
 
        PyObject *value;
309
 
        static char *kwlist[] = {"value", "dtype", "offset", 0};
310
 
 
311
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|i", kwlist,
312
 
                                         &value, PyArray_DescrConverter,
313
 
                                         &dtype, &offset)) return NULL;
314
 
 
315
 
        if (PyArray_SetField(self, dtype, offset, value) < 0)
316
 
                return NULL;
317
 
        Py_INCREF(Py_None);
318
 
        return Py_None;
 
333
    PyArray_Descr *dtype=NULL;
 
334
    int offset = 0;
 
335
    PyObject *value;
 
336
    static char *kwlist[] = {"value", "dtype", "offset", 0};
 
337
 
 
338
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO&|i", kwlist,
 
339
                                     &value, PyArray_DescrConverter,
 
340
                                     &dtype, &offset)) {
 
341
        Py_XDECREF(dtype);
 
342
        return NULL;
 
343
    }
 
344
 
 
345
    if (PyArray_SetField(self, dtype, offset, value) < 0)
 
346
        return NULL;
 
347
    Py_INCREF(Py_None);
 
348
    return Py_None;
319
349
}
320
350
 
321
351
/* This doesn't change the descriptor just the actual data...
325
355
static PyObject *
326
356
PyArray_Byteswap(PyArrayObject *self, Bool inplace)
327
357
{
328
 
        PyArrayObject *ret;
329
 
        intp size;
330
 
        PyArray_CopySwapNFunc *copyswapn;
331
 
        PyArrayIterObject *it;
332
 
 
333
 
        copyswapn = self->descr->f->copyswapn;
334
 
        if (inplace) {
335
 
                if (!PyArray_ISWRITEABLE(self)) {
336
 
                        PyErr_SetString(PyExc_RuntimeError,
337
 
                                        "Cannot byte-swap in-place on a " \
338
 
                                        "read-only array");
339
 
                        return NULL;
340
 
                }
341
 
                size = PyArray_SIZE(self);
342
 
                if (PyArray_ISONESEGMENT(self)) {
343
 
                        copyswapn(self->data, self->descr->elsize, NULL, -1, size, 1, self);
344
 
                }
345
 
                else { /* Use iterator */
346
 
                        int axis = -1;
347
 
                        intp stride;
348
 
                        it = (PyArrayIterObject *)                      \
349
 
                                PyArray_IterAllButAxis((PyObject *)self, &axis);
350
 
                        stride = self->strides[axis];
351
 
                        size = self->dimensions[axis];
352
 
                        while (it->index < it->size) {
353
 
                                copyswapn(it->dataptr, stride, NULL, -1, size, 1, self);
354
 
                                PyArray_ITER_NEXT(it);
355
 
                        }
356
 
                        Py_DECREF(it);
357
 
                }
358
 
 
359
 
                Py_INCREF(self);
360
 
                return (PyObject *)self;
361
 
        }
362
 
        else {
363
 
                PyObject *new;
364
 
                if ((ret = (PyArrayObject *)PyArray_NewCopy(self,-1)) == NULL)
365
 
                        return NULL;
366
 
                new = PyArray_Byteswap(ret, TRUE);
367
 
                Py_DECREF(new);
368
 
                return (PyObject *)ret;
369
 
        }
 
358
    PyArrayObject *ret;
 
359
    intp size;
 
360
    PyArray_CopySwapNFunc *copyswapn;
 
361
    PyArrayIterObject *it;
 
362
 
 
363
    copyswapn = self->descr->f->copyswapn;
 
364
    if (inplace) {
 
365
        if (!PyArray_ISWRITEABLE(self)) {
 
366
            PyErr_SetString(PyExc_RuntimeError,
 
367
                            "Cannot byte-swap in-place on a " \
 
368
                            "read-only array");
 
369
            return NULL;
 
370
        }
 
371
        size = PyArray_SIZE(self);
 
372
        if (PyArray_ISONESEGMENT(self)) {
 
373
            copyswapn(self->data, self->descr->elsize, NULL, -1, size, 1, self);
 
374
        }
 
375
        else { /* Use iterator */
 
376
            int axis = -1;
 
377
            intp stride;
 
378
            it = (PyArrayIterObject *)                      \
 
379
                PyArray_IterAllButAxis((PyObject *)self, &axis);
 
380
            stride = self->strides[axis];
 
381
            size = self->dimensions[axis];
 
382
            while (it->index < it->size) {
 
383
                copyswapn(it->dataptr, stride, NULL, -1, size, 1, self);
 
384
                PyArray_ITER_NEXT(it);
 
385
            }
 
386
            Py_DECREF(it);
 
387
        }
 
388
 
 
389
        Py_INCREF(self);
 
390
        return (PyObject *)self;
 
391
    }
 
392
    else {
 
393
        PyObject *new;
 
394
        if ((ret = (PyArrayObject *)PyArray_NewCopy(self,-1)) == NULL)
 
395
            return NULL;
 
396
        new = PyArray_Byteswap(ret, TRUE);
 
397
        Py_DECREF(new);
 
398
        return (PyObject *)ret;
 
399
    }
370
400
}
371
401
 
372
402
 
373
403
static PyObject *
374
404
array_byteswap(PyArrayObject *self, PyObject *args)
375
405
{
376
 
        Bool inplace=FALSE;
377
 
 
378
 
        if (!PyArg_ParseTuple(args, "|O&", PyArray_BoolConverter, &inplace))
379
 
                return NULL;
380
 
 
381
 
        return PyArray_Byteswap(self, inplace);
 
406
    Bool inplace=FALSE;
 
407
 
 
408
    if (!PyArg_ParseTuple(args, "|O&", PyArray_BoolConverter, &inplace))
 
409
        return NULL;
 
410
 
 
411
    return PyArray_Byteswap(self, inplace);
382
412
}
383
413
 
384
414
static PyObject *
385
415
array_tolist(PyArrayObject *self, PyObject *args)
386
416
{
387
 
        if (!PyArg_ParseTuple(args, "")) return NULL;
388
 
        return PyArray_ToList(self);
 
417
    if (!PyArg_ParseTuple(args, "")) return NULL;
 
418
    return PyArray_ToList(self);
389
419
}
390
420
 
391
421
 
392
422
static PyObject *
393
423
array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds)
394
424
{
395
 
        NPY_ORDER order=NPY_CORDER;
396
 
        static char *kwlist[] = {"order", NULL};
 
425
    NPY_ORDER order=NPY_CORDER;
 
426
    static char *kwlist[] = {"order", NULL};
397
427
 
398
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&", kwlist,
399
 
                                         PyArray_OrderConverter,
400
 
                                         &order)) return NULL;
401
 
        return PyArray_ToString(self, order);
 
428
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&", kwlist,
 
429
                                     PyArray_OrderConverter,
 
430
                                     &order)) return NULL;
 
431
    return PyArray_ToString(self, order);
402
432
}
403
433
 
404
434
 
408
438
static PyObject *
409
439
array_tofile(PyArrayObject *self, PyObject *args, PyObject *kwds)
410
440
{
411
 
        int ret;
412
 
        PyObject *file;
413
 
        FILE *fd;
414
 
        char *sep="";
415
 
        char *format="";
416
 
        static char *kwlist[] = {"file", "sep", "format", NULL};
417
 
 
418
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist,
419
 
                                         &file, &sep, &format)) return NULL;
420
 
 
421
 
        if (PyString_Check(file) || PyUnicode_Check(file)) {
422
 
                file = PyObject_CallFunction((PyObject *)&PyFile_Type,
423
 
                                             "Os", file, "wb");
424
 
                if (file==NULL) return NULL;
425
 
        }
426
 
        else {
427
 
                Py_INCREF(file);
428
 
        }
429
 
        fd = PyFile_AsFile(file);
430
 
        if (fd == NULL) {
431
 
                PyErr_SetString(PyExc_IOError, "first argument must be a " \
432
 
                                "string or open file");
433
 
                Py_DECREF(file);
434
 
                return NULL;
435
 
        }
436
 
        ret = PyArray_ToFile(self, fd, sep, format);
437
 
        Py_DECREF(file);
438
 
        if (ret < 0) return NULL;
439
 
        Py_INCREF(Py_None);
440
 
        return Py_None;
 
441
    int ret;
 
442
    PyObject *file;
 
443
    FILE *fd;
 
444
    char *sep="";
 
445
    char *format="";
 
446
    static char *kwlist[] = {"file", "sep", "format", NULL};
 
447
 
 
448
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist,
 
449
                                     &file, &sep, &format)) return NULL;
 
450
 
 
451
    if (PyString_Check(file) || PyUnicode_Check(file)) {
 
452
        file = PyObject_CallFunction((PyObject *)&PyFile_Type,
 
453
                                     "Os", file, "wb");
 
454
        if (file==NULL) return NULL;
 
455
    }
 
456
    else {
 
457
        Py_INCREF(file);
 
458
    }
 
459
    fd = PyFile_AsFile(file);
 
460
    if (fd == NULL) {
 
461
        PyErr_SetString(PyExc_IOError, "first argument must be a " \
 
462
                        "string or open file");
 
463
        Py_DECREF(file);
 
464
        return NULL;
 
465
    }
 
466
    ret = PyArray_ToFile(self, fd, sep, format);
 
467
    Py_DECREF(file);
 
468
    if (ret < 0) return NULL;
 
469
    Py_INCREF(Py_None);
 
470
    return Py_None;
441
471
}
442
472
 
443
473
 
444
474
static PyObject *
445
475
array_toscalar(PyArrayObject *self, PyObject *args) {
446
 
        int n, nd;
447
 
        n = PyTuple_GET_SIZE(args);
448
 
        
449
 
        if (n==1) {
450
 
                PyObject *obj;
451
 
                obj = PyTuple_GET_ITEM(args, 0);
452
 
                if (PyTuple_Check(obj)) {
453
 
                        args = obj;
454
 
                        n = PyTuple_GET_SIZE(args);
455
 
                }
456
 
        }
457
 
        
458
 
        if (n==0) {
459
 
                if (self->nd == 0 || PyArray_SIZE(self) == 1)
460
 
                        return self->descr->f->getitem(self->data, self);
461
 
                else {
462
 
                        PyErr_SetString(PyExc_ValueError, 
463
 
                                        "can only convert an array "    \
464
 
                                        " of size 1 to a Python scalar");
465
 
                        return NULL;
466
 
                }
467
 
        }
468
 
        else if (n != self->nd && (n > 1 || self->nd==0)) {
469
 
                PyErr_SetString(PyExc_ValueError, 
470
 
                                "incorrect number of indices for "      \
471
 
                                "array");
 
476
    int n, nd;
 
477
    n = PyTuple_GET_SIZE(args);
 
478
 
 
479
    if (n==1) {
 
480
        PyObject *obj;
 
481
        obj = PyTuple_GET_ITEM(args, 0);
 
482
        if (PyTuple_Check(obj)) {
 
483
            args = obj;
 
484
            n = PyTuple_GET_SIZE(args);
 
485
        }
 
486
    }
 
487
 
 
488
    if (n==0) {
 
489
        if (self->nd == 0 || PyArray_SIZE(self) == 1)
 
490
            return self->descr->f->getitem(self->data, self);
 
491
        else {
 
492
            PyErr_SetString(PyExc_ValueError,
 
493
                            "can only convert an array "    \
 
494
                            " of size 1 to a Python scalar");
 
495
            return NULL;
 
496
        }
 
497
    }
 
498
    else if (n != self->nd && (n > 1 || self->nd==0)) {
 
499
        PyErr_SetString(PyExc_ValueError,
 
500
                        "incorrect number of indices for "      \
 
501
                        "array");
 
502
        return NULL;
 
503
    }
 
504
    else if (n==1) { /* allows for flat getting as well as 1-d case */
 
505
        intp value, loc, index, factor;
 
506
        intp factors[MAX_DIMS];
 
507
        value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, 0));
 
508
        if (error_converting(value)) {
 
509
            PyErr_SetString(PyExc_ValueError, "invalid integer");
 
510
            return NULL;
 
511
        }
 
512
        factor = PyArray_SIZE(self);
 
513
        if (value < 0) value += factor;
 
514
        if ((value >= factor) || (value < 0)) {
 
515
            PyErr_SetString(PyExc_ValueError,
 
516
                            "index out of bounds");
 
517
            return NULL;
 
518
        }
 
519
        if (self->nd == 1) {
 
520
            value *= self->strides[0];
 
521
            return self->descr->f->getitem(self->data + value,
 
522
                                           self);
 
523
        }
 
524
        nd = self->nd;
 
525
        factor = 1;
 
526
        while (nd--) {
 
527
            factors[nd] = factor;
 
528
            factor *= self->dimensions[nd];
 
529
        }
 
530
        loc = 0;
 
531
        for (nd=0; nd < self->nd; nd++) {
 
532
            index = value / factors[nd];
 
533
            value = value % factors[nd];
 
534
            loc += self->strides[nd]*index;
 
535
        }
 
536
 
 
537
        return self->descr->f->getitem(self->data + loc,
 
538
                                       self);
 
539
 
 
540
    }
 
541
    else {
 
542
        intp loc, index[MAX_DIMS];
 
543
        nd = PyArray_IntpFromSequence(args, index, MAX_DIMS);
 
544
        if (nd < n) return NULL;
 
545
        loc = 0;
 
546
        while (nd--) {
 
547
            if (index[nd] < 0)
 
548
                index[nd] += self->dimensions[nd];
 
549
            if (index[nd] < 0 ||
 
550
                index[nd] >= self->dimensions[nd]) {
 
551
                PyErr_SetString(PyExc_ValueError,
 
552
                                "index out of bounds");
472
553
                return NULL;
473
 
        }
474
 
        else if (n==1) { /* allows for flat getting as well as 1-d case */
475
 
                intp value, loc, index, factor;
476
 
                intp factors[MAX_DIMS];
477
 
                value = PyArray_PyIntAsIntp(PyTuple_GET_ITEM(args, 0));
478
 
                if (error_converting(value)) {
479
 
                        PyErr_SetString(PyExc_ValueError, "invalid integer");
480
 
                        return NULL;
481
 
                }
482
 
                if (value >= PyArray_SIZE(self)) {
483
 
                        PyErr_SetString(PyExc_ValueError, 
484
 
                                        "index out of bounds");
485
 
                        return NULL;
486
 
                }
487
 
                if (self->nd == 1) {
488
 
                        value *= self->strides[0];
489
 
                        return self->descr->f->getitem(self->data + value,
490
 
                                                       self);
491
 
                }
492
 
                nd = self->nd;
493
 
                factor = 1;
494
 
                while (nd--) {
495
 
                        factors[nd] = factor;
496
 
                        factor *= self->dimensions[nd];
497
 
                }
498
 
                loc = 0;
499
 
                for (nd=0; nd < self->nd; nd++) {
500
 
                        index = value / factors[nd];
501
 
                        value = value % factors[nd];
502
 
                        loc += self->strides[nd]*index;
503
 
                }
504
 
                
505
 
                return self->descr->f->getitem(self->data + loc,
506
 
                                               self);
507
 
                
508
 
        }
509
 
        else {
510
 
                intp loc, index[MAX_DIMS];
511
 
                nd = PyArray_IntpFromSequence(args, index, MAX_DIMS);
512
 
                if (nd < n) return NULL;
513
 
                loc = 0;
514
 
                while (nd--) {
515
 
                        if (index[nd] < 0) 
516
 
                                index[nd] += self->dimensions[nd];
517
 
                        if (index[nd] < 0 || 
518
 
                            index[nd] >= self->dimensions[nd]) {
519
 
                                PyErr_SetString(PyExc_ValueError, 
520
 
                                                "index out of bounds");
521
 
                                return NULL;
522
 
                        }
523
 
                        loc += self->strides[nd]*index[nd];
524
 
                }
525
 
                return self->descr->f->getitem(self->data + loc, self);
526
 
        }
 
554
            }
 
555
            loc += self->strides[nd]*index[nd];
 
556
        }
 
557
        return self->descr->f->getitem(self->data + loc, self);
 
558
    }
527
559
}
528
560
 
529
561
static PyObject *
530
562
array_setscalar(PyArrayObject *self, PyObject *args) {
531
 
        int n, nd;
532
 
        int ret = -1;
533
 
        PyObject *obj;
534
 
        n = PyTuple_GET_SIZE(args)-1;
535
 
        
536
 
        if (n < 0) {
537
 
                PyErr_SetString(PyExc_ValueError, 
538
 
                                "itemset must have at least one argument");
539
 
                return NULL;
540
 
        }
541
 
        obj = PyTuple_GET_ITEM(args, n);
542
 
        if (n==0) {
543
 
                if (self->nd == 0 || PyArray_SIZE(self) == 1) {
544
 
                        ret = self->descr->f->setitem(obj, self->data, self);
545
 
                }                
546
 
                else {
547
 
                        PyErr_SetString(PyExc_ValueError, 
548
 
                                        "can only place a scalar for an "
549
 
                                        " array of size 1");
550
 
                        return NULL;
551
 
                }
552
 
        }
553
 
        else if (n != self->nd && (n > 1 || self->nd==0)) {
554
 
                PyErr_SetString(PyExc_ValueError, 
555
 
                                "incorrect number of indices for "      \
556
 
                                "array");
557
 
                return NULL;
558
 
        }
559
 
        else if (n==1) { /* allows for flat setting as well as 1-d case */
560
 
                intp value, loc, index, factor;
561
 
                intp factors[MAX_DIMS];
562
 
                PyObject *indobj;
 
563
    int n, nd;
 
564
    int ret = -1;
 
565
    PyObject *obj;
 
566
    n = PyTuple_GET_SIZE(args)-1;
563
567
 
564
 
                indobj = PyTuple_GET_ITEM(args, 0);
565
 
                if (PyTuple_Check(indobj)) {
566
 
                        PyObject *res;
567
 
                        PyObject *newargs;
568
 
                        PyObject *tmp;
569
 
                        int i, nn;
570
 
                        nn = PyTuple_GET_SIZE(indobj);
571
 
                        newargs = PyTuple_New(nn+1);
572
 
                        Py_INCREF(obj);
573
 
                        for (i=0; i<nn; i++) {
574
 
                                tmp = PyTuple_GET_ITEM(indobj, i);
575
 
                                Py_INCREF(tmp);
576
 
                                PyTuple_SET_ITEM(newargs, i, tmp);
577
 
                        }
578
 
                        PyTuple_SET_ITEM(newargs, nn, obj);
579
 
                        /* Call with a converted set of arguments */
580
 
                        res = array_setscalar(self, newargs);
581
 
                        Py_DECREF(newargs);
582
 
                        return res;
583
 
                }
584
 
                value = PyArray_PyIntAsIntp(indobj);
585
 
                if (error_converting(value)) {
586
 
                        PyErr_SetString(PyExc_ValueError, "invalid integer");
587
 
                        return NULL;
588
 
                }
589
 
                if (value >= PyArray_SIZE(self)) {
590
 
                        PyErr_SetString(PyExc_ValueError, 
591
 
                                        "index out of bounds");
592
 
                        return NULL;
593
 
                }
594
 
                if (self->nd == 1) {
595
 
                        value *= self->strides[0];
596
 
                        ret = self->descr->f->setitem(obj, self->data + value, 
597
 
                                                      self);
598
 
                        goto finish;
599
 
                }
600
 
                nd = self->nd;
601
 
                factor = 1;
602
 
                while (nd--) {
603
 
                        factors[nd] = factor;
604
 
                        factor *= self->dimensions[nd];
605
 
                }
606
 
                loc = 0;
607
 
                for (nd=0; nd < self->nd; nd++) {
608
 
                        index = value / factors[nd];
609
 
                        value = value % factors[nd];
610
 
                        loc += self->strides[nd]*index;
611
 
                }
612
 
                
613
 
                ret = self->descr->f->setitem(obj, self->data + loc, self);
 
568
    if (n < 0) {
 
569
        PyErr_SetString(PyExc_ValueError,
 
570
                        "itemset must have at least one argument");
 
571
        return NULL;
 
572
    }
 
573
    obj = PyTuple_GET_ITEM(args, n);
 
574
    if (n==0) {
 
575
        if (self->nd == 0 || PyArray_SIZE(self) == 1) {
 
576
            ret = self->descr->f->setitem(obj, self->data, self);
614
577
        }
615
578
        else {
616
 
                intp loc, index[MAX_DIMS];
617
 
                PyObject *tupargs;
618
 
                tupargs = PyTuple_GetSlice(args, 1, n+1);
619
 
                nd = PyArray_IntpFromSequence(tupargs, index, MAX_DIMS);
620
 
                Py_DECREF(tupargs);
621
 
                if (nd < n) return NULL;
622
 
                loc = 0;
623
 
                while (nd--) {
624
 
                        if (index[nd] < 0) 
625
 
                                index[nd] += self->dimensions[nd];
626
 
                        if (index[nd] < 0 || 
627
 
                            index[nd] >= self->dimensions[nd]) {
628
 
                                PyErr_SetString(PyExc_ValueError, 
629
 
                                                "index out of bounds");
630
 
                                return NULL;
631
 
                        }
632
 
                        loc += self->strides[nd]*index[nd];
633
 
                }
634
 
                ret = self->descr->f->setitem(obj, self->data + loc, self);
635
 
        }
 
579
            PyErr_SetString(PyExc_ValueError,
 
580
                            "can only place a scalar for an "
 
581
                            " array of size 1");
 
582
            return NULL;
 
583
        }
 
584
    }
 
585
    else if (n != self->nd && (n > 1 || self->nd==0)) {
 
586
        PyErr_SetString(PyExc_ValueError,
 
587
                        "incorrect number of indices for "      \
 
588
                        "array");
 
589
        return NULL;
 
590
    }
 
591
    else if (n==1) { /* allows for flat setting as well as 1-d case */
 
592
        intp value, loc, index, factor;
 
593
        intp factors[MAX_DIMS];
 
594
        PyObject *indobj;
 
595
 
 
596
        indobj = PyTuple_GET_ITEM(args, 0);
 
597
        if (PyTuple_Check(indobj)) {
 
598
            PyObject *res;
 
599
            PyObject *newargs;
 
600
            PyObject *tmp;
 
601
            int i, nn;
 
602
            nn = PyTuple_GET_SIZE(indobj);
 
603
            newargs = PyTuple_New(nn+1);
 
604
            Py_INCREF(obj);
 
605
            for (i=0; i<nn; i++) {
 
606
                tmp = PyTuple_GET_ITEM(indobj, i);
 
607
                Py_INCREF(tmp);
 
608
                PyTuple_SET_ITEM(newargs, i, tmp);
 
609
            }
 
610
            PyTuple_SET_ITEM(newargs, nn, obj);
 
611
            /* Call with a converted set of arguments */
 
612
            res = array_setscalar(self, newargs);
 
613
            Py_DECREF(newargs);
 
614
            return res;
 
615
        }
 
616
        value = PyArray_PyIntAsIntp(indobj);
 
617
        if (error_converting(value)) {
 
618
            PyErr_SetString(PyExc_ValueError, "invalid integer");
 
619
            return NULL;
 
620
        }
 
621
        if (value >= PyArray_SIZE(self)) {
 
622
            PyErr_SetString(PyExc_ValueError,
 
623
                            "index out of bounds");
 
624
            return NULL;
 
625
        }
 
626
        if (self->nd == 1) {
 
627
            value *= self->strides[0];
 
628
            ret = self->descr->f->setitem(obj, self->data + value,
 
629
                                          self);
 
630
            goto finish;
 
631
        }
 
632
        nd = self->nd;
 
633
        factor = 1;
 
634
        while (nd--) {
 
635
            factors[nd] = factor;
 
636
            factor *= self->dimensions[nd];
 
637
        }
 
638
        loc = 0;
 
639
        for (nd=0; nd < self->nd; nd++) {
 
640
            index = value / factors[nd];
 
641
            value = value % factors[nd];
 
642
            loc += self->strides[nd]*index;
 
643
        }
 
644
 
 
645
        ret = self->descr->f->setitem(obj, self->data + loc, self);
 
646
    }
 
647
    else {
 
648
        intp loc, index[MAX_DIMS];
 
649
        PyObject *tupargs;
 
650
        tupargs = PyTuple_GetSlice(args, 0, n);
 
651
        nd = PyArray_IntpFromSequence(tupargs, index, MAX_DIMS);
 
652
        Py_DECREF(tupargs);
 
653
        if (nd < n) return NULL;
 
654
        loc = 0;
 
655
        while (nd--) {
 
656
            if (index[nd] < 0)
 
657
                index[nd] += self->dimensions[nd];
 
658
            if (index[nd] < 0 ||
 
659
                index[nd] >= self->dimensions[nd]) {
 
660
                PyErr_SetString(PyExc_ValueError,
 
661
                                "index out of bounds");
 
662
                return NULL;
 
663
            }
 
664
            loc += self->strides[nd]*index[nd];
 
665
        }
 
666
        ret = self->descr->f->setitem(obj, self->data + loc, self);
 
667
    }
636
668
 
637
669
 finish:
638
 
        if (ret < 0) return NULL;
639
 
        Py_INCREF(Py_None);
640
 
        return Py_None;
 
670
    if (ret < 0) return NULL;
 
671
    Py_INCREF(Py_None);
 
672
    return Py_None;
641
673
}
642
674
 
643
675
 
644
676
static PyObject *
645
677
array_cast(PyArrayObject *self, PyObject *args)
646
678
{
647
 
        PyArray_Descr *descr=NULL;
648
 
        PyObject *obj;
649
 
 
650
 
        if (!PyArg_ParseTuple(args, "O&", PyArray_DescrConverter,
651
 
                              &descr)) return NULL;
652
 
 
653
 
        if (descr == self->descr) {
654
 
                obj = _ARET(PyArray_NewCopy(self,0));
655
 
                Py_XDECREF(descr);
656
 
                return obj;
657
 
        }
658
 
        if (descr->names != NULL) {
659
 
                return PyArray_FromArray(self, descr, NPY_FORCECAST);
660
 
        }
661
 
        return PyArray_CastToType(self, descr, 0);
 
679
    PyArray_Descr *descr=NULL;
 
680
    PyObject *obj;
 
681
 
 
682
    if (!PyArg_ParseTuple(args, "O&", PyArray_DescrConverter,
 
683
                          &descr)) {
 
684
        Py_XDECREF(descr);
 
685
        return NULL;
 
686
    }
 
687
 
 
688
    if (PyArray_EquivTypes(descr, self->descr)) {
 
689
        obj = _ARET(PyArray_NewCopy(self,NPY_ANYORDER));
 
690
        Py_XDECREF(descr);
 
691
        return obj;
 
692
    }
 
693
    if (descr->names != NULL) {
 
694
        int flags;
 
695
        flags = NPY_FORCECAST;
 
696
        if (PyArray_ISFORTRAN(self)) {
 
697
            flags |= NPY_FORTRAN;
 
698
        }
 
699
        return PyArray_FromArray(self, descr, flags);
 
700
    }
 
701
    return PyArray_CastToType(self, descr, PyArray_ISFORTRAN(self));
662
702
}
663
703
 
664
704
/* default sub-type implementation */
667
707
static PyObject *
668
708
array_wraparray(PyArrayObject *self, PyObject *args)
669
709
{
670
 
        PyObject *arr;
671
 
        PyObject *ret;
672
 
 
673
 
        if (PyTuple_Size(args) < 1) {
674
 
                PyErr_SetString(PyExc_TypeError,
675
 
                                "only accepts 1 argument");
676
 
                return NULL;
677
 
        }
678
 
        arr = PyTuple_GET_ITEM(args, 0);
679
 
        if (!PyArray_Check(arr)) {
680
 
                PyErr_SetString(PyExc_TypeError,
681
 
                                "can only be called with ndarray object");
682
 
                return NULL;
683
 
        }
684
 
 
685
 
        Py_INCREF(PyArray_DESCR(arr));
686
 
        ret = PyArray_NewFromDescr(self->ob_type,
687
 
                                   PyArray_DESCR(arr),
688
 
                                   PyArray_NDIM(arr),
689
 
                                   PyArray_DIMS(arr),
690
 
                                   PyArray_STRIDES(arr), PyArray_DATA(arr),
691
 
                                   PyArray_FLAGS(arr), (PyObject *)self);
692
 
        if (ret == NULL) return NULL;
693
 
        Py_INCREF(arr);
694
 
        PyArray_BASE(ret) = arr;
695
 
        return ret;
 
710
    PyObject *arr;
 
711
    PyObject *ret;
 
712
 
 
713
    if (PyTuple_Size(args) < 1) {
 
714
        PyErr_SetString(PyExc_TypeError,
 
715
                        "only accepts 1 argument");
 
716
        return NULL;
 
717
    }
 
718
    arr = PyTuple_GET_ITEM(args, 0);
 
719
    if (!PyArray_Check(arr)) {
 
720
        PyErr_SetString(PyExc_TypeError,
 
721
                        "can only be called with ndarray object");
 
722
        return NULL;
 
723
    }
 
724
 
 
725
    Py_INCREF(PyArray_DESCR(arr));
 
726
    ret = PyArray_NewFromDescr(self->ob_type,
 
727
                               PyArray_DESCR(arr),
 
728
                               PyArray_NDIM(arr),
 
729
                               PyArray_DIMS(arr),
 
730
                               PyArray_STRIDES(arr), PyArray_DATA(arr),
 
731
                               PyArray_FLAGS(arr), (PyObject *)self);
 
732
    if (ret == NULL) return NULL;
 
733
    Py_INCREF(arr);
 
734
    PyArray_BASE(ret) = arr;
 
735
    return ret;
696
736
}
697
737
 
698
738
 
699
739
static PyObject *
700
740
array_getarray(PyArrayObject *self, PyObject *args)
701
741
{
702
 
        PyArray_Descr *newtype=NULL;
703
 
        PyObject *ret;
704
 
 
705
 
        if (!PyArg_ParseTuple(args, "|O&", PyArray_DescrConverter,
706
 
                              &newtype)) return NULL;
707
 
 
708
 
        /* convert to PyArray_Type */
709
 
        if (!PyArray_CheckExact(self)) {
710
 
                PyObject *new;
711
 
                PyTypeObject *subtype = &PyArray_Type;
712
 
 
713
 
                if (!PyType_IsSubtype(self->ob_type, &PyArray_Type)) {
714
 
                        subtype = &PyArray_Type;
715
 
                }
716
 
 
717
 
                Py_INCREF(PyArray_DESCR(self));
718
 
                new = PyArray_NewFromDescr(subtype,
719
 
                                           PyArray_DESCR(self),
720
 
                                           PyArray_NDIM(self),
721
 
                                           PyArray_DIMS(self),
722
 
                                           PyArray_STRIDES(self),
723
 
                                           PyArray_DATA(self),
724
 
                                           PyArray_FLAGS(self), NULL);
725
 
                if (new == NULL) return NULL;
726
 
                Py_INCREF(self);
727
 
                PyArray_BASE(new) = (PyObject *)self;
728
 
                self = (PyArrayObject *)new;
729
 
        }
730
 
        else {
731
 
                Py_INCREF(self);
732
 
        }
733
 
 
734
 
        if ((newtype == NULL) || \
735
 
            PyArray_EquivTypes(self->descr, newtype)) {
736
 
                return (PyObject *)self;
737
 
        }
738
 
        else {
739
 
                ret = PyArray_CastToType(self, newtype, 0);
740
 
                Py_DECREF(self);
741
 
                return ret;
742
 
        }
 
742
    PyArray_Descr *newtype=NULL;
 
743
    PyObject *ret;
 
744
 
 
745
    if (!PyArg_ParseTuple(args, "|O&", PyArray_DescrConverter,
 
746
                          &newtype)) {
 
747
        Py_XDECREF(newtype);
 
748
        return NULL;
 
749
    }
 
750
 
 
751
    /* convert to PyArray_Type */
 
752
    if (!PyArray_CheckExact(self)) {
 
753
        PyObject *new;
 
754
        PyTypeObject *subtype = &PyArray_Type;
 
755
 
 
756
        if (!PyType_IsSubtype(self->ob_type, &PyArray_Type)) {
 
757
            subtype = &PyArray_Type;
 
758
        }
 
759
 
 
760
        Py_INCREF(PyArray_DESCR(self));
 
761
        new = PyArray_NewFromDescr(subtype,
 
762
                                   PyArray_DESCR(self),
 
763
                                   PyArray_NDIM(self),
 
764
                                   PyArray_DIMS(self),
 
765
                                   PyArray_STRIDES(self),
 
766
                                   PyArray_DATA(self),
 
767
                                   PyArray_FLAGS(self), NULL);
 
768
        if (new == NULL) return NULL;
 
769
        Py_INCREF(self);
 
770
        PyArray_BASE(new) = (PyObject *)self;
 
771
        self = (PyArrayObject *)new;
 
772
    }
 
773
    else {
 
774
        Py_INCREF(self);
 
775
    }
 
776
 
 
777
    if ((newtype == NULL) || \
 
778
        PyArray_EquivTypes(self->descr, newtype)) {
 
779
        return (PyObject *)self;
 
780
    }
 
781
    else {
 
782
        ret = PyArray_CastToType(self, newtype, 0);
 
783
        Py_DECREF(self);
 
784
        return ret;
 
785
    }
743
786
}
744
787
 
745
788
 
746
789
static PyObject *
747
790
array_copy(PyArrayObject *self, PyObject *args)
748
791
{
749
 
        PyArray_ORDER fortran=PyArray_CORDER;
750
 
        if (!PyArg_ParseTuple(args, "|O&", PyArray_OrderConverter,
751
 
                              &fortran)) return NULL;
 
792
    PyArray_ORDER fortran=PyArray_CORDER;
 
793
    if (!PyArg_ParseTuple(args, "|O&", PyArray_OrderConverter,
 
794
                          &fortran)) return NULL;
752
795
 
753
 
        return PyArray_NewCopy(self, fortran);
 
796
    return PyArray_NewCopy(self, fortran);
754
797
}
755
798
 
756
799
 
757
800
static PyObject *
758
801
array_resize(PyArrayObject *self, PyObject *args, PyObject *kwds)
759
802
{
760
 
        PyArray_Dims newshape;
761
 
        PyObject *ret;
762
 
        int n;
763
 
        int refcheck = 1;
764
 
        PyArray_ORDER fortran=PyArray_ANYORDER;
 
803
    PyArray_Dims newshape;
 
804
    PyObject *ret;
 
805
    int n;
 
806
    int refcheck = 1;
 
807
    PyArray_ORDER fortran=PyArray_ANYORDER;
765
808
 
766
 
        if (kwds != NULL) {
767
 
                PyObject *ref;
768
 
                ref = PyDict_GetItemString(kwds, "refcheck");
769
 
                if (ref) {
770
 
                        refcheck = PyInt_AsLong(ref);
771
 
                        if (refcheck==-1 && PyErr_Occurred()) {
772
 
                                return NULL;
773
 
                        }
774
 
                }
775
 
                ref = PyDict_GetItemString(kwds, "order");
776
 
                if (ref != NULL ||
777
 
                    (PyArray_OrderConverter(ref, &fortran) == PY_FAIL))
778
 
                        return NULL;
779
 
        }
780
 
        n = PyTuple_Size(args);
781
 
        if (n <= 1) {
782
 
                if (PyTuple_GET_ITEM(args, 0) == Py_None) {
783
 
                        Py_INCREF(Py_None);
784
 
                        return Py_None;
785
 
                }
786
 
                if (!PyArg_ParseTuple(args, "O&", PyArray_IntpConverter,
787
 
                                      &newshape)) return NULL;
788
 
        }
789
 
        else {
790
 
                if (!PyArray_IntpConverter(args, &newshape)) {
791
 
                        if (!PyErr_Occurred()) {
792
 
                                PyErr_SetString(PyExc_TypeError,
793
 
                                                "invalid shape");
794
 
                        }
795
 
                        return NULL;
796
 
                }
797
 
        }
798
 
        ret = PyArray_Resize(self, &newshape, refcheck, fortran);
799
 
        PyDimMem_FREE(newshape.ptr);
800
 
        if (ret == NULL) return NULL;
801
 
        Py_DECREF(ret);
802
 
        Py_INCREF(Py_None);
803
 
        return Py_None;
 
809
    if (kwds != NULL) {
 
810
        PyObject *ref;
 
811
        ref = PyDict_GetItemString(kwds, "refcheck");
 
812
        if (ref) {
 
813
            refcheck = PyInt_AsLong(ref);
 
814
            if (refcheck==-1 && PyErr_Occurred()) {
 
815
                return NULL;
 
816
            }
 
817
        }
 
818
        ref = PyDict_GetItemString(kwds, "order");
 
819
        if (ref != NULL ||
 
820
            (PyArray_OrderConverter(ref, &fortran) == PY_FAIL))
 
821
            return NULL;
 
822
    }
 
823
    n = PyTuple_Size(args);
 
824
    if (n <= 1) {
 
825
        if (PyTuple_GET_ITEM(args, 0) == Py_None) {
 
826
            Py_INCREF(Py_None);
 
827
            return Py_None;
 
828
        }
 
829
        if (!PyArg_ParseTuple(args, "O&", PyArray_IntpConverter,
 
830
                              &newshape)) return NULL;
 
831
    }
 
832
    else {
 
833
        if (!PyArray_IntpConverter(args, &newshape)) {
 
834
            if (!PyErr_Occurred()) {
 
835
                PyErr_SetString(PyExc_TypeError,
 
836
                                "invalid shape");
 
837
            }
 
838
            return NULL;
 
839
        }
 
840
    }
 
841
    ret = PyArray_Resize(self, &newshape, refcheck, fortran);
 
842
    PyDimMem_FREE(newshape.ptr);
 
843
    if (ret == NULL) return NULL;
 
844
    Py_DECREF(ret);
 
845
    Py_INCREF(Py_None);
 
846
    return Py_None;
804
847
}
805
848
 
806
849
static PyObject *
807
850
array_repeat(PyArrayObject *self, PyObject *args, PyObject *kwds) {
808
 
        PyObject *repeats;
809
 
        int axis=MAX_DIMS;
810
 
        static char *kwlist[] = {"repeats", "axis", NULL};
811
 
 
812
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&", kwlist,
813
 
                                         &repeats, PyArray_AxisConverter,
814
 
                                         &axis)) return NULL;
815
 
 
816
 
        return _ARET(PyArray_Repeat(self, repeats, axis));
 
851
    PyObject *repeats;
 
852
    int axis=MAX_DIMS;
 
853
    static char *kwlist[] = {"repeats", "axis", NULL};
 
854
 
 
855
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&", kwlist,
 
856
                                     &repeats, PyArray_AxisConverter,
 
857
                                     &axis)) return NULL;
 
858
 
 
859
    return _ARET(PyArray_Repeat(self, repeats, axis));
817
860
}
818
861
 
819
862
static PyObject *
820
863
array_choose(PyArrayObject *self, PyObject *args, PyObject *kwds)
821
864
{
822
 
        PyObject *choices;
823
 
        int n;
824
 
        PyArrayObject *out=NULL;
825
 
        NPY_CLIPMODE clipmode=NPY_RAISE;
826
 
 
827
 
        n = PyTuple_Size(args);
828
 
        if (n <= 1) {
829
 
                if (!PyArg_ParseTuple(args, "O", &choices))
830
 
                        return NULL;
831
 
        }
832
 
        else {
833
 
                choices = args;
834
 
        }
835
 
        if (kwds && PyDict_Check(kwds)) {
836
 
                if (PyArray_OutputConverter(PyDict_GetItemString(kwds,
837
 
                                                                 "out"),
838
 
                                            &out) == PY_FAIL)
839
 
                        return NULL;
840
 
                if (PyArray_ClipmodeConverter(PyDict_GetItemString(kwds,
841
 
                                                                   "mode"),
842
 
                                              &clipmode) == PY_FAIL)
843
 
                        return NULL;
844
 
        }
845
 
 
846
 
        return _ARET(PyArray_Choose(self, choices, out, clipmode));
 
865
    PyObject *choices;
 
866
    int n;
 
867
    PyArrayObject *out=NULL;
 
868
    NPY_CLIPMODE clipmode=NPY_RAISE;
 
869
 
 
870
    n = PyTuple_Size(args);
 
871
    if (n <= 1) {
 
872
        if (!PyArg_ParseTuple(args, "O", &choices))
 
873
            return NULL;
 
874
    }
 
875
    else {
 
876
        choices = args;
 
877
    }
 
878
    if (kwds && PyDict_Check(kwds)) {
 
879
        if (PyArray_OutputConverter(PyDict_GetItemString(kwds,
 
880
                                                         "out"),
 
881
                                    &out) == PY_FAIL)
 
882
            return NULL;
 
883
        if (PyArray_ClipmodeConverter(PyDict_GetItemString(kwds,
 
884
                                                           "mode"),
 
885
                                      &clipmode) == PY_FAIL)
 
886
            return NULL;
 
887
    }
 
888
 
 
889
    return _ARET(PyArray_Choose(self, choices, out, clipmode));
847
890
}
848
891
 
849
892
static PyObject *
850
893
array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds)
851
894
{
852
 
        int axis=-1;
853
 
        int val;
854
 
        PyArray_SORTKIND which=PyArray_QUICKSORT;
855
 
        PyObject *order=NULL;
856
 
        PyArray_Descr *saved=NULL;
857
 
        PyArray_Descr *newd;
858
 
        static char *kwlist[] = {"axis", "kind", "order", NULL};
859
 
        
860
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&O", kwlist, &axis,
861
 
                                         PyArray_SortkindConverter, &which,
862
 
                                         &order))
863
 
                return NULL;
864
 
 
865
 
        if (order == Py_None) order = NULL;
866
 
        if (order != NULL) {
867
 
                PyObject *new_name;
868
 
                saved = self->descr;
869
 
                if (saved->names == NULL) {
870
 
                        PyErr_SetString(PyExc_ValueError, "Cannot specify " \
871
 
                                        "order when the array has no fields.");
872
 
                        return NULL;
873
 
                }
874
 
                new_name = PyObject_CallMethod(_numpy_internal, "_newnames",
875
 
                                               "OO", saved, order);
876
 
                if (new_name == NULL) return NULL;
877
 
                newd = PyArray_DescrNew(saved);
878
 
                newd->names = new_name;
879
 
                self->descr = newd;
880
 
        }
881
 
 
882
 
        val = PyArray_Sort(self, axis, which);
883
 
        if (order != NULL) {
884
 
                Py_XDECREF(self->descr);
885
 
                self->descr = saved;
886
 
        }
887
 
        if (val < 0) return NULL;
888
 
        Py_INCREF(Py_None);
889
 
        return Py_None;
 
895
    int axis=-1;
 
896
    int val;
 
897
    PyArray_SORTKIND which=PyArray_QUICKSORT;
 
898
    PyObject *order=NULL;
 
899
    PyArray_Descr *saved=NULL;
 
900
    PyArray_Descr *newd;
 
901
    static char *kwlist[] = {"axis", "kind", "order", NULL};
 
902
 
 
903
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&O", kwlist, &axis,
 
904
                                     PyArray_SortkindConverter, &which,
 
905
                                     &order))
 
906
        return NULL;
 
907
 
 
908
    if (order == Py_None) order = NULL;
 
909
    if (order != NULL) {
 
910
        PyObject *new_name;
 
911
        PyObject *_numpy_internal;
 
912
        saved = self->descr;
 
913
        if (saved->names == NULL) {
 
914
            PyErr_SetString(PyExc_ValueError, "Cannot specify " \
 
915
                            "order when the array has no fields.");
 
916
            return NULL;
 
917
        }
 
918
        _numpy_internal = PyImport_ImportModule("numpy.core._internal");
 
919
        if (_numpy_internal == NULL) return NULL;
 
920
        new_name = PyObject_CallMethod(_numpy_internal, "_newnames",
 
921
                                       "OO", saved, order);
 
922
        Py_DECREF(_numpy_internal);
 
923
        if (new_name == NULL) return NULL;
 
924
        newd = PyArray_DescrNew(saved);
 
925
        newd->names = new_name;
 
926
        self->descr = newd;
 
927
    }
 
928
 
 
929
    val = PyArray_Sort(self, axis, which);
 
930
    if (order != NULL) {
 
931
        Py_XDECREF(self->descr);
 
932
        self->descr = saved;
 
933
    }
 
934
    if (val < 0) return NULL;
 
935
    Py_INCREF(Py_None);
 
936
    return Py_None;
890
937
}
891
938
 
892
939
static PyObject *
893
940
array_argsort(PyArrayObject *self, PyObject *args, PyObject *kwds)
894
941
{
895
 
        int axis=-1;
896
 
        PyArray_SORTKIND which=PyArray_QUICKSORT;
897
 
        PyObject *order=NULL, *res;
898
 
        PyArray_Descr *newd, *saved=NULL;
899
 
        static char *kwlist[] = {"axis", "kind", "order", NULL};
900
 
 
901
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O", kwlist, 
902
 
                                         PyArray_AxisConverter, &axis,
903
 
                                         PyArray_SortkindConverter, &which,
904
 
                                         &order))
905
 
                return NULL;
906
 
 
907
 
        if (order == Py_None) order = NULL;
908
 
        if (order != NULL) {
909
 
                PyObject *new_name;
910
 
                saved = self->descr;
911
 
                if (saved->names == NULL) {
912
 
                        PyErr_SetString(PyExc_ValueError, "Cannot specify " \
913
 
                                        "order when the array has no fields.");
914
 
                        return NULL;
915
 
                }
916
 
                new_name = PyObject_CallMethod(_numpy_internal, "_newnames",
917
 
                                               "OO", saved, order);
918
 
                if (new_name == NULL) return NULL;
919
 
                newd = PyArray_DescrNew(saved);
920
 
                newd->names = new_name;
921
 
                self->descr = newd;
922
 
        }
923
 
 
924
 
        res = PyArray_ArgSort(self, axis, which);
925
 
        if (order != NULL) {
926
 
                Py_XDECREF(self->descr);
927
 
                self->descr = saved;
928
 
        }
929
 
        return _ARET(res);
 
942
    int axis=-1;
 
943
    PyArray_SORTKIND which=PyArray_QUICKSORT;
 
944
    PyObject *order=NULL, *res;
 
945
    PyArray_Descr *newd, *saved=NULL;
 
946
    static char *kwlist[] = {"axis", "kind", "order", NULL};
 
947
 
 
948
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O", kwlist,
 
949
                                     PyArray_AxisConverter, &axis,
 
950
                                     PyArray_SortkindConverter, &which,
 
951
                                     &order))
 
952
        return NULL;
 
953
 
 
954
    if (order == Py_None) order = NULL;
 
955
    if (order != NULL) {
 
956
        PyObject *new_name;
 
957
        PyObject *_numpy_internal;
 
958
        saved = self->descr;
 
959
        if (saved->names == NULL) {
 
960
            PyErr_SetString(PyExc_ValueError, "Cannot specify " \
 
961
                            "order when the array has no fields.");
 
962
            return NULL;
 
963
        }
 
964
        _numpy_internal = PyImport_ImportModule("numpy.core._internal");
 
965
        if (_numpy_internal == NULL) return NULL;
 
966
        new_name = PyObject_CallMethod(_numpy_internal, "_newnames",
 
967
                                       "OO", saved, order);
 
968
        Py_DECREF(_numpy_internal);
 
969
        if (new_name == NULL) return NULL;
 
970
        newd = PyArray_DescrNew(saved);
 
971
        newd->names = new_name;
 
972
        self->descr = newd;
 
973
    }
 
974
 
 
975
    res = PyArray_ArgSort(self, axis, which);
 
976
    if (order != NULL) {
 
977
        Py_XDECREF(self->descr);
 
978
        self->descr = saved;
 
979
    }
 
980
    return _ARET(res);
930
981
}
931
982
 
932
983
static PyObject *
933
984
array_searchsorted(PyArrayObject *self, PyObject *args, PyObject *kwds)
934
985
{
935
 
        static char *kwlist[] = {"keys", "side", NULL};
936
 
        PyObject *keys;
937
 
        NPY_SEARCHSIDE side = NPY_SEARCHLEFT;
938
 
 
939
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&:searchsorted",
940
 
                                         kwlist, &keys,
941
 
                                         PyArray_SearchsideConverter, &side))
942
 
                return NULL;
943
 
 
944
 
        return _ARET(PyArray_SearchSorted(self, keys, side));
 
986
    static char *kwlist[] = {"keys", "side", NULL};
 
987
    PyObject *keys;
 
988
    NPY_SEARCHSIDE side = NPY_SEARCHLEFT;
 
989
 
 
990
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&:searchsorted",
 
991
                                     kwlist, &keys,
 
992
                                     PyArray_SearchsideConverter, &side))
 
993
        return NULL;
 
994
 
 
995
    return _ARET(PyArray_SearchSorted(self, keys, side));
945
996
}
946
997
 
947
998
static void
948
999
_deepcopy_call(char *iptr, char *optr, PyArray_Descr *dtype,
949
 
               PyObject *deepcopy, PyObject *visit)
 
1000
               PyObject *deepcopy, PyObject *visit)
950
1001
{
951
 
        if (!PyDataType_REFCHK(dtype)) return;
952
 
        else if (PyDescr_HASFIELDS(dtype)) {
953
 
                PyObject *key, *value, *title=NULL;
954
 
                PyArray_Descr *new;
955
 
                int offset;
956
 
                Py_ssize_t pos=0;
957
 
                while (PyDict_Next(dtype->fields, &pos, &key, &value)) {
958
 
                        if (!PyArg_ParseTuple(value, "Oi|O", &new, &offset,
959
 
                                              &title)) return;
960
 
                        _deepcopy_call(iptr + offset, optr + offset, new,
961
 
                                       deepcopy, visit);
962
 
                }
 
1002
    if (!PyDataType_REFCHK(dtype)) return;
 
1003
    else if (PyDescr_HASFIELDS(dtype)) {
 
1004
        PyObject *key, *value, *title=NULL;
 
1005
        PyArray_Descr *new;
 
1006
        int offset;
 
1007
        Py_ssize_t pos=0;
 
1008
        while (PyDict_Next(dtype->fields, &pos, &key, &value)) {
 
1009
            if (!PyArg_ParseTuple(value, "Oi|O", &new, &offset,
 
1010
                                  &title)) return;
 
1011
            _deepcopy_call(iptr + offset, optr + offset, new,
 
1012
                           deepcopy, visit);
963
1013
        }
964
 
        else {
965
 
                PyObject **itemp, **otemp;
966
 
                PyObject *res;
967
 
                itemp = (PyObject **)iptr;
968
 
                otemp = (PyObject **)optr;
969
 
                Py_XINCREF(*itemp);
970
 
                /* call deepcopy on this argument */
971
 
                res = PyObject_CallFunctionObjArgs(deepcopy,
972
 
                                                   *itemp, visit, NULL);
973
 
                Py_XDECREF(*itemp);
974
 
                Py_XDECREF(*otemp);
975
 
                *otemp = res;
976
 
        }
 
1014
    }
 
1015
    else {
 
1016
        PyObject **itemp, **otemp;
 
1017
        PyObject *res;
 
1018
        itemp = (PyObject **)iptr;
 
1019
        otemp = (PyObject **)optr;
 
1020
        Py_XINCREF(*itemp);
 
1021
        /* call deepcopy on this argument */
 
1022
        res = PyObject_CallFunctionObjArgs(deepcopy,
 
1023
                                           *itemp, visit, NULL);
 
1024
        Py_XDECREF(*itemp);
 
1025
        Py_XDECREF(*otemp);
 
1026
        *otemp = res;
 
1027
    }
977
1028
 
978
1029
}
979
1030
 
981
1032
static PyObject *
982
1033
array_deepcopy(PyArrayObject *self, PyObject *args)
983
1034
{
984
 
        PyObject* visit;
985
 
        char *optr;
986
 
        PyArrayIterObject *it;
987
 
        PyObject *copy, *ret, *deepcopy;
 
1035
    PyObject* visit;
 
1036
    char *optr;
 
1037
    PyArrayIterObject *it;
 
1038
    PyObject *copy, *ret, *deepcopy;
988
1039
 
989
 
        if (!PyArg_ParseTuple(args, "O", &visit)) return NULL;
990
 
        ret = PyArray_Copy(self);
991
 
        if (PyDataType_REFCHK(self->descr)) {
992
 
                copy = PyImport_ImportModule("copy");
993
 
                if (copy == NULL) return NULL;
994
 
                deepcopy = PyObject_GetAttrString(copy, "deepcopy");
995
 
                Py_DECREF(copy);
996
 
                if (deepcopy == NULL) return NULL;
997
 
                it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
998
 
                if (it == NULL) {Py_DECREF(deepcopy); return NULL;}
999
 
                optr = PyArray_DATA(ret);
1000
 
                while(it->index < it->size) {
1001
 
                        _deepcopy_call(it->dataptr, optr, self->descr,
1002
 
                                       deepcopy, visit);
1003
 
                        optr += self->descr->elsize;
1004
 
                        PyArray_ITER_NEXT(it);
1005
 
                }
1006
 
                Py_DECREF(deepcopy);
1007
 
                Py_DECREF(it);
 
1040
    if (!PyArg_ParseTuple(args, "O", &visit)) return NULL;
 
1041
    ret = PyArray_Copy(self);
 
1042
    if (PyDataType_REFCHK(self->descr)) {
 
1043
        copy = PyImport_ImportModule("copy");
 
1044
        if (copy == NULL) return NULL;
 
1045
        deepcopy = PyObject_GetAttrString(copy, "deepcopy");
 
1046
        Py_DECREF(copy);
 
1047
        if (deepcopy == NULL) return NULL;
 
1048
        it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
 
1049
        if (it == NULL) {Py_DECREF(deepcopy); return NULL;}
 
1050
        optr = PyArray_DATA(ret);
 
1051
        while(it->index < it->size) {
 
1052
            _deepcopy_call(it->dataptr, optr, self->descr,
 
1053
                           deepcopy, visit);
 
1054
            optr += self->descr->elsize;
 
1055
            PyArray_ITER_NEXT(it);
1008
1056
        }
1009
 
        return _ARET(ret);
 
1057
        Py_DECREF(deepcopy);
 
1058
        Py_DECREF(it);
 
1059
    }
 
1060
    return _ARET(ret);
1010
1061
}
1011
1062
 
1012
1063
/* Convert Array to flat list (using getitem) */
1013
1064
static PyObject *
1014
1065
_getlist_pkl(PyArrayObject *self)
1015
1066
{
1016
 
        PyObject *theobject;
1017
 
        PyArrayIterObject *iter=NULL;
1018
 
        PyObject *list;
1019
 
        PyArray_GetItemFunc *getitem;
 
1067
    PyObject *theobject;
 
1068
    PyArrayIterObject *iter=NULL;
 
1069
    PyObject *list;
 
1070
    PyArray_GetItemFunc *getitem;
1020
1071
 
1021
 
        getitem = self->descr->f->getitem;
1022
 
        iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
1023
 
        if (iter == NULL) return NULL;
1024
 
        list = PyList_New(iter->size);
1025
 
        if (list == NULL) {Py_DECREF(iter); return NULL;}
1026
 
        while (iter->index < iter->size) {
1027
 
                theobject = getitem(iter->dataptr, self);
1028
 
                PyList_SET_ITEM(list, (int) iter->index, theobject);
1029
 
                PyArray_ITER_NEXT(iter);
1030
 
        }
1031
 
        Py_DECREF(iter);
1032
 
        return list;
 
1072
    getitem = self->descr->f->getitem;
 
1073
    iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
 
1074
    if (iter == NULL) return NULL;
 
1075
    list = PyList_New(iter->size);
 
1076
    if (list == NULL) {Py_DECREF(iter); return NULL;}
 
1077
    while (iter->index < iter->size) {
 
1078
        theobject = getitem(iter->dataptr, self);
 
1079
        PyList_SET_ITEM(list, (int) iter->index, theobject);
 
1080
        PyArray_ITER_NEXT(iter);
 
1081
    }
 
1082
    Py_DECREF(iter);
 
1083
    return list;
1033
1084
}
1034
1085
 
1035
1086
static int
1036
1087
_setlist_pkl(PyArrayObject *self, PyObject *list)
1037
1088
{
1038
 
        PyObject *theobject;
1039
 
        PyArrayIterObject *iter=NULL;
1040
 
        PyArray_SetItemFunc *setitem;
 
1089
    PyObject *theobject;
 
1090
    PyArrayIterObject *iter=NULL;
 
1091
    PyArray_SetItemFunc *setitem;
1041
1092
 
1042
 
        setitem = self->descr->f->setitem;
1043
 
        iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
1044
 
        if (iter == NULL) return -1;
1045
 
        while(iter->index < iter->size) {
1046
 
                theobject = PyList_GET_ITEM(list, (int) iter->index);
1047
 
                setitem(theobject, iter->dataptr, self);
1048
 
                PyArray_ITER_NEXT(iter);
1049
 
        }
1050
 
        Py_XDECREF(iter);
1051
 
        return 0;
 
1093
    setitem = self->descr->f->setitem;
 
1094
    iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self);
 
1095
    if (iter == NULL) return -1;
 
1096
    while(iter->index < iter->size) {
 
1097
        theobject = PyList_GET_ITEM(list, (int) iter->index);
 
1098
        setitem(theobject, iter->dataptr, self);
 
1099
        PyArray_ITER_NEXT(iter);
 
1100
    }
 
1101
    Py_XDECREF(iter);
 
1102
    return 0;
1052
1103
}
1053
1104
 
1054
1105
 
1055
1106
static PyObject *
1056
1107
array_reduce(PyArrayObject *self, PyObject *args)
1057
1108
{
1058
 
        /* version number of this pickle type. Increment if we need to
1059
 
           change the format. Be sure to handle the old versions in
1060
 
           array_setstate. */
1061
 
        const int version = 1;
1062
 
        PyObject *ret=NULL, *state=NULL, *obj=NULL, *mod=NULL;
1063
 
        PyObject *mybool, *thestr=NULL;
1064
 
        PyArray_Descr *descr;
1065
 
 
1066
 
        /* Return a tuple of (callable object, arguments, object's state) */
1067
 
        /*  We will put everything in the object's state, so that on UnPickle
1068
 
            it can use the string object as memory without a copy */
1069
 
 
1070
 
        ret = PyTuple_New(3);
1071
 
        if (ret == NULL) return NULL;
1072
 
        mod = PyImport_ImportModule("numpy.core.multiarray");
1073
 
        if (mod == NULL) {Py_DECREF(ret); return NULL;}
1074
 
        obj = PyObject_GetAttrString(mod, "_reconstruct");
1075
 
        Py_DECREF(mod);
1076
 
        PyTuple_SET_ITEM(ret, 0, obj);
1077
 
        PyTuple_SET_ITEM(ret, 1,
1078
 
                         Py_BuildValue("ONc",
1079
 
                                       (PyObject *)self->ob_type,
1080
 
                                       Py_BuildValue("(N)",
1081
 
                                                     PyInt_FromLong(0)),
1082
 
                                       /* dummy data-type */
1083
 
                                       'b'));
1084
 
 
1085
 
        /* Now fill in object's state.  This is a tuple with
1086
 
           5 arguments
1087
 
 
1088
 
           1) an integer with the pickle version.
1089
 
           2) a Tuple giving the shape
1090
 
           3) a PyArray_Descr Object (with correct bytorder set)
1091
 
           4) a Bool stating if Fortran or not
1092
 
           5) a Python object representing the data (a string, or
1093
 
                a list or any user-defined object).
1094
 
 
1095
 
           Notice because Python does not describe a mechanism to write
1096
 
           raw data to the pickle, this performs a copy to a string first
1097
 
        */
1098
 
 
1099
 
        state = PyTuple_New(5);
1100
 
        if (state == NULL) {
1101
 
                Py_DECREF(ret); return NULL;
1102
 
        }
1103
 
        PyTuple_SET_ITEM(state, 0, PyInt_FromLong(version));
1104
 
        PyTuple_SET_ITEM(state, 1, PyObject_GetAttrString((PyObject *)self,
1105
 
                                                          "shape"));
1106
 
        descr = self->descr;
1107
 
        Py_INCREF(descr);
1108
 
        PyTuple_SET_ITEM(state, 2, (PyObject *)descr);
1109
 
        mybool = (PyArray_ISFORTRAN(self) ? Py_True : Py_False);
1110
 
        Py_INCREF(mybool);
1111
 
        PyTuple_SET_ITEM(state, 3, mybool);
1112
 
        if (PyDataType_FLAGCHK(self->descr, NPY_LIST_PICKLE)) {
1113
 
                thestr = _getlist_pkl(self);
1114
 
        }
1115
 
        else {
1116
 
                thestr = PyArray_ToString(self, NPY_ANYORDER);
1117
 
        }
1118
 
        if (thestr == NULL) {
1119
 
                Py_DECREF(ret);
1120
 
                Py_DECREF(state);
1121
 
                return NULL;
1122
 
        }
1123
 
        PyTuple_SET_ITEM(state, 4, thestr);
1124
 
        PyTuple_SET_ITEM(ret, 2, state);
1125
 
        return ret;
 
1109
    /* version number of this pickle type. Increment if we need to
 
1110
       change the format. Be sure to handle the old versions in
 
1111
       array_setstate. */
 
1112
    const int version = 1;
 
1113
    PyObject *ret=NULL, *state=NULL, *obj=NULL, *mod=NULL;
 
1114
    PyObject *mybool, *thestr=NULL;
 
1115
    PyArray_Descr *descr;
 
1116
 
 
1117
    /* Return a tuple of (callable object, arguments, object's state) */
 
1118
    /*  We will put everything in the object's state, so that on UnPickle
 
1119
        it can use the string object as memory without a copy */
 
1120
 
 
1121
    ret = PyTuple_New(3);
 
1122
    if (ret == NULL) return NULL;
 
1123
    mod = PyImport_ImportModule("numpy.core.multiarray");
 
1124
    if (mod == NULL) {Py_DECREF(ret); return NULL;}
 
1125
    obj = PyObject_GetAttrString(mod, "_reconstruct");
 
1126
    Py_DECREF(mod);
 
1127
    PyTuple_SET_ITEM(ret, 0, obj);
 
1128
    PyTuple_SET_ITEM(ret, 1,
 
1129
                     Py_BuildValue("ONc",
 
1130
                                   (PyObject *)self->ob_type,
 
1131
                                   Py_BuildValue("(N)",
 
1132
                                                 PyInt_FromLong(0)),
 
1133
                                   /* dummy data-type */
 
1134
                                   'b'));
 
1135
 
 
1136
    /* Now fill in object's state.  This is a tuple with
 
1137
       5 arguments
 
1138
 
 
1139
       1) an integer with the pickle version.
 
1140
       2) a Tuple giving the shape
 
1141
       3) a PyArray_Descr Object (with correct bytorder set)
 
1142
       4) a Bool stating if Fortran or not
 
1143
       5) a Python object representing the data (a string, or
 
1144
       a list or any user-defined object).
 
1145
 
 
1146
       Notice because Python does not describe a mechanism to write
 
1147
       raw data to the pickle, this performs a copy to a string first
 
1148
    */
 
1149
 
 
1150
    state = PyTuple_New(5);
 
1151
    if (state == NULL) {
 
1152
        Py_DECREF(ret); return NULL;
 
1153
    }
 
1154
    PyTuple_SET_ITEM(state, 0, PyInt_FromLong(version));
 
1155
    PyTuple_SET_ITEM(state, 1, PyObject_GetAttrString((PyObject *)self,
 
1156
                                                      "shape"));
 
1157
    descr = self->descr;
 
1158
    Py_INCREF(descr);
 
1159
    PyTuple_SET_ITEM(state, 2, (PyObject *)descr);
 
1160
    mybool = (PyArray_ISFORTRAN(self) ? Py_True : Py_False);
 
1161
    Py_INCREF(mybool);
 
1162
    PyTuple_SET_ITEM(state, 3, mybool);
 
1163
    if (PyDataType_FLAGCHK(self->descr, NPY_LIST_PICKLE)) {
 
1164
        thestr = _getlist_pkl(self);
 
1165
    }
 
1166
    else {
 
1167
        thestr = PyArray_ToString(self, NPY_ANYORDER);
 
1168
    }
 
1169
    if (thestr == NULL) {
 
1170
        Py_DECREF(ret);
 
1171
        Py_DECREF(state);
 
1172
        return NULL;
 
1173
    }
 
1174
    PyTuple_SET_ITEM(state, 4, thestr);
 
1175
    PyTuple_SET_ITEM(ret, 2, state);
 
1176
    return ret;
1126
1177
}
1127
1178
 
1128
1179
 
1136
1187
static PyObject *
1137
1188
array_setstate(PyArrayObject *self, PyObject *args)
1138
1189
{
1139
 
        PyObject *shape;
1140
 
        PyArray_Descr *typecode;
1141
 
        int version = 1;
1142
 
        int fortran;
1143
 
        PyObject *rawdata;
1144
 
        char *datastr;
1145
 
        Py_ssize_t len;
1146
 
        intp size, dimensions[MAX_DIMS];
1147
 
        int nd;
1148
 
 
1149
 
        /* This will free any memory associated with a and
1150
 
           use the string in setstate as the (writeable) memory.
1151
 
        */
1152
 
        if (!PyArg_ParseTuple(args, "(iO!O!iO)", &version, &PyTuple_Type,
1153
 
                              &shape, &PyArrayDescr_Type, &typecode,
1154
 
                              &fortran, &rawdata)) {
1155
 
            PyErr_Clear();
1156
 
            version = 0;
1157
 
            if (!PyArg_ParseTuple(args, "(O!O!iO)", &PyTuple_Type,
1158
 
                              &shape, &PyArrayDescr_Type, &typecode,
1159
 
                              &fortran, &rawdata)) {
1160
 
                return NULL;
1161
 
            }
1162
 
        }
1163
 
 
1164
 
        /* If we ever need another pickle format, increment the version
1165
 
           number. But we should still be able to handle the old versions.
1166
 
           We've only got one right now. */
1167
 
        if (version != 1 && version != 0) {
1168
 
            PyErr_Format(PyExc_ValueError,
1169
 
                         "can't handle version %d of numpy.ndarray pickle",
1170
 
                         version);
1171
 
            return NULL;
1172
 
        }
1173
 
 
1174
 
        Py_XDECREF(self->descr);
1175
 
        self->descr = typecode;
1176
 
        Py_INCREF(typecode);
1177
 
        nd = PyArray_IntpFromSequence(shape, dimensions, MAX_DIMS);
1178
 
        if (nd < 0) return NULL;
1179
 
        size = PyArray_MultiplyList(dimensions, nd);
1180
 
        if (self->descr->elsize == 0) {
1181
 
                PyErr_SetString(PyExc_ValueError, "Invalid data-type size.");
1182
 
                return NULL;
1183
 
        }
1184
 
        if (size < 0 || size > MAX_INTP / self->descr->elsize) {
1185
 
                PyErr_NoMemory();
1186
 
                return NULL;
1187
 
        }
1188
 
 
1189
 
        if (PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) {
1190
 
                if (!PyList_Check(rawdata)) {
1191
 
                        PyErr_SetString(PyExc_TypeError,
1192
 
                                        "object pickle not returning list");
1193
 
                        return NULL;
1194
 
                }
1195
 
        }
1196
 
        else {
1197
 
                if (!PyString_Check(rawdata)) {
1198
 
                        PyErr_SetString(PyExc_TypeError,
1199
 
                                        "pickle not returning string");
1200
 
                        return NULL;
1201
 
                }
1202
 
 
1203
 
                if (PyString_AsStringAndSize(rawdata, &datastr, &len))
1204
 
                        return NULL;
1205
 
 
1206
 
                if ((len != (self->descr->elsize * size))) {
1207
 
                        PyErr_SetString(PyExc_ValueError,
1208
 
                                        "buffer size does not"  \
1209
 
                                        " match array size");
1210
 
                        return NULL;
1211
 
                }
1212
 
        }
1213
 
 
1214
 
        if ((self->flags & OWNDATA)) {
1215
 
                if (self->data != NULL)
1216
 
                        PyDataMem_FREE(self->data);
1217
 
                self->flags &= ~OWNDATA;
1218
 
        }
1219
 
        Py_XDECREF(self->base);
1220
 
 
1221
 
        self->flags &= ~UPDATEIFCOPY;
1222
 
 
1223
 
        if (self->dimensions != NULL) {
 
1190
    PyObject *shape;
 
1191
    PyArray_Descr *typecode;
 
1192
    int version = 1;
 
1193
    int fortran;
 
1194
    PyObject *rawdata;
 
1195
    char *datastr;
 
1196
    Py_ssize_t len;
 
1197
    intp size, dimensions[MAX_DIMS];
 
1198
    int nd;
 
1199
 
 
1200
    /* This will free any memory associated with a and
 
1201
       use the string in setstate as the (writeable) memory.
 
1202
    */
 
1203
    if (!PyArg_ParseTuple(args, "(iO!O!iO)", &version, &PyTuple_Type,
 
1204
                          &shape, &PyArrayDescr_Type, &typecode,
 
1205
                          &fortran, &rawdata)) {
 
1206
        PyErr_Clear();
 
1207
        version = 0;
 
1208
        if (!PyArg_ParseTuple(args, "(O!O!iO)", &PyTuple_Type,
 
1209
                              &shape, &PyArrayDescr_Type, &typecode,
 
1210
                              &fortran, &rawdata)) {
 
1211
            return NULL;
 
1212
        }
 
1213
    }
 
1214
 
 
1215
    /* If we ever need another pickle format, increment the version
 
1216
       number. But we should still be able to handle the old versions.
 
1217
       We've only got one right now. */
 
1218
    if (version != 1 && version != 0) {
 
1219
        PyErr_Format(PyExc_ValueError,
 
1220
                     "can't handle version %d of numpy.ndarray pickle",
 
1221
                     version);
 
1222
        return NULL;
 
1223
    }
 
1224
 
 
1225
    Py_XDECREF(self->descr);
 
1226
    self->descr = typecode;
 
1227
    Py_INCREF(typecode);
 
1228
    nd = PyArray_IntpFromSequence(shape, dimensions, MAX_DIMS);
 
1229
    if (nd < 0) return NULL;
 
1230
    size = PyArray_MultiplyList(dimensions, nd);
 
1231
    if (self->descr->elsize == 0) {
 
1232
        PyErr_SetString(PyExc_ValueError, "Invalid data-type size.");
 
1233
        return NULL;
 
1234
    }
 
1235
    if (size < 0 || size > MAX_INTP / self->descr->elsize) {
 
1236
        PyErr_NoMemory();
 
1237
        return NULL;
 
1238
    }
 
1239
 
 
1240
    if (PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) {
 
1241
        if (!PyList_Check(rawdata)) {
 
1242
            PyErr_SetString(PyExc_TypeError,
 
1243
                            "object pickle not returning list");
 
1244
            return NULL;
 
1245
        }
 
1246
    }
 
1247
    else {
 
1248
        if (!PyString_Check(rawdata)) {
 
1249
            PyErr_SetString(PyExc_TypeError,
 
1250
                            "pickle not returning string");
 
1251
            return NULL;
 
1252
        }
 
1253
 
 
1254
        if (PyString_AsStringAndSize(rawdata, &datastr, &len))
 
1255
            return NULL;
 
1256
 
 
1257
        if ((len != (self->descr->elsize * size))) {
 
1258
            PyErr_SetString(PyExc_ValueError,
 
1259
                            "buffer size does not"  \
 
1260
                            " match array size");
 
1261
            return NULL;
 
1262
        }
 
1263
    }
 
1264
 
 
1265
    if ((self->flags & OWNDATA)) {
 
1266
        if (self->data != NULL)
 
1267
            PyDataMem_FREE(self->data);
 
1268
        self->flags &= ~OWNDATA;
 
1269
    }
 
1270
    Py_XDECREF(self->base);
 
1271
 
 
1272
    self->flags &= ~UPDATEIFCOPY;
 
1273
 
 
1274
    if (self->dimensions != NULL) {
 
1275
        PyDimMem_FREE(self->dimensions);
 
1276
        self->dimensions = NULL;
 
1277
    }
 
1278
 
 
1279
    self->flags = DEFAULT;
 
1280
 
 
1281
    self->nd = nd;
 
1282
 
 
1283
    if (nd > 0) {
 
1284
        self->dimensions = PyDimMem_NEW(nd * 2);
 
1285
        self->strides = self->dimensions + nd;
 
1286
        memcpy(self->dimensions, dimensions, sizeof(intp)*nd);
 
1287
        (void) _array_fill_strides(self->strides, dimensions, nd,
 
1288
                                   (size_t) self->descr->elsize,
 
1289
                                   (fortran ? FORTRAN : CONTIGUOUS),
 
1290
                                   &(self->flags));
 
1291
    }
 
1292
 
 
1293
    if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) {
 
1294
        int swap=!PyArray_ISNOTSWAPPED(self);
 
1295
        self->data = datastr;
 
1296
        if (!_IsAligned(self) || swap) {
 
1297
            intp num = PyArray_NBYTES(self);
 
1298
            self->data = PyDataMem_NEW(num);
 
1299
            if (self->data == NULL) {
 
1300
                self->nd = 0;
1224
1301
                PyDimMem_FREE(self->dimensions);
1225
 
                self->dimensions = NULL;
1226
 
        }
1227
 
 
1228
 
        self->flags = DEFAULT;
1229
 
 
1230
 
        self->nd = nd;
1231
 
 
1232
 
        if (nd > 0) {
1233
 
                self->dimensions = PyDimMem_NEW(nd * 2);
1234
 
                self->strides = self->dimensions + nd;
1235
 
                memcpy(self->dimensions, dimensions, sizeof(intp)*nd);
1236
 
                (void) _array_fill_strides(self->strides, dimensions, nd,
1237
 
                                           (size_t) self->descr->elsize,
1238
 
                                           (fortran ? FORTRAN : CONTIGUOUS),
1239
 
                                           &(self->flags));
1240
 
        }
1241
 
 
1242
 
        if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) {
1243
 
                int swap=!PyArray_ISNOTSWAPPED(self);
1244
 
                self->data = datastr;
1245
 
                if (!_IsAligned(self) || swap) {
1246
 
                        intp num = PyArray_NBYTES(self);
1247
 
                        self->data = PyDataMem_NEW(num);
1248
 
                        if (self->data == NULL) {
1249
 
                                self->nd = 0;
1250
 
                                PyDimMem_FREE(self->dimensions);
1251
 
                                return PyErr_NoMemory();
1252
 
                        }
1253
 
                        if (swap) { /* byte-swap on pickle-read */
1254
 
                                intp numels = num / self->descr->elsize;
1255
 
                                self->descr->f->copyswapn(self->data, self->descr->elsize,
1256
 
                                                          datastr, self->descr->elsize,
1257
 
                                                          numels, 1, self);
1258
 
                                if (!PyArray_ISEXTENDED(self)) {
1259
 
                                        self->descr = PyArray_DescrFromType(self->descr->type_num);
1260
 
                                }
1261
 
                                else {
1262
 
                                        self->descr = PyArray_DescrNew(typecode);
1263
 
                                        if (self->descr->byteorder == PyArray_BIG)
1264
 
                                                self->descr->byteorder = PyArray_LITTLE;
1265
 
                                        else if (self->descr->byteorder == PyArray_LITTLE)
1266
 
                                                self->descr->byteorder = PyArray_BIG;
1267
 
                                }
1268
 
                                Py_DECREF(typecode);
1269
 
                        }
1270
 
                        else {
1271
 
                                memcpy(self->data, datastr, num);
1272
 
                        }
1273
 
                        self->flags |= OWNDATA;
1274
 
                        self->base = NULL;
1275
 
                }
1276
 
                else {
1277
 
                        self->base = rawdata;
1278
 
                        Py_INCREF(self->base);
1279
 
                }
1280
 
        }
1281
 
        else {
1282
 
                self->data = PyDataMem_NEW(PyArray_NBYTES(self));
1283
 
                if (self->data == NULL) {
1284
 
                        self->nd = 0;
1285
 
                        self->data = PyDataMem_NEW(self->descr->elsize);
1286
 
                        if (self->dimensions) PyDimMem_FREE(self->dimensions);
1287
 
                        return PyErr_NoMemory();
1288
 
                }
1289
 
                if (PyDataType_FLAGCHK(self->descr, NPY_NEEDS_INIT))
1290
 
                        memset(self->data, 0, PyArray_NBYTES(self));
1291
 
                self->flags |= OWNDATA;
1292
 
                self->base = NULL;
1293
 
                if (_setlist_pkl(self, rawdata) < 0)
1294
 
                        return NULL;
1295
 
        }
1296
 
 
1297
 
        PyArray_UpdateFlags(self, UPDATE_ALL);
1298
 
 
1299
 
        Py_INCREF(Py_None);
1300
 
        return Py_None;
 
1302
                return PyErr_NoMemory();
 
1303
            }
 
1304
            if (swap) { /* byte-swap on pickle-read */
 
1305
                intp numels = num / self->descr->elsize;
 
1306
                self->descr->f->copyswapn(self->data, self->descr->elsize,
 
1307
                                          datastr, self->descr->elsize,
 
1308
                                          numels, 1, self);
 
1309
                if (!PyArray_ISEXTENDED(self)) {
 
1310
                    self->descr = PyArray_DescrFromType(self->descr->type_num);
 
1311
                }
 
1312
                else {
 
1313
                    self->descr = PyArray_DescrNew(typecode);
 
1314
                    if (self->descr->byteorder == PyArray_BIG)
 
1315
                        self->descr->byteorder = PyArray_LITTLE;
 
1316
                    else if (self->descr->byteorder == PyArray_LITTLE)
 
1317
                        self->descr->byteorder = PyArray_BIG;
 
1318
                }
 
1319
                Py_DECREF(typecode);
 
1320
            }
 
1321
            else {
 
1322
                memcpy(self->data, datastr, num);
 
1323
            }
 
1324
            self->flags |= OWNDATA;
 
1325
            self->base = NULL;
 
1326
        }
 
1327
        else {
 
1328
            self->base = rawdata;
 
1329
            Py_INCREF(self->base);
 
1330
        }
 
1331
    }
 
1332
    else {
 
1333
        self->data = PyDataMem_NEW(PyArray_NBYTES(self));
 
1334
        if (self->data == NULL) {
 
1335
            self->nd = 0;
 
1336
            self->data = PyDataMem_NEW(self->descr->elsize);
 
1337
            if (self->dimensions) PyDimMem_FREE(self->dimensions);
 
1338
            return PyErr_NoMemory();
 
1339
        }
 
1340
        if (PyDataType_FLAGCHK(self->descr, NPY_NEEDS_INIT))
 
1341
            memset(self->data, 0, PyArray_NBYTES(self));
 
1342
        self->flags |= OWNDATA;
 
1343
        self->base = NULL;
 
1344
        if (_setlist_pkl(self, rawdata) < 0)
 
1345
            return NULL;
 
1346
    }
 
1347
 
 
1348
    PyArray_UpdateFlags(self, UPDATE_ALL);
 
1349
 
 
1350
    Py_INCREF(Py_None);
 
1351
    return Py_None;
1301
1352
}
1302
1353
 
1303
1354
/*OBJECT_API*/
1304
1355
static int
1305
1356
PyArray_Dump(PyObject *self, PyObject *file, int protocol)
1306
1357
{
1307
 
        PyObject *cpick=NULL;
1308
 
        PyObject *ret;
1309
 
        if (protocol < 0) protocol = 2;
1310
 
 
1311
 
        cpick = PyImport_ImportModule("cPickle");
1312
 
        if (cpick==NULL) return -1;
1313
 
 
1314
 
        if PyString_Check(file) {
1315
 
                file = PyFile_FromString(PyString_AS_STRING(file), "wb");
1316
 
                if (file==NULL) return -1;
1317
 
        }
1318
 
        else Py_INCREF(file);
1319
 
        ret = PyObject_CallMethod(cpick, "dump", "OOi", self,
1320
 
                                  file, protocol);
1321
 
        Py_XDECREF(ret);
1322
 
        Py_DECREF(file);
1323
 
        Py_DECREF(cpick);
1324
 
        if (PyErr_Occurred()) return -1;
1325
 
        return 0;
 
1358
    PyObject *cpick=NULL;
 
1359
    PyObject *ret;
 
1360
    if (protocol < 0) protocol = 2;
 
1361
 
 
1362
    cpick = PyImport_ImportModule("cPickle");
 
1363
    if (cpick==NULL) return -1;
 
1364
 
 
1365
    if PyString_Check(file) {
 
1366
            file = PyFile_FromString(PyString_AS_STRING(file), "wb");
 
1367
            if (file==NULL) return -1;
 
1368
        }
 
1369
    else Py_INCREF(file);
 
1370
    ret = PyObject_CallMethod(cpick, "dump", "OOi", self,
 
1371
                              file, protocol);
 
1372
    Py_XDECREF(ret);
 
1373
    Py_DECREF(file);
 
1374
    Py_DECREF(cpick);
 
1375
    if (PyErr_Occurred()) return -1;
 
1376
    return 0;
1326
1377
}
1327
1378
 
1328
1379
/*OBJECT_API*/
1329
1380
static PyObject *
1330
1381
PyArray_Dumps(PyObject *self, int protocol)
1331
1382
{
1332
 
        PyObject *cpick=NULL;
1333
 
        PyObject *ret;
1334
 
        if (protocol < 0) protocol = 2;
 
1383
    PyObject *cpick=NULL;
 
1384
    PyObject *ret;
 
1385
    if (protocol < 0) protocol = 2;
1335
1386
 
1336
 
        cpick = PyImport_ImportModule("cPickle");
1337
 
        if (cpick==NULL) return NULL;
1338
 
        ret = PyObject_CallMethod(cpick, "dumps", "Oi", self, protocol);
1339
 
        Py_DECREF(cpick);
1340
 
        return ret;
 
1387
    cpick = PyImport_ImportModule("cPickle");
 
1388
    if (cpick==NULL) return NULL;
 
1389
    ret = PyObject_CallMethod(cpick, "dumps", "Oi", self, protocol);
 
1390
    Py_DECREF(cpick);
 
1391
    return ret;
1341
1392
}
1342
1393
 
1343
1394
 
1344
1395
static PyObject *
1345
1396
array_dump(PyArrayObject *self, PyObject *args)
1346
1397
{
1347
 
        PyObject *file=NULL;
1348
 
        int ret;
 
1398
    PyObject *file=NULL;
 
1399
    int ret;
1349
1400
 
1350
 
        if (!PyArg_ParseTuple(args, "O", &file))
1351
 
                return NULL;
1352
 
        ret = PyArray_Dump((PyObject *)self, file, 2);
1353
 
        if (ret < 0) return NULL;
1354
 
        Py_INCREF(Py_None);
1355
 
        return Py_None;
 
1401
    if (!PyArg_ParseTuple(args, "O", &file))
 
1402
        return NULL;
 
1403
    ret = PyArray_Dump((PyObject *)self, file, 2);
 
1404
    if (ret < 0) return NULL;
 
1405
    Py_INCREF(Py_None);
 
1406
    return Py_None;
1356
1407
}
1357
1408
 
1358
1409
 
1359
1410
static PyObject *
1360
1411
array_dumps(PyArrayObject *self, PyObject *args)
1361
1412
{
1362
 
        if (!PyArg_ParseTuple(args, ""))
1363
 
                return NULL;
1364
 
        return PyArray_Dumps((PyObject *)self, 2);
 
1413
    if (!PyArg_ParseTuple(args, ""))
 
1414
        return NULL;
 
1415
    return PyArray_Dumps((PyObject *)self, 2);
1365
1416
}
1366
1417
 
1367
1418
 
1368
1419
static PyObject *
1369
1420
array_transpose(PyArrayObject *self, PyObject *args)
1370
1421
{
1371
 
        PyObject *shape=Py_None;
1372
 
        int n;
1373
 
        PyArray_Dims permute;
1374
 
        PyObject *ret;
1375
 
 
1376
 
        n = PyTuple_Size(args);
1377
 
        if (n > 1) shape = args;
1378
 
        else if (n == 1) shape = PyTuple_GET_ITEM(args, 0);
1379
 
 
1380
 
        if (shape == Py_None)
1381
 
                ret = PyArray_Transpose(self, NULL);
1382
 
        else {
1383
 
                if (!PyArray_IntpConverter(shape, &permute)) return NULL;
1384
 
                ret = PyArray_Transpose(self, &permute);
1385
 
                PyDimMem_FREE(permute.ptr);
1386
 
        }
1387
 
 
1388
 
        return ret;
 
1422
    PyObject *shape=Py_None;
 
1423
    int n;
 
1424
    PyArray_Dims permute;
 
1425
    PyObject *ret;
 
1426
 
 
1427
    n = PyTuple_Size(args);
 
1428
    if (n > 1) shape = args;
 
1429
    else if (n == 1) shape = PyTuple_GET_ITEM(args, 0);
 
1430
 
 
1431
    if (shape == Py_None)
 
1432
        ret = PyArray_Transpose(self, NULL);
 
1433
    else {
 
1434
        if (!PyArray_IntpConverter(shape, &permute)) return NULL;
 
1435
        ret = PyArray_Transpose(self, &permute);
 
1436
        PyDimMem_FREE(permute.ptr);
 
1437
    }
 
1438
 
 
1439
    return ret;
1389
1440
}
1390
1441
 
1391
 
/* Return typenumber from dtype2 unless it is NULL, then return 
 
1442
/* Return typenumber from dtype2 unless it is NULL, then return
1392
1443
   NPY_DOUBLE if dtype1->type_num is integer or bool
1393
 
   and dtype1->type_num otherwise. 
 
1444
   and dtype1->type_num otherwise.
1394
1445
*/
1395
1446
static int
1396
1447
_get_type_num_double(PyArray_Descr *dtype1, PyArray_Descr *dtype2)
1397
1448
{
1398
 
        if (dtype2 != NULL)
1399
 
                return dtype2->type_num;
1400
 
        
1401
 
        /* For integer or bool data-types */
1402
 
        if (dtype1->type_num < NPY_FLOAT) {
1403
 
                return NPY_DOUBLE;
1404
 
        }
1405
 
        else {
1406
 
                return dtype1->type_num;
1407
 
        }
 
1449
    if (dtype2 != NULL)
 
1450
        return dtype2->type_num;
 
1451
 
 
1452
    /* For integer or bool data-types */
 
1453
    if (dtype1->type_num < NPY_FLOAT) {
 
1454
        return NPY_DOUBLE;
 
1455
    }
 
1456
    else {
 
1457
        return dtype1->type_num;
 
1458
    }
1408
1459
}
1409
1460
 
1410
1461
#define _CHKTYPENUM(typ) ((typ) ? (typ)->type_num : PyArray_NOTYPE)
1412
1463
static PyObject *
1413
1464
array_mean(PyArrayObject *self, PyObject *args, PyObject *kwds)
1414
1465
{
1415
 
        int axis=MAX_DIMS;
1416
 
        PyArray_Descr *dtype=NULL;
1417
 
        PyArrayObject *out=NULL;
1418
 
        int num;
1419
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1420
 
 
1421
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1422
 
                                         PyArray_AxisConverter,
1423
 
                                         &axis, PyArray_DescrConverter2,
1424
 
                                         &dtype,
1425
 
                                         PyArray_OutputConverter,
1426
 
                                         &out)) return NULL;        
1427
 
 
1428
 
        num = _get_type_num_double(self->descr, dtype);
1429
 
        return PyArray_Mean(self, axis, num, out);
 
1466
    int axis=MAX_DIMS;
 
1467
    PyArray_Descr *dtype=NULL;
 
1468
    PyArrayObject *out=NULL;
 
1469
    int num;
 
1470
    static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
1471
 
 
1472
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
 
1473
                                     PyArray_AxisConverter,
 
1474
                                     &axis, PyArray_DescrConverter2,
 
1475
                                     &dtype,
 
1476
                                     PyArray_OutputConverter,
 
1477
                                     &out)) {
 
1478
        Py_XDECREF(dtype);
 
1479
        return NULL;
 
1480
    }
 
1481
 
 
1482
    num = _get_type_num_double(self->descr, dtype);
 
1483
    Py_XDECREF(dtype);
 
1484
    return PyArray_Mean(self, axis, num, out);
1430
1485
}
1431
1486
 
1432
1487
static PyObject *
1433
1488
array_sum(PyArrayObject *self, PyObject *args, PyObject *kwds)
1434
1489
{
1435
 
        int axis=MAX_DIMS;
1436
 
        PyArray_Descr *dtype=NULL;
1437
 
        PyArrayObject *out=NULL;
1438
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1439
 
 
1440
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1441
 
                                         PyArray_AxisConverter,
1442
 
                                         &axis, PyArray_DescrConverter2,
1443
 
                                         &dtype,
1444
 
                                         PyArray_OutputConverter,
1445
 
                                         &out)) return NULL;
1446
 
 
1447
 
        return PyArray_Sum(self, axis, _CHKTYPENUM(dtype), out);
 
1490
    int axis=MAX_DIMS;
 
1491
    PyArray_Descr *dtype=NULL;
 
1492
    PyArrayObject *out=NULL;
 
1493
    int rtype;
 
1494
    static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
1495
 
 
1496
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
 
1497
                                     PyArray_AxisConverter,
 
1498
                                     &axis, PyArray_DescrConverter2,
 
1499
                                     &dtype,
 
1500
                                     PyArray_OutputConverter,
 
1501
                                     &out)) {
 
1502
        Py_XDECREF(dtype);
 
1503
        return NULL;
 
1504
    }
 
1505
 
 
1506
    rtype = _CHKTYPENUM(dtype);
 
1507
    Py_XDECREF(dtype);
 
1508
    return PyArray_Sum(self, axis, rtype, out);
1448
1509
}
1449
1510
 
1450
1511
 
1451
1512
static PyObject *
1452
1513
array_cumsum(PyArrayObject *self, PyObject *args, PyObject *kwds)
1453
1514
{
1454
 
        int axis=MAX_DIMS;
1455
 
        PyArray_Descr *dtype=NULL;
1456
 
        PyArrayObject *out=NULL;
1457
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1458
 
 
1459
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1460
 
                                         PyArray_AxisConverter,
1461
 
                                         &axis, PyArray_DescrConverter2,
1462
 
                                         &dtype,
1463
 
                                         PyArray_OutputConverter,
1464
 
                                         &out)) return NULL;
1465
 
 
1466
 
        return PyArray_CumSum(self, axis, _CHKTYPENUM(dtype), out);
 
1515
    int axis=MAX_DIMS;
 
1516
    PyArray_Descr *dtype=NULL;
 
1517
    PyArrayObject *out=NULL;
 
1518
    int rtype;
 
1519
    static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
1520
 
 
1521
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
 
1522
                                     PyArray_AxisConverter,
 
1523
                                     &axis, PyArray_DescrConverter2,
 
1524
                                     &dtype,
 
1525
                                     PyArray_OutputConverter,
 
1526
                                     &out)) {
 
1527
        Py_XDECREF(dtype);
 
1528
        return NULL;
 
1529
    }
 
1530
 
 
1531
    rtype = _CHKTYPENUM(dtype);
 
1532
    Py_XDECREF(dtype);
 
1533
    return PyArray_CumSum(self, axis, rtype, out);
1467
1534
}
1468
1535
 
1469
1536
static PyObject *
1470
1537
array_prod(PyArrayObject *self, PyObject *args, PyObject *kwds)
1471
1538
{
1472
 
        int axis=MAX_DIMS;
1473
 
        PyArray_Descr *dtype=NULL;
1474
 
        PyArrayObject *out=NULL;
1475
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1476
 
 
1477
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1478
 
                                         PyArray_AxisConverter,
1479
 
                                         &axis, PyArray_DescrConverter2,
1480
 
                                         &dtype,
1481
 
                                         PyArray_OutputConverter,
1482
 
                                         &out)) return NULL;
1483
 
 
1484
 
        return PyArray_Prod(self, axis, _CHKTYPENUM(dtype), out);
 
1539
    int axis=MAX_DIMS;
 
1540
    PyArray_Descr *dtype=NULL;
 
1541
    PyArrayObject *out=NULL;
 
1542
    int rtype;
 
1543
    static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
1544
 
 
1545
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
 
1546
                                     PyArray_AxisConverter,
 
1547
                                     &axis, PyArray_DescrConverter2,
 
1548
                                     &dtype,
 
1549
                                     PyArray_OutputConverter,
 
1550
                                     &out)) {
 
1551
        Py_XDECREF(dtype);
 
1552
        return NULL;
 
1553
    }
 
1554
 
 
1555
    rtype = _CHKTYPENUM(dtype);
 
1556
    Py_XDECREF(dtype);
 
1557
    return PyArray_Prod(self, axis, rtype, out);
1485
1558
}
1486
1559
 
1487
1560
static PyObject *
1488
1561
array_cumprod(PyArrayObject *self, PyObject *args, PyObject *kwds)
1489
1562
{
1490
 
        int axis=MAX_DIMS;
1491
 
        PyArray_Descr *dtype=NULL;
1492
 
        PyArrayObject *out=NULL;
1493
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1494
 
 
1495
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1496
 
                                         PyArray_AxisConverter,
1497
 
                                         &axis, PyArray_DescrConverter2,
1498
 
                                         &dtype,
1499
 
                                         PyArray_OutputConverter,
1500
 
                                         &out)) return NULL;
1501
 
 
1502
 
        return PyArray_CumProd(self, axis, _CHKTYPENUM(dtype), out);
 
1563
    int axis=MAX_DIMS;
 
1564
    PyArray_Descr *dtype=NULL;
 
1565
    PyArrayObject *out=NULL;
 
1566
    int rtype;
 
1567
    static char *kwlist[] = {"axis", "dtype", "out", NULL};
 
1568
 
 
1569
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
 
1570
                                     PyArray_AxisConverter,
 
1571
                                     &axis, PyArray_DescrConverter2,
 
1572
                                     &dtype,
 
1573
                                     PyArray_OutputConverter,
 
1574
                                     &out)) {
 
1575
        Py_XDECREF(dtype);
 
1576
        return NULL;
 
1577
    }
 
1578
 
 
1579
    rtype = _CHKTYPENUM(dtype);
 
1580
    Py_XDECREF(dtype);
 
1581
    return PyArray_CumProd(self, axis, rtype, out);
1503
1582
}
1504
1583
 
1505
1584
 
1506
1585
static PyObject *
1507
1586
array_any(PyArrayObject *self, PyObject *args, PyObject *kwds)
1508
1587
{
1509
 
        int axis=MAX_DIMS;
1510
 
        PyArrayObject *out=NULL;
1511
 
        static char *kwlist[] = {"axis", "out", NULL};
1512
 
 
1513
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
1514
 
                                         PyArray_AxisConverter,
1515
 
                                         &axis,
1516
 
                                         PyArray_OutputConverter,
1517
 
                                         &out))
1518
 
                return NULL;
1519
 
 
1520
 
        return PyArray_Any(self, axis, out);
 
1588
    int axis=MAX_DIMS;
 
1589
    PyArrayObject *out=NULL;
 
1590
    static char *kwlist[] = {"axis", "out", NULL};
 
1591
 
 
1592
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
1593
                                     PyArray_AxisConverter,
 
1594
                                     &axis,
 
1595
                                     PyArray_OutputConverter,
 
1596
                                     &out))
 
1597
        return NULL;
 
1598
 
 
1599
    return PyArray_Any(self, axis, out);
1521
1600
}
1522
1601
 
1523
1602
 
1524
1603
static PyObject *
1525
1604
array_all(PyArrayObject *self, PyObject *args, PyObject *kwds)
1526
1605
{
1527
 
        int axis=MAX_DIMS;
1528
 
        PyArrayObject *out=NULL;
1529
 
        static char *kwlist[] = {"axis", "out", NULL};
1530
 
 
1531
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
1532
 
                                         PyArray_AxisConverter,
1533
 
                                         &axis,
1534
 
                                         PyArray_OutputConverter,
1535
 
                                         &out))
1536
 
                return NULL;
1537
 
 
1538
 
        return PyArray_All(self, axis, out);
 
1606
    int axis=MAX_DIMS;
 
1607
    PyArrayObject *out=NULL;
 
1608
    static char *kwlist[] = {"axis", "out", NULL};
 
1609
 
 
1610
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&", kwlist,
 
1611
                                     PyArray_AxisConverter,
 
1612
                                     &axis,
 
1613
                                     PyArray_OutputConverter,
 
1614
                                     &out))
 
1615
        return NULL;
 
1616
 
 
1617
    return PyArray_All(self, axis, out);
1539
1618
}
1540
1619
 
1541
1620
 
1542
1621
static PyObject *
 
1622
__New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out,
 
1623
                  int variance, int num);
 
1624
static PyObject *
1543
1625
array_stddev(PyArrayObject *self, PyObject *args, PyObject *kwds)
1544
1626
{
1545
 
        int axis=MAX_DIMS;
1546
 
        PyArray_Descr *dtype=NULL;
1547
 
        PyArrayObject *out=NULL;
1548
 
        int num;
1549
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1550
 
 
1551
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1552
 
                                         PyArray_AxisConverter,
1553
 
                                         &axis, PyArray_DescrConverter2,
1554
 
                                         &dtype,
1555
 
                                         PyArray_OutputConverter,
1556
 
                                         &out)) return NULL;
1557
 
 
1558
 
        num = _get_type_num_double(self->descr, dtype);
1559
 
        return PyArray_Std(self, axis, num, out, 0);
 
1627
    int axis=MAX_DIMS;
 
1628
    PyArray_Descr *dtype=NULL;
 
1629
    PyArrayObject *out=NULL;
 
1630
    int num;
 
1631
    int ddof = 0;
 
1632
    static char *kwlist[] = {"axis", "dtype", "out", "ddof", NULL};
 
1633
 
 
1634
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&i", kwlist,
 
1635
                                     PyArray_AxisConverter,
 
1636
                                     &axis, PyArray_DescrConverter2,
 
1637
                                     &dtype,
 
1638
                                     PyArray_OutputConverter,
 
1639
                                     &out, &ddof)) {
 
1640
        Py_XDECREF(dtype);
 
1641
        return NULL;
 
1642
    }
 
1643
 
 
1644
    num = _get_type_num_double(self->descr, dtype);
 
1645
    Py_XDECREF(dtype);
 
1646
    return __New_PyArray_Std(self, axis, num, out, 0, ddof);
1560
1647
}
1561
1648
 
1562
1649
 
1563
1650
static PyObject *
1564
1651
array_variance(PyArrayObject *self, PyObject *args, PyObject *kwds)
1565
1652
{
1566
 
        int axis=MAX_DIMS;
1567
 
        PyArray_Descr *dtype=NULL;
1568
 
        PyArrayObject *out=NULL;
1569
 
        int num;
1570
 
        static char *kwlist[] = {"axis", "dtype", "out", NULL};
1571
 
 
1572
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&", kwlist,
1573
 
                                         PyArray_AxisConverter,
1574
 
                                         &axis, PyArray_DescrConverter2,
1575
 
                                         &dtype,
1576
 
                                         PyArray_OutputConverter,
1577
 
                                         &out)) return NULL;
1578
 
 
1579
 
        num = _get_type_num_double(self->descr, dtype);
1580
 
        return PyArray_Std(self, axis, num, out, 1);
 
1653
    int axis=MAX_DIMS;
 
1654
    PyArray_Descr *dtype=NULL;
 
1655
    PyArrayObject *out=NULL;
 
1656
    int num;
 
1657
    int ddof = 0;
 
1658
    static char *kwlist[] = {"axis", "dtype", "out", "ddof", NULL};
 
1659
 
 
1660
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&O&O&i", kwlist,
 
1661
                                     PyArray_AxisConverter,
 
1662
                                     &axis, PyArray_DescrConverter2,
 
1663
                                     &dtype,
 
1664
                                     PyArray_OutputConverter,
 
1665
                                     &out, &ddof)) {
 
1666
        Py_XDECREF(dtype);
 
1667
        return NULL;
 
1668
    } 
 
1669
 
 
1670
    num = _get_type_num_double(self->descr, dtype);
 
1671
    Py_XDECREF(dtype);
 
1672
    return __New_PyArray_Std(self, axis, num, out, 1, ddof);
1581
1673
}
1582
1674
 
1583
1675
 
1584
1676
static PyObject *
1585
1677
array_compress(PyArrayObject *self, PyObject *args, PyObject *kwds)
1586
1678
{
1587
 
        int axis=MAX_DIMS;
1588
 
        PyObject *condition;
1589
 
        PyArrayObject *out=NULL;
1590
 
        static char *kwlist[] = {"condition", "axis", "out", NULL};
1591
 
 
1592
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&", kwlist,
1593
 
                                         &condition, PyArray_AxisConverter,
1594
 
                                         &axis,
1595
 
                                         PyArray_OutputConverter,
1596
 
                                         &out)) return NULL;
1597
 
 
1598
 
        return _ARET(PyArray_Compress(self, condition, axis, out));
 
1679
    int axis=MAX_DIMS;
 
1680
    PyObject *condition;
 
1681
    PyArrayObject *out=NULL;
 
1682
    static char *kwlist[] = {"condition", "axis", "out", NULL};
 
1683
 
 
1684
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O&O&", kwlist,
 
1685
                                     &condition, PyArray_AxisConverter,
 
1686
                                     &axis,
 
1687
                                     PyArray_OutputConverter,
 
1688
                                     &out)) return NULL;
 
1689
 
 
1690
    return _ARET(PyArray_Compress(self, condition, axis, out));
1599
1691
}
1600
1692
 
1601
1693
 
1602
1694
static PyObject *
1603
1695
array_nonzero(PyArrayObject *self, PyObject *args)
1604
1696
{
1605
 
        if (!PyArg_ParseTuple(args, "")) return NULL;
 
1697
    if (!PyArg_ParseTuple(args, "")) return NULL;
1606
1698
 
1607
 
        return PyArray_Nonzero(self);
 
1699
    return PyArray_Nonzero(self);
1608
1700
}
1609
1701
 
1610
1702
 
1611
1703
static PyObject *
1612
1704
array_trace(PyArrayObject *self, PyObject *args, PyObject *kwds)
1613
1705
{
1614
 
        int axis1=0, axis2=1, offset=0;
1615
 
        PyArray_Descr *dtype=NULL;
1616
 
        PyArrayObject *out=NULL;
1617
 
        static char *kwlist[] = {"offset", "axis1", "axis2", "dtype", "out", NULL};
1618
 
 
1619
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiO&O&", kwlist,
1620
 
                                         &offset, &axis1, &axis2,
1621
 
                                         PyArray_DescrConverter2, &dtype,
1622
 
                                         PyArray_OutputConverter, &out))
1623
 
                return NULL;
1624
 
 
1625
 
        return _ARET(PyArray_Trace(self, offset, axis1, axis2,
1626
 
                                   _CHKTYPENUM(dtype), out));
 
1706
    int axis1=0, axis2=1, offset=0;
 
1707
    PyArray_Descr *dtype=NULL;
 
1708
    PyArrayObject *out=NULL;
 
1709
    int rtype;
 
1710
    static char *kwlist[] = {"offset", "axis1", "axis2", "dtype", "out", NULL};
 
1711
 
 
1712
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiO&O&", kwlist,
 
1713
                                     &offset, &axis1, &axis2,
 
1714
                                     PyArray_DescrConverter2, &dtype,
 
1715
                                     PyArray_OutputConverter, &out)) {
 
1716
        Py_XDECREF(dtype);
 
1717
        return NULL;
 
1718
    }
 
1719
 
 
1720
    rtype = _CHKTYPENUM(dtype);
 
1721
    Py_XDECREF(dtype);
 
1722
 
 
1723
    return _ARET(PyArray_Trace(self, offset, axis1, axis2,
 
1724
                               rtype, out));
1627
1725
}
1628
1726
 
1629
1727
#undef _CHKTYPENUM
1632
1730
static PyObject *
1633
1731
array_clip(PyArrayObject *self, PyObject *args, PyObject *kwds)
1634
1732
{
1635
 
        PyObject *min, *max;
1636
 
        PyArrayObject *out=NULL;
1637
 
        static char *kwlist[] = {"min", "max", "out", NULL};
1638
 
 
1639
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|O&", kwlist,
1640
 
                                         &min, &max,
1641
 
                                         PyArray_OutputConverter,
1642
 
                                         &out))
1643
 
                return NULL;
1644
 
 
1645
 
        return _ARET(PyArray_Clip(self, min, max, out));
 
1733
    PyObject *min=NULL, *max=NULL;
 
1734
    PyArrayObject *out=NULL;
 
1735
    static char *kwlist[] = {"min", "max", "out", NULL};
 
1736
 
 
1737
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO&", kwlist,
 
1738
                                     &min, &max,
 
1739
                                     PyArray_OutputConverter,
 
1740
                                     &out))
 
1741
        return NULL;
 
1742
 
 
1743
    if (max == NULL && min == NULL) {
 
1744
        PyErr_SetString(PyExc_ValueError, "One of max or min must be given.");
 
1745
        return NULL;                    
 
1746
    }
 
1747
    return _ARET(PyArray_Clip(self, min, max, out));
1646
1748
}
1647
1749
 
1648
1750
 
1650
1752
array_conjugate(PyArrayObject *self, PyObject *args)
1651
1753
{
1652
1754
 
1653
 
        PyArrayObject *out=NULL;
1654
 
        if (!PyArg_ParseTuple(args, "|O&",
1655
 
                              PyArray_OutputConverter,
1656
 
                              &out)) return NULL;
 
1755
    PyArrayObject *out=NULL;
 
1756
    if (!PyArg_ParseTuple(args, "|O&",
 
1757
                          PyArray_OutputConverter,
 
1758
                          &out)) return NULL;
1657
1759
 
1658
 
        return PyArray_Conjugate(self, out);
 
1760
    return PyArray_Conjugate(self, out);
1659
1761
}
1660
1762
 
1661
1763
 
1662
1764
static PyObject *
1663
1765
array_diagonal(PyArrayObject *self, PyObject *args, PyObject *kwds)
1664
1766
{
1665
 
        int axis1=0, axis2=1, offset=0;
1666
 
        static char *kwlist[] = {"offset", "axis1", "axis2", NULL};
1667
 
 
1668
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwlist,
1669
 
                                         &offset, &axis1, &axis2))
1670
 
                return NULL;
1671
 
 
1672
 
        return _ARET(PyArray_Diagonal(self, offset, axis1, axis2));
 
1767
    int axis1=0, axis2=1, offset=0;
 
1768
    static char *kwlist[] = {"offset", "axis1", "axis2", NULL};
 
1769
 
 
1770
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwlist,
 
1771
                                     &offset, &axis1, &axis2))
 
1772
        return NULL;
 
1773
 
 
1774
    return _ARET(PyArray_Diagonal(self, offset, axis1, axis2));
1673
1775
}
1674
1776
 
1675
1777
 
1676
1778
static PyObject *
1677
1779
array_flatten(PyArrayObject *self, PyObject *args)
1678
1780
{
1679
 
        PyArray_ORDER fortran=PyArray_CORDER;
1680
 
 
1681
 
        if (!PyArg_ParseTuple(args, "|O&", PyArray_OrderConverter,
1682
 
                              &fortran)) return NULL;
1683
 
 
1684
 
        return PyArray_Flatten(self, fortran);
 
1781
    PyArray_ORDER fortran=PyArray_CORDER;
 
1782
 
 
1783
    if (!PyArg_ParseTuple(args, "|O&", PyArray_OrderConverter,
 
1784
                          &fortran)) return NULL;
 
1785
 
 
1786
    return PyArray_Flatten(self, fortran);
1685
1787
}
1686
1788
 
1687
1789
 
1688
1790
static PyObject *
1689
1791
array_ravel(PyArrayObject *self, PyObject *args)
1690
1792
{
1691
 
        PyArray_ORDER fortran=PyArray_CORDER;
1692
 
 
1693
 
        if (!PyArg_ParseTuple(args, "|O&", PyArray_OrderConverter,
1694
 
                              &fortran)) return NULL;
1695
 
 
1696
 
        return PyArray_Ravel(self, fortran);
 
1793
    PyArray_ORDER fortran=PyArray_CORDER;
 
1794
 
 
1795
    if (!PyArg_ParseTuple(args, "|O&", PyArray_OrderConverter,
 
1796
                          &fortran)) return NULL;
 
1797
 
 
1798
    return PyArray_Ravel(self, fortran);
1697
1799
}
1698
1800
 
1699
1801
 
1700
1802
static PyObject *
1701
1803
array_round(PyArrayObject *self, PyObject *args, PyObject *kwds)
1702
1804
{
1703
 
        int decimals = 0;
1704
 
        PyArrayObject *out=NULL;
1705
 
        static char *kwlist[] = {"decimals", "out", NULL};
1706
 
 
1707
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&", kwlist,
1708
 
                                         &decimals, PyArray_OutputConverter,
1709
 
                                         &out))
1710
 
            return NULL;
1711
 
 
1712
 
        return _ARET(PyArray_Round(self, decimals, out));
 
1805
    int decimals = 0;
 
1806
    PyArrayObject *out=NULL;
 
1807
    static char *kwlist[] = {"decimals", "out", NULL};
 
1808
 
 
1809
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&", kwlist,
 
1810
                                     &decimals, PyArray_OutputConverter,
 
1811
                                     &out))
 
1812
        return NULL;
 
1813
 
 
1814
    return _ARET(PyArray_Round(self, decimals, out));
1713
1815
}
1714
1816
 
1715
1817
 
1720
1822
static PyObject *
1721
1823
array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds)
1722
1824
{
1723
 
        static char *kwlist[] = {"write", "align", "uic", NULL};
1724
 
        PyObject *write=Py_None;
1725
 
        PyObject *align=Py_None;
1726
 
        PyObject *uic=Py_None;
1727
 
        int flagback = self->flags;
1728
 
 
1729
 
        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist,
1730
 
                                         &write, &align, &uic))
1731
 
                return NULL;
1732
 
 
1733
 
        if (align != Py_None) {
1734
 
                if (PyObject_Not(align)) self->flags &= ~ALIGNED;
1735
 
                else if (_IsAligned(self)) self->flags |= ALIGNED;
1736
 
                else {
1737
 
                        PyErr_SetString(PyExc_ValueError,
1738
 
                                        "cannot set aligned flag of mis-"\
1739
 
                                        "aligned array to True");
1740
 
                        return NULL;
1741
 
                }
1742
 
        }
1743
 
 
1744
 
        if (uic != Py_None) {
1745
 
                if (PyObject_IsTrue(uic)) {
1746
 
                        self->flags = flagback;
1747
 
                        PyErr_SetString(PyExc_ValueError,
1748
 
                                        "cannot set UPDATEIFCOPY "       \
1749
 
                                        "flag to True");
1750
 
                        return NULL;
1751
 
                }
1752
 
                else {
1753
 
                        self->flags &= ~UPDATEIFCOPY;
1754
 
                        Py_XDECREF(self->base);
1755
 
                        self->base = NULL;
1756
 
                }
1757
 
        }
1758
 
 
1759
 
        if (write != Py_None) {
1760
 
                if (PyObject_IsTrue(write))
1761
 
                        if (_IsWriteable(self)) {
1762
 
                                self->flags |= WRITEABLE;
1763
 
                        }
1764
 
                        else {
1765
 
                                self->flags = flagback;
1766
 
                                PyErr_SetString(PyExc_ValueError,
1767
 
                                                "cannot set WRITEABLE " \
1768
 
                                                "flag to True of this " \
1769
 
                                                "array");               \
1770
 
                                return NULL;
1771
 
                        }
1772
 
                else
1773
 
                        self->flags &= ~WRITEABLE;
1774
 
        }
1775
 
 
1776
 
        Py_INCREF(Py_None);
1777
 
        return Py_None;
 
1825
    static char *kwlist[] = {"write", "align", "uic", NULL};
 
1826
    PyObject *write=Py_None;
 
1827
    PyObject *align=Py_None;
 
1828
    PyObject *uic=Py_None;
 
1829
    int flagback = self->flags;
 
1830
 
 
1831
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist,
 
1832
                                     &write, &align, &uic))
 
1833
        return NULL;
 
1834
 
 
1835
    if (align != Py_None) {
 
1836
        if (PyObject_Not(align)) self->flags &= ~ALIGNED;
 
1837
        else if (_IsAligned(self)) self->flags |= ALIGNED;
 
1838
        else {
 
1839
            PyErr_SetString(PyExc_ValueError,
 
1840
                            "cannot set aligned flag of mis-"\
 
1841
                            "aligned array to True");
 
1842
            return NULL;
 
1843
        }
 
1844
    }
 
1845
 
 
1846
    if (uic != Py_None) {
 
1847
        if (PyObject_IsTrue(uic)) {
 
1848
            self->flags = flagback;
 
1849
            PyErr_SetString(PyExc_ValueError,
 
1850
                            "cannot set UPDATEIFCOPY "       \
 
1851
                            "flag to True");
 
1852
            return NULL;
 
1853
        }
 
1854
        else {
 
1855
            self->flags &= ~UPDATEIFCOPY;
 
1856
            Py_XDECREF(self->base);
 
1857
            self->base = NULL;
 
1858
        }
 
1859
    }
 
1860
 
 
1861
    if (write != Py_None) {
 
1862
        if (PyObject_IsTrue(write))
 
1863
            if (_IsWriteable(self)) {
 
1864
                self->flags |= WRITEABLE;
 
1865
            }
 
1866
            else {
 
1867
                self->flags = flagback;
 
1868
                PyErr_SetString(PyExc_ValueError,
 
1869
                                "cannot set WRITEABLE " \
 
1870
                                "flag to True of this " \
 
1871
                                "array");               \
 
1872
                return NULL;
 
1873
            }
 
1874
        else
 
1875
            self->flags &= ~WRITEABLE;
 
1876
    }
 
1877
 
 
1878
    Py_INCREF(Py_None);
 
1879
    return Py_None;
1778
1880
}
1779
1881
 
1780
1882
 
1781
1883
static PyObject *
1782
1884
array_newbyteorder(PyArrayObject *self, PyObject *args)
1783
1885
{
1784
 
        char endian = PyArray_SWAP;
1785
 
        PyArray_Descr *new;
1786
 
 
1787
 
        if (!PyArg_ParseTuple(args, "|O&", PyArray_ByteorderConverter,
1788
 
                              &endian)) return NULL;
1789
 
 
1790
 
        new = PyArray_DescrNewByteorder(self->descr, endian);
1791
 
        if (!new) return NULL;
1792
 
        return PyArray_View(self, new, NULL);
 
1886
    char endian = PyArray_SWAP;
 
1887
    PyArray_Descr *new;
 
1888
 
 
1889
    if (!PyArg_ParseTuple(args, "|O&", PyArray_ByteorderConverter,
 
1890
                          &endian)) return NULL;
 
1891
 
 
1892
    new = PyArray_DescrNewByteorder(self->descr, endian);
 
1893
    if (!new) return NULL;
 
1894
    return PyArray_View(self, new, NULL);
1793
1895
 
1794
1896
}
1795
1897
 
1796
1898
static PyMethodDef array_methods[] = {
1797
1899
 
1798
 
        /* for subtypes */
1799
 
        {"__array__", (PyCFunction)array_getarray,
1800
 
            METH_VARARGS, NULL},
1801
 
        {"__array_wrap__", (PyCFunction)array_wraparray,
1802
 
            METH_VARARGS, NULL},
1803
 
 
1804
 
        /* for the copy module */
1805
 
        {"__copy__", (PyCFunction)array_copy,
1806
 
            METH_VARARGS, NULL},
1807
 
        {"__deepcopy__", (PyCFunction)array_deepcopy,
1808
 
            METH_VARARGS, NULL},
1809
 
 
1810
 
        /* for Pickling */
1811
 
        {"__reduce__", (PyCFunction) array_reduce,
1812
 
            METH_VARARGS, NULL},
1813
 
        {"__setstate__", (PyCFunction) array_setstate,
1814
 
            METH_VARARGS, NULL},
1815
 
        {"dumps", (PyCFunction) array_dumps,
1816
 
            METH_VARARGS, NULL},
1817
 
        {"dump", (PyCFunction) array_dump,
1818
 
            METH_VARARGS, NULL},
1819
 
 
1820
 
        /* Original and Extended methods added 2005 */
1821
 
        {"all", (PyCFunction)array_all,
1822
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1823
 
        {"any", (PyCFunction)array_any,
1824
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1825
 
        {"argmax", (PyCFunction)array_argmax,
1826
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1827
 
        {"argmin", (PyCFunction)array_argmin,
1828
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1829
 
        {"argsort", (PyCFunction)array_argsort,
1830
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1831
 
        {"astype", (PyCFunction)array_cast,
1832
 
            METH_VARARGS, NULL},
1833
 
        {"byteswap", (PyCFunction)array_byteswap,
1834
 
            METH_VARARGS, NULL},
1835
 
        {"choose", (PyCFunction)array_choose,
1836
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1837
 
        {"clip", (PyCFunction)array_clip,
1838
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1839
 
        {"compress", (PyCFunction)array_compress,
1840
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1841
 
        {"conj", (PyCFunction)array_conjugate,
1842
 
            METH_VARARGS, NULL},
1843
 
        {"conjugate", (PyCFunction)array_conjugate,
1844
 
            METH_VARARGS, NULL},
1845
 
        {"copy", (PyCFunction)array_copy,
1846
 
            METH_VARARGS, NULL},
1847
 
        {"cumprod", (PyCFunction)array_cumprod,
1848
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1849
 
        {"cumsum", (PyCFunction)array_cumsum,
1850
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1851
 
        {"diagonal", (PyCFunction)array_diagonal,
1852
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1853
 
        {"fill", (PyCFunction)array_fill,
1854
 
            METH_VARARGS, NULL},
1855
 
        {"flatten", (PyCFunction)array_flatten,
1856
 
            METH_VARARGS, NULL},
1857
 
        {"getfield", (PyCFunction)array_getfield,
1858
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1859
 
        {"item", (PyCFunction)array_toscalar,
1860
 
            METH_VARARGS, NULL},
1861
 
        {"itemset", (PyCFunction) array_setscalar,
1862
 
            METH_VARARGS, NULL},
1863
 
        {"max", (PyCFunction)array_max,
1864
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1865
 
        {"mean", (PyCFunction)array_mean,
1866
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1867
 
        {"min", (PyCFunction)array_min,
1868
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1869
 
        {"newbyteorder", (PyCFunction)array_newbyteorder,
1870
 
            METH_VARARGS, NULL},
1871
 
        {"nonzero", (PyCFunction)array_nonzero,
1872
 
            METH_VARARGS, NULL},
1873
 
        {"prod", (PyCFunction)array_prod,
1874
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1875
 
        {"ptp", (PyCFunction)array_ptp,
1876
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1877
 
        {"put", (PyCFunction)array_put,
1878
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1879
 
        {"ravel", (PyCFunction)array_ravel,
1880
 
            METH_VARARGS, NULL},
1881
 
        {"repeat", (PyCFunction)array_repeat,
1882
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1883
 
        {"reshape", (PyCFunction)array_reshape,
1884
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1885
 
        {"resize", (PyCFunction)array_resize,
1886
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1887
 
        {"round", (PyCFunction)array_round,
1888
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1889
 
        {"searchsorted", (PyCFunction)array_searchsorted,
1890
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1891
 
        {"setfield", (PyCFunction)array_setfield,
1892
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1893
 
        {"setflags", (PyCFunction)array_setflags,
1894
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1895
 
        {"sort", (PyCFunction)array_sort,
1896
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1897
 
        {"squeeze", (PyCFunction)array_squeeze,
1898
 
            METH_VARARGS, NULL},
1899
 
        {"std", (PyCFunction)array_stddev,
1900
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1901
 
        {"sum", (PyCFunction)array_sum,
1902
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1903
 
        {"swapaxes", (PyCFunction)array_swapaxes,
1904
 
            METH_VARARGS, NULL},
1905
 
        {"take", (PyCFunction)array_take,
1906
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1907
 
        {"tofile", (PyCFunction)array_tofile,
1908
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1909
 
        {"tolist", (PyCFunction)array_tolist,
1910
 
            METH_VARARGS, NULL},
1911
 
        {"tostring", (PyCFunction)array_tostring,
1912
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1913
 
        {"trace", (PyCFunction)array_trace,
1914
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1915
 
        {"transpose", (PyCFunction)array_transpose,
1916
 
            METH_VARARGS, NULL},
1917
 
        {"var", (PyCFunction)array_variance,
1918
 
            METH_VARARGS | METH_KEYWORDS, NULL},
1919
 
        {"view", (PyCFunction)array_view,
1920
 
            METH_VARARGS, NULL},
1921
 
        {NULL,          NULL}           /* sentinel */
 
1900
    /* for subtypes */
 
1901
    {"__array__", (PyCFunction)array_getarray,
 
1902
         METH_VARARGS, NULL},
 
1903
    {"__array_wrap__", (PyCFunction)array_wraparray,
 
1904
         METH_VARARGS, NULL},
 
1905
 
 
1906
    /* for the copy module */
 
1907
    {"__copy__", (PyCFunction)array_copy,
 
1908
         METH_VARARGS, NULL},
 
1909
    {"__deepcopy__", (PyCFunction)array_deepcopy,
 
1910
         METH_VARARGS, NULL},
 
1911
 
 
1912
    /* for Pickling */
 
1913
    {"__reduce__", (PyCFunction) array_reduce,
 
1914
         METH_VARARGS, NULL},
 
1915
    {"__setstate__", (PyCFunction) array_setstate,
 
1916
         METH_VARARGS, NULL},
 
1917
    {"dumps", (PyCFunction) array_dumps,
 
1918
         METH_VARARGS, NULL},
 
1919
    {"dump", (PyCFunction) array_dump,
 
1920
         METH_VARARGS, NULL},
 
1921
 
 
1922
    /* Original and Extended methods added 2005 */
 
1923
    {"all", (PyCFunction)array_all,
 
1924
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1925
    {"any", (PyCFunction)array_any,
 
1926
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1927
    {"argmax", (PyCFunction)array_argmax,
 
1928
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1929
    {"argmin", (PyCFunction)array_argmin,
 
1930
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1931
    {"argsort", (PyCFunction)array_argsort,
 
1932
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1933
    {"astype", (PyCFunction)array_cast,
 
1934
         METH_VARARGS, NULL},
 
1935
    {"byteswap", (PyCFunction)array_byteswap,
 
1936
         METH_VARARGS, NULL},
 
1937
    {"choose", (PyCFunction)array_choose,
 
1938
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1939
    {"clip", (PyCFunction)array_clip,
 
1940
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1941
    {"compress", (PyCFunction)array_compress,
 
1942
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1943
    {"conj", (PyCFunction)array_conjugate,
 
1944
         METH_VARARGS, NULL},
 
1945
    {"conjugate", (PyCFunction)array_conjugate,
 
1946
         METH_VARARGS, NULL},
 
1947
    {"copy", (PyCFunction)array_copy,
 
1948
         METH_VARARGS, NULL},
 
1949
    {"cumprod", (PyCFunction)array_cumprod,
 
1950
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1951
    {"cumsum", (PyCFunction)array_cumsum,
 
1952
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1953
    {"diagonal", (PyCFunction)array_diagonal,
 
1954
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1955
    {"fill", (PyCFunction)array_fill,
 
1956
         METH_VARARGS, NULL},
 
1957
    {"flatten", (PyCFunction)array_flatten,
 
1958
         METH_VARARGS, NULL},
 
1959
    {"getfield", (PyCFunction)array_getfield,
 
1960
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1961
    {"item", (PyCFunction)array_toscalar,
 
1962
         METH_VARARGS, NULL},
 
1963
    {"itemset", (PyCFunction) array_setscalar,
 
1964
         METH_VARARGS, NULL},
 
1965
    {"max", (PyCFunction)array_max,
 
1966
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1967
    {"mean", (PyCFunction)array_mean,
 
1968
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1969
    {"min", (PyCFunction)array_min,
 
1970
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1971
    {"newbyteorder", (PyCFunction)array_newbyteorder,
 
1972
         METH_VARARGS, NULL},
 
1973
    {"nonzero", (PyCFunction)array_nonzero,
 
1974
         METH_VARARGS, NULL},
 
1975
    {"prod", (PyCFunction)array_prod,
 
1976
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1977
    {"ptp", (PyCFunction)array_ptp,
 
1978
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1979
    {"put", (PyCFunction)array_put,
 
1980
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1981
    {"ravel", (PyCFunction)array_ravel,
 
1982
         METH_VARARGS, NULL},
 
1983
    {"repeat", (PyCFunction)array_repeat,
 
1984
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1985
    {"reshape", (PyCFunction)array_reshape,
 
1986
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1987
    {"resize", (PyCFunction)array_resize,
 
1988
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1989
    {"round", (PyCFunction)array_round,
 
1990
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1991
    {"searchsorted", (PyCFunction)array_searchsorted,
 
1992
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1993
    {"setfield", (PyCFunction)array_setfield,
 
1994
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1995
    {"setflags", (PyCFunction)array_setflags,
 
1996
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1997
    {"sort", (PyCFunction)array_sort,
 
1998
         METH_VARARGS | METH_KEYWORDS, NULL},
 
1999
    {"squeeze", (PyCFunction)array_squeeze,
 
2000
         METH_VARARGS, NULL},
 
2001
    {"std", (PyCFunction)array_stddev,
 
2002
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2003
    {"sum", (PyCFunction)array_sum,
 
2004
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2005
    {"swapaxes", (PyCFunction)array_swapaxes,
 
2006
         METH_VARARGS, NULL},
 
2007
    {"take", (PyCFunction)array_take,
 
2008
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2009
    {"tofile", (PyCFunction)array_tofile,
 
2010
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2011
    {"tolist", (PyCFunction)array_tolist,
 
2012
         METH_VARARGS, NULL},
 
2013
    {"tostring", (PyCFunction)array_tostring,
 
2014
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2015
    {"trace", (PyCFunction)array_trace,
 
2016
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2017
    {"transpose", (PyCFunction)array_transpose,
 
2018
         METH_VARARGS, NULL},
 
2019
    {"var", (PyCFunction)array_variance,
 
2020
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2021
    {"view", (PyCFunction)array_view,
 
2022
         METH_VARARGS | METH_KEYWORDS, NULL},
 
2023
    {NULL, NULL}           /* sentinel */
1922
2024
};
1923
2025
 
1924
2026
#undef _ARET