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?

gyuunyuu
  • 526
  • 5
  • 17

1 Answers1

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)]
Haoliang
  • 1,184
  • 4
  • 11
  • 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? – gyuunyuu Jul 03 '22 at 21:27
  • Either way is fine. In fact, Python has a doc about it: https://docs.python.org/3/faq/programming.html#faq-multidimensional-list – Haoliang Jul 03 '22 at 21:31
  • 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? – gyuunyuu Jul 03 '22 at 21:54
  • Yes, it would be better turn to numpy when dealing with matrices operations. Numpy has optimization and has better performance than pure Python list. – Haoliang Jul 03 '22 at 22:01
  • 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. – gyuunyuu Jul 05 '22 at 08:57
  • Tuples are immutable in Python. You can create a 2D tuple if you'd like. Here's an example to create a 10*10 tuple with random numbers: `a = tuple(tuple(random.randint(1, 10) for _ in range(10)) for _ in range(10))` – Haoliang Jul 06 '22 at 00:49
  • The data generated into the 8x8 matrix is basically a constant. As far as I know, Python does not have a constant type but does have tuples. – gyuunyuu Jul 06 '22 at 23:44