function F = factor(varargin)
% FACTOR Create a factor for a factor graph
%
% F = FACTOR(N,U) where N is the list of (numeric) variable names,
% and U is a table (tensor) of factor values.
%
% F = FACTOR creats an empty factor
%
% F = FACTOR(A) where A is a factor object will return a copy of A
  
% Fields
%  arguments
%  table

% Jerod Weinman
% jerod@acm.org
    
% $Id: factor.m 1 2009-05-07 13:41:00Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% Copyright 2008, 2009, 2011
 
%    This file is part of MatLibFactorGraph.
%
%    MatLibFactorGraph 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.
%
%    MatLibFactorGraph 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 MatLibFactorGraph.  If not, see <http://www.gnu.org/licenses/>.

  if nargin == 0
	
	% Set fields
	F.arguments = [];
	F.table = [];
	
	F = class(F,'factor');
	
  elseif nargin==1 & isa(varargin{1},'factor')
	
	F = varargin{1};
	
  else
	
	A = varargin{1};
	U = varargin{2};
	
	if (length(A)==1 && sum(size(U)>1)>1) || ...
		  (length(A)>1 && length(A)~= ndims(U))
	  error('Dimensions of table do not match argument list');
	end;
	
	F.arguments = A;
	F.table = U;
	
	F = class(F,'factor');
	
  end;
  
