From octave-sources-request at bevo dot che dot wisc dot edu Wed Jan 10 06:05:02 2001 Subject: Fw: Re: improved/corrected dot.m From: Melqart To: Date: Wed, 10 Jan 2001 13:03:22 +0100 Oops, sorry, I only replaid to Rolf (sorry for the duplicate mail) Here is the message: Begin forwarded message: Date: Wed, 10 Jan 2001 12:53:13 +0100 From: Melqart To: Rolf Fabian Subject: Re: improved/corrected dot.m You are right : version 5.0.0.4064 gives : >> x=[1,1-i,1+i] x = 1.0000 1.0000- 1.0000i 1.0000+ 1.0000i >> y=[3-i,3+i,2i] y = 3.0000- 1.0000i 3.0000+ 1.0000i 0+ 2.0000i >> dot(x,y) ans = 7.0000+ 5.0000i Note : dot(x,y) can also work on N-dimensional arrays. It works then on the first non-singleton dimension of each array. An optional third argument can alternatively be provided to specify the dimension on which the scalar product has to be computed. Rolf Fabian wrote : > Appended is an improved/corrected version of > the dot script function, which allows for switching between > the two legal flavours of an inner product definition. > They differ by returning complex conjugated results > for input of complex vectors. > To get more info about modes (A),(B) see the help header. > > Example > :)x=[1,1-i,1+i]; y=[3-i,3+i,2i]; > :)dot(x,y) #no ,global dot_arg_conj_prefer' defined, use definition (A) > by default > ans = 7 - 5i > > > #Now let's switch to less common definition (B) > #e.g. used by MatLab > # I conclude this from their online-help page, which says: > # << C = dot(A,B) returns the scalar product of the vectors A and B. > ... > # When A and B are both column vectors, dot(A,B) is the same as A'*B. > >> > # > #( I've no personal access to MatLab. Does anybody volunteer to mail > # me the result of Matlab's standard dot function with input vectors > # x,y given above ?) > > :)global dot_arg_conj_prefer; > dot_arg_conj_prefer=1; > :)dot(x,y) > ans = 7 + 5i > > :)dot_arg_conj_prefer=2; #switch back to (A) > > > Any comments welcome. > > Bye Rolf > > > --------------- snip ----------- > > ## -*- texinfo -*- > ## at deftypefn {Function File} {} dot (@var{x}, @var{y}) > ## DOT PRODUCT of two vectors > ## > ##The dot (inner) product may be legally defined in two flavours: > ##(A) at var{x}(1)*conj(@var{y}(1)) + .. + @var{x}(N)*conj(@var{y}(N)) > ## this definition applies complex conjugation to the second > ## argument and is preferred in most linear algebra textbooks. > ## It is choosen to be the default method in current function. > ## > ##(B) at conj(var{x}(1))*@var{y}(1) + .. + @conj(var{x}(N))*@var{y}(N) > ## this alternate definition prefers complex conjugation > ## of the first argument and is used as matlab's default. > ## > ##Both definitions are identical for vectors at var{x}, @var{y} in > ##euklidian (real) space, but differ in unitary (complex) space. > ##Switching between both definitions may be controlled by setting > ##variable 'global dot_arg_conj_prefer' =2 (A) or =1 (B). > ## > ##simple properties > ##dot( at var(x),C*@var(y)) == conj( C ) * dot(@var(y),@var(x)) (A) > ## == C * dot( at var(y),@var(x)) (B) > ##dot(C* at var(x),@var(y)) == C * dot(@var(y),@var(x)) (A) > ## == conj( C ) * dot( at var(y),@var(x)) (B) > ##dot( ) == conj( dot( at var(y),@var(x) ) ) (A ) == conj( dot( @var(y),@var(x) ) ) (A|B) > ## > ##The input vectors may pocess any combination of row/column > ##orientation, but their length N must be the same. > ## at end deftypefn > > ##AUTHOR Rolf Fabian Jan. 10, 2001 > ## published under current GNU GENERAL PUBLIC LICENSE > > function z = dot(x,y) > > if (nargin != 2) > usage ("dot( x,y )"); > endif > > if exist("dot_arg_conj_prefer")==1 > global dot_arg_conj_prefer; > DACP=dot_arg_conj_prefer; > if all([1,2]!=DACP), error(\ > "dot: invalid global \'dot_arg_conj_prefer\' .. set to 1|2"); > endif > else DACP=2; > endif > > save_do_fortran_indexing = do_fortran_indexing; > > unwind_protect > do_fortran_indexing = 1; > > if ( any(size(x)==1) && any(size(y)==1) && length(x)==length(y) ) > if DACP==2 > z = x(:).' * conj( y(:) ); #conj 2nd arg > elseif DACP==1 > z = x(:)' * y(:); #conj 1st arg > endif > else > error ("dot: both arguments must be vectors of the same length"); > endif > > unwind_protect_cleanup > > do_fortran_indexing = save_do_fortran_indexing; > > end_unwind_protect > > endfunction > > > > > ------------------------------------------------------------- > Octave is freely available under the terms of the GNU GPL. > > Octave's home on the web: http://www.octave.org > How to fund new projects: http://www.octave.org/funding.html > Subscription information: http://www.octave.org/archive.html > ------------------------------------------------------------- > ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------