multiplication

Why is the float * int multiplication faster than int * float in CPython?

Why is the float * int multiplication faster than int * float in CPython? Question: Basically, the expression 0.4 * a is consistently, and surprisingly, significantly faster than a * 0.4. a being an integer. And I have no idea why. I speculated that it is a case of a LOAD_CONST LOAD_FAST bytecode pair being …

Total answers: 1

Matrix multiplication between 2d with 3d?

Matrix multiplication between 2d with 3d? Question: Here is the two matrixs: a’s shape is (2, 2) and b’s shape is (2, 2, 3) I want to get c whose shape is (2, 3) How can I get c using a and b? a = array([[0.5, 0.5], [0.6, 0.4]]) b = array([[[1, 2, 1], [1, …

Total answers: 2

Creating a multiplying function

Creating a multiplying function Question: I don’t understand how to make a function and then make it work which will allow me to multiply. For E.g. def Multiply(answer): num1,num2 = int(2),int(3) answer = num1 * num2 return answer print(Multiply(answer)) I Had a go at making one and didnt work so the one below is where …

Total answers: 9

Multiplication of two arrays in numpy

Multiplication of two arrays in numpy Question: I have two numpy arrays: x = numpy.array([1, 2]) y = numpy.array([3, 4]) And I would like to create a matrix of elements products: [[3, 6], [4, 8]] What is the easiest way to do this? Asked By: Fomalhaut || Source Answers: You can you use np.outer. In …

Total answers: 3

How to multiply all integers inside list

How to multiply all integers inside list Question: Hello so I want to multiply the integers inside a list. For example; l = [1, 2, 3] l = [1*2, 2*2, 3*2] output: l = [2, 4, 6] So I was searching online and most of the answers were regarding multiply all the integers with each …

Total answers: 6

Multiplying without the multiplication operator in python

Multiplying without the multiplication operator in python Question: How do I write a python script that multiplies x * y without using the multiplication operator? I know that basically you should have: def multi(): x = raw_input(‘x is: ‘) y = raw_input(‘y is: ‘) z = #(x + x) y times return z multi() Asked …

Total answers: 5

Python: How to multiply values of a Counter object?

Python: How to multiply values of a Counter object? Question: I’m looking for a way to multiply the values of a Counter object, i.e. a = collections.Counter(one=1, two=2, three=3) >>> Counter({‘three’: 3, ‘two’: 2, ‘one’: 1}) b = a*2 >>> Counter({‘three’: 6, ‘two’: 4, ‘one’: 2}) What’s the standard way of doing this in python? …

Total answers: 3

Multiply Adjacent Elements

Multiply Adjacent Elements Question: I have a tuple of integers such as (1, 2, 3, 4, 5) and I want to produce the tuple (1*2, 2*3, 3*4, 4*5) by multiplying adjacent elements. Is it possible to do this with a one-liner? Asked By: Paul Manta || Source Answers: >>> t = (1, 2, 3, 4, …

Total answers: 6

Python and Powers Math

Python and Powers Math Question: 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: …

Total answers: 3

Multiply matrix by list in Python

Multiply matrix by list in Python Question: I am looking for a way to achieve the following in Python and can’t figure out how to do it: a=[[0,1],[1,0],[1,1]] b=[1,0,5] c=hocuspocus(a,b) –> c=[[0,1],[0,0],[5,5]] So basically I would like to multiply the different matrix rows in a with the list b. Thanks a lot in advance! Asked …

Total answers: 3