File: qwt_numerical_interface.cpp

package info (click to toggle)
pyqwt5 5.1.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,688 kB
  • ctags: 39,112
  • sloc: cpp: 32,930; python: 7,948; makefile: 101; sh: 32
file content (323 lines) | stat: -rw-r--r-- 9,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// The code for the interface PyQwt <-> Numerical Python extensions.
//
// Copyright (C) 2001-2008 Gerard Vermeulen
// Copyright (C) 2000 Mark Colclough
//
// This file is part of PyQwt.
//
// PyQwt is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PyQwt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with PyQwt; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
// In addition, as a special exception, Gerard Vermeulen gives permission
// to link PyQwt dynamically with non-free versions of Qt and PyQt,
// and to distribute PyQwt in this form, provided that equally powerful
// versions of Qt and PyQt have been released under the terms of the GNU
// General Public License.
//
// If PyQwt is dynamically linked with non-free versions of Qt and PyQt,
// PyQwt becomes a free plug-in for a non-free program.


#include <qwt_ndarray.h>
#include <qwt_numerical_interface.h>
#include <qwt_numarray.h>
#include <qwt_numeric.h>
#include <qwt_numpy.h>


static int try_PySequence_to_QwtArray(PyObject *in, QwtArray<double> &out)
{
    if (!PyList_Check(in) && !PyTuple_Check(in))
        return 0;

    // MSVC-6.0 chokes on passing an uint in QwtArray<double>::operator[](int)
    int size = PySequence_Size(in);
    out.resize(size);

    for (int i=0; i<size; i++) {
        PyObject *element = PySequence_Fast_GET_ITEM(in, i);
        if (PyFloat_Check(element)) {
            out[i] = PyFloat_AsDouble(element);
        } else if (PyInt_Check(element)) {
            out[i] = double(PyInt_AsLong(element));
        } else if (PyLong_Check(element)) {
            out[i] = PyLong_AsDouble(element);
        } else {
            PyErr_SetString(
                PyExc_TypeError,
                "The sequence may only contain float, int, or long types.");

            return -1;
        }
    }

    return 1;
}

int try_PyObject_to_QwtArray(PyObject *in, QwtArray<double> &out)
{
    int result;

#ifdef HAS_NUMPY
    if ((result = try_NumPyArray_to_QwtArray(in, out)))
        return result;
#endif

#ifdef HAS_NUMERIC
    if ((result = try_NumericArray_to_QwtArray(in, out)))
        return result;
#endif

#ifdef HAS_NUMARRAY
    if ((result = try_NumarrayArray_to_QwtArray(in, out)))
        return result;
#endif

    if ((result = try_NDArray_to_QwtArray(in, out)))
        return result;

    if ((result = try_PySequence_to_QwtArray(in, out)))
        return result;

    PyErr_SetString(PyExc_TypeError, "expected is\n"
                    "(*) a list or tuple of Python numbers.\n"
                    "(*) an array with the N-D array interface.\n"
#ifdef HAS_NUMPY
                    "(*) a NumPy array coercible to PyArray_DOUBLE.\n"
#else
                    "(!) rebuild PyQwt to support NumPy arrays.\n"
#endif
#ifdef HAS_NUMERIC
                    "(*) a Numeric array coercible to PyArray_DOUBLE.\n"
#else
                    "(!) rebuild PyQwt to support Numeric arrays.\n"
#endif
#ifdef HAS_NUMARRAY
                    "(*) a numarray array coercible to PyArray_DOUBLE.\n"
#else
                    "(!) rebuild PyQwt to support numarray arrays.\n"
#endif
        );

    return -1;
}


static int try_PySequence_to_QwtArray(PyObject *in, QwtArray<int> &out)
{
    if (!PyList_Check(in) && !PyTuple_Check(in))
        return 0;

    // MSVC-6.0 chokes on passing an uint in QwtArray<double>::operator[](int)
    int size = PySequence_Size(in);
    out.resize(size);

    for (int i=0; i<size; i++) {
        PyObject *element = PySequence_Fast_GET_ITEM(in, i);
        if (PyInt_Check(element)) {
            out[i] = int(PyInt_AsLong(element));
        } else if (PyFloat_Check(element)) {
            out[i] = int(PyFloat_AsDouble(element));
        } else if (PyLong_Check(element)) {
            out[i] = int(PyLong_AsLong(element));
        } else {
            PyErr_SetString(
                PyExc_TypeError,
                "The sequence may only contain float, int, or long types.");

            return -1;
        }
    }

    return 1;
}


int try_PyObject_to_QwtArray(PyObject *in, QwtArray<int> &out)
{
    int result;

#ifdef HAS_NUMPY
    if ((result = try_NumPyArray_to_QwtArray(in, out)))
        return result;
#endif

#ifdef HAS_NUMERIC
    if ((result = try_NumericArray_to_QwtArray(in, out)))
        return result;
#endif

#ifdef HAS_NUMARRAY
    if ((result = try_NumarrayArray_to_QwtArray(in, out)))
        return result;
#endif

    if ((result = try_NDArray_to_QwtArray(in, out)))
        return result;

    if ((result = try_PySequence_to_QwtArray(in, out)))
        return result;

    PyErr_SetString(PyExc_TypeError, "expected is\n"
                    "(*) a list or tuple of Python numbers.\n"
                    "(*) an array with the N-D array interface.\n"
#ifdef HAS_NUMPY
                    "(*) a NumPy array coercible to PyArray_INT.\n"
#else
                    "(!) rebuild PyQwt to support NumPy arrays.\n"
#endif
#ifdef HAS_NUMERIC
                    "(*) a Numeric array coercible to PyArray_INT.\n"
#else
                    "(!) rebuild PyQwt to support Numeric arrays.\n"
#endif
#ifdef HAS_NUMARRAY
                    "(*) a numarray array coercible to PyArray_INT.\n"
#else
                    "(!) rebuild PyQwt to support numarray arrays.\n"
#endif
        );

    return -1;
}


static int try_PySequence_to_QwtArray(PyObject *in, QwtArray<long> &out)
{
    if (!PyList_Check(in) && !PyTuple_Check(in))
        return 0;

    // MSVC-6.0 chokes on passing an uint in QwtArray<double>::operator[](int)
    int size = PySequence_Size(in);
    out.resize(size);

    for (int i=0; i<size; i++) {
        PyObject *element = PySequence_Fast_GET_ITEM(in, i);
        if (PyInt_Check(element)) {
            out[i] = PyInt_AsLong(element);
        } else if (PyFloat_Check(element)) {
            out[i] = long(PyFloat_AsDouble(element));
        } else if (PyLong_Check(element)) {
            out[i] = PyLong_AsLong(element);
        } else {
            PyErr_SetString(
                PyExc_TypeError,
                "The sequence may only contain float, int, or long types.");

            return -1;
        }
    }

    return 1;
}


int try_PyObject_to_QwtArray(PyObject *in, QwtArray<long> &out)
{
    int result;

#ifdef HAS_NUMPY
    if ((result = try_NumPyArray_to_QwtArray(in, out)))
        return result;
#endif

#ifdef HAS_NUMERIC
    if ((result = try_NumericArray_to_QwtArray(in, out)))
        return result;
#endif

#ifdef HAS_NUMARRAY
    if ((result = try_NumarrayArray_to_QwtArray(in, out)))
        return result;
#endif

    if ((result = try_NDArray_to_QwtArray(in, out)))
        return result;

    if ((result = try_PySequence_to_QwtArray(in, out)))
        return result;

    PyErr_SetString(PyExc_TypeError, "expected is\n"
                    "(*) a list or tuple of Python numbers.\n"
                    "(*) an array with the N-D array interface.\n"
#ifdef HAS_NUMPY
                    "(*) a NumPy array coercible to PyArray_INT.\n"
#else
                    "(!) rebuild PyQwt to support NumPy arrays.\n"
#endif
#ifdef HAS_NUMERIC
                    "(*) a Numeric array coercible to PyArray_INT.\n"
#else
                    "(!) rebuild PyQwt to support Numeric arrays.\n"
#endif
#ifdef HAS_NUMARRAY
                    "(*) a numarray array coercible to PyArray_INT.\n"
#else
                    "(!) rebuild PyQwt to support numarray arrays.\n"
#endif
        );

    return -1;
}


int try_PyObject_to_QImage(PyObject *in, QImage **out)
{
    int result;

#ifdef HAS_NUMPY
    if ((result = try_NumPyArray_to_QImage(in, out)))
        return result;
#endif

#ifdef HAS_NUMERIC
    if ((result = try_NumericArray_to_QImage(in, out)))
        return result;
#endif

#ifdef HAS_NUMARRAY
    if ((result = try_NumarrayArray_to_QImage(in, out)))
        return result;
#endif

    if ((result = try_NDArray_to_QImage(in, out)))
        return result;

    PyErr_SetString(PyExc_TypeError, "expected is\n"
                    "(*) an array with the N-D array interface.\n"
#ifdef HAS_NUMPY
                    "(*) a NumPy array.\n"
#else
                    "(!) rebuild PyQwt to support NumPy arrays.\n"
#endif
#ifdef HAS_NUMERIC
                    "(*) a Numeric array.\n"
#else
                    "(!) rebuild PyQwt to support Numeric arrays.\n"
#endif
#ifdef HAS_NUMARRAY
                    "(*) a numarray array.\n"
#else
                    "(!) rebuild PyQwt to support numarray arrays.\n"
#endif
        );

    return -1;
}

// Local Variables:
// mode: C++
// c-file-style: "stroustrup"
// indent-tabs-mode: nil
// End: