In [1]:
%mkdir hello
%cd hello
In [2]:
%%file hello.cpp
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "Hello, world" << endl;
}
In [3]:
! g++ hello.cpp -o hello
In [4]:
! ./hello
In [5]:
%cd ..
In [6]:
%mkdir add1
%cd add1
In [7]:
%%file add.cpp
#include <iostream>
using std::cout;
using std::endl;
double add(double a, double b) {
return a + b;
}
int main() {
double a = 1.0, b= 2.0;
double c = add(a, b);
cout << a << " + " << b << " = " << c << endl;
}
In [8]:
! g++ add.cpp -o add
In [9]:
! ./add
In [10]:
%cd ..
In [11]:
%mkdir add2
%cd add2
In [12]:
%%file add.hpp
#pragma once
double add(double a, double b);
In [13]:
%%file add.cpp
double add(double a, double b) {
return a + b;
}
In [14]:
%%file add_driver.cpp
#include "add.hpp"
#include <iostream>
using std::cout;
using std::endl;
int main() {
double a = 1.0, b = 2.0;
double c = add(a, b);
cout << a << " + " << b << " = " << c << endl;
}
In [15]:
%%bash
g++ add_driver.cpp add.cpp -o add_driver
./add_driver
In [16]:
%cd ..
In [17]:
%mkdir add3
%cp add2/add.cpp add2/add.hpp add2/add_driver.cpp add3/
%cd add3
In [18]:
%%file Makefile
add_driver: add_driver.o add.o
g++ add_driver.o add.o -o add_driver
add_driver.o: add_driver.cpp add.hpp
g++ -c add_driver.cpp
add.o: add.cpp
g++ -c add.cpp
.PHONY: clean
clean:
rm add_driver *.o
In [19]:
! make
In [20]:
! make clean
In [21]:
! make
In [22]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = add_driver
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [23]:
%%bash
make clean
make
./add_driver
In [24]:
%cd ..
In [25]:
%mkdir linker
%cd linker
In [26]:
%%file test_linker.cpp
#include <cmath>
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "2^10 = " << pow(2, 10) << endl;
}
In [27]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = test_linker
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [28]:
! make
In [29]:
! ./test_linker
In [30]:
%cd ..
In [31]:
%mkdir arrays
%cd arrays
In [32]:
%%file arrays.cpp
#include <cmath>
#include <iostream>
using std::cout;
using std::endl;
int main() {
// pointers and address-of opertor
int a = 1, b = 2;
int *p = &a, *q = &b;
cout << a << ", " << b << endl;
cout << *p << ", " << *q << endl;
cout << p << ", " << q << endl;
// An array name is just a pointer
int ms[] = {1,2,3,4};
// using indexing
cout << ms[0] << ", " << ms[1] << endl;
// using pointer arithmetic
cout << *(ms) << ", " << *(ms + 0) << ", " << *(ms + 2) << endl;
cout << 2[ms] << ", " << 3[ms] << endl; // wait, what??
// size of an array
cout << sizeof(ms)/sizeof(*ms) << endl;
}
In [33]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = arrays
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [34]:
%%bash
make
./arrays
In [35]:
%cd ..
In [36]:
%mkdir loops
%cd loops
In [37]:
%%file loops.cpp
#include <cmath>
#include <iostream>
using std::cout;
using std::endl;
int main() {
double xs[] = {0,1,2,3,4,5,6,7,8,9};
// looping with an index
for (int i=0; i<sizeof(xs)/sizeof(*xs); i++) {
cout << pow(xs[i], 2) << " ";
}
cout << endl;
// looping with an iterator
for (auto it=std::begin(xs); it!=std::end(xs); it++) {
cout << pow(*it, 2) << " ";
}
cout << endl;
// ranged for loop
for (auto x : xs) {
cout << pow(x, 2) << " ";
}
cout << endl;
}
In [38]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = loops
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [39]:
%%bash
make clean
make
./loops
In [40]:
%cd ..
In [41]:
%mkdir funcs1
%cd funcs1
In [42]:
%%file funcs1.cpp
#include <iostream>
using std::cout;
using std::endl;
double sum(double *xs, int n) {
double s = 0.0;
for (int i=0; i<n; i++) {
s += xs[i];
}
return s;
}
void triple(double *xs, double * ys, int n) {
for (int i=0; i<n; i++) {
ys[i] = 3 * xs[i];
}
}
int main() {
double xs[] = {1,2,3,4};
int n = sizeof(xs)/sizeof(*xs);
cout << sum(xs, n) << endl;
double ys[n];
triple(xs, ys, n);
for (auto y : ys) {
cout << y << " ";
}
cout << endl;
}
In [43]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = funcs1
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [44]:
%%bash
make
./funcs1
In [45]:
%cd ..
In [46]:
%mkdir funcs2
%cd funcs2
In [47]:
%%file funcs2.cpp
#include <iostream>
using std::cout;
using std::endl;
int main() {
double k = 5.0;
double a = 1.0, b = 2.0;
auto add1 = [](int a, int b) { return a + b; };
auto add2 = [k](int a, int b) { return a + b + k; };
auto add3 = [&k](int a, int b) { return a + b + k; };
k *= 2;
cout << "Lambda: " << add1(a, b) << endl;
cout << "Lambda with capture by value: " << add2(a, b) << endl;
cout << "Lmabda with capture by reference: " << add3(a, b) << endl;
}
In [48]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = funcs2
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [49]:
%%bash
make clean
make
./funcs2
In [50]:
%cd ..
In [51]:
%mkdir templates
%cd templates
In [52]:
%%file templates.cpp
#include <iostream>
#include <vector>
#include <list>
#include <numeric>
using std::cout;
using std::endl;
using std::list;
using std::vector;
template<typename T>
T sum(vector<T> xs) {
T s = 0.0;
for (auto x : xs) {
s += x;
}
return s;
}
int main(){
vector<int> ns = {1,2,3};
vector<double> xs = {4.5, 6.4, 7.8};
// sum works with integers
cout << "Sum of ints: " << sum(ns) << endl;
// sum works with doubles
cout << "Sum of doubles: " << sum(xs) << endl;
// iota from the numeric library behaves like range
list<int> ys(10);
std::iota(ys.begin(), ys.end(), -3);
// accumulate from the numeric library behavses like reduce with default operation of addition
cout << "Sum from iota: " << std::accumulate(ys.begin(), ys.end(), 6.0) << endl;
// Note that the initial value determines the template type
cout << "Sum of doubles using accumulate: " << std::accumulate(xs.begin(), xs.end(), 0.0) << endl;
cout << "Surpise: " << std::accumulate(xs.begin(), xs.end(), 0) << endl;
// The binary operation can be user-defined
auto op = std::multiplies<int>();
cout << "Product of ints: " << std::accumulate(ns.begin(), ns.end(), 1, op) << endl;
}
In [53]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = templates
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [54]:
%%bash
make clean
make
./templates
In [55]:
%cd ..
In [56]:
%mkdir func_ptrs
%cd func_ptrs
In [57]:
%%file func_ptrs.cpp
#include <numeric>
#include <functional>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
double sum(vector<double> xs) {
return std::accumulate(xs.begin(), xs.end(), 0.0);
}
double prod(vector<double> xs) {
return std::accumulate(xs.begin(), xs.end(), 1.0, std::multiplies<double>());
}
// funciton pointers in C++ are easy
using func = std::function<double(double)>;
// now you can pass in a funciton as an argument
double mystery(double x, func f) {
return f(x);
}
double foo(double x) {
return 2*x + 1;
}
double bar(double x) {
return 42*x;
}
int main() {
vector<double> xs = {1.2, 2.3};
cout << sum(xs) << endl;
cout << prod(xs) << endl;
// auto can crate iterables of functions!
auto funcs = {sum, prod};
for (auto f: funcs) {
cout << f(xs) << endl;
}
int x = 2;
cout << mystery(x, foo) << endl;
cout << mystery(x, bar) << endl;
cout << mystery(x, [](double x) {return x*x;}) << endl;
}
In [58]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
# File names
EXEC = func_ptrs
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [59]:
%%bash
make clean
make
./func_ptrs
In [60]:
%cd ..
In [61]:
%mkdir numeric
%cd numeric
In [62]:
%%file numeric.cpp
#include <iostream>
#include <fstream>
#include <armadillo>
using std::cout;
using std::ofstream;
int main()
{
using namespace arma;
vec u = linspace<vec>(0,1,5);
vec v = ones<vec>(5);
mat A = randu<mat>(4,5); // uniform random deviates
mat B = randn<mat>(4,5); // normal random deviates
cout << "\nVecotrs in Armadillo\n";
cout << u << endl;
cout << v << endl;
cout << u.t() * v << endl;
cout << "\nRandom matrices in Armadillo\n";
cout << A << endl;
cout << B << endl;
cout << A * B.t() << endl;
cout << A * v << endl;
cout << "\nQR in Armadillo\n";
mat Q, R;
qr(Q, R, A.t() * A);
cout << Q << endl;
cout << R << endl;
}
In [63]:
%%file Makefile
# Declaration of variables
CC = g++
CC_FLAGS = -Wall -std=c++11
LD_FLAGS = -larmadillo # Add library for linking
# File names
EXEC = numeric
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
# Main target
$(EXEC): $(OBJECTS)
$(CC) $(LD_FLAGS) $(OBJECTS) -o $(EXEC)
# To obtain object files
%.o: %.cpp
$(CC) -c $(CC_FLAGS) $< -o $@
# To remove generated files
clean:
rm -f $(EXEC) $(OBJECTS)
In [64]:
%%bash
make clean
make
./numeric
In [65]:
%cd ..