samer@45: % vecgen - Finite vector generator base class samer@45: samer@45: classdef vec samer@45: properties (SetAccess=private,GetAccess=private) samer@45: fillfn % N:natural -> [[N]] samer@45: label % string samer@45: end samer@45: methods (Sealed) samer@45: function o=vec(fillfn,label), samer@45: o.fillfn=fillfn; samer@45: if nargin<2, o.label=tostring(fillfn); samer@45: else o.label=label; end samer@45: end samer@45: function x=fill(o,N), x=o.fillfn(N); end samer@45: function display(o), disp([' ' tostring(o) ' :: vec']); end samer@45: function s=tostring(o), s=o.label; end samer@45: function c=log(a), c=apply(@log,a); end samer@45: function c=log2(a), c=apply(@log2,a); end samer@45: function c=log10(a), c=apply(@log10,a); end samer@45: function c=exp(a), c=apply(@exp,a); end samer@45: function c=sqrt(a), c=apply(@sqrt,a); end samer@45: function c=uminus(a), c=unop(@uminus,a,str.prefix('-')); end samer@45: function c=not(a), c=unop(@not,a,str.prefix('~')); end samer@45: function c=mtimes(a,b), c=binop(@times,a,b,str.infix('*')); end samer@45: function c=mrdivide(a,b),c=binop(@rdivide,a,b,str.infix('/')); end samer@45: function c=plus(a,b), c=binop(@plus,a,b,str.infix('+')); end samer@45: function c=minus(a,b), c=binop(@minus,a,b,str.infix('-')); end samer@45: function c=eq(a,b), c=binop(@eq*@double,a,b,str.infix('==')); end samer@45: function c=lt(a,b), c=binop(@lt,a,b,str.infix('<')); end samer@45: function c=le(a,b), c=binop(@le,a,b,str.infix('<=')); end samer@45: function c=gt(a,b), c=binop(@gt,a,b,str.infix('>')); end samer@45: function c=ge(a,b), c=binop(@ge,a,b,str.infix('>=')); end samer@45: function c=ne(a,b), c=binop(@ne,a,b,str.infix('=')); end samer@45: function c=or(a,b), c=binop(@or,a,b,str.infix('|')); end samer@45: function c=and(a,b), c=binop(@and,a,b,str.infix('&')); end samer@45: samer@45: function o=apply(f,o), o=unop(f,o,str.functor(func2str(f))); end samer@45: end samer@45: end samer@45: samer@45: function v=unop(op,src,rep) samer@45: v=vec(@(N)op(src.fill(N)), rep(tostring(src))); samer@45: end samer@45: samer@45: function c=binop(op,a,b,rep) samer@45: if isa(a,'vec') && isa(b,'vec') samer@45: c=zipper(op,rep,a,b); samer@45: elseif isa(a,'vec'), samer@45: c=unop(@(A)op(A,b),a,@(A)rep(A,tostring(b))); samer@45: else samer@45: c=unop(@(B)op(a,B),b,@(B)rep(tostring(a),B)); samer@45: end samer@45: end samer@45: samer@45: function v=zipper(op,rep,varargin) samer@45: strs=map(@tostring,varargin); samer@45: v=vec(@f, rep(strs{:})); samer@45: function x=f(N), yx=map(@(s)fill(s,N),varargin); x=op(yx{:}); end samer@45: end