📄 reduce.cpp
字号:
/*****************************************************************************
* reduce.cpp Blitz++ array reduction example
*
* $Id: reduce.cpp,v 1.2 1997/07/16 19:38:23 tveldhui Exp $
*
* $Log: reduce.cpp,v $
* Revision 1.2 1997/07/16 19:38:23 tveldhui
* Update: Alpha release 0.2 (Arrays)
*
* Revision 1.1 1997/07/12 15:06:27 tveldhui
* Initial revision
*
*
*****************************************************************************
* This example illustrates the array reduction functions provided by
* Blitz++. These functions reduce an N dimensional array (or array
* expression) to an N-1 dimensional array expression by summing, taking
* the mean, etc. These array reductions are currently provided: sum,
* mean, min, max, minIndex, maxIndex, product, count, any and all.
*/
#include <blitz/array.h>
#ifdef BZ_NAMESPACES
using namespace blitz;
#endif
int main()
{
Array<int, 2> A(4,4);
A = 3, 1, 2, 4,
8, -1, -5, 3,
0, 9, -1, 4,
1, 3, 1, 2;
cout << "A = " << A << endl;
/*
* Reduce the array A to a one-dimensional array, by summing/taking
* the mean/etc. of each row.
*/
Array<int, 1> z(4);
Array<float, 1> z2(4);
secondIndex j;
z = sum(A, j);
cout << "sum(A,j) = " << endl << z << endl;
z2 = mean(A, j);
cout << "mean(A,j) = " << endl << z2 << endl;
z = min(A, j);
cout << "min(A,j) = " << endl << z << endl;
z = minIndex(A, j);
cout << "minIndex(A, j) = " << endl << z << endl;
z = max(A, j);
cout << "max(A, j) = " << endl << z << endl;
z = maxIndex(A, j);
cout << "maxIndex(A, j) = " << endl << z << endl;
z = first((A < 0), j);
cout << "first((A < 0), j) = " << endl << z << endl;
z = product(A, j);
cout << "product(A, j) = " << endl << z << endl;
z = count( (A > 0), j);
cout << "count((A > 0), j) = " << endl << z << endl;
z = any((abs(A) > 4), j);
cout << "any((abs(A) > 4), j) = " << endl << z << endl;
z = all(A > 0, j);
cout << "all(A > 0, j) = " << endl << z << endl;
return 0;
}
/*
* Output
*/
#if 0
A = 4 x 4
3 1 2 4
8 -1 -5 3
0 9 -1 4
1 3 1 2
sum(A,j) =
[ 10 5 12 7 ]
mean(A,j) =
[ 2.5 1.25 3 1.75 ]
min(A,j) =
[ 1 -5 -1 1 ]
minIndex(A, j) =
[ 1 2 2 0 ]
max(A, j) =
[ 4 8 9 3 ]
maxIndex(A, j) =
[ 3 0 1 1 ]
product(A, j) =
[ 24 120 0 6 ]
count((A > 0), j) =
[ 4 2 2 4 ]
any((abs(A) > 4), j) =
[ 0 1 1 0 ]
all(A > 0, j) =
[ 1 0 0 1 ]
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -