Skip to content

Commit 43ecfa3

Browse files
oyamadmmcky
authored andcommitted
Player, NormalFormGame: Make payoff_array's C contiguous (#265)
* Add test_normalformgame_payoff_profile_array_c_contiguous Test should fail * NormalFormGame: Make `payoff_array`'s C contiguous when payoff_profile_array is passed to NormalFormGame * Player: Make `payoff_array` C contiguous
1 parent e9e3214 commit 43ecfa3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

quantecon/game_theory/normal_form_game.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Player(object):
163163
164164
"""
165165
def __init__(self, payoff_array):
166-
self.payoff_array = np.asarray(payoff_array)
166+
self.payoff_array = np.asarray(payoff_array, order='C')
167167

168168
if self.payoff_array.ndim == 0:
169169
raise ValueError('payoff_array must be an array_like')
@@ -475,11 +475,16 @@ def __init__(self, data, dtype=None):
475475
'size of innermost array must be equal to ' +
476476
'the number of players'
477477
)
478-
self.players = tuple(
479-
Player(
478+
payoff_arrays = tuple(
479+
np.empty(data.shape[i:-1]+data.shape[:i], dtype=data.dtype)
480+
for i in range(N)
481+
)
482+
for i, payoff_array in enumerate(payoff_arrays):
483+
payoff_array[:] = \
480484
data.take(i, axis=-1).transpose(list(range(i, N)) +
481485
list(range(i)))
482-
) for i in range(N)
486+
self.players = tuple(
487+
Player(payoff_array) for payoff_array in payoff_arrays
483488
)
484489
self.dtype = data.dtype
485490

quantecon/game_theory/tests/test_normal_form_game.py

+10
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ def test_normalformgame_payoff_profile_array():
236236
assert_array_equal(player_new.payoff_array, payoff_array)
237237

238238

239+
def test_normalformgame_payoff_profile_array_c_contiguous():
240+
nums_actions = (2, 3, 4)
241+
shape = nums_actions + (len(nums_actions),)
242+
payoff_profile_array = \
243+
np.arange(np.prod(shape)).reshape(shape)
244+
g = NormalFormGame(payoff_profile_array)
245+
for player in g.players:
246+
ok_(player.payoff_array.flags['C_CONTIGUOUS'])
247+
248+
239249
# Trivial cases with one player #
240250

241251
class TestPlayer_0opponents:

0 commit comments

Comments
 (0)