Skip to content

Commit 6ec61ea

Browse files
committed
NumPy
1 parent 35bec32 commit 6ec61ea

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,7 +2710,7 @@ import numpy as np
27102710
<1/2d_arr> = <2d>[<2d/1d_bools>] # 1d_bools must have size of a column.
27112711
```
27122712
* **`':'` returns a slice of all dimension's indices. Omitted dimensions default to `':'`.**
2713-
* **Sixth line fails if tuple is used because Python converts `'obj[i, j]'` to `'obj[(i, j)]'`!**
2713+
* **Python converts `'obj[i, j]'` to `'obj[(i, j)]'`. This makes `'<2d>[row_i, col_i]'` and `'<2d>[row_indices]'` indistinguishable to NumPy if tuple of indices is passed!**
27142714
* **Indexing with a slice and 1d array works the same as when using two slices (lines 4, 6, 7).**
27152715
* **`'ix_([1, 2], [3, 4])'` returns `'[[1], [2]]'` and `'[[3, 4]]'`. Due to broadcasting rules, this is the same as using `'[[1, 1], [2, 2]]'` and `'[[3, 4], [3, 4]]'`.**
27162716
* **Any value that is broadcastable to the indexed shape can be assigned to the selection.**
@@ -2734,7 +2734,9 @@ right = [[0.1], [0.6], [0.8]] # Shape: (3, 1)
27342734
left = [[0.1, 0.6, 0.8], # Shape: (3, 3) <- !
27352735
[0.1, 0.6, 0.8],
27362736
[0.1, 0.6, 0.8]]
2737+
```
27372738

2739+
```python
27382740
right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- !
27392741
[0.6, 0.6, 0.6],
27402742
[0.8, 0.8, 0.8]]
@@ -2748,14 +2750,11 @@ right = [[0.1, 0.1, 0.1], # Shape: (3, 3) <- !
27482750
[ 0.1, 0.6, 0.8 ]
27492751
>>> wrapped_points = points.reshape(3, 1)
27502752
[[0.1], [0.6], [0.8]]
2751-
>>> distances = points - wrapped_points
2753+
>>> deltas = points - wrapped_points
27522754
[[ 0. , 0.5, 0.7],
27532755
[-0.5, 0. , 0.2],
27542756
[-0.7, -0.2, 0. ]]
2755-
>>> distances = np.abs(distances)
2756-
[[ 0. , 0.5, 0.7],
2757-
[ 0.5, 0. , 0.2],
2758-
[ 0.7, 0.2, 0. ]]
2757+
>>> distances = np.abs(deltas)
27592758
>>> distances[range(3), range(3)] = np.inf
27602759
[[ inf, 0.5, 0.7],
27612760
[ 0.5, inf, 0.2],

index.html

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<body>
5757
<header>
58-
<aside>December 11, 2024</aside>
58+
<aside>December 13, 2024</aside>
5959
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
6060
</header>
6161

@@ -2211,7 +2211,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
22112211
</code></pre>
22122212
<ul>
22132213
<li><strong><code class="python hljs"><span class="hljs-string">':'</span></code> returns a slice of all dimension's indices. Omitted dimensions default to <code class="python hljs"><span class="hljs-string">':'</span></code>.</strong></li>
2214-
<li><strong>Sixth line fails if tuple is used because Python converts <code class="python hljs"><span class="hljs-string">'obj[i, j]'</span></code> to <code class="python hljs"><span class="hljs-string">'obj[(i, j)]'</span></code>!</strong></li>
2214+
<li><strong>Python converts <code class="python hljs"><span class="hljs-string">'obj[i, j]'</span></code> to <code class="python hljs"><span class="hljs-string">'obj[(i, j)]'</span></code>. This makes <code class="python hljs"><span class="hljs-string">'&lt;2d&gt;[row_i, col_i]'</span></code> and <code class="python hljs"><span class="hljs-string">'&lt;2d&gt;[row_indices]'</span></code> indistinguishable to NumPy if tuple of indices is passed!</strong></li>
22152215
<li><strong>Indexing with a slice and 1d array works the same as when using two slices (lines 4, 6, 7).</strong></li>
22162216
<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>
22172217
<li><strong>Any value that is broadcastable to the indexed shape can be assigned to the selection.</strong></li>
@@ -2228,24 +2228,21 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
22282228
<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>
22292229
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>],
22302230
[<span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span>]]
2231+
</code></pre></div>
22312232

2232-
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>
2233+
<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>
22332234
[<span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.6</span>],
22342235
[<span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">0.8</span>]]
2235-
</code></pre></div>
2236-
2236+
</code></pre>
22372237
<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>])
22382238
[ <span class="hljs-number">0.1</span>, <span class="hljs-number">0.6</span>, <span class="hljs-number">0.8</span> ]
22392239
<span class="hljs-meta">&gt;&gt;&gt; </span>wrapped_points = points.reshape(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>)
22402240
[[<span class="hljs-number">0.1</span>], [<span class="hljs-number">0.6</span>], [<span class="hljs-number">0.8</span>]]
2241-
<span class="hljs-meta">&gt;&gt;&gt; </span>distances = points - wrapped_points
2241+
<span class="hljs-meta">&gt;&gt;&gt; </span>deltas = points - wrapped_points
22422242
[[ <span class="hljs-number">0.</span> , <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
22432243
[<span class="hljs-number">-0.5</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.2</span>],
22442244
[<span class="hljs-number">-0.7</span>, <span class="hljs-number">-0.2</span>, <span class="hljs-number">0.</span> ]]
2245-
<span class="hljs-meta">&gt;&gt;&gt; </span>distances = np.abs(distances)
2246-
[[ <span class="hljs-number">0.</span> , <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
2247-
[ <span class="hljs-number">0.5</span>, <span class="hljs-number">0.</span> , <span class="hljs-number">0.2</span>],
2248-
[ <span class="hljs-number">0.7</span>, <span class="hljs-number">0.2</span>, <span class="hljs-number">0.</span> ]]
2245+
<span class="hljs-meta">&gt;&gt;&gt; </span>distances = np.abs(deltas)
22492246
<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
22502247
[[ inf, <span class="hljs-number">0.5</span>, <span class="hljs-number">0.7</span>],
22512248
[ <span class="hljs-number">0.5</span>, inf, <span class="hljs-number">0.2</span>],
@@ -2925,7 +2922,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
29252922

29262923

29272924
<footer>
2928-
<aside>December 11, 2024</aside>
2925+
<aside>December 13, 2024</aside>
29292926
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
29302927
</footer>
29312928

0 commit comments

Comments
 (0)