Blitz++ 矩阵相乘(张量运算) 示例

简介:
//   整理 by RobinKin
None.gif
  //   Blitz++ 张量计算的示例  
ExpandedBlockStart.gif 
/* ****************************************************************************
InBlock.gif * matmult.cpp     Blitz++ tensor notation example
InBlock.gif *****************************************************************************
InBlock.gif * This example illustrates the tensor-like notation provided by Blitz++.
ExpandedBlockEnd.gif 
 */ 

None.gif 
None.gif#include 
  <   blitz   /   array.h   >  
None.gif#include 
  <   iostream   >  
None.gif 
None.gif 
using       namespace    blitz;
None.gif
None.gif
  int    main()
ExpandedBlockStart.gif
  {
InBlock.gif     //  Create two 4x4 arrays.  We want them to look like matrices, so
InBlock.gif    
 //  we'll make the valid index range 1..4 (rather than 0..3 which is
InBlock.gif    
 //  the default). 
InBlock.gif 

InBlock.gif    Range r( 1 , 4 );
InBlock.gif    Array < float , 2 >  A(r,r), B(r,r);
InBlock.gif
InBlock.gif     //  The first will be a Hilbert matrix:
InBlock.gif    
 // 
InBlock.gif    
 //  a   =   1
InBlock.gif    
 //   ij   -----
InBlock.gif    
 //        i+j-1
InBlock.gif    
 // 
InBlock.gif    
 //  Blitz++ provides a set of types { firstIndex, secondIndex, dot.gif }
InBlock.gif    
 //  which act as placeholders for indices.  These can be used directly
InBlock.gif    
 //  in expressions.  For example, we can fill out the A matrix like this: 
InBlock.gif 

InBlock.gif    firstIndex i;     //  Placeholder for the first index 
InBlock.gif 
    secondIndex j;    //  Placeholder for the second index 
InBlock.gif 

InBlock.gif    A  =   1.0   /  (i + j - 1 );
InBlock.gif
InBlock.gif    cout  <<   " A =  "   <<  A  <<  endl;
InBlock.gif
InBlock.gif     //  A = 4 x 4
InBlock.gif    
 //          1       0.5  0.333333      0.25
InBlock.gif    
 //        0.5  0.333333      0.25       0.2
InBlock.gif    
 //   0.333333      0.25       0.2  0.166667
InBlock.gif    
 //       0.25       0.2  0.166667  0.142857
InBlock.gif
InBlock.gif    
 //  Now the A matrix has each element equal to a_ij = 1/(i+j-1).
InBlock.gif
InBlock.gif    
 //  The matrix B will be the permutation matrix
InBlock.gif    
 // 
InBlock.gif    
 //  [ 0 0 0 1 ]
InBlock.gif    
 //  [ 0 0 1 0 ]
InBlock.gif    
 //  [ 0 1 0 0 ]
InBlock.gif    
 //  [ 1 0 0 0 ]
InBlock.gif    
 // 
InBlock.gif    
 //  Here are two ways of filling out B: 
InBlock.gif 

InBlock.gif    B  =  (i  ==  ( 5 - j));          //  Using an equation -- a bit cryptic 
InBlock.gif 

InBlock.gif    cout  <<   " B =  "   <<  B  <<  endl;
InBlock.gif
InBlock.gif     //  B = 4 x 4
InBlock.gif    
 //          0         0         0         1
InBlock.gif    
 //          0         0         1         0
InBlock.gif    
 //          0         1         0         0
InBlock.gif    
 //          1         0         0         0 
InBlock.gif 

InBlock.gif    B  =   0 ,  0 ,  0 ,  1 ,            //  Using an initializer list 
InBlock.gif 
         0 ,  0 ,  1 ,  0 ,           
InBlock.gif         0 ,  1 ,  0 ,  0 ,
InBlock.gif         1 ,  0 ,  0 ,  0 ;
InBlock.gif
InBlock.gif    cout  <<   " B =  "   <<  B  <<  endl;
InBlock.gif
InBlock.gif     //  Now some examples of tensor-like notation. 
InBlock.gif 

InBlock.gif    Array < float , 3 >  C(r,r,r);   //  A three-dimensional array: 1..4, 1..4, 1..4 
InBlock.gif 

InBlock.gif    thirdIndex k;              //  Placeholder for the third index
InBlock.gif
InBlock.gif    
 //  This expression will set
InBlock.gif    
 // 
InBlock.gif    
 //  c    = a   * b
InBlock.gif    
 //   ijk    ik    kj
InBlock.gif
InBlock.gif 
 //    C = A(i,k) * B(k,j); 
InBlock.gif 
   cout  <<   " C =  "   <<  C  <<  endl;
InBlock.gif
InBlock.gif
InBlock.gif     //  In real tensor notation, the repeated k index would imply a
InBlock.gif    
 //  contraction (or summation) along k.  In Blitz++, you must explicitly
InBlock.gif    
 //  indicate contractions using the sum(expr, index) function: 
InBlock.gif 

InBlock.gif    Array < float , 2 >  D(r,r);
InBlock.gif    D  =  sum(A(i,k)  *  B(k,j), k); // 指标收缩, 计算矩阵积
InBlock.gif
InBlock.gif    
 //  The above expression computes the matrix product of A and B. 
InBlock.gif 

InBlock.gif    cout  <<   " D =  "   <<  D  <<  endl;
InBlock.gif
InBlock.gif     //  D = 4 x 4
InBlock.gif    
 //       0.25  0.333333       0.5         1
InBlock.gif    
 //        0.2      0.25  0.333333       0.5
InBlock.gif    
 //   0.166667       0.2      0.25  0.333333
InBlock.gif    
 //   0.142857  0.166667       0.2      0.25
InBlock.gif
InBlock.gif    
 //  Indices like i,j,k can be used in any order in an expression.
InBlock.gif    
 //  For example, the following computes a kronecker product of A and B,
InBlock.gif    
 //  but permutes the indices along the way: 
InBlock.gif 

InBlock.gif    Array < float , 4 >  E(r,r,r,r);     //  A four-dimensional array 
InBlock.gif 
    fourthIndex l;                 //  Placeholder for the fourth index 
InBlock.gif 

InBlock.gif    E  =  A(l,j)  *  B(k,i); // 指标轮换
InBlock.gif
 // cout << "E = " << E << endl;
InBlock.gif
InBlock.gif
InBlock.gif    
 //  Now let's fill out a two-dimensional array with a radially symmetric
InBlock.gif    
 //  decaying sinusoid. 
InBlock.gif 

InBlock.gif     int  N  =   64 ;                    //  Size of array: N x N 
InBlock.gif 
    Array < float , 2 >  F(N,N);
InBlock.gif     float  midpoint  =  (N - 1 ) / 2 .;
InBlock.gif     int  cycles  =   3 ;
InBlock.gif     float  omega  =   2.0   *  M_PI  *  cycles  /   double (N);
InBlock.gif     float  tau  =   -   10.0   /  N;
InBlock.gif
InBlock.gif    F  =  cos(omega  *  sqrt(pow2(i - midpoint)  +  pow2(j - midpoint)))
InBlock.gif         *  exp(tau  *  sqrt(pow2(i - midpoint)  +  pow2(j - midpoint)));
InBlock.gif
InBlock.gif // cout << "F = " << F << endl; 
InBlock.gif 

InBlock.gif     return   0 ;
ExpandedBlockEnd.gif

None.gif 
//   输出  
None.gif 
  =       4    x    4   [            1             0.5        0.333333            0.25               0.5        0.333333            0.25             0.2          0.333333            0.25             0.2        0.166667              0.25             0.2      0.166667        0.142857    ]
None.gif
None.gif
  =       4    x    4  
None.gif[         
  0               0               0               1    
None.gif          
  0               0               1               0    
None.gif          
  0               1               0               0    
None.gif          
  1               0               0               0    ]
None.gif
None.gif
None.gif
  =       4    x    4  
None.gif[         
  0               0               0               1    
None.gif          
  0               0               1               0    
None.gif          
  0               1               0               0    
None.gif          
  1               0               0               0    ]
None.gif
None.gif
None.gif
  =       4    x    4  
None.gif[      
  0.25        0.333333             0.5               1    
None.gif        
  0.2            0.25        0.333333             0.5    
None.gif   
  0.166667             0.2            0.25        0.333333    
None.gif   
  0.142857        0.166667             0.2            0.25    ]
目录
相关文章
|
4月前
|
计算机视觉 Python
OpenCV中图像的掩模、加法运算讲解与实战(附Python源码)
OpenCV中图像的掩模、加法运算讲解与实战(附Python源码)
110 0
|
4月前
|
机器学习/深度学习 PyTorch 算法框架/工具
PyTorch基础之张量模块数据类型、基本操作、与Numpy数组的操作详解(附源码 简单全面)
PyTorch基础之张量模块数据类型、基本操作、与Numpy数组的操作详解(附源码 简单全面)
36 0
|
1月前
|
机器学习/深度学习 图形学 Python
Python矩阵加法
Python矩阵加法
|
2月前
|
Python
在Python中计算一组数值数据的中位数
在Python中计算一组数值数据的中位数
39 2
|
3月前
|
存储 大数据 索引
【Python】NumPy数组和矢量计算
【1月更文挑战第26天】【Python】NumPy数组和矢量计算
|
4月前
|
IDE Serverless 开发工具
Python 教程之 Numpy(9)—— 二元运算
Python 教程之 Numpy(9)—— 二元运算
30 0
|
4月前
|
TensorFlow 算法框架/工具 Python
把python函数转化为 tensorflow 函数 加速运算
把python函数转化为 tensorflow 函数 加速运算
22 1
|
7月前
|
机器学习/深度学习 存储 并行计算
Python的张量运算
Python的张量运算
44 0
|
7月前
|
安全 编译器 C++
[Eigen中文文档] 矩阵与向量运算
本文章旨在提供有关如何使用 Eigen 在矩阵、向量和标量之间执行算术操作的概述和一些详细信息。
193 0
|
Python
Python经典编程习题100例:第44例:两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵
Python经典编程习题100例:第44例:两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵
239 0

热门文章

最新文章