octave-maintainers
[Top][All Lists]
Advanced

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

min and max functions


From: John W. Eaton
Subject: min and max functions
Date: Thu, 13 Nov 2008 17:04:42 -0500

I see that we have many versions of these functions

  min (scalar, array)
  min (array, scalar)
  min (array, array)

(and the same for max).  I think they appear in all of the following
classes:

   Matrix   FloatMatrix   ComplexMatrix   FloatComplexMatrix
   NDArray  FloatNDArray  ComplexNDArray  FloatComplexNDArray

   int8NDArray   int16NDArray   int32NDArray   int64NDArray
   uint8NDArray  uint16NDArray  uint32NDArray  uint64NDArray

I don't see an obvious place to put these templates because the class
hierarchies are

  Matrix  <- MArray2 <- Array2 <- Array
  NDArray <- MArrayN <- ArrayN <- Array

(for example) and Jaroslav has already suggested (and I agree) that it
would be best to avoid requiring that the elements of an Array object
have an operator < method.

I think it would still be good to avoid the explicit code
duplication, which I think we could do using templates like this

  template <typename ARRAY_T>
  ARRAY_T
  min (typename ARRAY_T::element_type scalar, const ARRAY_T& array)
  {
    dim_vector dv = array.dims ();
    octave_idx_type nel = dv.numel ();

    ARRAY_T result (dv);

    if (nel != 0)
      {
        for (octave_idx_type i = 0; i < nel; i++)
          {
            OCTAVE_QUIT;
            result(i) = xmin (d, array(i));
          }
      }

    return result;
  }

etc., along with suitable definition of xmin (or xmax).  I hesitate to
use < here since this should work for complex types and we have to do
the right thing for NaN and I think it would be best to hide all of
that inside a function.  Otherwise, it is not as easy to take
advantage of the template for integer types, or we need to define a
bunch of specializations, which goes against my reason for trying to
eliminate the duplication...

So, if we make a change like this, where should we put these
templates?  Do we need an octave-algorithms.h header file?

OTOH, there is no difference between the template for the Matrix and
NDArray objects, so they could easily go in the base class if we had
an acceptable one that was common to both classes.

Thinking about this a little more, is the Matrix class in the wrong
spot in the hierarchy?  Is it necessary to have the Array2 and MArray2
classes now (they really only exist for historical reasons as Octave
did not originally have N-d arrays)?  Should the Matrix class just be
an NDArray object with the added constraint that there are only two
dimensions?  If so, what would be the right way to enforce that?

Comments?

jwe


reply via email to

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