## Differential geometry in Rust

During the last few weeks I've been working on a library that would let the user do some differential-geometric calculations in Rust. By differential geometry I mean mostly the tensor calculus in curved spaces or space-times. I've already created something like that in C++, but I wanted to try and use some of the Rust features to create an improved version.

## What could Rust do better?

The most convenient representation of tensors for doing calculations is in the form of arrays of numbers. The problem is that representing a tensor numerically requires choosing a coordinate system. Various operations, like for example addition of two tensors, only make sens when the tensors involved are expressed in the same coordinate system. The only possibility of enforcing this rule in C++ was to encode the coordinate system as a property of the tensor object and checking for compatibility in the operator code. This way any errors will be detected at runtime.

Ok, so the errors were detectable, so what could be done better? Well, for examples the tensors expressed in different coordinate systems could not only have a different value of some property, but be objects of different types. This way the error can be detected at compile time, before the program is even translated into an executable form. It wouldn't be very practical in C++, but the Rust type system allows to do it quite interestingly.
EDIT: It has been brought to my attention that C++'s templates also allow for this kind of thing. Nevertheless, doing it in Rust was a fun experiment :)

## Generic arrays in Rust

Recently, I decided to try and "translate" the black hole simulator into Rust as an exercise. Initially I wanted to create a library implementing differential geometry, like tensor calculus, metric etc. I quickly encountered a problem.

## Rust and arrays

Tensors are objects with some constant number of coordinates, depending on the dimension of the space they function in. For example, in an n-dimensional space, vectors have $n$ coordinates, rank-2 tensors have $n^2$ etc. Arrays are perfect for representing such objects.
Rust handles arrays without problems. An array of N elements of type T is written in Rust as [T; N]. Everything would be fine, except for one tiny detail - I would like my code not to depend on the dimension of the space.

## The problem

It is possible to define so-called generic types in Rust. They are datatypes that have internal details parametrized by some other type. For example, we could easily create a generic 3-dimensional vector:

What if we wanted to create an n-dimensional vector, though?

Nope. You can't express an integer type parameter in Rust.
It looked hopeless, but I accidentally stumbled upon some code that suggested the existence of a solution.
(more…)

## Rust version has caught up to the C one

It took a while, but the Rust version of the code generating the positions of galaxies has finally reached the level of functionality of the C version. Meanwhile, I have gathered quite a bit of interesting experience, which I'm now going to share.

### Rust vs other languages

Programming in Rust is nothing like programming in any other language I've had contact with (which means mainly the C family and Python). One remotely similar experience was experimenting with Haskell, but even that generally due to incompatibility between my intuition and the language (although Rust has many functional features, but as will be mentioned later, one shouldn't overuse them...).

## Progress of the Universe project

I made some progress in rewriting the Universe project in Rust during the last few days.

Before I started with the project itself, I had to make some preparations. The order of magnitude of the numbers appearing in the program caused the need for the GMP and MPFR libraries, allowing for calculations on arbitrarily big numbers. I also used the xxhash algorithm. The problem? All 3 pieces of code were written in C.

It's not actually that big of a problem, since one of the advantages of Rust is the ease of using it with C libraries. The only thing to do was to find or create modules (or, in Rust terms - crates) which would make it more straightforward, so I started searching.

I focused on GMP and MPFR first. There were 2 crates for GMP and one for MPFR, created by the author of one of the GMP crates. After having a look at them I decided that the GMP module without corresponding MPFR one is more straightforward to use, which left me with the task of writing my own crate for MPFR. Moreover, the GMP module was old and didn't even compile with the latest version of the language.

I started working. Fixing the GMP crate turned out to be tedious, but not very hard. Writing the MPFR module was similar, but needed a bit more work (like writing many similar functions that only served to call their C counterparts...). Both modules are available on GitHub (click, click). I created a pull request for the author of the GMP crate, but he turned out not to be interested in maintaining the project any longer.

Then it was time for xxhash. Here, like with GMP, a crate existed, but it didn't compile. A few hours of work later everything was fixed and the library worked.

So, now I have working dependencies for the project. I also started to rewrite the code of the project itself, but for now only a small part of it is done. More news about the project progress - (hopefully) soon.

## Thoughts about light bending

I had a sudden moment of clarity recently when thinking about how to remove a graphical artifact from the Black Hole Simulator.

### The problem The current simulator has one ugly aspect of the rendered image. Exactly 90 degrees from the direction to the black hole a graphical artifact appears - a strip of smudged, incorrectly calculated pixels (see the picture to the right). The reason is hidden deeply in the rendering mechanism.

In short, it looks like this - you can't efficiently calculate the color of every pixel by raytracing. Taking advantage of the symmetry of the Schwarzschild black hole, I created a table of angles of light deflection. Thanks to the symmetry, I can describe each ray of light by only one parameter - simply put, the minimal distance from the black hole along its path (actually, the impact parameter). To calculate the deflection, I need one more thing, and that is the distance from the black hole at which I send/receive it - the greater part of the whole path the ray has to travel, the greater the deflection.

Such a table was being sent to the graphics card. Then, during rendering, a direction of the ray was calculated for each pixel, and this was converted into the impact parameter. The distance was known independently. Appropriate deflection was being read from the table and this was used to calculate the color of the pixel.

In theory everything is fine, but one problem appeared - the light rays sent in directions close to 90 degrees from the black hole have very similar impact parameters. This gives nearly identical deflection angles, which makes many pixels the same color. An ugly strip appears.

## A new article and a program

I finally translated some new things into English.

One of them is the first part of the series about relativity - this time I described the concept of a metric and its uses. You can read it here.

The second one is a small program, letting you distort images as if they were affected by a black hole. The full description and a download link are here.

Enjoy :)

## Part 3 - the metric

We already mentioned the notion of the magnitude of a vector, but we said nothing about what it actually is. On a plane it's easy - when we move by $v_x$ in the $x$ axis and by $v_y$ in the $y$ axis, the distance between the starting and the ending point is $\sqrt{v_x^2 + v_y^2}$ (which can be seen by drawing a right triangle and using the Pythagorean theorem - see the picture). It doesn't have to be always like that, though, and here is where the metric comes into play.

The metric is a way of generalizing the Pythagorean theorem. The coordinates don't always correspond to distances along perpendicular axes, and it is even not always possible to introduce such coordinates (but let's not get ahead of ourselves). We want then to have a way of calculating the distance between points $\Delta x^\mu$ apart, where $x^\mu$ are some unspecified coordinates.

## Part 2 - coordinates, vectors and the summation convention

The basic object in GR is the spacetime. As a mathematical object, formally it is a differential manifold, but for our purposes it is enough to consider it as a set of points called events, which can be described by coordinates. In GR, the spacetime is 4-dimensional, which means that we need 4 coordinates - one temporal and three spatial ones.

The coordinates can be denoted by pretty much anything (like $x$, $y$, $z$, $t$), but since we will refer to all four of them at multiple occasions, it will be convenient to denote them by numbers. It is pretty standard to denote time by 0, and the spatial coordinates by 1, 2 and 3. The coordinate number $\mu$ will be written like this: $x^\mu$ (attention: in this case it is not a power!). $\mu$ here is called an index (here: an upper one). By convention, if we mean one of the 4 coordinates, we use a greek letter as the index; if only the spatial ones are to be considered, we use a letter from the latin alphabet.

## A new category - articles

I started thinking that since I already have a blog, I can use it for one of my favourite activities - passing some knowledge on to others. I decided to do my favourite topic first, that is, relativity and black holes. This field is widely being considered very complex mathematically and there are reasons for that, but a person with a high school education should be able to grasp the general picture in my opinion (assuming, of course, that they are interested in understanding the topic).

Therefore, I started a series of articles aiming to explain this fascinating field to the readers. Am I succeeding? See for yourself :)

## Part 1 - partial derivatives

Let's remember the ordinary derivatives first. We denote a derivative of a function $f(x)$ as $f'(x)$ or $\frac{df}{dx}$. It means, basically, how fast the value of the function changes while we change the argument x. For example, when $f(x) = x^2$, then $\frac{df}{dx} = 2x$.
But what if the function depends on more than one variable? Like if we have a function $f(x,y) = x^2 + y^2$ that assigns to each point of the plane the square of its distance from the origin. How do we even define the derivative of such a function?