Blitz ++ matrix multiplied (tensor operation) example

xiaoxiao2021-03-06  13

// Solving by Robinkin

// Blitz 张 量 量 计算 量

/ ************************************************** *****************************

* MATMULT.CPP Blitz Tensor Notation Example

*********************************************************** ***********************************

* This Example Illustrates The Tensor-Like Notation Provided by Blitz .

* /

#include

#include

Using Namespace Blitz;

int main ()

{

// Create Two 4x4 arrays. We want Them to look like Matrices, SO

// We'll Make The Valid Index Range 1..4 (Rather Than 0..3 Which IS

// the default).

RANGE R (1, 4);

Array a (r, r), b (r, r);

// The First Will Be a Hilbert Matrix:

//

// a = 1

// IJ ------

// i j-1

//

// BLITZ Provides a set of type {firstIndex, secondindex, ...}

// Which Act As PlaceHolders for Indices. There Can Be Used Directly

// in Expressions. For Example, We can Fill Out the a matrix like this:

First Index I; // PlaceHolder for the first index

SecondIndex J; // PlaceHolder for the second index

A = 1.0 / (i j-1);

COUT << "a =" << a << endl;

// a = 4 x 4

// 1 0.5 0.33333 0.25

// 0.5 0.333333 0.25 0.2

// 0.333333 0.25 0.2 0.166667

// 0.25 0.2 0.16667 0.142857

// NOW The a Matrix Has Each ELEMENT Equal to a_ij = 1 / (i J-1).

// The Matrix B Will Be The Permutation Matrix

//

// [0 0 0 1]

// [0 0 1 0]

// [0 1 0 0]

// [1 0 0 0]

//

// Here Are Two Ways of Filling Out B:

B = (i == (5-j)); // using an equation - a bit cryptic

COUT << "b =" << b << endl;

// b = 4 x 4 // 0 0 0 1

// 0 0 1 0

// 0 1 0 0

// 1 0 0 0

B = 0, 0, 0, 1, // using an inTILAL LIST

0, 0, 1, 0,

0, 1, 0, 0,

1, 0, 0, 0;

COUT << "b =" << b << endl;

// now Some Examples of Tensor-like Notation.

Array c (r, r, r); // a Three-Dimensional Array: 1..4, 1..4, 1..4

ThirdIndex K; // PlaceHolder for the third Index

// this Expression Will Set

//

// c = a * b

// IJK IK KJ

// c = a (i, k) * b (k, j);

COUT << "c =" << c << endl;

// in Real Tensor Notation, The Repeated K Index Would IMPLY A

// Contraction (or Summation) Along K. In Blitz , You Must Explicitly

// Indicate Contractions Using The Sum (Expr, Index) Function:

Array d (r, r);

D = SUM (A (i, k) * b (k, j), k); // indicator shrinks, calculation matrix

// The Above Expression Computes The Matrix Product of A and B.

Cout << "d =" << D << endl;

// d = 4 x 4

// 0.25 0.333333 0.5 1

// 0.2 0.25 0.333333 0.5

// 0.166667 0.2 0.25 0.33333

// 0.142857 0.166667 0.2 0.25

// Idices Like I, J, K CAN be used in any ORDER IN AN Expression.

// for Example, The Following Computes a Kronecker Product of A and B,

// But permutes the indeices along the Way:

Array e (r, r, r, r); // a four-dimensional array

FourchnDex L; // PlaceHolder for the Fourth INDEX

E = a (l, j) * b (k, i); // index rotation

// cout << "e =" << E << ENDL;

// Now Let's Fill Out A Two-Dimensional Array with a radially symmetric // decaying sinusoid.

INT n = 64; // size of arch: N x N

Array f (n, n);

FLOAT MIDPOINT = (N-1) / 2 .;

INT CYCLES = 3;

FLOAT OMEGA = 2.0 * m_pi * cycles / double (n);

Float tau = - 10.0 / n;

F = cos (Omega * SQRT (Pow2 (I-MIDPOINT) POW2 (J-Midpoint)))))

* EXP (TAU * SQRT (Pow2 (I-MIDPOINT)))))));

// cout << "f =" << f << endl;

Return 0;

}

// output

A = 4 x 4 [1 0.5 0.333333 0.25 0.2 0.2 0.333333 0.25 0.2 0.166667 0.25 0.2 0.16667 0.142857]

B = 4 x 4 [0 0 0 1 0 0 1 0 0 0 0 1 0 0]

B = 4 x 4 [0 0 0 1 0 0 1 0 0 0 0 1 0 0]

D = 4 x 4 [0.25 0.25 0.25 0.333333 0.5 0.166667 0.2 0.25 0.33333 0.142857 0.166667 0.2 0.25]

转载请注明原文地址:https://www.9cbs.com/read-48758.html

New Post(0)