multiplication

cin input (input is an int) when I input a letter, instead of printing back incorrect once, it prints correct once then inc for the rest of the loop

萝らか妹 提交于 2019-11-26 23:45:57
问题 I'm making a multiplication practice program. As my title says, if I enter a letter into the console instead of a number, it will run off saying correct on the first one, but incorrect on the rest. Even if you're not touching the keyboard, it'll still spit out incorrect. ans = table * i; std::cout << table << " * " << i << " =" << std::endl; std::cin >> input; if(input == ans) { std::cout << "Correct! " << ans << std::endl; } else { std::cout << "Incorrect, the answer was " << ans << std:

Powers of a matrix

丶灬走出姿态 提交于 2019-11-26 21:58:18
问题 I have a square matrix A (nxn). I would like to create a series of k powers of this matrix into an nxnxk multidimensional matrix (Not element-wise but actual powers of the matrix), i.e.getting [A^0 A^1 A^2..A^k] . It's sort of a varied vandermonde for matrix case. I am able to do it with loops but it is annoying and slow. I tried using bsxfun but no luck since I am probably missing something here. Here is a simple loop that I did: for j=1:1:100 final(:,:,j)=A^(j-1); end 回答1: You are trying to

Multiply each element in a vector by itself to create a matrix

混江龙づ霸主 提交于 2019-11-26 21:52:41
问题 I'm trying to multiply each element in a vector by itself such that it produces a matrix that is symmetric about the diagonal. For example, given this vector:: x <- 1:3 I would like to create this: 1 2 3 2 4 6 3 6 9 i.e: x[1]*x[1] x[2]*x[1] x[3]*x[1] x[1]*x[2] x[2]*x[2] x[3]*x[2] x[1]*x[3] x[2]*x[3] x[3]*x[3] Any help would be greatly appreciated. Thanks. 回答1: Like this: x %o% x which is a shortcut for outer(x, x) You can also do tcrossprod(x) 来源: https://stackoverflow.com/questions/22212484

Perform integer division using multiplication [duplicate]

喜你入骨 提交于 2019-11-26 21:14:01
问题 This question already has answers here : Why does GCC use multiplication by a strange number in implementing integer division? (4 answers) Closed 10 months ago . Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) / 0x100000000 For example, performing a division by 9: 12345678 / 9 = (12345678 * 0x1C71C71D) /

32-bit signed integer multiplication without using 64-bit data type

最后都变了- 提交于 2019-11-26 21:04:54
问题 I want to do 32-bit signed integer multiplication without using a 64-bit data type. My inputs are in Q1.31 (both) format. input1 = A32 (Ah Al) - higher, lower half's of A32 input2 = B32 (Bh Bl) - higher, lower half's of B32 Result should be in Q1.31 format, leave the overflow case. I need C code. Please provide the explanation with formats also. 回答1: Signed Q1.31 format is a fully fractional format capable of representing operands between -1 and almost +1. The scale factor is 2 31 . This

Multiply rows of matrix by vector?

不想你离开。 提交于 2019-11-26 17:23:32
I have a numeric matrix with 25 columns and 23 rows, and a vector of length 25. How can I multiply each row of the matrix by the vector without using a for loop? The result should be a 25x23 matrix (the same size as the input), but each row has been multiplied by the vector. Added reproducible example from @hatmatrix's answer: matrix <- matrix(rep(1:3,each=5),nrow=3,ncol=5,byrow=TRUE) [,1] [,2] [,3] [,4] [,5] [1,] 1 1 1 1 1 [2,] 2 2 2 2 2 [3,] 3 3 3 3 3 vector <- 1:5 Desired output: [,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 4 5 [2,] 2 4 6 8 10 [3,] 3 6 9 12 15 hatmatrix I think you're looking for

How can I multiply all items in a list together with Python?

拜拜、爱过 提交于 2019-11-26 17:07:33
I need to write a function that takes a list of numbers and multiplies them together. Example: [1,2,3,4,5,6] will give me 1*2*3*4*5*6 . I could really use your help. Python 3: use functools.reduce : >>> from functools import reduce >>> reduce(lambda x, y: x*y, [1,2,3,4,5,6]) 720 Python 2: use reduce : >>> reduce(lambda x, y: x*y, [1,2,3,4,5,6]) 720 For compatible with 2 and 3 use pip install six , then: >>> from six.moves import reduce >>> reduce(lambda x, y: x*y, [1,2,3,4,5,6]) 720 icecrime You can use: import operator import functools functools.reduce(operator.mul, [1,2,3,4,5,6], 1) See

Python and Powers Math

一曲冷凌霜 提交于 2019-11-26 16:31:37
问题 I've been learning Python but I'm a little confused. Online instructors tell me to use the operator ** as opposed to ^ when I'm trying to raise to a certain number. Example: print 8^3 Gives an output of 11. But what I'm look for (I'm told) is more akin to: print 8**3 which gives the correct answer of 512. But why? Can someone explain this to me? Why is it that 8^3 does not equal 512 as it is the correct answer? In what instance would 11 (the result of 8^3)? I did try to search SO but I'm only

How to multiply a register by 37 using only 2 consecutive leal instructions in x86?

你。 提交于 2019-11-26 14:41:30
问题 Say %edi contains x and I want to end up with 37*x using only 2 consecutive leal instructions, how would I go about this? For example to get 45x you would do leal (%edi, %edi, 8), %edi leal (%edi, %edi, 4), %eax (to be returned) I cannot for the life of me figure out what numbers to put in place of the 8 and 4 so that the result (%eax) will be 37x 回答1: At -O3, gcc will emit (Godbolt compiler explorer): int mul37(int a) { return a*37; } leal (%rdi,%rdi,8), %eax # eax = a * 9 leal (%rdi,%rax,4)

Extracting bits with a single multiplication

落爺英雄遲暮 提交于 2019-11-26 13:55:18
I saw an interesting technique used in an answer to another question , and would like to understand it a little better. We're given an unsigned 64-bit integer, and we are interested in the following bits: 1.......2.......3.......4.......5.......6.......7.......8....... Specifically, we'd like to move them to the top eight positions, like so: 12345678........................................................ We don't care about the value of the bits indicated by . , and they don't have to be preserved. The solution was to mask out the unwanted bits, and multiply the result by 0x2040810204081 .