help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new types and INSTALL_BINOP


From: John W. Eaton
Subject: new types and INSTALL_BINOP
Date: Wed, 28 Feb 2001 20:56:33 -0600

On 28-Feb-2001, address@hidden <address@hidden> wrote:

| I'm working on adding complex sparse matrix
| support to my octave sparse matrix library.
| (http://cvs.sourceforge.net/
|    cgi-bin/cvsweb.cgi/octave/dld/sparse/?cvsroot=octave)
| 
| It's going fairly well, except I seem to be 
| required to add an enourmous amount of binary
| operators for each type combination

The advantage of defining the mixed-type operations is that they can
be significantly more efficient (memory and time) than doing
conversions when you are working on large matrices.  But they do have
the problem of requiring many functions to be defined (n-ops x n-types
x n-types).  But, you can define a type conversion function so that
you don't have to define all the binary ops.  It makes sense to do
this, and then only write the code for the operations that really
benefit from having a special mixed-type operation defined.

| for example, for addition, I need to define
| the following BINOPS for each operator.
| For example, for (op_add, ie + ) we have:
| 
| octave_sparse + octave_scalar
| octave_sparse + octave_complex
| octave_sparse + octave_matrix
| octave_sparse + octave_complex_matrix
| octave_sparse + octave_sparse
| octave_sparse + octave_complex_sparse
| 
| octave_complex_sparse + octave_scalar
| octave_complex_sparse + octave_complex
| octave_complex_sparse + octave_matrix
| octave_complex_sparse + octave_complex_matrix
| octave_complex_sparse + octave_sparse
| octave_complex_sparse + octave_complex_sparse
| 
| as well as the opposite order
| ie, octave_scalar + octave_sparse
| 
| This turns into an enormous amount of code.
| Is there some way to simplify this.

Yes, you can define a conversion operator for the sparse classes
(presumably it would convert the sparse matrix to a full matrix).  The
conversion operator is applied whenever there is no matching operator
for the exact types.  This is handled in do_binary_op in src/ov.cc.
The logic is

  determine types of operands

  look up function to perform binary op on those exact types

  if one exists, then
    call it
  else
    attempt to convert the operands to their "default" numeric types

    if an operand changed types, then

      look up function to perform binary op given the new operand types

      if one exists, then
        call it
      else
        fail
    else
      fail

The octave_bool and octave_bool_matrix classes provide examples of how
these conversion functions are defined.

| For example, could I define
|    octave_sparse + octave_complex
| and then let octave convert scalars into
| complex so that I don't need to define
|    octave_sparse + octave_scalar

Yes, if you define the operator for octave_sparse + octave_scalar and
do the conversion there.

I don't think Octave's method of handling this is the best.  It
doesn't seem like an easy problem to me, but then I may be missing
something (clues welcome).

jwe



-------------------------------------------------------------
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
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]