function correct = linear_discriminant(A1, A2, B, class_B, d)
%Function uses A1 and A2 as training samples to compute the linear discriminant
%of the form a0+a1*x+a2*x^2....
%A1 = samples from class  1
%A2 = samples from class -1
%B  = Test point
%class_B = Actual Class of B
%d = degree of the polynomial
%Correct = 1 if B is correctly classified
%Correct = 0 if B is incorrectly classified

correct=[];

for i=1:size(d,2)
    
    %Compute the big Y matrix
    Y=[];
    for n=1:size(A1,2)
        y_t=[1];
        for pwr=1:d(i)
            y_t=[y_t A1(n)^pwr];
        end
        Y=[Y; y_t];
    end
    for n=1:size(A2,2)
        y_t=[-1];
        for pwr=1:d(i)
            y_t=[y_t -A2(n)^pwr];
        end
        Y=[Y; y_t];
    end
    
    %Compute a using psuedoinverse
    %This is what is given in the text
    %it starts having numerical problems for high
    %order ploynomials
    %a=(inv(Y'*Y)*Y')*ones(size(Y,1),1);
    %Instead, you can also do the following to avoid those:
    a=pinv(Y)*ones(size(Y,1),1);
    
    %Test the discriminant on the test point B
    testy_t=[1];
    for pwr=1:d(i)
        testy_t=[testy_t; B^pwr];
    end
    if sign(testy_t'*a)==class_B
        correct=[correct 1];
    else
        correct=[correct 0];
    end
end

