Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
*.o
*.o
catch.hpp

# Ignore executables created by the exercises
exercises/2.1-class-types/test
exercises/2.2-complex/test
exercises/3-containers/part1/test
exercises/3-containers/part2/test
exercises/5-templates/part1/sum
exercises/5-templates/part2/test
exercises/8-algorithm/ex
exercises/9-eigen/explicit
exercises/9-eigen/implicit
exercises/9-eigen/sparse
exercises/9-eigen/*.txt
exercises/9-eigen/*.gif
exercises/10-threads/area
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion exercises/2.1-class-types/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXXFLAGS = --std=c++14 -I../include
CXXFLAGS = --std=c++17 -I../include

test : complex.o test.o
$(CXX) $^ -o $@
Expand Down
2 changes: 1 addition & 1 deletion exercises/2.2-complex/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXXFLAGS = --std=c++14 -I../include
CXXFLAGS = --std=c++17 -I../include

test : complex.o test.o
$(CXX) $^ -o $@
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion exercises/5-templates/part1/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXXFLAGS = --std=c++14 -I../../include
CXXFLAGS = --std=c++17 -I../../include

sum : sum.o
$(CXX) $^ -o $@
Expand Down
7 changes: 1 addition & 6 deletions exercises/5-templates/part2/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
CXXFLAGS = --std=c++14 -I../../include
CXXFLAGS = --std=c++17 -I../../include

test : complex.o test.o
$(CXX) $^ -o $@

test.o : catch.hpp

catch.hpp :
wget https://github.com/catchorg/Catch2/releases/download/v2.13.6/catch.hpp

run : test
./test

Expand Down
18 changes: 11 additions & 7 deletions exercises/5-templates/part2/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ class Complex {
friend bool operator==(Complex const& a, Complex const& b);
friend bool operator!=(Complex const& a, Complex const& b);

// Declare binary arithmetic operators
/* What if I want to add a Complex<double> to a Complex<int>?
What would be returned in this case? What should the return type
be to make this flexible?
*/
// Declare binary arithmetic operators - Assume both complex numbers have the same template type.
friend Complex operator+(Complex const& a, Complex const& b);
friend Complex operator-(Complex const& a, Complex const& b);
friend Complex operator*(Complex const& a, Complex const& b);
friend Complex operator/(Complex const& a, Complex const& b);
// Question: how would you declare multiplication and division by a real number?

/*
Extension: What if I want to add a Complex<double> to a Complex<int>?
What would be returned in this case? What should the return type
be to make this flexible?
Hint: Look up std::common_type from the type_traits header.

If you complete this extension, uncomment the final test case in
test.cpp to test your implementation.
*/

// Unary negation
friend Complex operator-(Complex const& a);
Expand Down
13 changes: 13 additions & 0 deletions exercises/5-templates/part2/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ TEST_CASE("Complex numbers can be templated") {
REQUIRE(z1.imag() == 5);
REQUIRE(z1.norm2() == 34);
}

/*
Uncomment the code below to test the binary operators extension exercise

TEST_CASE("Complex number binary operators can use two different template types") {
const Complex<int> z1{3, 5};
const Complex<double> z2{3.4, 5.1};
REQUIRE(z1 + z2 == Complex<double>{6.4, 10.1});
REQUIRE(std::is_same<decltype(z1 + z2), Complex<double>>::value);
REQUIRE(std::is_same<decltype(z1 - z2), Complex<double>>::value);
REQUIRE(std::is_same<decltype(z1 * z2), Complex<double>>::value);
}
*/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
module load PrgEnv-gnu
module load cray-python
module load matplotlib
module load eigen/3.4.0

39 changes: 39 additions & 0 deletions exercises/9-eigen/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import argparse
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def plot(simulation):
n = 20
steps = 200

fig, ax = plt.subplots()
xdata = np.arange(0, n)
ydata = []
ln, = ax.plot([], [], 'ro')

data = np.loadtxt(f"{simulation}_sim.txt")

def init():
ax.set_xlim(0,n)
ax.set_ylim(0,1.1)
return ln,

def update(frame):
ydata = data[frame]
ln.set_data(xdata, ydata)
return ln,

ani = FuncAnimation(fig, update, frames=np.arange(0, 200),
init_func=init, blit=True)

ani.save(f"{simulation}.gif", writer="pillow")
plt.show()


if __name__ == "__main__":
parser = argparse.ArgumentParser(prog='movie.py', description='Create animation from simulation output.')
parser.add_argument('simulation', default='implicit', const='implicit', nargs='?', choices=['implicit', 'explicit', 'sparse'])

args = parser.parse_args()
plot(args.simulation)
File renamed without changes.
17 changes: 9 additions & 8 deletions exercises/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ See each subdirectory for further instructions
* [2.1 Class types](2.1-class-types/)
* [2.2 Complex numbers](2.2-complex/)
* [3 Containers](3-containers/)
* [4 My array](4-my_array)
* [5 Templates](5-templates)
* [6.1 Pointers](6.1-pointer)
* [6.2 Special pointers](6.2-special-pointers)
* [Morton-order matrix class template](morton-order/)
* [Using algorithms](algorithm/)
* [Eigen](eigen/)
* [Simple use of threads](threads/)
* [4 My array](4-my-array/)
* [5 Templates](5-templates/)
* [6.1 Pointers](6.1-pointers/)
* [6.2 Special pointers](6.2-special-pointers/)
* [6.3 Morton-order matrix class template](6.3-morton-order/)
* [7 Inheritance](7-inheritance/)
* [8 Using algorithms](8-algorithm/)
* [9 Eigen](9-eigen/)
* [10 Simple use of threads](10-threads/)
28 changes: 0 additions & 28 deletions exercises/eigen/movie.py

This file was deleted.

Loading