function [nu] = lafcount(q,wghts)
%LAFCOUNT - count commutators in graded free Liealgebra
%  function [nu] = lafcount(q,wghts)
%
%  Given the free Liealgebra generated by s symbols [1],...,[s], each
%  with a positive integer weight: wghts(1),...,wghts(s).
%
%  nu is the number of commutators in the Hall set that has exactly
%  weight q.
%
%  Example: lafcount(3,[1,2,3]) == 2
%     since the commutators of weight 3 are:
%     [1,2] and [3].
%

% written by: Hans Munthe-Kaas, 27/10/97.

if any(wghts)<1,
	error('weights must be positive integers');
end;
if ~all(isinteger(wghts)),
	error('weights must be positive integers');
end;
% compute roots of characteristic polynomial
pdeg = max(wghts);
pol = zeros(1,pdeg+1);
for i = 1:length(wghts),
	pos = pdeg-wghts(i)+1;
	pol(pos) = pol(pos)-1;
end;
pol(pdeg+1) = 1;
lambs = roots(pol);
% plug into formula
nu = 0;
for d = divisors(q),
	a = sum(lambs.^(-q/d));
	nu = nu + a*mobius(d);
end;
nu = nu/q;
nu = round(nu);
return;
