samer@4
|
1 classdef dmap
|
samer@4
|
2 properties (GetAccess=private, SetAccess=immutable)
|
samer@4
|
3 cardr_
|
samer@4
|
4 map_rn
|
samer@4
|
5 map_ni
|
samer@4
|
6 domain_
|
samer@4
|
7 end
|
samer@4
|
8
|
samer@4
|
9 methods
|
samer@4
|
10 % dmap - Create discretisation map
|
samer@4
|
11 %
|
samer@4
|
12 % dmap ::
|
samer@4
|
13 % N:natural~'range of discretisation function will be 1..N',
|
samer@4
|
14 % (real->natural) ~'discretisation function itself',
|
samer@4
|
15 % (natural->[[2]]) ~'reverse map, returns upper and lower bin edges'
|
samer@4
|
16 % -> dmap.
|
samer@4
|
17 %
|
samer@4
|
18 % The map consists of a mapping between the integers 1..N and a set
|
samer@4
|
19 % of consectutive half-open intervals. The discretisation is performed
|
samer@4
|
20 % by mapping a real number to the index of the half-open interval in
|
samer@4
|
21 % which it lies.
|
samer@4
|
22 %
|
samer@4
|
23 % The map be applied using parentheses (function application), eg
|
samer@4
|
24 %
|
samer@4
|
25 % m = linmap(-2,10,24);
|
samer@4
|
26 % i = m(2.45);
|
samer@4
|
27 %
|
samer@4
|
28 % If the input is outside, the map returns -Inf or Inf.
|
samer@4
|
29 %
|
samer@4
|
30 % METHODS
|
samer@4
|
31 % bins - return some or all of the intervals in the map
|
samer@4
|
32 % cardr - number of intervals in a map
|
samer@4
|
33 % centres - return the centres of some or all of the intervals
|
samer@4
|
34 % domain - return the interval which is union of all the intervals
|
samer@4
|
35 % edges - return the set of all the interval endpoints.
|
samer@4
|
36 % range - the set 1..N
|
samer@4
|
37 % map_clamped - apply map with output clamped to valid range
|
samer@4
|
38 % feval - apply map to real numbers
|
samer@4
|
39 %
|
samer@4
|
40 % dmap also implements subsref, tostring and display
|
samer@4
|
41 %
|
samer@4
|
42 % linmap, binmap, edgemap - create convenient discretisation maps
|
samer@4
|
43
|
samer@4
|
44 function M=dmap(card,mapfn,revmap)
|
samer@4
|
45 if nargin==0, M=dmap(1,@floor,@(i)[i-1;i]); % dummy map
|
samer@4
|
46 elseif isa(card,'dmap'), M=card
|
samer@4
|
47 else
|
samer@4
|
48 M.cardr_=card;
|
samer@4
|
49 M.map_rn=mapfn;
|
samer@4
|
50 M.map_ni=revmap;
|
samer@4
|
51
|
samer@4
|
52 intervals=revmap([1 M.cardr_]);
|
samer@4
|
53 M.domain_=[intervals(1,1) intervals(2,2)];
|
samer@4
|
54 end
|
samer@4
|
55 end
|
samer@4
|
56
|
samer@4
|
57 % bins - return bin edges of discretisation map
|
samer@4
|
58 %
|
samer@4
|
59 % bins :: dmap(N) -> [[2,N]]~'all the bins'.
|
samer@4
|
60 % bins :: dmap(N), [[M]->[N]]~'M bin indices'-> [[2,M]]~'selected bins'.
|
samer@4
|
61 function X=bins(M,I),
|
samer@4
|
62 if nargin<2,I=range(M); end
|
samer@4
|
63 X=M.map_ni(I);
|
samer@4
|
64 end
|
samer@4
|
65
|
samer@4
|
66 % cardr - Cardinality of range (ie size)
|
samer@4
|
67 %
|
samer@4
|
68 % cardr :: dmap(N) -> N:natural.
|
samer@4
|
69 function N=cardr(M), N=M.cardr_; end
|
samer@4
|
70
|
samer@4
|
71 % centres - Return centres of discretisation bins
|
samer@4
|
72 %
|
samer@4
|
73 % centres :: dmap(N) -> [[N]] ~'centres of all bins'.
|
samer@4
|
74 % centres :: dmap(N), [[M]->[N]] -> [[M]] ~'centres of selected bins'.
|
samer@4
|
75 function X=centres(M,I)
|
samer@4
|
76 if nargin<2,I=range(M); end
|
samer@4
|
77 X=mean(M.map_ni(I),1);
|
samer@4
|
78 end
|
samer@4
|
79
|
samer@4
|
80 % tostring - string describing dmap
|
samer@4
|
81 %
|
samer@4
|
82 % tostring :: dmap(N) -> string.
|
samer@4
|
83 function s=tostring(M)
|
samer@4
|
84 s=sprintf('dmap::%g--%g->[%d]',M.domain_(1),M.domain_(2),M.cardr_);
|
samer@4
|
85 end
|
samer@4
|
86
|
samer@4
|
87 % DISPLAY - Display dmap object as string
|
samer@4
|
88 function display(c), disp(tostring(c)); end
|
samer@4
|
89
|
samer@4
|
90 % domain - return domain of a dmap
|
samer@4
|
91 %
|
samer@4
|
92 % domain :: dmap(N) -> [[2]]
|
samer@4
|
93 function X=domain(M), X=M.domain_; end
|
samer@4
|
94
|
samer@4
|
95 % edges - return edges of discretisation map
|
samer@4
|
96 %
|
samer@4
|
97 % edges :: dmap(N) -> [[N+1]].
|
samer@4
|
98 % edges :: dmap(N) [[M]->[N]-> [[M+1]].
|
samer@4
|
99 function X=edges(M,I),
|
samer@4
|
100 if nargin<2,I=range(M); end
|
samer@4
|
101 Y=M.map_ni(I);
|
samer@4
|
102 X=[Y(1,1) Y(2,:)];
|
samer@4
|
103 end
|
samer@4
|
104
|
samer@4
|
105 function I=feval(M,X), I=M.map_rn(X); end
|
samer@4
|
106 % if isa(M,'dmap'), I=M.map_rn(X);
|
samer@4
|
107 % else I=builtin('feval',M,X); end
|
samer@4
|
108 % end
|
samer@4
|
109
|
samer@4
|
110 % map_clamped - Apply clamped discretisation map
|
samer@4
|
111 %
|
samer@4
|
112 % map_clamped :: dmap(N), real -> [N].
|
samer@4
|
113 function I=map_clamped(M,x)
|
samer@4
|
114 I=M.map_rn(x);
|
samer@4
|
115 I(I==-inf)=1;
|
samer@4
|
116 I(I==inf) =M.cardr_;
|
samer@4
|
117 end
|
samer@4
|
118
|
samer@4
|
119 % range - range of dmap (set of natural numbers)
|
samer@4
|
120 %
|
samer@4
|
121 % range :: dmap(N) -> 1..N:[[N]->natural].
|
samer@4
|
122 function R=range(M), R=1:M.cardr_; end
|
samer@4
|
123
|
samer@4
|
124 % SUBSREF - Subscript referencing for DMAP objects
|
samer@4
|
125 %
|
samer@4
|
126 % I=MAP(X) - Apply real-to-natural map to all elements of X
|
samer@4
|
127 function I=subsref(M,S)
|
samer@4
|
128 s=S(1);
|
samer@4
|
129 switch s.type
|
samer@4
|
130 case '()'
|
samer@4
|
131 I=M.map_rn(s.subs{1});
|
samer@4
|
132 end
|
samer@4
|
133 end
|
samer@4
|
134 end
|
samer@4
|
135 end
|
samer@4
|
136
|