sparsemat_main.cpp

来自「斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》」· C++ 代码 · 共 73 行

CPP
73
字号
// ENERGY211/CME211//// main.cpp - Main application file that tests functions from// the SparseMatrix class//#include <iostream>#include <cmath>#include "sparsemat.h"using namespace std;int main() {		try	{		// This creates a 3x3 zero matrix		SparseMatrix A(3,3);			A(1,1) = 1.0;		cout << "(2,2) element:" << endl;		double x = A(2,2);		cout << "x = " << x << endl;		// By trying to access the (2,2) element,		// entries in the map are created even though		// the element is zero		cout << "A(2,2) = " << A(2,2) << endl;		// So this will report nnz = 2 even though		// it should be 1		cout << "nnz = " << A.nnz() << endl;		// This will correct the problem		A.Squeeze();		cout << "nnz = " << A.nnz() << endl;		cout << endl;		// Confirm that the matrix hasn't changed		cout << "A:" << endl;		cout << A;	}	catch(runtime_error e)	{		cout << e.what() << endl;	}	try	{		// Test basic matrix operations		SparseMatrix A;		A.Identity( 4 );		A(0,1) = 2.0;		A(1,2) = 3.0;		A(2,3) = 4.0;		cout << "A = \n";		cout << A;			SparseMatrix B = A * A;		cout << "B = \n";		cout << B;		SparseMatrix T = B.Transpose();		cout << "B^T = \n";		cout << T;		SparseMatrix C = B + T;		cout << "C = \n";		cout << C;	}	catch(runtime_error e)	{		cout << e.what() << endl;	}	return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?