Dear pythoneers,
In Chapter 12 ("Writing a C extension") in the Numerical Python manual, in the
first example PyArray_ContiguousFromObject is used together with PyDECREF, while
in the last example PyArray_ContiguousFromObject is used but the arrays are not
PyDECREF'ed before the function returns. From Chapter 13 ("C API Reference") it
seems that a PyDECREF is needed in both cases. Or am I missing something here?
Is PyDECREF not needed for some reason in the last example?
Thanks in advance,
Michiel de Hoon
U Tokyo
First example:
==============
static PyObject *
trace(PyObject *self, PyObject *args)
{
PyObject *input;
PyArrayObject *array;
double sum;
int i, n;
if (!PyArg_ParseTuple(args, "O", &input))
return NULL;
array = (PyArrayObject *)
PyArray_ContiguousFromObject(input, PyArray_DOUBLE, 2, 2);
if (array == NULL)
return NULL;
n = array->dimensions[0];
if (n > array->dimensions[1])
n = array->dimensions[1];
sum = 0.;
for (i = 0; i < n; i++)
sum += *(double *)(array->data + i*array->strides[0] + i*array->strides[1]);
Py_DECREF(array);
return PyFloat_FromDouble(sum);
}
Last example:
=============
static PyObject *
matrix_vector(PyObject *self, PyObject *args)
{
PyObject *input1, *input2;
PyArrayObject *matrix, *vector, *result;
int dimensions[1];
double factor[1];
double real_zero[1] = {0.};
long int_one[1] = {1};
long dim0[1], dim1[1];
extern dgemv_(char *trans, long *m, long *n,
double *alpha, double *a, long *lda,
double *x, long *incx,
double *beta, double *Y, long *incy);
if (!PyArg_ParseTuple(args, "dOO", factor, &input1, &input2))
return NULL;
matrix = (PyArrayObject *)
PyArray_ContiguousFromObject(input1, PyArray_DOUBLE, 2, 2);
if (matrix == NULL)
return NULL;
vector = (PyArrayObject *)
PyArray_ContiguousFromObject(input2, PyArray_DOUBLE, 1, 1);
if (vector == NULL)
return NULL;
if (matrix->dimensions[1] != vector->dimensions[0]) {
PyErr_SetString(PyExc_ValueError,
"array dimensions are not compatible");
return NULL;
}
dimensions[0] = matrix->dimensions[0];
result = (PyArrayObject *)PyArray_FromDims(1, dimensions, PyArray_DOUBLE);
if (result == NULL)
return NULL;
dim0[0] = (long)matrix->dimensions[0];
dim1[0] = (long)matrix->dimensions[1];
dgemv_("T", dim1, dim0, factor, (double *)matrix->data, dim1,
(double *)vector->data, int_one,
real_zero, (double *)result->data, int_one);
return PyArray_Return(result);
}
--
Michiel de Hoon, Assistant Professor
University of Tokyo, Institute of Medical Science
Human Genome Center
4-6-1 Shirokane-dai, Minato-ku
Tokyo 108-8639
Japan
http://bonsai.ims.u-tokyo.ac.jp/~mdehoon

Hi,
I have a question to Numeric/Numarray experts:
Let A be a contiguous one or two-dimensional array with shape (n,) or
(m,n), respectively. Let k be a positive integer less than n.
Reference count for A is 1. The objective is to execute A=A[:k]
or A=A[:,:k], respctively, in C.
Q: Is it safe to resize the array A by simply resetting its dimensions,
that is,
A->dimensions[0] = k;
or
A->dimensions[1] = k;
if A->nd is 1 or 2, respectively, in C? This appears to work but may be
there are some corner cases that I have overlooked.
Would that work also when using Numarray?
If not, how to resize (preferably in-situ) an array in C under the
conditions given above?
TIA,
Pearu

Hi Alan,
Yes, thank you. Shared elements is the out come I
need. The problem I have now is how to return the
shared elements back into the numarray.array([])
constructor.
Numarray doesn't recognize the sets.Set([])
constructor as a python numeric type. I actually don't
see any functionality/methodology to convert a
sets.Set([]) back into any of the sequence types.
I am also don't understand why this ability, to
retrieve shared elements of two unequal arrays, is not
an inherent numarry function/method.
Thank you for your initial direction to the sets
module,
Kevin
__________________________________
Do you Yahoo!?
The all-new My Yahoo! - Get yours free!
http://my.yahoo.com

Hello,
After spending a great deal of time researching how to
do something as trivial as an equality test for
arrrays of unequal lengths, I've given up to defer to
your wisdom.
a = array([19289 19291 19299 ..., 21966 21978 21990])
b = array([ 0 1 2 ..., 95956 95717 95768])
Evidently, where(equal... has no merit, as well as all
the other expressions I've tried.
Thank you for your help :)
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

How are all of you, if you are at all, managing to keep
ATLAS/numarray/Numeric/Scipy/etc up to date on a heterogenous network?
I have to do this, really, and would like to do it with ATLAS support,
but with Athlon XP, Athlon MP, P4's and a couple of older P3's in house,
it is becoming both difficult to automate this and time consuming to do
it manually.
Not strictly a numpy question, of course, but I felt it likely others on
this list have faced this problem.
--
Stephen Walton, Professor, Dept. of Physics & Astronomy, Cal State Northridge
stephen.walton(a)csun.edu

Hi
I only hope that you guys on this list can help me, for I didn't find a
list specified on numarray.
Well, may problem is that I want to write an algorithm where I combine
amplitude data from one 3D-image volume with phase data from another.
(Actually they are identical except for one small correction, that's
why this should be legal.) Numarray only offers 1D and 2D FFTs. I could
possibly implement a 3D transform, but if somebody already has written
something like this, it would be very nice if one could point me on an
URL for this.
Thanks a lot in advance.
Cheers
Christian Meesters

I am proud to announce PyQwt-4.1.
What is PyQwt?
- it is a set of Python bindings for the Qwt C++ class library which
extends the Qt framework with widgets for scientific, engineering
and financial applications. It provides a widget to plot
2-dimensional data and various widgets to display and control
bounded or unbounded floating point values.
- it requires and extends PyQt, a set of Python bindings for Qt.
- it really shines with either Numeric, numarray or both. Numeric
and/or numarray extend the Python language with new data types which
turn Python into an ideal system for numerical computing and
experimentation, better than MatLab and IDL.
- it supports the use of PyQt, Qt, Qwt, the Numerical Python extensions
(either Numeric, or numarray or both) and optionally SciPy in a GUI
Python application or in an interactive Python session.
- it runs on POSIX, MacOS/X and Windows (any operating system supported
by Qt and Python).
The home page of PyQwt is http://pyqwt.sourceforge.net.
Main changes in PyQwt-4.1:
1. supports PyQt-3.13, -3.12, PyQt-3.11, and PyQt-3.10.
2. supports sip-4.1.1, -4.1, -4.0, sip-3.11, and sip-3.10.
3. supports Qt-3.3.3 downto -2.3.0.
4. based on Qwt-4.2.0.
5. either links with a shared (dll) Qwt-4.2.0 library or links
statically with a slightly patched internal version of Qwt-4.2.0.
Have fun -- Gerard Vermeulen

Hello,
I am using swig to wrap some numerical code. I have managed to make swig
understand and convert between numarrays and C arrays. However if I use
NA_IoArray to make the convertion and pass an integer array when a
double array is expected the changes are not copied back to the original
array. Let me give a "minimal" example.
When I want to wrap the following function:
void inc(int len, double *v) {
int i;
for (i = 0; i < len; i++) v[i]++;
}
Swig generates the following code, that looks right to me.
static PyObject *_wrap_inc(PyObject *self, PyObject *args) {
PyObject *resultobj;
int arg1 ;
double *arg2 = (double *) 0 ;
PyArrayObject *NAimg2 = NULL ;
PyObject * obj1 = 0 ;
if(!PyArg_ParseTuple(args,(char *)"iO:inc",&arg1,&obj1)) goto fail;
{
PyArrayObject *NAimg2 = NA_IoArray(obj1, tFloat64,
NUM_C_ARRAY);
if (!NAimg2) {
printf("**** no (double) numarray *****\n");
SWIG_exception(SWIG_RuntimeError, "No double Array Found");
return 0;
}
arg2 = (double *) NA_OFFSETDATA(NAimg2);
}
inc(arg1,arg2);
Py_INCREF(Py_None); resultobj = Py_None;
{
Py_XDECREF(NAimg2);
}
return resultobj;
fail:
{
Py_XDECREF(NAimg2);
}
return NULL;
}
However if I run the following python code:
>>> import test_na_ioarray
>>> from numarray import *
>>> v = zeros((5,))
>>> test_na_ioarray(5, v)
>>> print v
[0 0 0 0 0]
Shouldn't the resultant array be full of ones (when casting the Float64
array of ones to int)?
Thanks,
Paulo
Obs: The code runs fine if the original vector is already a Float64
array.