#OK to post homework #Dayoon Kim, 2024-2-4, HW4 Help:=proc(): print(`Fqn(q,n), HD(u,v), RV(q,n) , RC(q,n,d,K), SPB(q,n,t)`): print(`genMatrix(r,c),BestCode(q,n,d,K),BHC(r),M_BH(r)`): end: #----------------------------------------------- #HW4-2. # Function to generate a matrix with 0 and 1 #r:=rows, c:=columns genMatrix := proc(r,c) if not isprime(r) or not isprime(c) then error `Both the number of rows and columns must be prime numbers.`: fi: Matrix(r, c, rand(0..1)): end: # Define the "Y" pattern# Define the matrix M := Matrix(5, 11, fill=0): # Define the letter 'D' M[1, 1] := 1: M[1, 2] := 1: M[1, 3] := 1: M[1, 4] := 1: M[2, 1] := 1: M[2, 5] := 1: M[3, 1] := 1: M[3, 5] := 1: M[4, 1] := 1: M[4, 5] := 1: M[5, 1] := 1: M[5, 2] := 1: M[5, 3] := 1: M[5, 4] := 1: # Define the letter 'Y' M[1, 6] := 1: M[1, 10] := 1: M[2, 7] := 1: M[2, 9] := 1: M[3, 8] := 1: M[4, 8] := 1: M[5, 8] := 1: # DY:=M: # Flatten the matrix into a 1-dimensional vector VectorDY := convert(DY, Vector): # Print the matrix and the vector print(`DY=`,DY); print(`1-dim DY=`,VectorDY); #----------------------------------------------- #HW4-3. ###C4 code #Alphabet {0,1,...q-1}, Fqn(q,n): {0,1,...,q-1}^n Fqn:=proc(q,n) local S,a,v: option remember: if n=0 then RETURN({[]}): fi: S:=Fqn(q,n-1): {seq(seq([op(v),a],a=0..q-1), v in S)}: end: #HD(u,v): The Hamming distance between two words (of the same length) HD:=proc(u,v) local i,co: co:=0: for i from 1 to nops(u) do if u[i]<>v[i] then co:=co+1: fi: od: co: end: #SPB(q,n,d): The best you can hope for (as far as the size of C) for q-ary (n,2*t+1) code SPB:=proc(q,n,t) local i: trunc(q^n/add(binomial(n,i)*(q-1)^i,i=0..t)): end: #RV(q,n): A random word of length n in {0,1,..,q-1} RV:=proc(q,n) local ra,i: ra:=rand(0..q-1): [seq( ra(), i=1..n)]: end: #RC(q,n,d,K): inputs q,n,d, and K and keeps picking K+1 (thanks to Nuray) random vectors #whenver the new vector is not distance <=d-1 from all the previous one RC:=proc(q,n,d,K) local C,v,i,c: C:={RV(q,n)}: for i from 1 to K do v:=RV(q,n): if min(seq(HD(v,c), c in C))>=d then C:=C union {v}: fi: od: C: end: ###End of C4 code ###Start of HW code # Define the parameters d_values := [3, 5, 7]: n_values := [$5..16]: # a procedure to generate the code and check if it's better than the current best BestCode := proc(q, n, d, K) local C, size, best_code, best_size; best_code := {}: best_size := 0: C := RC(q, n, d, K): size := nops(C): if size > best_size then best_code := C: best_size := size: fi: best_code, best_size: end: #This code gets the best possible values everytime, but not 'THE' best possible value. #----------------------------------------------- #HW4-4. # Calculate the size of a binary Hamming code M_BH:= proc(r) local q, n, d, M: q := 2; n := 2^r - 1; d := 3; M := SPB(q, n, d); return M; end: