Commit 50afa926 authored by Ferdinand Fischer's avatar Ferdinand Fischer
Browse files

First version of the quantity.domain object.

parent 532c63d3
......@@ -6,13 +6,15 @@ classdef Parser < inputParser
methods
function obj = Parser()
function obj = Parser(varargin)
obj = obj@inputParser();
% set default properties
obj.KeepUnmatched = true;
obj.CaseSensitive = true;
obj.PartialMatching = false;
end % Parser()
function i = isDefault(obj, name)
......
function value = ensureIsCell(value)
%ENSUREISCELL ensures that the value is a cell.
% c = ensureIsCell(value) checks if value is a cell. If it is not a cell,
% it is converted as a cell.
if ~iscell(value)
value = {value};
end
end
This diff is collapsed.
classdef Domain
%DOMAIN class to describes a range of values on which a function can be defined.
% todo:
% * EquidistantDomain
% * multi dimensional
properties
% The discrete points of the grid for the evaluation of a
% continuous quantity. For an example, the function f(x) should be
% considered on the domain x \in X = [0, 1]. Then, a grid can be
% generated by X_grid = linspace(0, 1).
grid double {mustBeReal};
% a speaking name for this domain; Should be unique, so that the
% domain can be identified by the name.
name char;
end
properties (Dependent)
n; % number of discretization points
lower; % lower bound of the domain
upper; % upper bound of the domain
end
methods
function obj = Domain(varargin)
%DOMAIN initialize the domain
%
if nargin >= 1
parser = misc.Parser();
parser.addParameter('grid', [], @isvector);
parser.addParameter('name', '', @ischar);
parser.parse(varargin{:});
% todo: assertions
% * ascending ?
obj.grid = parser.Results.grid(:);
obj.name = parser.Results.name;
else
obj = quantity.Domain.empty();
end
end
function n = get.n(obj)
n = length(obj.grid);
end
function lower = get.lower(obj)
lower = min( obj.grid );
end
function upper = get.upper(obj)
upper = max( obj.grid );
end
function nd = ndims(obj)
%NDIMS number of dimensions of the domain specified by the
%object-array.
nd = size(obj(:), 1);
end
function n = numGridElements(obj)
% NUMGRIDLEMENTS returns the number of the elements of the grid
n = prod([obj.n]);
end
function s = gridLength(obj)
%GRIDLENGTH number of discretization points for each grid in the
%object-array.
s = [obj.n];
end
end
methods (Static)
function g = defaultGrid(gridSize, name)
if nargin == 1
% If no names are specified, chose x_1, x_2, ... as default
% names.
name = cell(1, length(gridSize));
for k = 1:length(gridSize)
name{k} = ['x_' num2str(k)];
end
end
% generate a default gird with given sizes
g = quantity.Domain.empty();
for k = 1:length(gridSize)
o = ones(1, length(gridSize) + 1); % + 1 is required to deal with one dimensional grids
o(k) = gridSize(k);
O = ones(o);
O(:) = linspace(0, 1, gridSize(k));
g(k) = quantity.Domain('grid', O, 'name', name{k});
end
end
end
end
classdef EquidistantDomain < quantity.Domain
%EQUIDISTANTDOMAIN class to handle the discretization of the range of
%definition of a function. The discretization points are equally
%distributed over the domain.
properties
Property1
end
methods
function obj = untitled(inputArg1,inputArg2)
%UNTITLED Construct an instance of this class
% Detailed explanation goes here
obj.Property1 = inputArg1 + inputArg2;
end
function outputArg = method1(obj,inputArg)
%METHOD1 Summary of this method goes here
% Detailed explanation goes here
outputArg = obj.Property1 + inputArg;
end
end
end
function [tests] = testDomain()
tests = functiontests(localfunctions);
end
function setupOnce(testCase)
end
function testDomainInit(testCase)
Z = linspace(0,pi, 3);
d = quantity.Domain('name', 'z', 'grid', Z);
D = [d d];
testCase.verifyEqual( ndims(D), 2);
testCase.verifyEqual( ndims(d), 1);
end
function testDomainGridLength(testCase)
Z = linspace(0,pi, 3);
d = quantity.Domain('name', 'z', 'grid', Z);
D = [d d];
testCase.verifyEqual( cellfun(@(v) numel(v), {Z, Z}), D.gridLength)
end
function testDomainNumGridElements(testCase)
Z = linspace(0,pi, 3);
d = quantity.Domain('name', 'z', 'grid', Z);
D = [d d];
testCase.verifyEqual( D.numGridElements, prod([length(Z), length(Z)]));
end
function testDomainEmpty(testCase)
d = quantity.Domain();
testCase.verifyTrue( isempty(d) )
end
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment