You can multiply numpy arrays by scalars and it just works.
>>> import numpy as np
>>> np.array([1, 2, 3]) * 2
array([2, 4, 6])
>>> np.array([[1, 2, 3], [4, 5, 6]]) * 2
array([[ 2, 4, 6],
[ 8, 10, 12]])
This is also a very fast and efficient operation. With your example:
>>> a_1 = np.array([1.0, 2.0, 3.0])
>>> a_2 = np.array([[1., 2.], [3., 4.]])
>>> b = 2.0
>>> a_1 * b
array([2., 4., 6.])
>>> a_2 * b
array([[2., 4.],
[6., 8.]])
Answer from iz_ on Stack OverflowYou can multiply numpy arrays by scalars and it just works.
>>> import numpy as np
>>> np.array([1, 2, 3]) * 2
array([2, 4, 6])
>>> np.array([[1, 2, 3], [4, 5, 6]]) * 2
array([[ 2, 4, 6],
[ 8, 10, 12]])
This is also a very fast and efficient operation. With your example:
>>> a_1 = np.array([1.0, 2.0, 3.0])
>>> a_2 = np.array([[1., 2.], [3., 4.]])
>>> b = 2.0
>>> a_1 * b
array([2., 4., 6.])
>>> a_2 * b
array([[2., 4.],
[6., 8.]])
Using .multiply() (ufunc multiply)
a_1 = np.array([1.0, 2.0, 3.0])
a_2 = np.array([[1., 2.], [3., 4.]])
b = 2.0
np.multiply(a_1,b)
# array([2., 4., 6.])
np.multiply(a_2,b)
# array([[2., 4.],[6., 8.]])
Videos
The Numpythonic approach: (using numpy.dot in order to get the dot product of two matrices)
In [1]: import numpy as np
In [3]: np.dot([1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]])
Out[3]: array([1, 1])
The Pythonic approach:
The length of your second for loop is len(v) and you attempt to indexing v based on that so you got index Error . As a more pythonic way you can use zip function to get the columns of a list then use starmap and mul within a list comprehension:
In [13]: first,second=[1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]]
In [14]: from itertools import starmap
In [15]: from operator import mul
In [16]: [sum(starmap(mul, zip(first, col))) for col in zip(*second)]
Out[16]: [1, 1]
I think the problem with your code was that you loop through the rows of the matrix rather than by the columns. Also you don't reset your 'total' variable after each vector*matrix column calculation. This is what you want:
def multiply(v, G):
result = []
for i in range(len(G[0])): #this loops through columns of the matrix
total = 0
for j in range(len(v)): #this loops through vector coordinates & rows of matrix
total += v[j] * G[j][i]
result.append(total)
return result
Hi
I'm quite new to Twine, using SugarCube 2.31.1 and have a nested array that I want to extract and multiply by a scalar variable.
<<set $belief0001 to ["I believe in God",1,[0,1,2,3,1,2,1],1,14,[]]>>
<<set $belief0002 to ["Human beings are OK",2,[0,1,3,2,3,2,1],1,5,[]]>>
<<set $belief0003 to ["Reality is an illusion",3,[0,3,3,3,1,1,1],1,9,[]]>>
<<set $beliefs to [$belief0001,$belief0002,$belief0003]>>
Now what I want to do is create a new array that is the product of $beliefs[$i][2] * $beliefs[$i][3] * $beliefs[$i][4] using a <<for>> loop something like this:
<<for $i to 0; $i < $beliefs.length; $i++>>
<<set $newarray to $beliefs[$i][2] * $beliefs[$i][3] * $beliefs[$i][4]>>
<</for>>
The thing is, $beliefs[$i][2] is a nested array and it seems Twine doesn't like me multipliying it with the other two scalar variables. I also think this is affecting the $beliefs array which I don't want it to do, I want it to create an entirely new array called $newarray.
Any help appreciated!
ar = [0] * 3 ar[0] = 10 print(ar) # 10, 0, 0
ar = [ [0] * 3 ] * 3 ar[0][0] = 10 print(ar) # 10, 0, 0 , 10, 0, 0 , 10, 0, 0
-
operator on list creates n references to the same list hence the second case is digestible. But if that's the case why don't in 1d list we are getting 10, 10, 10?
Hey guys! So I'm having trouble learning this exercise and I can't figure out why I keep getting this type error every time run this code. The task is to use a nested set of for loops to multiply each integer in a list of lists by a float
# These are all the numbers in the list
allscores = [['90', '83', '95'], ['100', '78', '89', '87', '90'], ['57', '82', '85', '89']]
# This is the nested set of for loops I used to multiply each number
for student_scores in allscores:
for i in range(len(student_scores)):
student_scores[i] = int(student_scores[i] * 1.05)
print("All scores after extra credit:", allscores)after running the code, pycharm responded with this
student_scores[i] = int(student_scores[i] * 1.05)
~~~~~~~~~~~~~~~~~~^~~~~~
TypeError: can't multiply sequence by non-int of type 'float'The list output should end up looking like this
All scores after extra credit: [[94, 87, 99], [105, 81, 93, 91, 94], [59, 86, 89, 93]]
>>> b = np.arange(12).reshape([3, 4])
>>> a = np.arange(3)
>>> b
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> a
array([0, 1, 2])
>>> a * b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (3,) (3,4)
>>> b = np.arange(9).reshape([3, 3])
>>> a * b
array([[ 0, 1, 4],
[ 0, 4, 10],
[ 0, 7, 16]])This isn't making any sense to me:
import numpy as np
# Builds matrices
A = np.matrix('1 -2 3 7; 2 1 1 4; -3 2 -2 10')
B = np.matrix('0 1 -3 -2; 10 -1 2 -3; 5 1 -1 4')
C = np.matrix('4 0 -2 3; 3 6 9 7; 2 2 5 1; 9 4 6 -2')
print(A@B)That returns a value error, even though they're the same size.
A@C
That one works though, even though A and C are different sizes. Why can't NumPy do A@B?