This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>>transitionMatrix = np.matrix([ | |
... [0, 1, 1, 0], | |
... [2, 0, 0, 0], | |
... [2, 0, 0, 1], | |
... [0, 0, 2, 0]], dtype=object) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getMatrix(matrix, turns): | |
""" | |
returns a matrix represents the number of unique paths starting in one position and ending in another in N turns | |
""" | |
# get an array of the binary powers we need to calculate | |
baseTwoArray = getArrayOfPowersOfTwos(turns) | |
# start with an identity matrix | |
resultMatrix = np.identity(matrix.shape[0], dtype=object) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getArrayOfPowersOfTwos(number): | |
""" | |
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest. | |
for example: 9 = 2**0 + 2**3. | |
>>> getArrayOfPowerssOfTwos(9) | |
[0, 3] | |
""" | |
array = list() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import math | |
from functools import lru_cache | |
@lru_cache(maxsize=None) # for memoization | |
def getMatrix(turns): | |
""" | |
returns a matrix represents the number of unique paths starting in one position and ending in another in N turns | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
startingVector0 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
startingVector1 = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] | |
startingVector2 = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0] | |
startingVector3 = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0] | |
startingVector4 = [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] | |
startingVector5 = [0, 0, 0, 0, 0, 1, 0, 0, 0, 0] | |
startingVector6 = [0, 0, 0, 0, 0, 0, 1, 0, 0, 0] | |
startingVector7 = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] | |
startingVector8 = [0, 0, 0, 0, 0, 0, 0, 0, 1, 0] | |
startingVector9 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def getPossibilities(startingPosition, turns): | |
""" | |
Calculates the total number of uniques paths from the starting position in N turns for the transition matrix | |
""" | |
# edgecase: 0 turns, return 1 | |
if turns == 0: | |
return 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getArrayOfMultiplesOfTwos(number): | |
""" | |
breaks an integer into an array of numbers that represent the number as a series of 2 to a power in order from lowest to highest. | |
for example: 9 = 2**0 + 2**3. | |
>>> getArrayOfMultiplesOfTwos(9) | |
[0, 3] | |
""" | |
array = list() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getMatrix(matrix, turns): | |
""" | |
returns a matrix equal to the original matrix multiplied by itself a number of times equal to the number of turns minus one | |
""" | |
if turns==1: | |
return matrix | |
else: | |
turnsBaseTwo = math.log(turns, 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getBaseTwoTurnsMatrix(matrix, baseTwoTurns): | |
""" | |
use this function to calculate the resulting matrix from raising matrix to the 2^baseTwoTurns power. | |
""" | |
for _ in range(baseTwoTurns): | |
matrix = matrix*matrix | |
return matrix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getMatrix(matrix, turns): | |
""" | |
returns a matrix equal to the original matrix multiplied by itself a number of times euqal to the number of turns minus one | |
""" | |
if turns==1: | |
return matrix | |
else: | |
turnsBaseTwo = math.log(turns, 2) |