laziness, impatience, and hubris PerlMonks

### Conways Game of Life in PDL

by mxb (Monk)
 on May 16, 2018 at 15:31 UTC ( #1214661=CUFP: print w/replies, xml ) Need Help??

Edit: Apparently this is in the PDL Documentation, as an example. Whoops! Still, it was a good learning exercise :)

Rather than a ported numpy tutorial, this is a self developed implementation of Conways Game of Life written in Perl/PDL. Hopefully people find this interesting as I feel it shows how concise PDL code can be.

The code is fairly straightforward. There is a single function conway() which accepts a single argument of the game arena. This is a two dimensional PDL matrix. Alive cells are represented by a one, dead ones by zero. The conway() function sums the value of each cell along with value of its nine neighbours into a temporary variable \$tmp. It then applies the rules of the game, which are:

• Any live cell with fewer than two live neighbors dies, as if caused by under population.
• Any live cell with two or three live neighbors lives on to the next generation.
• Any live cell with more than three live neighbors dies, as if by overpopulation.
• Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

This is implemented as an elementwise or and an elementwise and.

The main loop of the game is in the body of the code and simply displays the generation and the game arena and awaits input

The game arena is initialised with a 'glider', but feel free to experiment. As PDL wraps around by default, the surface is that of a torus.

Enter a blank line for the next generation, anything else to exit

Enjoy

```#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;

use PDL;

sub conway {
my \$pdl = shift;
die "Not 2D piddle" unless \$pdl->ndims == 2;

# Add up all values:
my \$tmp = \$pdl +                                       # original
\$pdl->transpose->rotate(-1)->transpose +             # north
\$pdl->transpose->rotate(-1)->transpose->rotate(-1) + # northeast
\$pdl->rotate(-1) +                                   # east
\$pdl->transpose->rotate(1)->transpose->rotate(-1) +  # southeast
\$pdl->transpose->rotate(1)->transpose +              # south
\$pdl->transpose->rotate(1)->transpose->rotate(1) +   # southwest
\$pdl->rotate(1) +                                    # west
\$pdl->transpose->rotate(-1)->transpose->rotate(1);   # northwest

# Cell is alive if it's either:
return ( \$tmp == 4 & \$pdl == 1 ) |    # Alive +3 neighbors
\$tmp == 3;    #  Alive +2 neighbors or dead +3 neighbors
}

my \$arena = pdl(byte,
[
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
]
);

my \$gen = 0;
while (1) {
print "Generation: \$gen (press enter for next)\n";
print \$arena;
\$arena = conway(\$arena);
\$gen++;
exit if <STDIN> ne "\n";
}

Replies are listed 'Best First'.
Re: Conways Game of Life in PDL
by Discipulus (Monsignor) on May 17, 2018 at 09:33 UTC
Hello mxb and thanks!

I must admit first I'm looking at PDL and at your work in the same way a, let say, an hystorician can look at a Stephen Hawking movie: very interesting but I do not understand nothing.. ;=)

That said, I've used a strawberry perl perl-5.26.64bit-PDL version (portable 5.26 failed installing PDL at Inline::C step ) and I got (just once before time zero generation):

```Possible precedence problem on bitwise & operator at pdl-gameoflife.pl
+ line 24, <DATA> line 207.
Possible precedence problem on bitwise | operator at pdl-gameoflife.pl
+ line 25, <DATA> line 207.

I poked it a bit ( reading PDL::FAQ too: good think to link to FAQ from errors!)

and the following version, with parens superfetation, gives no warnings:

```return ( (\$tmp == 4) & (\$pdl == 1) ) |    # Alive +3 neighbors
( \$tmp == 3);    #  Alive +2 neighbors or dead +3 neighbors

Just my 2 neurocents.. I'll stay tuned looking for your precious nodes!

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Create A New User
Node Status?
node history
Node Type: CUFP [id://1214661]
Approved by marto
Front-paged by Discipulus
help
Chatterbox?
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2018-05-22 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (163 votes). Check out past polls.

Notices?