please help to make code correct to give correct output.
이전 댓글 표시
classdef gf
properties
p=2;
n=0;
ply=0;
gfmt=[];
gfat=[];
gfdt=[];
gfst=[];
ply_reps=[];
gf_java=[];
end
methods
function obj=gf(p,n,ply)
if ~isprime(p)
error('p needs to be prime');
end
obj.p=p;
obj.n=n;
if ~exist('ply','var')
obj.ply=find_irreducible_poly(p,n);
else
obj.ply=ply;
if ~is_irreducible(ply,p,n)
error('The input vector is not irreducible');
end
end
[gfat,gfst,gfmt,gfdt,ply_reps]=build_tables(obj);
obj.gfmt=gfmt;
obj.gfat=gfat;
obj.gfdt=gfdt;
obj.gfst=gfst;
obj.ply_reps=ply_reps;
import gf.*
obj.gf_java=GF_java(gfat,gfmt,gfst,gfdt);
end
function check_error(obj,a)
if length(size(a))>2
error('input can only be matrix/vector/scalar');
end
if ~isempty([find(a >=obj.p^obj.n) find(a <0)])
error('input is out of bound')
end
b=abs(round(a)-a);
if sum(b(:)) ~=0
error('input cannot be fractional');
end
end
function r=rank(obj,a)
check_error(obj,a);
r=obj.gf_java.rank(a);
end
function c=mult(obj,a,b)
check_error(obj,a);
check_error(obj,b);
if (size(a,2)~=size(b,1))
error('number of columns of first input has to be equal to number of rows of second input');
end
c=obj.gf_java.mat_mult(a,b);
end
function c=dmult(obj,a,b)
check_dim(obj,a,b);
check_error(obj,a);
check_error(obj,b);
c=obj.gf_java.mat_dot(a,b);
end
function c=ddiv(obj,a,b)
check_dim(obj,a,b);
check_error(obj,a);
check_error(obj,b);
c=obj.gf_java.mat_dotdiv(a,b);
end
function c=conv(obj,a,b)
check_error(obj,a);
check_error(obj,b);
c=obj.gf_java.vec_conv(a,b);
c=c(:)';
end
function [q,rem]=deconv(obj,b,a);
check_error(obj,a);
check_error(obj,b);
o=obj.gf_java.vec_deconv(b,a);
q=o(1);
rem=o(2);
q=q(:)';
rem=rem(:)';
end
function c=add(obj,a,b)
check_dim(obj,a,b);
check_error(obj,a);
check_error(obj,b);
c=obj.gf_java.mat_add(a,b);
end
function c=sub(obj,a,b)
check_dim(obj,a,b);
check_error(obj,a);
check_error(obj,b);
c=obj.gf_java.mat_sub(a,b);
end
function c=div(obj,a,b)
check_dim(obj,a,b);
check_error(obj,a);
check_error(obj,b);
binv=inv(obj,b);
if (sum(binv(:))==0)
error('second argument is not invertible');
end
c=obj.gf_java.mat_mult(a,binv);
end
function p=return_primitive_polynomial(obj)
p=obj.ply;
end
function ainv=inv(obj,a)
check_error(obj,a);
if (size(a,1)~=size(a,2))
error('input should be a square matrix');
end
ainv=obj.gf_java.inverse(a);
if (sum(ainv(:))==0)
error('input is not invertible');
end
end
function check_dim(obj,a,b)
if length(size(a))~=length(size(b))
error('dimension of input does not match');
end
if sum(abs(size(a)-size(b)))~=0
error('dimension of input does not match');
end
end
function ply=return_poly_representation(obj,i)
if ~exist('i','var')
error('Function needs at least one input');
end
if (i>=obj.p^obj.n)
error('Input index is too large');
end
if (i <0)
error('Input index is too small');
end
if ((round(i)-i)~=0)
error('Input index should be integer');
end
ply=obj.ply_reps{i+1};
end
function [gfat,gfst,gfmt,gfdt,ply_reps]=build_tables(obj)
p=obj.p;
n=obj.n;
gfst=zeros(p^n);
gfmt=zeros(p^n);
gfat=zeros(p^n);
gfdt=zeros(p^n);
for id1=0:p^n-1
v1=fliplr(num2vec(id1,p,n));
ply_reps{id1+1}=v1;
for id2=id1:p^n-1
v2=fliplr(num2vec(id2,p,n));
[dummy,rem]=deconv(conv(v1,v2),obj.ply);
pd=vec2num(fliplr(mod(rem,p)),p);
sm=vec2num(fliplr(mod(v1+v2,p)),p);
gfmt(id1+1,id2+1)=pd;
gfmt(id2+1,id1+1)=pd;
gfat(id1+1,id2+1)=sm;
gfat(id2+1,id1+1)=sm;
gfdt(pd+1,id1+1)=id2;
gfdt(pd+1,id2+1)=id1;
gfst(sm+1,id1+1)=id2;
gfst(sm+1,id2+1)=id1;
end
end
% rlabel indices
% olabels=gfmt(3,:);
% nlabels=[0 2:p^n-1 1]; % desired labeling (1=1, 2=a 3=a^2,...)
% gfmt=rlabel(gfmt,olabels,nlabels);
% gfat=rlabel(gfat,olabels,nlabels);
% gfdt=rlabel(gfdt,olabels,nlabels);
% gfst=rlabel(gfst,olabels,nlabels);
% check tables
assert(mean(sum(gfat))==sum(1:p^n-1));
assert(mean(sum(gfst))==sum(1:p^n-1));
sm=sum(gfmt);
assert(mean(sm(2:length(sm)))==sum(1:p^n-1));
sm=sum(gfdt);
assert(mean(sm(2:length(sm)))==sum(1:p^n-1));
end
end
end
function num=vecs2num(vs,z)
num=zeros(1,size(vs,2));
for id=size(vs,1):-1:2
num=num+double(vs(id,:));
num=num*z;
end
num=num+double(vs(1,:));
end
-----------------------------------
%
% Test example for class gf
%
% Written by Samuel Cheng (Sept 13, 2011)
%
% if you need to contact me, just google :)
clear;
java_path_setup; % please run this to setup java path
% create gf class of 3^2
fprintf('%%Create GF(3^2):\ngf9=gf(3,2);\n');
gf9=gf(3,2);
%%%%
% Eg. 1
a=[2 1;1 0]
% compute rank
fprintf('\n%%Compute Rank:\ngf9.rank(a)\n');
% fprintf('Rank of a = %d\n',gf9.rank(a)); % return rank
gf9.rank(obj,a)
% compute inverse
fprintf('\n%%Compute a inverse:\ninva = gf9.inv(a)\n')
inva = gf9.inv(a)
% Check inverse
fprintf('\n%%Check inverse (a * inva):\na_times_ainva=gf9.mult(a,inva)\n');
a_times_ainva=gf9.mult(a,inva)
% matrix divide
fprintf('\n%%Compute a / a:\ngf9.div(a,a)\n');
gf9.div(a,a)
%%%%%
% Eg. 2
fprintf('\n%%More examples:\n');
b=[1 2 1;1 0 1]
c=[1 1 2;2 1 1]
% compute summation
fprintf('\n%%Compute b + c:\ngf9.add(b,c)\n');
gf9.add(b,c)
% compute subtraction
fprintf('\n%%Compute b - c:\ngf9.sub(b,c)\n');
gf9.sub(b,c)
% compute dot multiplication
fprintf('\n%%Compute b .* c:\ngf9.dmult(b,c)\n');
gf9.dmult(b,c)
% compute dot division
fprintf('\n%%Compute b ./ c:\ngf9.ddiv(b,c)\n');
gf9.ddiv(b,c)
%%%%%
% Eg. 3
fprintf('\n%%Yet more examples:\n');
a=[1 2 1 1];
b=[1 3 1];
% compute convolution of two polynomials
fprintf('\n%%Polynomial x^3 + 2 x^2 + x + 1 is represented by\n');
disp(a);
fprintf('\n%%Polynomial x^2 + 3 x + 1 is represented by\n');
disp(b);
fprintf('\n%%Polynomial a times polynomial b:\ngf9.conv(a,b)\n');
gf9.conv(a,b)
fprintf('\n%%Polynomial a divided by polynomial b:\n[q,rem]=gf9.deconv(a,b)\n');
[q,rem]=gf9.deconv(a,b);
fprintf('\n%%Quotient:\n');
disp(q);
fprintf('\n%%Remainder:\n');
disp(rem);
fprintf('\n%%Check:\ngf9.sub(a,gf9.conv(q,b)) %%should be equal to rem');
gf9.sub(a,gf9.conv(q,b))
%%%%
% Eg. 4
% output the primitive polynomial
fprintf('\n%%More information on GF:\n');
fprintf('\n%%Output primitive polynomial:\ngf9.return_primitive_polynomial %%x^2 +1\n');
gf9.return_primitive_polynomial % x^2 + 1
% show polynomial representation
fprintf('\n%%Show polynomial representations of symbol 2:\ngf9.return_poly_representation(5) %%x + 2\n');
gf9.return_poly_representation(5) % x + 2
답변 (1개)
Image Analyst
2015년 1월 3일
0 개 추천
I don't know what the correct output is but this link will definitely allow you to get the correct output.
댓글 수: 6
hajer
2015년 1월 3일
hajer
2015년 1월 3일
Image Analyst
2015년 1월 3일
Did you contact the author? None of us know anything about his code.
hajer
2015년 1월 3일
Image Analyst
2015년 1월 3일
I guess you're on your own then. At least I'm not going to spend the time to understand that long chunk of code and add a bunch of comment to it to explain each little chunk of code. Maybe someone else will (though I doubt it). Just take it a line at a time and you'll probably be able to do it. Good luck.
hajer
2015년 1월 6일
카테고리
도움말 센터 및 File Exchange에서 Structural Mechanics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!