Skip to content

Commit

Permalink
Closure, NumPy
Browse files Browse the repository at this point in the history
  • Loading branch information
gto76 committed Dec 16, 2024
1 parent 94cc064 commit a603cb5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 58 deletions.
57 changes: 28 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ import <package>.<module> # Imports a built-in or '<package>/<module>.py'.

Closure
-------
**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.**
**We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.**

```python
def get_multiplier(a):
Expand Down Expand Up @@ -2717,50 +2717,49 @@ import numpy as np

### Broadcasting
**A set of rules by which NumPy functions operate on arrays of different shapes.**

```python
left = [ 0.1 , 0.6 , 0.8 ] # Shape: (3,)
right = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
left = np.array([ 0.1, 0.6, 0.8 ]) # `left.shape == (3,)`
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
```

#### 1. If array shapes differ in length, left-pad the shorter shape with ones:
```python
left = [[0.1 , 0.6 , 0.8]] # Shape: (1, 3) <- !
right = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
left = np.array([[0.1, 0.6, 0.8]]) # `left.shape == (1, 3)`
right = np.array([[0.1],[0.6],[0.8]]) # `right.shape == (3, 1)`
```

#### 2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:
```python
left = [[0.1, 0.6, 0.8], # Shape: (3, 3) <- !
[0.1, 0.6, 0.8],
[0.1, 0.6, 0.8]]
```
left = np.array([[0.1, 0.6, 0.8], # `left.shape == (3, 3)`
[0.1, 0.6, 0.8],
[0.1, 0.6, 0.8]])

```python
right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- !
[0.6, 0.6, 0.6],
[0.8, 0.8, 0.8]]
right = np.array([[0.1, 0.1, 0.1], # `right.shape == (3, 3)`
[0.6, 0.6, 0.6],
[0.8, 0.8, 0.8]])
```

### Example
#### For each point returns index of its nearest point (`[0.1, 0.6, 0.8] => [1, 2, 1]`):

```python
>>> points = np.array([0.1, 0.6, 0.8])
[ 0.1, 0.6, 0.8 ]
>>> wrapped_points = points.reshape(3, 1)
[[0.1], [0.6], [0.8]]
>>> deltas = points - wrapped_points
[[ 0. , 0.5, 0.7],
[-0.5, 0. , 0.2],
[-0.7, -0.2, 0. ]]
>>> distances = np.abs(deltas)
>>> distances[range(3), range(3)] = np.inf
[[ inf, 0.5, 0.7],
[ 0.5, inf, 0.2],
[ 0.7, 0.2, inf]]
>>> distances.argmin(1)
[1, 2, 1]
>>> print(points := np.array([0.1, 0.6, 0.8]))
[0.1 0.6 0.8]
>>> print(wrapped_points := points.reshape(3, 1))
[[0.1]
[0.6]
[0.8]]
>>> print(deltas := points - wrapped_points)
[[ 0. 0.5 0.7]
[-0.5 0. 0.2]
[-0.7 -0.2 0. ]]
>>> deltas[range(3), range(3)] = np.inf
>>> print(distances := np.abs(deltas))
[[inf 0.5 0.7]
[0.5 inf 0.2]
[0.7 0.2 inf]]
>>> print(distances.argmin(axis=1))
[1 2 1]
```


Expand Down
60 changes: 31 additions & 29 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<body>
<header>
<aside>December 14, 2024</aside>
<aside>December 16, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>

Expand Down Expand Up @@ -701,7 +701,7 @@
<li><strong>Directory of the file that is passed to python command serves as a root of local imports.</strong></li>
<li><strong>For relative imports use <code class="python hljs"><span class="hljs-string">'from .[…][&lt;pkg/module&gt;[.…]] import &lt;obj&gt;'</span></code>.</strong></li>
</ul>
<div><h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns the nested function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_multiplier</span><span class="hljs-params">(a)</span>:</span>
<div><h2 id="closure"><a href="#closure" name="closure">#</a>Closure</h2><p><strong>We have/get a closure in Python when a nested function references a value of its enclosing function and then the enclosing function returns its nested function.</strong></p><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_multiplier</span><span class="hljs-params">(a)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">out</span><span class="hljs-params">(b)</span>:</span>
<span class="hljs-keyword">return</span> a * b
<span class="hljs-keyword">return</span> out
Expand Down Expand Up @@ -2216,39 +2216,41 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
<li><strong><code class="python hljs"><span class="hljs-string">'ix_([1, 2], [3, 4])'</span></code> returns <code class="python hljs"><span class="hljs-string">'[[1], [2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4]]'</span></code>. Due to broadcasting rules, this is the same as using <code class="python hljs"><span class="hljs-string">'[[1, 1], [2, 2]]'</span></code> and <code class="python hljs"><span class="hljs-string">'[[3, 4], [3, 4]]'</span></code>.</strong></li>
<li><strong>Any value that is broadcastable to the indexed shape can be assigned to the selection.</strong></li>
</ul>
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><code class="python language-python hljs">left = [ <span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span> ] <span class="hljs-comment"># Shape: (3,)</span>
right = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
<div><h3 id="broadcasting">Broadcasting</h3><p><strong>A set of rules by which NumPy functions operate on arrays of different shapes.</strong></p><pre><code class="python language-python hljs">left = np.array([ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]) <span class="hljs-comment"># `left.shape == (3,)`</span>
right = np.array([[<span class="hljs-number">0.1</span>],[<span class="hljs-number">0.6</span>],[<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
</code></pre></div>


<div><h4 id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span> , <span class="hljs-number">0.6</span> , <span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (1, 3) &lt;- !</span>
right = [[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]] <span class="hljs-comment"># Shape: (3, 1)</span>
<div><h4 id="1ifarrayshapesdifferinlengthleftpadtheshortershapewithones">1. If array shapes differ in length, left-pad the shorter shape with ones:</h4><pre><code class="python language-python hljs">left = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `left.shape == (1, 3)`</span>
right = np.array([[<span class="hljs-number">0.1</span>],[<span class="hljs-number">0.6</span>],[<span class="hljs-number">0.8</span>]]) <span class="hljs-comment"># `right.shape == (3, 1)`</span>
</code></pre></div>

<div><h4 id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><code class="python language-python hljs">left = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], <span class="hljs-comment"># Shape: (3, 3) &lt;- !</span>
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>],
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]
<div><h4 id="2ifanydimensionsdifferinsizeexpandtheonesthathavesize1byduplicatingtheirelements">2. If any dimensions differ in size, expand the ones that have size 1 by duplicating their elements:</h4><pre><code class="python language-python hljs">left = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>], <span class="hljs-comment"># `left.shape == (3, 3)`</span>
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>],
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]])

right = np.array([[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>], <span class="hljs-comment"># `right.shape == (3, 3)`</span>
[<span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>],
[<span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>]])
</code></pre></div>

<pre><code class="python language-python hljs">right = [[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>, <span class="hljs-number">0.1</span>], <span class="hljs-comment"># Shape: (3, 3) &lt;- !</span>
[<span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>],
[<span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>]]
</code></pre>
<div><h3 id="example-3">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] =&gt; [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>points = np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>])
[ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]
<span class="hljs-meta">&gt;&gt;&gt; </span>wrapped_points = points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>)
[[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]]
<span class="hljs-meta">&gt;&gt;&gt; </span>deltas = points - wrapped_points
[[ <span class="hljs-number">0.</span> , <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
[<span class="hljs-number">-0.5</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.2</span>],
[<span class="hljs-number">-0.7</span>, <span class="hljs-number">-0.2</span>, <span class="hljs-number">0.</span> ]]
<span class="hljs-meta">&gt;&gt;&gt; </span>distances = np.abs(deltas)
<span class="hljs-meta">&gt;&gt;&gt; </span>distances[range(<span class="hljs-number">3</span>), range(<span class="hljs-number">3</span>)] = np.inf
[[ inf, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
[ <span class="hljs-number">0.5</span>, inf, <span class="hljs-number">0.2</span>],
[ <span class="hljs-number">0.7</span>, <span class="hljs-number">0.2</span>, inf]]
<span class="hljs-meta">&gt;&gt;&gt; </span>distances.argmin(<span class="hljs-number">1</span>)
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]
<div><h3 id="example-3">Example</h3><div><h4 id="foreachpointreturnsindexofitsnearestpoint010608121">For each point returns index of its nearest point (<code class="python hljs">[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>] =&gt; [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]</code>):</h4><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>print(points := np.array([<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]))
[<span class="hljs-number">0.1</span> <span class="hljs-number">0.6</span> <span class="hljs-number">0.8</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>print(wrapped_points := points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>))
[[<span class="hljs-number">0.1</span>]
[<span class="hljs-number">0.6</span>]
[<span class="hljs-number">0.8</span>]]
<span class="hljs-meta">&gt;&gt;&gt; </span>print(deltas := points - wrapped_points)
[[ <span class="hljs-number">0.</span> <span class="hljs-number">0.5</span> <span class="hljs-number">0.7</span>]
[<span class="hljs-number">-0.5</span> <span class="hljs-number">0.</span> <span class="hljs-number">0.2</span>]
[<span class="hljs-number">-0.7</span> <span class="hljs-number">-0.2</span> <span class="hljs-number">0.</span> ]]
<span class="hljs-meta">&gt;&gt;&gt; </span>deltas[range(<span class="hljs-number">3</span>), range(<span class="hljs-number">3</span>)] = np.inf
<span class="hljs-meta">&gt;&gt;&gt; </span>print(distances := np.abs(deltas))
[[inf <span class="hljs-number">0.5</span> <span class="hljs-number">0.7</span>]
[<span class="hljs-number">0.5</span> inf <span class="hljs-number">0.2</span>]
[<span class="hljs-number">0.7</span> <span class="hljs-number">0.2</span> inf]]
<span class="hljs-meta">&gt;&gt;&gt; </span>print(distances.argmin(axis=<span class="hljs-number">1</span>))
[<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">1</span>]
</code></pre></div></div>


Expand Down Expand Up @@ -2922,7 +2924,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment


<footer>
<aside>December 14, 2024</aside>
<aside>December 16, 2024</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Expand Down

0 comments on commit a603cb5

Please sign in to comment.