行列式

行列式 (determinant) の計算は,例えば多次元のガウス関数を扱うときなどに必要だ.
行列 (Matrix/ComplexMatrix) A に対して A.determinant().value() で行列式が計算できる.行列式計算中に発生したエラーの判定などは,逆行列の場合と同じようにして判定できる:

  Matrix A(4,4);
  stringstream ss (
    "  1.0  2.0  3.0  4.0"
    "  2.0 -1.0  1.0  0.0"
    "  4.0  3.0  2.0  1.0"
    " -4.0 -1.0 -5.0  0.0"); ss >> A;
  int info;
  cout<<"A="<<endl<<A;
  // 行列式を得る(double) :
  cout<<"A.determinant().value()= "<< A.determinant().value()<<endl;
  // 行列式を計算し,エラー情報をinfoに格納 :
  cout<<"A.determinant(info).value()= "<< A.determinant(info).value()<<endl;
  cout<<"info= "<<info<<endl;
  cout<<"A.fill(0.0, 2,0, 2,3)="<<endl<< A.fill(0.0, 2,0, 2,3);
  // 行列式の計算に失敗する場合 :
  cout<<"A.determinant(info).value()= "<< A.determinant(info).value()<<endl;
  cout<<"info= "<<info<<endl;

結果:

  A=
    1 2 3 4
    2 -1 1 0
    4 3 2 1
    -4 -1 -5 0
  A.determinant().value()= 120
  A.determinant(info).value()= 120
    info= 0
  A.fill(0.0, 2,0, 2,3)=
    1 2 3 4
    2 -1 1 0
    0 0 0 0
    -4 -1 -5 0
  A.determinant(info).value()= 0
    info= -1


この行列式を返すメンバ関数の宣言は

  DET Matrix::determinant (octave_idx_type &info) const;

となっていて, DET クラス を返すことがわかる. DET クラスの主なメンバ関数には以下のものがある:

  Matrix A(4,4);
  stringstream ss (
    "  4.0  2.0  3.0  1.0"
    "  0.0 -1.0  1.0  2.0"
    "  1.0  3.0  2.0  4.0"
    "  0.0 -1.0 -5.0 -4.0"); ss >> A;
  int info;
  cout<<"A="<<endl<<A;
  // DET オブジェクトの生成
  DET det(A.determinant(info));
  // 行列式の値を得る(double) :
  cout<<"det.value()= "<< det.value()<<endl;
  // 係数を得る(double) :
  cout<<"det.coefficient()= "<< det.coefficient()<<endl;
  // 指数を得る(int) :
  cout<<"det.exponent()= "<< det.exponent()<<endl;

結果:

  A=
    4 2 3 1
    0 -1 1 2
    1 3 2 4
    0 -1 -5 -4
  det.value()= -120
  det.coefficient()= -1.2
  det.exponent()= 2