📄 matrix.h
字号:
}
else {
cerr << "Error! It's impossible to add a column of different length." << endl;
}
}
template<class T>
void Matrix<T>::AddColCopy (T* V, int N)
{
if (this->GetLengthRows()==0 && this->GetLengthCols()==0) {
for (int i=0; i<N; i++) {
Vector<T>* V3 = new Vector<T>();
V3->Add(V[i]);
V3->SetStepSize(this->StepSize);
this->Values->Add(V3);
}
}
else if (this->GetLengthRows() == N) {
for (int i=0; i<N; i++) {
this->Values->Values[i]->Add(V[i]);
}
}
else {
cerr << "Error! It's impossible to add a column of different length." << endl;
}
}
template<class T>
void Matrix<T>::AddColCopyAt (Vector<T>* V, int Index)
{
if (this->GetLengthRows()==0 && this->GetLengthCols()==0 && Index==0) {
this->AddColCopy(V);
}
else if (this->GetLengthRows()==V->GetLength() && Index>=0 && Index<=this->GetLengthRows()) {
for (int i=0; i<V->GetLength(); i++) {
this->Values->Values[i]->AddAt(V->Values[i],Index);
}
}
else {
cerr << "Error! It's impossible to add a row of different length or in a bad index." << endl;
}
}
template<class T>
void Matrix<T>::AddColCopyAt (T* V, int N, int Index)
{
if (this->GetLengthRows()==0 && this->GetLengthCols()==0 && Index==0) {
this->AddCol(V,N);
}
else if (this->GetLengthRows()==N && Index>=0 && Index<=this->GetLengthRows()) {
for (int i=0; i<N; i++) {
this->Values->Values[i]->AddAt(V[i],Index);
}
}
else {
cerr << "Error! It's impossible to add a row of different length or in a bad index." << endl;
}
}
template<class T>
void Matrix<T>::RemoveRow (int Index)
{
if (Index>=0 && Index<this->GetLengthRows()) {
Vector<T>* V = this->Values->Values[Index];
this->Values->RemoveAt(Index);
delete V;
}
else {
cerr << "Error! It's impossible to remove an element from the matrix that doesn't exist." << endl;
}
}
template<class T>
void Matrix<T>::RemoveCol (int Index)
{
if (Index>=0 && Index<this->GetLengthCols()) {
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->RemoveAt(Index);
}
}
else {
cerr << "Error! It's impossible to remove an element from the matrix that doesn't exist." << endl;
}
}
template<class T>
Matrix<T>* Matrix<T>::ExtractRows (int FromRowIndex, int ToRowIndex)
{
if (FromRowIndex>=0 && ToRowIndex<=this->GetLengthRows()-1 && FromRowIndex<=ToRowIndex) {
Matrix<T>* M = new Matrix<T>();
for (int i=FromRowIndex; i<=ToRowIndex; i++)
M->AddRowRef(this->GetRowCopy(i));
return M;
}
else {
cerr << "Error! It's impossible to extract the rows: invalid indexes" << endl;
return new Matrix<T>();
}
}
template<class T>
Matrix<T>* Matrix<T>::ExtractCols (int FromColIndex, int ToColIndex)
{
if (FromColIndex>=0 && ToColIndex<=this->GetLengthCols()-1 && FromColIndex<=ToColIndex) {
Matrix<T>* M = new Matrix<T>();
for (int i=0; i<this->GetLengthRows(); i++)
M->AddRowRef(this->GetRowRef(i)->Extract(FromColIndex,ToColIndex));
return M;
}
else {
cerr << "Error! It's impossible to extract the columns: invalid indexes" << endl;
return new Matrix<T>();
}
}
// Pre-built Matrix
template<class T>
Matrix<double>* Matrix<T>::ZeroMatrix (int RowsNumber, int ColsNumber)
{
Matrix<double>* M = new Matrix<double>();
for (int i=0; i<RowsNumber; i++) {
Vector<double>* V = V->ZeroVector(ColsNumber);
M->AddRowRef(V);
}
return M;
}
template<class T>
Matrix<double>* Matrix<T>::RandMatrix (int RowsNumber, int ColsNumber)
{
Matrix<double>* M = new Matrix<double>();
for (int i=0; i<RowsNumber; i++) {
Vector<double>* V = V->RandVector(ColsNumber);
M->AddRowRef(V);
}
return M;
}
// Mathematical Operations
template<class T>
void Matrix<T>::SumScalar (T X)
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->SumScalar(X);
}
}
template<class T>
void Matrix<T>::ProductScalar (T X)
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->ProductScalar(X);
}
}
template<class T>
void Matrix<T>::DivideScalar (T X)
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->DivideScalar(X);
}
}
template<class T>
void Matrix<T>::PowScalar (T X)
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->PowScalar(X);
}
}
template<class T>
void Matrix<T>::SumMatrix (Matrix<T>* M)
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->SumVector(M->Values->Values[i]);
}
}
template<class T>
void Matrix<T>::SubtractMatrix (Matrix<T>* M)
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->SubtractVector(M->Values->Values[i]);
}
}
template<class T>
Vector<T>* Matrix<T>::ProductVector (Vector<T>* V)
{
if (this->GetLengthCols()==0 || this->GetLengthCols()==V->GetLength()) {
Vector<T>* V2 = new Vector<T>(this->GetLengthRows());
for (int i=0; i<this->GetLengthRows(); i++) {
V2->Add(V->ProductVectorScalar(this->Values->Values[i],V));
}
return V2;
}
else {
cerr << "Error! It's impossible to multiply a matrix and a vector with different length." << endl;
return new Vector<T>();
}
}
template<class T>
Vector<T>* Matrix<T>::ProductVector (Matrix* M, Vector<T>* V)
{
if (M->GetLengthCols()==0 || M->GetLengthCols()==V->GetLength()) {
Vector<T>* V2 = new Vector<T>(M->GetLengthRows());
for (int i=0; i<M->GetLengthRows(); i++) {
V2->Add(V->ProductVectorScalar(M->Values->Values[i],V));
}
return V2;
}
else {
cerr << "Error! It's impossible to multiply a matrix and a vector with different length." << endl;
return new Vector<T>();
}
}
template<class T>
Matrix<T>* Matrix<T>::ProductVectorVector (Vector<T>* V1, Vector<T>* V2)
{
if (V1->GetLength() == V2->GetLength()) {
Matrix<T>* M = new Matrix();
for (int i=0; i<V1->GetLength(); i++)
{
Vector<T>* V4 = V2->Clone();
V4->ProductScalar(V1->Values[i]);
M->AddRowRef(V4);
}
return M;
}
else {
cerr << "Error! It's impossible to multiply two vectors with different length." << endl;
return new Matrix<T>();
}
}
template<class T>
Matrix<T>* Matrix<T>::ProductMatrixMatrix (Matrix<T>* M1, Matrix<T>* M2)
{
if (M1->GetLengthCols()==M2->GetLengthCols()) {
Matrix<T>* M3 = new Matrix<T>();
for (int i=0; i<M1->GetLengthRows(); i++) {
Vector<T>* V = new Vector<T>(M2->GetLengthRows());
for (int j=0; j<M2->GetLengthRows(); j++) {
V->Add(V->ProductVectorScalar(M1->Values->Values[i],M2->Values->Values[i]));
}
M3->AddRowRef(V);
}
return M3;
}
else {
cerr << "Error! It's impossible to multiply two matrix with not compatiple size." << endl;
return new Matrix<T>();
}
}
// I/O Operations
template<class T>
Matrix<double>* Matrix<T>::Load(char* Filename)
{
// Open the file
ifstream File (Filename, ios::in);
if (!File) {
cerr << "Error. It's impossible to open the file." << endl;
return new Matrix<double>();
}
Matrix<double>* M = new Matrix<double>();
// Save the vector
try {
int RowsNumber, ColsNumber;
double Value;
File >> RowsNumber >> ColsNumber;
for (int i=0; i<RowsNumber; i++) {
Vector<double>* V = new Vector<double>(ColsNumber);
for (int j=0; j<ColsNumber; j++) {
File >> Value;
V->Add(Value);
}
M->AddRowRef(V);
}
}
catch (...) {
cerr << "Error. It's impossible to complete the save." << endl;
}
// Close the file
File.close();
return M;
}
template<class T>
void Matrix<T>::Save (char* Filename)
{
// Open the file
ofstream File (Filename, ios::out);
if (!File) {
cerr << "Error. It's impossible to create the file." << endl;
return;
}
File.precision(30);
// Save the matrix
try {
File << this->GetLengthRows() << " " << this->GetLengthCols() << endl;
for (int i=0; i<this->GetLengthRows(); i++) {
for (int j=0; j<this->GetLengthCols(); j++)
File << this->Values->Values[i]->Values[j] << " ";
File << endl;
}
}
catch (...) {
cerr << "Error. It's impossible to complete the save." << endl;
}
// Close the file
File.close();
}
template<class T>
void Matrix<T>::Print ()
{
for (int i=0; i<this->GetLengthRows(); i++) {
this->Values->Values[i]->Print();
}
}
template<class T>
void Matrix<T>::Print (char* MatrixName)
{
cout << MatrixName << endl;
this->Print();
}
// Operators Redefinition
template<class T>
Vector<T> Matrix<T>::operator [] (int Index)
{
return (*this->Values->Values[Index]);
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -