I am a data scientist working on time series forecasting (using R and Python 3) at the London Ambulance Service NHS Trust. I earned my PhD in cognitive neuroscience at the University of Glasgow working with fmri data and neural networks. I favour linux machines, and working in the terminal with Vim as my editor of choice.
We define a useful method for applying any function to each element of a matrix (e.g. scaling, raising to any power) and for applying any function to the elements in two matrices to produce a third matrix (e.g. adding, Hadamard products). The 'function_elwise' method forms the basis for several operations.
The method requires an arbitrary function as an argument, and can also take an optional second matrix, B. If B is supplied, then the function must be a function of two variables (these will be the elements in the $i,j$ positions of the two matrices).
We loop over the row and column indices of the matrix and, depending on if a second matrix, B, was supplied, apply the function to yield the new element:
We also define a method to choose which of a list of supplied functions to apply, given the argument B. If the argument B was not an instance of the class 'Mat' we assume an integer or float was passed and that the user wants to use the first listed function, and otherwise wants to apply the second function. Thus the appropriate function is passed, along with the argument B into function_elwise:
Here we call the function_choice method with a list of two lambda functions. In the case that B was not a matrix, we assume that the user wants to add/subtract the same scaler to each element. This is a simple function of each element and is reflected in the first lambda function in the list. On the other hand, if the B argument was a matrix, the second lambda function of two variables (x and y: the $i,j$ elements from the original matrix and matrix B) will be applied:
Just what it sounds like. We will use $\odot$ and $\oslash$ to denote these two operations:
Scalar multiplication, which is a legal operation in linear algebra, is a simple matter of taking some matrix (or vector) and multiplying all of its elements by some number (the scalar):
Just as with the case in the add and subtract methods, we check if the B argument is of class matrix and decide whether to use a function of one or of two variables:
The method requires an arbitrary function as an argument, and can also take an optional second matrix, B. If B is supplied, then the function must be a function of two variables (these will be the elements in the $i,j$ positions of the two matrices).
We loop over the row and column indices of the matrix and, depending on if a second matrix, B, was supplied, apply the function to yield the new element:
< Combining matrices and getting the diagonal
back to project main page
back to home