where.cpp | where.cpp | |||
---|---|---|---|---|
/************************************************************************** *** | /************************************************************************** *** | |||
* where.cpp Blitz++ Vector<T> example, illustrating where(X,Y,Z) | * where.cpp Blitz++ 1D Array example, illustrating where(X,Y,Z) | |||
* expressions. | * expressions. | |||
************************************************************************** ***/ | ************************************************************************** ***/ | |||
#include <blitz/vector-et.h> | #include <blitz/array.h> | |||
#include <blitz/vecwhere.h> | ||||
BZ_USING_NAMESPACE(blitz) | BZ_USING_NAMESPACE(blitz) | |||
int main() | int main() | |||
{ | { | |||
Vector<int> x = Range(-3,+3); // [ -3 -2 -1 0 1 2 3 ] | // Vector<int> x = Range(-3,+3); // [ -3 -2 -1 0 1 2 3 ] | |||
Array<int,1> x(Range(-3,+3)); | ||||
x = tensor::i; | ||||
// The where(X,Y,Z) function is similar to the X ? Y : Z operator. | // The where(X,Y,Z) function is similar to the X ? Y : Z operator. | |||
// If X is logical true, then Y is returned; otherwise, Z is | // If X is logical true, then Y is returned; otherwise, Z is | |||
// returned. | // returned. | |||
Vector<int> y = where(abs(x) > 2, x+10, x-10); | // Vector<int> y = where(abs(x) > 2, x+10, x-10); | |||
Array<int,1> y(x.shape()); | ||||
y = where(abs(x) > 2, x+10, x-10); | ||||
// The above statement is transformed into something resembling: | // The above statement is transformed into something resembling: | |||
// | // | |||
// for (unsigned i=0; i < 7; ++i) | // for (unsigned i=0; i < 7; ++i) | |||
// y[i] = (abs(x[i]) > 2) ? (x[i]+10) : (x[i]-10); | // y[i] = (abs(x[i]) > 2) ? (x[i]+10) : (x[i]-10); | |||
// | // | |||
// The first expression (abs(x) > 2) can involve the usual | // The first expression (abs(x) > 2) can involve the usual | |||
// comparison and logical operators: < > <= >= == != && || | // comparison and logical operators: < > <= >= == != && || | |||
cout << x << endl | cout << x << endl | |||
End of changes. 4 change blocks. | ||||
5 lines changed or deleted | 8 lines changed or added | |||
This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/ |