MTL 矩阵划分示例

//整理 by RobinKin  

#include "mtl/matrix.h"
#include "mtl/linalg_vec.h"
#include "mtl/utils.h"
#include <list>

/*
  Sample output:

 Partitioning Vectors
 //向量划分
[0,1,2,3,4,5,6,7,8,9,]
split in half
[0,1,2,3,4,]
[5,6,7,8,9,]
split into thirds
[0,1,2,]
[3,4,5,6,]
[7,8,9,]
 
Partitioning Matrices
 //矩阵划分
4x4
[
[0,1,2,3],
[4,5,6,7],
[8,9,10,11],
[12,13,14,15]
]

划分成 2x2的 子矩阵(行2个 ,列两个)
split into four submatrices
Top mat: 2x2
2x2
[
[0,1],
[4,5]
]
2x2
[
[2,3],
[6,7]
]
 
2x2
[
[8,9],
[12,13]
]
2x2
[
[10,11],
[14,15]
]
 划分成 2x2的 子矩阵
split into six submatrics
Top mat: 3x2 (行3 ,列2)
1x2
[
[0,1]
]
1x2
[
[2,3]
]
 
2x2
[
[4,5],
[8,9]
]
2x2
[
[6,7],
[10,11]
]
 
1x2
[
[12,13]
]
1x2
[
[14,15]
]

 
  */

int
main()
{
  using namespace mtl;

  /* Partitioning Vectors */
  std::cout << "Partitioning Vectors" << std::endl << std::endl;
  const int N = 10;
  int dx[N];
  int i, j;

  for (i = 0; i < N; ++i)
    dx[i] = i;

  typedef external_vec<int> Vec;
  typedef dense1D<Vec::subrange_type> PVec;

  Vec x(dx, N);
  print_vector(x);

  std::cout << "split in half" << std::endl;
  PVec xp1(2); //分成两个
  subdivide(5, x, xp1);//每个子向量5个元素
  print_partitioned_vector(xp1);

  std::cout << "split into thirds" << std::endl;
  PVec xp2(3);//分3 份
  int splits[] = { 3,7  };
  partition(array_to_vec(splits), x, xp2);
  print_partitioned_vector(xp2);

  /* Partitioning Matrices */
  std::cout << std::endl;
  std::cout << "Partitioning Matrices" << std::endl << std::endl;

  typedef matrix<int, rectangle<>, dense<>, row_major>::type Matrix;

  typedef matrix<Matrix::submatrix_type,
                 rectangle<>, dense<>, row_major>::type PMatrix;

  const int M = 4;
  Matrix A(M, M);
  for (i = 0; i < M; ++i)
    for (j = 0; j < M; ++j)
      A(i,j) = i * M + j;

  print_all_matrix(A);

  std::cout << "split into four submatrices" << std::endl;
 
  PMatrix Ap1(2,2);//2x2个子矩阵
  subdivide(2, 2, A, Ap1);
  print_partitioned_matrix(Ap1);

  std::cout << "split into six submatrics" << std::endl;

 
  int row_splits[] = { 1, 3 };//按照1,3 划分行
  int col_splits[] = { 2 };
  PMatrix Ap2(3,2);//3x2的子矩阵
  partition(array_to_vec(row_splits),
     array_to_vec(col_splits),
     A, Ap2);
  print_partitioned_matrix(Ap2);

  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值