Mojo example class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Mitchley

    Mojo example class

    Hi

    I have a rather complicated class for performing matrix operations using
    complex arithmetic.

    I have written two classes, CComplexMatrix and CComplexMatrixT emp, and have
    been attempting to remove unnecessary object copies by converting to the
    temp class for intermediate operations. The temp class reuses allocated
    space as is necessary.

    While ironing out some bugs and attempting to improve the performance of
    certain operations, I have been tempted to try and convert the entire class
    to use the Mojo conventions outlined by Andrei Alexandrescu
    (http://www.cuj.com/documents/s=8246/...2102alexandr/).

    I was wondering if anyone has a complete example of a class that has been
    "Mojo-fied", that I could use for comparison and inspiration (e.g. a
    complete String class). There are a couple of things that I am hazy about,
    and I think I could learn a lot from a complete example - as opposed to
    posting about specific issues. The article provides all of the bones, but I
    am interested in a little bit of meat!

    I can email the source code of my matrix class to anyone interested.

    Ryan




  • Pavel Vozenilek

    #2
    Re: Mojo example class

    "Ryan Mitchley" <rmitchle@remov ethis.worldonli ne.co.za> wrote
    [color=blue]
    > I have been tempted to try and convert the entire class
    > to use the Mojo conventions outlined by Andrei Alexandrescu
    > (http://www.cuj.com/documents/s=8246/...2102alexandr/).
    >
    > I was wondering if anyone has a complete example of a class that has been
    > "Mojo-fied"[/color]
    Patch to STLport containers:



    /Pavel

    Comment

    • Ryan Mitchley

      #3
      Re: Mojo example class

      Thanks, Pavel!

      I had a look through some of the code, and may have been looking in the
      wrong places - but I didn't find too many examples of some of the techniques
      outlined in Andrei's article. I saw several constructors taking mojo
      temporary parameters, but that was about it. What about mojo::fnresult? Does
      mojo::constant ever get used?

      For the record, the header file for the class that I would like to convert
      to using the MoJo conventions is attached below. It has a couple of issues
      that I know are frowned upon by C++ gurus, but hopefully not too many. I am
      not really sure how many of these functions should stay, and how many should
      go. Which assignment operators do I need? Should *everything* that returns a
      matrix return a mojo::fnresult< >? I would guess that everything that
      currently takes a CComplexMatrixT emp as a parameter would now take a
      mojo::temporary <> parameter? I think I need a couple of more clues to be
      able to use this technique . . . I am probably going to go ahead and try it,
      anyway, but I don't rate my chances too highly at the moment!

      Thanks for any help!

      Ryan

      -----------------------------------

      // ComplexMatrix.h : interface for the CComplexMatrix class.
      //
      //////////////////////////////////////////////////////////////////////

      #if !defined(__COMP LEXARRAY_H)
      #define __COMPLEXARRAY_ H

      #if _MSC_VER > 1000
      #pragma once
      #endif // _MSC_VER > 1000

      class CComplexMatrixT emp;
      //class CComplexMatrix;

      #include "MathCore/include/Matrix.h"
      #include <complex>

      using namespace std;

      #define COMPLEX_WIDTH 8
      #define COMPLEX_PRECISI ON 4

      class CComplexMatrix : public CMatrix
      {
      public:
      static registerInFacto ry<CBase, CComplexMatrix> regClass;
      // Data generation
      CComplexMatrix & FillWithNoise(N OISE_DISTRIB Distribution, NOISE_SPECT
      Spectrum,
      FTYPE fMean, FTYPE fVariance, size_t nM=0, size_t nN=0);
      operator complex<FTYPE>* () const { return m_cfArray; }
      CComplexMatrix & operator=(CComp lexMatrixTemp another);
      CComplexMatrix operator=(const CComplexMatrix &another);
      // Element-wise array operators
      CComplexMatrixT emp operator+(const CComplexMatrix &another);
      CComplexMatrixT emp operator+(CComp lexMatrixTemp another);
      CComplexMatrixT emp operator-(const CComplexMatrix &another);
      CComplexMatrixT emp operator-(CComplexMatrix Temp another);
      CComplexMatrixT emp operator%(const CComplexMatrix &another);
      CComplexMatrixT emp operator*(const CComplexMatrix &another);
      CComplexMatrixT emp operator*(CComp lexMatrixTemp x);
      CComplexMatrixT emp operator%(CComp lexMatrixTemp another);
      CComplexMatrixT emp operator/(const CComplexMatrix &another);
      CComplexMatrixT emp operator/(CComplexMatrix Temp another);
      CComplexMatrix & operator+=(CCom plexMatrixTemp another);
      CComplexMatrix & operator+=(cons t CComplexMatrix &another);
      CComplexMatrix & operator-=(CComplexMatri xTemp another);
      CComplexMatrix & operator-=(const CComplexMatrix &another);
      CComplexMatrix & operator%=(CCom plexMatrixTemp another);
      CComplexMatrix & operator%=(cons t CComplexMatrix &another);
      CComplexMatrix & operator/=(CComplexMatri xTemp another);
      CComplexMatrix & operator/=(const CComplexMatrix &another);
      // Operators with scalar arguments
      CComplexMatrixT emp operator+(const FTYPE fScalar);
      CComplexMatrixT emp operator-(const FTYPE fScalar);
      CComplexMatrixT emp operator*(const FTYPE fScalar);
      CComplexMatrixT emp operator/(const FTYPE fScalar);
      CComplexMatrixT emp operator+(const complex<FTYPE> fScalar);
      CComplexMatrixT emp operator-(const complex<FTYPE> fScalar);
      CComplexMatrixT emp operator*(const complex<FTYPE> fScalar);
      CComplexMatrixT emp operator/(const complex<FTYPE> fScalar);
      CComplexMatrix operator+=(cons t FTYPE fScalar);
      CComplexMatrix operator-=(const FTYPE fScalar);
      CComplexMatrix operator*=(cons t FTYPE fScalar);
      CComplexMatrix operator/=(const FTYPE fScalar);
      CComplexMatrixT emp operator~() const;
      complex<FTYPE> operator[](const size_t n) { return m_cfArray[n]; }
      // N.B. The () (matrix subscript) operators assume that the first
      // element row or column is referenced as item 1, not item 0 as
      // is conventional when programming in C. This is to preserve
      // easy compatibility with MATLAB syntax.
      complex<FTYPE> operator()(cons t size_t nM, const size_t nN) const;
      complex<FTYPE>& operator()(cons t size_t nM, const size_t nN);
      CComplexMatrixT emp operator()(cons t size_t nM, const cColonType
      ColonParam);
      CComplexMatrixT emp operator()(cons t cColonType ColonParam, const size_t
      nN);
      CComplexMatrixT emp operator()(cons t size_t nM);

      complex<FTYPE> Min();
      complex<FTYPE> Min(size_t &nMinIndex);
      complex<FTYPE> Max();
      complex<FTYPE> Max(size_t &nMaxIndex);
      CComplexMatrix & Swap(CComplexMa trix &another);
      CComplexMatrix & ScalarMult(cons t FTYPE fScalar);
      FTYPE Norm();
      complex<FTYPE> CComplexMatrix: :DotConj(const CComplexMatrix &another);
      complex<FTYPE> Dot(const CComplexMatrix &another);
      CComplexMatrix & AddArray(const complex<FTYPE> fScaleCoef, const
      CComplexMatrix &another);
      virtual FTYPE MagSum() const;
      void Clear();
      CComplexMatrix& SetRef(complex< FTYPE> *cfArray, const size_t nM, const
      size_t nN,
      int nTranspose);
      int SetRow(const size_t nRow, const CComplexMatrix &RowVector);
      // int SetRow(const size_t nRow, const complex<FTYPE> & z1, ...);
      int SetColumn(const size_t nColumn, const CComplexMatrix &ColumnVecto r);
      CComplexMatrix& Set(const complex<FTYPE> * cfArray, const size_t nM, const
      size_t nN,
      int nTranspose);
      CComplexMatrix& Set(const FTYPE * fArray, const size_t nM, const size_t nN,
      int nTranspose);
      CComplexMatrix& Set(const FTYPE *fRealMatrix, const FTYPE *fImagArray,
      const size_t nM, const size_t nN, int nTranspose);
      CComplexMatrix& Set(const complex<FTYPE> fX, const complex<FTYPE> fY,
      const complex<FTYPE> fZ);
      CComplexMatrix& Set(const complex<FTYPE> fX, const complex<FTYPE> fY);
      CComplexMatrix& Set(const complex<FTYPE> fX);
      CComplexMatrix& Set(const FTYPE fX, const FTYPE fY, const FTYPE fZ);
      CComplexMatrix& Set(const FTYPE fX, const FTYPE fY);
      CComplexMatrix& Set(const FTYPE fX);
      CComplexMatrix& Create(const size_t nM, const size_t nN);
      CComplexMatrix( const complex<FTYPE> *cfArray, const size_t nM, const size_t
      nN,
      int nTranspose);
      CComplexMatrix( size_t NumRows, size_t NumColumns);
      CComplexMatrix( const CComplexMatrix &another); // copy constructor
      CComplexMatrix( );
      virtual ~CComplexMatrix ();
      // Serialization
      virtual void XML_WriteHead(o stream &os) const;
      virtual void XML_WriteTail(o stream &os) const;
      virtual int XML_ReadHead(li st<string> &is, list<string>::c onst_iterator&
      i);
      virtual int XML_ReadTail(li st<string> &is, list<string>::c onst_iterator&
      i);
      virtual void XML_SchemaWrite Head(ostream &out) const;
      virtual void XML_SchemaWrite Tail(ostream &out) const;
      virtual string ArrayType() const { return "complex"; }

      // friend functions
      // friend CComplexMatrixT emp::CComplexMa trixTemp(const CComplexMatrix
      &another);

      friend CComplexMatrixT emp conj(const CComplexMatrix &z);
      friend CComplexMatrixT emp ctranspose(cons t CComplexMatrix &z);
      friend CComplexMatrixT emp herm(const CComplexMatrix &z);
      friend CComplexMatrixT emp transpose(const CComplexMatrix &z);
      friend CComplexMatrixT emp tanh(const CComplexMatrix &z);
      friend CComplexMatrixT emp sinh(const CComplexMatrix &z);
      friend CComplexMatrixT emp cosh(const CComplexMatrix &z);
      friend CComplexMatrixT emp atan(const CComplexMatrix &z);
      friend CComplexMatrixT emp asin(const CComplexMatrix &z);
      friend CComplexMatrixT emp acos(const CComplexMatrix &z);
      friend CComplexMatrixT emp tan(const CComplexMatrix &z);
      friend CComplexMatrixT emp sin(const CComplexMatrix &z);
      friend CComplexMatrixT emp cos(const CComplexMatrix &z);
      friend CComplexMatrixT emp log10(const CComplexMatrix &z);
      friend CComplexMatrixT emp ln(const CComplexMatrix &z);
      friend CComplexMatrixT emp exp(const CComplexMatrix &z);
      friend CComplexMatrixT emp invcbrt(const CComplexMatrix &z);
      friend CComplexMatrixT emp cbrt(const CComplexMatrix &z);
      friend CComplexMatrixT emp invsqrt(const CComplexMatrix &z);
      friend CComplexMatrixT emp sqrt(const CComplexMatrix &z);
      friend CComplexMatrixT emp inv(const CComplexMatrix &z);
      friend CComplexMatrixT emp pow(const CComplexMatrix &mantissas, const
      CComplexMatrix &exponents);
      friend CComplexMatrixT emp pow(const CComplexMatrix &mantissas,
      CComplexMatrixT emp exponents);
      friend CComplexMatrixT emp pow(CComplexMat rixTemp mantissas, const
      CComplexMatrix &exponents);

      friend complex<FTYPE> mean(const CComplexMatrix &x);
      friend FTYPE var(const CComplexMatrix &x);
      friend FTYPE norm(const CComplexMatrix &x);
      friend CComplexMatrixT emp chol(const CComplexMatrix &z);

      // friend operators
      friend CComplexMatrixT emp operator+(CComp lexMatrixTemp lhs, const
      CComplexMatrix & another);
      friend CComplexMatrixT emp operator-(CComplexMatrix Temp lhs, const
      CComplexMatrix & another);
      friend CComplexMatrixT emp operator%(CComp lexMatrixTemp lhs, const
      CComplexMatrix & another);
      friend CComplexMatrixT emp operator/(CComplexMatrix Temp lhs, const
      CComplexMatrix & another);
      friend CComplexMatrixT emp operator+(FTYPE fScalar, const CComplexMatrix &
      realArray);
      friend CComplexMatrixT emp operator-(FTYPE fScalar, const CComplexMatrix &
      realArray);
      friend CComplexMatrixT emp operator*(FTYPE fScalar, const CComplexMatrix &
      realArray);
      friend CComplexMatrixT emp operator/(FTYPE fScalar, const CComplexMatrix &
      realArray);
      friend CComplexMatrixT emp operator+(compl ex<FTYPE> cfScalar, const
      CComplexMatrix & realArray);
      friend CComplexMatrixT emp operator-(complex<FTYPE> cfScalar, const
      CComplexMatrix & realArray);
      friend CComplexMatrixT emp operator*(compl ex<FTYPE> cfScalar, const
      CComplexMatrix & realArray);
      friend CComplexMatrixT emp operator/(complex<FTYPE> cfScalar, const
      CComplexMatrix & realArray);
      friend CComplexMatrixT emp & pow(CComplexMat rixTemp &mantissas,
      CComplexMatrix &exponents);

      // Stream I/O
      virtual void printOn(std::os tream& os) const;

      protected:
      complex<FTYPE> * m_cfArray;
      };

      #endif // !defined(__COMP LEXARRAY_H)


      Comment

      • Pavel Vozenilek

        #4
        Re: Mojo example class

        "Ryan Mitchley" <ryan@peralex.c om> wrote in message news:<3faa0b61$ 0$91668$a32e20b 9@news.nntpserv ers.com>...
        [color=blue]
        > I had a look through some of the code, and may have been looking in the
        > wrong places - but I didn't find too many examples of some of the techniques
        > outlined in Andrei's article. I saw several constructors taking mojo
        > temporary parameters, but that was about it. What about mojo::fnresult? Does
        > mojo::constant ever get used?
        >[/color]
        I don't know details - its the only place Mojo is used I know.

        You may also consider some exiting matrix library: daixtrose, ublas (I
        am not expert here and cannot compare).

        /Pavel

        Comment

        Working...