by Nikolai Shokhirev
| Scientific Programming |
Up: Scientific programming
Previous: Project environment
Next: C++ notes, tips
Why do we need home-made C++ libraries at all?
The language itself does not have a decent support for numerical objects (complex numbers, vectors, matrices, etc). The STL also does solve all problems. This situation is reflected in the variety coexisting libraries (see e.g. C++ links).
Option 1. Use STL complex:
#include <complex> using namespace std; typedef complex<double> Complex; Complex a, b, c;
Option 2. Use the structure and complex function libraries:
/* Option 2a C style */
struct complex {
double Re;
double Im;
};
typedef struct complex Complex;
Complex a, b, c;
or
/* Option 2b C++ style */
struct Complex {
double Re;
double Im;
};
Complex a, b, c;
This approach is implemented in the files below
| View source | Complex.h, Complex.cpp, MathUtils.h ,MathUtils.cpp, StringUtils.h, StringUtils.cpp, test.cpp |
| Download source | CppComplexStruct.zip ( includes make file for g++ ). |
To build the project, cd to unzipped directory and run mingw32-make:
C:\WINDOWS\system32>cd C:\MinGW\projects\CppComplexStruct C:\MinGW\projects\CppComplexStruct>mingw32-make g++ -c test.cpp g++ -c Complex.cpp g++ -c MathUtils.cpp g++ -c StringUtils.cpp g++ test.o Complex.o MathUtils.o StringUtils.o -o test C:\MinGW\projects\CppComplexStruct>
Then run test.
C:\MinGW\projects\CppComplexStruct>test c0 = c2 : true Cadd(c0, c1) : true Cadd(c0, -1.0) : true Cadd(1.0, c1) : true Cadd(Real(1),Img(2)) : true Csub(c0, c1) : true Csub(c0, 1.0) : true Csub(3.0, c1) : false c2 = conjug(c1) : true c0*conjug(c0)=mod2(c0) : true Cmul(c0, c1) : true Cmul(c0, 3.0) : true Cmul(3.0, c1) : true Cdiv(c0, c0) : true Cdiv(c1, c0) : true (3.0/c0)*c0 : true (c0/3.0)*3.0 : true Csqrt(c0) = 1.27202+i*0.786151 : true
Option 3. Take advantage of C++ operator overloading and extend the option 3b.
We are going to keep the object simple and will not implement unnecessary features:
++, -- ==, !=, >, <The first two features make no sense for the complex numbers. The third one formally violate one of the Object-oriented dogmas. The accessor idiom is intended to hide the details of private members and protect them (e.g. by range checking). In case of Re and Im there is no structure to hide and they accept any floating point values.
The implementation of the first two features fall into the category of "Speculative generality" and the third one into the "Cargo cult programming".
Variant 1. "Classical"
Implemented as member functions
Implemented as global friends:
| View source | Complex.h, Complex.cpp, MathUtils.h ,MathUtils.cpp, StringUtils.h, StringUtils.cpp, Tests.cpp, GenTests.h, GenTests.cpp |
| Download source | CppComplexVar1.zip ( includes make file for g++ ). |
Variant 2. Via compound assignment operators
In this approach the compound assignment operators
+=, -=, *=, /=
are implemented first and the rest are expressed in terms of the above operators (see [2-4]).
Implemented as member functions
Implemented as global functions:
| View source | Complex.h, Complex.cpp, MathUtils.h ,MathUtils.cpp, StringUtils.h, StringUtils.cpp, Tests.cpp, GenTests.h, GenTests.cpp |
| Download source | CppComplexVar2.zip ( includes make file for g++ ). |
in progress . . . for now visit my Programming Tools page.
Variant 1.
in progress . . .
Variant 2. Via compound assignment operators
in progress . . .
References
| Scientific Programming |
Up: Scientific programming
Previous: Project environment
Next: C++ notes, tips
©Nikolai V. Shokhirev, 2004-2008