Home > matgraph > @partition > partition.m

partition

PURPOSE ^

partition --- constructor for the partition class

SYNOPSIS ^

function p = partition(param)

DESCRIPTION ^

 partition --- constructor for the partition class
 p = partition(n) creates a default partition of [n] with all parts of
 size 1 (finest possible partition).
 p = partition(cell_array) creates a partition whose parts are given in
 the cell array.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function p = partition(param)
0002 % partition --- constructor for the partition class
0003 % p = partition(n) creates a default partition of [n] with all parts of
0004 % size 1 (finest possible partition).
0005 % p = partition(cell_array) creates a partition whose parts are given in
0006 % the cell array.
0007 
0008 
0009 if nargin==0
0010     param = 0;
0011 end
0012 
0013 % if called with a single argument, presumably a positive integer
0014 
0015 if ~isa(param,'cell')
0016     n = param;
0017     p.array = logical(speye(n,n));  % singleton parts
0018     p.array = sortrows(p.array);
0019     p = class(p,'partition');
0020     return
0021 end
0022 
0023 % otherwise, called with a cell array containing the parts of p
0024 
0025 % special case: param is an empty cell array
0026 if isempty(param)
0027     p.array = speye(0);
0028     p = class(p,'partition');
0029     return
0030 end
0031 
0032 % Find nv (n) and np (m)
0033 maxv = 0;
0034 for i=1:length(param)
0035     m = max(param{i});
0036     maxv = max([m,maxv]);
0037 end
0038 n = maxv;
0039 m = length(param);
0040 
0041 
0042 % allocate the matrix
0043 p.array = logical(sparse([],[],[],m,n,n));
0044 
0045 % load the entries
0046 for i=1:m
0047     row = zeros(1,n);
0048     row(param{i}) = 1;
0049     p.array(i,:) = logical(row);
0050 end
0051 p.array = sortrows(p.array);
0052 
0053 p = class(p,'partition');
0054 
0055 if ~check(p)
0056     p.array = speye(0);
0057     error('The cell array does not define a valid partition');
0058 end
0059

Generated on Thu 13-Mar-2008 14:23:52 by m2html © 2003