good chemistry is complicated,and a little bit messy -LW PerlMonks

comment on

 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;

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";
}

In reply to Conways Game of Life in PDL by mxb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-07-20 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.