473,473 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Numeric N-dimensional array initialization

TG
Hi there !

I'm just starting to use Numeric here, and I'm wondering : how can I
efficiently initialize every values of a N-dimensional array, given I
don't know the number of dimensions ?

I'm looking for something like a map function, or a way to conveniently
iterate through the whole N-array, but I didn't find anything ... yet.
If anyone has a clue, I'm listening.

Thanks

Jun 22 '06 #1
4 2292
TG
I tried to use Numeric.fromfunction, but there seems to be a problem :

the function called must have the right number of args (hint : the
number of dimensions, which I don't know). So i tried to use a function
like :

def myfunc(*args, **kw):
return 0

and then i get :
Numeric.fromfunction(myfunc,(5,5))

0

I'm a bit puzzled here

Jun 22 '06 #2
TG wrote:
Hi there !

I'm just starting to use Numeric here, and I'm wondering : how can I
efficiently initialize every values of a N-dimensional array, given I
don't know the number of dimensions ?

I'm looking for something like a map function, or a way to conveniently
iterate through the whole N-array, but I didn't find anything ... yet.
If anyone has a clue, I'm listening.


Since you're just starting, you should know that Numeric is no longer being
developed. The actively developed version is numpy:

http://www.scipy.org/NumPy

You will want to ask numpy questions on the numpy-discussion mailing list. The
answers one gets here tend to be hit or miss.

https://lists.sourceforge.net/lists/...mpy-discussion

As to your actual question, I'm not entirely sure what you are asking for, but
you should look at the .flat attribute.
In [5]: from numpy import *

In [6]: a = empty((2, 3))

In [7]: a.flat[:] = 10

In [8]: a
Out[8]:
array([[10, 10, 10],
[10, 10, 10]])

# Note, in numpy, .flat is not a real array although it should be usable in most
# places that need an array. However, unlike Numeric, it can always be used
# even if the array is not contiguous. If you need a real array, use the
# .ravel() method, but know that it will make a copy if the array is not
# contiguous (for example, if it is the result of a .transpose() call).
In [9]: a.flat
Out[9]: <numpy.flatiter object at 0x196a800>

In [10]: a.flat = arange(10)

In [11]: a
Out[11]:
array([[0, 1, 2],
[3, 4, 5]])

In [12]: for i in a.flat:
....: print i
....:
....:
0
1
2
3
4
5

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 22 '06 #3
TG wrote:
I tried to use Numeric.fromfunction, but there seems to be a problem :

the function called must have the right number of args (hint : the
number of dimensions, which I don't know). So i tried to use a function
like :

def myfunc(*args, **kw):
return 0

and then i get :
Numeric.fromfunction(myfunc,(5,5))

0

I'm a bit puzzled here


In [24]: def myfunc(*args):
....: print args
....:
....:

In [26]: fromfunction(myfunc, (2, 2))
(array([[0, 0],
[1, 1]]),
array([[0, 1],
[0, 1]]))

fromfunction() does not iterate over the possible indices and call the function
with scalar arguments to get a scalar return value. It generates N arrays with
index values in them and calls the function once with those arrays. The return
value should be another array.

If you actually just want 0s:

In [27]: zeros((5, 5))
Out[27]:
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])

If you want 1s:

In [28]: ones((5, 5))
Out[28]:
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])

If you just want an array as fast as possible because you are going to fill in
values later:

In [29]: empty((5, 5))
Out[29]:
array([[ 13691, 0, 0, 2883587, 3],
[ 3, 0, 828189706, 6, 0],
[ 0, 9, 10, 828202281, 0],
[ 7, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]])
If you have more complicated needs, we can talk about them on numpy-discussion.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jun 22 '06 #4
TG
Thanks for your precious advices. The flat iterator is definitely what
i need.

Jun 22 '06 #5

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

Similar topics

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...
4
by: Chris Weisiger | last post by:
I'm trying to install numeric on my MacOS X box using Darwin, with the eventual goal of satisfying all of PyGame's dependencies so I can finally start working on my semester project. I would be...
4
by: Gezeala 'Eyah' Bacu\361o II | last post by:
hey guys..need your help on this.. i have a plpgsql function where in i compute numeric values for my php scripts.. my problem is my function just won't round some numbers properly.. what i...
5
by: David Garamond | last post by:
In Interbase and Firebird, NUMERIC is implemented as 64-bit integer. This limits the range to NUMERIC(18, *) but for many uses that's adequate. And moreover it's fast and efficient. Is there a...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
6
by: M.A. Oude Kotte | last post by:
Hi All, I hope this is the correct mailing list for this question. But neither postgresql.org nor google could help me out on this subject. I did find one disturbing topic on the mailing list...
0
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
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...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
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...
1
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.