Homework Assignments - PerimeterInstitute/Julia-Course-Fall-2015 GitHub Wiki
Homework Assignments
Homework 1: Quaternions
This assignment teaches defining a new type, and associating functions with that type.
Define a parameterized type that implements quaternions. Define respective functions for addition, subtraction, multiplication, and division.
Note:
- You should import the operators
+,-,*, and/fromBase - Provide ways to convert real numbers to quaternions, and to extract the quaternion components as real numbers
- Make the type
immutablefor efficiency and convenience - The quaternion type should be a subtype of
Number - Use Julia's
@testmechanism (see the users' guide) to define tests for each arithmetic operation - Place your code (either notebook or script) into a git repository so that others can look at and comment on it
Homework 2: Secant method
This assignment teaches implementing simple algorithms with loops and conditionals.
Define a function secant that takes as input
- a function
f - a lower bound
xlo - an upper bound
xhi - a resolution
dx
that finds a root of f in the interval [xlo:xhi] with a resolution of dx. That is, the function secant should iteratively reduce the size of the interval until it is smaller than dx, bracketing a root of f.
Note:
- Check initially whether there is actually a root inside the interval, i.e. whether
f(xlo)andf(xhi)have different sign - Use the secant method; alternatively, use the bisection method which is slower, but slightly easier to implement
- If you pass a function as argument to a function, it is customary to not specify a type constraint for this argument, as in
function secant(f, xlo::Real, xhi::Real, dx::Real) - To test performance (in the sense of number of iterations required), use the
infocommand to output a message during each iteration - As a test, find the value of
sqrt(2)to 100 digits. Hint: Solvex^2-2==0, and use theBigFloattype.
Homework 3: Matrix multiplication
This assignment teaches linear algebra operations via explicit for loops.
Write a function that accepts as input two rank-3 tensors A_kil and B_ljk, and that calculate the traces T_ij := A_kil B_ljk.
- Calculate this trace via explicit
forloops - Use
reshapeto calculate the traces also via built-in linear algebra calls, i.e. withoutforloops - Compare code size, clarity, time needed to develop the code, and performance (using
@time, for reasonably large matrices)