// FILE ex_vector2.cc #include #include #include #include #include using namespace std; class Vector { private: valarray vec_; public: Vector (int size) : vec_(size) { } // assignment Vector& operator= (const Vector& v) { if (vec_.size () != v.vec_.size ()) { vec_.resize (v.vec_.size ()); } vec_ = v.vec_; } // set all elements to value Vector& operator= (double value) { vec_ = value; } // get size int size () const { return (int)vec_.size (); } // resize and initialize with 0 void resize (int newsize) { vec_.resize ((size_t)newsize); } // element access double operator[] (int i) const { assert (0<=i && i 0) os << vec_[0]; for (size_t i=1; i 0 && v.size() == w.size()); double sum = 0; for (int i=0; i x = " << x << endl; cout << "||x|| = " << x.l2norm () << endl; cout << "(x, y) = " << inner_product (x, y) << endl; for (int i=0; i<5; ++i) { x[i] = i; } cout << "x = " << x << endl; cout << "writing x and y to file 'data.gpd'..." << endl; // file output ofstream ofs; ofs.open("data.gpd"); ofs << "#x y" << endl; for (int i=0; i<5; ++i) { ofs << x[i] << " " << y[i] << endl; } ofs.close (); cout << "done" << endl; cout << "in Linux, these data can be visualized using gnuplot" << endl; cout << "plot 'data.gpd' using 1:2 w lp" << endl; }