Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebf21a3

Browse files
committed
Added Expert 4
1 parent ce70d05 commit ebf21a3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.ipynb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,37 @@
11991199
"metadata": {},
12001200
"outputs": []
12011201
},
1202+
{
1203+
"cell_type": "markdown",
1204+
"metadata": {},
1205+
"source": [
1206+
"Consider a set of p matrices wich shape (n,n) and a set of p vectors\n",
1207+
"with shape (n,1). How to compute the sum of of the p matrix products at\n",
1208+
"once ? (result has shape (n,1))\n"
1209+
]
1210+
},
1211+
{
1212+
"cell_type": "code",
1213+
"collapsed": false,
1214+
"input": [
1215+
"# St\u00e9fan van der Walt\n",
1216+
"\n",
1217+
"p, n = 10, 20\n",
1218+
"M = np.ones((p,n,n))\n",
1219+
"V = np.ones((p,n,1))\n",
1220+
"S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])\n",
1221+
"print S\n",
1222+
"\n",
1223+
"# It works, because:\n",
1224+
"# M is (P, N, N)\n",
1225+
"# V is (P, N, 1)\n",
1226+
"# Thus, summing over the paired axes 0 and 0 (of M and V independently),\n",
1227+
"# and 2 and 1, to remain with a Mx1 vector."
1228+
],
1229+
"language": "python",
1230+
"metadata": {},
1231+
"outputs": []
1232+
},
12021233
{
12031234
"cell_type": "heading",
12041235
"level": 2,

README.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,25 @@ Expert
628628
S[2,3] = 42
629629
print S
630630
631+
4. Consider a set of p matrices wich shape (n,n) and a set of p vectors with shape (n,1).
632+
How to compute the sum of of the p matrix products at once ? (result has shape (n,1))
633+
634+
.. code-block:: python
635+
636+
# Stéfan van der Walt
637+
638+
p, n = 10, 20
639+
M = np.ones((p,n,n))
640+
V = np.ones((p,n,1))
641+
S = np.tensordot(M, V, axes=[[0, 2], [0, 1]])
642+
print S
643+
644+
# It works, because:
645+
# M is (P, N, N)
646+
# V is (P, N, 1)
647+
# Thus, summing over the paired axes 0 and 0 (of M and V independently),
648+
# and 2 and 1, to remain with a Mx1 vector.
649+
631650
632651
Master
633652
======

0 commit comments

Comments
 (0)