473,791 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(PyObj ect *self, PyObject *args)
{
PyArrayObject *p;
int ok;

/* get the array */
ok = PyArg_ParseTupl e(args,"O!", &PyArray_Typ e, &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(scorem at)
print scoremat[5][5]

Thank you,
Ognen
Jul 18 '05 #1
3 1394
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_GetIte m (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.f reeshell.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,f loat) = atof(row[0]);
get1d(v,count,i nt) = 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.b sd.uchicago.edu > wrote:
>> "Ognen" == Ognen Duzlevski <ma****@norge.f reeshell.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
1797
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
1293
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. from RandomArray import random from Numeric import sum n = 1000000 m = 10 for i in range(m):
7
3010
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 you are new to Numerical Python, please use Numarray.". It's not that easy, dangit. The download page for numpy does not contain a 2.4 version of Numeric, and I suspect they do not intend to release one, because there IS a 2.4 version of...
0
1529
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 woes: Numeric, gnuplot, and Python 2.4 Date: 2004-12-11 18:45:10 PST > Here's my sitch: > > I use gnuplot.py at work, platform Win32. > I want to upgrade to Python 2.4.
2
1834
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 workstation cluster with 64-Bit Opteron CPUs - I have no more than three days to do this. Compiling Python and running Numeric has been no problem at all. However, I get an error message when accessing the data pickled before. (I can load it...
10
2248
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 re-implementation of an older Python array module called Numeric" http://www.stsci.edu/resources/software_hardware/numarray
15
1904
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 PIL-1.1.5.win32-py2.4.exe Numeric-24.2.win32-py2.4.exe Wayne T. Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
11
2585
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 install Password: running install
0
2120
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 studied SWIG and Python/C API a bit. I was able to pass numeric array (Numeric Python) into the C function and access/change these arrays. Problem I am having is that I couldn't quite get the array converted back to some PyObject or something that...
5
1060
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 on 32-bit Windows XP:
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.