我有一个模板类,我想获取给定矩阵的行列式。该类也可以使用复杂的数字进行方法。我定义了复杂的类,并且其中没有错误。
这是我的课程。
{
template<class T = double>
class Matrix
{
private:
unsigned row;
unsigned column;
T **matrix;
template<class OUTP>
friend std::ostream& operator<<(std::ostream&, const Matrix<OUTP>&);
template<class INP>
friend std::istream& operator>>(std::istream&, Matrix<INP>&);
public:
Matrix(unsigned = 0, unsigned = 0);
~Matrix();
Matrix(const Matrix<T>&);
void setMatrixElment(unsigned, unsigned, T);
void delMatrix();
unsigned getRow()const { return row; }
unsigned getColumn()const { return column; }
T getElement(unsigned = 0, unsigned = 0);
Matrix<T>& operator=(const Matrix<T>&);
Matrix<T> operator+(const Matrix<T>&);
Matrix<T> operator-(const Matrix<T>&);
Matrix <T>inverz();
bool open1();
bool open2();
Matrix<T> operator*(const double&);
Matrix<T> operator*(const Matrix<T>&);
T determinant() const;
}
}
这是我的方法
{
template<class T>
T Matrix<T>::determinant() const
try {
if (column != row)
{
throw "not zero";
}
T d = 1;
for (unsigned k = 0; k < column; k++)
{
for (unsigned i = k + 1; i < column; i++)
{
T g = (matrix[i][k] / matrix[k][k]);
for (unsigned j = k + 1; j < column; j++)
{
matrix[i][j] = matrix[i][j] - g *
matrix[k][j];
}
}
d = d * matrix[k][k];
}
return d;
}
catch (...)
{
cerr << "the matrix is not a square matrix" << endl;
}
}
我希望有一个数字。调试器没有显示任何错误,但是在输出中我看到了(对于3 * 3矩阵)
{
Matrix<Complex> b1;
bool nyit1 = true;
Matrix<Complex> a1;
nyit1 = a1.open1();
if (!nyit1)
cout << "Hibas megnyitas." << endl;
b1 = a1;
b1 << cout // here shows me a 3x3 matrix
b1.determinant; // here is the using of the method
}
输出:
1+(2)*i 3+(4)*i 5+(6)*i
7+(8)*i 9+(1)*i 2+(3)*i
4+(5)*i 6+(7)*i 8+(9)*i
:18.6
14.8
30.2
21.6
10.8
9.4
17.6
13.8
1
2
9.25596e+61
9.25596e+61
-9.25596e+61
2.77679e+62
-6.85383e+124
3.42691e+124
}