Multi-dim slices playground prototype by Robert Griesemer: https://github.com/griesemer/dotGo2016

polaris · · 478 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://github.com/griesemer/dotGo2016">https://github.com/griesemer/dotGo2016</a></p> <hr/>**评论:**<br/><br/>SteveMcQwark: <pre><p><a href="https://github.com/griesemer/dotGo2016/">https://github.com/griesemer/dotGo2016/</a></p></pre>chewxy: <pre><p>For existing &#34;multi-dim&#34; slices, there&#39;s always <a href="https://github.com/chewxy/gorgonia">Gorgonia</a>&#39;s tensor package. </p> <h1>Slice</h1> <p>You can slice multidimensionally:</p> <pre><code>T := NewTensor(WithShape(3,3), WithBacking(RangeFloat64(0,9)) fmt.Printf(&#34;T: %+v\n&#34;, T) // T[0] T0, _ := T.Slice(S(0)) fmt.Printf(&#34;T[0]: %+v\n&#34;, T0) //T[:, 0] T_0, _ := T.Slice(nil, S(0)) fmt.Printf(&#34;T[:, 0]: %+v\n&#34;, T_0) // T[0:4:2] - rows 0 to 4, but skip every 2nd row T012, _ := T.Slice(S(0, 4, 2)) fmt.Printf(&#34;T[0:4;2]: %+v\n&#34;, T012) // T[:, 1:3] T_13, _ := T.Slice(nil, S(1, 3)) fmt.Printf(&#34;T[:, 1:3]: %+v\n&#34;, T_13) </code></pre> <p>Will yield these:</p> <pre><code>T: Matrix (5, 5) [5 1] ⎡ 0 1 2 3 4⎤ ⎢ 5 6 7 8 9⎥ ⎢10 11 12 13 14⎥ ⎢15 16 17 18 19⎥ ⎣20 21 22 23 24⎦ T[0]: Vector (5) [1] [0 1 2 3 4] T[:, 0]: Vector (5) [5] [ 0 5 10 15 20] T[0:4:2]: Matrix (2, 5) [10 1] ⎡ 0 1 2 3 4⎤ ⎣10 11 12 13 14⎦ T[:, 1:3]: Matrix (5, 2) [5 1] ⎡ 1 2⎤ ⎢ 6 7⎥ ⎢11 12⎥ ⎢16 17⎥ ⎣21 22⎦ </code></pre> <h1>Transpose</h1> <p>You can transpose! The <code>.T()</code> method doesn&#39;t move the underlying data. The underlying data is moved only when <code>.Transpose()</code> is called AFTER a <code>.T()</code> is called. This results in a zero-op transposition. Fun fact - the <code>.Transpose()</code> method also doesn&#39;t allocate a new array - it&#39;s an in-place transpose. I&#39;m working on a version that is based on a rubiks cubes&#39; permutation that allocates even less!</p> <p>Given a 3-Tensor (oh yeah, it goes beyond 2D tables):</p> <pre><code>T = NewTensor(WithShape(2, 3, 4), WithBacking(RangeFloat64(0, 24))) fmt.Printf(&#34;T: %+v\n&#34;, T) T.T() // vanilla transpose fmt.Printf(&#34;T.T(): %+v\n&#34;, T) T.UT() T.T(2, 0, 1) fmt.Printf(&#34;T.T(2,0,1): %+v\n&#34;, T) </code></pre> <p>This is the result:</p> <pre><code>T: 3-Tensor (2, 3, 4) [12 4 1] ⎡ 0 1 2 3⎤ ⎢ 4 5 6 7⎥ ⎣ 8 9 10 11⎦ ⎡12 13 14 15⎤ ⎢16 17 18 19⎥ ⎣20 21 22 23⎦ T.T(): 3-Tensor (4, 3, 2) [1 4 12] ⎡ 0 12⎤ ⎢ 4 16⎥ ⎣ 8 20⎦ ⎡ 1 13⎤ ⎢ 5 17⎥ ⎣ 9 21⎦ ⎡ 2 14⎤ ⎢ 6 18⎥ ⎣10 22⎦ ⎡ 3 15⎤ ⎢ 7 19⎥ ⎣11 23⎦ T.T(2,0,1): 3-Tensor (4, 2, 3) [1 12 4] ⎡ 0 4 8⎤ ⎣12 16 20⎦ ⎡ 1 5 9⎤ ⎣13 17 21⎦ ⎡ 2 6 10⎤ ⎣14 18 22⎦ ⎡ 3 7 11⎤ ⎣15 19 23⎦ </code></pre> <h1>Reshape</h1> <p>You can reshape!</p> <pre><code>T = NewTensor(WithShape(2, 3), WithBacking(RangeFloat64(0, 6))) fmt.Printf(&#34;T: %+v\n&#34;, T) T.Reshape(3, 2) fmt.Printf(&#34;T(reshaped): %+v\n&#34;, T) </code></pre> <p>Yields:</p> <pre><code>T: Matrix (2, 3) [3 1] ⎡0 1 2⎤ ⎣3 4 5⎦ T(reshaped): Matrix (3, 2) [2 1] ⎡0 1⎤ ⎢2 3⎥ ⎣4 5⎦ </code></pre></pre>f2f: <pre><p>was anyone from here attending? what is this about, indexing multi-dimensional arrays or iterators?</p> <p>is there a supporting proposal?</p></pre>joetsai: <pre><p>The main point of his talk: Prototypes are a valuable tool to accompany language proposals.</p> <p>A prototypes should either explore alternative solutions or be a bare-bones implementation of the proposed language feature. Ideally, the prototype is:</p> <ul> <li>Easy to build (only few hundred lines of change)</li> <li>Thus, easy to the throw away too</li> <li>Provides valuable insight into other approaches to the problem, how effectively the proposal solves a given problem, or unknown pain-points that the proposal introduces</li> </ul></pre>xargon7: <pre><p>See this issue: <a href="https://github.com/golang/go/issues/6282" rel="nofollow">https://github.com/golang/go/issues/6282</a></p></pre>Kyrra: <pre><p>So you don&#39;t have to dig through the comments, here is the design document: <a href="https://github.com/golang/proposal/blob/master/design/6282-table-data.md" rel="nofollow">https://github.com/golang/proposal/blob/master/design/6282-table-data.md</a></p></pre>howeman: <pre><p>Note that there is a very substantial revision to the proposal in progress:</p> <p><a href="https://go-review.googlesource.com/#/c/25180/" rel="nofollow">https://go-review.googlesource.com/#/c/25180/</a></p></pre>ngrilly: <pre><p>I really like Griesemer&#39;s prototype. It demonstrates two things:</p> <ul> <li><p>It looks possible to have user-defined operators ([], +, -, *, etc.) without making the language too much complex.</p></li> <li><p>If this is possible, Go could become a really great alternative to Python/numpy.</p></li> </ul> <p>Example (from the repo):</p> <pre><code>a := NewMatrix(4, 5) a.Set( 4, 2, 7, 9, 1, 5, 0, 1, 8, 3, 5, 6, 3, 2, 1, 7, 9, 0, 1, 2, ) b := NewMatrix(5, 3) b.Set( 3, 4, 5, 0, 3, 1, 3, 2, 1, 8, 2, 6, 2, 7, 1, ) (a * b).Print() </code></pre></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

478 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传