0

Here is the code:

import math

def create_dct_mat(size=8):
    
    rows, cols = (size, size)
    T = [[0.0]*cols]*rows

    for i in range(0,size):
        print("")
        for j in range(0,size):
            if i == 0:
                T[i][j] = 1/math.sqrt(size)
            else:
                T[i][j] = 2/math.sqrt(size)*math.cos(((2*j+1)*i*math.pi)/(2*size))
            print(T[i][j], end=", ")    
    
    return T

def main():

    size = 8
    rows, cols = (size, size)
    T = [[0.0]*cols]*rows    
    
    T = create_dct_mat(size)
    
    print("")
    
    for i in range(0,size):
        print("")
        for j in range(0,size):
            print(T[i][j], end=", ")


if __name__ == '__main__':
    main()

The output looks like this:

0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373, 0.35355339059327373,
0.6935199226610737, 0.5879378012096793, 0.3928474791935511, 0.13794968964147153, -0.13794968964147145, -0.39284747919355084, -0.5879378012096794, -0.6935199226610737,
0.6532814824381882, 0.2705980500730985, -0.27059805007309845, -0.6532814824381882, -0.6532814824381883, -0.2705980500730989, 0.2705980500730986, 0.6532814824381881,
0.5879378012096793, -0.13794968964147145, -0.6935199226610737, -0.392847479193551, 0.3928474791935508, 0.6935199226610737, 0.13794968964147186, -0.5879378012096792,
0.5, -0.4999999999999999, -0.5000000000000001, 0.49999999999999983, 0.5000000000000001, -0.4999999999999994, -0.49999999999999967, 0.4999999999999993,
0.3928474791935511, -0.6935199226610737, 0.13794968964147153, 0.5879378012096795, -0.5879378012096792, -0.13794968964147133, 0.6935199226610738, -0.39284747919355056,
0.2705980500730985, -0.6532814824381883, 0.6532814824381881, -0.27059805007309856, -0.270598050073099, 0.6532814824381882, -0.653281482438188, 0.27059805007309834,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,

0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,
0.13794968964147153, -0.392847479193551, 0.5879378012096795, -0.6935199226610738, 0.6935199226610738, -0.5879378012096792, 0.39284747919355056, -0.13794968964147172,

The matrix is created correctly inside the create_dct_mat. However, when I loop through the value returned by this function, I end up seeing the last row repeated multiple times. Why is this so?

1 Answer 1

1

This happened as a result of initializing 2D list with [[0.0]*cols]*rows. It creates a 1D list [0.0] * cols, and makes rows copies. You can see this problem for more discussion.

The solution is simple, you can change the way to initialize your 2D list:

T = [[0.0] * size for i in range(size)]
Sign up to request clarification or add additional context in comments.

7 Comments

Why not use the in the answer you have referenced? Matrix = [[0 for x in range(w)] for y in range(h)] I was completely taken by surprise here. Is using the numpy array a better alternative?
Either way is fine. In fact, Python has a doc about it: docs.python.org/3/faq/…
Thanks, one last question on this topic, is it better that I use some other package that can provide this feature of 2D or 3D arrays and things like transpose and matrix multiplication, rather than rely on this 2D list method?
Yes, it would be better turn to numpy when dealing with matrices operations. Numpy has optimization and has better performance than pure Python list.
Your solution has fixed my problem. I have found that I should actually store the result as a tuple since once the matrix is generated, it does not need to be changed. What is the correct way to create a tuple to store the 2D array I have created? Converting a single list (i.e 1D) to tuple is trivial, we just use the tuple function on list. However, I am not sure how a 2D tuple would be created. Perhaps it is better if I just post a new question on this topic.
|

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.