{"action":"created","comment":{"url":"https://api.github.com/repos/tBuLi/HowToThink/pulls/comments/59334245","id":59334245,"diff_hunk":"@@ -183,6 +189,67 @@ Looks rather silly now, doesn't it? When working with images this becomes even m\n because there we might have to loop over three dimensions before we can use the if/else. \n Can you imagine the mess?\n \n+Broadcasting\n+------------\n+\n+Another powerful feature of Numpy is broadcasting. Broadcasting takes place when you perform operations between arrays of different shapes. For instance\n+\n+    .. sourcecode:: python3\n+\n+        >>> a = np.array([\n+            [0, 1],\n+            [2, 3],\n+            [4, 5],\n+            ])\n+        >>> b = np.array([10, 100])\n+        >>> a * b\n+        array([[  0, 100],\n+               [ 20, 300],\n+               [ 40, 500]])\n+\n+The shapes of ``a`` and ``b`` don't match. In order to proceed, Numpy will stretch ``b`` into a second dimension, as if it were stacked three times upon itself.\n+The operation then takes place element-wise.\n+\n+One of the rules of broadcasting is that only dimensions of size 1 can be stretched (if an array only has one dimension, all other dimensions are considered for\n+broadcasting purposes to have size 1). In the example above ``b`` is 1D, and has shape (2,). For broadcasting with ``a``, which has two dimensions, Numpy adds\n+another dimension of size 1 to ``b``. ``b`` now has shape (1, 2). This new dimension can now be stretched three times so that ``b``'s shape matches ``a``'s\n+shape of (3, 2).\n+\n+The other rule is that dimensions are compared from the last to the first. Any dimensions that do not match must be stretched to become equally sized. However,\n+according to the previous rule, only dimensions of size 1 can stretch. This means that some shapes cannot broadcast and Numpy will give you an error:\n+\n+    .. sourcecode:: python3\n+\n+        >>> c = np.array([\n+            [0, 1, 2],\n+            [3, 4, 5],\n+            ])\n+        >>> b = np.array([10, 100])\n+        >>> c * b\n+        ValueError: operands could not be broadcast together with shapes (2,3) (2,)\n+\n+What happens here is that Numpy again adds a dimension to ``b``, making it of shape (1, 2). The sizes of the last dimensions of ``b`` and ``c`` (2 and 3,","path":"source/PvL_06.rst","position":125,"original_position":125,"commit_id":"26bfa3c81d4be353025c4578d487e8a0ab09070a","original_commit_id":"26bfa3c81d4be353025c4578d487e8a0ab09070a","user":{"login":"jbarnoud","id":1382074,"avatar_url":"https://avatars.githubusercontent.com/u/1382074?v=3","gravatar_id":"","url":"https://api.github.com/users/jbarnoud","html_url":"https://github.com/jbarnoud","followers_url":"https://api.github.com/users/jbarnoud/followers","following_url":"https://api.github.com/users/jbarnoud/following{/other_user}","gists_url":"https://api.github.com/users/jbarnoud/gists{/gist_id}","starred_url":"https://api.github.com/users/jbarnoud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jbarnoud/subscriptions","organizations_url":"https://api.github.com/users/jbarnoud/orgs","repos_url":"https://api.github.com/users/jbarnoud/repos","events_url":"https://api.github.com/users/jbarnoud/events{/privacy}","received_events_url":"https://api.github.com/users/jbarnoud/received_events","type":"User","site_admin":false},"body":"...is that Numpt **,** again**,** adds... (comas around again)","created_at":"2016-04-12T07:53:26Z","updated_at":"2016-04-12T07:53:26Z","html_url":"https://github.com/tBuLi/HowToThink/pull/10#discussion_r59334245","pull_request_url":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10","_links":{"self":{"href":"https://api.github.com/repos/tBuLi/HowToThink/pulls/comments/59334245"},"html":{"href":"https://github.com/tBuLi/HowToThink/pull/10#discussion_r59334245"},"pull_request":{"href":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10"}}},"pull_request":{"url":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10","id":66018497,"html_url":"https://github.com/tBuLi/HowToThink/pull/10","diff_url":"https://github.com/tBuLi/HowToThink/pull/10.diff","patch_url":"https://github.com/tBuLi/HowToThink/pull/10.patch","issue_url":"https://api.github.com/repos/tBuLi/HowToThink/issues/10","number":10,"state":"open","locked":false,"title":"Cleanup of Numpy chapter. Added broadcasting examples.","user":{"login":"mnmelo","id":11721337,"avatar_url":"https://avatars.githubusercontent.com/u/11721337?v=3","gravatar_id":"","url":"https://api.github.com/users/mnmelo","html_url":"https://github.com/mnmelo","followers_url":"https://api.github.com/users/mnmelo/followers","following_url":"https://api.github.com/users/mnmelo/following{/other_user}","gists_url":"https://api.github.com/users/mnmelo/gists{/gist_id}","starred_url":"https://api.github.com/users/mnmelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mnmelo/subscriptions","organizations_url":"https://api.github.com/users/mnmelo/orgs","repos_url":"https://api.github.com/users/mnmelo/repos","events_url":"https://api.github.com/users/mnmelo/events{/privacy}","received_events_url":"https://api.github.com/users/mnmelo/received_events","type":"User","site_admin":false},"body":"- Also removed the mention to `np.matrix`, which has really limited application and can always be replaced by the specific algebraic functions.\r\n\r\n- Added further links to advanced material.","created_at":"2016-04-11T15:59:02Z","updated_at":"2016-04-12T07:53:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"16ce3e68903ea3a2ed2d28a11ce4e09caf5c1491","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10/commits","review_comments_url":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10/comments","review_comment_url":"https://api.github.com/repos/tBuLi/HowToThink/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tBuLi/HowToThink/issues/10/comments","statuses_url":"https://api.github.com/repos/tBuLi/HowToThink/statuses/26bfa3c81d4be353025c4578d487e8a0ab09070a","head":{"label":"mnmelo:numpy_cleanup","ref":"numpy_cleanup","sha":"26bfa3c81d4be353025c4578d487e8a0ab09070a","user":{"login":"mnmelo","id":11721337,"avatar_url":"https://avatars.githubusercontent.com/u/11721337?v=3","gravatar_id":"","url":"https://api.github.com/users/mnmelo","html_url":"https://github.com/mnmelo","followers_url":"https://api.github.com/users/mnmelo/followers","following_url":"https://api.github.com/users/mnmelo/following{/other_user}","gists_url":"https://api.github.com/users/mnmelo/gists{/gist_id}","starred_url":"https://api.github.com/users/mnmelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mnmelo/subscriptions","organizations_url":"https://api.github.com/users/mnmelo/orgs","repos_url":"https://api.github.com/users/mnmelo/repos","events_url":"https://api.github.com/users/mnmelo/events{/privacy}","received_events_url":"https://api.github.com/users/mnmelo/received_events","type":"User","site_admin":false},"repo":{"id":55970228,"name":"HowToThink","full_name":"mnmelo/HowToThink","owner":{"login":"mnmelo","id":11721337,"avatar_url":"https://avatars.githubusercontent.com/u/11721337?v=3","gravatar_id":"","url":"https://api.github.com/users/mnmelo","html_url":"https://github.com/mnmelo","followers_url":"https://api.github.com/users/mnmelo/followers","following_url":"https://api.github.com/users/mnmelo/following{/other_user}","gists_url":"https://api.github.com/users/mnmelo/gists{/gist_id}","starred_url":"https://api.github.com/users/mnmelo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mnmelo/subscriptions","organizations_url":"https://api.github.com/users/mnmelo/orgs","repos_url":"https://api.github.com/users/mnmelo/repos","events_url":"https://api.github.com/users/mnmelo/events{/privacy}","received_events_url":"https://api.github.com/users/mnmelo/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mnmelo/HowToThink","description":"Adaptation of the excellent open book How to Think Like a Computer Scientist","fork":true,"url":"https://api.github.com/repos/mnmelo/HowToThink","forks_url":"https://api.github.com/repos/mnmelo/HowToThink/forks","keys_url":"https://api.github.com/repos/mnmelo/HowToThink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mnmelo/HowToThink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mnmelo/HowToThink/teams","hooks_url":"https://api.github.com/repos/mnmelo/HowToThink/hooks","issue_events_url":"https://api.github.com/repos/mnmelo/HowToThink/issues/events{/number}","events_url":"https://api.github.com/repos/mnmelo/HowToThink/events","assignees_url":"https://api.github.com/repos/mnmelo/HowToThink/assignees{/user}","branches_url":"https://api.github.com/repos/mnmelo/HowToThink/branches{/branch}","tags_url":"https://api.github.com/repos/mnmelo/HowToThink/tags","blobs_url":"https://api.github.com/repos/mnmelo/HowToThink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mnmelo/HowToThink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mnmelo/HowToThink/git/refs{/sha}","trees_url":"https://api.github.com/repos/mnmelo/HowToThink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mnmelo/HowToThink/statuses/{sha}","languages_url":"https://api.github.com/repos/mnmelo/HowToThink/languages","stargazers_url":"https://api.github.com/repos/mnmelo/HowToThink/stargazers","contributors_url":"https://api.github.com/repos/mnmelo/HowToThink/contributors","subscribers_url":"https://api.github.com/repos/mnmelo/HowToThink/subscribers","subscription_url":"https://api.github.com/repos/mnmelo/HowToThink/subscription","commits_url":"https://api.github.com/repos/mnmelo/HowToThink/commits{/sha}","git_commits_url":"https://api.github.com/repos/mnmelo/HowToThink/git/commits{/sha}","comments_url":"https://api.github.com/repos/mnmelo/HowToThink/comments{/number}","issue_comment_url":"https://api.github.com/repos/mnmelo/HowToThink/issues/comments{/number}","contents_url":"https://api.github.com/repos/mnmelo/HowToThink/contents/{+path}","compare_url":"https://api.github.com/repos/mnmelo/HowToThink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mnmelo/HowToThink/merges","archive_url":"https://api.github.com/repos/mnmelo/HowToThink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mnmelo/HowToThink/downloads","issues_url":"https://api.github.com/repos/mnmelo/HowToThink/issues{/number}","pulls_url":"https://api.github.com/repos/mnmelo/HowToThink/pulls{/number}","milestones_url":"https://api.github.com/repos/mnmelo/HowToThink/milestones{/number}","notifications_url":"https://api.github.com/repos/mnmelo/HowToThink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mnmelo/HowToThink/labels{/name}","releases_url":"https://api.github.com/repos/mnmelo/HowToThink/releases{/id}","deployments_url":"https://api.github.com/repos/mnmelo/HowToThink/deployments","created_at":"2016-04-11T12:27:21Z","updated_at":"2016-04-11T12:27:22Z","pushed_at":"2016-04-11T15:55:27Z","git_url":"git://github.com/mnmelo/HowToThink.git","ssh_url":"git@github.com:mnmelo/HowToThink.git","clone_url":"https://github.com/mnmelo/HowToThink.git","svn_url":"https://github.com/mnmelo/HowToThink","homepage":null,"size":1376,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tBuLi:master","ref":"master","sha":"4ca31d8c4076d88908a49117cd5021c5d7cd7b46","user":{"login":"tBuLi","id":2188968,"avatar_url":"https://avatars.githubusercontent.com/u/2188968?v=3","gravatar_id":"","url":"https://api.github.com/users/tBuLi","html_url":"https://github.com/tBuLi","followers_url":"https://api.github.com/users/tBuLi/followers","following_url":"https://api.github.com/users/tBuLi/following{/other_user}","gists_url":"https://api.github.com/users/tBuLi/gists{/gist_id}","starred_url":"https://api.github.com/users/tBuLi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tBuLi/subscriptions","organizations_url":"https://api.github.com/users/tBuLi/orgs","repos_url":"https://api.github.com/users/tBuLi/repos","events_url":"https://api.github.com/users/tBuLi/events{/privacy}","received_events_url":"https://api.github.com/users/tBuLi/received_events","type":"User","site_admin":false},"repo":{"id":55002146,"name":"HowToThink","full_name":"tBuLi/HowToThink","owner":{"login":"tBuLi","id":2188968,"avatar_url":"https://avatars.githubusercontent.com/u/2188968?v=3","gravatar_id":"","url":"https://api.github.com/users/tBuLi","html_url":"https://github.com/tBuLi","followers_url":"https://api.github.com/users/tBuLi/followers","following_url":"https://api.github.com/users/tBuLi/following{/other_user}","gists_url":"https://api.github.com/users/tBuLi/gists{/gist_id}","starred_url":"https://api.github.com/users/tBuLi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tBuLi/subscriptions","organizations_url":"https://api.github.com/users/tBuLi/orgs","repos_url":"https://api.github.com/users/tBuLi/repos","events_url":"https://api.github.com/users/tBuLi/events{/privacy}","received_events_url":"https://api.github.com/users/tBuLi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tBuLi/HowToThink","description":"Adaptation of the excellent open book How to Think Like a Computer Scientist","fork":false,"url":"https://api.github.com/repos/tBuLi/HowToThink","forks_url":"https://api.github.com/repos/tBuLi/HowToThink/forks","keys_url":"https://api.github.com/repos/tBuLi/HowToThink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tBuLi/HowToThink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tBuLi/HowToThink/teams","hooks_url":"https://api.github.com/repos/tBuLi/HowToThink/hooks","issue_events_url":"https://api.github.com/repos/tBuLi/HowToThink/issues/events{/number}","events_url":"https://api.github.com/repos/tBuLi/HowToThink/events","assignees_url":"https://api.github.com/repos/tBuLi/HowToThink/assignees{/user}","branches_url":"https://api.github.com/repos/tBuLi/HowToThink/branches{/branch}","tags_url":"https://api.github.com/repos/tBuLi/HowToThink/tags","blobs_url":"https://api.github.com/repos/tBuLi/HowToThink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tBuLi/HowToThink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tBuLi/HowToThink/git/refs{/sha}","trees_url":"https://api.github.com/repos/tBuLi/HowToThink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tBuLi/HowToThink/statuses/{sha}","languages_url":"https://api.github.com/repos/tBuLi/HowToThink/languages","stargazers_url":"https://api.github.com/repos/tBuLi/HowToThink/stargazers","contributors_url":"https://api.github.com/repos/tBuLi/HowToThink/contributors","subscribers_url":"https://api.github.com/repos/tBuLi/HowToThink/subscribers","subscription_url":"https://api.github.com/repos/tBuLi/HowToThink/subscription","commits_url":"https://api.github.com/repos/tBuLi/HowToThink/commits{/sha}","git_commits_url":"https://api.github.com/repos/tBuLi/HowToThink/git/commits{/sha}","comments_url":"https://api.github.com/repos/tBuLi/HowToThink/comments{/number}","issue_comment_url":"https://api.github.com/repos/tBuLi/HowToThink/issues/comments{/number}","contents_url":"https://api.github.com/repos/tBuLi/HowToThink/contents/{+path}","compare_url":"https://api.github.com/repos/tBuLi/HowToThink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tBuLi/HowToThink/merges","archive_url":"https://api.github.com/repos/tBuLi/HowToThink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tBuLi/HowToThink/downloads","issues_url":"https://api.github.com/repos/tBuLi/HowToThink/issues{/number}","pulls_url":"https://api.github.com/repos/tBuLi/HowToThink/pulls{/number}","milestones_url":"https://api.github.com/repos/tBuLi/HowToThink/milestones{/number}","notifications_url":"https://api.github.com/repos/tBuLi/HowToThink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tBuLi/HowToThink/labels{/name}","releases_url":"https://api.github.com/repos/tBuLi/HowToThink/releases{/id}","deployments_url":"https://api.github.com/repos/tBuLi/HowToThink/deployments","created_at":"2016-03-29T18:54:12Z","updated_at":"2016-04-04T09:04:03Z","pushed_at":"2016-04-11T15:59:02Z","git_url":"git://github.com/tBuLi/HowToThink.git","ssh_url":"git@github.com:tBuLi/HowToThink.git","clone_url":"https://github.com/tBuLi/HowToThink.git","svn_url":"https://github.com/tBuLi/HowToThink","homepage":null,"size":1375,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":4,"forks":3,"open_issues":4,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10"},"html":{"href":"https://github.com/tBuLi/HowToThink/pull/10"},"issue":{"href":"https://api.github.com/repos/tBuLi/HowToThink/issues/10"},"comments":{"href":"https://api.github.com/repos/tBuLi/HowToThink/issues/10/comments"},"review_comments":{"href":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10/comments"},"review_comment":{"href":"https://api.github.com/repos/tBuLi/HowToThink/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tBuLi/HowToThink/pulls/10/commits"},"statuses":{"href":"https://api.github.com/repos/tBuLi/HowToThink/statuses/26bfa3c81d4be353025c4578d487e8a0ab09070a"}}}}