{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to C++" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting dtypes.cpp\n" ] } ], "source": [ "%%file dtypes.cpp\n", "\n", "#include \n", "#include \n", "\n", "using std::cout;\n", "\n", "int main() {\n", " // Boolean\n", " bool a = true, b = false; \n", " \n", " cout << \"and \" << (a and b) << \"\\n\";\n", " cout << \"&& \" << (a && b) << \"\\n\";\n", " cout << \"or \" << (a or b) << \"\\n\";\n", " cout << \"|| \" << (a || b) << \"\\n\";\n", " cout << \"not \" << not (a or b) << \"\\n\";\n", " cout << \"! \" << !(a or b) << \"\\n\";\n", "\n", " // Integral numbers\n", " cout << \"char \" << sizeof(char) << \"\\n\";\n", " cout << \"short int \" << sizeof(short int) << \"\\n\";\n", " cout << \"int \" << sizeof(int) << \"\\n\";\n", " cout << \"long \" << sizeof(long) << \"\\n\";\n", "\n", " // Floating point numbers\n", " cout << \"float \" << sizeof(float) << \"\\n\";\n", " cout << \"double \" << sizeof(double) << \"\\n\";\n", " cout << \"long double \" << sizeof(long double) << \"\\n\";\n", " cout << \"complex double \" << sizeof(std::complex) << \"\\n\";\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compilation" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ dtypes.cpp -o dtypes.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execution" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "and 0\n", "&& 0\n", "or 1\n", "|| 1\n", "not 0\n", "! 0\n", "char 1\n", "short int 2\n", "int 4\n", "long 8\n", "float 4\n", "double 8\n", "long double 16\n", "complex double 16\n" ] } ], "source": [ "%%bash\n", "\n", "./dtypes.exe" ] }, { "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": [ "## Type conversions" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting type.cpp\n" ] } ], "source": [ "%%file type.cpp\n", "#include \n", "using std::cout;\n", "using std::string;\n", "using std::stoi;\n", " \n", "int main() {\n", " char c = '3'; // A char is an integer type\n", " string s = \"3\"; // A string is not an integer type\n", " int i = 3;\n", " float f = 3;\n", " double d = 3;\n", " \n", " cout << c << \"\\n\";\n", " cout << i << \"\\n\";\n", " cout << f << \"\\n\";\n", " cout << d << \"\\n\";\n", " \n", " cout << \"c + i is \" << c + i << \"\\n\";\n", " cout << \"c + i is \" << c - '0' + i << \"\\n\";\n", " cout << \"s + i is \" << stoi(s) + i << \"\\n\"; // Use std::stod to convert to double\n", "}\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o type.exe type.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "3\n", "3\n", "3\n", "c + i is 54\n", "c + i is 6\n", "s + i is 6\n" ] } ], "source": [ "%%bash\n", "\n", "./type.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Command line inputs" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting command_args.cpp\n" ] } ], "source": [ "%%file command_args.cpp\n", "#include \n", "#include \n", "using std::cout;\n", "using std::stoi;\n", "\n", "int main(int argc, char *argv[]) {\n", " for (int i=0; i\n", "#include \n", "using std::cout;\n", "using std::stod;\n", " \n", "int main(int argc, char *argv[]) {\n", " if (argc ==2 ) {\n", " double x = stod(argv[1]);\n", " cout << x*x << \"\\n\";\n", " } else {\n", " cout << \"Usage: ex01.exe \\n\";\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o ex01.exe ex01.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n" ] } ], "source": [ "%%bash\n", "\n", "./ex01.exe 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting func01.cpp\n" ] } ], "source": [ "%%file func01.cpp\n", "\n", "#include \n", "\n", "double add(double x, double y) {\n", " return x + y;\n", "}\n", "\n", "double mult(double x, double y) {\n", " return x * y;\n", "}\n", "\n", "int main() {\n", " double a = 3;\n", " double b = 4;\n", " \n", " std::cout << add(a, b) << std::endl;\n", " std::cout << mult(a, b) << std::endl;\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compilation" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o func01.exe func01.cpp -std=c++14" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execution" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "12\n" ] } ], "source": [ "%%bash\n", "\n", "./func01.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Header, implementation and driver files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Header file(s)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting func02.hpp\n" ] } ], "source": [ "%%file func02.hpp\n", "\n", "double add(double x, double y);\n", "double mult(double x, double y);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Implementation file(s)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting func02.cpp\n" ] } ], "source": [ "%%file func02.cpp\n", "\n", "double add(double x, double y) {\n", " return x + y;\n", "}\n", "\n", "double mult(double x, double y) {\n", " return x * y;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Driver program" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting test_func02.cpp\n" ] } ], "source": [ "%%file test_func02.cpp\n", "\n", "#include \n", "#include \"func02.hpp\"\n", "\n", "int main() {\n", " double a = 3;\n", " double b = 4;\n", " \n", " std::cout << add(a, b) << std::endl;\n", " std::cout << mult(a, b) << std::endl;\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compilation" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ test_func02.cpp func02.cpp -o main02.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execution" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "12\n" ] } ], "source": [ "%%bash\n", "\n", "./test_func02.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `make`" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting Makefile\n" ] } ], "source": [ "%%file Makefile\n", "\n", "test_func02.exe: test_func02.o func02.o\n", "\t g++ -o test_func02.exe test_func02.o func02.o\n", " \n", "test_func02.o: test_func02.cpp func02.hpp\n", "\t g++ -c test_func02.cpp\n", " \n", "func02.o: func02.cpp\n", "\t g++ -c func02.cpp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compilation" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "g++ -c test_func02.cpp\n", "g++ -c func02.cpp\n", "g++ -o test_func02.exe test_func02.o func02.o\n" ] } ], "source": [ "%%bash\n", "\n", "make" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execution" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "12\n" ] } ], "source": [ "%%bash\n", "\n", "./test_func02.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A more flexible Makefile" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting Makefile2\n" ] } ], "source": [ "%%file Makefile2\n", "\n", "CC=g++\n", "CFLAGS=-Wall -std=c++14\n", "\n", "test_func02.exe: test_func02.o func02.o\n", "\t $(CC) $(CFLAGS) -o test_func02.exe test_func02.o func02.o\n", " \n", "test_func02.o: test_func02.cpp func02.hpp\n", "\t $(CC) $(CFLAGS) -c test_func02.cpp\n", " \n", "func02.o: func02.cpp\n", "\t $(CC) $(CFLAGS) -c func02.cpp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Compilation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that no re-compilation occurs!" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "make: 'test_func02.exe' is up to date.\n" ] } ], "source": [ "%%bash\n", "\n", "make -f Makefile2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execution" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "12\n" ] } ], "source": [ "%%bash \n", "\n", "./test_func02.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Input and output" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting data.txt\n" ] } ], "source": [ "%%file data.txt\n", "9 6" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting io.cpp\n" ] } ], "source": [ "%%file io.cpp\n", "\n", "#include \n", "#include \"func02.hpp\"\n", "\n", "int main() {\n", " std::ifstream fin(\"data.txt\");\n", " std::ofstream fout(\"result.txt\");\n", " \n", " double a, b;\n", " \n", " fin >> a >> b;\n", " fin.close();\n", " \n", " fout << add(a, b) << std::endl;\n", " fout << mult(a, b) << std::endl;\n", " fout.close();\n", "}" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ io.cpp -o io.exe func02.cpp " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "./io.exe" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\r\n", "54\r\n" ] } ], "source": [ "! cat result.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arrays" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting array.cpp\n" ] } ], "source": [ "%%file array.cpp\n", "\n", "#include \n", "using std::cout;\n", "using std::endl;\n", " \n", "int main() {\n", " \n", " int N = 3;\n", " double counts[N];\n", " \n", " counts[0] = 1;\n", " counts[1] = 3;\n", " counts[2] = 3;\n", "\n", " double avg = (counts[0] + counts[1] + counts[2])/3;\n", " \n", " cout << avg << endl; \n", "}" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o array.exe array.cpp " ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.33333\n" ] } ], "source": [ "%%bash\n", "\n", "./array.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loops" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing loop.cpp\n" ] } ], "source": [ "%%file loop.cpp\n", "\n", "#include \n", "using std::cout;\n", "using std::endl;\n", "using stl::begin;\n", "\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 << \"\\nUsing iterators\\n\";\n", " for (auto it=begin(x); it != end(x); it++) {\n", " cout << *it << endl;\n", " }\n", " \n", " cout << \"\\nRanged for loop\\n\\n\";\n", " for (auto &i : x) {\n", " cout << i << endl;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o loop.exe loop.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\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" ] } ], "source": [ "%%bash\n", "\n", "./loop.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2**\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": {}, "outputs": [], "source": [ "%%file ex1.cpp\n", "#include \n", "#include \n", "\n", "int main()\n", "{\n", " for (int i=1; i<=12; i++) {\n", " for (int j=1; j<=12; j++) {\n", " std::cout << std::setw(3) << i*j << ' ';\n", " }\n", " std::cout << \"\\n\";\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "g++ ex1.cpp -o ex1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "./ex1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function arguments" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting func_arg.cpp\n" ] } ], "source": [ "%%file func_arg.cpp\n", "\n", "#include \n", "using std::cout;\n", "using std::endl;\n", " \n", "// Value parameter\n", "void f1(int x) {\n", " x *= 2;\n", " cout << \"In f1 : x=\" << x << endl;\n", "}\n", "\n", "// Reference parameter\n", "void f2(int &x) {\n", " x *= 2;\n", " cout << \"In f2 : x=\" << x << endl;\n", "}\n", "\n", "/* Note\n", "If you want to avoid side effects \n", "but still use references to avoid a copy operation\n", "use a const refernece like this to indicate that x cannot be changed\n", "\n", "void f2(const int &x) \n", "*/\n", "\n", "/* Note \n", "Raw pointers are prone to error and \n", "generally avoided in modern C++\n", "See unique_ptr and shared_ptr\n", "*/\n", "\n", "// Raw pointer parameter\n", "void f3(int *x) {\n", " *x *= 2;\n", " cout << \"In f3 : x=\" << *x << endl;\n", "}\n", " \n", "int main() {\n", " int x = 1;\n", " \n", " cout << \"Before f1: x=\" << x << \"\\n\";\n", " f1(x);\n", " cout << \"After f1 : x=\" << x << \"\\n\";\n", " \n", " cout << \"Before f2: x=\" << x << \"\\n\";\n", " f2(x);\n", " cout << \"After f2 : x=\" << x << \"\\n\";\n", " \n", " cout << \"Before f3: x=\" << x << \"\\n\";\n", " f3(&x);\n", " cout << \"After f3 : x=\" << x << \"\\n\";\n", "}" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "c++ -o func_arg.exe func_arg.cpp --std=c++14" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Before f1: x=1\n", "In f1 : x=2\n", "After f1 : x=1\n", "Before f2: x=1\n", "In f2 : x=2\n", "After f2 : x=2\n", "Before f3: x=2\n", "In f3 : x=4\n", "After f3 : x=4\n" ] } ], "source": [ "%%bash \n", "\n", "./func_arg.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 3**\n", "\n", "Write a C++ program ex02.cpp that uses a function to calculate the 10th Fibonacci number. Here is the Python version to calculate the nth Fibonacci number for comparison." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "def fib(n):\n", " a, b = 0, 1\n", " for i in range(n):\n", " a, b = b, a + b\n", " return a" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib(10)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting ex02.cpp\n" ] } ], "source": [ "%%file ex02.cpp\n", "\n", "#include \n", "using std::cout;\n", "\n", "// In practice, would probably use double rather than int\n", "// as C++ ints will overflow when n is large.\n", "int fib(int n) {\n", " int a = 0;\n", " int b = 1;\n", " int tmp;\n", " for (int i=0; i\n", "using std::cout;\n", "using std::endl;\n", "\n", "int main() {\n", "\n", " int a = 3, b = 4;\n", " int c = 0;\n", "\n", " // Lambda function with no capture\n", " auto add1 = [] (int a, int b) { return a + b; };\n", " // Lambda function with value capture\n", " auto add2 = [c] (int a, int b) { return c * (a + b); };\n", " // Lambda funciton with reference capture \n", " auto add3 = [&c] (int a, int b) { return c * (a + b); };\n", "\n", " // Change value of c after function definition\n", " c += 5;\n", "\n", " cout << \"Lambda function\\n\";\n", " cout << add1(a, b) << endl;\n", " cout << \"Lambda function with value capture\\n\";\n", " cout << add2(a, b) << endl;\n", " cout << \"Lambda function with reference capture\\n\";\n", " cout << add3(a, b) << endl;\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "c++ -o lambda.exe lambda.cpp --std=c++14" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Lambda function\n", "7\n", "Lambda function with value capture\n", "0\n", "Lambda function with reference capture\n", "35\n" ] } ], "source": [ "%%bash\n", "\n", "./lambda.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function pointers" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting func_pointer.cpp\n" ] } ], "source": [ "%%file func_pointer.cpp\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "using std::cout;\n", "using std::endl;\n", "using std::function;\n", "using std::vector;\n", "\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", "}" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o func_pointer.exe func_pointer.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Using generalized function pointers\n", "7\n", "12\n", "19\n" ] } ], "source": [ "%%bash\n", "\n", "./func_pointer.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generic programming with templates" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting template.cpp\n" ] } ], "source": [ "%%file template.cpp\n", "\n", "#include \n", "\n", "template\n", "T add(T a, T b) {\n", " return a + b;\n", "}\n", "\n", "int main() {\n", " int m =2, n =3;\n", " double u = 2.5, v = 4.5;\n", " \n", " std::cout << add(m, n) << std::endl;\n", " std::cout << add(u, v) << std::endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o template.exe template.cpp" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "7\n" ] } ], "source": [ "%%bash\n", "\n", "./template.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Standard template library (STL)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting stl.cpp\n" ] } ], "source": [ "%%file stl.cpp\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "using std::vector;\n", "using std::map;\n", "using std::unordered_map;\n", "using std::string;\n", "using std::cout;\n", "using std::endl;\n", " \n", "struct Point{\n", " int x;\n", " int y;\n", " \n", " Point(int x_, int y_) : \n", " x(x_), y(y_) {};\n", "};\n", "\n", "int main() {\n", " vector v1 = {1,2,3};\n", " v1.push_back(4);\n", " v1.push_back(5);\n", " \n", " cout << \"Vecotr\" << endl;\n", " for (auto n: v1) {\n", " cout << n << endl;\n", " }\n", " cout << endl;\n", " \n", " vector v2;\n", " v2.push_back(Point(1, 2));\n", " v2.emplace_back(3,4);\n", " \n", " cout << \"Vector\" << endl;\n", " for (auto p: v2) {\n", " cout << \"(\" << p.x << \", \" << p.y << \")\" << endl;\n", " }\n", " cout << endl;\n", " \n", " map v3 = {{\"foo\", 1}, {\"bar\", 2}};\n", " v3[\"hello\"] = 3;\n", " v3.insert({\"goodbye\", 4}); \n", " \n", " // Note the a C++ map is ordered\n", " // Note using (traditional) iterators instead of ranged for loop\n", " cout << \"Map\" << endl;\n", " for (auto iter=v3.begin(); iter != v3.end(); iter++) {\n", " cout << iter->first << \": \" << iter->second << endl;\n", " }\n", " cout << endl;\n", " \n", " unordered_map v4 = {{\"foo\", 1}, {\"bar\", 2}};\n", " v4[\"hello\"] = 3;\n", " v4.insert({\"goodbye\", 4}); \n", " \n", " // Note theunordered_map is similar to Python' dict.'\n", " // Note using (traditional) iterators instead of ranged for loop\n", " cout << \"Unordered_map\" << endl;\n", " for (auto i: v4) {\n", " cout << i.first << \": \" << i.second << endl;\n", " }\n", " cout << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o stl.exe stl.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vecotr\n", "1\n", "2\n", "3\n", "4\n", "5\n", "\n", "Vector\n", "(1, 2)\n", "(3, 4)\n", "\n", "Map\n", "bar: 2\n", "foo: 1\n", "goodbye: 4\n", "hello: 3\n", "\n", "Unordered_map\n", "goodbye: 4\n", "hello: 3\n", "bar: 2\n", "foo: 1\n", "\n" ] } ], "source": [ "%%bash\n", "\n", "./stl.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 4**\n", "\n", "Write a C++ program ex03.cpp that implements a map(func, xs) function where xs is an vector of integers and func is any function that takes an integer and returns another integer. Map should return an vector of integers. Test it on a vector [1,2,3,4] and an anonymous function that adds 3 to its input argument." ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting ex03.cpp\n" ] } ], "source": [ "%%file ex03.cpp\n", "\n", "#include \n", "#include \n", "#include \n", "using std::cout;\n", "using std::vector;\n", "using std::function; \n", "\n", "using func = function;\n", " \n", "vector map(func f, vector xs) {\n", " vector res;\n", " for (auto x: xs) {\n", " res.push_back(f(x));\n", " }\n", " return res;\n", "}\n", "\n", "int main() {\n", " vector xs = {1,2,3,4};\n", " vector ys = map([](int x){ return 3+x;}, xs);\n", " for (auto y: ys) {\n", " cout << y << \"\\n\";\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o ex03.exe ex03.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n", "6\n", "7\n" ] } ], "source": [ "%%bash\n", "\n", "./ex03.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## STL algorithms" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting stl_algorithm.cpp\n" ] } ], "source": [ "%%file stl_algorithm.cpp\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "using std::cout;\n", "using std::endl;\n", "using std::vector;\n", "using std::begin;\n", "using std::end;\n", " \n", "int main() {\n", " vector v(10);\n", "\n", " // iota is somewhat like range\n", " std::iota(v.begin(), v.end(), 1);\n", " \n", " for (auto i: v) {\n", " cout << i << \" \";\n", " }\n", " cout << endl;\n", " \n", " // C++ version of reduce \n", " cout << std::accumulate(begin(v), end(v), 0) << endl;\n", " \n", " // Accumulate with lambda\n", " cout << std::accumulate(begin(v), end(v), 1, [](int a, int b){return a * b; }) << endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o stl_algorithm.exe stl_algorithm.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5 6 7 8 9 10 \n", "55\n", "3628800\n" ] } ], "source": [ "%%bash\n", "\n", "./stl_algorithm.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Random numbers" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting random.cpp\n" ] } ], "source": [ "%%file random.cpp\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "using std::cout;\n", "using std::default_random_engine;\n", "using std::uniform_int_distribution;\n", "using std::poisson_distribution;\n", "using std::student_t_distribution;\n", "using std::bind;\n", " \n", "// start random number engine with 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", "int main() \n", "{\n", " cout << \"\\nGenerating random numbers\\n\";\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", "}" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o random.exe random.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Generating random numbers\n", "1, 29, 0.134715\n", "6, 25, -0.537355\n", "6, 25, -0.658889\n", "1, 31, -0.669327\n", "1, 24, -1.0099\n", "1, 31, 0.426892\n", "5, 30, 0.173495\n", "4, 38, 1.54426\n", "6, 24, -1.50522\n", "5, 26, -0.477974\n" ] } ], "source": [ "%%bash\n", "\n", "./random.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 5**\n", "\n", "Generate 100 numbers from $N(100, 15)$ in C++. Write to a plain text file that can be read in Python. Plot a normalized histogram of the numbers." ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting ex04.cpp\n" ] } ], "source": [ "%%file ex04.cpp\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "using std::cout;\n", "using std::endl;\n", "using std::ofstream;\n", " \n", "using std::default_random_engine;\n", "using std::normal_distribution;\n", "using std::bind;\n", " \n", "// start random number engine with fixed seed\n", "default_random_engine re{12345};\n", "\n", "normal_distribution norm(100, 15); // mean and standard deviation\n", "auto rnorm = bind(norm, re); \n", "\n", "int main() {\n", " ofstream fout(\"norm_data.txt\");\n", " \n", " for (int i=0; i<100; i++) {\n", " fout << rnorm() << \"\\n\";\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "%%bash \n", "\n", "g++ -o ex04.exe ex04.cpp -std=c++14" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "./ex04.exe" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "try:\n", " data = np.loadtxt('norm_data.txt')\n", " plt.hist(data, density=True)\n", " plt.show()\n", "except FileNotFoundError:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numerics\n", "\n", "Using the Eigen library." ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting numeric.cpp\n" ] } ], "source": [ "%%file numeric.cpp\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "using std::cout;\n", "using std::endl;\n", "using std::ofstream;\n", " \n", "using std::default_random_engine;\n", "using std::normal_distribution;\n", "using std::bind;\n", " \n", "// start random number engine with fixed seed\n", "default_random_engine re{12345};\n", "\n", "normal_distribution norm(5,2); // mean and standard deviation\n", "auto rnorm = bind(norm, re); \n", " \n", "int main() \n", "{\n", " using namespace Eigen;\n", "\n", " VectorXd x1(6);\n", " x1 << 1, 2, 3, 4, 5, 6;\n", " VectorXd x2 = VectorXd::LinSpaced(6, 1, 2);\n", " VectorXd x3 = VectorXd::Zero(6);\n", " VectorXd x4 = VectorXd::Ones(6);\n", " VectorXd x5 = VectorXd::Constant(6, 3);\n", " VectorXd x6 = VectorXd::Random(6);\n", " \n", " double data[] = {6,5,4,3,2,1};\n", " Map x7(data, 6);\n", "\n", " VectorXd x8 = x6 + x7;\n", " \n", " MatrixXd A1(3,3);\n", " A1 << 1 ,2, 3,\n", " 4, 5, 6,\n", " 7, 8, 9;\n", " MatrixXd A2 = MatrixXd::Constant(3, 4, 1);\n", " MatrixXd A3 = MatrixXd::Identity(3, 3);\n", " \n", " Map A4(data, 3, 2);\n", " \n", " MatrixXd A5 = A4.transpose() * A4;\n", " MatrixXd A6 = x7 * x7.transpose();\n", " MatrixXd A7 = A4.array() * A4.array();\n", " MatrixXd A8 = A7.array().log();\n", " MatrixXd A9 = A8.unaryExpr([](double x) { return exp(x); });\n", " MatrixXd A10 = MatrixXd::Zero(3,4).unaryExpr([](double x) { return rnorm(); });\n", "\n", " VectorXd x9 = A1.colwise().norm();\n", " VectorXd x10 = A1.rowwise().sum();\n", " \n", " MatrixXd A11(x1.size(), 3);\n", " A11 << x1, x2, x3;\n", " \n", " MatrixXd A12(3, x1.size());\n", " A12 << x1.transpose(),\n", " x2.transpose(),\n", " x3.transpose();\n", " \n", " JacobiSVD svd(A10, ComputeThinU | ComputeThinV);\n", " \n", " \n", " cout << \"x1: comman initializer\\n\" << x1.transpose() << \"\\n\\n\";\n", " cout << \"x2: linspace\\n\" << x2.transpose() << \"\\n\\n\";\n", " cout << \"x3: zeors\\n\" << x3.transpose() << \"\\n\\n\";\n", " cout << \"x4: ones\\n\" << x4.transpose() << \"\\n\\n\";\n", " cout << \"x5: constant\\n\" << x5.transpose() << \"\\n\\n\";\n", " cout << \"x6: rand\\n\" << x6.transpose() << \"\\n\\n\";\n", " cout << \"x7: mapping\\n\" << x7.transpose() << \"\\n\\n\";\n", " cout << \"x8: element-wise addition\\n\" << x8.transpose() << \"\\n\\n\";\n", " \n", " cout << \"max of A1\\n\";\n", " cout << A1.maxCoeff() << \"\\n\\n\";\n", " cout << \"x9: norm of columns of A1\\n\" << x9.transpose() << \"\\n\\n\";\n", " cout << \"x10: sum of rows of A1\\n\" << x10.transpose() << \"\\n\\n\"; \n", " \n", " cout << \"head\\n\";\n", " cout << x1.head(3).transpose() << \"\\n\\n\";\n", " cout << \"tail\\n\";\n", " cout << x1.tail(3).transpose() << \"\\n\\n\";\n", " cout << \"slice\\n\";\n", " cout << x1.segment(2, 3).transpose() << \"\\n\\n\";\n", " \n", " cout << \"Reverse\\n\";\n", " cout << x1.reverse().transpose() << \"\\n\\n\";\n", " \n", " cout << \"Indexing vector\\n\";\n", " cout << x1(0);\n", " cout << \"\\n\\n\";\n", " \n", " cout << \"A1: comma initilizer\\n\";\n", " cout << A1 << \"\\n\\n\";\n", " cout << \"A2: constant\\n\";\n", " cout << A2 << \"\\n\\n\";\n", " cout << \"A3: eye\\n\";\n", " cout << A3 << \"\\n\\n\";\n", " cout << \"A4: mapping\\n\";\n", " cout << A4 << \"\\n\\n\";\n", " cout << \"A5: matrix multiplication\\n\";\n", " cout << A5 << \"\\n\\n\";\n", " cout << \"A6: outer product\\n\";\n", " cout << A6 << \"\\n\\n\"; \n", " cout << \"A7: element-wise multiplication\\n\";\n", " cout << A7 << \"\\n\\n\"; \n", " cout << \"A8: ufunc log\\n\";\n", " cout << A8 << \"\\n\\n\"; \n", " cout << \"A9: custom ufucn\\n\";\n", " cout << A9 << \"\\n\\n\"; \n", " cout << \"A10: custom ufunc for normal deviates\\n\";\n", " cout << A10 << \"\\n\\n\"; \n", " cout << \"A11: np.c_\\n\";\n", " cout << A11 << \"\\n\\n\"; \n", " cout << \"A12: np.r_\\n\";\n", " cout << A12 << \"\\n\\n\"; \n", " \n", " cout << \"2x2 block startign at (0,1)\\n\";\n", " cout << A1.block(0,1,2,2) << \"\\n\\n\";\n", " cout << \"top 2 rows of A1\\n\";\n", " cout << A1.topRows(2) << \"\\n\\n\";\n", " cout << \"bottom 2 rows of A1\";\n", " cout << A1.bottomRows(2) << \"\\n\\n\";\n", " cout << \"leftmost 2 cols of A1\";\n", " cout << A1.leftCols(2) << \"\\n\\n\";\n", " cout << \"rightmost 2 cols of A1\";\n", " cout << A1.rightCols(2) << \"\\n\\n\";\n", "\n", " cout << \"Diagonal elements of A1\\n\";\n", " cout << A1.diagonal() << \"\\n\\n\";\n", " A1.diagonal() = A1.diagonal().array().square();\n", " cout << \"Transforming diagonal eelemtns of A1\\n\";\n", " cout << A1 << \"\\n\\n\";\n", " \n", " cout << \"Indexing matrix\\n\";\n", " cout << A1(0,0) << \"\\n\\n\";\n", " \n", " cout << \"singular values\\n\";\n", " cout << svd.singularValues() << \"\\n\\n\";\n", " \n", " cout << \"U\\n\";\n", " cout << svd.matrixU() << \"\\n\\n\";\n", " \n", " cout << \"V\\n\";\n", " cout << svd.matrixV() << \"\\n\\n\";\n", "}" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "\n", "g++ -o numeric.exe numeric.cpp -std=c++14 -I/usr/include/eigen3" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x1: comman initializer\n", "1 2 3 4 5 6\n", "\n", "x2: linspace\n", " 1 1.2 1.4 1.6 1.8 2\n", "\n", "x3: zeors\n", "0 0 0 0 0 0\n", "\n", "x4: ones\n", "1 1 1 1 1 1\n", "\n", "x5: constant\n", "3 3 3 3 3 3\n", "\n", "x6: rand\n", " 0.680375 -0.211234 0.566198 0.59688 0.823295 -0.604897\n", "\n", "x7: mapping\n", "6 5 4 3 2 1\n", "\n", "x8: element-wise addition\n", " 6.68038 4.78877 4.5662 3.59688 2.82329 0.395103\n", "\n", "max of A1\n", "9\n", "\n", "x9: norm of columns of A1\n", "8.12404 9.64365 11.225\n", "\n", "x10: sum of rows of A1\n", " 6 15 24\n", "\n", "head\n", "1 2 3\n", "\n", "tail\n", "4 5 6\n", "\n", "slice\n", "3 4 5\n", "\n", "Reverse\n", "6 5 4 3 2 1\n", "\n", "Indexing vector\n", "1\n", "\n", "A1: comma initilizer\n", "1 2 3\n", "4 5 6\n", "7 8 9\n", "\n", "A2: constant\n", "1 1 1 1\n", "1 1 1 1\n", "1 1 1 1\n", "\n", "A3: eye\n", "1 0 0\n", "0 1 0\n", "0 0 1\n", "\n", "A4: mapping\n", "6 3\n", "5 2\n", "4 1\n", "\n", "A5: matrix multiplication\n", "77 32\n", "32 14\n", "\n", "A6: outer product\n", "36 30 24 18 12 6\n", "30 25 20 15 10 5\n", "24 20 16 12 8 4\n", "18 15 12 9 6 3\n", "12 10 8 6 4 2\n", " 6 5 4 3 2 1\n", "\n", "A7: element-wise multiplication\n", "36 9\n", "25 4\n", "16 1\n", "\n", "A8: ufunc log\n", "3.58352 2.19722\n", "3.21888 1.38629\n", "2.77259 0\n", "\n", "A9: custom ufucn\n", "36 9\n", "25 4\n", "16 1\n", "\n", "A10: custom ufunc for normal deviates\n", "5.22353 6.16474 3.67474 6.18264\n", "3.81868 4.07998 3.52576 3.82792\n", "3.74872 5.76697 5.3517 2.85655\n", "\n", "A11: np.c_\n", " 1 1 0\n", " 2 1.2 0\n", " 3 1.4 0\n", " 4 1.6 0\n", " 5 1.8 0\n", " 6 2 0\n", "\n", "A12: np.r_\n", " 1 2 3 4 5 6\n", " 1 1.2 1.4 1.6 1.8 2\n", " 0 0 0 0 0 0\n", "\n", "2x2 block startign at (0,1)\n", "2 3\n", "5 6\n", "\n", "top 2 rows of A1\n", "1 2 3\n", "4 5 6\n", "\n", "bottom 2 rows of A14 5 6\n", "7 8 9\n", "\n", "leftmost 2 cols of A11 2\n", "4 5\n", "7 8\n", "\n", "rightmost 2 cols of A12 3\n", "5 6\n", "8 9\n", "\n", "Diagonal elements of A1\n", "1\n", "5\n", "9\n", "\n", "Transforming diagonal eelemtns of A1\n", " 1 2 3\n", " 4 25 6\n", " 7 8 81\n", "\n", "Indexing matrix\n", "1\n", "\n", "singular values\n", "15.8886\n", "2.58818\n", "0.54229\n", "\n", "U\n", " -0.67345 0.607383 0.421368\n", "-0.479529 0.0748696 -0.874326\n", "-0.562598 -0.790873 0.240837\n", "\n", "V\n", " -0.46939 0.190799 -0.433197\n", "-0.588634 -0.197482 0.773183\n", "-0.451663 -0.670964 -0.452453\n", "-0.478731 0.68877 -0.099069\n", "\n" ] } ], "source": [ "%%bash\n", "\n", "./numeric.exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Check SVD" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "A10 = np.array([\n", " [5.17237, 3.73572, 6.29422, 6.55268],\n", " [5.33713, 3.88883, 1.93637, 4.39812],\n", " [8.22086, 6.94502, 6.36617, 6.5961]\n", "])\n", "\n", "U, s, Vt = np.linalg.svd(A10, full_matrices=False)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 19.50007376, 2.80674189, 1.29869186])" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.55849978, -0.75124103, -0.3517313 ],\n", " [-0.40681745, 0.61759344, -0.67311062],\n", " [-0.72289526, 0.2328417 , 0.65054376]])" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "U" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.56424535, 0.47194895, -0.04907563],\n", " [-0.44558625, 0.43195279, 0.45157518],\n", " [-0.45667231, -0.73048295, 0.48064272],\n", " [-0.52395657, -0.23890509, -0.75010267]])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Vt.T" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }