Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Beginner help numpy 2D arrays
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 04-05-2013, 06:51 AM PM User | #1 anthes4 New to the CF scene   Join Date: Apr 2013 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts 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""" 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: 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 PM User | #2 anthes4 New to the CF scene   Join Date: Apr 2013 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts 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

 Bookmarks

 Tags arrays, numpy, python

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:03 AM.