MTL 带状矩阵乘法

//MTL 带状矩阵乘法

//整理 by RobinKin
//

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

/*

  Sample Output

  Array A in packed form
  [
  [1,1,1,],
  [2,2,2,],
  [3,3,3,],
  [4,4,],
  [5,],
  ]
  x
  [1,2,3,4,5,]
  y
  [1,1,1,1,1,]
  Ax + y
  [15,39,75,75,53,]

  */

using namespace mtl;

typedef matrix< double, banded<>, banded<>, row_major>::type Matrix;
typedef dense1D<double> Vector;

int
main()
{
  const int M = 5;
  const int N = 5;
 
  Matrix A(M, N, 0, 2);

  Vector x(N);
  Vector y(M);

  //           1  1  1              1        1
  //
  //              2  2  2           2        1
  //
  //       A =       3  3  3    x = 3    y = 1
  //
  //                    4  4        4        1
  //
  //                       5        5        1

  int row = 1;
//赋值
  Matrix::iterator ri = A.begin();
  while (ri != A.end()) {
    Matrix::Row::iterator i = (*ri).begin();
    while (i != (*ri).end()) {
      *i = row;
      ++i;
    }
    ++row;
    ++ri;
  }

  for (int i = 0; i < N; ++i)
    x[i] = i + 1;

  mtl::set_value(y, 1);


  std::cout << "Array A in packed form" << std::endl;
  print_row(A);

  std::cout << "x" << std::endl;
  print_vector(x);
  std::cout << "y" << std::endl;
  print_vector(y);

//y=2*x*A+3*y
  mult(A, scaled(x, 2), scaled(y, 3), y);

  std::cout << "Ax + y" << std::endl;
  print_vector(y);

  return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值