Thomas Russell: Programmer & Designer
Have a browse of my thoughts!

Recent Posts - What I've been saying

Adding a Generic p-norm in Modern C++

In this article, I will show how to use modern C++ features such as fold expressions, variadic templates and type-traits to make a safe, stable and generic $p$-norm function. Read more »

Sherlock and Probability (in general)

I’ve recently been exploring HackerRank, a wonderful (and somewhat addictive) site with interactive programming problems and I came across one of the problems in the Probability section and thought that it would be interesting to share a slightly more probabilistic approach here.

The problem (loosely stated) is that given a binary string $S$, and two randomly selected indices $i,j\in \{1,\dots,N\}$, what is the probability that: $S_{i}=S_{j}=1$ and $|i-j|\leq K$, where $K \in \{1,\dots,N\}$. Read more »

Writing a Tensor class in Modern C++

As promised in my article about the mathematics of solving the multidimensional Poisson equation, I am beginning writing about the process of writing a parallelized solution to the Poisson Equation by successive over-relaxation. This article will be about writing a tensor class in modern C++, as we will need a multidimensional grid for our Poisson solver.

Mathematically speaking, a Tensor is a multidimensional object over some appropriate field. In this case we will be considering simple fields, real numbers (to floating-point precision), integers (modulo some appropriate power of 2) and complex numbers. Tensors are mathematically-rich objects, with their own notation and algebras embedded in Ricci calculus, but here we will only be writing a basic Tensor class equipped with a few useful operators, however, it is an easily extendable object, so can easily be modified for most general purposes. Read more »

Multidimensional Poisson Equation Numerical Methods

Further to my posts on solving the Laplace Equation and Poisson Equation in $R \subset \mathbb{R}^{2}$ this post is describing mathematical extensions to these methods allowing us to consider numerical solvers for regions $R \subset \mathbb{R}^{n}$, for arbitrary $n \in \mathbb{N}$.

We begin by once again considering the multidimensional Poisson equation in $\mathbb{R}^{n}$, where $\psi,f : \mathbb{R}^{n} \to \mathbb{R}$:

$$\nabla^{2} \psi = \sum_{k=1}^{n}\frac{\partial^{2} \psi}{\partial x_{k}^{2}}=f(\vec{r})$$

Recalling the central difference method from before: $f^{\prime \prime}(x) \approx \frac{f(x+h) – 2f(x) + f(x-h)}{h^{2}}$, we can write:

$$\nabla^{2} \psi \approx \sum_{k=1}^{n}\frac{\psi(\dots,x_{k}+\delta x_{k},\dots) – 2\psi(\dots,x_{k},\dots) + \psi(\dots,x_{k}-\delta x_{k},\dots)}{(\delta x_{k})^{2}}=f(x_{1},\dots,x_{n})$$

Or in a more readable vector notation, where $\vec{r} = \left(\begin{smallmatrix}x_{1} & \dots & x_{n}\end{smallmatrix}\right)^{T}$ and $\delta \vec{x}_{k} = \delta x_{k} \boldsymbol{\hat{e}}_{k}$, and $\boldsymbol{\hat{e}}_{k}$ is the $k$th unit vector: Read more »

Exporting a Matrix from C++ to Mathematica

In my previous two posts: Solution to the Laplace Equation in C++ using successive over-relaxation and Solution to the Poisson Equation in C++ using Successive Over-relaxation I generated a matrix in C++ and computed the elements of it using a numerical method. I then used Mathematica to visualize the data and compare it to the analytic solution to demonstrate the validity of the methods.

In this blog post I will demonstrate how to export a matrix from C++ (which I will be representing simplistically using a std::vector<std::vector<T>>) to Mathematica using the MatrixMarket (.mtx) format.

The reason I have chosen this particular format for exporting a matrix is because it is space efficient at representing sparse matrices and a NIST standard which is natively handled by many systems, including Mathematica.

N.B: The .cpp file is available in ZIP format at the bottom of this post. Read more »

Designed and Produced by Thomas Russell © 2014-2017

Log-in | Register