Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'subject:Python': 0.05; 'linear': 0.07; 'referring': 0.07; 'api': 0.09; 'python': 0.09; '(1,': 0.09; 'abstraction': 0.09; 'pointless': 0.09; 'underlying': 0.09; 'cc:addr:python-list': 0.10; 'language': 0.14; 'sat,': 0.15; "'b'": 0.16; '(2,': 0.16; 'algorithm.': 0.16; 'array)': 0.16; 'cc:name:python list': 0.16; 'coordinates': 0.16; 'dimension': 0.16; 'display,': 0.16; 'element,': 0.16; 'expects': 0.16; 'numpy': 0.16; 'pairs': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'tim': 0.18; '>>>': 0.18; 'memory': 0.18; 'feb': 0.19; 'written': 0.20; 'import': 0.21; 'libraries': 0.22; 'sorry,': 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'split': 0.23; 'allows': 0.25; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'creating': 0.26; 'url:wiki': 0.26; 'am,': 0.27; '(as': 0.27; '(such': 0.27; 'convention': 0.27; 'first.': 0.27; 'forgot': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'went': 0.28; 'chris': 0.28; 'arrays': 0.29; 'url:wikipedia': 0.29; 'array': 0.29; "i'm": 0.29; 'code': 0.31; 'true.': 0.33; 'handle': 0.33; 'typically': 0.33; 'received:google.com': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'useful': 0.36; 'subject: (': 0.36; 'does': 0.37; 'two': 0.37; 'uses': 0.37; 'rather': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'url:en': 0.38; 'page': 0.38; 'called': 0.39; 'lost': 0.60; 'back': 0.62; 'time,': 0.62; 'different': 0.63; 'skip:n 10': 0.63; 'subject:First': 0.65; 'forth': 0.75; '(according': 0.84; '2013': 0.84; 'multiply': 0.84; 'order:': 0.84; 'oscar': 0.84; 'by.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=R9lpFYL+lvC/FKF832ToF4w5EaaUJo4cdJMSsB1ZBqA=; b=DzNMDwTrVH/mJTc6YK43alAsGzyatdGfrk9MdFRtxWrYvgzx6WoiUZ3DqenrImIEHu BzSFakFatTefFJZRGItLLWV6W15qd8Ebfa7JifELPn3rzp2jlbFm3xML+IxB23vqt1+r t4/WseLEspMHf2wGXSPioapbBnG2clN2gkexYEQQ0RPM5iG9vKoba2FmJOEWKDDfRPDk qc3Of8FW0A8vw/XMQxt42kQU80ez0gbjPbHaWmOrMmRDR7Ar46QxWeJv4Zae0al/jUrk qArrr2B7yTxv4MAQ05Ef+M2GE1GMMqPqesPqbFpd/Li1IOx4+SZNflIOH0lziDrpjfO7 uVEg== X-Received: by 10.52.90.140 with SMTP id bw12mr6940079vdb.50.1361025571850; Sat, 16 Feb 2013 06:39:31 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <2013021323250974803-chrishinsley@gmailcom> <511E1DCB.40406@timgolden.me.uk> <511E55E1.6050603@timgolden.me.uk> From: Oscar Benjamin Date: Sat, 16 Feb 2013 14:39:11 +0000 Subject: Re: First attempt at a Python prog (Chess) To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361025580 news.xs4all.nl 6942 [2001:888:2000:d::a6]:42480 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39003 On 15 February 2013 15:49, Chris Angelico wrote: > On Sat, Feb 16, 2013 at 2:36 AM, Tim Golden wrote: >> How true. This last time, my team split into two: one half >> to handle the display, the other working on the algorithm. We >> ended up having to draw a really simple diagram on the back of >> an envelope with the x,y pairs written out and pass it back >> and forth as *everyone* kept forgetting which went first. > > I'm sorry, I don't follow. People forgot that x comes before y, like > it does in everything algebraic? Or is it that people lost track of > which axis you called 'x' and which you called 'y'? When you have a flat array and need to do a[x*N + y] to get an element, the order is irrelevant. What matters is which of the coordinates you multiply and what dimension of the array you multiply it by. There are two equally good ways to do this, row-major and column-major order: http://en.wikipedia.org/wiki/Row-major_order Any time you call into an API that expects a flattened array representing a multidimensional array you need to check what order the API expects. Typically a language has a convention such as C (row-major) or Fortran (column-major). (According to that Wikipedia page Python uses row-major, but I'm not really sure what that is referring to). Numpy allows you to specify the order when creating an array: >>> import numpy as np >>> aC = np.array([['a', 'b'], ['c', 'd']], dtype=str, order='C') >>> aF = np.array([['a', 'b'], ['c', 'd']], dtype=str, order='F') >>> aC array([['a', 'b'], ['c', 'd']], dtype='|S1') >>> aF array([['a', 'b'], ['c', 'd']], dtype='|S1') The different order in the two arrays above is transparent to Python code that doesn't directly refer to the underlying memory arrangement: >>> aC[0, 1] 'b' >>> aF[0, 1] 'b' >>> aC.strides (2, 1) >>> aF.strides (1, 2) >>> str(buffer(aC)) 'abcd' >>> str(buffer(aF)) 'acbd' This is useful when interfacing with linked Fortran and C libraries (as is the case in scipy). I find keeping track of what order I'm using when accessing the elements of an array to be a pointless distraction. I would much rather use an abstraction over the linear memory (such as a numpy array) that enables me to forget all about it. Oscar