Scipy/numpy 2D array: how to define with a function?

Click For Summary

Discussion Overview

The discussion revolves around methods for creating a 2D array in Python using SciPy or NumPy, specifically focusing on defining the elements of the array through a function F(i,j). Participants explore different approaches to achieve this without relying heavily on for loops.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant presents an initial approach using nested for loops to populate a list with values from the function F(i,j) and then reshaping it into a 2D array.
  • Another participant suggests using numpy.empty to create an uninitialized array and filling it with values from F(i,j) using nested loops.
  • A different method is proposed using numpy.meshgrid to generate coordinate matrices from the ranges, allowing for a more concise assignment of values from F(i,j) directly to the array.
  • One participant notes that using range(n) is equivalent to range(0,n), suggesting a slight simplification in the code.

Areas of Agreement / Disagreement

Participants generally agree on the methods presented for creating the 2D array, with no significant disagreement noted. However, there are multiple approaches discussed without a consensus on a single "best" method.

Contextual Notes

Some methods rely on the assumption that the function F(i,j) can accept array inputs, which may not hold for all functions. The discussion does not resolve the efficiency or performance implications of the different approaches.

Who May Find This Useful

Readers interested in Python programming, particularly those working with NumPy or SciPy for numerical computations, may find the discussion relevant.

Dunhausen
Messages
30
Reaction score
0
I want to make a matrix of values as so:

F(0,0) . . . F(1,n)
.
.
.
F(n,1) . . . F(n,n)

I could of course do it like this

Code:
list=[]
for i in range(0,n):
     for j in range(0,n):
     list.append(F(i,j))
a=array(list)
a.reshape(n,n)

But I am curious if there is a more elegant way to build an array with the rule Element i,j = F(i,j) ?

When working with 1-d arrays, I am accustomed to their savvy nature eliminating all need for 'for loops' in my code.
 
Last edited:
Technology news on Phys.org
Code:
a = numpy.empty((n+1,n+1))
for i in range(0,n):
    for j in range(0,n):
        a[i,j] = F(i,j)

or
Code:
i,j = numpy.meshgrid(range(0,n), range(0,n))
a = F(i,j)
 
Last edited:
That is excellent! Thank you. :)
 
Also, didn't have chance to edit, but range(n) generates the same list as range(0,n), if you want to be slightly more concise.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 14 ·
Replies
14
Views
5K