samer@45
|
1 % vecgen - Finite vector generator base class
|
samer@45
|
2
|
samer@45
|
3 classdef vec
|
samer@45
|
4 properties (SetAccess=private,GetAccess=private)
|
samer@45
|
5 fillfn % N:natural -> [[N]]
|
samer@45
|
6 label % string
|
samer@45
|
7 end
|
samer@45
|
8 methods (Sealed)
|
samer@45
|
9 function o=vec(fillfn,label),
|
samer@45
|
10 o.fillfn=fillfn;
|
samer@45
|
11 if nargin<2, o.label=tostring(fillfn);
|
samer@45
|
12 else o.label=label; end
|
samer@45
|
13 end
|
samer@45
|
14 function x=fill(o,N), x=o.fillfn(N); end
|
samer@45
|
15 function display(o), disp([' ' tostring(o) ' :: vec']); end
|
samer@45
|
16 function s=tostring(o), s=o.label; end
|
samer@45
|
17 function c=log(a), c=apply(@log,a); end
|
samer@45
|
18 function c=log2(a), c=apply(@log2,a); end
|
samer@45
|
19 function c=log10(a), c=apply(@log10,a); end
|
samer@45
|
20 function c=exp(a), c=apply(@exp,a); end
|
samer@45
|
21 function c=sqrt(a), c=apply(@sqrt,a); end
|
samer@45
|
22 function c=uminus(a), c=unop(@uminus,a,str.prefix('-')); end
|
samer@45
|
23 function c=not(a), c=unop(@not,a,str.prefix('~')); end
|
samer@45
|
24 function c=mtimes(a,b), c=binop(@times,a,b,str.infix('*')); end
|
samer@45
|
25 function c=mrdivide(a,b),c=binop(@rdivide,a,b,str.infix('/')); end
|
samer@45
|
26 function c=plus(a,b), c=binop(@plus,a,b,str.infix('+')); end
|
samer@45
|
27 function c=minus(a,b), c=binop(@minus,a,b,str.infix('-')); end
|
samer@45
|
28 function c=eq(a,b), c=binop(@eq*@double,a,b,str.infix('==')); end
|
samer@45
|
29 function c=lt(a,b), c=binop(@lt,a,b,str.infix('<')); end
|
samer@45
|
30 function c=le(a,b), c=binop(@le,a,b,str.infix('<=')); end
|
samer@45
|
31 function c=gt(a,b), c=binop(@gt,a,b,str.infix('>')); end
|
samer@45
|
32 function c=ge(a,b), c=binop(@ge,a,b,str.infix('>=')); end
|
samer@45
|
33 function c=ne(a,b), c=binop(@ne,a,b,str.infix('=')); end
|
samer@45
|
34 function c=or(a,b), c=binop(@or,a,b,str.infix('|')); end
|
samer@45
|
35 function c=and(a,b), c=binop(@and,a,b,str.infix('&')); end
|
samer@45
|
36
|
samer@45
|
37 function o=apply(f,o), o=unop(f,o,str.functor(func2str(f))); end
|
samer@45
|
38 end
|
samer@45
|
39 end
|
samer@45
|
40
|
samer@45
|
41 function v=unop(op,src,rep)
|
samer@45
|
42 v=vec(@(N)op(src.fill(N)), rep(tostring(src)));
|
samer@45
|
43 end
|
samer@45
|
44
|
samer@45
|
45 function c=binop(op,a,b,rep)
|
samer@45
|
46 if isa(a,'vec') && isa(b,'vec')
|
samer@45
|
47 c=zipper(op,rep,a,b);
|
samer@45
|
48 elseif isa(a,'vec'),
|
samer@45
|
49 c=unop(@(A)op(A,b),a,@(A)rep(A,tostring(b)));
|
samer@45
|
50 else
|
samer@45
|
51 c=unop(@(B)op(a,B),b,@(B)rep(tostring(a),B));
|
samer@45
|
52 end
|
samer@45
|
53 end
|
samer@45
|
54
|
samer@45
|
55 function v=zipper(op,rep,varargin)
|
samer@45
|
56 strs=map(@tostring,varargin);
|
samer@45
|
57 v=vec(@f, rep(strs{:}));
|
samer@45
|
58 function x=f(N), yx=map(@(s)fill(s,N),varargin); x=op(yx{:}); end
|
samer@45
|
59 end
|