I have a dynamic programming algorithm (modified by Needleman-Wunsch) that requires the same basic calculation twice, but the calculation is performed in the orthogonal direction a second time. For example, from a given cell (i, j) in the scoreMatrix matrix, I want to calculate both the value from the βupβ values ββfrom (i, j) and the value from the values ββin βleftβ of (i, k). To reuse the code, I used a function in which in the first case I send the parameters i, j, scoreMatrix, and in the next case I send j, i, scoreMatrix.transpose (). Here is a very simplified version of this code:
def calculateGapCost(i,j,scoreMatrix,gapcost): return scoreMatrix[i-1,j] - gapcost ... gapLeft = calculateGapCost(i,j,scoreMatrix,gapcost) gapUp = calculateGapCost(j,i,scoreMatrix.transpose(),gapcost) ...
I realized that I could alternatively send a function that in one case would go through the arguments (i, j) when retrieving the value from scoreMatrix, and in the other case would cancel them with (j, i), and not rearrange the matrix every time .
def passThrough(i,j,matrix): return matrix[i,j] def flipIndices(i,j,matrix): return matrix[j,i] def calculateGapCost(i,j,scoreMatrix,gapcost,retrieveValue): return retrieveValue(i-1,j,scoreMatrix) - gapcost ... gapLeft = calculateGapCost(i,j,scoreMatrix,gapcost,passThrough) gapUp = calculateGapCost(j,i,scoreMatrix,gapcost,flipIndices) ...
However, if numpy transpose uses some functions that I donβt know about to transpose in just a few operations, it is possible that the transpose is actually faster than my idea of ββa pass-through function. Can someone tell me what will be faster (or if there is a better method that I did not think about)?
The actual method will call retrieveValue 3 times and includes 2 matrices that will be referenced (and thus transposed using this approach).