[python-arrayfire] 158/250: Adding conway example

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:44 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit 0d2c9dc26f967ff6846bd35164947abf7e0d1485
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Dec 9 14:47:00 2015 -0500

    Adding conway example
---
 examples/graphics/conway.py  | 74 ++++++++++++++++++++++++++++++++++++++++++++
 examples/graphics/plot3.py   |  1 -
 examples/graphics/surface.py |  1 -
 3 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/examples/graphics/conway.py b/examples/graphics/conway.py
new file mode 100644
index 0000000..9f5d329
--- /dev/null
+++ b/examples/graphics/conway.py
@@ -0,0 +1,74 @@
+#!/usr/bin/python
+
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+
+import arrayfire as af
+import array
+from time import time
+
+h_kernel = array.array('f', (1, 1, 1, 1, 0, 1, 1, 1, 1))
+reset = 500
+game_w = 128
+game_h = 128
+fps = 30
+
+print("Example demonstrating conway's game of life using arrayfire")
+print("The conway_pretty example visualizes all the states in Conway")
+print("Red   : Cells that have died due to under population"    )
+print("Yellow: Cells that continue to live from previous state" )
+print("Green : Cells that are new as a result of reproduction"  )
+print("Blue  : Cells that have died due to over population"     )
+print("This examples is throttled to 30 FPS so as to be a better visualization")
+
+simple_win = af.Window(512, 512, "Conway's Game of Life - Current State")
+pretty_win = af.Window(512, 512, "Conway's Game of Life - Current State with visualization")
+
+simple_win.set_pos(25, 15)
+pretty_win.set_pos(600, 25)
+frame_count = 0
+
+# Copy kernel that specifies neighborhood conditions
+kernel = af.Array(h_kernel, dims=(3,3))
+
+# Generate the initial state with 0s and 1s
+state = af.cast(af.randu(game_h, game_w) > 0.4, af.Dtype.f32)
+
+# tile 3 times to display color
+display  = af.tile(state, 1, 1, 3, 1)
+
+while (not simple_win.close()) and (not pretty_win.close()):
+    delay = time()
+    if (not simple_win.close()): simple_win.image(state)
+    if (not pretty_win.close()): pretty_win.image(display)
+
+    frame_count += 1
+    if (frame_count % reset == 0):
+        state = af.cast(af.randu(game_h, game_w) > 0.4, af.Dtype.f32)
+
+    neighborhood = af.convolve(state, kernel)
+
+    # state == 1 && neighborhood <  2 --> state = 0
+    # state == 1 && neighborhood >  3 --> state = 0
+    # state == 0 && neighborhood == 3 --> state = 1
+    # else state remains un changed
+
+    C0 = neighborhood == 2
+    C1 = neighborhood == 3
+    A0 = (state == 1) & (neighborhood < 2)
+    A1 = (state != 0) & (C0 | C1)
+    A2 = (state == 0) & C1
+    A3 = (state == 1) & (neighborhood > 3)
+
+    display = af.cast(af.join(2, A0 + A1, A1 + A2, A3), af.Dtype.f32)
+
+    state = state * C0 + C1
+
+    while(time() - delay < (1.0 / fps)):
+        pass
diff --git a/examples/graphics/plot3.py b/examples/graphics/plot3.py
index 6f4beb9..4957326 100644
--- a/examples/graphics/plot3.py
+++ b/examples/graphics/plot3.py
@@ -10,7 +10,6 @@
 ########################################################
 
 import arrayfire as af
-import math
 
 af.info()
 
diff --git a/examples/graphics/surface.py b/examples/graphics/surface.py
index 30ea119..1af980e 100644
--- a/examples/graphics/surface.py
+++ b/examples/graphics/surface.py
@@ -10,7 +10,6 @@
 ########################################################
 
 import arrayfire as af
-import math
 
 af.info()
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git



More information about the debian-science-commits mailing list