行列とベクトル・クラスのインタフェースは次のようになります。
// matrix18.h // Matrixクラス Vectorクラス インターフェイス部 // 非メンバ関数と2つのクラスについてのfriend関数 // 添え字演算子をインライン化 #ifndef MATRIX18_H #define MATRIX18_H #include <iostream.h> #include <iomanip.h> #include <stdlib.h> #include <math.h> #include <assert.h> #define NEARLY_ZERO 1.E-16 // ゼロと見なせる #define ZERO_TOLERANCE 1.E-8 // 相対誤差 class Matrix; // 前方宣言 class Vector; // 前方宣言 // 非メンバ関数 const Vector operator+(const Vector &, const Vector &);//ベクトルの和 const Vector operator-(const Vector &, const Vector &);//ベクトルの差 const Vector operator*(double , const Vector &); //スカラー倍 const Vector operator*(const Vector &, double); //スカラー倍 const Vector operator/(const Vector &, double); // 実数で割り算 const Matrix operator+(const Matrix &, const Matrix &);// 行列の和 const Matrix operator-(const Matrix &, const Matrix &);// 行列の差 class Vector{ friend ostream &operator<<(ostream &, const Vector &); friend istream &operator>>(istream &, Vector &); friend double operator*(const Vector &, const Vector &);//内積 friend bool operator==(const Vector &, const Vector &);//等しいかを判定 friend bool operator!=(const Vector &, const Vector &);// 不等号を判定 friend const Vector operator*(const Matrix &, const Vector &); //行列*ベクトル friend const Vector operator*(const Vector &, const Matrix &); //ベクトル*行列 friend const Matrix operator*(const Matrix &, const Matrix &);// 行列の積 public: explicit Vector(int = 0); //デフォルトコンストラクタ Vector(const Vector &); //コピーコンストラクタ Vector(const double *, int); // 配列で初期化 ~Vector(); //デストラクタ void setSize(int);//ベクトルのサイズを設定する int getSize() const { return Dim;} //ベクトルのサイズを返す Vector &operator=(const Vector &); //ベクトルを代入する double &operator[](int); // 添え字演算子 const double &operator[](int) const;// 添え字演算子(constオブジェクト) const Vector operator-() const { return -1.0*(*this);} // (-1)ベクトル Vector &operator*=(double); Vector &operator/=(double); Vector &operator+=(const Vector&); Vector &operator-=(const Vector&); double norm() const; // ノルムを求める const Vector &Vector::normalize(); // ベクトルの規格化 private: double *ptr; //ベクトルの先頭要素へのポインタ int Dim; //ベクトルの次元 // ユーティリティ関数 void new_vector();// 領域を確保する void del_vector();// 領域を開放する }; class Matrix{ friend ostream &operator<<(ostream &, const Matrix &); friend istream &operator>>(istream &, Matrix &); friend const Matrix operator*(const Matrix &, const Matrix &);// 行列の積 friend const Vector operator*(const Matrix &, const Vector &); //行列*ベクトル friend const Vector operator*(const Vector &, const Matrix &); //ベクトル*行列 friend bool operator==(const Matrix &, const Matrix &);// 行列が等しいか判定 friend bool operator!=(const Matrix &, const Matrix &);// 等しくないか判定 public: explicit Matrix(int = 0, int = 0);// デフォルトコンストラクタ Matrix(const Matrix &);// コピーコンストラクタ ~Matrix(); // デストラクタ void setSize(int, int);// 行列のサイズを設定する int getRow() const { return Row;}// 行数を取得 int getCol() const { return Col;}// 列数を取得 Matrix &operator=(const Matrix &);// 行列を代入する Vector &operator[](int); // 添え字演算子 const Vector &operator[](int) const;// 添え字演算子(constオブジェクト) Matrix &operator+=(const Matrix &); Matrix &operator-=(const Matrix &); Matrix &operator*=(const Matrix &); private: Vector *ptr;// 行ベクトルへのポインタ int Row; // 行の数 int Col; // 列の数 // ユーティリティ関数 void new_matrix();// 行列の領域を確保する void del_matrix();// 領域を開放する }; //非const Vectorオブジェクトの添え字演算子 inline double &Vector::operator[](int index) { assert( index >= 0 && index < Dim ); return ptr[index]; } //const Vectorオブジェクトの添え字演算子 inline const double &Vector::operator[](int index) const { assert( index >= 0 && index < Dim ); return ptr[index]; } //非const Matrixオブジェクトの添え字演算子 inline Vector &Matrix::operator[](int index) { assert( index >= 0 && index < Row ); return ptr[index]; } //const Matrixオブジェクトの添え字演算子 inline const Vector &Matrix::operator[](int index) const { assert( index >= 0 && index < Row ); return ptr[index]; } #endif |
| 戻る |
Copyright(c) 1999 Yamada, K