您的位置:首页 > 编程语言 > C语言/C++

C++ MTL 矩阵 行交换 示例 (整理 by RobinKin from DevonIT)

2005-02-25 12:36 337 查看
#include "mtl/matrix.h"
#include "mtl/mtl.h"
#include "mtl/utils.h"

/*
Swaps the first row with the row that has the largest element in
column 1. This is the kind of operation one would do inside a
Gaussian elimintation algorithm.

Sample Output

3x3
[
[1,1.5,4.5],
[3,2.5,9.5],
[2,3.5,5.5]
]
3x3
[
[3,2.5,9.5],
[1,1.5,4.5],
[2,3.5,5.5]
]

*/

using namespace mtl;
using namespace std;

int
main()
{
//begin
typedef matrix< double,
rectangle<>,
dense<external>,
column_major>::type Matrix;
const Matrix::size_type N = 3;
Matrix::size_type large;
double dA[] = { 1, 3, 2, 1.5, 2.5, 3.5, 4.5, 9.5, 5.5 };
Matrix A(dA, N, N);
//end
print_all_matrix(A);
//begin
// Find the largest element in the first column.
large = max_index(A[0]); //第一列中最大元素所在行号
//end
cout << "x" << endl;
//begin
// Swap the first row with the row containing the largest
// element in the first column.
swap( rows(A)[0] , rows(A)[large]);//交换第一行和目标行
//end
print_all_matrix(A);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: