<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><font class="Apple-style-span" size="4">Hello All,</font><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">It seems that flex arrays do not support slice assignment:</font></div><div><font class="Apple-style-span" size="4"><br></font></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="Monaco">py> from scitbx.array_family import flex</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py> ary = flex.float(range(5))</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py> list(ary)</font></div></div><div><div><font class="Apple-style-span" face="Monaco">[0.0, 1.0, 2.0, 3.0, 4.0]</font></div></div><div><div><font class="Apple-style-span" face="Monaco"><div>py> ary[:3] = flex.float([41, 42, 43])</div><div>---------------------------------------------------------------------------</div><div>ArgumentError Traceback (most recent call last)</div><div>/usr/local/cctbx/cctbx_sources/mmtbx/dynamics/<ipython-input-13-5ce3d648bc6f> in <module>()</div><div>----> 1 ary[:3] = flex.float([41, 42, 43])</div><div><br></div><div>ArgumentError: Python argument types in</div><div> float.__setitem__(float, slice, float)</div><div>did not match C++ signature:</div><div> __setitem__(scitbx::af::versa<float, scitbx::af::flex_grid<scitbx::af::small<long, 10ul> > > {lvalue}, scitbx::af::small<long, 10ul>, float)</div><div> __setitem__(scitbx::af::versa<float, scitbx::af::flex_grid<scitbx::af::small<long, 10ul> > > {lvalue}, long, float)</div></font></div></div></blockquote><div><div><span class="Apple-style-span" style="font-size: large; "><br></span></div><div><span class="Apple-style-span" style="font-size: large; ">Slice assignment is common in python:</span></div><div><font class="Apple-style-span" size="4"><br></font></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="Monaco">py> alist = range(5)</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py> alist[:3] = [41, 42, 43]</font></div></div><div><div><font class="Apple-style-span" face="Monaco">py> alist</font></div></div><div><div><font class="Apple-style-span" face="Monaco">[41, 42, 43, 3, 4]</font></div></div></blockquote><div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">Is there a flex array work-alike for this operation?</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">I've found set_selected():</font></div><div><font class="Apple-style-span" size="4"><br></font></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><div><font class="Apple-style-span" face="Monaco">py> sel_ary = flex.bool([True, True, True, False, False])</font></div></div></div><div><div><div><font class="Apple-style-span" face="Monaco">py> ary.set_selected(sel_ary, flex.float([41, 42, 43]))</font></div></div></div><div><div><div><font class="Apple-style-span" face="Monaco"><scitbx_array_family_flex_ext.float at 0x101df1158></font></div></div></div><div><div><div><font class="Apple-style-span" face="Monaco">py> list(ary)</font></div></div></div><div><div><div><font class="Apple-style-span" face="Monaco">[41.0, 42.0, 43.0, 3.0, 4.0]</font></div></div></div></blockquote><div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">But set_selected() is cumbersome for slice assignment because (1) you have to build a flex selection array somehow and (2) you have to put the new values into a flex array of the appropriate type. Is set_selected() the best way? If so, is there something in the cctbx libraries that makes building "slicing" selection arrays more efficient than say a list comprehension?</font></div><div><font class="Apple-style-span" size="4"><br></font></div></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div><font class="Apple-style-span" face="Monaco">sel_ary = flex.bool([True if i < 3 else False for i in xrange(5)])</font></div></div></blockquote><div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4">James</font></div><div><font class="Apple-style-span" size="4"><br></font></div><div><font class="Apple-style-span" size="4"><br></font></div><div apple-content-edited="true"><font class="Apple-style-span" size="4"><br class="Apple-interchange-newline">
</font></div>
<br></div></body></html>