📄 tiling_create.m
字号:
function [xdata, wdata, tdim] = tiling_create(xg, dsize, c, cfg)% TILING_CREATE Creates tiling structures% [XDATA, WDATA, TDIM, GRIDS] = TILING_CREATE(XG, DSIZE, C, CFG)% Parameters:% xg - input grids, in a cell array of dimension d, the continuous input dimension% dsize - size of discrete inputs, an array of dimension dd, the discrete input% dimension. May be empty when there are no discrete inputs.% c - number of tilings% cfg - additional config, structure with fields (defaults in square brackets)% distr - distribution of tilings: 'even', ['random']% delta - base spacing between grids (dimension-wise or one for all dimensions)% [-1], meaning that delta(i) = XG{i}(2) - XG{i}(1) % init - initialization of tile weights: {0}, another fixed numeric value, or 'random'% sigma - random standard deviation for random tile init% exact - whether one grid on each dimension should be exactly% as supplied in xg%% Returns:% xdata - grid data, a cell array containing matrices of tile boundaries, one per dimension% wdata - tiling weight data% tdim - dimensionality information for the tiling% ===== INPUT PARAMETERS ====% default configCFG.distr = 'random';CFG.delta = -1;CFG.init = 0;CFG.sigma = 1;CFG.exact = 0; % force exact fit of one tiling% tiling dimensiond = length(xg);% process configif nargin < 3, cfg = CFG;else cfg = checkparams(cfg, CFG);end;% fields which may be specified on a per dimension basisperdimfields = {'distr', 'delta'};for i = 1:length(perdimfields), f = perdimfields{i}; if ~iscell(cfg.(f)), % expand a scalar value to cell of identical values val = cfg.(f); if ischar(val), cfg.(f) = cell(1, d); for di = 1:d, cfg.(f){di} = val; end; elseif isscalar(val), cfg.(f) = val + zeros(1, d); end; end;end;% correctness testsif c < 2, error('There must be at least two tilings'); end;if cfg.sigma <= 0, error('Random init stdev must be greater than zero'); end;if any(dsize <= 0) || any(mod(dsize, 1) > 0), error('Discrete sizes must be strictly positive integers'); end;% ===== TILING GRIDS CONSTRUCTION ====xdata = cell(1, d);twdim = zeros(1, d); % weight data dimensions for one tiling% iterate over dimensions, constructing tile grids for each dimensionfor di = 1:d, xi = xg{di}; delta = cfg.delta(di); distr = cfg.distr{di}; % default delta is the distance between the first two points if delta <= 0, delta = xi(2) - xi(1); end; % correctness tests if any(diff(xi) <= 0), error('Tiling grid must be monotonically strictly increasing'); end; if delta > xi(end) - xi(1), error('Tiling delta cannot be larger than the variable domain'); end; if ~any(strcmp(distr, {'even', 'random'})), error(['Unknown tiling distribution type ' distr]); end; switch distr, case 'even', ddelta = (0 : delta/(c-1) : delta) - delta / 2; % for even number of tilings, % correct such that one tiling falls exactly onto the given grid if mod(c, 2) == 0, ddelta = ddelta + delta/(c-1)/2; end; case 'random', if cfg.exact, % random even distribution centered on 0 ddelta = delta * rand(1, c-1) - repmat(delta / 2, 1, c-1); % nicely order the tilings ddelta = [ddelta(ddelta < 0) 0 ddelta(ddelta > 0)]; else % random even distribution centered on 0 ddelta = delta * rand(1, c) - repmat(delta / 2, 1, c); % nicely order the tilings ddelta = [ddelta(ddelta <= 0) ddelta(ddelta > 0)]; end; end; % iterate over tilings, constructing tile grid for each xdata{di} = zeros(c, length(xi) + 1); for ci = 1:c, xci = xi + ddelta(ci); % make sure the tiling completely covers the variable domain if xci(1) > xi(1), % at start xci = [xi(1) xci]; % truncate tiling at end of domain xci(end) = xi(end) + 1e-10; elseif xci(end) < xi(end), % at end (displacing the end a bit s.t. strict comparisons fail on the very % interval end) xci = [xci xi(end) + 1e-10]; % truncate tiling at start of domain xci(1) = xi(1); else % or else (exactly on grid), fill with 'dummy' entry to match dimension of matrix % also doing the trick above xci(end) = xci(end) + 1e-10;% xci = [xci xi(end) + 1e-10]; xci = [xci NaN]; end; xdata{di}(ci, :) = xci; end; twdim(di) = length(xi+1);end;% ===== DIMENSION INFO STRUCTURE ====tdim.tsize = [c twdim];tdim.dsize = dsize;tdim.c = c; % number of tilingstdim.cdim = d; % dimension of continuous parttdim.ddim = length(dsize); % dimension of discrete part, if any% ===== WEIGHTS INITIALIZATION ====% data is stored in a flat array for easy handlingif isscalar(cfg.init), wdata = cfg.init + zeros(prod(tdim.tsize), prod(tdim.dsize));elseif strcmp(cfg.init, 'random'), wdata = cfg.sigma * rand(prod(tdim.tsize), prod(tdim.dsize));else error('Unsupported initialization type');end;% === END tiling_create() RETURNING xdata, wdata, tdim ========================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -