/***************************************************************************** * * where.cpp Blitz++ Vector example, illustrating where(X,Y,Z) * expressions. * * $Id: where.cpp,v 1.1.1.1 2005/06/15 14:31:20 tveldhui Exp $ * * $Log: where.cpp,v $ * Revision 1.1.1.1 2005/06/15 14:31:20 tveldhui * * */ #include using namespace blitz; int main() { Vector x = Range(-3,+3); // [ -3 -2 -1 0 1 2 3 ] // 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 // returned. Vector y = where(abs(x) > 2, x+10, x-10); // The above statement is transformed into something resembling: // // for (unsigned i=0; i < 7; ++i) // y[i] = (abs(x[i]) > 2) ? (x[i]+10) : (x[i]-10); // // The first expression (abs(x) > 2) can involve the usual // comparison and logical operators: < > <= >= == != && || cout << x << endl << y << endl; return 0; } Output: [ -3 -2 -1 0 1 2 3 ] [ 7 -12 -11 -10 -9 -8 13 ]