matrix-multiplication

Use a dope vector to access arbitrary axial slices of a multidimensional array?

匆匆过客 提交于 2019-11-26 17:20:02
问题 I'm building a suite of functions to work with a multidimensional-array data structure and I want to be able to define arbitrary slices of the arrays so I can implement a generalized inner product of two arbitrary matrices (aka Tensors or n-d arrays ). An APL paper I read (I honestly can't find which -- I've read so many) defines the matrix product on left-matrix X with dimensions A;B;C;D;E;F and right-matrix Y with dimensions G;H;I;J;K where F==G as Z <- X +.× Y Z[A;B;C;D;E;H;I;J;K] <- +/ X

Matrix expression causes error “requires numeric/complex matrix/vector arguments”?

蹲街弑〆低调 提交于 2019-11-26 16:49:39
问题 ma=diag(3)+t(da)%*%da R Code above, error message as follows: Error in t(da) %*% da : requires numeric/complex matrix/vector arguments da is a matrix, looks as following: V45 V46 V47 V48 V49 V50 V51 1 0.461727059 2.357732985 -1.536932071 -1.34425710 0.893541975 -0.0676913075 -0.86532231 2 0.253022555 1.524473647 -0.588911138 -1.65207275 -0.072255170 -0.5212951533 -1.43686625 3 0.824678362 1.497001189 0.335973892 -0.84027799 0.275289411 -0.2921928001 -0.16277595 4 0.854530787 2.258305198 0

CUDA determining threads per block, blocks per grid

為{幸葍}努か 提交于 2019-11-26 15:13:37
问题 I'm new to the CUDA paradigm. My question is in determining the number of threads per block, and blocks per grid. Does a bit of art and trial play into this? What I've found is that many examples have seemingly arbitrary number chosen for these things. I'm considering a problem where I would be able to pass matrices - of any size - to a method for multiplication. So that, each element of C (as in C = A * B) would be calculated by a single thread. How would you determine the threads/block,

Why list comprehension is much faster than numpy for multiplying arrays?

时间秒杀一切 提交于 2019-11-26 14:34:14
问题 Recently I answered to THIS question which wanted the multiplication of 2 lists,some user suggested the following way using numpy, alongside mine which I think is the proper way : (a.T*b).T Also I found that aray.resize() has a same performance like that. any way another answer suggested a solution using list comprehension : [[m*n for n in second] for m, second in zip(b,a)] But after the benchmark I saw that the list comprehension performs very faster than numpy : from timeit import timeit s1

What is the &#39;@=&#39; symbol for in Python?

独自空忆成欢 提交于 2019-11-26 13:01:44
I know @ is for decorators, but what is @= for in Python? Is it just reservation for some future idea? This is just one of my many questions while reading tokenizer.py . rightfold From the documentation : The @ (at) operator is intended to be used for matrix multiplication. No builtin Python types implement this operator. The @ operator was introduced in Python 3.5. @= is matrix multiplication followed by assignment, as you would expect. They map to __matmul__ , __rmatmul__ or __imatmul__ similar to how + and += map to __add__ , __radd__ or __iadd__ . The operator and the rationale behind it

Matrix multiplication: Small difference in matrix size, large difference in timings

倾然丶 夕夏残阳落幕 提交于 2019-11-26 12:48:55
I have a matrix multiply code that looks like this: for(i = 0; i < dimension; i++) for(j = 0; j < dimension; j++) for(k = 0; k < dimension; k++) C[dimension*i+j] += A[dimension*i+k] * B[dimension*k+j]; Here, the size of the matrix is represented by dimension . Now, if the size of the matrices is 2000, it takes 147 seconds to run this piece of code, whereas if the size of the matrices is 2048, it takes 447 seconds. So while the difference in no. of multiplications is (2048*2048*2048)/(2000*2000*2000) = 1.073, the difference in the timings is 447/147 = 3. Can someone explain why this happens? I

Why is there huge performance hit in 2048x2048 versus 2047x2047 array multiplication?

谁都会走 提交于 2019-11-26 12:35:48
问题 I am making some matrix multiplication benchmarking, as previously mentioned in Why is MATLAB so fast in matrix multiplication? Now I\'ve got another issue, when multiplying two 2048x2048 matrices, there is a big difference between C# and others. When I try multiply only 2047x2047 matrices, it seems normal. Added some others for comparsion too. 1024x1024 - 10 seconds. 1027x1027 - 10 seconds. 2047x2047 - 90 seconds. 2048x2048 - 300 seconds. 2049x2049 - 91 seconds. (update) 2500x2500 - 166

R: How to rescale my matrix by column

六月ゝ 毕业季﹏ 提交于 2019-11-26 11:43:52
问题 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

bsxfun implementation in matrix multiplication

不想你离开。 提交于 2019-11-26 08:30:30
问题 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

Multiply a 3D matrix with a 2D matrix

笑着哭i 提交于 2019-11-26 08:16:12
问题 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 ? 回答1: 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