matrix-multiplication

Why is matrix multiplication in .NET so slow?

江枫思渺然 提交于 2019-11-27 02:45:08
问题 I don't quite understand what makes matrix multiplication in C#/.NET (and even Java) so slow. Take a look at this benchmark (source): Trying to find an updated benchmark. C#'s integer and double performance is damn close to C++ compiled with MSVC++. 87% as fast for double and 99% as fast for 32-bit integer. Pretty damn good, I'd say. But then look at matrix multiplication. The gap widens to C# being about 19% as fast. This is a pretty huge discrepancy that I don't understand. Matrix

R: How to rescale my matrix by column

元气小坏坏 提交于 2019-11-27 02:18:59
I have a matrix of disease states states0CommercialA , where the columns are states (i.e., "no disease", "disease", "dead") and the rows are model cycles (i.e., 1, 2, 3, 4, etc.). I'm trying to multiply this by a vector of costs commercialMedExp , wherein each cost corresponds to a disease state. I've attempted the following: commercialMedExp * states0CommercialA but it appears as though the multiplication is occurring across columns instead of across rows. Could someone help me with the correct syntax? Something like commercialMedExp0A <- t(apply(states0CommercialA, 1, function(x){ x *

numpy matrix inversion rounding errors

╄→尐↘猪︶ㄣ 提交于 2019-11-26 23:43:41
问题 I am getting a very strange value for my (1,1) entry for my BinvA matrix I am just trying to invert B matrix and do a (B^-1)A multiplication. I understand that when I do the calculation by hand my (1,1) is supposed to be 0 but instead I get 1.11022302e-16. How can I fix it? I know floating point numbers can't be represented to full accuracy but why is this giving me such an inaccurate response and not rounding to 0 is there any way I can make it more accurate? Her is my code: import numpy as

bsxfun implementation in matrix multiplication

喜你入骨 提交于 2019-11-26 23:02:05
As always trying to learn more from you, I was hoping I could receive some help with the following code. I need to accomplish the following: 1) I have a vector: x = [1 2 3 4 5 6 7 8 9 10 11 12] 2) and a matrix: A =[11 14 1 5 8 18 10 8 19 13 20 16] I need to be able to multiply each value from x with every value of A , this means: new_matrix = [1* A 2* A 3* A ... 12* A] This will give me this new_matrix of size (12*m x n) assuming A (mxn) . And in this case (12*4x3) How can I do this using bsxfun from matlab? and, would this method be faster than a for-loop ? Regarding my for-loop , I need some

How to get element-wise matrix multiplication (Hadamard product) in numpy?

 ̄綄美尐妖づ 提交于 2019-11-26 22:47:02
问题 I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]] , equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the result [[19 22], [43 50]] which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions? 回答1: For elementwise multiplication of matrix objects, you can use numpy

Multiply a 3D matrix with a 2D matrix

隐身守侯 提交于 2019-11-26 22:14:18
Suppose I have an AxBxC matrix X and a BxD matrix Y . Is there a non-loop method by which I can multiply each of the C AxB matrices with Y ? You can do this in one line using the functions NUM2CELL to break the matrix X into a cell array and CELLFUN to operate across the cells: Z = cellfun(@(x) x*Y,num2cell(X,[1 2]),'UniformOutput',false); The result Z is a 1-by-C cell array where each cell contains an A-by-D matrix. If you want Z to be an A-by-D-by-C matrix, you can use the CAT function: Z = cat(3,Z{:}); NOTE: My old solution used MAT2CELL instead of NUM2CELL , which wasn't as succinct: [A,B

Difference between numpy dot() and Python 3.5+ matrix multiplication @

。_饼干妹妹 提交于 2019-11-26 19:31:55
I recently moved to Python 3.5 and noticed the new matrix multiplication operator (@) sometimes behaves differently from the numpy dot operator. In example, for 3d arrays: import numpy as np a = np.random.rand(8,13,13) b = np.random.rand(8,13,13) c = a @ b # Python 3.5+ d = np.dot(a, b) The @ operator returns an array of shape: c.shape (8, 13, 13) while the np.dot() function returns: d.shape (8, 13, 8, 13) How can I reproduce the same result with numpy dot? Are there any other significant differences? The @ operator calls the array's __matmul__ method, not dot . This method is also present in

2-D convolution as a matrix-matrix multiplication

两盒软妹~` 提交于 2019-11-26 18:58:27
问题 I know that, in the 1D case, the convolution between two vectors, a and b , can be computed as conv(a, b) , but also as the product between the T_a and b , where T_a is the corresponding Toeplitz matrix for a . Is it possible to extend this idea to 2D? Given a = [5 1 3; 1 1 2; 2 1 3] and b=[4 3; 1 2] , is it possible to convert a in a Toeplitz matrix and compute the matrix-matrix product between T_a and b as in the 1-D case? 回答1: Yes, it is possible and you should also use a doubly block

Efficient 4x4 matrix vector multiplication with SSE: horizontal add and dot product - what's the point?

送分小仙女□ 提交于 2019-11-26 17:43:44
I am trying to find the most efficient implementation of 4x4 matrix (M) multiplication with a vector (u) using SSE. I mean Mu = v. As far as I understand there are two primary ways to go about this: method 1) v1 = dot(row1, u), v2 = dot(row2, u), v3 = dot(row3, u), v4 = dot(row4, u) method 2) v = u1 col1 + u2 col2 + u3 col3 + u4 col4. Method 2 is easy to implement in SSE2. Method 1 can be implement with either the horizontal add instruction in SSE3 or the dot product instruction in SSE4. However, in all my tests method 2 always outperforms method 1. One place where I though method 1 would have

Matrix Multiplication in python?

妖精的绣舞 提交于 2019-11-26 17:32:14
I'm trying to multiply two matrices together using pure python. Input (X1 is a 3x3 and Xt is a 3x2): X1 = [[1.0016, 0.0, -16.0514], [0.0, 10000.0, -40000.0], [-16.0514, -40000.0, 160513.6437]] Xt = [(1.0, 1.0), (0.0, 0.25), (0.0, 0.0625)] where Xt is the zip transpose of another matrix. Now here is the code: def matrixmult (A, B): C = [[0 for row in range(len(A))] for col in range(len(B[0]))] for i in range(len(A)): for j in range(len(B[0])): for k in range(len(B)): C[i][j] += A[i][k]*B[k][j] return C The error that python gives me is this: IndexError: list index out of range. Now I'm not sure