From octave-sources-request at bevo dot che dot wisc dot edu Fri Jan 21 05:01:13 2000 Subject: mvo.m : matrix-by-vector operators From: Rolf Fabian To: "'octave-sources UWISC'" Date: Fri, 21 Jan 2000 12:00:09 +0100 Hi, besides the ,dot'-class , e.g. .+ .* .^ ... (operating elementwise) ,non-dot' class e.g. + * ^ ... (operating in matrix sense) this work proposes a third class of binary operators, namely a ,colon'-class e.g. :+ :* *^ ... for matrix-by-vector operations of compatible sizes. E.g. A:+V operation should depend on vector V orientation and be allowed, if either V has the same number of columns or same number of rows as matrix A. If of compatible size, each element of V should operate on the corresponding column of A (if V is ROW) or row of A (if V is a COL) Preferentially, I would like seeing it implemented into command parser. I don't see any reasons for conflicts or name collisions, but I might be in error? In order to illustrate this conceptual idea, I've written a simple script file ,mvo.m' (attached), which mimics the desired behaviour. Comments, suggestions welcome! Bye Rolf %USAGE B = mvo( A, V {,op=':*'} ) % % matrix-by-vector binary operation % %A matrix %V vector --- COMPATIBILITY CONDITION --- % ( I) ROW : length ( V ) equals columns( A ) % then element V(j) operates on A(:,j) % (II) COL : length ( V ) equals rows( A ) % then element V(j) operates on A(j,:) %op operator one of ':*',':/',':+',':-',':^',':|',':&',':**',':\\' % %EXAS (I) B = mvo ( A, ROW V, ':+') % A = [ A11, A12, A13 ] B = [ A11+V1, A12+V2, A13+V3 ] % [ A21, A22, A23 ] [ A21+V1, A22+V2, A23+V3 ] % + + + corresponding element of V % V = [ V1 , V2 , V3 ] added to each column of A % % (II) B = mvo ( A, COL V, ':/') same A as above % compatible COL V must have 2 elements V=[V1;V2] % B = [ A11/V1 , A12/V1, A13/V1 ] corresponding element of V % [ A21/V2 , A22/V2, A23/V2 ] divides each row of mat A % %NOTE - op=':*' by default multiplication % - escaped backslash operator necessary %AUTHOR (c) Rolf Fabian 000121V function B=mvo(A,V,op) ft='mvo error: '; et='... quit.\n'; m0='nargin '; m1='unknown operator '; m2='incompatible size V '; if (nargin<2||nargin>3) fprintf(stderr,"%s%s%s",ft,m0,et); B=[]; return; end if (nargin<3) op=':*'; end [ra,ca]=size( A); [rv,cv]=size( V); [ro,co]=size(op); %translate the only (non-escaped) 3 char op ':**' to equivalent 2 chars if ( ro==1&&co==3&&op==':**') op=':^'; co=2; end if ( ro!=1||co>2||op(1)!=':'||\ all(toascii(['*','/','+','-','^','|','&','\\'])-toascii(op(2))) ) fprintf(stderr,"%s%s'%s'%s",ft,m1,op,et); B=[]; return; end if (op==':&'||op==':|') op=op(2); %V2.0.13: '.&' and '.|' fail else op=['.',op(2)]; end B = A*NaN; %init output at same size if (rv==1&&cv==ca) %ROW V for j=1:ca eval(sprintf('B(:,j)=A(:,j) %s V(j);',op)); end elseif (cv==1&&rv==ra) %COL V for j=1:ra eval(sprintf('B(j,:)=A(j,:) %s V(j);',op)); end else fprintf(stderr,"%s%s[%d,%d] %s",ft,m2,rv,cv,et); B=[]; end end