function h = CS_clause2hpv(clause,n,offset)
% CS_clause2hpv - converts a KB clause to a hyperplane normal form
%                 set of coefficients with offset distance from H_k
% On input:
%     clause (1xk vector): disjunction; pos is atom, neg is negated atom
%     n (int): number of atoms in KB
%     offset (float): backoff distance of (n-1)-D hyperplane from H_k
% On output:
%     h (1 by (n+1) vector): hyperplane coefficients:
%       c_1*x_1 + c_2*x_2 +...+ c_n*x_n + c_(n+1)
% Call:
%     h = CS_clause2hpv([-1,2],2,0.99);
% Author:
%     T. Henderson
%     UU
%     Fall 2021

k = length(clause);
I = unique(abs(clause));
alpha = zeros(n,1);     %0 if the literal is not an atom in the clause
for j = 1:k
    i_j = abs(clause(j));   %index in alpha
    alpha(i_j) = sign(clause(j));   %-1 or 1
end
alpha = alpha/norm(alpha); %hyperplane normal vector (coefficents)
b_k = ones(k,1)/k;
d = offset*norm(b_k);   %distance
p = zeros(n,1);         %non-solution vertex
indexes = find(clause<0);
atoms = abs(clause(indexes));
p(atoms) = 1;
q = p + d*alpha;    %point on the hyperplane
c = -dot(alpha,q);  %solution for constant in the hyperplane
h = [alpha',c];
