outer - outer product implementation.

Muthiah Annamalai <[email protected]> Mon, 24 Apr 2006 21:02:03 -0700 (PDT)
Newsgroups gmane.comp.gnu.octave.sources
Message-ID <[email protected]>
--0-588001722-1145937723=:21593
Content-Type: multipart/alternative; boundary="0-382265567-1145937723=:21593"

--0-382265567-1145937723=:21593
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Paul,
I have implemented the changes you had suggested in your earlier e-mail,
and I think this is a complete implementation of outer() function as 
specified in: http://www.gnu.org/software/octave/projects.

I have a detailed test-case and the code, is cleaner compared to earlier version.
I think it could be useful to someone who is looking for this function, but 
Im not so sure if it must go into Octave or Octave-forge.

Thanks
Muthu


		
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.  Great rates starting at 1&cent;/min.
--0-382265567-1145937723=:21593
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Paul,<br>I have implemented the changes you had suggested in your earlier e-mail,<br>and I think this is a complete implementation of outer() function as <br>specified in: http://www.gnu.org/software/octave/projects.<br><br>I have a detailed test-case and the code, is cleaner compared to earlier version.<br>I think it could be useful to someone who is looking for this function, but <br>Im not so sure if it must go into Octave or Octave-forge.<br><br>Thanks<br>Muthu<br><br><p>
		<hr size=1>Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. <a href="http://us.rd.yahoo.com/mail_us/taglines/postman7/*http://us.rd.yahoo.com/evt=39666/*http://beta.messenger.yahoo.com"> Great rates starting at 1&cent;/min.
--0-382265567-1145937723=:21593--
--0-588001722-1145937723=:21593
Content-Type: text/plain; name="outer.cpp"
Content-Description: 593200191-outer.cpp
Content-Disposition: inline; filename="outer.cpp"

/* 
 * (C) 2006,April, Muthiah Annamalai, FSF. <[email protected]>
 *    An implementation of the 'outer'-product function as specified in the
 *    octave-projects page, at www.octave.org.
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details. 
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* Include Octave header files */
#include<iostream>
#include<octave/oct.h>
#include<octave/parse.h>
#include<octave/dynamic-ld.h>
#include<octave/oct-map.h>
#include<octave/oct-stream.h>
#include<octave/ov.h>
#include<octave/parse.h>

#include<octave/Matrix.h>
#include<octave/ov-cx-mat.h>
#include<octave/ov-list.h>
#include<octave/ov-base-mat.h>

#include<octave/quit.h>
#include<octave/error.h>


#define IS_VECTOR(arg) ((arg.is_matrix_type()&& !arg.is_char_matrix() &&  (arg.columns()==1 || arg.rows()==1))? true : false)

/*
 * fcn_handle is what I want.
 * it has a reference to a octave_function
 */

/*
 * ISSUES:
 *
 * Can we take X or Y as a scalar iff the other one is a vector? Yes.
 * If we only take the names of functions using the @FUNC_NAME notation,
 * we will be restricted to taking only names of loaded functions. Yes.
 * So how do we make a deal? 
 *
 */

DEFUN_DLD(outer,args,,
	  "function outer(VecX,VecY,opt:function)"\
	  "Return a MxN matrix with f(x,y) evaluated at x,y position"\
	  "for each x,y in VecX,VecY respectively"\
	  "If not function is provided, use '*' as default operation"\
	  "Function can be provided using @FUNCTION_NAME "\
 	  "as a string. Both are compatible. This is the outer product.")
{
  int m=0,n=0,aLength=0;
  ColumnVector cv[2];
  bool function_present=false;
  octave_function *func=NULL;
  octave_value_list erval;
  const char *func_name=NULL;
  
  /* hope to use commutativity of the operators someday */
  bool is_commutative=false;

  aLength=args.length();

  if(aLength < 2)
    {
      error("Usage: outer(VecX,VecY,opt:function)");
      return erval;
    }

  if(!(IS_VECTOR(args(0)) || IS_VECTOR(args(1))))
    {
      error("Usage: outer(VecX,VecY,opt:function);\n Invalid argument arg- [1 or 2]Expected Row/Column Vector");
      return erval;
    }

  /* 
   * FIXME: [Support for String-Name functions]
   * Also allow functions to be passed by their names, and then replace the
   * name with a reference to the function-, later.
   */
  if(aLength>=3 && (args(2).is_function_handle() /* ||args(2).is_string() */))
    {
      function_present=true;
      is_commutative=false;
      
      func=args(2).function_value();
    }

  cv[0]=ColumnVector(args(0).vector_value());
  cv[1]=ColumnVector(args(1).vector_value());

  m=cv[0].length();
  n=cv[1].length();

  Matrix rmat(m,n);
  octave_value_list ovl;
  
  /*
   * If operator is commutative, then
   * do we want to handle this as a special
   * case?
   *
   */

  if(function_present && func!=NULL)
    {
      /* try a dummy invoke on the function,
       and verify if the return value is there. Otherwise
      we report and error & die.*/

      ovl(0)=0;
      ovl(1)=0;

      func_name = func->name().c_str();

      if(feval(func,ovl).length() < 1)
	{
	  error("The function %s must return atleast one value, on invocation\n",func_name);
	  return octave_value(-1);
	}

      if(error_state)
	{
	  error("Exception: Cannot find function specified");
	  return erval;
	}
      
      /* FIXME: <Vectorize Code>
       * Paul says do this thing as a single loop.
       * Iterate over Columns, taking one full column at a time, 
       * and save the result on the columns of the matrix.
       * Atleast this is possible for builtins that take arguments
       * over columns/rows.
       */
      for(int itr=0;itr<m;itr++)
	for(int icol=0;icol<n;icol++)
	  {

	    OCTAVE_QUIT;
	      
	    ovl(0)=cv[0].elem(itr);
	    ovl(1)=cv[1].elem(icol);

	    rmat.elem(itr,icol)=feval(func,ovl)(0).double_value();
	    if(error_state)
	      {
		error("Exception: while evaluating %s with arguments %g %g\n",func_name,cv[0].elem(itr),cv[1].elem(icol));
		return erval;
	      }
	  }
    }
  else 
    {
      /* do a simple product */
      rmat=cv[0]*cv[1].transpose();
    }

  return octave_value(rmat);
}
/* g++ -fpic outer.cpp -shared -o outer.oct -Wall -ggdb -Wall -Wunused -Wconversion -fno-exceptions -DDEBUG=1  `mkoctfile -p INCFLAGS` */

--0-588001722-1145937723=:21593
Content-Type: application/octet-stream; name="test.m"
Content-Transfer-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pb25zIHRocm93IGVycm9ycyBh
dCBPY3RhdmUgcHJvbXB0IGl0c2VsZi4nKTsKdHJ5Cm91dGVyKFsxNCAyMCAz
NSA0MF0sWzddLEBid2FuZCkgJW5vbi1leGlzdGVudCBmdW5jdGlvbnMgdGhy
b3cgZXJyb3JzIGF0IE9jdGF2ZSBwcm9tcHQgaXRzZWxmLgpjYXRjaAogIGRp
c3AoJ0V4Y2VwdGlvbicpCmVuZAoKdHJ5CmRpc3AoJ0JvdGggU2NhbGFyJykK
b3V0ZXIoWzFdLFsyXSkgJWVycm9yIApjYXRjaAogIGRpc3AoJ0V4Y2VwdGlv
bicpCmVuZAo=

--0-588001722-1145937723=:21593
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Octave-sources mailing list
[email protected]
https://www.cae.wisc.edu/mailman/listinfo/octave-sources

--0-588001722-1145937723=:21593--