//: C07:Valarray1.cpp {-bor} // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Illustrates basic valarray functionality. #include "PrintValarray.h" using namespace std; double f(double x) { return 2.0 * x - 1.0; } int main() { double n[] = { 1.0, 2.0, 3.0, 4.0 }; valarray v(n, sizeof n / sizeof n[0]); print("v", v); valarray sh(v.shift(1)); print("shift 1", sh); valarray acc(v + sh); print("sum", acc); valarray trig(sin(v) + cos(acc)); print("trig", trig); valarray p(pow(v, 3.0)); print("3rd power", p); valarray app(v.apply(f)); print("f(v)", app); valarray eq(v == app); print("v == app?", eq); double x = v.min(); double y = v.max(); double z = v.sum(); cout << "x = " << x << ", y = " << y << ", z = " << z << endl; } ///:~