Python Newaxis vs for loop

Discussion in 'Python' started by jfnossa, Apr 24, 2013.

  1. jfnossa

    jfnossa

    Joined:
    Apr 24, 2013
    Messages:
    1
    Likes Received:
    0
    Hi, I am trying to make my program faster.
    I have a matrix and a vector:
    GDES = N.array([[1,2,3,4,5],
    [6,7,8,9,10],
    [11,12,13,14,15],
    [16,17,18,19,20],
    [21,22,23,24,25]])
    Ene=N.array([1,2,3,4,5])
    NN=len(GDES);

    I have defined a function for matrix multiplication:
    def Gl(n,np,k,q):
    matrix = GDES[k,np]*GDES[k,n]*GDES[q,np]*GDES[q,n]
    return matrix

    and I have made a for loop in my calculation:
    SIl = N.zeros((NN,NN),N.float)
    for n in xrange(NN):
    for np in xrange(NN):
    SumJ = N.sum(N.sum(Gl(n,np,k,q) for q in xrange(NN)) for k in xrange(NN))
    SIl[n,np]=SumJ

    print 'SIl:',SIl

    output:
    SIl: [[ 731025. 828100. 931225. 1040400. 1155625.]
    [ 828100. 940900. 1060900. 1188100. 1322500.]
    [ 931225. 1060900. 1199025. 1345600. 1500625.]
    [ 1040400. 1188100. 1345600. 1512900. 1690000.]
    [ 1155625. 1322500. 1500625. 1690000. 1890625.]]



    I want to use newaxis to make it faster:

    def G():
    Mknp = GDES[:, :, N.newaxis, N.newaxis]
    Mkn = GDES[:, N.newaxis, :, N.newaxis]
    Mqnp = GDES[:, N.newaxis, N.newaxis, :]
    Mqn = GDES[N.newaxis, :, :, N.newaxis]
    matrix=Mknp*Mkn*Mqnp*Mqn
    return matrix

    tmp = G()
    MGI = N.sum(N.sum(tmp,axis=3), axis=2)
    MGI = N.reshape(MGI,(NN,NN))
    print 'MGI:', MGI
    output:
    MGI: [[ 825 3900 9225 16800 26625]
    [ 31200 92400 169600 262800 372000]
    [ 146575 413400 722475 1073800 1467375]
    [ 403200 1116900 1911600 2787300 3744000]
    [ 857325 2352900 3980725 5740800 7633125]]


    Any idea how can I get the right answer???
    Thanks
     
    jfnossa, Apr 24, 2013
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,743
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,617
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    1,087
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    710
    adam.deprince@gmail.com
    Mar 24, 2006
  5. Daniel Pitts
    Replies:
    14
    Views:
    1,657
    Patricia Shanahan
    Dec 23, 2006
  6. Allerdyce.John@gmail.com

    loop thru a STL list causes an infinite loop

    Allerdyce.John@gmail.com, Jan 30, 2006, in forum: C++
    Replies:
    5
    Views:
    930
    benben
    Jan 31, 2006
  7. Anshu
    Replies:
    5
    Views:
    633
    Howard
    Aug 14, 2006
  8. Isaac Won
    Replies:
    9
    Views:
    911
    Ulrich Eckhardt
    Mar 4, 2013
Loading...