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.]])
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?
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!
>>> 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]])