Created
November 18, 2018 17:24
-
-
Save manvillej/4cf7c9a6b498899d76f3111a72988ba1 to your computer and use it in GitHub Desktop.
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) | |
# we need to remember the last power used so we can calculate how many times we need to | |
# square the current matrix based on the last call. | |
lastPower = 0 | |
for nextPower in baseTwoArray: | |
# calculates how many times to square the matrix | |
timesToSquare = nextPower - lastPower | |
# calculate the next matrix power | |
matrix = getBaseTwoTurnsMatrix(matrix, timesToSquare) | |
# store the cumulative Matrix | |
resultMatrix = resultMatrix*matrix | |
lastPower = nextPower | |
return resultMatrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment