function A = trimConstraintMatrix(A, zerotol, removeZeroRows)
% normalize and trim rows and (optionally) remove zero-rows
%
% While trimming almost equal entries in the same columns, the rows are
%   eliminated, using the unique function.
%
% It also eliminates almost-zero rows.
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2013 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

%% identify rows that are non-zero rows
if removeZeroRows
    A(all(abs(A) < sqrt(zerotol), 2), :) = [];
end
%! DEV: changed the above from zerotol to sqrt(zerotol) as it occured that
%       zero rows might really be THAT tight.
%/

%% normalize constraints
A = bsxfun(@rdivide, A, sqrt(sum(A.^2, 2)) );

%% trim columns for almost equal values
A = trimColumns(A, zerotol);