multiplication

Python, numpy, einsum multiply a stack of matrices

我们两清 提交于 2019-11-29 17:47:38
问题 For performance reasons, I'm curious if there is a way to multiply a stack of a stack of matrices . I have a 4-D array (500, 201, 2, 2). Its basically a 500 length stack of (201,2,2) matrices where for each of the 500, I want to multiply the adjacent matrices using einsum and get another (201,2,2) matrix. I am only doing matrix multiplication on the [2x2] matrices at the end. Since my explanation is already heading off the rails, I'll just show what I'm doing now, and also the 'reduce'

Understanding Modified Baugh-Wooley multiplication algorithm

雨燕双飞 提交于 2019-11-29 16:20:51
For Modified Baugh-Wooley multiplication algorithm , why is it !(A0*B5) instead of just (A0*B5) ? Same questions for !(A1*B5), !(A2*B5), !(A3*B5), !(A4*B5), !(A5*B4), !(A5*3), !(A5*B2), !(A5*B1) and !(A5*B0) Besides, why there are two extra '1' ? In signed 6-bit 2s complement notation, the place values of the bits are: -32 16 8 4 2 1 Notice that the top bit has a negative value. When addition, subtraction, and multiplication are performed mod 64, however, that minus sign makes absolutely no difference to how those operations work, because 32 = -32 mod 64. Your multiplication is not being

Multiply 2D Matrix with vector to span third dimension - MATLAB

China☆狼群 提交于 2019-11-29 15:33:18
As I am trying to multiply a m x n Matrix with a p-dimensional vector, I am stumbling across some difficulties. Trying to avoid for loops, here is what I am looking to achieve enter code here M = [1 2 3; p = [1;2;3] 4 5 6; 7 8 9] I want to obtain a 3x3x3 matrix, where the slices in third dimension are simply the entries of M multiplied by the respective entry in p . Help is much appreciated You can use bsxfun with permute for a vectorized (no-loop) approach like so - out = bsxfun(@times,M,permute(p(:),[3 2 1])) You would end up with - out(:,:,1) = 1 2 3 4 5 6 7 8 9 out(:,:,2) = 2 4 6 8 10 12

Why is imul used for multiplying unsigned numbers?

为君一笑 提交于 2019-11-29 14:39:55
问题 I compiled the following program: #include <stdint.h> uint64_t usquare(uint32_t x) { return (uint64_t)x * (uint64_t)x; } This disassembles to: 0: 89 f8 mov eax,edi 2: 48 0f af c0 imul rax,rax 6: c3 ret But imul is the instruction for multiplying signed numbers. Why is it used by gcc then? /edit: when using uint64_t the assembly is similar: 0: 48 0f af ff imul rdi,rdi 4: 48 89 f8 mov rax,rdi 7: c3 ret 回答1: TL:DR: because it's a faster way of getting the correct result when we don't care about

Fork Join Matrix Multiplication in Java

别来无恙 提交于 2019-11-29 12:07:29
I’m doing some performance research on the fork/join framework in Java 7. To improve the test results I want to use different recursive algorithms during the tests. One of them is multiplying matrixes. I downloaded the following example from Doug Lea's website (): public class MatrixMultiply { static final int DEFAULT_GRANULARITY = 16; /** The quadrant size at which to stop recursing down * and instead directly multiply the matrices. * Must be a power of two. Minimum value is 2. **/ static int granularity = DEFAULT_GRANULARITY; public static void main(String[] args) { final String usage =

tinyAVR: best known multiplication routines for 8-bit and 16-bit factors? [closed]

大城市里の小女人 提交于 2019-11-29 08:20:44
"Faster than avr200b.asm"? The mpy8u -routine from avr200b.asm for those processors of Atmel's AVR family that do not implement any of the MUL instructions seems pretty generic, but mpy16u looks sloppy for rotating both lower result bytes 16 times instead of 8. Antonio presented a fast 16×16→16 unsigned multiplication using 64 cycles worst case excluding call/return overhead. I arbitrarily suggest as optimisation goals worst case cycle count , word count (RAM and flash), register usage , and expected cycle count in order of decreasing priority. (There are reduced core AVRs ("single digit"

BigInteger most time optimized multiplication

此生再无相见时 提交于 2019-11-29 08:02:24
Hi I want to multiply 2 big integer in a most timely optimized way. I am currently using karatsuba algorithm. Can anyone suggest more optimized way or algo to do it. Thanks public static BigInteger karatsuba(BigInteger x, BigInteger y) { // cutoff to brute force int N = Math.max(x.bitLength(), y.bitLength()); System.out.println(N); if (N <= 2000) return x.multiply(y); // optimize this parameter // number of bits divided by 2, rounded up N = (N / 2) + (N % 2); // x = a + 2^N b, y = c + 2^N d BigInteger b = x.shiftRight(N); BigInteger a = x.subtract(b.shiftLeft(N)); BigInteger d = y.shiftRight(N

Parallel Matrix Multiplication in Java 6

烂漫一生 提交于 2019-11-29 07:33:25
Yesterday I asked a question about parallel matrix multiplication in Java 7 using the fork/join framework here . With the help of axtavt I got my example program to work. Now I’m implementing an equivalent program using Java 6 functionality only. I get the same problem as yesterday, dispite applying the the feedback axtavt gave me (I think). Am I overlooking something? Code: package algorithms; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Java6MatrixMultiply implements Algorithm { private static final int

Matrix multiplication with iterator dependency - NumPy

送分小仙女□ 提交于 2019-11-29 05:05:58
Sometime back this question (now deleted but 10K+ rep users can still view it) was posted. It looked interesting to me and I learnt something new there while trying to solve it and I thought that's worth sharing. I would like to post those ideas/solutions and would love to see people post other possible ways to solve it. I am posting the gist of the question next. So, we have two NumPy ndarrays a and b of shapes : a : (m,n,N) b : (n,m,N) Let's assume we are dealing with cases where m , n & N are comparable. The problem is to solve the following multiplication and summation with focus on

Bash Multiplying Decimal to int

孤者浪人 提交于 2019-11-29 04:40:34
问题 I read price from user input. When i multiply the input with int like this T= "$((PRICE*QTY))"|bc ; gives line 272: 12.00: syntax error: invalid arithmetic operator (error token is ".00") or .50 depending on user input. How do i multiply these two variables and get a total with 2 decimal points? 回答1: this works: PRICE=1.1 QTY=21 RES=$(echo "scale=4; $PRICE*$QTY" | bc) echo $RES 回答2: var=$(echo "scale=2;$PRICE*$QTY" |bc) You can also use awk awk -vp=$PRICE -vq=$QTY 'BEGIN{printf "%.2f" ,p * q}