{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "C++\n", "====\n", "\n", "A brief introduction to features of C++ that are not found in C, using C++11 features where possible. We will assume the following `#includes` in the code snippets. As usual, we will exclude classes and any discussion of object-oriented programming.\n", "\n", "```c++\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \"/usr/local/include/armadillo\"\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hello, world\n", "\n", "Note the use of the `iostream` library and the standard namepace qualification `std::cout`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```c++\n", "int main()\n", "{\n", " std::cout << \"Hello, world!\\n\";\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Hello, world!\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Namespaces\n", "\n", "Just like Python, C++ has namespaces that allow us to build large libraries without worrying about name collisions. In the `Hello world` program, we used the explicit name `std::cout` indicating that `cout` is a member of the standard workspace. We can also use the `using` keyword to import selected functions or classes from a namespace. \n", "\n", "```c++\n", "using std::cout;\n", "\n", "int main()\n", "{\n", " cout << \"Hello, world!\\n\";\n", "}\n", "```\n", "\n", "For small programs, we sometimes import the entire namespace for convenience, but this may cause namespace collisions in larger programs.\n", "\n", "```c++\n", "using namespace std;\n", "\n", "int main()\n", "{\n", " cout << \"Hello, world!\\n\";\n", "}\n", "```\n", "\n", "You can easily create your own namespace.\n", "\n", "```c++\n", "namespace sta_663 {\n", " const double pi=2.14159;\n", "\n", " void greet(string name) {\n", " cout << \"\\nTraditional first program\\n\";\n", " cout << \"Hello, \" << name << \"\\n\";\n", " }\n", "}\n", "\n", "int main() \n", "{\n", " cout << \"\\nUsing namespaces\\n\";\n", " string name = \"Tom\";\n", " cout << sta_663::pi << \"\\n\";\n", " sta_663::greet(name);\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Using namespaces\n", "2.14159\n", "\n", "Traditional first program\n", "Hello, Tom\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Looping\n", "\n", "Note the traditional for loop and the new range for loop. There is also a `while` loop (not shown).\n", "\n", "```c++\n", "// for loops\n", "int main() \n", "{\n", " int x[] = {1, 2, 3, 4, 5};\n", "\n", " cout << \"\\nTraditional for loop\\n\";\n", " for (int i=0; i < sizeof(x)/sizeof(x[0]); i++) {\n", " cout << i << endl;\n", " }\n", "\n", " cout << \"\\nRanged for loop\\n\\n\";\n", " for (auto &i : x) {\n", " cout << i << endl;\n", " }\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Traditional for loop\n", "0\n", "1\n", "2\n", "3\n", "4\n", "\n", "Ranged for loop\n", "\n", "1\n", "2\n", "3\n", "4\n", "5\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1**\n", "\n", "Use loop to generate the 12 by 12 times table. Compile and run. You don't have to worry much about formatting, but the output should have 12 rows with numbers separated by spaces." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions and Lambdas\n", "\n", "```c++\n", "// simple function\n", "int add0(int a, int b) {\n", " return a + b;\n", "}\n", "\n", "// simple function with reference variables\n", "void add1(int a, int b, int& c) {\n", " c = a + b;\n", "}\n", "\n", "// When to use values and when to use refernces in function arguments?\n", "\n", "// Use values when the variable is small (e.g. a double) \n", "double f1(double x) { return 2 * x; };\n", "\n", "// Use const references when the variable is large to avoid copying\n", "double f2(const vector& v) { \n", " double s = 0.0;\n", " for (auto x: v) {\n", " s += x;\n", " }\n", " return s;\n", "}\n", "\n", "// Use references when you want to change the variable within the function\n", "void f3(vector& v) {\n", " for (auto& x: v) {\n", " x++;\n", " }\n", "}\n", "\n", "// lambda function\n", "auto add2 = [] (int a, int b) { return a + b; };\n", "\n", "int main() {\n", "\n", " cout << \"\\nStandard function\\n\";\n", " int a = 3, b = 4;\n", " cout << add0(a, b) << endl;\n", "\n", " int c = 0;\n", " cout << \"\\nStandard with reference varaibles\\n\";\n", "\n", " add1(a, b, c);\n", " cout << c << endl;\n", "\n", " cout << \"\\nLambda function\\n\";\n", " cout << add2(a, b) << endl;\n", "\n", " auto add3 = [c] (int a, int b) { return c * add2(a, b); };\n", "\n", " c -= 5;\n", " cout << \"\\nLambda function with value capture\\n\";\n", " cout << add3(a, b) << endl;\n", "\n", " auto add4 = [&c] (int a, int b) { return c * add2(a, b); };\n", "\n", " cout << \"\\nLambda function with reference capture\\n\";\n", " cout << add4(a, b) << endl;\n", "\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Standard function\n", "7\n", "\n", "Standard with reference varaibles\n", "7\n", "\n", "Lambda function\n", "7\n", "\n", "Lambda function with value capture\n", "49\n", "\n", "Lambda function with reference capture\n", "14\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Templates\n", "\n", "```c++\n", "// templates\n", "template \n", "T add5(T a, T b) { return a + b; }\n", "\n", "int main() \n", "{\n", "\n", " cout << \"\\nTemplate function with ints\\n\";\n", " cout << add5(3, 4) << endl;\n", "\n", " cout << \"\\nTemplate function with doubles\\n\";\n", " cout << add5(3.14, 2.78) << endl;\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Template function with ints\n", "7\n", "\n", "Template function with doubles\n", "5.92\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Iterators\n", "\n", "```c++\n", "int main() \n", "{\n", " int x[] = {1, 2, 3, 4, 5};\n", "\n", " cout << \"\\nUsing iterators\\n\";\n", " for (auto it=begin(x); it != end(x); it++) {\n", " cout << *it << endl;\n", " }\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Using iterators\n", "1\n", "2\n", "3\n", "4\n", "5\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Containers\n", "\n", "```c++\n", "int main() \n", "{\n", " vector v = {1,2,3};\n", "\n", " cout << \"\\nUsing the vector container\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << endl;\n", " }\n", "\n", " v.push_back(4);\n", " v.push_back(5);\n", " cout << \"\\nGrowing the vector container\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << endl;\n", " }\n", "\n", " v.pop_back();\n", " cout << \"\\nShrinking the vector container\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << endl;\n", " }\n", "\n", " cout << \"\\nUsing the unordered_map container\\n\";\n", " unordered_map dict = { {\"ann\", 23}, {\"bob\", 32}, {\"charles\", 17}};\n", " dict[\"doug\"] = 30;\n", " for (auto it=begin(dict); it != end(dict); it++) {\n", " cout << it->first << \", \" << it->second << endl;\n", " }\n", "\n", " cout << dict[\"bob\"] << endl;\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Using the vector container\n", "1\n", "2\n", "3\n", "\n", "Growing the vector container\n", "1\n", "2\n", "3\n", "4\n", "5\n", "\n", "Shrinking the vector container\n", "1\n", "2\n", "3\n", "4\n", "\n", "Using the unordered_map container\n", "doug, 30\n", "charles, 17\n", "bob, 32\n", "ann, 23\n", "32\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2**\n", " \n", "Write a function that takes a vector of doubles returns the squared vector. Compile and run the function with the initial vector containing 1.0, 2.0, 3.0, 4.0, 5.0." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**EXercise 3**\n", "\n", "Convert the function from Exercise 2 so that it works for lists or vectors of ints, floats and doubles." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Algorithms\n", "\n", "```c++\n", "int main()\n", "{\n", "void show_algorithms() {\n", " vector v(10, 0);\n", "\n", " cout << \"\\nWorking with standard library algorithm\\n\";\n", " cout << \"\\nInitial state\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << \" \";\n", " }\n", " cout << endl;\n", "\n", " cout << \"\\nAfter iota\\n\";\n", " iota(begin(v), end(v), 5);\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << \" \";\n", " }\n", " cout << endl;\n", "\n", " cout << \"\\nSimple accumulate\\n\";\n", " int s = accumulate(begin(v), end(v), 0);\n", " cout << s << endl;\n", "\n", " cout << \"\\nAccumulate with custom sum of squares reduction\\n\";\n", " int t = accumulate(begin(v), end(v), 0, [] (int acc, int x) { return acc + x*x; });\n", " cout << t << endl;\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Working with standard library algorithm\n", "\n", "Initial state\n", "0 0 0 0 0 0 0 0 0 0 \n", "\n", "After iota\n", "5 6 7 8 9 10 11 12 13 14 \n", "\n", "Simple accumulate\n", "95\n", "\n", "Accumulate with custom sum of squares reduction\n", "985\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 4**\n", "\n", "Write a function to calculate the mean of a vector of numbers using `accumulate` from the `` library. Compile and test with some vectors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Function pointers\n", "\n", "```c++\n", "int main() \n", "{\n", " cout << \"\\nUsing generalized function pointers\\n\";\n", " using func = function;\n", "\n", " auto f1 = [](double x, double y) { return x + y; };\n", " auto f2 = [](double x, double y) { return x * y; };\n", " auto f3 = [](double x, double y) { return x + y*y; };\n", "\n", " double x = 3, y = 4;\n", "\n", " vector funcs = {f1, f2, f3,};\n", "\n", " for (auto& f : funcs) {\n", " cout << f(x, y) << \"\\n\";\n", " }\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Using generalized function pointers\n", "7\n", "12\n", "19\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 5**\n", "\n", "Implement Newton's method in 1D for root finding. Pass in the function and gradient as generalized function pointers. Test your function with some simple polynomial and starting points." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Random numbers\n", "\n", "C++ now comes with its own collection of random number generators and quite a broad selection of distributions. See [here](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3551.pdf) for a great explanation.\n", "\n", "```c++\n", "int main() \n", "{\n", " cout << \"\\nGenerating random numbers\\n\";\n", "\n", " // start random number engine wiht fixed seed\n", " default_random_engine re{12345};\n", "\n", " uniform_int_distribution uniform(1,6); // lower and upper bounds\n", " poisson_distribution poisson(30); // rate\n", " student_t_distribution t(10); // degrees of freedom\n", "\n", " auto runif = bind (uniform, re);\n", " auto rpois = bind(poisson, re);\n", " auto rt = bind(t, re);\n", "\n", " for (int i=0; i<10; i++) {\n", " cout << runif() << \", \" << rpois() << \", \" << rt() << \"\\n\";\n", "\n", " }\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Generating random numbers\n", "3, 30, 0.0796641\n", "5, 38, 0.163947\n", "3, 26, -0.570003\n", "6, 27, 0.872475\n", "6, 33, -0.260322\n", "2, 28, 0.798292\n", "1, 22, 0.00164128\n", "4, 29, 0.633913\n", "5, 41, 1.00468\n", "6, 31, -0.00420647\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 6**\n", "\n", "Generate 1000 random points from the exponential distribution and save as a comma-separated values (CSV) file. Open the file in Python and plot the distribution using `plt.hist`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Numeric library\n", "\n", "Armadillo is an accessible library for doing numeric operations, much like `numpy` in Python. Please see [official documentation](http://arma.sourceforge.net/docs.html) for details. It provides vectors, matrices, tensors, linear algebra, statistical functions and a limited set of convenient random number generators.\n", "\n", "```c++\n", "int main() \n", "{\n", " using namespace arma;\n", "\n", " vec u = linspace(0,1,5);\n", " vec v = ones(5);\n", " mat A = randu(4,5); // uniform random deviates\n", " mat B = randn(4,5); // normal random deviates\n", "\n", " cout << \"\\nVecotrs in Armadillo\\n\";\n", " cout << u << endl;\n", " cout << v << endl;\n", " cout << u.t() * v << endl;\n", "\n", " cout << \"\\nRandom matrices in Armadillo\\n\";\n", " cout << A << endl;\n", " cout << B << endl;\n", " cout << A * B.t() << endl;\n", " cout << A * v << endl;\n", "\n", " cout << \"\\nQR in Armadillo\\n\";\n", " mat Q, R;\n", " qr(Q, R, A.t() * A);\n", " cout << Q << endl;\n", " cout << R << endl;\n", "}\n", "```\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "Vecotrs in Armadillo\n", " 0\n", " 0.2500\n", " 0.5000\n", " 0.7500\n", " 1.0000\n", "\n", " 1.0000\n", " 1.0000\n", " 1.0000\n", " 1.0000\n", " 1.0000\n", "\n", " 2.5000\n", "\n", "\n", "Random matrices in Armadillo\n", " 7.8264e-06 5.3277e-01 6.7930e-01 8.3097e-01 6.7115e-01\n", " 1.3154e-01 2.1896e-01 9.3469e-01 3.4572e-02 7.6982e-03\n", " 7.5561e-01 4.7045e-02 3.8350e-01 5.3462e-02 3.8342e-01\n", " 4.5865e-01 6.7886e-01 5.1942e-01 5.2970e-01 6.6842e-02\n", "\n", " -0.7649 1.2041 -0.7020 1.1862 0.8284\n", " 1.7313 0.0937 1.6814 -0.8631 0.9426\n", " 0.1454 -0.6920 0.2742 0.6810 0.2091\n", " 0.7032 0.2610 0.1752 1.3165 -0.5897\n", "\n", " 1.7063 1.1075 0.5238 0.9563\n", " -0.4457 1.7973 0.1490 0.3544\n", " -0.4095 2.2726 0.2990 0.4551\n", " 0.7857 1.3368 0.1140 1.2487\n", "\n", " 2.7142\n", " 1.3275\n", " 1.6230\n", " 2.2535\n", "\n", "\n", "QR in Armadillo\n", " -0.6776 0.6377 0.3253 0.0944 -0.1395\n", " -0.3188 -0.4409 0.3521 0.4177 0.6368\n", " -0.5524 -0.2366 -0.7774 0.1504 -0.1092\n", " -0.2443 -0.5816 0.4071 -0.1709 -0.6380\n", " -0.2727 -0.0688 -0.0099 -0.8745 0.3950\n", "\n", " -1.1785e+00 -1.3394e+00 -2.1015e+00 -1.3527e+00 -1.0229e+00\n", " 0e+00 -8.3421e-01 -9.7607e-01 -9.9515e-01 -5.3245e-01\n", " 0e+00 0e+00 -4.6336e-01 7.6793e-02 -4.0376e-03\n", " 0e+00 0e+00 0e+00 -2.0273e-01 -3.2745e-01\n", " 0e+00 0e+00 0e+00 0e+00 1.1102e-16\n", "\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 7**\n", "\n", "Use the armadillo library to\n", "\n", "- Generate 10 x-coordinates linearly spaced between 10 and 15\n", "- Generate 10 random y-values as $y = 3x^2 - 7x + 2 + \\epsilon$ where $\\epsilon \\sim N(0,1)$\n", "- Find the length of $x$ and $y$ and the Euclidean distance between $x$ and $y$\n", "- Find the correlation between $x$ and $y$\n", "- Solve the linear system to find a quadratic fit for this data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Collected source code" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting main.cpp\n" ] } ], "source": [ "%%file main.cpp\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "using namespace std;\n", "\n", "/* Topics\n", " *\n", " * - for loop\n", " *\n", " *\n", " * - functions\n", " * - lambdas\n", " * - templates\n", " *\n", " * - iterators\n", " * - containers\n", " * - algorithms\n", " * - Armadillo\n", "*/\n", "\n", "// hello world\n", "void show_hello() {\n", " cout << \"Hello, world!\\n\";\n", "}\n", "\n", "namespace sta_663 {\n", " const double pi=2.14159;\n", "\n", " void greet(string name) {\n", " cout << \"\\nTraditional first program\\n\";\n", " cout << \"Hello, \" << name << \"\\n\";\n", " }\n", "}\n", "\n", "void show_namespace() {\n", " cout << \"\\nUsing namespaces\\n\";\n", " string name = \"Tom\";\n", " cout << sta_663::pi << \"\\n\";\n", " sta_663::greet(name);\n", "}\n", "\n", "\n", "// for loops\n", "void show_for() {\n", " int x[] = {1, 2, 3, 4, 5};\n", "\n", " cout << \"\\nTraditional for loop\\n\";\n", " for (int i=0; i < sizeof(x)/sizeof(x[0]); i++) {\n", " cout << i << endl;\n", " }\n", "\n", " cout << \"\\nRanged for loop\\n\\n\";\n", " for (auto &i : x) {\n", " cout << i << endl;\n", " }\n", "}\n", "\n", "// simple funciton\n", "int add0(int a, int b) {\n", " return a + b;\n", "}\n", "\n", "// simple function with reference variables\n", "void add1(int a, int b, int& c) {\n", " c = a + b;\n", "}\n", "\n", "// lambda function\n", "auto add2 = [] (int a, int b) { return a + b; };\n", "\n", "void show_func() {\n", "\n", " cout << \"\\nStandard function\\n\";\n", " int a = 3, b = 4;\n", " cout << add0(a, b) << endl;\n", "\n", " int c = 0;\n", " cout << \"\\nStandard with reference varaibles\\n\";\n", "\n", " add1(a, b, c);\n", " cout << c << endl;\n", "\n", " cout << \"\\nLambda function\\n\";\n", " cout << add2(a, b) << endl;\n", "\n", " auto add3 = [c] (int a, int b) { return c * add2(a, b); };\n", "\n", " c -= 5;\n", " cout << \"\\nLambda function with value capture\\n\";\n", " cout << add3(a, b) << endl;\n", "\n", " auto add4 = [&c] (int a, int b) { return c * add2(a, b); };\n", "\n", " cout << \"\\nLambda function with reference capture\\n\";\n", " cout << add4(a, b) << endl;\n", "\n", "}\n", "\n", "// templates\n", "template \n", "T add5(T a, T b) { return a + b; }\n", "\n", "void show_template() {\n", "\n", " cout << \"\\nTemplate function with ints\\n\";\n", " cout << add5(3, 4) << endl;\n", "\n", " cout << \"\\nTemplate function with doubles\\n\";\n", " cout << add5(3.14, 2.78) << endl;\n", "}\n", "\n", "void show_iterators() {\n", " int x[] = {1, 2, 3, 4, 5};\n", "\n", " cout << \"\\nUsing iterators\\n\";\n", " for (auto it=begin(x); it != end(x); it++) {\n", " cout << *it << endl;\n", " }\n", "}\n", "\n", "void show_containers() {\n", " vector v = {1,2,3};\n", "\n", " cout << \"\\nUsing the vector container\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << endl;\n", " }\n", "\n", " v.push_back(4);\n", " v.push_back(5);\n", " cout << \"\\nGrowing the vector container\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << endl;\n", " }\n", "\n", " v.pop_back();\n", " cout << \"\\nShrinking the vector container\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << endl;\n", " }\n", "\n", " cout << \"\\nUsing the unordered_map container\\n\";\n", " unordered_map dict = { {\"ann\", 23}, {\"bob\", 32}, {\"charles\", 17}};\n", " dict[\"doug\"] = 30;\n", " for (auto it=begin(dict); it != end(dict); it++) {\n", " cout << it->first << \", \" << it->second << endl;\n", " }\n", "\n", " cout << dict[\"bob\"] << endl;\n", "}\n", "\n", "void show_algorithms() {\n", " vector v(10, 0);\n", "\n", " cout << \"\\nWorking with standard library algorithm\\n\";\n", " cout << \"\\nInitial state\\n\";\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << \" \";\n", " }\n", " cout << endl;\n", "\n", " cout << \"\\nAfter iota\\n\";\n", " iota(begin(v), end(v), 5);\n", " for (auto it=begin(v); it != end(v); it++) {\n", " cout << *it << \" \";\n", " }\n", " cout << endl;\n", "\n", " cout << \"\\nSimple accumulate\\n\";\n", " int s = accumulate(begin(v), end(v), 0);\n", " cout << s << endl;\n", "\n", " cout << \"\\nAccumulate with custom sum of squares reduction\\n\";\n", " int t = accumulate(begin(v), end(v), 0, [] (int acc, int x) { return acc + x*x; });\n", " cout << t << endl;\n", "}\n", "\n", "void show_functional() {\n", "\n", " cout << \"\\nUsing generalized function pointers\\n\";\n", " using func = function;\n", "\n", " auto f1 = [](double x, double y) { return x + y; };\n", " auto f2 = [](double x, double y) { return x * y; };\n", " auto f3 = [](double x, double y) { return x + y*y; };\n", "\n", " double x = 3, y = 4;\n", "\n", " vector funcs = {f1, f2, f3,};\n", "\n", " for (auto& f : funcs) {\n", " cout << f(x, y) << \"\\n\";\n", " }\n", "\n", "}\n", "\n", "void show_random() {\n", " cout << \"\\nGenerating random numbers\\n\";\n", "\n", " // start random number engine wiht fixed seed\n", " default_random_engine re{12345};\n", "\n", " uniform_int_distribution uniform(1,6); // lower and upper bounds\n", " poisson_distribution poisson(30); // rate\n", " student_t_distribution t(10); // degrees of freedom\n", "\n", " auto runif = bind (uniform, re);\n", " auto rpois = bind(poisson, re);\n", " auto rt = bind(t, re);\n", "\n", " for (int i=0; i<10; i++) {\n", " cout << runif() << \", \" << rpois() << \", \" << rt() << \"\\n\";\n", " }\n", "}\n", "\n", "void show_amrmadillo() {\n", " using namespace arma;\n", "\n", " vec u = linspace(0,1,5);\n", " vec v = ones(5);\n", " mat A = randu(4,5);\n", " mat B = randn(4,5);\n", "\n", " cout << \"\\nVecotrs in Armadillo\\n\";\n", " cout << u << endl;\n", " cout << v << endl;\n", " cout << u.t() * v << endl;\n", "\n", " cout << \"\\nRandom matrices in Armadillo\\n\";\n", " cout << A << endl;\n", " cout << B << endl;\n", " cout << A * B.t() << endl;\n", " cout << A * v << endl;\n", "\n", " cout << \"\\nQR in Armadillo\\n\";\n", " mat Q, R;\n", " qr(Q, R, A.t() * A);\n", " cout << Q << endl;\n", " cout << R << endl;\n", "}\n", "\n", "int main() {\n", " show_hello();\n", " show_namespace();\n", " show_for();\n", " show_func();\n", " show_template();\n", " show_iterators();\n", " show_containers();\n", " show_algorithms();\n", " show_functional();\n", " show_random();\n", " show_amrmadillo();\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compilation\n", "----" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -O3 -std=c++11 -larmadillo main.cpp -o main" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execution\n", "----" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n", "\n", "Using namespaces\n", "2.14159\n", "\n", "Traditional first program\n", "Hello, Tom\n", "\n", "Traditional for loop\n", "0\n", "1\n", "2\n", "3\n", "4\n", "\n", "Ranged for loop\n", "\n", "1\n", "2\n", "3\n", "4\n", "5\n", "\n", "Standard function\n", "7\n", "\n", "Standard with reference varaibles\n", "7\n", "\n", "Lambda function\n", "7\n", "\n", "Lambda function with value capture\n", "49\n", "\n", "Lambda function with reference capture\n", "14\n", "\n", "Template function with ints\n", "7\n", "\n", "Template function with doubles\n", "5.92\n", "\n", "Using iterators\n", "1\n", "2\n", "3\n", "4\n", "5\n", "\n", "Using the vector container\n", "1\n", "2\n", "3\n", "\n", "Growing the vector container\n", "1\n", "2\n", "3\n", "4\n", "5\n", "\n", "Shrinking the vector container\n", "1\n", "2\n", "3\n", "4\n", "\n", "Using the unordered_map container\n", "doug, 30\n", "charles, 17\n", "bob, 32\n", "ann, 23\n", "32\n", "\n", "Working with standard library algorithm\n", "\n", "Initial state\n", "0 0 0 0 0 0 0 0 0 0 \n", "\n", "After iota\n", "5 6 7 8 9 10 11 12 13 14 \n", "\n", "Simple accumulate\n", "95\n", "\n", "Accumulate with custom sum of squares reduction\n", "985\n", "\n", "Using generalized function pointers\n", "7\n", "12\n", "19\n", "\n", "Generating random numbers\n", "3, 30, 0.0796641\n", "5, 38, 0.163947\n", "3, 26, -0.570003\n", "6, 27, 0.872475\n", "6, 33, -0.260322\n", "2, 28, 0.798292\n", "1, 22, 0.00164128\n", "4, 29, 0.633913\n", "5, 41, 1.00468\n", "6, 31, -0.00420647\n", "\n", "Vecotrs in Armadillo\n", " 0\n", " 0.2500\n", " 0.5000\n", " 0.7500\n", " 1.0000\n", "\n", " 1.0000\n", " 1.0000\n", " 1.0000\n", " 1.0000\n", " 1.0000\n", "\n", " 2.5000\n", "\n", "\n", "Random matrices in Armadillo\n", " 7.8264e-06 5.3277e-01 6.7930e-01 8.3097e-01 6.7115e-01\n", " 1.3154e-01 2.1896e-01 9.3469e-01 3.4572e-02 7.6982e-03\n", " 7.5561e-01 4.7045e-02 3.8350e-01 5.3462e-02 3.8342e-01\n", " 4.5865e-01 6.7886e-01 5.1942e-01 5.2970e-01 6.6842e-02\n", "\n", " -0.7649 1.2041 -0.7020 1.1862 0.8284\n", " 1.7313 0.0937 1.6814 -0.8631 0.9426\n", " 0.1454 -0.6920 0.2742 0.6810 0.2091\n", " 0.7032 0.2610 0.1752 1.3165 -0.5897\n", "\n", " 1.7063 1.1075 0.5238 0.9563\n", " -0.4457 1.7973 0.1490 0.3544\n", " -0.4095 2.2726 0.2990 0.4551\n", " 0.7857 1.3368 0.1140 1.2487\n", "\n", " 2.7142\n", " 1.3275\n", " 1.6230\n", " 2.2535\n", "\n", "\n", "QR in Armadillo\n", " -0.6776 0.6377 0.3253 0.0944 -0.1395\n", " -0.3188 -0.4409 0.3521 0.4177 0.6368\n", " -0.5524 -0.2366 -0.7774 0.1504 -0.1092\n", " -0.2443 -0.5816 0.4071 -0.1709 -0.6380\n", " -0.2727 -0.0688 -0.0099 -0.8745 0.3950\n", "\n", " -1.1785e+00 -1.3394e+00 -2.1015e+00 -1.3527e+00 -1.0229e+00\n", " 0e+00 -8.3421e-01 -9.7607e-01 -9.9515e-01 -5.3245e-01\n", " 0e+00 0e+00 -4.6336e-01 7.6793e-02 -4.0376e-03\n", " 0e+00 0e+00 0e+00 -2.0273e-01 -3.2745e-01\n", " 0e+00 0e+00 0e+00 0e+00 1.1102e-16\n", "\n" ] } ], "source": [ "%%bash\n", "\n", "./main" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }