473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Mojo example class

Hi

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

I have written two classes, CComplexMatrix and CComplexMatrixTemp, 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


Jul 19 '05 #1
3 3926
"Ryan Mitchley" <rm******@removethis.worldonline.co.za> wrote
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"

Patch to STLport containers:

http://www.stlport.com/cgi-bin/forum...ForumID4&omm=0

/Pavel
Jul 19 '05 #2
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 CComplexMatrixTemp 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(__COMPLEXARRAY_H)
#define __COMPLEXARRAY_H

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

class CComplexMatrixTemp;
//class CComplexMatrix;

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

using namespace std;

#define COMPLEX_WIDTH 8
#define COMPLEX_PRECISION 4

class CComplexMatrix : public CMatrix
{
public:
static registerInFactory<CBase, CComplexMatrix> regClass;
// Data generation
CComplexMatrix & FillWithNoise(NOISE_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=(CComplexMatrixTemp another);
CComplexMatrix operator=(const CComplexMatrix &another);
// Element-wise array operators
CComplexMatrixTemp operator+(const CComplexMatrix &another);
CComplexMatrixTemp operator+(CComplexMatrixTemp another);
CComplexMatrixTemp operator-(const CComplexMatrix &another);
CComplexMatrixTemp operator-(CComplexMatrixTemp another);
CComplexMatrixTemp operator%(const CComplexMatrix &another);
CComplexMatrixTemp operator*(const CComplexMatrix &another);
CComplexMatrixTemp operator*(CComplexMatrixTemp x);
CComplexMatrixTemp operator%(CComplexMatrixTemp another);
CComplexMatrixTemp operator/(const CComplexMatrix &another);
CComplexMatrixTemp operator/(CComplexMatrixTemp another);
CComplexMatrix & operator+=(CComplexMatrixTemp another);
CComplexMatrix & operator+=(const CComplexMatrix &another);
CComplexMatrix & operator-=(CComplexMatrixTemp another);
CComplexMatrix & operator-=(const CComplexMatrix &another);
CComplexMatrix & operator%=(CComplexMatrixTemp another);
CComplexMatrix & operator%=(const CComplexMatrix &another);
CComplexMatrix & operator/=(CComplexMatrixTemp another);
CComplexMatrix & operator/=(const CComplexMatrix &another);
// Operators with scalar arguments
CComplexMatrixTemp operator+(const FTYPE fScalar);
CComplexMatrixTemp operator-(const FTYPE fScalar);
CComplexMatrixTemp operator*(const FTYPE fScalar);
CComplexMatrixTemp operator/(const FTYPE fScalar);
CComplexMatrixTemp operator+(const complex<FTYPE> fScalar);
CComplexMatrixTemp operator-(const complex<FTYPE> fScalar);
CComplexMatrixTemp operator*(const complex<FTYPE> fScalar);
CComplexMatrixTemp operator/(const complex<FTYPE> fScalar);
CComplexMatrix operator+=(const FTYPE fScalar);
CComplexMatrix operator-=(const FTYPE fScalar);
CComplexMatrix operator*=(const FTYPE fScalar);
CComplexMatrix operator/=(const FTYPE fScalar);
CComplexMatrixTemp 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()(const size_t nM, const size_t nN) const;
complex<FTYPE>& operator()(const size_t nM, const size_t nN);
CComplexMatrixTemp operator()(const size_t nM, const cColonType
ColonParam);
CComplexMatrixTemp operator()(const cColonType ColonParam, const size_t
nN);
CComplexMatrixTemp operator()(const size_t nM);

complex<FTYPE> Min();
complex<FTYPE> Min(size_t &nMinIndex);
complex<FTYPE> Max();
complex<FTYPE> Max(size_t &nMaxIndex);
CComplexMatrix & Swap(CComplexMatrix &another);
CComplexMatrix & ScalarMult(const 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 &ColumnVector);
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(ostream &os) const;
virtual void XML_WriteTail(ostream &os) const;
virtual int XML_ReadHead(list<string> &is, list<string>::const_iterator&
i);
virtual int XML_ReadTail(list<string> &is, list<string>::const_iterator&
i);
virtual void XML_SchemaWriteHead(ostream &out) const;
virtual void XML_SchemaWriteTail(ostream &out) const;
virtual string ArrayType() const { return "complex"; }

// friend functions
// friend CComplexMatrixTemp::CComplexMatrixTemp(const CComplexMatrix
&another);

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

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

// friend operators
friend CComplexMatrixTemp operator+(CComplexMatrixTemp lhs, const
CComplexMatrix & another);
friend CComplexMatrixTemp operator-(CComplexMatrixTemp lhs, const
CComplexMatrix & another);
friend CComplexMatrixTemp operator%(CComplexMatrixTemp lhs, const
CComplexMatrix & another);
friend CComplexMatrixTemp operator/(CComplexMatrixTemp lhs, const
CComplexMatrix & another);
friend CComplexMatrixTemp operator+(FTYPE fScalar, const CComplexMatrix &
realArray);
friend CComplexMatrixTemp operator-(FTYPE fScalar, const CComplexMatrix &
realArray);
friend CComplexMatrixTemp operator*(FTYPE fScalar, const CComplexMatrix &
realArray);
friend CComplexMatrixTemp operator/(FTYPE fScalar, const CComplexMatrix &
realArray);
friend CComplexMatrixTemp operator+(complex<FTYPE> cfScalar, const
CComplexMatrix & realArray);
friend CComplexMatrixTemp operator-(complex<FTYPE> cfScalar, const
CComplexMatrix & realArray);
friend CComplexMatrixTemp operator*(complex<FTYPE> cfScalar, const
CComplexMatrix & realArray);
friend CComplexMatrixTemp operator/(complex<FTYPE> cfScalar, const
CComplexMatrix & realArray);
friend CComplexMatrixTemp & pow(CComplexMatrixTemp &mantissas,
CComplexMatrix &exponents);

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

protected:
complex<FTYPE> * m_cfArray;
};

#endif // !defined(__COMPLEXARRAY_H)
Jul 19 '05 #3
"Ryan Mitchley" <ry**@peralex.com> wrote in message news:<3f***********************@news.nntpservers.c om>...
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?

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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

56
by: john bailo | last post by:
I just installed mono from ximian on my redHat 9 workstation and wrote a simple program from the interesting book ado.net in c# by Mahesh Chand. mono is fun ( is there ado for linux ? ...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
25
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
13
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My...
6
by: cj | last post by:
Lets just take this example I'm looking at now. I'm looking at the help screen titled .NET Framework Class Library FolderBrowserDialog Class . It gives an example at the bottom that begins with:...
26
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37:...
4
by: Kirit Sælensminde | last post by:
Thanks to a link I followed from here or clc++m (thanks to whoever posted, but I don't remember which post or even which thread it was now) I got to Andrei Alexandrescu's Doctor Dobbs article on...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.