Previous: Array multi, Up: Customized Arrays [Contents][Index]
You can use the Array
class with types you have created yourself, or
types from another library. If you want to do arithmetic on the array,
whatever operators you use on the arrays have to be defined on the
underlying type.
For example, here’s a simple class for doing fixed point computations in the interval [0,1]:
#include <blitz/array.h> #include <blitz/numinquire.h> // for huge() using namespace blitz; // A simple fixed point arithmetic class which represents a point // in the interval [0,1]. class FixedPoint { public: // The type to use for the mantissa typedef unsigned int T_mantissa; FixedPoint() { } FixedPoint(T_mantissa mantissa) { mantissa_ = mantissa; } FixedPoint(double value) { assert((value >= 0.0) && (value <= 1.0)); mantissa_ = static_cast<T_mantissa>(value * huge(T_mantissa())); } FixedPoint operator+(FixedPoint x) { return FixedPoint(mantissa_ + x.mantissa_); } double value() const { return mantissa_ / double(huge(T_mantissa())); } private: T_mantissa mantissa_; }; ostream& operator<<(ostream& os, const FixedPoint& a) { os << a.value(); return os; }
The function huge(T)
returns the largest representable value for type
T; in the example above, it’s equal to UINT_MAX
.
The FixedPoint
class declares three useful operations: conversion
from double
, addition, and outputing to an ostream
. We can
use all of these operations on an Array<FixedPoint>
object:
#include <fixed-point.h> // FixedPoint class int main() { // Create an array using the FixedPoint class: Array<FixedPoint, 2> A(4,4), B(4,4); A = 0.5, 0.3, 0.8, 0.2, 0.1, 0.3, 0.2, 0.9, 0.0, 1.0, 0.7, 0.4, 0.2, 0.3, 0.8, 0.4; B = A + 0.05; cout << "B = " << B << endl; return 0; }
Note that the array A
is initialized using a comma-delimited list of
double
; this makes use of the constructor FixedPoint(double)
.
The assignment B = A + 0.05
uses
FixedPoint::operator+(FixedPoint)
, with an implicit conversion from
double
to FixedPoint
. Formatting the array B
onto the
standard output stream is done using the output operator defined for
FixedPoint
.
Here’s the program output:
B = (0,3) x (0,3) [ 0.55 0.35 0.85 0.25 0.15 0.35 0.25 0.95 0.05 0.05 0.75 0.45 0.25 0.35 0.85 0.45 ]
Previous: Array multi, Up: Customized Arrays [Contents][Index]