Coding Theory Programs Built in Octave, Part IV.

#
1;

#================================
function Badd = add(x1,x2)
# description: this function takes two binary vectors x1,x2, and returns x1+x2
# mode 2.
# usgae: Badd = add(x1,x2)   
      n=rows(x1);
      x3=zeros(n,1);
      for i=1:n
            m=x1(i)+x2(i);
            if (m==1)  (x3(i)=1); endif;
            if (m==2)  (x3(i)=0); endif;
      endfor;
      Badd=x3;
endfunction
#=====================================
function TwoFact=TF(n)

# Description: This function finds(2^n).
# Usage: TwoFact=TF(n)
Prod=1;
for i=1:n
      Prod=Prod*2;
endfor;
TF=Prod;
endfunction
#=====================================

function BtoD=BtD(x)

# Description: This function takes a number x in the binary form
# and returns the decimal form.
# Usage: BtoD=BtD(x)

[n,m]=size(x);
N=max(n,m);
s=x(1);
for i=2:N
      s=s+x(i)*TF(i-1);
endfor;
BtD=s;
endfunction

#=====================================
function DV=div(a,b)

# Description: This function finds a div b
# Usage: DV=div(a,b)
 
x=a/b;
y=round(x);
if (y==x) (DV=x); endif;
if (y<>x) (DV=y-1); endif;
endfunction
#=====================================
function MD=mod(a,b)

# Description: This function returns a mod b
# Usage: MD=mod(a,b)

x=a/b;
y=round(x);
if (y==x) (MD=0);endif;
if (y<>x) (MD=(x-(y-1))*b);endif;
endfunction
#=====================================
function DecToBin=DtoB(x,g)

# Description: Changes any positive integer from the decimal form to the
# required system (from 1 to 16, eg: 2 corresponds to binary). Note: x is
# the number to be changed, and g is the system to change to.
# Usage: DecToBin=DtoB(x,g)

dd=0;
k=[];
y=x;
for i=1:1000
      z=div(y,g);
      e=mod(y,g);
      dd=dd+1;
      k=[k,e];
      y=z;
      if (y==0)
            k=k';
            tt=zeros(dd,1);
            for i=1:dd
                  tt(i)=k(dd-i+1);
            endfor;
            DecToBin=tt;
            return;    
      endif;
endfor;
     
endfunction

#=====================================
function Code=CodeGen(M)

# description: This function takes a generator of a certain code and returns
# the code itself. Each row of the matrix M is one generator.
# usage: Code=CodeGen(M)

[m,n]=size(M);
Prod=1;
for k=1:m
      Prod=Prod*2;
endfor;
C=zeros(Prod,n);
#for i=1:m
#     C(i,:)=M(i,:);
#endfor;
DD=zeros(Prod,m);
for i=1:Prod-1
      TEMP=DtoB(i,2)';
      S=TEMP;
      if (columns(TEMP)<m)
            dif=m-columns(TEMP);
            q=zeros(1,dif);
            #pp=zeros(1,n)
            #for kk=1:columns(TEMP)
            #     pp(1,n-kk+1)=TEMP(1,n-kk+1)
            #endfor;
            pp=[q,TEMP];
            S=pp;
            #DD(i,:)=pp
      endif;
      DD(i,:)=S;
endfor;
#DD
#size(M)
#Prod
#m
for i=1:Prod-1
      MM=M;
      for j=1:m
            #i
            #j
            #F=DD(i,:);
            MM(j,:)=MM(j,:)*DD(i,j);
      endfor;
      #printf("YES");
      #MM(i,:);
      summ=MM(1,:);
      for j=2:m
            summ=add(summ',MM(j,:)')';
      endfor;
      C(i,:)=summ;
endfor;

Code=C;
endfunction
#=====================================
function weight=wt(v)

# Description: This function takes a certain vector v in the binary form and
# returns the weight of the vector; i.e., the number of ones in the vector.

[n,m]=size(v);
if (m>n) (v=v');  endif;
n=rows(v);
w=0;
for i=1:n
      if (v(i)==1) (w=w+1); endif;
endfor;
weight=w;
endfunction

#=====================================
function MatVecMul=Bmult(M,v)

# Description: This function multiplies a certain matrix M by a certain
# binary vector v. The multiplication is mod 2.
# Usage: MatVecMul=Bmult(M,v)

#[n,m]=size(v);
#if (m>n) (v=v')  endif;
[n,m]=size(M);
w=zeros(n,1);
for i=1:n
      qq=zeros(m,1);
      for j=1:m
            qq(j)=M(i,j)*v(j);
      endfor;
      #qq
      count=0;
      for k=1:m
            #sum=sum+qq(k)
            #if (sum==2) (sum==0);  endif;
            if (qq(k)==1) (count=count+1); endif;
            #count
      endfor;
      zz=count/2;
      yy=round(zz);
      if (yy==zz) (w(i)=0); endif;
      if (yy<>zz) (w(i)=1); endif;
      #w(i)=sum
endfor;
w=w; #new
MatVecMul=w;
           
endfunction


#=====================================

function [MVadd,flag1] = ad(M,v,check)
# description: This function takes a binary vectors v,a binary matrix M, and
# returns M+v mod 2.
# usgae: [MVadd,flag1] = ad(M,v,check) 
      [nn,n]=size(M);flag1=0;M1=M;
      x=zeros(n,1);y=zeros(n,1);
      for i=1:nn
            for j=1:n
                  x(j)=M1(i,j);
            endfor;
            #x=M1(i,:)';
            y=add(x,v);
            for k=1:8
                  if (check(k,:)==y')
                        (flag1=1);
                  endif;
            endfor;
            for j=1:n
                  M1(i,j)=y(j);
            endfor;

      endfor;
      MVadd=M1;
endfunction
#================
function [GenMat,ParCheckMat,Dist,C]=solv(c)
# Description: This function solves question 3.1.21 in the text.
# Usage: [GenMat,ParCheckMat,Dist,C]=solv(c)
a=eye(9);
b=[1,1,1,1,0,0,0,0,0;1,0,0,0,1,1,1,0,0;1,0,1,0,0,0,0,1,1]';
d=[b,c]'
M=[eye(6),d];
C=CodeGen(M);
[m,n]=size(C);
minWT=1000;
for i=1:m-1
      w=wt(C(i,:)');
      if (w<5)
            printf("Distance < 5\n");
            return;
      endif;
      if (w<minWT)
            minWT=w;
      endif;
endfor;
printf("\n");
ParCheckMat=[b,c,eye(9)];
GenMat=M;
Dist=minWT;
endfunction



 

Back to Iyad Abu-Jeib's Homepage