# Beginner help numpy 2D arrays

• 04-05-2013, 06:51 AM
anthes4
Beginner help numpy 2D arrays
Hi, my issues is with trying to work with arrays that for each elements is a tuple of 2 values.

specifically the problem is to generate a random 2-dimensional walk of 200 (but for testing say 2) steps with a max distance, then 100 (try just 2) of those walks per stage, and commencing each stage at the largest distance from the origin of the previous stage.

I can successfully generate the array of random steps and get them to return the final position (x,y) value and as well calculate the distance of them from the origin of each walk:

That's defined in these functions:

#............................................................getPositionInteger
def getPosInt(description) :
"""Asks the user to enter a positive integer"""
while askAgain == False:
try:
posInt = eval(raw_input("\n %s : " %description))
assert 0 < posInt , "Not a positive integer"
assert type(posInt) == int , "Not a positive integer"
except :
print "Input failed, not a positive integer"
return posInt
#...............................................................initialPosition
def initialPosition() :
"""Initial position of walker at the start of a random walk"""
return (0.0, 0.0)

#......................................................................distance
def distance(posA, posB) :
"""Distance between two positions"""
xi = posA[0] ; yi = posA[1]
xf = posB[0] ; yf = posB[1]
return np.sqrt((xf-xi)**2+(yf-yi)**2)

#..................................................................getPositions
def getPositions(start, nSteps, maxStep):
xArray = maxStep * np.random.random(nSteps+1)* np.cos(2.0 * np.pi * random.random())
yArray = maxStep * np.random.random(nSteps+1)* np.sin(2.0 * np.pi * random.random())
xArray[0] = start[0]
yArray[0] = start[-1]
xArray = np.cumsum(xArray)
yArray = np.cumsum(yArray)
return (xArray[-1], yArray[-1])

But I can't get the array of the final position of each walk per stage in (x,y) form per stage

Here's the main script and where I'm having trouble:

import numpy as np
import matplotlib.pylab as plt
import random
import time

MAX_STEP_SIZE = 0.90 # maximum size of a single step [m]
random.seed(12345)

#..........................................................................main
def main ():
''''''
print "RANDOM WALK IN STAGES IN TWO DIMENSIONS"
userdata = getDetails()
print "\nPlease wait while random walks are generated and analyzed..."
NUM_STAGES = userdata[0]
NUM_WALKS = userdata[1]
NUM_STEPS = userdata[2]
stageStart = initialPosition()
for stage in np.arange(NUM_STAGES):
walks = np.zeros((NUM_WALKS, NUM_WALKS), dtype=np.ndarray)
for walk in walks:
walk = getPositions(stageStart, NUM_STEPS, MAX_STEP_SIZE)
print walk
print walks

You will see I'm having trouble making a an (x,y) style array, where the [0 0] should be [0.0 , 0.0] and its printed twice and additionally, its not changing to the final position.

I really appreciate and help, advice or references you can provide.
Thanks
-Sid
• 04-05-2013, 06:52 AM
anthes4
SORRY HERES THE INDENTED CODE

Hi, my issues is with trying to work with arrays that for each elements is a tuple of 2 values.

specifically the problem is to generate a random 2-dimensional walk of 200 (but for testing say 2) steps with a max distance, then 100 (try just 2) of those walks per stage, and commencing each stage at the largest distance from the origin of the previous stage.

I can successfully generate the array of random steps and get them to return the final position (x,y) value and as well calculate the distance of them from the origin of each walk:

That's defined in these functions:

Code:

```    #............................................................getPositionInteger     def getPosInt(description) :         """Asks the user to enter a positive integer"""         askAgain = False         while askAgain == False:             try:                 posInt = eval(raw_input("\n %s :  " %description))                 assert 0 < posInt , "Not a positive integer"                 assert type(posInt) == int , "Not a positive integer"                 askAgain = True             except :                 print "Input failed, not a positive integer"         return posInt     #...............................................................initialPosition     def initialPosition() :         """Initial position of walker at the start of a random walk"""         return (0.0, 0.0)         #......................................................................distance     def distance(posA, posB) :         """Distance between two positions"""         xi = posA[0] ; yi = posA[1]         xf = posB[0] ; yf = posB[1]         return np.sqrt((xf-xi)**2+(yf-yi)**2)             #..................................................................getPositions     def getPositions(start, nSteps, maxStep):         xArray = maxStep * np.random.random(nSteps+1)* np.cos(2.0 * np.pi * random.random())         yArray = maxStep * np.random.random(nSteps+1)* np.sin(2.0 * np.pi * random.random())         xArray[0] = start[0]         yArray[0] = start[-1]         xArray = np.cumsum(xArray)         yArray = np.cumsum(yArray)                                                            return (xArray[-1], yArray[-1])```
But I can't get the array of the final position of each walk per stage in (x,y) form per stage

Here's the main script and where I'm having trouble:
Code:

```    import numpy as np     import matplotlib.pylab as plt     import random     import time         MAX_STEP_SIZE = 0.90    # maximum size of a single step [m]     random.seed(12345)         #..........................................................................main     def main ():         ''''''         print "RANDOM WALK IN STAGES IN TWO DIMENSIONS"         userdata = getDetails()         print "\nPlease wait while random walks are generated and analyzed..."         NUM_STAGES = userdata[0]         NUM_WALKS = userdata[1]         NUM_STEPS = userdata[2]         stageStart = initialPosition()         for stage in np.arange(NUM_STAGES):             walks = np.zeros((NUM_WALKS, NUM_WALKS), dtype=np.ndarray)             for walk in walks:                 walk = getPositions(stageStart, NUM_STEPS, MAX_STEP_SIZE)                 print walk             print walks```
You will see I'm having trouble making a an (x,y) style array, where the [0 0] should be [0.0 , 0.0] and its printed twice and additionally, its not changing to the final position.

I really appreciate and help, advice or references you can provide.
Thanks
-Sid