Chess

Norwegian world champion of chess, Magnus Carlsen is playing fast chess/blitz chess in Russia these days.

Making a chess board

Let us start out by defining a chess board using either of 4 methods.

Method #1: Toeplitz

Using the toeplitz() function to fill the diagonals with a binary fluctuating vector:

N = 8;
cb = toeplitz(rem((1:N)-1,2));

Method#2: frequency domain:

A single frequency component is needed (at Nyquist), aside from DC offset:

x = zeros(N);
x([1 (N^2+N)/2+1]) = (N^2/2)*[1 -1];
cb = ifft2(x)

Method#3: outer product:

Generate rows or columns of +1/-1 that themselves cycle between inverted or non-inverted:

osc = cos(pi*(0:N-1));
cb = (-osc'*osc+1)/2

Method#4: false 1-d periodicity

Matlab is column-major, meaning that a 2-d array is scanned _vertically_ into continuous memory, like Fortran (but unlike C/C++). Matlab allows us to treat this array as a 1-dimensional vector whenever that makes life easier.

Each column is oscillating between logical 0/1 (or -1/+1 if you like), but the value of A1 is the same as B8, thus we cannot directly serialize/wrap-around the 2-d array into a 1-d vector and keep this simple oscillation. If we extend the 2-d array to have 9 rows instead of 8, we can do our thing in 1-d easily, then trim off the last row afterwards:

cb = zeros(N+1,N);

cb(2:2:end) = 1;

cb = cb(1:N,:);

As is often the case, more energy is needed to make a pretty plot than to obtain the data:

figure
imagesc(cb)
colormap gray
set(gca, 'Ydir', 'normal')
xticklabels(char((0:7)'+'A'))

Rice grains on a chess board

I assume that people are familiar with the legend of a wise man asking the emperor to put one grain of rice on the first field of a chessboard, 2 on the second, then 4 etc. It is evident that the number of rice grains is the sum of 2^0, 2^1, ... 2^63. Looking at any iteration n starting at 0, we see that the "current" contribution is 2^n, while the accumulated contribution of all prior iterations is 2^n-1. Thus, the sum for any iteration n is 2^(n+1)-1. For chess having 8 fields on each side, n is 8x8-1=63:

number_of_grains = (2^(N^2)-1);
number_of_grains =

   1.8447e+19

That is a large number. In fact, I have a hard time envisioning what that really means. So let us try playing with it.

Ask google for the weight of a single grain of rice:

S = urlread('https://www.google.com/search?q=how%20much%20does%20a%20grain%20of%20riceweigh');
grain_weight = str2double(regexp(S, '<b>(\d+.\d+) grams</b>', 'tokens', 'once'));
grain_weight = grain_weight/1e3;
%find the total weight in kg
rice_weight = grain_weight*number_of_grains;
rice_weight =

   5.3496e+14

Nah, still an incomprehensible number. What if we compare it to a known object that is considerably heavier than the SI kg?

%Find the dry weight of KNM Helge Ingstad
data = webread('https://no.wikipedia.org/w/api.php?action=parse&page=KNM «Helge Ingstad»&prop=text&format=json&formatversion=2');
pat = 'Deplasement</th><td colspan="2">(\d*) (\d*)';
hiw = regexp(data.parse.text, pat, 'tokens');
hiw = 1e3*(1e3*str2num(hiw{1}{1}) + str2num(hiw{1}{2}));
rice_weight_hi = rice_weight/hiw
rice_weight_hi =

   1.0094e+08

Thus we get that the weight of the rice grain is really close to 100 million Norwegian frigates (sans water). I do get a feeling for that being "really heavy".

Dithering

What better way to depict a chess master than to use a binary "chessboard" dithering? Using Floyd Steinberg error diffusion dithering as built into MATLAB (https://en.wikipedia.org/wiki/Floyd–Steinberg_dithering) working on a crude pseudo-luminance conversion of the original rgb image:

%% Do a simple dithering
im=imread('https://blog.playmagnus.com/content/images/2018/09/6.jpg');
im = double(im(1:400, 200:550,:));
luma = sum(im.*shiftdim([0.25 0.5 0.25], -1), 3);
dithered = dither(uint8(luma));

figure
image(dithered), 
colormap(gray(2))

(image source: https://blog.playmagnus.com/ten-things-you-probably-didnt-know-about-magnus-carlsen/)

To view or add a comment, sign in

More articles by Knut Inge Hvidsten

  • Hello JPEG

    This article will cover the encoding of an image into a valid JPEG file using MATLAB. Compact code and comprehension is…

    1 Comment
  • Developing iPhone raw images using MATLAB

    This guide will describe how to capture and process raw (undeveloped) images from an Apple iPhone. Typically, the jpeg…

    3 Comments
  • Visual intuition into the Z-transform

    Motivation The goal of this post is to get an intuitive, visual understanding of the Z-transform used for e.g.

    1 Comment
  • Angles, atan2 and taxicabs

    Background Calculating the angle, theta [radians] of some 2-dimensional vector <x,y> against the x-axis would seem like…

    2 Comments
  • DFT and FFT

    As a dsp-guy, the Fast Fourier Transform (FFT) and its lineage has been both a part of my education and day-to-day use…

Others also viewed

Explore content categories