473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Numeric Python and C

Hi,
can someone explain how to change a value within an array from a C function?

For example, I have the following array:

scoremat = zeros((10,10))

and the following C function:

PyObject *DynAlign(PyObject *self, PyObject *args)
{
PyArrayObject *p;
int ok;

/* get the array */
ok = PyArg_ParseTuple(args,"O!", &PyArray_Type, &p);
/* this gets a value p[5][5] */
v55 = *(double *)p->data + 5*p->strides[0] + 5*p->strides[1]);
}

How to I change the value at p[5][5] and retain it in the same array after function exit?

So, from Python I might have:

from Numeric import *

scoremat = zeros((10,10))
DynAlign(scoremat)
print scoremat[5][5]

Thank you,
Ognen
Jul 18 '05 #1
3 1382
On Wed, 14 Jan 2004 19:17:55 +0000, Ognen Duzlevski wrote:
Hi,
can someone explain how to change a value within an array from a C
function?


What about using PyObject_GetItem (is that what it's called?)

I know numarray uses functions for this:

PyArrayObject* NA_InputArray( PyObject *numarray, NumarrayType t, int requires)
The purpose of NA_InputArray is to transfer array data from Pythonto C.

PyArrayObject* NA_OutputArray( PyObject *numarray, NumarrayType t, int requires)
The purpose of NA_OutputArray is to transfer data from C to Python. Practically speaking, the output numarray must be a PyArrayObject, and cannot be an arbitrary Python sequence.

PyArrayObject* NA_IoArray( PyObject *numarray, NumarrayType t, int requires)
NA_IoArray has fully bidirectional data transfer, creating the illusion of call-by-reference.
Simon.

Jul 18 '05 #2
>>>>> "Ognen" == Ognen Duzlevski <ma****@norge.freeshell.org> writes:

Ognen> Hi, can someone explain how to change a value within an
Ognen> array from a C function?

Warning: I am not a Numeric C API guru.

For 1D arrays I use the following macro

// get x[i] from a 1d array of type xtype
#define get1d(x,i,xtype) \
*(xtype *)(x->data+i*x->strides[0])
You can assign to the return value of this, as in

count = 0;
while ((row = mysql_fetch_row(res)) != NULL) {
get1d(p,count,float) = atof(row[0]);
get1d(v,count,int) = atoi(row[1]);
++count;
}

where p and v are 1D numeric arrays.

You'll have to do a bit more work for 2D arrays, but this may speed
you on your way....

JDH

Jul 18 '05 #3
John Hunter <jd******@ace.bsd.uchicago.edu> wrote:
>> "Ognen" == Ognen Duzlevski <ma****@norge.freeshell.org> writes:
Ognen> Hi, can someone explain how to change a value within an
Ognen> array from a C function? Warning: I am not a Numeric C API guru. For 1D arrays I use the following macro [snipped] You'll have to do a bit more work for 2D arrays, but this may speed
you on your way....


Hi, for 2D arrays it is much similar:

get(ndx1,ndx2): array->data + ndx1*array->strides[0] + ndx2*array->strides[1]
set(ndx1,ndx2): I use memcpy(same as get, &val, sizeof(val);

Thanks,
Ognen
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil | last post by:
Hi, I don't understand this strange behaviour: I compile this code : #include <Python.h> #include"Numeric/arrayobject.h" static PyObject *
0
by: beliavsky | last post by:
I have been looking at the speed of Python with the Numeric module by simulating some random numbers and computing some statistics. Here is the code. Line (1) is replaced as shown in the table. ...
7
by: Jive | last post by:
Here's my sitch: I use gnuplot.py at work, platform Win32. I want to upgrade to Python 2.4. Gnuplot.py uses extension module Numeric. Numeric is now "unsupported." The documentation says "If...
0
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade...
2
by: Johannes Nix |Johannes.Nix | last post by:
Hi, I have a tricky problem with Numeric. Some time ago, I have generated a huge and complex data structure, and stored it using the cPickle module. Now I want to evaluate it quickly again on a...
10
by: Bryan | last post by:
hi, what is the difference among numeric, numpy and numarray? i'm going to start using matplotlib soon and i'm not sure which one i should use. this page says, "Numarray is a...
15
by: W. Watson | last post by:
For some reason Python 2.2.4 cannot find the Numeric module. It's been suggested that I should re-install the Numeric file. How do that? Also the PIL. The three install files are: python-2.4.4.msi...
11
by: ZMY | last post by:
Dear all, I am a real newbie for both python and QNX, but I am still trying to compile Numeric-24.2 under QNX4.25 with python 2.2. I got following error message: $ sudo python setup.py...
0
by: ronysk | last post by:
Hi, I am posting here to seek for help on type conversion between Python (Numeric Python) and C. Attachment A is a math function written in C, which is called by a Python program. I had...
5
by: Benyang | last post by:
Maybe it has been reported somewhere, but it is a big surprise to me. # Try the following: import Numeric a = Numeric.ones(10) a = -1 print a It works correctly on 32-bit linux machines and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.