0

My intention with this program was to get a 4x4 matrix with certain values, but for some reason the loop is putting everything into the same row/column... What is off about my code?

def matrixH0(k):
    H0=[]
    print H0
    for m in range (0,k):
        for n in range (0,k):
            if abs(m-n)==1:
                H0.append(math.sqrt(n+m+1)/2.)
            else:
                H0.append(0)
        print H0

This is my output:

[0,
 0.7071067811865476,
 0,
 0,
 0.7071067811865476,
 0,
 1.0,
 0,
 0,
 1.0,
 0,
 1.224744871391589,
 0,
 0,
 1.224744871391589,
 0]

5 Answers 5

2

Append rows to H0, append values to rows:

import math
import pprint
def matrixH0(k):
    H0 = []
    for m in range(k):
        # create a new row 
        row = []               
        for n in range(k):
            if abs(m-n)==1:
                row.append(math.sqrt(n+m+1)/2.)
            else:
                row.append(0)
        H0.append(row)
    return H0
pprint.pprint(matrixH0(4))

yields

[[0, 0.7071067811865476, 0, 0],
 [0.7071067811865476, 0, 1.0, 0],
 [0, 1.0, 0, 1.224744871391589],
 [0, 0, 1.224744871391589, 0]]

By the way, matrixH0 could also be written using nested list comprehensions:

def matrixH0(k):
    return [[math.sqrt(n+m+1)/2. if abs(m-n)==1 else 0 for n in range(k)]
            for m in range(k)]
Sign up to request clarification or add additional context in comments.

1 Comment

You beat me to it :(
1

You never create a multidimensional array in your code, you just append to a single list. Here is a solution:

def matrixH0(k):
    H0=[]
    print H0
    for m in range (0,k):
        H0.append([])
        for n in range (0,k):
            if abs(m-n)==1:
                H0[m].append(math.sqrt(n+m+1)/2.)
            else:
                H0[m].append(0)
        print H0

Comments

1

Init the rows at on the first loop and append the numbers to the array under the first loop index

def matrixH0(k):
    H0=[]
    print H0
    for m in range (0,k):
        H0.append([])
        for n in range (0,k):
            if abs(m-n)==1:
                H0[m].append(math.sqrt(n+m+1)/2.)
            else:
                H0[m].append(0)
        print H0

3 Comments

That's exactly the same as my answer -_-
I didn't saw your answer. I guess we wrote at the same time. I see you updated the else clause
We did ^_^ I think I might have been about two seconds before... xD
0

you find the correct Function

def matrixH0(k):
    H1=[]
    k=4
    print H1
    for m in range (0,k):
        H0=[]
        for n in range (0,k):
            if abs(m-n)==1:
                 H0.append(math.sqrt(n+m+1)/2.)
            else:
                 H0.append(0)
        H1.append(H0)
    return H1

[[0, 0.7071067811865476, 0, 0], [0.7071067811865476, 0, 1.0, 0], [0, 1.0, 0, 1.224744871391589], [0, 0, 1.224744871391589, 0]]

1 Comment

Why k = 4? This is an argument specified by the calling of the function and shouldn't necessarily always be 4...
0

I think Raulucco's answer will work. But you if you work with number and matrix, numpy is certainly the module you want.

Your code will look like

import numpy
H0 = numpy.diag([math.sqrt(n + 1) for n in xrange(k - 1)], 1) + \
    numpy.diag([math.sqrt(n) for n in xrange(k - 1)], -1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.