Category Archives: Maths

Books on Numerical Analysis

Due my writing an exam on numerical analysis I had the pleasure to look through lots and lots of books on numerical analysis, and here is a list of my favorite ones so far:

  • Afternotes on Numerical AnalysisAfternotes Goes to Graduate School by G. W. Stewart
    Both books are very readable and introduce many of the concepts in a light way that builds an intuitive understanding for them.
    It's possible to read the books cover to cover in a few days and you can learn a lot very quickly.
    They are also quite amusing:

    "In the nineteenth century the Norwegian mathematician Niels Abel showed that no polynomial of degree five could be solved by a finite number of additions, multiplications, divisions, and root extractions. If we had a finite algorithm for finding eigenvalues of general matrices, we could apply it to companion matrices and make a fool out of Abel. Abel was no fool."

  • Numerical Linear Algebra by Trefethen
    another very good book which I haven't used much personally, though
  • Numerical Methods by Kelley
    contains an okay introduction to CG and GMRES.
  • Numerische Mathematik INumerische Mathematik II
    are very good books, too. The first volume contains a good introduction to everything but ODEs and PDEs and the second volume consists of a very thorough overview of numerical algorithms for differential equations, including a nice introduction to the general theory of their solvability etc
  • Numerical Analysis by Burden and Faires
    a very nice book that contains lots of visualizations and covers many topics
More to follow soon :-)
Cheers,
 Andreas

Light Propagation Volumes

I've finally finished my lab course last week - thanks to my supervisor Matthäus G. Chajdas - you can read his blog here -, it wasn't your usual lab course with work sheets and boring homework, instead I've been allowed to implement a nice paper about a Global Illumination approximation algorithm called (Cascaded) Light Propagation Volumes. It's been developed by Crytek and you can find more information (including some presentations and videos) on their server. (Note: this is an implementation of the I3D paper, not the earlier SIGGRAPH one.)

Sponza scene (direct + indirect lighting w/ occlusion)

Sponza scene (only indirect lighting w/ occlusion)

Sponza scene (ony direct lighting)

Sponza scene (boosted indirect lighting w/ occlusion)

Sponza scene (boosted indirect lighting w/o occlusion)

The algorithm approximates global illumination by rendering the light into a reflective shadow map, injecting it into a volume (using a spherical harmonics representation) and propagates the light flux in this volume (hence the name of the algorithm) and taking into account occlusion as possible extension.

The whole algorithm is physically motivated but corners everywhere, of course, to be more efficient. The paper also contains a few errors and doesn't explain everything needed to implement it in great detail (like eg the solid angles of the side faces), so I've written two documents detailing the mistakes I've found and the additional calculations I've performed.

You can find the mistakes here (including suggested corrections) and the full annotations document here.

Finally I've also uploaded the whole prototype (including my code licensed under the FreeBSD license and the media files) here - it's 68 MB big (and it's been compressed with 7zip with a compression mode that might not be supported by WinZIP. The Sponza model is from Crytek, too. You can download the original model and textures here.
The project uses DirectX 10.1 and by default it won't run in DirectX 10, because it uses a texture format that is deprecated in D3D10 but supported again 10.1 (BGRA). See the comment by FatGarfield for the location that needs to be changed for it work in DX10, too. (However red and blue will be swapped then.)

I haven't implemented cascaded LPVs and I also use only one light/RSM and only inject its depth into the occlusion volume, but the results already look very nice in my opinion.

Stay tuned for more :-)
Cheers,
Andreas

Rotation of Low Order Spherical Harmonics

I'm currently working at university on implementing Light Propagation Volumes. The paper makes extensive use of spherical harmonics while the implementation uses the first two bands.

Below is a visualization of the first 4 bands of the SH basis functions (created using Mayavi):

sh0to3

The first 4 bands of the spherical harmonic basis functions

As you can see the first two bands are 4 functions, so 4 coefficients to store which conveniently fits into one RGBA texture.

One of the main transformations that is performed in the LPV paper is the rotation of the spherical harmonics representation of a clamped cosine lobe (that represents surface lighting) onto a normal vector direction.  It took me a while to figure out, but actually it's quite easy, which is why I write about it :-)

The analytical presentation of the first four base functions is simple:

S_0 \left( x, y, z \right ) = \frac{1}{2 \sqrt{\pi}}
S_1 \left( x, y, z \right ) = - \frac{\sqrt{3}}{2 \sqrt{\pi}} y
S_2 \left( x, y, z \right ) = \frac{\sqrt{3}}{2 \sqrt{\pi}} z
S_3 \left( x, y, z \right ) = - \frac{\sqrt{3}}{2 \sqrt{\pi}} x

To evaluate lighting with SH for some direction v, you first determine the coefficients/weights of the SH basis functions and then sum them up.

 L = \sum_i s_i \, S_i \left( v \right )

Let's assume we know the coefficients  s^z_0, s^z_1, ... of the clamped cosine lobe around the z axis, then we can determine the lighting in direction v for the cosine lobe around the normal n by transforming it into the space where the normal coincides with the z axis (ie rotate n onto the z axis):

 L = \sum_i s^z_i \, S_i \left( R_{n \to z} \, v \right )

where  R_{n \to z} is a rotation matrix that rotates n onto z.

The idea is to expand  S_i \left( R_{n \to z} \, v \right ) and rewrite it in terms of  S_i \left ( v \right ) .

Before doing this, let's first take a look at the coefficients of the clamped cosine lobe:

\begin{align*} 
s^z_0 &=\frac{ \sqrt{ \pi } }{ 2 }\\ 
s^z_1 &= 0\\ 
s^z_2 &= \sqrt\frac{ \pi }{3}\\ 
s^z_3 &= 0\\ 
\end{align*}

The y and x direction are 0 because the cosine lobe is centered isotropic around the z axis:

So let's look at the expanded version of this formula if  r_1^T ,  r_2^T ,  r_3^T are the row vectors of the matrix,
 v=\bigl(\begin{smallmatrix} 
x\\ 
y\\ 
z 
\end{smallmatrix}\bigr) and  R_{n \to z}=\left(\begin{smallmatrix} 
r_1^T\\ 
r_2^T\\ 
r_3^T 
\end{smallmatrix}\right ) , then:

 L = \sum_i s^z_i \, S_i \left( R_{n \to z} \, v \right ) = \sum_i s^z_i \, S_i \left( \left(\begin{smallmatrix} 
r_1^T \, v\\ 
r_2^T \, v\\ 
r_3^T \, v\end{smallmatrix}\right ) \right )
\begin{align*} L &= s^z_0 \, c_0\\ 
&+ s^z_1 \, (-c_1) \, r_2^T \, v \\ 
&+ s^z_2 \, c_1 \, r_3^T \, v\\ 
&+ s^z_3 \, (-c_1) \, r_1^T \, v 
\end{align*}

Since  s^z_1 = 0 and  s^z_3 = 0 :

 L = s^z_0 \, c_0 + s^z_2 \, c_1 \, r_3^T \, v = s^z_0 \, c_0 + s^z_2 \, c_1 \, r_{31} \, x + s^z_2 \, c_1 \, r_{32} \, y + s^z_2 \, c_1 \, r_{33} \, z

  L = s^z_0 \, S_0 \left ( v \right ) - s^z_2 \, r_{32} \, S_1 \left ( v \right )+ s^z_2 \, r_{33} \, S_2 \left ( v \right ) - s^z_2 \, r_{31} \, S_3 \left ( v \right )

Now the question is: what is the third row of  R_{n \to z} ? If we look at the inverse matrix instead:  R_{z \to n} , we can immediately see that its third column has to be n, because  R_{z \to n} \, \bigl(\begin{smallmatrix} 
0\\ 
0\\ 
1 
\end{smallmatrix}\bigr) = n by construction. Since rotations are orthogonal matrices, the inverse is the same as the transposed, so we can deduce that the third row of  R_{n \to z} is the same as the third column of  R_{z \to n} ,  that is: n. Thus with  n = \bigl(\begin{smallmatrix} 
n_x\\ 
n_y\\ 
n_z 
\end{smallmatrix}\bigr) we get:

  L = s^z_0 \, S_0 \left ( v \right ) - s^z_2 \, n_y \, S_1 \left (  v \right )+ s^z_2 \, n_z \, S_2 \left ( v \right ) - s^z_2 \, n_x  \, S_3 \left ( v \right )

So the SH coefficients of the clamped cosine lobe along n are:

 
s^n_0 = s^z_0 = \frac{ \sqrt{ \pi } }{ 2 } \\ 
s^n_1 = - s^z_2 \, n_y =  -\sqrt{ \frac{ \pi }{3} } \, n_y \\ 
s^n_2 = s^z_2 \, n_z = \sqrt{\frac{ \pi }{3} } \, n_z \\ 
s^n_1 = - s^z_2 \, n_x = - \sqrt{\frac{ \pi }{3}} \, n_x

This is it :-)

Cheers,
Andreas

PS: a few screenshots from the LPV project:

GPUPropCopy 0616
noLPV
LPV32P128C

noLPV_2LPV32P128C_2

Rigid Body Motion

Last week I had to give a presentation about Rigid Body Motion (ie the basics of rigid body physics and some general mechanics).

Here are two versions of my presentation (one with less text and one with more):

Rigid Body Motion Presentation as PPTX; Rigid Body Motion Presentation as PDF
Rigid Body Motion Full Version as PPTM; Rigid Body Motion Full Version as PDF

If you are truly interested in learning about rigid body physics, here are some books/links:

Cheers,
Andreas

A quick note on quantifiers: For almost all and there exist infinitely many

It's been a while since my last post and now it's time for a mathematical post:

I'm currently preparing for a math exam (calculus) and I'm thinking it would be nice if there was a way to avoid much of the "for every \epsilon > 0 there is an N \in \mathbb{N}, so that for all n \ge N some property ... holds" stuff you find in textbooks. Some textbooks actually shorten it to "for every \epsilon > 0 some property ... holds for almost all n".
However, I haven't found a quantifier to express this anywhere yet, so I'm proposing to introduce a new one:

\tilde{\forall} x \in M: P(x) should mean "for almost all x \in M P(x) holds", which suggests that there are only finitely many elements for which it does not hold.
One can formalize this as:
\tilde{\forall} x \in M: P(x) \equiv \exists n \in \mathbb{N}: \left | \left \{ x \in M \mid \neg P(x) \right \} \right | \le n

Of course, a new existential quantifier is required then, too (for negation):
\tilde{\exists} x \in M: P(x) stands for "there exist infinitely many x \in M, for which P(x) holds".
And this can be formalized as:
\tilde{\exists} x \in M: P(x) \equiv \forall n \in \mathbb{N}: \left | \left \{ x \in M \mid P(x) \right \} \right | > n

It's easy to see that \neg \left ( \tilde{\forall} x \in M: P(x) \right ) \equiv \tilde{\exists} x \in M: \neg P(x)

Thus one can use the two quantifiers just as one would use \forall and \exists usually. Note however than \forall and \tilde{\forall} don't interchange and neither do \exists and \tilde{\exists}.

One last note: it might be worth using a different notation, for example: \exists ^ \infty might be easier to understand than  \tilde{\exists} , and \forall ^ \approx might be better than \tilde{\forall}.

I'll try to formalize these quantifiers some more when I find some spare time.

Stay tuned for coding related updates soon :-)
Cheers,
Andreas

Random Things

It's been a while since the last update. Here's a small update on what I'm thinking about various stuff.

Prototype

Gameplay is okay I guess. Nothing I would spent too much time on though. It looks a lot worse than GTA 4 though (same as Crackdown).

Resident Evil

Seems to be lots of fun and some nice graphics, too.

Red Faction: Guerrilla

Some weird texture filtering issues and the presentation doesn't knock off my chair (terrain popping and other ugliness), but multiplayer is loads of fun with friends. Physics isn't completely stable though. I played the game for one hour in MP with friends and there were quite a few cases where geometry dropped through the floor with enough pressure from above (after destroying a building).

Brüno

A bad and quite stupid movie (Ali G in Da House is probably the only movie I kinda like that stars Cohen). If you haven't watched it already, don't >_<

Shadow Complex

I bought shadow complex a few weeks ago and I have to say that it is an awesome game. I read somewhere that the developer used the Metroid series as inspiration and it shows. It's really fun to play and quite addictive. The graphics are pretty awesome (it is using the Unreal 3 engine) and the whole presentation is pretty polished. It certainly is worth its 1200 gamerpoints

Summing Formulas

A month ago I was doing some exercises in an analysis book (Königsberger) and found a nice/interesting problem:

Prove that if we denote of the sum of the numbers 1 to n to the p-th power by  S_n^p = 1^p + 2^p + ... + n^p , then the following equation by Pascal holds:  (p+1) S_n^p + \binom{ p + 1 }{ 2 } S_n^{p-1} + \binom{ p + 1 }{ 3 } S_n^{p-2} + ... + S_n^0 = (n+1)^{p+1} - 1

It can be used to get recursively/iteratively get formulas for sums of higher powers of numbers.

This is interesting because the proof doesn't need any advanced maths (like eg the Euler-MacLaurin formula, which can be used to show this, too).

You can download the proof and a few examples here.

Cheers

Semi-Conductor Optimization (Uni Project)

I've written my last exam yesterday (except for two oral exams in September), so now I have got some spare time before I start working on my Bachelor Thesis tomorrow and I want to use it to wrap up a few things.

During this term I took part in a course that was both a (research) project/presentation/lecture thing, which was fun but also a lot of work.
I've already written about one mathematical aspect of it in my post about Analysis, Cauchy-Schwarz and Reciprocal Sums.

The project was about optimizing semi-conductor wiring placement. We wrote a small paper about our findings and the work it was based one - you can look download it here.

We also created a self-running presentation that doesn't contain any Maths at all but makes heavy use of Flash animations (which were exported to .gif manually, which was a huge pain in the ass, which I will never do again if possible) to visualize all the concepts and algorithms.

You can download a PowerPoint 2007 (.pptx) version here or one that works with PowerPoint 2003 here.

For the Student's I sat down and wrote a small Flash application to show the algorithms at work. It's not obvious how it works, so let me explain the major points:

  • On the right you have a number of panels that you can enlarge by clicking on the small button in the upper right of each panel.
  • The upper three panels show different views of the same dataset. They will all be updated as you run the algorithm step by step.
  • The fourth panel lets you change the number of wires and/or their activity.
  • The last panel shows the electrical field that is created by one active wire in the center. It was created using MatLAB. I've also uploaded the script here.

Abitag

Last but not least I've also uploaded the current version of all my .fla and .as files. You can download it here.

ActionScript is a nice language and you can quickly learn it using the available resources from Adobe.
While ActionScript 2.0 is arguably weird, ActionScript 3.0 is quite logical and it's syntax is straight-forward and consistent, too. You can't say that about the IDE (Flash CS4), which is braindead, but if you're only interested in writing ActionScript code, FlashDevelop is an excellent and free alternative.

This is it for now, maybe I'll play around with Flash some more another time.
Cheers,
Andreas

\epsilon Induction

Induction over natural numbers is a standard tool in Mathematics. But what about doing induction over real numbers´? The structure is different and it's not immediately clear what is meant, so let me clarify it.

Let P(v) be a proposition that we want to show for all  v \in \mathbb{R}^+_0 .

Then we can use the following "induction principle" to prove it:

 \exists \epsilon \in \mathbb{R}^+
 \text{a)} \forall u \in \left [0,\epsilon \right ]: P(u)
 \text{b)} \left( \forall u \in \left  [0, v - \epsilon \right ]: P(u) \right ) \Rightarrow P(v) ´

It's easy to show that this is a correct way to prove it. I'll use induction over natural numbers to prove it :-)

Let  I_k := \left [k \cdot \frac \epsilon 2, (k+1) \cdot \frac \epsilon 2 \right ] .
Then let's show by induction over  k that  \text{(*)} \forall u \in I_k: P(u) :

Base:

From a) it follows that (*) already holds for I_0 and I_1.

Induction Step ( k \ge 2 ):

If (*) holds for  I_0, ..., I_{k-1}, then it obviously holds for all  u \in \left [ 0, k \cdot \frac \epsilon 2 \right ].

Fix  v \in I_k , then  v \le (k+1) \cdot \frac \epsilon 2 \Leftrightarrow v - \epsilon \le (k-2) \cdot \frac \epsilon 2 < k \cdot \frac \epsilon 2 \right . With b) it follows that P(v) holds.

That is,  P(v)  \forall v \in I_k .

#

It should be possible to show that you can generalize this to:

 \exists \epsilon \in \mathbb{R}^+
 \text{a)} \forall u \in \left [0,\epsilon \right ]: P(u)
 \text{b)} \left( \forall u \in \left  [\psi(v), \phi( v ) \right ]: P(u) \right ) \Rightarrow P(v)
with  \phi( v ):  \mathbb{R}^+ \to \mathbb{R}^+_0 ,  \psi( v ): \mathbb{R}^+ \to \mathbb{R}^+_0 and  \psi(v) \le \phi(v) < v .

Analysis, Cauchy-Schwarz and Reciprocal Sums

Konkrete Analysis

First I want to share some solutions for the exercises of a Maths book. The book is called Konkrete Analysis by Folkmar Bornemann and is written in German as are my solutions for some of the exercises. (I think I cover about 70% of all exercises in the book and pretty much every easy one :-) )

I don't claim that my solutions are correct and there are probably quite a few (uncorrected) mistakes, but right now I haven't been able to find any other openly available solutions. Although the book claims to be easily readable without attending the lectures of Professor Bornemann, I doubt that it is possible to do so successfully without being able to do the exercises and while doing so being able to get help or look at possible solutions to find new ideas.

I did all the exercises as way to prepare myself for the exam (it was very successful alas probably not in the most time efficient way), so it also contains solutions for old exams (but those are appended at the end and probably not interesting at all).

You can download the PDF konkrete-analysis-solutions here.

Cauchy-Schwarz and a Problem

The book is quite useful though as was the lecture "Analysis for Computer Scientists" which was based on the book (or vice-versa) and it is probably the single one lecture that has taught most since I started attending Computer Science at university.

One of the many topics (we covered a lot which was one of the nice things) was inequalities and the useful things you can do with them. Especially the Cauchy-Schwarz inequality turns out to be quite useful while pretty basic:

 <a,b> \leq ||a|| \cdot ||b|| with  <a,b> = || a || \cdot || b || when  \exists \lambda: a = \lambda b

Interestingly enough this can already yield results that are quite difficult to obtain otherwise. For example we can easily prove lower or upper bounds that is the existence of a maximum or minimum by applying it.

First we obtain a lower or upper bound (for the right or the left side respectively) and then we can use the equality case to prove the existence of the a minium or maximum.

For example, let's take a look at this problem:

We have  x \in \mathbb{R}^n, 0 < x, \sum_{i=1}^n {x_i} = 1 and want to minimize  \sum_{i=1}^n { \frac 1 x } .
We simply set  a_i := \sqrt{x_i}, b_i := \sqrt{ \frac 1 x_i } .

Then we get:

 \sum_{i=1}^n { a_i b_i } = \sum_{i=1}^n { \sqrt{ x_i } \frac 1 {\sqrt{ x_i }} } = \sum_{i=1}^n 1 = n
 \leq \sqrt{ \sum_{i=1}^n \sqrt{x_i}^2 } \sqrt{ \sum_{i=1}^n \frac 1 {\sqrt{x_i}^2} } = \sqrt { \sum_{i=1}^n x_i } \sqrt{\sum_{i=1}^n \frac 1 x_i } = 1 \cdot \sqrt{\sum_{i=1}^n \frac 1 x_i }

That is:  \sum_{i=1}^n \frac 1 x_i \geq n^2 .

Now we know an lower bound for the reciprocal sum.
To determine the minimal x vector, we remember to start with:

 a = \lamda b \Leftrightarrow \sqrt x_i = \lambda \frac 1 \x_i \Leftrightarrow x_i = \lambda

We use that with the constraint:  1 = \sum_{i=1}^n x_i = \sum_{i=1}^n \lambda = n \cdot \lambda ,
resulting in:  x_i = \lambda = \frac 1 n .

Generalization

The nice thing is you can go and generalize this finding to more interesting minimization problems as there also exist more advanced versions of the Cauchy-Schwarz inequality´ adding degrees of freedom to play around with´.

Thus it is possible to solve the following class of problems using a slightly more advanced Cauchy-Schwarz inequality:

 x,q \in \mathbb{R}^n; 0 < x, q
0 < \alpha, \gamma < \infty; \beta > 0
\large\sum_{i=1}^n {x_i}^\gamma = \beta
\large min \sum_{i=1}^n \frac {q_i} {x_i^\alpha}

by utilizing

\large \sum_{i=1}^n { a_i b_i } \le \sqrt[p]{ \sum_{i=1}^n a_i^p} \sqrt[p']{ \sum_{i=1}^n b_i^p'} with  \frac 1 p + \frac 1 {p'} = 1

Equality requires the same linear dependence condition as the normal Cauchy-Schwarz inequation.

I've only deduced the solution for  \gamma = 1 but it shouldn't be difficult to adapt it once you get the idea how it works (it's fairly straight forward).

You can find the whole deduction in the PDF "Minimum of a Generalized Reciprocal Sum".

For completeness' sake here are the results for an optimal solution  x^{*} :

\large x_i^{*} = \beta \cdot \frac { \sqrt[1 + \alpha]{q_i} }{\sum_{k=1}^n \sqrt[1 + \alpha]{q_k}}

\large \frac { \left (\sum_{i=1}^n \sqrt[1 + \alpha]{q_i} \right )^{1 + \alpha}} {\beta^\alpha} = \sum_{i=1}^n \frac {q_i} {x_i^{*}^\alpha}

Epilog

Coming up with the solution was actually a lot of fun and quite interesting because I didn't know it was possible to deduce it this way nor did I know how to do it before. If you read the PDF, I think it's quite nice how you can play around with the formulas and come up with new things from the Cauchy-Schwarz inequality.
If you are only interested in  x ^ {*} , you can shortcut everything by immediately skipping to the equality case once  p and  p' have been determined and solve for that.
I mainly wrote it all out because I wanted to make sure, that it actually was correct.

If it wasn't for my Analysis lecture last term, I probably wouldn't have found this.

Cheers,
Andreas

The Reverse Pigeonhole Principle

Everybody (I hope) knows about the pigeonhole principle.

In short it states that:

If  f: N \to M and  n:= \left | N \right | \leq \left | M \right | =: m , then there exists at least one  m_0 with  \left | f^{-1}\left ( m_0 \right ) \right | \geq \left \lceil \frac {n} {m} \right \rceil  .

For example, if we have n + 1 pigeons in only n pigeonholes, there is (at least) one pigeonhole that has at least two pigeons inside (thus the name).

One thing I haven't seen mentioned anywhere yet (at least not with this name) is the reverse pigeonhole principle:

If  f: N \to M and  n  := \left | N \right | \leq \left | M \right |  =: m , then there exists at least one  m_0 \in M with  \left | f^{-1}\left ( m_0 \right ) \right | \leq \left \lfloor \frac {n} {m} \right \rfloor .

Again an example: if we have n + 1 pigeons (it's valid up to 2n - 1) and n pigeonholes, then there exists (at least one) pigeonhole with at most one pigeon in it.

The proof for the reverse principle is simple:

Assume that for all  m_0 \in M :  \left | f^{-1}\left ( m_0 \right ) \right | > \left \lfloor \frac {n} {m} \right \rfloor, that is each such set contains at least  \left \lfloor \frac {n} {m} \right \rfloor + 1 elements, then from   \frac n m - 1 < \left \lfloor \frac {n} {m} \right \rfloor it follows that:

 n = \sum_{m_0 \in M} { \left | f^{-1} \left ( m_0 \right ) \right | \geq m \left ( \left \lfloor \frac {n} {m} \right \rfloor + 1 \right ) > m \cdot \frac n m = n  } .

This clearly is a contradiction, so the original assumption must have been wrong, which proves the reverse pigeonhole principle.

I haven't seen this written down anywhere before and it's useful to keep it in my mind on its own because it shortens some arguments, so I hope this might help someone :-)

On other news my LaTeX script is severly broken and I don't know why :-|
For some reason it sometimes merges two LaTeX pieces into one because it forgets to echo the last " after the title (which is impossible, of course..).
Cheers,
Andreas