i+1. cvicenie
Created: 2009-11-04 - 17:25
> with (linalg):
> lfsr := proc (c, iv, n) #koeficienty polynómu, inicializačný vektor, počet prvkov
> local i, v;
> if (nops(c) <> nops(iv)) then error "DLZKA INICIALIZACNEHO VEKTORA NEZODPOVEDA STUPNU POLYNOMU!!!!!!"; end if;
> v := iv;
> for i to n-nops(c) do
> v := [op(v), sum(op(j+i-1, v)*c[j], j = 1 .. nops(c)) mod 2];
> end do;
> return v;
> end proc;
Warning, the protected names norm and trace have been redefined and unprotected
lfsr := proc(c, iv, n)
local i, v;
if nops(c) <> nops(iv) then
error "DLZKA INICIALIZACNEHO VEKTORA NEZODPOVEDA STUPNU POLYNOMU!!!!!!"
end if;
v := iv;
for i to n - nops(c) do v := [op(v),
`mod`(sum(op(j + i - 1, v)*c[j], j = 1 .. nops(c)), 2)]
end do;
return v;
end proc;
> a:=lfsr([1,0,0,0,0],[0,1,1,0,1],40);
> b:=lfsr([1,1],[0,1],40);
a := [0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0,
1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1]
b := [0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0]
> c:=a+b mod 2;
c := [0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1]
> lfsrlength := proc (v) #postupnosť
> local w, k;
> for k while k <= (1+nops(v))/2 do #stupeň polynómu
> w := matrix(k, k, (i, j) -> v[i+j-1]);
> if (det(w) mod 2 = 1) then print(k); end if;
> end do;
> end proc;
> lfsrsolve := proc (lfsr, n) #postupnosť, stupeň polynómu
> local m, v;
> m := Matrix(n, n, (i, j) -> lfsr[i+j-1]);
> v := vector(n, i -> lfsr[i+n]);
> return convert(map(x -> x mod 2, evalm(inverse(m)*v)), list);
> end proc;
lfsrlength := proc(v)
local w, k;
for k while k <= 1/2 + 1/2*nops(v) do w := matrix(k, k,
proc(i, j) option operator, arrow; v[i + j - 1] end proc;);
if `mod`((linalg:-det)(w), 2) = 1 then print(k) end if;
end do;
end proc;
lfsrsolve := proc(lfsr, n)
local m, v;
m := Matrix(n, n,
proc(i, j) option operator, arrow; lfsr[i + j - 1] end proc;);
v := vector(n, proc(i) option operator, arrow; lfsr[i + n] end proc;);
return convert(map(proc(x) option operator, arrow; `mod`(x, 2) end proc;,
evalm((linalg:-inverse)(m)*v)), list);
end proc;
> lfsrlength(c);
6
7
> lfsrsolve(c,6);
[1, 1, 0, 0, 1, 1]
> lfsrsolve(c,7);
[1, 1, 1, 0, 0, 1, 1]