行列とベクトル・クラスのインタフェースは次のようになります。
// matrix17.h // Matrixクラス Vectorクラス インターフェイス部 // 非メンバ関数と2つのクラスについてのfriend関数 #ifndef MATRIX17_H #define MATRIX17_H #include <iostream.h> #include <iomanip.h> #include <stdlib.h> #include <math.h> #include <assert.h> #define NEARLY_ZERO 1.E-16 // ゼロと見なせる 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 &);// 不等号を判定 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();// 領域を開放する }; #endif |
| 戻る |
Copyright(c) 1999 Yamada, K