473,779 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Jul 19 '05 #1
3 3953
"Ryan Mitchley" <rm******@remov ethis.worldonli ne.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 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)
Jul 19 '05 #3
"Ryan Mitchley" <ry**@peralex.c om> wrote in message news:<3f******* *************** *@news.nntpserv ers.com>...
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
3605
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 ? why/why not? )
1
3975
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 size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
25
7754
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 minutes with Python 3 times a week since, have 14 years of computing experience, 8 years in mathematical computing and 4 years in unix admin and perl, i have quickly found the official doc: http://python.org/doc/2.4.1/lib/module-gzip.html
13
2105
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 example is a program called HijackThis. I'm sure many are familiar that it is a spyware removal tool. The program looks at over 20 places on Windows computers to see what is starting (with options to remove the offending software). The program can...
6
2327
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: ' The following example displays an application that provides the ability to ' open rich text files (rtf) into the RichTextBox. The example demonstrates ' using the FolderBrowserDialog to set the default directory for opening files. ' The...
26
2891
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: error: using obsolete binding at 'j'
4
1616
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 Mojo. Unfortunately the download link is broken. Is Mojo still available anywhere? I couldn't find any references to the same thing in either Boost or Loki. Has it gone thorugh a namechange or some other change or is it now in another library?
7
3091
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 item 7 for why this is virtual ...
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.