index.html 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- ra:: (version 14, updated 2021 February 22)
  4. (c) Daniel Llorens 2005-2020
  5. Permission is granted to copy, distribute and/or modify this document
  6. under the terms of the GNU Free Documentation License, Version 1.3 or
  7. any later version published by the Free Software Foundation; with no
  8. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. -->
  9. <!-- Created by GNU Texinfo 6.3, http://www.gnu.org/software/texinfo/ -->
  10. <head>
  11. <title>ra:: —An array library for C++20</title>
  12. <meta name="description" content="ra:: —An array library for C++20">
  13. <meta name="keywords" content="ra:: —An array library for C++20">
  14. <meta name="resource-type" content="document">
  15. <meta name="distribution" content="global">
  16. <meta name="Generator" content="makeinfo">
  17. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  18. <link href="#Top" rel="start" title="Top">
  19. <link href="#Indices" rel="index" title="Indices">
  20. <link href="dir.html#Top" rel="up" title="(dir)">
  21. <style type="text/css">
  22. <!--
  23. a.summary-letter {text-decoration: none}
  24. blockquote.indentedblock {margin-right: 0em}
  25. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  26. blockquote.smallquotation {font-size: smaller}
  27. div.display {margin-left: 3.2em}
  28. div.example {margin-left: 3.2em}
  29. div.lisp {margin-left: 3.2em}
  30. div.smalldisplay {margin-left: 3.2em}
  31. div.smallexample {margin-left: 3.2em}
  32. div.smalllisp {margin-left: 3.2em}
  33. kbd {font-style: oblique}
  34. pre.display {font-family: inherit}
  35. pre.format {font-family: inherit}
  36. pre.menu-comment {font-family: serif}
  37. pre.menu-preformatted {font-family: serif}
  38. pre.smalldisplay {font-family: inherit; font-size: smaller}
  39. pre.smallexample {font-size: smaller}
  40. pre.smallformat {font-family: inherit; font-size: smaller}
  41. pre.smalllisp {font-size: smaller}
  42. span.nolinebreak {white-space: nowrap}
  43. span.roman {font-family: initial; font-weight: normal}
  44. span.sansserif {font-family: sans-serif; font-weight: normal}
  45. ul.no-bullet {list-style: none}
  46. -->
  47. </style>
  48. </head>
  49. <body lang="en">
  50. <h1 class="settitle" align="center">ra:: —An array library for C++20</h1>
  51. <a name="Top"></a>
  52. <div class="header">
  53. <p>
  54. Next: <a href="#Overview" accesskey="n" rel="next">Overview</a>, Up: <a href="dir.html#Top" accesskey="u" rel="up">(dir)</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <a name="ra_003a_003a"></a>
  57. <h1 class="top"><code>ra::</code></h1>
  58. <p><code>ra::</code> (version 14, updated 2021 February 22)
  59. </p>
  60. <p>(c) Daniel Llorens 2005&ndash;2020
  61. </p>
  62. <div class="smalldisplay">
  63. <pre class="smalldisplay">Permission is granted to copy, distribute and/or modify this document
  64. under the terms of the GNU Free Documentation License, Version 1.3 or
  65. any later version published by the Free Software Foundation; with no
  66. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  67. </pre></div>
  68. <p><code>ra::</code><a name="DOCF1" href="#FOOT1"><sup>1</sup></a> is a general purpose multidimensional array and expression template library for C++20. Please keep in mind that this manual is a work in progress. There are many errors and whole sections unwritten.
  69. </p>
  70. <table class="menu" border="0" cellspacing="0">
  71. <tr><td align="left" valign="top">&bull; <a href="#Overview" accesskey="1">Overview</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Array programming and C++.
  72. </td></tr>
  73. <tr><td align="left" valign="top">&bull; <a href="#Usage" accesskey="2">Usage</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Everything you can do with <code>ra::</code>.
  74. </td></tr>
  75. <tr><td align="left" valign="top">&bull; <a href="#Extras" accesskey="3">Extras</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Additional libraries provided with <code>ra::</code>.
  76. </td></tr>
  77. <tr><td align="left" valign="top">&bull; <a href="#Hazards" accesskey="4">Hazards</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">User beware.
  78. </td></tr>
  79. <tr><td align="left" valign="top">&bull; <a href="#Internals" accesskey="5">Internals</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">For all the world to see.
  80. </td></tr>
  81. <tr><td align="left" valign="top">&bull; <a href="#The-future" accesskey="6">The future</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Could be even better.
  82. </td></tr>
  83. <tr><td align="left" valign="top">&bull; <a href="#Reference" accesskey="7">Reference</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Systematic list of types and functions.
  84. </td></tr>
  85. <tr><td align="left" valign="top">&bull; <a href="#Sources" accesskey="8">Sources</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">It&rsquo;s been done before.
  86. </td></tr>
  87. <tr><td align="left" valign="top">&bull; <a href="#Indices" accesskey="9">Indices</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Or try the search function.
  88. </td></tr>
  89. <tr><td align="left" valign="top">&bull; <a href="#Notes">Notes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Technically...
  90. </td></tr>
  91. </table>
  92. <hr>
  93. <a name="Overview"></a>
  94. <div class="header">
  95. <p>
  96. Next: <a href="#Usage" accesskey="n" rel="next">Usage</a>, Previous: <a href="#Top" accesskey="p" rel="prev">Top</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  97. </div>
  98. <a name="Overview-1"></a>
  99. <h2 class="chapter">1 Overview</h2>
  100. <p>A multidimensional array is a container whose elements can be looked up using a multi-index (i₀, i₁, ...). Each of the indices i₀, i₁, ... has a constant range [0, n₀), [0, n₁), ... independent of the values of the other indices, so the array is ‘rectangular’. The number of indices in the multi-index is the <em>rank</em> of the array, and the list (n₀, n₁, ... nᵣ₋₁) is the <em>shape</em> of the array. We speak of a rank-<em>r</em> array or of an <em>r</em>-array.
  101. </p>
  102. <p>Often we deal with multidimensional <em>expressions</em> where the elements aren&rsquo;t stored anywhere, but are computed on demand when the expression is looked up. In this general sense, an ‘array’ is just a function of integers with a rectangular domain.
  103. </p>
  104. <p>Arrays (in the form of <em>matrices</em>, <em>vectors</em>, or <em>tensors</em>) are very common objects in math and programming, and it is enormously useful to be able to manipulate arrays as individual entities rather than as aggregates. Not only is
  105. </p>
  106. <pre class="verbatim">A = B+C;
  107. </pre>
  108. <p>much more compact and easier to read than
  109. </p>
  110. <pre class="verbatim">for (int i=0; i!=m; ++i)
  111. for (int j=0; j!=n; ++j)
  112. for (int k=0; k!=p; ++k)
  113. A(i, j, k) = B(i, j, k)+C(i, j, k);
  114. </pre>
  115. <p>but it&rsquo;s also safer and less redundant. For example, the order of the loops may be something you don&rsquo;t really care about.
  116. </p>
  117. <p>However, if array operations are implemented naively, a piece of code such as <code>A=B+C</code> may result in the creation of a temporary to hold <code>B+C</code> which is then assigned to <code>A</code>. Needless to say this is very wasteful if the arrays involved are large.
  118. </p>
  119. <a name="index-Blitz_002b_002b"></a>
  120. <p>Fortunately the problem is almost as old as aggregate data types, and other programming languages have addressed it with optimizations such as <a href="https://en.wikipedia.org/wiki/Loop_fission_and_fusion">‘loop fusion’</a>, ‘drag along’ [<a href="#Sources">Abr70</a>]
  121. , or ‘deforestation’ [<a href="#Sources">Wad90</a>]
  122. . In the C++ context the technique of ‘expression templates’ was pioneered in the late 90s by libraries such as Blitz++ [<a href="#Sources">bli17</a>]
  123. . It works by making <code>B+C</code> into an ‘expression object’ which holds references to its arguments and performs the sum only when its elements are looked up. The compiler removes the temporary expression objects during optimization, so that <code>A=B+C</code> results (in principle) in the same generated code as the complicated loop nest above.
  124. </p>
  125. <table class="menu" border="0" cellspacing="0">
  126. <tr><td align="left" valign="top">&bull; <a href="#Rank-polymorphism" accesskey="1">Rank polymorphism</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">What makes arrays special.
  127. </td></tr>
  128. <tr><td align="left" valign="top">&bull; <a href="#Drag-along-and-beating" accesskey="2">Drag along and beating</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">The basic array optimizations.
  129. </td></tr>
  130. <tr><td align="left" valign="top">&bull; <a href="#Why-C_002b_002b" accesskey="3">Why C++</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">High level, low level.
  131. </td></tr>
  132. <tr><td align="left" valign="top">&bull; <a href="#Guidelines" accesskey="4">Guidelines</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How <code>ra::</code> tries to do things.
  133. </td></tr>
  134. <tr><td align="left" valign="top">&bull; <a href="#Other-libraries" accesskey="5">Other libraries</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Inspiration and desperation.
  135. </td></tr>
  136. </table>
  137. <hr>
  138. <a name="Rank-polymorphism"></a>
  139. <div class="header">
  140. <p>
  141. Next: <a href="#Drag-along-and-beating" accesskey="n" rel="next">Drag along and beating</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  142. </div>
  143. <a name="Rank-polymorphism-1"></a>
  144. <h3 class="section">1.1 Rank polymorphism</h3>
  145. <p><em>Rank polymorphism</em> is the ability to treat an array of rank <em>r</em> as an array of lower rank where the elements are themselves arrays.
  146. </p>
  147. <a name="index-cell"></a>
  148. <a name="index-frame"></a>
  149. <p>For example, think of a matrix A, a 2-array with sizes (n₀, n₁) where the elements A(i₀, i₁) are numbers. If we consider the subarrays (rows) A(0, ...), A(1, ...), ..., A(n₀-1, ...) as individual elements, then we have a new view of A as a 1-array of size n₀ with those rows as elements. We say that the rows A(i₀)≡A(i₀, ...) are the 1-<em>cells</em> of A, and the numbers A(i₀, i₁) are 0-cells of A. For an array of arbitrary rank <em>r</em> the (<em>r</em>-1)-cells of A are called its <em>items</em>. The prefix of the shape (n₀, n₁, ... nₙ₋₁₋ₖ) that is not taken up by the k-cell is called the k-<em>frame</em>.
  150. </p>
  151. <p>An obvious way to store an array in linearly addressed memory is to place its items one after another. So we would store a 3-array as
  152. </p>
  153. <blockquote>
  154. <p>A: [A(0), A(1), ...]
  155. </p></blockquote>
  156. <p>and the items of A(i₀), etc. are in turn stored in the same way, so
  157. </p>
  158. <blockquote>
  159. <p>A: [A(0): [A(0, 0), A(0, 1) ...], ...]
  160. </p></blockquote>
  161. <p>and the same for the items of A(i₀, i₁), etc.
  162. </p>
  163. <blockquote>
  164. <p>A: [[A(0, 0): [A(0, 0, 0), A(0, 0, 1) ...], A(0, 1): [A(0, 1, 0), A(0, 1, 1) ...]], ...]
  165. </p></blockquote>
  166. <a name="index-order_002c-row_002dmajor"></a>
  167. <p>This way to lay out an array in memory is called <em>row-major order</em> or <em>C-order</em>, since it&rsquo;s the default order for built-in arrays in C (see <a href="#Other-libraries">Other libraries</a>). A row-major array A with sizes (n₀, n₁, ... nᵣ₋₁) can be looked up like this:
  168. </p>
  169. <a name="x_002dstrides"></a><blockquote>
  170. <p>A(i₀, i₁, ...) = (storage-of-A) [(((i₀n₁ + i₁)n₂ + i₂)n₃ + ...)+iᵣ₋₁] = (storage-of-A) [o + s₀i₀ + s₁i₁ + ...]
  171. </p></blockquote>
  172. <p>where the numbers (s₀, s₁, ...) are called the <em>strides</em><a name="DOCF2" href="#FOOT2"><sup>2</sup></a>. Note that the ‘linear’ or ‘raveled’ address [o + s₀i₀ + s₁i₁ + ...] is an affine function of (i₀, i₁, ...). If we represent an array as a tuple
  173. </p>
  174. <blockquote>
  175. <p>A ≡ ((storage-of-A), o, (s₀, s₁, ...))
  176. </p></blockquote>
  177. <p>then any affine transformation of the indices can be achieved simply by modifying the numbers (o, (s₀, s₁, ...)), with no need to touch the storage. This includes very common operations such as: <a href="#x_002dtranspose">transposing</a> axes, <a href="#x_002dreverse">reversing</a> the order along an axis, most cases of <a href="#Slicing">slicing</a>, and sometimes even reshaping or tiling the array.
  178. </p>
  179. <p>A basic example is obtaining the i₀-th item of A:
  180. </p>
  181. <blockquote>
  182. <p>A(i₀) ≡ ((storage-of-A), o+s₀i₀, (s₁, ...))
  183. </p></blockquote>
  184. <p>Note that we can iterate over these items by simply bumping the pointer o+s₀i₀. This means that iterating over (k&gt;0)-cells doesn&rsquo;t cost any more than iterating over 0-cells (see <a href="#Cell-iteration">Cell iteration</a>).
  185. </p>
  186. <hr>
  187. <a name="Drag-along-and-beating"></a>
  188. <div class="header">
  189. <p>
  190. Next: <a href="#Why-C_002b_002b" accesskey="n" rel="next">Why C++</a>, Previous: <a href="#Rank-polymorphism" accesskey="p" rel="prev">Rank polymorphism</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  191. </div>
  192. <a name="Drag-along-and-beating-1"></a>
  193. <h3 class="section">1.2 Drag along and beating</h3>
  194. <p>These two fundamental array optimizations are described in [<a href="#Sources">Abr70</a>]
  195. .
  196. </p>
  197. <p><em>Drag-along</em> is the process that delays evaluation of array operations. Expression templates can be seen as an implementation of drag-along. Drag-along isn&rsquo;t an optimization in and of itself; it simply preserves the necessary information up to the point where the expression can be executed efficiently.
  198. </p>
  199. <p><em>Beating</em> is the implementation of certain array operations on the array <a href="#Containers-and-views">view</a> descriptor instead of on the array contents. For example, if <code>A</code> is a 1-array, one can implement <a href="#x_002dreverse"><code>reverse(A, 0)</code></a> by negating the <a href="#x_002dstrides">stride</a> and moving the offset to the other end of the array, without having to move any elements. More generally, beating applies to any function-of-indices (generator) that can take the place of an array in an array expression. For instance, an expression such as <a href="#x_002diota"><code>1+iota(3, 0)</code></a> can be beaten into <code>iota(3, 1)</code>, and this can enable further optimizations.
  200. </p>
  201. <hr>
  202. <a name="Why-C_002b_002b"></a>
  203. <div class="header">
  204. <p>
  205. Next: <a href="#Guidelines" accesskey="n" rel="next">Guidelines</a>, Previous: <a href="#Drag-along-and-beating" accesskey="p" rel="prev">Drag along and beating</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  206. </div>
  207. <a name="Why-C_002b_002b-1"></a>
  208. <h3 class="section">1.3 Why C++</h3>
  209. <p>Of course the main reason is that (this being a personal project) I&rsquo;m more familiar with C++ than with other languages to which the following might apply.
  210. </p>
  211. <p>C++ supports the low level control that is necessary for interoperation with external libraries and languages, but still has the abstraction power to create the features we want even though the language has no native support for most of them.
  212. </p>
  213. <a name="index-APL"></a>
  214. <a name="index-J"></a>
  215. <p>The classic array languages, APL [<a href="#Sources">FI73</a>]
  216. and J [<a href="#Sources">Ric08</a>]
  217. , have array support baked in. The same is true for other languages with array facilities such as Fortran or Octave/Matlab. Array libraries for general purpose languages usually depend heavily on C extensions. In Numpy&rsquo;s case [<a href="#Sources">num17</a>]
  218. this is both for reasons of flexibility (e.g. to obtain predictable memory layout and machine types) and of performance.
  219. </p>
  220. <p>On the other extreme, an array library for C would be hampered by the limited means of abstraction in the language (no polymorphism, no metaprogramming, etc.) so the natural choice of C programmers is to resort to code generators, which eventually turn into new languages.
  221. </p>
  222. <p>In C++, a library is enough.
  223. </p>
  224. <hr>
  225. <a name="Guidelines"></a>
  226. <div class="header">
  227. <p>
  228. Next: <a href="#Other-libraries" accesskey="n" rel="next">Other libraries</a>, Previous: <a href="#Why-C_002b_002b" accesskey="p" rel="prev">Why C++</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  229. </div>
  230. <a name="Guidelines-1"></a>
  231. <h3 class="section">1.4 Guidelines</h3>
  232. <p><code>ra::</code> attempts to be general, consistent, and transparent.
  233. </p>
  234. <p>Generality is achieved by removing arbitrary restrictions and by adopting the rank extension mechanism of J. <code>ra::</code> supports array operations with an arbitrary number of arguments. Any of the arguments in an array expression can be read from or written to. Arrays or array expressions can be of any rank. Slicing operations work for subscripts of any rank, as in APL. You can use your own types as array elements.
  235. </p>
  236. <p>Consistency is achieved by having a clear set of concepts and having the realizations of those concepts adhere to the concept as closely as possible. <code>ra::</code> offers a few different types of views and containers, but it should be possible to use them interchangeably whenever the properties that justify their existence are not involved. When this isn&rsquo;t possible, it&rsquo;s a bug. For example, it used to be the case that you couldn&rsquo;t create a higher rank iterator on a <code>SmallView</code>, even though you could do it on a <code>View</code>; this was a bug.
  237. </p>
  238. <p>Sometimes consistency requires a choice. For example, given array views A and B, <code>A=B</code> copies the contents of view B into view A. To change view A instead (to treat A as a pointer) would be the default meaning of A=B for C++ types, and result in better consistency with the rest of the language, but I have decided that having consistency between views and containers (which ‘are’ their contents in a sense that views aren&rsquo;t) is more important.
  239. </p>
  240. <p>Transparency is achieved by avoiding opaque types. An array view consists of a pointer and a list of strides and I see no point in hiding that. Manipulating the strides directly is often useful. A container consists of storage and a view and that isn&rsquo;t hidden either. Some of the types have an obscure implementation but I consider that a defect. Ideally you should be able to rewrite expressions on the fly, or plug in your own traversal methods or storage handling.
  241. </p>
  242. <p>That isn&rsquo;t to mean that you need to be in command of a lot of internal detail to be able to use the library. I hope to have provided a high level interface to most operations and a reasonably sweet syntax. However, transparency is critical to achieve interoperation with external libraries and languages. When you need to, you&rsquo;ll be able to guarantee that an array is stored by compact columns or that the real parts are interleaved with the imaginary parts.
  243. </p>
  244. <hr>
  245. <a name="Other-libraries"></a>
  246. <div class="header">
  247. <p>
  248. Previous: <a href="#Guidelines" accesskey="p" rel="prev">Guidelines</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  249. </div>
  250. <a name="Other-array-libraries"></a>
  251. <h3 class="section">1.5 Other array libraries</h3>
  252. <p>Here I try to list the C++ array libraries that I know of, or libraries that I think deserve a mention for the way they deal with arrays. It is not an extensive review, since I have only used a few of these libraries myself. Please follow the links if you want to be properly informed.
  253. </p>
  254. <p>Since the C++ standard library doesn&rsquo;t offer a standard multidimensional array type, some libraries for specific tasks (linear algebra operations, finite elements, optimization) offer an accessory array library, which may be more or less general. Other libraries have generic array interfaces without needing to provide an array type. FFTW is a good example, maybe because it isn&rsquo;t C++!
  255. </p>
  256. <a name="Standard-C_002b_002b"></a>
  257. <h4 class="subsection">1.5.1 Standard C++</h4>
  258. <p>The C++ language offers multidimensional arrays as a legacy feature from C, e.g. <code>int a[3][4]</code>. These decay to pointers when you do nearly anything with them, don&rsquo;t know their own sizes or rank at runtime, and are generally too limited.
  259. </p>
  260. <p>The C++ standard library also offers a number of contiguous storage containers that can be used as 1-arrays: <code>&lt;array&gt;</code>, <code>&lt;vector&gt;</code> and <code>&lt;valarray&gt;</code>. Neither supports higher ranks out of the box, but <code>&lt;valarray&gt;</code> offers array operations for 1-arrays. <code>ra::</code> makes use of <code>&lt;array&gt;</code> and <code>&lt;vector&gt;</code> for storage and bootstrapping.
  261. </p>
  262. <p><code>ra::</code> accepts built-in arrays and standard library types as array objects (see <a href="#Compatibility">Compatibility</a>).
  263. </p>
  264. <a name="Blitz_002b_002b"></a>
  265. <h4 class="subsection">1.5.2 Blitz++</h4>
  266. <a name="index-Blitz_002b_002b-1"></a>
  267. <p>Blitz++ [<a href="#Sources">bli17</a>]
  268. pioneered the use of expression templates in C++. It supported higher rank arrays, as high as it was practical in C++98, but not dynamic rank. It also supported small arrays with compile time sizes (<code>Tiny</code>), and convenience features such as Fortran-order constructors and arbitrary lower bounds for the array indices (both of which <code>ra::</code> chooses not to support). It placed a strong emphasis on performance, with array traversal methods such as blocking, space filling curves, etc.
  269. </p>
  270. <p>To date it remains, I believe, one of the most general array libraries for C++. However, the implementation had to fight the limitations of C++98, and it offered no general rank extension mechanism.
  271. </p>
  272. <p>One important difference between Blitz++ and <code>ra::</code> is that Blitz++&rsquo;s arrays were reference counted. <code>ra::</code> doesn&rsquo;t do any memory management on its own: the default container types are explicitly values (data-owning) or views. You can select your own storage for the data-owning objects, including reference-counted storage (<code>ra::</code> declares a type using <code>std::shared_ptr</code>), but this is not the default.
  273. </p>
  274. <a name="Other-C_002b_002b-libraries"></a>
  275. <h4 class="subsection">1.5.3 Other C++ libraries</h4>
  276. <p>I guess this is important enough!
  277. </p>
  278. <a name="Other-languages"></a>
  279. <h4 class="subsection">1.5.4 Other languages</h4>
  280. <p>TODO Maybe review other languages, at least the big ones (Fortran/APL/J/Matlab/Numpy).
  281. </p>
  282. <hr>
  283. <a name="Usage"></a>
  284. <div class="header">
  285. <p>
  286. Next: <a href="#Extras" accesskey="n" rel="next">Extras</a>, Previous: <a href="#Overview" accesskey="p" rel="prev">Overview</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  287. </div>
  288. <a name="Usage-1"></a>
  289. <h2 class="chapter">2 Usage</h2>
  290. <p>This is an extended exposition of the features of <code>ra::</code> and is probably best read in order. For details on specific functions or types, please see <a href="#Reference">Reference</a>.
  291. </p>
  292. <table class="menu" border="0" cellspacing="0">
  293. <tr><td align="left" valign="top">&bull; <a href="#Using-the-library" accesskey="1">Using the library</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top"><code>ra::</code> is a header-only library.
  294. </td></tr>
  295. <tr><td align="left" valign="top">&bull; <a href="#Containers-and-views" accesskey="2">Containers and views</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Data objects.
  296. </td></tr>
  297. <tr><td align="left" valign="top">&bull; <a href="#Array-operations" accesskey="3">Array operations</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Building and traversing expressions.
  298. </td></tr>
  299. <tr><td align="left" valign="top">&bull; <a href="#Rank-extension" accesskey="4">Rank extension</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How array operands are matched.
  300. </td></tr>
  301. <tr><td align="left" valign="top">&bull; <a href="#Cell-iteration" accesskey="5">Cell iteration</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">At any rank.
  302. </td></tr>
  303. <tr><td align="left" valign="top">&bull; <a href="#Slicing" accesskey="6">Slicing</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Subscripting is a special operation.
  304. </td></tr>
  305. <tr><td align="left" valign="top">&bull; <a href="#Special-objects" accesskey="7">Special objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Not arrays, yet arrays.
  306. </td></tr>
  307. <tr><td align="left" valign="top">&bull; <a href="#The-rank-conjunction" accesskey="8">The rank conjunction</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">J comes to C++.
  308. </td></tr>
  309. <tr><td align="left" valign="top">&bull; <a href="#Compatibility" accesskey="9">Compatibility</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">With the STL and other libraries.
  310. </td></tr>
  311. <tr><td align="left" valign="top">&bull; <a href="#Extension">Extension</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Using your own types and more.
  312. </td></tr>
  313. <tr><td align="left" valign="top">&bull; <a href="#Functions">Functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Ready to go.
  314. </td></tr>
  315. <tr><td align="left" valign="top">&bull; <a href="#Error-handling">Error handling</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">What to check and what to do.
  316. </td></tr>
  317. </table>
  318. <hr>
  319. <a name="Using-the-library"></a>
  320. <div class="header">
  321. <p>
  322. Next: <a href="#Containers-and-views" accesskey="n" rel="next">Containers and views</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  323. </div>
  324. <a name="Using-ra_003a_003a"></a>
  325. <h3 class="section">2.1 Using <code>ra::</code></h3>
  326. <p><code>ra::</code> is a header only library with no dependencies, so you just need to place the &lsquo;<samp>ra/</samp>&rsquo; folder somewhere in your include path and add <code>#include &quot;ra/ra.hh&quot;</code> at the top of your sources.
  327. </p>
  328. <p>A compiler with C++20 support is required. At the time of writing this means <b>gcc 10.1</b> with <samp>-std=c++20</samp>. Check the top README.md for more up-to-date information.
  329. </p>
  330. <p>Here is a minimal program<a name="DOCF3" href="#FOOT3"><sup>3</sup></a>:
  331. </p>
  332. <div class="example">
  333. <pre class="verbatim">#include &quot;ra/ra.hh&quot;
  334. #include &lt;iostream&gt;
  335. int main()
  336. {
  337. ra::Big&lt;char, 2&gt; A({2, 5}, &quot;helloworld&quot;);
  338. std::cout &lt;&lt; ra::noshape &lt;&lt; format_array(transpose&lt;1, 0&gt;(A), &quot;|&quot;) &lt;&lt; std::endl;
  339. }
  340. </pre><pre class="example">-| h|w
  341. e|o
  342. l|r
  343. l|l
  344. d|d
  345. </pre></div>
  346. <p>You may want to <code>#include &quot;ra/real.hh&quot;</code> and <code>&quot;ra/complex.hh&quot;</code>. These put some functions in the global namespace that make it easier to work on built-in scalar types or array expressions indistinctly. They are not required for the rest of the library to function.
  347. </p>
  348. <a name="index-container"></a>
  349. <hr>
  350. <a name="Containers-and-views"></a>
  351. <div class="header">
  352. <p>
  353. Next: <a href="#Array-operations" accesskey="n" rel="next">Array operations</a>, Previous: <a href="#Using-the-library" accesskey="p" rel="prev">Using the library</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  354. </div>
  355. <a name="Containers-and-views-1"></a>
  356. <h3 class="section">2.2 Containers and views</h3>
  357. <p><code>ra::</code> offers two kinds of data objects. The first kind, the <em>container</em>, owns its data. Creating a container requires memory and destroying it causes that memory to be freed.
  358. </p>
  359. <p>There are three kinds of containers: static size, static rank/dynamic size, and dynamic rank. Here static means ‘compile time constant’ while dynamic means ‘run time constant’. Some dynamic size arrays can be resized but dynamic rank arrays cannot normally have their rank changed. Instead, you create a new container or view with the rank you want.
  360. </p>
  361. <p>For example:
  362. </p>
  363. <div class="example">
  364. <pre class="verbatim">{
  365. ra::Small&lt;double, 2, 3&gt; a(0.); // a static size 2x3 array
  366. ra::Big&lt;double, 2&gt; b({2, 3}, 0.); // a dynamic size 2x3 array
  367. ra::Big&lt;double&gt; c({2, 3}, 0.); // a dynamic rank 2x3 array
  368. // a, b, c destroyed at end of scope
  369. }
  370. </pre></div>
  371. <p>The main reason to have all these different types is performance; the compiler can do a better job when it knows the size or the rank of the array. Also, the sizes of a static size array do not need to be stored in memory, so when you have thousands of small arrays it pays off to use the static size types. Static size or static rank arrays are also safer to use; sometimes <code>ra::</code> will be able to detect errors in the sizes or ranks of array operands at compile time, if the appropriate types are used.
  372. </p>
  373. <p>Container constructors come in two forms. The first form takes a single argument which is copied into the new container. This argument provides shape information if the container type requires it.<a name="DOCF4" href="#FOOT4"><sup>4</sup></a>
  374. </p>
  375. <div class="example">
  376. <pre class="verbatim">using ra::Small, ra::Big;
  377. Small&lt;int, 2, 2&gt; a = {{1, 2}, {3, 4}}; // explicit contents
  378. Big&lt;int, 2&gt; a1 = {{1, 2}, {3, 4}}; // explicit contents
  379. Small&lt;int, 2, 2&gt; a2 = {{1, 2}}; // error: bad size
  380. Small&lt;int, 2, 2&gt; b = 7; // 7 is copied into b
  381. Small&lt;int, 2, 2&gt; c = a; // the contents of a are copied into c
  382. Big&lt;int&gt; d = a; // d takes the shape of a and a is copied into d
  383. Big&lt;int&gt; e = 0; // e is a 0-array with one element f()==0.
  384. </pre></div>
  385. <p>The second form takes two arguments, one giving the shape, the second the contents.
  386. </p>
  387. <a name="index-none"></a>
  388. <a name="index-uninitialized-container"></a>
  389. <div class="example">
  390. <pre class="verbatim">ra::Big&lt;double, 2&gt; a({2, 3}, 1.); // a has size 2x3 and be filled with 1.
  391. ra::Big&lt;double&gt; b({2, 3}, ra::none); // b has size 2x3 and contents don't matter
  392. ra::Big&lt;double&gt; c({2, 3}, a); // c has size 2x3 and a is copied into c
  393. </pre></div>
  394. <p>The last example may result in an error if the shape of <code>a</code> and (2,&nbsp;<!-- /@w -->3) don&rsquo;t match. Here the shape of <code>1.</code> [which is ()] matches (2,&nbsp;<!-- /@w -->3) by a mechanism of rank extension (see <a href="#Rank-extension">Rank extension</a>). The special value <code>ra::none</code> can be used to request <a href="https://en.cppreference.com/w/cpp/language/default_initialization">default initialization</a> of the container&rsquo;s elements.
  395. </p>
  396. <p>When the content argument is a pointer or a 1D brace list, it&rsquo;s handled especially, not for shape<a name="DOCF5" href="#FOOT5"><sup>5</sup></a>, but only as the (row-major) ravel of the content. The pointer constructor is unsafe —use at your own risk!<a name="DOCF6" href="#FOOT6"><sup>6</sup></a>
  397. </p>
  398. <a name="index-order_002c-column_002dmajor"></a>
  399. <div class="example">
  400. <pre class="verbatim">Small&lt;int, 2, 2&gt; aa = {1, 2, 3, 4}; // ravel of the content
  401. ra::Big&lt;double, 2&gt; a({2, 3}, {1, 2, 3, 4, 5, 6}); // same as a = {{1, 2, 3}, {4, 5, 6}}
  402. </pre></div>
  403. <div class="example">
  404. <pre class="verbatim">double bx[6] = {1, 2, 3, 4, 5, 6}
  405. ra::Big&lt;double, 2&gt; b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}
  406. double cx[4] = {1, 2, 3, 4}
  407. ra::Big&lt;double, 2&gt; c({3, 2}, cx); // *** WHO NOSE ***
  408. </pre></div>
  409. <div class="example">
  410. <pre class="verbatim">using sizes = mp::int_list&lt;2, 3&gt;;
  411. using strides = mp::int_list&lt;1, 2&gt;;
  412. ra::SmallArray&lt;double, sizes, strides&gt; a {{1, 2, 3}, {4, 5, 6}}; // stored column-major: 1 4 2 5 3 6
  413. </pre></div>
  414. <p>These produce compile time errors:
  415. </p>
  416. <div class="example">
  417. <pre class="verbatim">Big&lt;int, 2&gt; b = {1, 2, 3, 4}; // error: shape cannot be deduced from ravel
  418. Small&lt;int, 2, 2&gt; b = {1, 2, 3, 4 5}; // error: bad size
  419. Small&lt;int, 2, 2&gt; b = {1, 2, 3}; // error: bad size
  420. </pre></div>
  421. <a name="x_002dscalar_002dchar_002dstar"></a><p>Sometimes the pointer constructor gets in the way (see <a href="#x_002dscalar"><code>scalar</code></a>): </p>
  422. <div class="example">
  423. <pre class="verbatim">ra::Big&lt;char const *, 1&gt; A({3}, &quot;hello&quot;); // error: try to convert char to char const *
  424. ra::Big&lt;char const *, 1&gt; A({3}, ra::scalar(&quot;hello&quot;)); // ok, &quot;hello&quot; is a single item
  425. cout &lt;&lt; ra::noshape &lt;&lt; format_array(A, &quot;|&quot;) &lt;&lt; endl;
  426. </pre><pre class="example">-| hello|hello|hello
  427. </pre></div>
  428. <a name="index-view"></a>
  429. <p>A <em>view</em> is similar to a container in that it points to actual data in memory. However, the view doesn&rsquo;t own that data and destroying the view won&rsquo;t affect it. For example:
  430. </p>
  431. <div class="example">
  432. <pre class="verbatim">ra::Big&lt;double&gt; c({2, 3}, 0.); // a dynamic rank 2x3 array
  433. {
  434. auto c1 = c(1); // the second row of array c
  435. // c1 is destroyed here
  436. }
  437. cout &lt;&lt; c(1, 1) &lt;&lt; endl; // ok
  438. </pre></div>
  439. <p>The data accessed through a view is the data of the ‘root’ container, so modifying the former will be reflected in the latter.
  440. </p>
  441. <div class="example">
  442. <pre class="verbatim">ra::Big&lt;double&gt; c({2, 3}, 0.);
  443. auto c1 = c(1);
  444. c1(2) = 9.; // c(1, 2) = 9.
  445. </pre></div>
  446. <p>Just as for containers, there are separate types of views depending on whether the size is known at compile time, the rank is known at compile time but the size is not, or neither the size nor the rank are known at compile time. <code>ra::</code> has functions to create the most common kinds of views:
  447. </p>
  448. <div class="example">
  449. <pre class="verbatim">ra::Big&lt;double&gt; c {{1, 2, 3}, {4, 5, 6}};
  450. auto ct = transpose&lt;1, 0&gt;(c); // {{1, 4}, {2, 5}, {3, 6}}
  451. auto cr = reverse(c, 0); // {{4, 5, 6}, {1, 2, 3}}
  452. </pre></div>
  453. <p>However, views can point to anywhere in memory and that memory doesn&rsquo;t have to belong to an <code>ra::</code> container. For example:
  454. </p>
  455. <div class="example">
  456. <pre class="verbatim">int raw[6] = {1, 2, 3, 4, 5, 6};
  457. ra::View&lt;int&gt; v1({{2, 3}, {3, 1}}, raw); // view with sizes {2, 3} strides {3, 1}
  458. ra::View&lt;int&gt; v2({2, 3}, raw); // same, default C (row-major) strides
  459. </pre></div>
  460. <p>Containers can be treated as views of the same ‘dynamicness’. If you declare a function
  461. </p>
  462. <div class="example">
  463. <pre class="verbatim">void f(ra::View&lt;int, 3&gt; &amp; v);
  464. </pre></div>
  465. <p>you may pass it an object of type <code>ra::Big&lt;int, 3&gt;</code>.
  466. </p>
  467. <hr>
  468. <a name="Array-operations"></a>
  469. <div class="header">
  470. <p>
  471. Next: <a href="#Rank-extension" accesskey="n" rel="next">Rank extension</a>, Previous: <a href="#Containers-and-views" accesskey="p" rel="prev">Containers and views</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  472. </div>
  473. <a name="Array-operations-1"></a>
  474. <h3 class="section">2.3 Array operations</h3>
  475. <p>To apply an operation to each element of an array, use the function <code>for_each</code>. The array is traversed in an order that is decided by the library.
  476. </p>
  477. <div class="example">
  478. <pre class="verbatim">ra::Small&lt;double, 2, 3&gt; a = {{1, 2, 3}, {4, 5, 6}};
  479. double s = 0.;
  480. for_each([&amp;s](auto &amp;&amp; a) { s+=a; }, a);
  481. </pre><pre class="example">&rArr; s = 21.
  482. </pre></div>
  483. <p>To construct an array expression but stop short of traversing it, use the function <code>map</code>. The expression will be traversed when it is assigned to a view, printed out, etc.
  484. </p>
  485. <div class="example">
  486. <pre class="verbatim">using T = ra::Small&lt;double, 2, 2&gt;;
  487. T a = {{1, 2}, {3, 4}};
  488. T b = {{10, 20}, {30, 40}};
  489. T c = map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; }, a, b); // (1)
  490. </pre><pre class="example">&rArr; c = {{11, 22}, {33, 44}}
  491. </pre></div>
  492. <p>Expressions may take any number of arguments and be nested arbitrarily.
  493. </p>
  494. <div class="example">
  495. <pre class="verbatim">T d = 0;
  496. for_each([](auto &amp;&amp; a, auto &amp;&amp; b, auto &amp;&amp; d) { d = a+b; },
  497. a, b, d); // same as (1)
  498. for_each([](auto &amp;&amp; ab, auto &amp;&amp; d) { d = ab; },
  499. map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; },
  500. a, b),
  501. d); // same as (1)
  502. </pre></div>
  503. <p>The operator of an expression may return a reference and you may assign to an expression in that case. <code>ra::</code> will complain if the expression is somehow not assignable.
  504. </p>
  505. <div class="example">
  506. <pre class="verbatim">T d = 0;
  507. map([](auto &amp; d) -&gt; decltype(auto) { return d; }, d) // just pass d along
  508. = map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; }, a, b); // same as (1)
  509. </pre></div>
  510. <p><code>ra::</code> defines many shortcuts for common array operations. You can of course just do:
  511. </p>
  512. <div class="example">
  513. <pre class="verbatim">T c = a+b; // same as (1)
  514. </pre></div>
  515. <hr>
  516. <a name="Rank-extension"></a>
  517. <div class="header">
  518. <p>
  519. Next: <a href="#Cell-iteration" accesskey="n" rel="next">Cell iteration</a>, Previous: <a href="#Array-operations" accesskey="p" rel="prev">Array operations</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  520. </div>
  521. <a name="Rank-extension-1"></a>
  522. <h3 class="section">2.4 Rank extension</h3>
  523. <p>Rank extension is the mechanism that allows <code>R+S</code> to be defined even when <code>R</code>, <code>S</code> may have different ranks. The idea is an interpolation of the following basic cases.
  524. </p>
  525. <p>Suppose first that <code>R</code> and <code>S</code> have the same rank. We require that the shapes be the same. Then the shape of <code>R+S</code> will be the same as the shape of either <code>R</code> or <code>S</code> and the elements of <code>R+S</code> will be
  526. </p>
  527. <blockquote>
  528. <p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ᵣ₋₁₎)</code>
  529. </p></blockquote>
  530. <p>where <code>r</code> is the rank of <code>R</code>.
  531. </p>
  532. <p>Now suppose that <code>S</code> has rank 0. The shape of <code>R+S</code> is the same as the shape of <code>R</code> and the elements of <code>R+S</code> will be
  533. </p>
  534. <blockquote>
  535. <p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S()</code>.
  536. </p></blockquote>
  537. <p>The two rules above are supported by all primitive array languages, e.g. Matlab [<a href="#Sources">Mat</a>]
  538. . But suppose that <code>S</code> has rank <code>s</code>, where <code>0&lt;s&lt;r</code>. Looking at the expressions above, it seems natural to define <code>R+S</code> by
  539. </p>
  540. <blockquote>
  541. <p><code>(R+S)(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ₛ₋₁₎)</code>.
  542. </p></blockquote>
  543. <p>That is, after we run out of indices in <code>S</code>, we simply repeat the elements. We have aligned the shapes so:
  544. </p>
  545. <blockquote>
  546. <pre class="verbatim">[n₀ n₁ ... n₍ₛ₋₁₎ ... n₍ᵣ₋₁₎]
  547. [n₀ n₁ ... n₍ₛ₋₁₎]
  548. </pre></blockquote>
  549. <a name="index-shape-agreement_002c-prefix"></a>
  550. <a name="index-shape-agreement_002c-suffix"></a>
  551. <a name="index-Numpy"></a>
  552. <p>This rank extension rule is used by the J language [<a href="#Sources">J S</a>]
  553. and is known as <em>prefix agreement</em>. The opposite rule of <em>suffix agreement</em> is used, for example, in Numpy [<a href="#Sources">num17</a>]
  554. <a name="DOCF7" href="#FOOT7"><sup>7</sup></a>.
  555. </p>
  556. <p>As you can verify, the prefix agreement rule is distributive. Therefore it can be applied to nested expressions or to expressions with any number of arguments. It is applied systematically throughout <code>ra::</code>, even in assignments. For example,
  557. </p>
  558. <div class="example">
  559. <pre class="verbatim">ra::Small&lt;int, 3&gt; x {3, 5, 9};
  560. ra::Small&lt;int, 3, 2&gt; a = x; // assign x(i) to each a(i, j)
  561. </pre><pre class="example">&rArr; a = {{3, 3}, {5, 5}, {9, 9}}
  562. </pre></div>
  563. <div class="example">
  564. <pre class="verbatim">ra::Small&lt;int, 3&gt; x(0.);
  565. ra::Small&lt;int, 3, 2&gt; a = {{1, 2}, {3, 4}, {5, 6}};
  566. x += a; // sum the rows of a
  567. </pre><pre class="example">&rArr; x = {3, 7, 11}
  568. </pre></div>
  569. <div class="example">
  570. <pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 3, 3}, ra::_0);
  571. ra::Big&lt;double, 1&gt; b({5}, 0.);
  572. b += transpose&lt;0, 1, 1&gt;(a); // b(i) = ∑ⱼ a(i, j, j)
  573. </pre><pre class="example">&rArr; b = {0, 3, 6, 9, 12}
  574. </pre></div>
  575. <a name="index-Numpy-1"></a>
  576. <a name="index-broadcasting_002c-singleton_002c-newaxis"></a>
  577. <p>An weakness of prefix agreement is that the axes you want to match aren&rsquo;t always the prefix axes. Other array systems offer a feature similar to rank extension called ‘broadcasting’ that is a bit more flexible. For example, in the way it&rsquo;s implemented in Numpy [<a href="#Sources">num17</a>]
  578. , an array of shape [A B 1 D] will match an array of shape [A B C D]. The process of broadcasting consists in inserting so-called ‘singleton dimensions’ (axes with size one) to align the axes that one wishes to match. You can think of prefix agreement as a particular case of broadcasting where the singleton dimensions are added to the end of the shorter shapes automatically.
  579. </p>
  580. <p>A drawback of singleton broadcasting is that it muddles the distinction between a scalar and a vector of size 1. Sometimes, an axis of size 1 is no more than that, and if 2≠3 is a size error, it isn&rsquo;t obvious why 1≠2 shouldn&rsquo;t be. To avoid this problem, <code>ra::</code> supports broadcasting with undefined size axes (see <a href="#x_002dinsert"><code>insert</code></a>).
  581. </p>
  582. <div class="example">
  583. <pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 3}, ra::_0);
  584. ra::Big&lt;double, 1&gt; b({3}, 0.);
  585. ra::Big&lt;double, 3&gt; c({1, 3}, ra::_0);
  586. // b(?, i) += a(j, i) → b(i) = ∑ⱼ a(j, i) (sum columns)
  587. b(ra::insert&lt;1&gt;) += a;
  588. c = a; // 1 ≠ 5, still an agreement error
  589. </pre></div>
  590. <p>Still another way to align array axes is provided by the <a href="#The-rank-conjunction">rank conjunction</a>.
  591. </p>
  592. <p>Even with axis insertion, it is still necessary that the axes one wishes to match are in the same order in all the arguments.
  593. <a href="#x_002dtranspose">Transposing</a> the axes before extension is a possible workaround.
  594. </p>
  595. <hr>
  596. <a name="Cell-iteration"></a>
  597. <div class="header">
  598. <p>
  599. Next: <a href="#Slicing" accesskey="n" rel="next">Slicing</a>, Previous: <a href="#Rank-extension" accesskey="p" rel="prev">Rank extension</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  600. </div>
  601. <a name="Cell-iteration-1"></a>
  602. <h3 class="section">2.5 Cell iteration</h3>
  603. <p><code>map</code> and <code>for_each</code> apply their operators to each element of their arguments; in other words, to the 0-cells of the arguments. But it is possible to specify directly the rank of the cells that one iterates over:
  604. </p>
  605. <div class="example">
  606. <pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 4, 3}, ra::_0);
  607. for_each([](auto &amp;&amp; b) { /* b has shape (5 4 3) */ }, iter&lt;3&gt;(a));
  608. for_each([](auto &amp;&amp; b) { /* b has shape (4 3) */ }, iter&lt;2&gt;(a));
  609. for_each([](auto &amp;&amp; b) { /* b has shape (3) */ }, iter&lt;1&gt;(a));
  610. for_each([](auto &amp;&amp; b) { /* b has shape () */ }, iter&lt;0&gt;(a)); // elements
  611. for_each([](auto &amp;&amp; b) { /* b has shape () */ }, a); // same as iter&lt;0&gt;(a); default
  612. </pre></div>
  613. <p>One may specify the <em>frame</em> rank instead:
  614. </p>
  615. <div class="example">
  616. <pre class="verbatim">for_each([](auto &amp;&amp; b) { /* b has shape () */ }, iter&lt;-3&gt;(a)); // same as iter&lt;0&gt;(a)
  617. for_each([](auto &amp;&amp; b) { /* b has shape (3) */ }, iter&lt;-2&gt;(a)); // same as iter&lt;1&gt;(a)
  618. for_each([](auto &amp;&amp; b) { /* b has shape (4 3) */ }, iter&lt;-1&gt;(a)); // same as iter&lt;2&gt;(a)
  619. </pre></div>
  620. <p>In this way it is possible to match shapes in various ways. Compare
  621. </p>
  622. <div class="example">
  623. <pre class="verbatim">ra::Big&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  624. ra::Big&lt;double, 1&gt; b = {10, 20};
  625. ra::Big&lt;double, 2&gt; c = a * b; // multiply (each item of a) by (each item of b)
  626. </pre><pre class="example">&rArr; a = {{10, 20, 30}, {80, 100, 120}}
  627. </pre></div>
  628. <p>with
  629. </p>
  630. <div class="example">
  631. <pre class="verbatim">ra::Big&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  632. ra::Big&lt;double, 1&gt; b = {10, 20, 30};
  633. ra::Big&lt;double, 2&gt; c({2, 3}, 0.);
  634. iter&lt;1&gt;(c) = iter&lt;1&gt;(a) * iter&lt;1&gt;(b); // multiply (each item of a) by (b)
  635. </pre><pre class="example">&rArr; a = {{10, 40, 90}, {40, 100, 180}}
  636. </pre></div>
  637. <p>Note that in this case we cannot construct <code>c</code> directly from <code>iter&lt;1&gt;(a) * iter&lt;1&gt;(b)</code>, since the constructor for <code>ra::Big</code> matches its argument using (the equivalent of) <code>iter&lt;0&gt;(*this)</code>. See <a href="#x_002diter"><code>iter</code></a> for more examples.
  638. </p>
  639. <p>Cell iteration is appropriate when the operations take naturally operands of rank &gt; 0; for instance, the operation ‘determinant of a matrix’ is naturally of rank 2. When the operation is of rank 0, such as <code>*</code> above, there may be faster ways to rearrange shapes for matching (see <a href="#The-rank-conjunction">The rank conjunction</a>).
  640. </p>
  641. <p>FIXME More examples.
  642. </p>
  643. <hr>
  644. <a name="Slicing"></a>
  645. <div class="header">
  646. <p>
  647. Next: <a href="#Special-objects" accesskey="n" rel="next">Special objects</a>, Previous: <a href="#Cell-iteration" accesskey="p" rel="prev">Cell iteration</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  648. </div>
  649. <a name="Slicing-1"></a>
  650. <h3 class="section">2.6 Slicing</h3>
  651. <p>Slicing is an array extension of the subscripting operation. However, tradition and convenience have given it a special status in most array languages, together with some peculiar semantics that <code>ra::</code> supports.
  652. </p>
  653. <p>The form of the scripting operator <code>A(i₀, i₁, ...)</code> makes it plain that <code>A</code> is a function of <code>rank(A)</code> integer arguments. An array extension is immediately available through <code>map</code>. For example:
  654. </p>
  655. <div class="example">
  656. <pre class="verbatim">ra::Big&lt;double, 1&gt; a = {1., 2., 3., 4.};
  657. ra::Big&lt;int, 1&gt; i = {1, 3};
  658. map(a, i) = 77.;
  659. </pre><pre class="example">&rArr; a = {1., 77., 3, 77.}
  660. </pre></div>
  661. <p>Just as with any use of <code>map</code>, array arguments are subject to the prefix agreement rule.
  662. </p>
  663. <div class="example">
  664. <pre class="verbatim">ra::Big&lt;double, 2&gt; a({2, 2}, {1., 2., 3., 4.});
  665. ra::Big&lt;int, 1&gt; i = {1, 0};
  666. ra::Big&lt;double, 1&gt; b = map(a, i, 0);
  667. </pre><pre class="example">&rArr; b = {3., 1.} // {a(1, 0), a(0, 0)}
  668. </pre></div>
  669. <div class="example">
  670. <pre class="verbatim">ra::Big&lt;int, 1&gt; j = {0, 1};
  671. b = map(a, i, j);
  672. </pre><pre class="example">&rArr; b = {3., 2.} // {a(1, 0), a(0, 1)}
  673. </pre></div>
  674. <p>The latter is a form of sparse subscripting.
  675. </p>
  676. <p>Most array operations (e.g. <code>+</code>) are defined through <code>map</code> in this way. For example, <code>A+B+C</code> is defined as <code>map(+, A, B, C)</code> (or the equivalent <code>map(+, map(+, A, B), C)</code>). Not so for the subscripting operation:
  677. </p>
  678. <div class="example">
  679. <pre class="verbatim">ra::Big&lt;double, 2&gt; A {{1., 2.}, {3., 4.}};
  680. ra::Big&lt;int, 1&gt; i = {1, 0};
  681. ra::Big&lt;int, 1&gt; j = {0, 1};
  682. // {{A(i₀, j₀), A(i₀, j₁)}, {A(i₁, j₀), A(i₁, j₁)}}
  683. ra::Big&lt;double, 2&gt; b = A(i, j);
  684. </pre><pre class="example">&rArr; b = {{3., 4.}, {1., 2.}}
  685. </pre></div>
  686. <p><code>A(i, j, ...)</code> is defined as the <em>outer product</em> of the indices <code>(i, j, ...)</code> with operator <code>A</code>, because this operation sees much more use in practice than <code>map(A, i, j ...)</code>.
  687. </p>
  688. <p>Besides, when the subscripts <code>i, j, ...</code> are scalars or <em>linear ranges</em> (integer sequences of the form <code>(o, o+s, ..., o+s*(n-1))</code>), the subscripting can be performed inmediately at constant cost and without needing to construct an expression object. This optimization is called <a href="#Drag-along-and-beating"><em>beating</em></a>.
  689. </p>
  690. <p><code>ra::</code> isn&rsquo;t smart enough to know when an arbitrary expression might be a linear range, so the following special objects are provided:
  691. </p>
  692. <a name="x_002diota"></a><dl>
  693. <dt><a name="index-iota"></a>Special&nbsp;object<!-- /@w -->: <strong>iota</strong> <em>count [start:0 [step:1]]</em></dt>
  694. <dd><p>Create a linear range <code>start, start+step, ... start+step*(count-1)</code>.
  695. </p></dd></dl>
  696. <p>This can used anywhere an array expression is expected.
  697. </p>
  698. <div class="example">
  699. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = ra::iota(4, 3 -2);
  700. </pre><pre class="example">&rArr; a = {3, 1, -1, -3}
  701. </pre></div>
  702. <p>Here, <code>b</code> and <code>c</code> are <code>View</code>s (see <a href="#Containers-and-views">Containers and views</a>).
  703. </p><div class="example">
  704. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3, 4, 5, 6};
  705. auto b = a(iota(3));
  706. auto c = a(iota(3, 3));
  707. </pre><pre class="example">&rArr; a = {1, 2, 3}
  708. &rArr; a = {4, 5, 6}
  709. </pre></div>
  710. <dl>
  711. <dt><a name="index-all"></a>Special&nbsp;object<!-- /@w -->: <strong>all</strong></dt>
  712. <dd><p>Create a linear range <code>0, 1, ... (nᵢ-1)</code> when used as a subscript for the <code>i</code>-th argument of a subscripting expression.
  713. </p></dd></dl>
  714. <p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
  715. </p>
  716. <div class="example">
  717. <pre class="verbatim">ra::Big&lt;int, 2&gt; a({3, 2}, {1, 2, 3, 4, 5, 6});
  718. auto b = a(ra::all, ra::all); // (1) a view of the whole of a
  719. auto c = a(iota(3), iota(2)); // same as (1)
  720. auto d = a(iota(3), ra::all); // same as (1)
  721. auto e = a(ra:all, iota(2)); // same as (1)
  722. auto f = a(0, ra::all); // first row of a
  723. auto g = a(ra::all, 1); // second column of a
  724. </pre></div>
  725. <p><code>all</code> is a special case (<code>dots&lt;1&gt;</code>) of the more general object <code>dots</code>.
  726. </p>
  727. <dl>
  728. <dt><a name="index-dots_003cn_003e"></a>Special&nbsp;object<!-- /@w -->: <strong>dots&lt;n&gt;</strong></dt>
  729. <dd><p>Equivalent to as many instances of <code>ra::all</code> as indicated by <code>n</code>, which must not be negative. Each instance takes the place of one argument to the subscripting operation.
  730. </p></dd></dl>
  731. <p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
  732. </p>
  733. <div class="example">
  734. <pre class="verbatim">auto h = a(ra::all, ra::all); // same as (1)
  735. auto i = a(ra::all, ra::dots&lt;1&gt;); // same as (1)
  736. auto j = a(ra::dots&lt;2&gt;); // same as (1)
  737. auto k = a(ra::dots&lt;0&gt;, ra::dots&lt;2&gt;); // same as (1)
  738. auto l = a(0, ra::dots&lt;1&gt;); // first row of a
  739. auto m = a(ra::dots&lt;1&gt;, 1); // second column of a
  740. </pre></div>
  741. <p>This is useful when writing rank-generic code, see <code>examples/maxwell.cc</code> in the distribution for an example.
  742. </p>
  743. <a name="x_002dinsert"></a><dl>
  744. <dt><a name="index-insert_003cn_003e"></a>Special&nbsp;object<!-- /@w -->: <strong>insert&lt;n&gt;</strong></dt>
  745. <dd><p>Inserts <code>n</code> new axes at the subscript position. <code>n</code> must not be negative.
  746. </p></dd></dl>
  747. <p>The new axes have stride 0 and undefined size, so they will match any size on those axes by repeating items. <code>insert</code> objects cannot stand alone as an array expression. The examples below result in <code>View</code> objects:
  748. </p>
  749. <div class="example">
  750. <pre class="verbatim">auto h = a(insert&lt;0&gt;); // same as (1)
  751. auto k = a(insert&lt;1&gt;); // shape [undefined, 3, 2]
  752. </pre></div>
  753. <a name="index-broadcasting_002c-singleton_002c-Numpy"></a>
  754. <p><code>insert&lt;n&gt;</code> main use is to prepare arguments for broadcasting. In other array systems (e.g. Numpy) broadcasting is done with singleton dimensions, that is, dimensions of size one match dimensions of any size. In <code>ra::</code> singleton dimensions aren&rsquo;t special, so broadcasting requires the use of <code>insert</code>. For example: </p>
  755. <div class="example">
  756. <pre class="verbatim">ra::Big&lt;int, 1&gt; x = {1, 10};
  757. // match shapes [2, U, U] with [U, 3, 2] to produce [2, 3, 2]
  758. cout &lt;&lt; x(ra::all, ra::insert&lt;2&gt;) * a(insert&lt;1&gt;) &lt;&lt; endl;
  759. </pre><pre class="example">-| 2 3 2
  760. 1 2
  761. 3 4
  762. 5 6
  763. 10 20
  764. 30 40
  765. 50 60
  766. </pre></div>
  767. <p>Here&rsquo;s a way to perform the outer product of two <code>Views</code> of static rank (but see <a href="#x_002dfrom"><code>from</code></a> for a more general way):
  768. </p><div class="example">
  769. <pre class="verbatim">cout &lt;&lt; (a(ra::dots&lt;a.rank()&gt;, ra::insert&lt;b.rank()&gt;) * b(ra::insert&lt;a.rank()&gt;, ra::dots&lt;b.rank()&gt;)) &lt;&lt; endl;
  770. // same thing by prefix matching
  771. cout &lt;&lt; (a * b(ra::insert&lt;a.rank()&gt;)) &lt;&lt; endl;
  772. </pre></div>
  773. <p>In addition to the special objects listed above, you can also omit any trailing <code>ra::all</code> subscripts. For example:
  774. </p>
  775. <div class="example">
  776. <pre class="verbatim">ra::Big&lt;int, 3&gt; a({2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8});
  777. auto a0 = a(0); // same as a(0, ra::all, ra::all)
  778. auto a10 = a(1, 0); // same as a(1, 0, ra::all)
  779. </pre><pre class="example">&rArr; a0 = {{1, 2}, {3, 4}}
  780. &rArr; a10 = {5, 6}
  781. </pre></div>
  782. <p>This supports the notion (see <a href="#Rank-polymorphism">Rank polymorphism</a>) that a 3-array is also an 2-array where the elements are 1-arrays themselves, or a 1-array where the elements are 2-arrays. This important property is directly related to the mechanism of rank extension (see <a href="#Rank-extension">Rank extension</a>).
  783. </p>
  784. <a name="Subscripting-and-rank_002d0-views"></a>
  785. <h4 class="subsection">2.6.1 Subscripting and rank-0 views</h4>
  786. <a name="index-view_002c-rank-0"></a>
  787. <a name="index-rank_002c-dynamic"></a>
  788. <a name="index-rank_002c-static"></a>
  789. <p>When the result of the subscripting operation would have rank 0, the type returned is the type of the view <em>element</em> and not a rank-0 view as long as the rank of the result can be determined at compile time. When that&rsquo;s not possible (for instance, the subscripted view has dynamic rank) then a rank-0 view is returned instead. An automatic conversion is defined for rank-0 views, but manual conversion may be needed in some contexts.
  790. </p>
  791. <div class="example">
  792. <pre class="verbatim">using T = std::complex&lt;double&gt;;
  793. int f(T &amp;);
  794. Big&lt;T, 2&gt; a({2, 3}, 0); // static rank
  795. Big&lt;T&gt; b({2, 3}, 0); // dynamic rank
  796. cout &lt;&lt; a(0, 0).real_part() &lt;&lt; endl; // ok, a(0, 0) returns complex &amp;
  797. // cout &lt;&lt; b(0, 0).real_part() &lt;&lt; endl; // error, View&lt;T&gt; has no member real_part
  798. cout &lt;&lt; ((T &amp;)(b(0, 0))).real_part() &lt;&lt; endl; // ok, manual conversion to T &amp;
  799. cout &lt;&lt; f(b(0, 0)) &lt;&lt; endl; // ok, automatic conversion from View&lt;T&gt; to T &amp;
  800. // cout &lt;&lt; f(a(0)) &lt;&lt; endl; // compile time error, conversion failed since static rank of a(0) is not 0
  801. // cout &lt;&lt; f(b(0)) &lt;&lt; endl; // runtime error, conversion failed since dynamic rank of b(0) is not 0
  802. </pre></div>
  803. <hr>
  804. <a name="Special-objects"></a>
  805. <div class="header">
  806. <p>
  807. Next: <a href="#The-rank-conjunction" accesskey="n" rel="next">The rank conjunction</a>, Previous: <a href="#Slicing" accesskey="p" rel="prev">Slicing</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  808. </div>
  809. <a name="Special-objects-1"></a>
  810. <h3 class="section">2.7 Special objects</h3>
  811. <dl>
  812. <dt><a name="index-TensorIndex_003cn_002c"></a>Special&nbsp;object<!-- /@w -->: <strong>TensorIndex&lt;n,</strong> <em>integer_type=ra::dim_t&gt;</em></dt>
  813. <dd><p><code>TensorIndex&lt;n&gt;</code> represents the <code>n</code>-th index of an array expression. <code>TensorIndex&lt;n&gt;</code> is itself an array expression of rank <code>n</code>-1 and size undefined. It must be used with other terms whose dimensions are defined, so that the overall shape of the array expression can be determined.
  814. </p>
  815. <p><code>ra::</code> offers the shortcut <code>ra::_0</code> for <code>ra::TensorIndex&lt;0&gt;{}</code>, etc.
  816. </p></dd></dl>
  817. <div class="example">
  818. <pre class="verbatim">ra::Big&lt;int, 1&gt; v = {1, 2, 3};
  819. cout &lt;&lt; (v - ra::_0) &lt;&lt; endl; // { 1-0, 2-1, 3-2 }
  820. // cout &lt;&lt; (ra::_0) &lt;&lt; endl; // error: TensorIndex cannot drive array expression
  821. // cout &lt;&lt; (v - ra::_1) &lt;&lt; endl; // error: TensorIndex cannot drive array expression
  822. ra::Big&lt;int, 2&gt; a({3, 2}, 0);
  823. cout &lt;&lt; (a + ra::_0 - ra::_1) &lt;&lt; endl; // {{0, -1, -2}, {1, 0, -1}, {2, 1, 0}}
  824. </pre></div>
  825. <hr>
  826. <a name="The-rank-conjunction"></a>
  827. <div class="header">
  828. <p>
  829. Next: <a href="#Compatibility" accesskey="n" rel="next">Compatibility</a>, Previous: <a href="#Special-objects" accesskey="p" rel="prev">Special objects</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  830. </div>
  831. <a name="The-rank-conjunction-1"></a>
  832. <h3 class="section">2.8 The rank conjunction</h3>
  833. <p>We have seen in <a href="#Cell-iteration">Cell iteration</a> that it is possible to treat an r-array as an array of lower rank with subarrays as its elements. With the <a href="#x_002diter"><code>iter&lt;cell rank&gt;</code></a> construction, this ‘exploding’ is performed (notionally) on the argument; the operation of the array expression is applied blindly to these cells, whatever they turn out to be.
  834. </p>
  835. <div class="example">
  836. <pre class="verbatim">for_each(my_sort, iter&lt;1&gt;(A)); // (in ra::) my_sort is a regular function, cell rank must be given
  837. for_each(my_sort, iter&lt;0&gt;(A)); // (in ra::) error, bad cell rank
  838. </pre></div>
  839. <p>The array language J has instead the concept of <em>verb rank</em>. Every function (or <em>verb</em>) has an associated cell rank for each of its arguments. Therefore <code>iter&lt;cell rank&gt;</code> is not needed.
  840. </p>
  841. <div class="example">
  842. <pre class="verbatim">for_each(sort_rows, A); // (not in ra::) will iterate over 1-cells of A, sort_rows knows
  843. </pre></div>
  844. <p><code>ra::</code> doesn&rsquo;t have ‘verb ranks’ yet. In practice one can think of <code>ra::</code>&rsquo;s operations as having a verb rank of 0. However, <code>ra::</code> supports a limited form of J&rsquo;s <em>rank conjunction</em> with the function <a href="#x_002dwrank"><code>wrank</code></a>.
  845. </p>
  846. <p>This is an operator that takes one verb (such operators are known as <em>adverbs</em> in J) and produces another verb with different ranks. These ranks are used for rank extension through prefix agreement, but then the original verb is used on the cells that result. The rank conjunction can be nested, and this allows repeated rank extension before the innermost operation is applied.
  847. </p>
  848. <p>A standard example is ‘outer product’.
  849. </p>
  850. <div class="example">
  851. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3};
  852. ra::Big&lt;int, 1&gt; b = {40, 50};
  853. ra::Big&lt;int, 2&gt; axb = map(ra::wrank&lt;0, 1&gt;([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }),
  854. a, b)
  855. </pre><pre class="example">&rArr; axb = {{40, 80, 120}, {50, 100, 150}}
  856. </pre></div>
  857. <p>It works like this. The verb <code>ra::wrank&lt;0, 1&gt;([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; })</code> has verb ranks (0, 1), so the 0-cells of <code>a</code> are paired with the 1-cells of <code>b</code>. In this case <code>b</code> has a single 1-cell. The frames and the cell shapes of each operand are:
  858. </p>
  859. <div class="example">
  860. <pre class="verbatim">a: 3 |
  861. b: | 2
  862. </pre></div>
  863. <p>Now the frames are rank-extended through prefix agreement.
  864. </p>
  865. <div class="example">
  866. <pre class="verbatim">a: 3 |
  867. b: 3 | 2
  868. </pre></div>
  869. <p>Now we need to perform the operation on each cell. The verb <code>[](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }</code> has verb ranks (0, 0). This results in the 0-cells of <code>a</code> (which have shape ()) being rank-extended to the shape of the 1-cells of <code>b</code> (which is (2)).
  870. </p>
  871. <div class="example">
  872. <pre class="verbatim">a: 3 | 2
  873. b: 3 | 2
  874. </pre></div>
  875. <p>This use of the rank conjunction is packaged in <code>ra::</code> as the <a href="#x_002dfrom"><code>from</code></a> operator. It supports any number of arguments, not only two.
  876. </p>
  877. <div class="example">
  878. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3};
  879. ra::Big&lt;int, 1&gt; b = {40, 50};
  880. ra::Big&lt;int, 2&gt; axb = from([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }), a, b)
  881. </pre><pre class="example">&rArr; axb = {{40, 80, 120}, {50, 100, 150}}
  882. </pre></div>
  883. <p>Another example is matrix multiplication. For 2-array arguments C, A and B with shapes C: (m, n) A: (m, p) and B: (p, n), we want to perform the operation C(i, j) += A(i, k)*B(k, j). The axis alignment gives us the ranks we need to use.
  884. </p>
  885. <div class="example">
  886. <pre class="verbatim">C: m | | n
  887. A: m | p |
  888. B: | p | n
  889. </pre></div>
  890. <p>First we&rsquo;ll align the first axes of C and A using the cell ranks (1, 1, 2). The cell shapes are:
  891. </p>
  892. <div class="example">
  893. <pre class="verbatim">C: m | n
  894. A: m | p
  895. B: | p n
  896. </pre></div>
  897. <p>Then we&rsquo;ll use the ranks (1, 0, 1) on the cells:
  898. </p>
  899. <div class="example">
  900. <pre class="verbatim">C: m | | n
  901. A: m | p |
  902. B: | p | n
  903. </pre></div>
  904. <p>The final operation is a standard operation on arrays of scalars. In actual <code>ra::</code> syntax:
  905. </p>
  906. <div class="example">
  907. <pre class="verbatim">ra::Big A({3, 2}, {1, 2, 3, 4, 5, 6});
  908. ra::Big B({2, 3}, {7, 8, 9, 10, 11, 12});
  909. ra::Big C({3, 3}, 0.);
  910. for_each(ra::wrank&lt;1, 1, 2&gt;(ra::wrank&lt;1, 0, 1&gt;([](auto &amp;&amp; c, auto &amp;&amp; a, auto &amp;&amp; b) { c += a*b; })), C, A, B);
  911. </pre><pre class="example">&rArr; C = {{27, 30, 33}, {61, 68, 75}, {95, 106, 117}}
  912. </pre></div>
  913. <p>Note that <code>wrank</code> cannot be used to transpose axes in general.
  914. </p>
  915. <p>I hope that in the future something like <code>C(i, j) += A(i, k)*B(k, j)</code>, where <code>i, j, k</code> are special objects, can be automatically translated to the requisite combination of <code>wrank</code> and perhaps also <a href="#x_002dtranspose"><code>transpose</code></a>. For the time being, you have to align or transpose the axes yourself.
  916. </p>
  917. <hr>
  918. <a name="Compatibility"></a>
  919. <div class="header">
  920. <p>
  921. Next: <a href="#Extension" accesskey="n" rel="next">Extension</a>, Previous: <a href="#The-rank-conjunction" accesskey="p" rel="prev">The rank conjunction</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  922. </div>
  923. <a name="Compatibility-1"></a>
  924. <h3 class="section">2.9 Compatibility</h3>
  925. <a name="Using-other-C-and-C_002b_002b-types-with-ra_003a_003a"></a>
  926. <h4 class="subsection">2.9.1 Using other C and C++ types with <code>ra::</code></h4>
  927. <a name="index-foreign-type"></a>
  928. <p><code>ra::</code> accepts certain types from outside <code>ra::</code> (<em>foreign types</em>) as array expressions. Generally it is enough to mix the foreign type with a type from <code>ra::</code> and let deduction work.
  929. </p>
  930. <div class="example">
  931. <pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
  932. ra::Small&lt;int, 3&gt; y = {6, 5, 4};
  933. cout &lt;&lt; (x-y) &lt;&lt; endl;
  934. </pre><pre class="example">-| -5 -3 -1
  935. </pre></div>
  936. <a name="index-start"></a>
  937. <p>Foreign types can be brought into <code>ra::</code> explicitly with the function <a href="#x_002dstart"><code>start</code></a>.
  938. </p>
  939. <div class="example">
  940. <pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
  941. // cout &lt;&lt; sum(x) &lt;&lt; endl; // error, sum not found
  942. cout &lt;&lt; sum(ra::start(x)) &lt;&lt; endl;
  943. cout &lt;&lt; ra::sum(x) &lt;&lt; endl;
  944. </pre><pre class="example">-| 6
  945. 6
  946. </pre></div>
  947. <p>The following types are accepted as foreign types:
  948. </p>
  949. <ul>
  950. <li> <code>std::vector</code>
  951. produces an expression of rank 1 and dynamic size.
  952. </li><li> <code>std::array</code>
  953. produces an expression of rank 1 and static size.
  954. </li><li> Built-in arrays <a name="index-built_002din-array"></a>
  955. produce an expression of positive rank and static size.
  956. </li><li> Raw pointers
  957. produce an expression of rank 1 and <em>undefined</em> size. Raw pointers must be brought into <code>ra::</code> explicitly with the function <a href="#x_002dptr"><code>ptr</code></a>.
  958. </li></ul>
  959. <p>Compare:
  960. </p>
  961. <div class="example">
  962. <pre class="verbatim">int p[] = {1, 2, 3};
  963. int * z = p;
  964. ra::Big&lt;int, 1&gt; q {1, 2, 3};
  965. q += p; // ok, q is ra::, p is foreign object with size info
  966. ra::start(p) += q; // can't redefine operator+=(int[]), foreign needs ra::start()
  967. // z += q; // error: raw pointer needs ra::ptr()
  968. ra::ptr(z) += p; // ok, size is determined by foreign object p
  969. </pre></div>
  970. <a name="x_002dis_002dscalar"></a><p>Some types are accepted automatically as scalars. These include:
  971. </p><ul>
  972. <li> Any type <code>T</code> for which <code>std::is_scalar_v&lt;T&gt;</code> is true, <em>except</em> pointers. These include <code>char</code>, <code>int</code>, <code>double</code>, etc.
  973. </li><li> <code>std::complex&lt;T&gt;</code>, if you import <code>ra/complex.hh</code>.
  974. </li></ul>
  975. <p>You can add your own types as scalar types with the following declaration (see <code>ra/complex.hh</code>):
  976. </p>
  977. <pre class="verbatim"> namespace ra { template &lt;&gt; constexpr bool is_scalar_def&lt;MYTYPE&gt; = true; }
  978. </pre>
  979. <p>Otherwise, you can bring a scalar object into <code>ra::</code> on the spot, with the function <a href="#x_002dscalar"><code>scalar</code></a>.
  980. </p>
  981. <a name="Using-ra_003a_003a-types-with-the-STL"></a>
  982. <h4 class="subsection">2.9.2 Using <code>ra::</code> types with the STL</h4>
  983. <p>General <code>ra::</code> <a href="#Containers-and-views">views</a> provide STL compatible <code>ForwardIterator</code>s through the members <code>begin()</code> and <code>end()</code>. These iterators traverse the elements of the array (0-cells) in row major order, regardless of the internal order of the view.
  984. </p>
  985. <p>For <a href="#Containers-and-views">containers</a> <code>begin()</code> provides <code>RandomAccessIterator</code>s, which is handy for certain functions such as <code>sort</code>. There&rsquo;s no reason why all views couldn&rsquo;t provide <code>RandomAccessIterator</code>, but these wouldn&rsquo;t be efficient in general for ranks above 1, and I haven&rsquo;t implemented them. The container <code>RandomAccessIterator</code>s that are provided are in fact raw pointers.
  986. </p>
  987. <div class="example">
  988. <pre class="verbatim">ra::Big&lt;int&gt; x {3, 2, 1}; // x is a Container
  989. auto y = x(); // y is a View on x
  990. // std::sort(y.begin(), y.end()); // error: y.begin() is not RandomAccessIterator
  991. std::sort(x.begin(), x.end()); // ok, we know x is stored in row-major order
  992. </pre><pre class="example">&rArr; x = {1, 2, 3}
  993. </pre></div>
  994. <a name="index-other-libraries_002c-interfacing-with"></a>
  995. <a name="Using-ra_003a_003a-types-with-other-libraries"></a>
  996. <h4 class="subsection">2.9.3 Using <code>ra::</code> types with other libraries</h4>
  997. <p>When you have to pass arrays back and forth between your program and an external library, perhaps even another language, it is necessary for both sides to agree on a memory layout. <code>ra::</code> gives you access to its own memory layout and allows you to obtain an <code>ra::</code> view on any type of memory.
  998. </p>
  999. <a name="The-good-array-citizen"></a>
  1000. <h4 class="subsubsection">2.9.3.1 The good array citizen</h4>
  1001. <p>FIXME Put these in examples/ and reference them here.
  1002. </p>
  1003. <a name="index-BLIS"></a>
  1004. <p>The good array citizen describes its arrays with the same parameters as <code>ra::</code>, that is: a pointer, plus a size and a stride per dimension. You pass those and you&rsquo;re done; you don&rsquo;t have to worry about special cases. Say <a href="https://github.com/flame/blis">BLIS</a>:
  1005. </p>
  1006. <blockquote>
  1007. <pre class="verbatim">#include &lt;blis.h&gt;
  1008. ra::Big&lt;double, 2&gt; A({M, K}, ...);
  1009. ra::Big&lt;double, 2&gt; B({K, N}, ...);
  1010. ra::Big&lt;double, 2&gt; C({M, N}, ...);
  1011. double alpha = ...;
  1012. double beta = ...;
  1013. // C := βC + αAB
  1014. bli_dgemm(BLIS_NO_TRANSPOSE, BLIS_NO_TRANSPOSE, M, N, K, &amp;alpha,
  1015. A.data(), A.stride(0), A.stride(1),
  1016. B.data(), B.stride(0), B.stride(1),
  1017. &amp;beta, C.data(), C.stride(0), C.stride(1));
  1018. </pre></blockquote>
  1019. <a name="index-FFTW"></a>
  1020. <p>Another good array citizen, <a href="http://fftw.org">FFTW</a> handles arbitrary rank:
  1021. </p>
  1022. <blockquote>
  1023. <pre class="verbatim">#include &lt;fftw3.h&gt;
  1024. ...
  1025. using complex = std::complex&lt;double&gt;;
  1026. static_assert(sizeof(complex)==sizeof(fftw_complex));
  1027. // forward DFT over the last r axes of a -&gt; b
  1028. void fftw(int r, ra::View&lt;complex&gt; const a, ra::View&lt;complex&gt; b)
  1029. {
  1030. int const rank = a.rank();
  1031. assert(r&gt;0 &amp;&amp; r&lt;=rank);
  1032. assert(every(shape(a)==shape(b)));
  1033. fftw_iodim dims[r];
  1034. fftw_iodim howmany_dims[rank-r];
  1035. for (int i=0; i!=rank; ++i) {
  1036. if (i&gt;=rank-r) {
  1037. dims[i-rank+r].n = a.size(i);
  1038. dims[i-rank+r].is = a.stride(i);
  1039. dims[i-rank+r].os = b.stride(i);
  1040. } else {
  1041. howmany_dims[i].n = a.size(i);
  1042. howmany_dims[i].is = a.stride(i);
  1043. howmany_dims[i].os = b.stride(i);
  1044. }
  1045. }
  1046. fftw_plan p;
  1047. p = fftw_plan_guru_dft(r, dims, rank-r, howmany_dims,
  1048. (fftw_complex *)(a.data()), (fftw_complex *)(b.data()),
  1049. FFTW_FORWARD, FFTW_ESTIMATE);
  1050. fftw_execute(p);
  1051. fftw_destroy_plan(p);
  1052. }
  1053. </pre></blockquote>
  1054. <a name="index-Guile"></a>
  1055. <p>Translating array descriptors from a foreign language should be fairly simple. For example, this is how to convert a <a href="https://www.gnu.org/software/guile/manual/html_node/Accessing-Arrays-from-C.html#Accessing-Arrays-from-C">Guile</a> array view into an <code>ra::</code> view:
  1056. </p>
  1057. <blockquote>
  1058. <pre class="verbatim"> SCM a; // say a is #nf64(...)
  1059. ...
  1060. scm_t_array_handle h;
  1061. scm_array_get_handle(a, &amp;h);
  1062. scm_t_array_dim const * dims = scm_array_handle_dims(&amp;h);
  1063. View&lt;double&gt; v(map([](int i) { return ra::Dimv {dim[i].ubnd-dim[i].lbnd+1, dim[i].inc}; },
  1064. ra::iota(scm_array_handle_rank(&amp;h))),
  1065. scm_array_handle_f64_writable_elements(&amp;h));
  1066. ...
  1067. scm_array_handle_release(&amp;h);
  1068. </pre></blockquote>
  1069. <a name="index-Numpy-2"></a>
  1070. <a name="index-Python"></a>
  1071. <p>Numpy&rsquo;s C API has the type <a href="https://docs.scipy.org/doc/numpy/reference/c-api.array.html"><code>PyArrayObject</code></a> which can be used in the same way as Guile&rsquo;s <code>scm_t_array_handle</code> in the example above.
  1072. </p>
  1073. <p>Generally it is simpler to let the foreign language handle the memory, even though there should be ways to transfer ownership (e.g. Guile has <a href="https://www.gnu.org/software/guile/manual/html_node/SRFI_002d4-API.html#index-scm_005ftake_005ff64vector"><code>scm_take_xxx</code></a>).
  1074. </p>
  1075. <a name="The-bad-array-citizen"></a>
  1076. <h4 class="subsubsection">2.9.3.2 The bad array citizen</h4>
  1077. <p>Unfortunately there are many libraries that don&rsquo;t accept general array parameters, or that do strange things with particular values of sizes and/or strides.
  1078. </p>
  1079. <p>The most common case is that a library doesn&rsquo;t handle strides at all, and it only accepts unit stride for rank 1 arrays, or packed row-major or column-major storage for higher rank arrays. In that case, you might be forced to copy your array before passing it along.
  1080. </p>
  1081. <p>FIXME using is_c_order, etc.
  1082. </p>
  1083. <p>Other libraries do accept strides, but not general ones. For example <a href="https://www.netlib.org/blas">https://www.netlib.org/blas</a>&rsquo; <code>cblas_dgemm</code> has this prototype:
  1084. </p>
  1085. <blockquote>
  1086. <pre class="verbatim">cblas_dgemm(order, transA, transB, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
  1087. </pre></blockquote>
  1088. <p><code>A</code>, <code>B</code>, <code>C</code> are (pointers to) 2-arrays, but the routine accepts only one stride argument for each (<code>lda</code>, etc.). CBLAS also doesn&rsquo;t understand <code>lda</code> as a general stride, but rather as the dimension of a larger array that you&rsquo;re slicing <code>A</code> from, and some implementations will handle negative or zero <code>lda</code> in bizarre ways.
  1089. </p>
  1090. <p>Sometimes you can work around this by fiddling with <code>transA</code> and <code>transB</code>, but in general you need to check your array parameters and you may need to make copies.
  1091. </p>
  1092. <a name="index-OpenGL"></a>
  1093. <p>OpenGL is another library that requires <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml">contortions:</a>
  1094. </p>
  1095. <blockquote>
  1096. <pre class="verbatim">void glVertexAttribPointer(GLuint index,
  1097. GLint size,
  1098. GLenum type,
  1099. GLboolean normalized,
  1100. GLsizei stride,
  1101. const GLvoid * pointer);
  1102. </pre>
  1103. <p>[...]
  1104. </p>
  1105. <p><em>stride</em>
  1106. </p>
  1107. <blockquote>
  1108. <p>Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
  1109. </p></blockquote>
  1110. </blockquote>
  1111. <p>It isn&rsquo;t clear whether negative strides are legal, either. So just as with CBLAS, passing general array views will require copies.
  1112. </p>
  1113. <hr>
  1114. <a name="Extension"></a>
  1115. <div class="header">
  1116. <p>
  1117. Next: <a href="#Functions" accesskey="n" rel="next">Functions</a>, Previous: <a href="#Compatibility" accesskey="p" rel="prev">Compatibility</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1118. </div>
  1119. <a name="Extension-1"></a>
  1120. <h3 class="section">2.10 Extension</h3>
  1121. <a name="New-scalar-types"></a>
  1122. <h4 class="subsection">2.10.1 New scalar types</h4>
  1123. <p><code>ra::</code> will let you construct arrays of arbitrary types out of the box. This is the same functionality you get with e.g. <code>std::vector</code>.
  1124. </p>
  1125. <div class="example">
  1126. <pre class="verbatim">struct W { int x; }
  1127. ra::Big&lt;W, 2&gt; w = {{ {4}, {2} }, { {1}, {3} }};
  1128. cout &lt;&lt; W(1, 1).x &lt;&lt; endl;
  1129. cout &lt;&lt; amin(map([](auto &amp;&amp; x) { return w.x; }, w)) &lt;&lt; endl;
  1130. </pre><pre class="example">-| 3
  1131. 1
  1132. </pre></div>
  1133. <p>However, if you want to mix arbitrary types in array operations, you&rsquo;ll need to tell <code>ra::</code> that that is actually what you want. This is to avoid conflicts with other libraries.
  1134. </p>
  1135. <div class="example">
  1136. <pre class="verbatim">namespace ra { template &lt;&gt; constexpr bool is_scalar_def&lt;W&gt; = true; }
  1137. ...
  1138. W ww {11};
  1139. for_each([](auto &amp;&amp; x, auto &amp;&amp; y) { cout &lt;&lt; (x.x + y.y) &lt;&lt; &quot; &quot;; }, w, ww); // ok
  1140. </pre><pre class="example">-| 15 13 12 14
  1141. </pre></div>
  1142. <p>but
  1143. </p>
  1144. <div class="example">
  1145. <pre class="verbatim">struct U { int x; }
  1146. U uu {11};
  1147. for_each([](auto &amp;&amp; x, auto &amp;&amp; y) { cout &lt;&lt; (x.x + y.y) &lt;&lt; &quot; &quot;; }, w, uu); // error: can't find ra::start(U)
  1148. </pre></div>
  1149. <a name="x_002dnew_002darray_002doperations"></a><a name="New-array-operations"></a>
  1150. <h4 class="subsection">2.10.2 New array operations</h4>
  1151. <p><code>ra::</code> provides array extensions for standard operations such as <code>+</code>, <code>*</code>, <code>cos</code> <a href="#x_002dscalar_002dops">and so on</a>. You can add array extensions for your own operations in the obvious way, with <a href="#x_002dmap"><code>map</code></a> (but note the namespace qualifiers):
  1152. </p>
  1153. <div class="example">
  1154. <pre class="verbatim">return_type my_fun(...) { };
  1155. ...
  1156. namespace ra {
  1157. template &lt;class ... A&gt; inline auto
  1158. my_fun(A &amp;&amp; ... a)
  1159. {
  1160. return map(::my_fun, std::forward&lt;A&gt;(a) ...);
  1161. }
  1162. } // namespace ra
  1163. </pre></div>
  1164. <a name="index-Blitz_002b_002b-2"></a>
  1165. <p>If you compare this with what Blitz++ had to do, modern C++ sure has made our lives easier.
  1166. </p>
  1167. <p>If <code>my_fun</code> is an overload set, you can use
  1168. </p>
  1169. <div class="example">
  1170. <pre class="verbatim">namespace ra {
  1171. template &lt;class ... A&gt; inline auto
  1172. my_fun(A &amp;&amp; ... a)
  1173. {
  1174. return map([](auto &amp;&amp; ... a) { return ::my_fun(a ...); }, std::forward&lt;A&gt;(a) ...);
  1175. }
  1176. } // namespace ra
  1177. </pre></div>
  1178. <hr>
  1179. <a name="Functions"></a>
  1180. <div class="header">
  1181. <p>
  1182. Next: <a href="#Error-handling" accesskey="n" rel="next">Error handling</a>, Previous: <a href="#Extension" accesskey="p" rel="prev">Extension</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1183. </div>
  1184. <a name="Functions-1"></a>
  1185. <h3 class="section">2.11 Functions</h3>
  1186. <p>You don&rsquo;t need to use <a href="#Array-operations"><code>map</code></a> every time you want to do something with arrays in <code>ra::</code>. A number of array functions are already defined.
  1187. </p>
  1188. <a name="x_002dscalar_002dops"></a><a name="Standard-scalar-operations"></a>
  1189. <h4 class="subsection">2.11.1 Standard scalar operations</h4>
  1190. <p><code>ra::</code> defines array extensions for <code>+</code>, <code>-</code> (both unary and binary), <code>*</code>, <code>/</code>, <code>!</code>, <code>&amp;&amp;</code>, <code>||</code><a name="DOCF8" href="#FOOT8"><sup>8</sup></a>, <code>&gt;</code>, <code>&lt;</code>, <code>&gt;=</code>, <code>&lt;=</code>, <code>&lt;=&gt;</code>, <code>==</code>, <code>!=</code>, <code>pow</code>, <code>sqr</code>, <code>abs</code>, <code>cos</code>, <code>sin</code>, <code>exp</code>, <code>expm1</code>, <code>sqrt</code>, <code>log</code>, <code>log1p</code>, <code>log10</code>, <code>isfinite</code>, <code>isnan</code>, <code>isinf</code>, <code>max</code>, <code>min</code>, <code>odd</code>, <code>asin</code>, <code>acos</code>, <code>atan</code>, <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, and <code>tanh</code>.
  1191. Extending other scalar operations is straightforward; see <a href="#x_002dnew_002darray_002doperations">New array operations</a>. <code>ra::</code> also defines (and extends) the non-standard functions <a href="#x_002dsqr"><code>sqr</code></a>, <a href="#x_002dsqrm"><code>sqrm</code></a>, <a href="#x_002dconj"><code>conj</code></a>, <a href="#x_002drel_002derror"><code>rel_error</code></a>, and <a href="#x_002dxI"><code>xI</code></a>.
  1192. </p>
  1193. <p>For example:
  1194. </p><div class="example">
  1195. <pre class="verbatim">cout &lt;&lt; exp(ra::Small&lt;double, 3&gt; {4, 5, 6}) &lt;&lt; endl;
  1196. </pre><pre class="example"> -| 54.5982 148.413 403.429
  1197. </pre></div>
  1198. <a name="Conditional-operations"></a>
  1199. <h4 class="subsection">2.11.2 Conditional operations</h4>
  1200. <p><a href="#x_002dmap"><code>map</code></a> evaluates all of its arguments before passing them along to its operator. This isn&rsquo;t always what you want. The simplest example is <code>where(condition, iftrue, iffalse)</code>, which returns an expression that will evaluate <code>iftrue</code> when <code>condition</code> is true and <code>iffalse</code> otherwise.
  1201. </p>
  1202. <div class="example">
  1203. <pre class="verbatim">ra::Big&lt;double&gt; x ...
  1204. ra::Big&lt;double&gt; y = where(x&gt;0, expensive_expr_1(x), expensive_expr_2(x));
  1205. </pre></div>
  1206. <p>Here <code>expensive_expr_1</code> and <code>expensive_expr_2</code> are array expressions. So the computation of the other arm would be wasted if one were to do instead
  1207. </p>
  1208. <div class="example">
  1209. <pre class="verbatim">ra::Big&lt;double&gt; y = map([](auto &amp;&amp; w, auto &amp;&amp; t, auto &amp;&amp; f) -&gt; decltype(auto) { return w ? t : f; }
  1210. x&gt;0, expensive_expr_1(x), expensive_function_2(x));
  1211. </pre></div>
  1212. <p>If the expressions have side effects, then <code>map</code> won&rsquo;t even give the right result.
  1213. </p>
  1214. <div class="example">
  1215. <pre class="verbatim">ra::Big&lt;int, 1&gt; o = {};
  1216. ra::Big&lt;int, 1&gt; e = {};
  1217. ra::Big&lt;int, 1&gt; n = {1, 2, 7, 9, 12};
  1218. ply(where(odd(n), map([&amp;o](auto &amp;&amp; x) { o.push_back(x); }, n), map([&amp;e](auto &amp;&amp; x) { e.push_back(x); }, n)));
  1219. cout &lt;&lt; &quot;o: &quot; &lt;&lt; ra::noshape &lt;&lt; o &lt;&lt; &quot;, e: &quot; &lt;&lt; ra::noshape &lt;&lt; e &lt;&lt; endl;
  1220. </pre><pre class="example">-| o: 1 7 9, e: 2 12
  1221. </pre></div>
  1222. <p>FIXME Very artificial example.
  1223. FIXME Do we want to expose ply(); this is the only example in the manual that uses it.
  1224. </p>
  1225. <p>When the choice is between more than two expressions, there&rsquo;s <a href="#x_002dpick"><code>pick</code></a>, which operates similarly.
  1226. </p>
  1227. <a name="Special-operations"></a>
  1228. <h4 class="subsection">2.11.3 Special operations</h4>
  1229. <p>Some operations are essentially scalar operations, but require special syntax and would need a lambda wrapper to be used with <code>map</code>. <code>ra::</code> comes with a few of these already defined.
  1230. </p>
  1231. <p>FIXME
  1232. </p>
  1233. <a name="Elementwise-reductions"></a>
  1234. <h4 class="subsection">2.11.4 Elementwise reductions</h4>
  1235. <p><code>ra::</code> defines the whole-array one-argument reductions <code>any</code>, <code>every</code>, <code>amax</code>, <code>amin</code>, <code>sum</code>, <code>prod</code> and the two-argument reductions <code>dot</code> and <code>cdot</code>. Note that <code>max</code> and <code>min</code> are two-argument scalar operations with array extensions, while <code>amax</code> and <code>amin</code> are reductions. <code>any</code> and <code>every</code> are short-circuiting.
  1236. </p>
  1237. <p>You can define similar reductions in the same way that <code>ra::</code> does it:
  1238. </p>
  1239. <div class="example">
  1240. <pre class="verbatim">template &lt;class A&gt;
  1241. inline auto op_reduce(A &amp;&amp; a)
  1242. {
  1243. T c = op_default;
  1244. for_each([&amp;c](auto &amp;&amp; a) { c = op(c, a); }, a);
  1245. return c;
  1246. }
  1247. </pre></div>
  1248. <p>Often enough you need to reduce over particular axes. This is possible by combining assignment operators with the <a href="#Rank-extension">rank extension</a> mechanism, or using the <a href="#The-rank-conjunction">rank conjunction</a>, or iterating over <a href="#Cell-iteration">cells of higher rank</a>. For example:
  1249. </p>
  1250. <div class="example">
  1251. <pre class="verbatim"> ra::Big&lt;double, 2&gt; a({m, n}, ...);
  1252. ra::Big&lt;double, 1&gt; sum_rows({n}, 0.);
  1253. iter&lt;1&gt;(sum_rows) += iter&lt;1&gt;(a);
  1254. // for_each(ra::wrank&lt;1, 1&gt;([](auto &amp; c, auto &amp;&amp; a) { c += a; }), sum_rows, a) // alternative
  1255. // sum_rows += transpose&lt;1, 0&gt;(a); // another
  1256. ra::Big&lt;double, 1&gt; sum_cols({m}, 0.);
  1257. sum_cols += a;
  1258. </pre></div>
  1259. <p>FIXME example with assignment op
  1260. </p>
  1261. <p>A few common operations of this type are already packaged in <code>ra::</code>.
  1262. </p>
  1263. <a name="Special-reductions"></a>
  1264. <h4 class="subsection">2.11.5 Special reductions</h4>
  1265. <p><code>ra::</code> defines the following special reductions.
  1266. </p>
  1267. <p>FIXME
  1268. </p>
  1269. <a name="Shortcut-reductions"></a>
  1270. <h4 class="subsection">2.11.6 Shortcut reductions</h4>
  1271. <p>Some reductions do not need to traverse the whole array if a certain condition is encountered early. The most obvious ones are the reductions of <code>&amp;&amp;</code> and <code>||</code>, which <code>ra::</code> defines as <code>every</code> and <code>any</code>.
  1272. </p>
  1273. <p>FIXME
  1274. </p>
  1275. <p>These operations are defined on top of another function <code>early</code>.
  1276. </p>
  1277. <p>FIXME early
  1278. </p>
  1279. <p>The following is often useful.
  1280. </p>
  1281. <p>FIXME lexicographical compare etc.
  1282. </p>
  1283. <a name="index-error"></a>
  1284. <a name="index-assert"></a>
  1285. <hr>
  1286. <a name="Error-handling"></a>
  1287. <div class="header">
  1288. <p>
  1289. Previous: <a href="#Functions" accesskey="p" rel="prev">Functions</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1290. </div>
  1291. <a name="Error-handling-1"></a>
  1292. <h3 class="section">2.12 Error handling</h3>
  1293. <p>Error handling in <code>ra::</code> is barebones. It is controlled by two macros:
  1294. </p>
  1295. <ul>
  1296. <li> <code>RA_DO_CHECK</code>
  1297. is a binary flag that controls runtime checks. The default is 1. The checks themselves are done with <code>RA_ASSERT</code>.
  1298. </li><li> <code>RA_ASSERT(cond, ...)</code>
  1299. is a function-like macro. <code>cond</code> an expression that evaluates to true (in the <code>ra::</code> namespace) if the check passes. The other arguments are informative and may or may not be used. The default defintion of <code>RA_ASSERT(cond, ...)</code> is <code>assert(cond)</code>, from <code>&lt;cassert&gt;</code>.
  1300. </li></ul>
  1301. <p><code>ra::</code> contains uses of <code>assert</code> for checking invariants or for sanity checks that are separate from uses of <code>RA_ASSERT</code>. Those can be disabled in the usual way with <code>-DNDEBUG</code>. The use of <code>-DNDEBUG</code> is untested.
  1302. </p>
  1303. <p>You can redefine <code>RA_ASSERT</code> to something that is more appropriate for your program. <code>examples/throw.cc</code> in the distribution shows how to throw an user-defined exception instead.
  1304. </p>
  1305. <hr>
  1306. <a name="Extras"></a>
  1307. <div class="header">
  1308. <p>
  1309. Next: <a href="#Hazards" accesskey="n" rel="next">Hazards</a>, Previous: <a href="#Usage" accesskey="p" rel="prev">Usage</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1310. </div>
  1311. <a name="Extras-1"></a>
  1312. <h2 class="chapter">3 Extras</h2>
  1313. <hr>
  1314. <a name="Hazards"></a>
  1315. <div class="header">
  1316. <p>
  1317. Next: <a href="#Internals" accesskey="n" rel="next">Internals</a>, Previous: <a href="#Extras" accesskey="p" rel="prev">Extras</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1318. </div>
  1319. <a name="Hazards-1"></a>
  1320. <h2 class="chapter">4 Hazards</h2>
  1321. <p>Some of these issues arise because <code>ra::</code> applies its principles systematically, which can have surprising results. Still others are the result of unfortunate compromises. And a few are just bugs.
  1322. </p>
  1323. <a name="Reuse-of-expression-objects"></a>
  1324. <h3 class="section">4.1 Reuse of expression objects</h3>
  1325. <p>Expression objects are meant to be used once. This applies to anything produced with <code>ra::map</code>, <code>ra::iter</code>, <code>ra::start</code>, <code>ra::vector</code>, or <code>ra::ptr</code>. Reuse errors are <em>not</em> checked. For example:
  1326. </p>
  1327. <div class="example">
  1328. <pre class="verbatim"> ra::Big&lt;int, 2&gt; B({3, 3}, ra::_1 + ra::_0*3); // {{0 1 2} {3 4 5} {6 7 8}}
  1329. std::array&lt;int, 2&gt; l = { 1, 2 };
  1330. cout &lt;&lt; B(ra::vector(l), ra::vector(l)) &lt;&lt; endl; // ok =&gt; {{4 5} {7 8}}
  1331. auto ll = ra::vector(l);
  1332. cout &lt;&lt; B(ll, ll) &lt;&lt; endl; // ??
  1333. </pre></div>
  1334. <a name="Assignment-to-views"></a>
  1335. <h3 class="section">4.2 Assignment to views</h3>
  1336. <p>FIXME
  1337. With dynamic-shape containers (e.g. <code>Big</code>), <code>operator=</code> replaces the left hand side instead of writing over its contents. This behavior is inconsistent with <code>View::operator=</code> and is there only so that istream &gt;&gt; container may work; do not rely on it.
  1338. </p>
  1339. <a name="View-of-const-vs-const-view"></a>
  1340. <h3 class="section">4.3 View of const vs const view</h3>
  1341. <p>FIXME
  1342. Passing view arguments by reference
  1343. </p>
  1344. <a name="Rank-extension-in-assignments"></a>
  1345. <h3 class="section">4.4 Rank extension in assignments</h3>
  1346. <p>Assignment of an expression onto another expression of lower rank may not do what you expect. This example matches <code>a</code> and 3 [both of shape ()] with a vector of shape (3). This is equivalent to <code>{a=3+4; a=3+5; a=3+6;}</code>. You may get a different result depending on traversal order.
  1347. </p>
  1348. <div class="example">
  1349. <pre class="verbatim">int a = 0;
  1350. ra::scalar(a) = 3 + ra::Small&lt;int, 3&gt; {4, 5, 6}; // ?
  1351. </pre><pre class="example"> &rArr; a = 9
  1352. </pre></div>
  1353. <p>Compare with
  1354. </p>
  1355. <div class="example">
  1356. <pre class="verbatim">int a = 0;
  1357. ra::scalar(a) += 3 + ra::Small&lt;int, 3&gt; {4, 5, 6}; // 0 + 3 + 4 + 5 + 6
  1358. </pre><pre class="example"> &rArr; a = 18
  1359. </pre></div>
  1360. <a name="Performance-pitfalls-of-rank-extension"></a>
  1361. <h3 class="section">4.5 Performance pitfalls of rank extension</h3>
  1362. <p>In the following example where <code>b</code> has its shape extended from (3) to (3, 4), <code>f</code> is called 12 times, even though only 3 calls are needed if <code>f</code> doesn&rsquo;t have side effects. In such cases it might be preferrable to write the outer loop explicitly, or to do some precomputation.
  1363. </p>
  1364. <div class="example">
  1365. <pre class="verbatim">ra::Big&lt;int, 2&gt; a = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}};
  1366. ra::Big&lt;int, 1&gt; b = {1, 2, 3};
  1367. ra::Big&lt;int, 2&gt; c = map(f, b) + a;
  1368. </pre></div>
  1369. <a name="Chained-assignment"></a>
  1370. <h3 class="section">4.6 Chained assignment</h3>
  1371. <p>FIXME
  1372. When <code>a=b=c</code> works, it operates as <code>b=c; a=b;</code> and not as an array expression.
  1373. </p>
  1374. <a name="Unregistered-scalar-types"></a>
  1375. <h3 class="section">4.7 Unregistered scalar types</h3>
  1376. <p>FIXME
  1377. <code>View&lt;T, N&gt; x; x = T()</code> fails if <code>T</code> isn&rsquo;t registered as <code>is_scalar</code>.
  1378. </p>
  1379. <ol>
  1380. <li> Item 0
  1381. </li><li> Item 1
  1382. </li><li> Item 2
  1383. </li></ol>
  1384. <hr>
  1385. <a name="Internals"></a>
  1386. <div class="header">
  1387. <p>
  1388. Next: <a href="#The-future" accesskey="n" rel="next">The future</a>, Previous: <a href="#Hazards" accesskey="p" rel="prev">Hazards</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1389. </div>
  1390. <a name="Internals-1"></a>
  1391. <h2 class="chapter">5 Internals</h2>
  1392. <p><code>ra::</code> has two main components: a set of container classes, and the expression template mechanism. The container classes provide leaves for the expression template trees, and the container classes also make some use of the expression template mechanism internally (e.g. in the selection operator, or for initialization).
  1393. </p>
  1394. <table class="menu" border="0" cellspacing="0">
  1395. <tr><td align="left" valign="top">&bull; <a href="#Type-hierarchy" accesskey="1">Type hierarchy</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">From <code>Containers</code> to <code>Flat</code>.
  1396. </td></tr>
  1397. <tr><td align="left" valign="top">&bull; <a href="#Term-agreement" accesskey="2">Term agreement</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Abstain or approve.
  1398. </td></tr>
  1399. <tr><td align="left" valign="top">&bull; <a href="#Loop-types" accesskey="3">Loop types</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Chosen for performance.
  1400. </td></tr>
  1401. <tr><td align="left" valign="top">&bull; <a href="#Introspection" accesskey="4">Introspection</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Speaking to myself.
  1402. </td></tr>
  1403. <tr><td align="left" valign="top">&bull; <a href="#Compiling-and-running" accesskey="5">Compiling and running</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Practical matters.
  1404. </td></tr>
  1405. </table>
  1406. <hr>
  1407. <a name="Type-hierarchy"></a>
  1408. <div class="header">
  1409. <p>
  1410. Next: <a href="#Term-agreement" accesskey="n" rel="next">Term agreement</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1411. </div>
  1412. <a name="Type-hierarchy-1"></a>
  1413. <h3 class="section">5.1 Type hierarchy</h3>
  1414. <p>This is a rough and possibly not very accurate summary. I&rsquo;m hoping the planned feature of ‘C++ concepts’ will force me to be more systematic about it all.
  1415. </p>
  1416. <ul>
  1417. <li> <b>Container</b> &mdash; <code>Big</code> <code>Shared</code> <code>Unique</code> <code>Small</code>
  1418. <p>These are array types that own their data in one way or another. Creating or destroying these objects may allocate or deallocate memory, respectively.
  1419. </p>
  1420. </li><li> <b>View</b> &mdash; <code>View</code> <code>SmallView</code>
  1421. <p>These are array views into data in memory. Access to their contents doesn&rsquo;t involve computation and they may be writable. Any of the <b>Container</b> types can be treated as a <b>View</b>, but one may also create <b>View</b>s that aren&rsquo;t associated with any <b>Container</b>, for example into memory allocated by a different library. Creating and destroying <b>View</b>s doesn&rsquo;t allocate or deallocate memory for array elements.
  1422. </p>
  1423. </li><li> <b>RaIterator</b> &mdash; <code>Expr</code> <code>Pick</code> <code>cell_iterator</code> <code>cell_iterator_small</code> <code>Iota</code> <code>Vector</code> <code>Scalar</code>
  1424. <p>This is a traversable object. <b>RaIterator</b>s are accepted by all the array functions such as <code>map</code>, <code>for_each</code>, etc. <code>map</code> produces an <b>RaIterator</b> itself, so most array expressions are <b>RaIterator</b>s. <b>RaIterator</b>s are created from <b>View</b>s and from certain foreign array-like types primarily through the function <code>start</code>. This is done automatically when those types are used in array expressions.
  1425. </p>
  1426. </li><li> <b>Ravelable</b> &mdash; <code>cell_iterator</code> <code>cell_iterator_small</code> <code>Iota</code> <code>Vector</code> <code>Scalar</code> <code>TensorIndex</code>
  1427. <p>This is a kind of <b>RaIterator</b> that provides a <code>flat()</code> method to obtain a linearized view of a section of the array. Together with the methods <code>size()</code>, <code>stride()</code>, <code>keep_stride()</code> and <code>adv()</code>, a loop involving only <b>Ravelable</b>s can have its inner loop unfolded and traversed using <b>Flat</b> objects. This is faster than a multidimensional loop, especially if the inner dimensions of the loop are small.
  1428. </p>
  1429. </li><li> <b>Indexable</b> <code>cell_iterator</code> <code>cell_iterator_small</code> <code>Iota</code> <code>Vector</code> <code>Scalar</code> <code>TensorIndex</code>
  1430. <p>This is a kind of <b>RaIterator</b> that provides an <code>at(i ...)</code> method for random access to any element of the array. In the current version of <code>ra::</code>, every <b>RaIterator</b> is both <b>Ravelable</b> and <b>Indexable</b>.
  1431. </p>
  1432. </li><li> <b>Flat</b> <code>Flat</code> <code>PickFlat</code> <code>CellFlat</code> <code>IotaFlat</code> <code>ScalarFlat</code>
  1433. <p>These are types that support ‘forward iterator’ traversal. They have methods <code>operator+=</code> (to advance) and <code>operator*</code> (to derreference). <b>Flat</b> objects are obtained from <b>Ravelable</b> objects through a method <code>flat</code>.
  1434. </p>
  1435. </li></ul>
  1436. <hr>
  1437. <a name="Term-agreement"></a>
  1438. <div class="header">
  1439. <p>
  1440. Next: <a href="#Loop-types" accesskey="n" rel="next">Loop types</a>, Previous: <a href="#Type-hierarchy" accesskey="p" rel="prev">Type hierarchy</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1441. </div>
  1442. <a name="Term-agreement-1"></a>
  1443. <h3 class="section">5.2 Term agreement</h3>
  1444. <p>The execution of an expression template begins with the determination of its shape — the size of each of its dimensions. This is done recursively by traversing the terms of the expression. For a given dimension <code>k</code>≥0, terms that have rank less or equal than <code>k</code> are ignored, following the prefix matching principle. Likewise terms where dimension <code>k</code> has undefined size (such as <code>TensorIndex</code> or view axes created with <code>insert</code>) are ignored. All the other terms must match.
  1445. </p>
  1446. <p>Then we select a traversal method depending on the types of the arguments. <code>ra::</code> has two traversal methods, both based on pointer-like iterators. <code>ply_ravel</code> is used for dynamic-size expressions and <code>plyf</code> for static-size expressions.
  1447. </p>
  1448. <p>Finally we select an order of traversal. <code>ra::</code> supports ‘array’ orders, meaning that the dimensions are sorted in a certain way from outermost to innermost and a full dimension is traversed before one advances on the dimension outside. However, currently (v14) there is no heuristic to choose a dimension order, so traversal always happens in row-major order (which shouldn&rsquo;t be relied upon). <code>ply_ravel</code> will unroll as many innermost dimensions as it can, and in some cases traversal will be executed as a single 1D loop.
  1449. </p>
  1450. <hr>
  1451. <a name="Loop-types"></a>
  1452. <div class="header">
  1453. <p>
  1454. Next: <a href="#Introspection" accesskey="n" rel="next">Introspection</a>, Previous: <a href="#Term-agreement" accesskey="p" rel="prev">Term agreement</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1455. </div>
  1456. <a name="Loop-types-1"></a>
  1457. <h3 class="section">5.3 Loop types</h3>
  1458. <p>TODO
  1459. </p>
  1460. <hr>
  1461. <a name="Introspection"></a>
  1462. <div class="header">
  1463. <p>
  1464. Next: <a href="#Compiling-and-running" accesskey="n" rel="next">Compiling and running</a>, Previous: <a href="#Loop-types" accesskey="p" rel="prev">Loop types</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1465. </div>
  1466. <a name="Introspection-1"></a>
  1467. <h3 class="section">5.4 Introspection</h3>
  1468. <hr>
  1469. <a name="Compiling-and-running"></a>
  1470. <div class="header">
  1471. <p>
  1472. Previous: <a href="#Introspection" accesskey="p" rel="prev">Introspection</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1473. </div>
  1474. <a name="Compiling-and-running-1"></a>
  1475. <h3 class="section">5.5 Compiling and running</h3>
  1476. <p>The following boolean <code>#define</code>s affect the behavior of <code>ra::</code>.
  1477. </p>
  1478. <ul>
  1479. <li> <code>RA_DO_CHECK</code> (default 1): Check bounds on dimension agreement (e.g. <code>Big&lt;int, 1&gt; {2, 3} + Big&lt;int, 1&gt; {1, 2, 3}</code>) and random array accesses (e.g. <code>Small&lt;int, 2&gt; a = 0; int i = 10; a[i] = 0;</code>).
  1480. </li><li> <code>RA_USE_BLAS</code> (default 0): Try to use BLAS for certain rank 1 and rank 2 operations. Currently this is only used by some of the benchmarks and not by the library itself.
  1481. </li><li> <code>RA_DO_OPT</code> (default 1): Replace certain expressions by others that are expected to perform better. This acts as a global mask on other <code>RA_DO_OPT_xxx</code> flags.
  1482. </li><li> <code>RA_DO_OPT_IOTA</code> (default 1): Perform immediately (beat) certain operations on <code>ra::Iota</code> objects. For example, <code>ra::Iota(3, 0) + 1</code> becomes <code>ra::Iota(3, 1)</code> instead of a two-operand expression template.
  1483. </li><li> <code>RA_DO_OPT_SMALLVECTOR</code> (default 0): Perform immediately certain operations on <code>ra::Small</code> objects, using small vector intrinsics. Currently this only works on <b>gcc</b> and doesn&rsquo;t necessarily result in improved performance.
  1484. </li></ul>
  1485. <p><code>ra::</code> comes with three kinds of tests: examples, proper tests, and benchmarks. <code>ra::</code> uses its own crude test and benchmark suites. Run <code>CXXFLAGS=-O3 scons</code> from the top directory of the distribution to build and run them all. Alternatively, you can use <code>CXXFLAGS=-O3 cmake . &amp;&amp; make &amp;&amp; make test</code>.
  1486. </p>
  1487. <p>TODO Flags and notes about different compilers
  1488. </p>
  1489. <hr>
  1490. <a name="The-future"></a>
  1491. <div class="header">
  1492. <p>
  1493. Next: <a href="#Reference" accesskey="n" rel="next">Reference</a>, Previous: <a href="#Internals" accesskey="p" rel="prev">Internals</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1494. </div>
  1495. <a name="The-future-1"></a>
  1496. <h2 class="chapter">6 The future</h2>
  1497. <a name="Error-messages"></a>
  1498. <h3 class="section">6.1 Error messages</h3>
  1499. <p>FIXME
  1500. </p>
  1501. <a name="Reductions"></a>
  1502. <h3 class="section">6.2 Reductions</h3>
  1503. <p>FIXME
  1504. </p>
  1505. <a name="Etc"></a>
  1506. <h3 class="section">6.3 Etc</h3>
  1507. <p>FIXME
  1508. </p>
  1509. <hr>
  1510. <a name="Reference"></a>
  1511. <div class="header">
  1512. <p>
  1513. Next: <a href="#Sources" accesskey="n" rel="next">Sources</a>, Previous: <a href="#The-future" accesskey="p" rel="prev">The future</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1514. </div>
  1515. <a name="Reference-1"></a>
  1516. <h2 class="chapter">7 Reference</h2>
  1517. <a name="x_002dmap"></a><dl>
  1518. <dt><a name="index-map"></a>Function: <strong>map</strong> <em>op expr ...</em></dt>
  1519. <dd><p>Create an array expression that applies <var>op</var> to <var>expr</var> ...
  1520. </p></dd></dl>
  1521. <p>For example:
  1522. </p><div class="example">
  1523. <pre class="verbatim">ra::Big&lt;double, 1&gt; x = map(cos, ra::Small&lt;double, 1&gt; {0.});
  1524. </pre><pre class="example">&rArr; x = { 1. }
  1525. </pre></div>
  1526. <p><var>op</var> can return a reference. A typical use is subscripting. For example (TODO better example, e.g. using STL types):
  1527. </p>
  1528. <div class="example">
  1529. <pre class="verbatim">ra::Big&lt;int, 2&gt; x = {{3, 3}, 0.};
  1530. map([](auto &amp;&amp; i, auto &amp;&amp; j) -&gt; int &amp; { return x(i, j); },
  1531. ra::Big&lt;int, 1&gt; {0, 1, 1, 2}, ra::Big&lt;int, 1&gt; {1, 0, 2, 1})
  1532. = 1;
  1533. </pre><pre class="example">&rArr; x = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}
  1534. </pre></div>
  1535. <p>Here the anonymous function can be replaced by simply <code>x</code>. Remember that unspecified return type defaults to (value) <code>auto</code>, so either a explicit type or <code>decltype(auto)</code> should be used if you want to return a reference.
  1536. </p>
  1537. <a name="index-for_005feach"></a>
  1538. <a name="x_002dfor_005feach"></a><dl>
  1539. <dt><a name="index-for_005feach-1"></a>Function: <strong>for_each</strong> <em>op expr ...</em></dt>
  1540. <dd><p>Create an array expression that applies <var>op</var> to <var>expr</var> ..., and traverse it.
  1541. </p></dd></dl>
  1542. <p><var>op</var> is run for effect; whatever it returns is discarded. For example:
  1543. </p><div class="example">
  1544. <pre class="verbatim">double s = 0.;
  1545. for_each([&amp;s](auto &amp;&amp; a) { s+=a; }, ra::Small&lt;double, 1&gt; {1., 2., 3})
  1546. </pre><pre class="example">&rArr; s = 6.
  1547. </pre></div>
  1548. <a name="index-ply"></a>
  1549. <a name="x_002dply"></a><dl>
  1550. <dt><a name="index-ply-1"></a>Function: <strong>ply</strong> <em>expr</em></dt>
  1551. <dd><p>Traverse <var>expr</var>. <code>ply</code> returns <code>void</code> so <var>expr</var> should be run for effect.
  1552. </p></dd></dl>
  1553. <p>It is rarely necessary to use <code>ply</code>. Expressions are traversed automatically when they are assigned to views, for example, or printed out. <a href="#x_002dfor_005feach"><code>for_each</code></a><code>(...)</code> (which is actually <code>ply(map(...))</code> should cover most other uses.
  1554. </p>
  1555. <div class="example">
  1556. <pre class="verbatim">double s = 0.;
  1557. ply(map([&amp;s](auto &amp;&amp; a) { s+=a; }, ra::Small&lt;double, 1&gt; {1., 2., 3})) // same as for_each
  1558. </pre><pre class="example">&rArr; s = 6.
  1559. </pre></div>
  1560. <a name="index-pack"></a>
  1561. <a name="x_002dpack"></a><dl>
  1562. <dt><a name="index-pack-1"></a>Function: <strong>pack</strong> <em>&lt;type&gt; expr ...</em></dt>
  1563. <dd><p>Create an array expression that brace-constructs <var>type</var> from <var>expr</var> ...
  1564. </p></dd></dl>
  1565. <a name="index-cast"></a>
  1566. <a name="x_002dcast"></a><dl>
  1567. <dt><a name="index-cast-1"></a>Function: <strong>cast</strong> <em>&lt;type&gt; expr</em></dt>
  1568. <dd><p>Create an array expression that casts <var>expr</var> into <var>type</var>.
  1569. </p></dd></dl>
  1570. <a name="index-pick"></a>
  1571. <a name="x_002dpick"></a><dl>
  1572. <dt><a name="index-pick-1"></a>Function: <strong>pick</strong> <em>select_expr expr ...</em></dt>
  1573. <dd><p>Create an array expression that selects the first of <var>expr</var> ... if <var>select_expr</var> is 0, the second if <var>select_expr</var> is 1, and so on. The expressions that are not selected are not looked up.
  1574. </p></dd></dl>
  1575. <p>This function cannot be defined using <a href="#x_002dmap"><code>map</code></a>, because <code>map</code> looks up each one of its argument expressions before calling <var>op</var>.
  1576. </p>
  1577. <p>For example:
  1578. </p><div class="example">
  1579. <pre class="verbatim"> ra::Small&lt;int, 3&gt; s {2, 1, 0};
  1580. ra::Small&lt;double, 3&gt; z = pick(s, s*s, s+s, sqrt(s));
  1581. </pre><pre class="example"> &rArr; z = {1.41421, 2, 0}
  1582. </pre></div>
  1583. <a name="index-where"></a>
  1584. <a name="x_002dwhere"></a><dl>
  1585. <dt><a name="index-where-1"></a>Function: <strong>where</strong> <em>pred_expr true_expr false_expr</em></dt>
  1586. <dd><p>Create an array expression that selects <var>true_expr</var> if <var>pred_expr</var> is <code>true</code>, and <var>false_expr</var> if <var>pred_expr</var> is <code>false</code>. The expression that is not selected is not looked up.
  1587. </p></dd></dl>
  1588. <p>For example:
  1589. </p><div class="example">
  1590. <pre class="verbatim"> ra::Big&lt;double, 1&gt; s {1, -1, 3, 2};
  1591. s = where(s&gt;=2, 2, s); // saturate s
  1592. </pre><pre class="example"> &rArr; s = {1, -1, 2, 2}
  1593. </pre></div>
  1594. <a name="index-from"></a>
  1595. <a name="x_002dfrom"></a><dl>
  1596. <dt><a name="index-from-1"></a>Function: <strong>from</strong> <em>op ... expr</em></dt>
  1597. <dd><p>Create outer product expression. This is defined as <em>E = from(op, e₀, e₁ ...)</em> ⇒ <em>E(i₀₀, i₀₁ ..., i₁₀, i₁₁, ..., ...) = op[e₀(i₀₀, i₀₁, ...), e₁(i₁₀, i₁₁, ...), ...]</em>.
  1598. </p></dd></dl>
  1599. <p>For example:
  1600. </p><div class="example">
  1601. <pre class="verbatim"> ra::Big&lt;double, 1&gt; a {1, 2, 3};
  1602. ra::Big&lt;double, 1&gt; b {10, 20, 30};
  1603. ra::Big&lt;double, 2&gt; axb = from([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }, a, b)
  1604. </pre><pre class="example"> &rArr; axb = {{10, 20, 30}, {20, 40, 60}, {30, 60, 90}}
  1605. </pre></div>
  1606. <div class="example">
  1607. <pre class="verbatim"> ra::Big&lt;int, 1&gt; i {2, 1};
  1608. ra::Big&lt;int, 1&gt; j {0, 1};
  1609. ra::Big&lt;double, 2&gt; A = {{1, 2}, {3, 4}, {5, 6}};
  1610. ra::Big&lt;double, 2&gt; Aij = from(A, i, j)
  1611. </pre><pre class="example"> &rArr; Aij = {{6, 5}, {4, 3}}
  1612. </pre></div>
  1613. <p>The last example is more or less how <code>A(i, j)</code> is actually implemented (see <a href="#The-rank-conjunction">The rank conjunction</a>).
  1614. </p>
  1615. <a name="index-at"></a>
  1616. <a name="x_002dat"></a><dl>
  1617. <dt><a name="index-at-1"></a>Function: <strong>at</strong> <em>expr indices</em></dt>
  1618. <dd><p>Look up <var>expr</var> at each element of <var>indices</var>, which shall be a multi-index into <var>expr</var>.
  1619. </p></dd></dl>
  1620. <p>This can be used for sparse subscripting. For example:
  1621. </p><div class="example">
  1622. <pre class="verbatim"> ra::Big&lt;int, 2&gt; A = {{100, 101}, {110, 111}, {120, 121}};
  1623. ra::Big&lt;ra::Small&lt;int, 2&gt;, 2&gt; i = {{{0, 1}, {2, 0}}, {{1, 0}, {2, 1}}};
  1624. ra::Big&lt;int, 2&gt; B = at(A, i);
  1625. </pre><pre class="example"> &rArr; B = {{101, 120}, {110, 121}}
  1626. </pre></div>
  1627. <a name="index-shape"></a>
  1628. <a name="x_002dshape"></a><dl>
  1629. <dt><a name="index-shape-1"></a>Function: <strong>shape</strong> <em>a</em></dt>
  1630. <dd><p>Get the shape of an <code>ra::</code> object.
  1631. </p></dd></dl>
  1632. <p>The shape of a dynamic rank array is a rank-1 object with dynamic size, and the shape of a static rank array is a rank-1 object with static size.
  1633. </p>
  1634. <p>This function may return an expression object or an array object. If you need to operate on the result, it might be necessary to use <code>concrete</code>.
  1635. </p>
  1636. <a name="index-size"></a>
  1637. <a name="x_002dsize"></a><dl>
  1638. <dt><a name="index-size-1"></a>Function: <strong>size</strong> <em>a</em></dt>
  1639. <dd><p>Get the total size of an <code>ra::</code> object: the product of all its sizes.
  1640. </p></dd></dl>
  1641. <a name="index-concrete"></a>
  1642. <a name="x_002dconcrete"></a><dl>
  1643. <dt><a name="index-concrete-1"></a>Function: <strong>concrete</strong> <em>a</em></dt>
  1644. <dd><p>Convert the argument to a container of the same rank and size as <code>a</code>.
  1645. </p></dd></dl>
  1646. <p>If the argument has dynamic or static shape, it is the same for the result. The main use of this function is to obtain modifiable copy of an array expression without having to prepare a container beforehand, or compute the appropiate type.
  1647. </p>
  1648. <a name="index-wrank"></a>
  1649. <a name="x_002dwrank"></a><dl>
  1650. <dt><a name="index-wrank-1"></a>Function: <strong>wrank</strong> <em>&lt;input_rank ...&gt; op</em></dt>
  1651. <dd><p>Wrap op using a rank conjunction (see <a href="#The-rank-conjunction">The rank conjunction</a>).
  1652. </p></dd></dl>
  1653. <p>For example: TODO
  1654. </p><div class="example">
  1655. <pre class="verbatim"></pre><pre class="example"> &rArr; x = 0
  1656. </pre></div>
  1657. <a name="index-transpose"></a>
  1658. <a name="x_002dtranspose"></a><dl>
  1659. <dt><a name="index-transpose-1"></a>Function: <strong>transpose</strong> <em>&lt;axes ...&gt; (view) | (axes, view)</em></dt>
  1660. <dd><p>Create a new view by transposing the axes of <var>view</var>. The number of <var>axes</var> must match the rank of <var>view</var>.
  1661. </p></dd></dl>
  1662. <p>For example:
  1663. </p>
  1664. <div class="example">
  1665. <pre class="verbatim"> ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  1666. cout &lt;&lt; transpose&lt;1, 0&gt;(a) &lt;&lt; endl;
  1667. </pre><pre class="example"> -|
  1668. 3 2
  1669. 1 4
  1670. 2 5
  1671. 3 6
  1672. </pre></div>
  1673. <p>The rank of the result is <code>maxᵢ(axesᵢ)+1</code> and it may be smaller or larger than that of <var>view</var>. If an axis is repeated, the smallest of the dimensions of <var>view</var> is used. For example:
  1674. </p>
  1675. <div class="example">
  1676. <pre class="verbatim"> ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  1677. cout &lt;&lt; transpose&lt;0, 0&gt;(a) &lt;&lt; endl; // { a(0, 0), a(1, 1) }
  1678. </pre><pre class="example"> -|
  1679. 2
  1680. 1 5
  1681. </pre></div>
  1682. <p>If one of the destination axes isn&rsquo;t mentioned in <var>axes</var>, then is a ‘dead’ axis similar to those produced by <a href="#x_002dinsert"><code>insert</code></a>. For example:
  1683. </p>
  1684. <div class="example">
  1685. <pre class="verbatim"> ra::Unique&lt;double, 1&gt; a = {1, 2, 3};
  1686. cout &lt;&lt; ((a*10) + transpose&lt;1&gt;(a)) &lt;&lt; endl;
  1687. </pre><pre class="example"> -|
  1688. 3 3
  1689. 11 21 31
  1690. 12 22 32
  1691. 13 23 33
  1692. </pre></div>
  1693. <p>The two argument form lets you specify the axis list dynamically. In that case the result will have dynamic rank as well. For example: </p>
  1694. <div class="example">
  1695. <pre class="verbatim"> ra::Small&lt;int, 2&gt; axes = {0, 1};
  1696. ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  1697. cout &lt;&lt; &quot;A: &quot; &lt;&lt; transpose(axes, a) &lt;&lt; endl;
  1698. axes = {1, 0};
  1699. cout &lt;&lt; &quot;B: &quot; &lt;&lt; transpose(axes, a) &lt;&lt; endl;
  1700. </pre><pre class="example"> -|
  1701. A: 2
  1702. 2 3
  1703. 1 2 3
  1704. 4 5 6
  1705. B: 2
  1706. 3 2
  1707. 1 4
  1708. 2 5
  1709. 3 6
  1710. </pre></div>
  1711. <p>This operation does not work on arbitrary array expressions yet. TODO FILL
  1712. </p>
  1713. <a name="index-diag"></a>
  1714. <a name="x_002ddiag"></a><dl>
  1715. <dt><a name="index-diag-1"></a>Function: <strong>diag</strong> <em>view</em></dt>
  1716. <dd><p>Equivalent to <code>transpose&lt;0, 0&gt;(view)</code>.
  1717. </p></dd></dl>
  1718. <a name="index-reverse"></a>
  1719. <a name="x_002dreverse"></a><dl>
  1720. <dt><a name="index-reverse-1"></a>Function: <strong>reverse</strong> <em>view axis</em></dt>
  1721. <dd><p>Create a new view by reversing axis <var>k</var> of <var>view</var>.
  1722. </p></dd></dl>
  1723. <p>This is equivalent to <code>view(ra::dots&lt;k&gt;, ra::iota(view.size(k), view.size(k)-1, -1))</code>.
  1724. </p>
  1725. <p>This operation does not work on arbitrary array expressions yet. TODO FILL
  1726. </p>
  1727. <a name="index-stencil"></a>
  1728. <a name="x_002dstencil"></a><dl>
  1729. <dt><a name="index-stencil-1"></a>Function: <strong>stencil</strong> <em>view lo hi</em></dt>
  1730. <dd><p>Create a stencil on <var>view</var> with lower bounds <var>lo</var> and higher bounds <var>hi</var>.
  1731. </p></dd></dl>
  1732. <p><var>lo</var> and <var>hi</var> are expressions of rank 1 indicating the extent of the stencil on each dimension. Scalars are rank extended, that is, <var>lo</var>=0 is equivalent to <var>lo</var>=(0, 0, ..., 0) with length equal to the rank <code>r</code> of <var>view</var>. The stencil view has twice as many axes as <var>view</var>. The first <code>r</code> dimensions are the same as those of <var>view</var> except that they have their sizes reduced by <var>lo</var>+<var>hi</var>. The last <code>r</code> dimensions correspond to the stencil around each element of <var>view</var>; the center element is at <code>s(i0, i1, ..., lo(0), lo(1), ...)</code>.
  1733. </p>
  1734. <p>This operation does not work on arbitrary array expressions yet. TODO FILL
  1735. </p>
  1736. <a name="index-collapse"></a>
  1737. <a name="x_002dcollapse"></a><dl>
  1738. <dt><a name="index-collapse-1"></a>Function: <strong>collapse</strong></dt>
  1739. <dd><p>TODO
  1740. </p></dd></dl>
  1741. <a name="index-explode"></a>
  1742. <a name="x_002dexplode"></a><dl>
  1743. <dt><a name="index-explode-1"></a>Function: <strong>explode</strong></dt>
  1744. <dd><p>TODO
  1745. </p></dd></dl>
  1746. <a name="index-real_005fpart"></a>
  1747. <a name="x_002dreal_005fpart"></a><dl>
  1748. <dt><a name="index-real_005fpart-1"></a>Function: <strong>real_part</strong></dt>
  1749. <dd><p>Take real part of a complex number. This can be used as reference.
  1750. </p>
  1751. <p>See also <a href="#x_002dimag_005fpart"><code>imag_part</code></a>.
  1752. </p></dd></dl>
  1753. <a name="index-imag_005fpart"></a>
  1754. <a name="x_002dimag_005fpart"></a><dl>
  1755. <dt><a name="index-imag_005fpart-1"></a>Function: <strong>imag_part</strong></dt>
  1756. <dd><p>Take imaginary part of a complex number. This can be used as reference.
  1757. </p>
  1758. <p>For example: </p>
  1759. <div class="example">
  1760. <pre class="verbatim">ra::Small&lt;std::complex&lt;double&gt;, 2, 2&gt; A = {{1., 2.}, {3., 4.}};
  1761. imag_part(A) = -2*real_part(A);
  1762. cout &lt;&lt; A &lt;&lt; endl;
  1763. </pre><pre class="example">-|
  1764. (1, -2) (2, -4)
  1765. (3, -6) (4, -8)
  1766. </pre></div>
  1767. <p>See also <a href="#x_002dreal_005fpart"><code>real_part</code></a>.
  1768. </p></dd></dl>
  1769. <a name="index-format_005farray"></a>
  1770. <a name="x_002dformat_005farray"></a><dl>
  1771. <dt><a name="index-format_005farray-1"></a>Function: <strong>format_array</strong> <em>expr [last_axis_separator [second_last_axis_separator ...]]</em></dt>
  1772. <dd><p>Formats an array for character output.
  1773. </p>
  1774. <p>For example:
  1775. </p>
  1776. <div class="example">
  1777. <pre class="verbatim">ra::Small&lt;int, 2, 2&gt; A = {{1, 2}, {3, 4}};
  1778. cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
  1779. cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; format_array(A) &lt;&lt; endl;
  1780. cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; format_array(A, &quot;|&quot;, &quot;-&quot;) &lt;&lt; endl;
  1781. </pre><pre class="example">-| case a:
  1782. 1 2
  1783. 3 4
  1784. case b:
  1785. 1 2
  1786. 3 4
  1787. case c:
  1788. 1|2-3|4
  1789. </pre></div>
  1790. </dd></dl>
  1791. <p>The shape that might be printed before the expression itself (depending on its type) is not subject to these separators. See <a href="#x_002dnoshape"><code>noshape</code></a>, <a href="#x_002dwithshape"><code>withshape</code></a>.
  1792. </p>
  1793. <a name="index-noshape"></a>
  1794. <a name="index-withshape"></a>
  1795. <a name="x_002dnoshape"></a><a name="x_002dwithshape"></a><dl>
  1796. <dt><a name="index-withshape-noshape"></a>Special&nbsp;object<!-- /@w -->: <strong>withshape noshape</strong></dt>
  1797. <dd><p>If either of these objects is sent to <code>std::ostream</code> before an expression object, the shape of that object will or won&rsquo;t be printed.
  1798. </p></dd></dl>
  1799. <p>If the object has static (compile time) shape, the default is not to print the shape, so <code>noshape</code> isn&rsquo;t necessary, and conversely for <code>withshape</code> if the object has dynamic (runtime) shape. Note that the array readers [<code>operator&gt;&gt;(std::istream &amp;, ...)</code>] expect the shape to be present or not according to the default.
  1800. </p>
  1801. <p>For example:
  1802. </p>
  1803. <div class="example">
  1804. <pre class="verbatim">ra::Small&lt;int, 2, 2&gt; A = {77, 99};
  1805. cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
  1806. cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; ra::noshape &lt;&lt; A &lt;&lt; endl;
  1807. cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; ra::withshape &lt;&lt; A &lt;&lt; endl;
  1808. </pre><pre class="example">-| case a:
  1809. 77 99
  1810. case b:
  1811. 77 99
  1812. case c:
  1813. 2
  1814. 77 99
  1815. </pre></div>
  1816. <p>but:
  1817. </p>
  1818. <div class="example">
  1819. <pre class="verbatim">ra::Big&lt;int&gt; A = {77, 99};
  1820. cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
  1821. cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; ra::noshape &lt;&lt; A &lt;&lt; endl;
  1822. cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; ra::withshape &lt;&lt; A &lt;&lt; endl;
  1823. </pre><pre class="example">-| case a:
  1824. 1
  1825. 2
  1826. 77 99
  1827. case b:
  1828. 77 99
  1829. case c:
  1830. 1
  1831. 2
  1832. 77 99
  1833. </pre></div>
  1834. <p>Note that in the last example the very shape of <code>ra::Big&lt;int&gt;</code> has runtime size, so that size (the rank of <code>A</code>, that is 1) is printed as part of the shape of <code>A</code>.
  1835. </p>
  1836. <p>See also <a href="#x_002dformat_005farray"><code>format_array</code></a>.
  1837. </p>
  1838. <a name="index-start-1"></a>
  1839. <a name="x_002dstart"></a><dl>
  1840. <dt><a name="index-start-2"></a>Function: <strong>start</strong> <em>foreign_object</em></dt>
  1841. <dd><p>Create a array expression from <var>foreign_object</var>.
  1842. </p></dd></dl>
  1843. <p><var>foreign_object</var> can be of type <code>std::vector</code> or <code>std::array</code>, a built-in array (<code>int[3]</code>, etc.) or an initializer list, or any object that <code>ra::</code> accepts as scalar (see <a href="#x_002dis_002dscalar"><code>here</code></a>). The resulting expresion has rank and size according to the original object. Compare this with <a href="#x_002dscalar"><code>scalar</code></a>, which will always produce an expression of rank 0.
  1844. </p>
  1845. <p>Generally one can mix these types with <code>ra::</code> expressions without needing <code>ra::start</code>, but sometimes this isn&rsquo;t possible, for example for operators that must be class members.
  1846. </p>
  1847. <div class="example">
  1848. <pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
  1849. ra::Big&lt;int, 1&gt; y = {10, 20, 30};
  1850. cout &lt;&lt; (x+y) &lt;&lt; endl; // same as ra::start(x)+y
  1851. // x += y; // error: no match for operator+=
  1852. ra::start(x) += y; // ok
  1853. </pre><pre class="example">-| 3
  1854. 11 22 33
  1855. &rArr; x = { 11, 22, 33 }
  1856. </pre></div>
  1857. <a name="index-ptr"></a>
  1858. <a name="x_002dptr"></a><dl>
  1859. <dt><a name="index-ptr-1"></a>Function: <strong>ptr</strong> <em>pointer [size]</em></dt>
  1860. <dd><p>Create vector expression from raw <var>pointer</var>.
  1861. </p></dd></dl>
  1862. <p>The resulting expression has rank 1 and the size given. If <code>size</code> is not given, the expression has undefined size, and it will need to be matched with other expressions whose size <em>is</em> defined.
  1863. </p>
  1864. <p><code>ra::</code> doesn&rsquo;t know what is actually accessible through the pointer, so be careful. For instance:
  1865. </p>
  1866. <div class="example">
  1867. <pre class="verbatim">int p[] = {1, 2, 3};
  1868. ra::Big&lt;int, 1&gt; v3 {1, 2, 3};
  1869. ra::Big&lt;int, 1&gt; v4 {1, 2, 3, 4};
  1870. v3 += ra::ptr(p); // ok, shape (3): v3 = {2, 4, 6}
  1871. v4 += ra::ptr(p); // undefined, shape (4): bad access to p[3]
  1872. // cout &lt;&lt; (ra::ptr(p)+ra::TensorIndex&lt;0&gt;{}) &lt;&lt; endl; // ct error, expression has undefined shape
  1873. cout &lt;&lt; (ra::ptr(p, 3)+ra::TensorIndex&lt;0&gt;{}) &lt;&lt; endl; // ok, prints { 1, 3, 5 }
  1874. cout &lt;&lt; (ra::ptr(p, 4)+ra::TensorIndex&lt;0&gt;{}) &lt;&lt; endl; // undefined, bad access at p[4]
  1875. </pre></div>
  1876. <p>Of course in this example one could simply have used <code>p</code> instead of <code>ra::ptr(p)</code>, since the array type retains size information.
  1877. </p>
  1878. <div class="example">
  1879. <pre class="verbatim">v3 += p; // ok
  1880. v4 += p; // error checked by ra::, shape clash (4) += (3)
  1881. cout &lt;&lt; (p + ra::TensorIndex&lt;0&gt;{}) &lt;&lt; endl; // ok
  1882. </pre></div>
  1883. <a name="index-scalar"></a>
  1884. <a name="x_002dscalar"></a><dl>
  1885. <dt><a name="index-scalar-1"></a>Function: <strong>scalar</strong> <em>expr</em></dt>
  1886. <dd><p>Create scalar expression from <var>expr</var>.
  1887. </p></dd></dl>
  1888. <p>The primary use of this function is to bring a scalar object into the <code>ra::</code> namespace. A somewhat artificial example:
  1889. </p>
  1890. <div class="example">
  1891. <pre class="verbatim">struct W { int x; }
  1892. ra::Big&lt;W, 1&gt; w { {1}, {2}, {3} };
  1893. // error: no matching function for call to start(W)
  1894. // for_each([](auto &amp;&amp; a, auto &amp;&amp; b) { cout &lt;&lt; (a.x + b.x) &lt;&lt; endl; }, w, W {7});
  1895. // bring W into ra:: with ra::scalar
  1896. for_each([](auto &amp;&amp; a, auto &amp;&amp; b) { cout &lt;&lt; (a.x + b.x) &lt;&lt; endl; }, w, ra::scalar(W {7}));
  1897. </pre><pre class="example">-| 8
  1898. 9
  1899. 10
  1900. </pre></div>
  1901. <p>See also <a href="#x_002dscalar_002dchar_002dstar"><code>this example</code></a>.
  1902. </p>
  1903. <p>Since <code>scalar</code> produces an object with rank 0, it&rsquo;s also useful when dealing with nested arrays, even for objects that are already in <code>ra::</code>. Consider:
  1904. </p><div class="example">
  1905. <pre class="verbatim">using Vec2 = ra::Small&lt;double, 2&gt;;
  1906. Vec2 x {-1, 1};
  1907. ra::Big&lt;Vec2, 1&gt; c { {1, 2}, {2, 3}, {3, 4} };
  1908. // c += x // error: x has shape (2) and c has shape (3)
  1909. c += ra::scalar(x); // ok: scalar(x) has shape () and matches c.
  1910. </pre><pre class="example"> &rArr; c = { {0, 3}, {1, 4}, {2, 5} }
  1911. </pre></div>
  1912. <p>The result is {c(0)+x, c(1)+x, c(2)+x}. Compare this with
  1913. </p><div class="example">
  1914. <pre class="verbatim">c(ra::iota(2)) += x; // c(ra::iota(2)) with shape (2) matches x with shape (2)
  1915. </pre><pre class="example"> &rArr; c = { {-1, 2}, {2, 5}, {2, 5} }
  1916. </pre></div>
  1917. <p>where the result is {c(0)+x(0), c(1)+x(1), c(2)}.
  1918. </p>
  1919. <a name="index-iter"></a>
  1920. <a name="x_002diter"></a><dl>
  1921. <dt><a name="index-iter-1"></a>Function: <strong>iter</strong> <em>&lt;k&gt; (view)</em></dt>
  1922. <dd><p>Create iterator over the <var>k</var>-cells of <var>view</var>. If <var>k</var> is negative, it is interpreted as the negative of the frame rank. In the current version of <code>ra::</code>, <var>view</var> may have dynamic or static shape.
  1923. </p></dd></dl>
  1924. <div class="example">
  1925. <pre class="verbatim">ra::Big&lt;int, 2&gt; c {{1, 3, 2}, {7, 1, 3}};
  1926. cout &lt;&lt; &quot;max of each row: &quot; &lt;&lt; map([](auto &amp;&amp; a) { return amax(a); }, iter&lt;1&gt;(c)) &lt;&lt; endl;
  1927. ra::Big&lt;int, 1&gt; m({3}, 0);
  1928. scalar(m) = max(scalar(m), iter&lt;1&gt;(c));
  1929. cout &lt;&lt; &quot;max of each column: &quot; &lt;&lt; m &lt;&lt; endl;
  1930. m = 0;
  1931. for_each([&amp;m](auto &amp;&amp; a) { m = max(m, a); }, iter&lt;1&gt;(c));
  1932. cout &lt;&lt; &quot;max of each column again: &quot; &lt;&lt; m &lt;&lt; endl;
  1933. </pre><pre class="example">-| max of each row: 2
  1934. 3 7
  1935. max of each column: 3
  1936. 7 3 3
  1937. max of each column again: 3
  1938. 7 3 3
  1939. </pre></div>
  1940. <p>In the following example, <code>iter</code> emulates <code>scalar</code>. Note that the shape () of <code>iter&lt;1&gt;(m)</code> matches the shape (3) of <code>iter&lt;1&gt;(c)</code>. Thus, each of the 1-cells of <code>c</code> matches against the single 1-cell of <code>m</code>.
  1941. </p>
  1942. <div class="example">
  1943. <pre class="verbatim">m = 0;
  1944. iter&lt;1&gt;(m) = max(iter&lt;1&gt;(m), iter&lt;1&gt;(c));
  1945. cout &lt;&lt; &quot;max of each column yet again: &quot; &lt;&lt; m &lt;&lt; endl;
  1946. </pre><pre class="example">-| max of each column again: 3
  1947. 7 3 3
  1948. </pre></div>
  1949. <p>The following example computes the trace of each of the items [(-1)-cells] of <code>c</code>. </p>
  1950. <div class="example">
  1951. <pre class="verbatim">ra::Small&lt;int, 3, 2, 2&gt; c = ra::_0 - ra::_1 - 2*ra::_2;
  1952. cout &lt;&lt; &quot;c: &quot; &lt;&lt; c &lt;&lt; endl;
  1953. cout &lt;&lt; &quot;s: &quot; &lt;&lt; map([](auto &amp;&amp; a) { return sum(diag(a)); }, iter&lt;-1&gt;(c)) &lt;&lt; endl;
  1954. </pre><pre class="example">-| c: 0 -2
  1955. -1 -3
  1956. 1 -1
  1957. 0 -2
  1958. 2 0
  1959. 1 -1
  1960. s: -3 -1 -1
  1961. </pre></div>
  1962. <a name="index-sum"></a>
  1963. <a name="x_002dsum"></a><dl>
  1964. <dt><a name="index-sum-1"></a>Function: <strong>sum</strong> <em>expr</em></dt>
  1965. <dd><p>Return the sum (+) of the elements of <var>expr</var>, or 0 if expr is empty. This sum is performed in unspecified order.
  1966. </p></dd></dl>
  1967. <a name="index-prod"></a>
  1968. <a name="x_002dprod"></a><dl>
  1969. <dt><a name="index-prod-1"></a>Function: <strong>prod</strong> <em>expr</em></dt>
  1970. <dd><p>Return the product (*) of the elements of <var>expr</var>, or 1 if expr is empty. This product is performed in unspecified order.
  1971. </p></dd></dl>
  1972. <a name="index-amax"></a>
  1973. <a name="x_002damax"></a><dl>
  1974. <dt><a name="index-amax-1"></a>Function: <strong>amax</strong> <em>expr</em></dt>
  1975. <dd><p>Return the maximum of the elements of <var>expr</var>. If <var>expr</var> is empty, return <code>-std::numeric_limits&lt;T&gt;::infinity()</code> if the type supports it, otherwise <code>std::numeric_limits&lt;T&gt;::lowest()</code>, where <code>T</code> is the value type of the elements of <var>expr</var>.
  1976. </p></dd></dl>
  1977. <a name="index-amin"></a>
  1978. <a name="x_002damin"></a><dl>
  1979. <dt><a name="index-amin-1"></a>Function: <strong>amin</strong> <em>expr</em></dt>
  1980. <dd><p>Return the minimum of the elements of <var>expr</var>. If <var>expr</var> is empty, return <code>+std::numeric_limits&lt;T&gt;::infinity()</code> if the type supports it, otherwise <code>std::numeric_limits&lt;T&gt;::max()</code>, where <code>T</code> is the value type of the elements of <var>expr</var>.
  1981. </p></dd></dl>
  1982. <a name="index-early"></a>
  1983. <a name="x_002dearly"></a><dl>
  1984. <dt><a name="index-early-1"></a>Function: <strong>early</strong> <em>expr default</em></dt>
  1985. <dd><p><var>expr</var> shall be an array expression that returns <code>std::tuple&lt;bool, T&gt;</code>. <var>expr</var> is traversed as by <code>for_each</code>; if the expression ever returns <code>true</code> in the first element of the tuple, traversal stops and the second element is returned. If this never happens, <var>default</var> is returned instead.
  1986. </p></dd></dl>
  1987. <p>The following definition of elementwise <code>lexicographical_compare</code> relies on <code>early</code>.
  1988. </p>
  1989. <div class="example">
  1990. <pre class="verbatim">template &lt;class A, class B&gt;
  1991. inline bool lexicographical_compare(A &amp;&amp; a, B &amp;&amp; b)
  1992. {
  1993. return early(map([](auto &amp;&amp; a, auto &amp;&amp; b)
  1994. { return a==b ? std::make_tuple(false, true) : std::make_tuple(true, a&lt;b); },
  1995. a, b),
  1996. false);
  1997. }
  1998. </pre></div>
  1999. <a name="index-any"></a>
  2000. <a name="x_002dany"></a><dl>
  2001. <dt><a name="index-any-1"></a>Function: <strong>any</strong> <em>expr</em></dt>
  2002. <dd><p>Return <code>true</code> if any element of <var>expr</var> is true, <code>false</code> otherwise. The traversal of the array expression will stop as soon as possible, but the traversal order is not specified.
  2003. </p></dd></dl>
  2004. <a name="index-every"></a>
  2005. <a name="x_002devery"></a><dl>
  2006. <dt><a name="index-every-1"></a>Function: <strong>every</strong> <em>expr</em></dt>
  2007. <dd><p>Return <code>true</code> if every element of <var>expr</var> is true, <code>false</code> otherwise. The traversal of the array expression will stop as soon as possible, but the traversal order is not specified.
  2008. </p></dd></dl>
  2009. <a name="index-sqr"></a>
  2010. <a name="x_002dsqr"></a><dl>
  2011. <dt><a name="index-sqr-1"></a>Function: <strong>sqr</strong> <em>expr</em></dt>
  2012. <dd><p>Compute the square of <var>expr</var>.
  2013. </p></dd></dl>
  2014. <a name="index-sqrm"></a>
  2015. <a name="x_002dsqrm"></a><dl>
  2016. <dt><a name="index-sqrm-1"></a>Function: <strong>sqrm</strong> <em>expr</em></dt>
  2017. <dd><p>Compute the square of the norm-2 of <var>expr</var>, that is, <code>conj(expr)*expr</code>.
  2018. </p></dd></dl>
  2019. <a name="index-conj"></a>
  2020. <a name="x_002dconj"></a><dl>
  2021. <dt><a name="index-conj-1"></a>Function: <strong>conj</strong> <em>expr</em></dt>
  2022. <dd><p>Compute the complex conjugate of <var>expr</var>.
  2023. </p></dd></dl>
  2024. <a name="index-xI"></a>
  2025. <a name="x_002dxI"></a><dl>
  2026. <dt><a name="index-xI-1"></a>Function: <strong>xI</strong> <em>expr</em></dt>
  2027. <dd><p>Compute <code>(0+1j)</code> times <var>expr</var>.
  2028. </p></dd></dl>
  2029. <a name="index-rel_005ferror"></a>
  2030. <a name="x_002drel_002derror"></a><dl>
  2031. <dt><a name="index-rel_005ferror-1"></a>Function: <strong>rel_error</strong> <em>a b</em></dt>
  2032. <dd><p><var>a</var> and <var>b</var> are arbitrary array expressions. Compute the error of <var>a</var> relative to <var>b</var> as
  2033. </p>
  2034. <p><code>(a==0. &amp;&amp; b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b))</code>
  2035. </p>
  2036. </dd></dl>
  2037. <a name="index-none-1"></a>
  2038. <a name="x_002dnone"></a><dl>
  2039. <dt><a name="index-none-2"></a>Special&nbsp;object<!-- /@w -->: <strong>none</strong></dt>
  2040. <dd><p>Pass <code>none</code> to container constructors to indicate that the contents shouldn&rsquo;t be initialized. This is appropriate when the initialization you have in mind wouldn&rsquo;t fit in a constructor argument. For example:
  2041. </p>
  2042. <div class="example">
  2043. <pre class="verbatim">void old_style_initializer(int m, int n, double *);
  2044. ra::Big&lt;double&gt; b({2, 3}, ra::none);
  2045. old_style_initializer(2, 3, b.data());
  2046. </pre></div>
  2047. </dd></dl>
  2048. <hr>
  2049. <a name="Sources"></a>
  2050. <div class="header">
  2051. <p>
  2052. Next: <a href="#Indices" accesskey="n" rel="next">Indices</a>, Previous: <a href="#Reference" accesskey="p" rel="prev">Reference</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  2053. </div>
  2054. <a name="Sources-1"></a>
  2055. <h2 class="chapter">8 Sources</h2>
  2056. <table>
  2057. <tr><td width="10%"></td></tr>
  2058. <tr><td width="10%"><a name="Abr70"></a>[Abr70]</td><td width="90%">Philip S. Abrams. An APL machine. Technical report SLAC-114 UC-32 (MISC), Stanford Linear Accelerator Center, Stanford University, Stanford, CA, USA, February 1970.</td></tr>
  2059. <tr><td width="10%"></td></tr>
  2060. <tr><td width="10%"><a name="Ber87"></a>[Ber87]</td><td width="90%">Robert Bernecky. An introduction to function rank. ACM SIGAPL APL Quote Quad, 18(2):39–43, December 1987.</td></tr>
  2061. <tr><td width="10%"></td></tr>
  2062. <tr><td width="10%"><a name="bli17"></a>[bli17]</td><td width="90%">The Blitz++ meta-template library. <a href="http://blitz.sourceforge.net">http://blitz.sourceforge.net</a>, November 2017.</td></tr>
  2063. <tr><td width="10%"></td></tr>
  2064. <tr><td width="10%"><a name="Cha86"></a>[Cha86]</td><td width="90%">Gregory J. Chaitin. Physics in APL2, June 1986.</td></tr>
  2065. <tr><td width="10%"></td></tr>
  2066. <tr><td width="10%"><a name="FI68"></a>[FI68]</td><td width="90%">Adin D. Falkoff and Kenneth Eugene Iverson. APL\360 User’s manual. IBM Thomas J. Watson Research Center, August 1968.</td></tr>
  2067. <tr><td width="10%"></td></tr>
  2068. <tr><td width="10%"><a name="FI73"></a>[FI73]</td><td width="90%">Adin D. Falkoff and Kenneth Eugene Iverson. The design of APL. IBM Journal of Research and Development, 17(4):5–14, July 1973.</td></tr>
  2069. <tr><td width="10%"></td></tr>
  2070. <tr><td width="10%"><a name="FI78"></a>[FI78]</td><td width="90%">Adin D. Falkoff and Kenneth Eugene Iverson. The evolution of APL. ACM SIGAPL APL, 9(1):30– 44, 1978.</td></tr>
  2071. <tr><td width="10%"></td></tr>
  2072. <tr><td width="10%"><a name="J-S"></a>[J S]</td><td width="90%">J Primer. J Software, <a href="https://www.jsoftware.com/help/primer/contents.htm">https://www.jsoftware.com/help/primer/contents.htm</a>, November 2017.</td></tr>
  2073. <tr><td width="10%"></td></tr>
  2074. <tr><td width="10%"><a name="Mat"></a>[Mat]</td><td width="90%">MathWorks. MATLAB documentation, <a href="https://www.mathworks.com/help/matlab/">https://www.mathworks.com/help/matlab/</a>, November 2017.</td></tr>
  2075. <tr><td width="10%"></td></tr>
  2076. <tr><td width="10%"><a name="num17"></a>[num17]</td><td width="90%">NumPy. <a href="http://www.numpy.org">http://www.numpy.org</a>, November 2017.</td></tr>
  2077. <tr><td width="10%"></td></tr>
  2078. <tr><td width="10%"><a name="Ric08"></a>[Ric08]</td><td width="90%">Henry Rich. J for C programmers, February 2008.</td></tr>
  2079. <tr><td width="10%"></td></tr>
  2080. <tr><td width="10%"><a name="SSM14"></a>[SSM14]</td><td width="90%">Justin Slepak, Olin Shivers, and Panagiotis Manolios. An array-oriented language with static rank polymorphism. In Z. Shao, editor, ESOP 2014, LNCS 8410, pages 27–46, 2014.</td></tr>
  2081. <tr><td width="10%"></td></tr>
  2082. <tr><td width="10%"><a name="Vel01"></a>[Vel01]</td><td width="90%">Todd Veldhuizen. Blitz++ user’s guide, February 2001.</td></tr>
  2083. <tr><td width="10%"></td></tr>
  2084. <tr><td width="10%"><a name="Wad90"></a>[Wad90]</td><td width="90%">Philip Wadler. Deforestation: transforming programs to eliminate trees. Theoretical Computer Science, 73(2): 231&ndash;248, June 1990. <a href="https://doi.org/10.1016/0304-3975%2890%2990147-A">https://doi.org/10.1016/0304-3975%2890%2990147-A</a></td></tr>
  2085. </table>
  2086. <hr>
  2087. <a name="Indices"></a>
  2088. <div class="header">
  2089. <p>
  2090. Next: <a href="#Notes" accesskey="n" rel="next">Notes</a>, Previous: <a href="#Sources" accesskey="p" rel="prev">Sources</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  2091. </div>
  2092. <a name="Indices-1"></a>
  2093. <h2 class="unnumbered">Indices</h2>
  2094. <table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
  2095. &nbsp;
  2096. <a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
  2097. &nbsp;
  2098. <a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
  2099. &nbsp;
  2100. <a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
  2101. &nbsp;
  2102. <a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
  2103. &nbsp;
  2104. <a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
  2105. &nbsp;
  2106. <a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
  2107. &nbsp;
  2108. <a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
  2109. &nbsp;
  2110. <a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
  2111. &nbsp;
  2112. <a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
  2113. &nbsp;
  2114. <a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
  2115. &nbsp;
  2116. <a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
  2117. &nbsp;
  2118. <a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
  2119. &nbsp;
  2120. <a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
  2121. &nbsp;
  2122. <a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
  2123. &nbsp;
  2124. <a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
  2125. &nbsp;
  2126. <a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
  2127. &nbsp;
  2128. <a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
  2129. &nbsp;
  2130. <a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
  2131. &nbsp;
  2132. </td></tr></table>
  2133. <table class="index-cp" border="0">
  2134. <tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
  2135. <tr><td colspan="4"> <hr></td></tr>
  2136. <tr><th><a name="Indices_cp_letter-A">A</a></th><td></td><td></td></tr>
  2137. <tr><td></td><td valign="top"><a href="#index-amax"><code>amax</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2138. <tr><td></td><td valign="top"><a href="#index-amin"><code>amin</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2139. <tr><td></td><td valign="top"><a href="#index-any"><code>any</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2140. <tr><td></td><td valign="top"><a href="#index-APL">APL</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Why-C_002b_002b">Why C++</a></td></tr>
  2141. <tr><td></td><td valign="top"><a href="#index-assert">assert</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Functions">Functions</a></td></tr>
  2142. <tr><td></td><td valign="top"><a href="#index-at"><code>at</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2143. <tr><td colspan="4"> <hr></td></tr>
  2144. <tr><th><a name="Indices_cp_letter-B">B</a></th><td></td><td></td></tr>
  2145. <tr><td></td><td valign="top"><a href="#index-BLIS">BLIS</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2146. <tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b">Blitz++</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
  2147. <tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b-1">Blitz++</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Other-libraries">Other libraries</a></td></tr>
  2148. <tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b-2">Blitz++</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
  2149. <tr><td></td><td valign="top"><a href="#index-broadcasting_002c-singleton_002c-newaxis">broadcasting, singleton, newaxis</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
  2150. <tr><td></td><td valign="top"><a href="#index-broadcasting_002c-singleton_002c-Numpy">broadcasting, singleton, Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2151. <tr><td></td><td valign="top"><a href="#index-built_002din-array">built-in array</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2152. <tr><td colspan="4"> <hr></td></tr>
  2153. <tr><th><a name="Indices_cp_letter-C">C</a></th><td></td><td></td></tr>
  2154. <tr><td></td><td valign="top"><a href="#index-cast"><code>cast</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2155. <tr><td></td><td valign="top"><a href="#index-cell">cell</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
  2156. <tr><td></td><td valign="top"><a href="#index-collapse"><code>collapse</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2157. <tr><td></td><td valign="top"><a href="#index-concrete"><code>concrete</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2158. <tr><td></td><td valign="top"><a href="#index-conj"><code>conj</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2159. <tr><td></td><td valign="top"><a href="#index-container">container</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Using-the-library">Using the library</a></td></tr>
  2160. <tr><td colspan="4"> <hr></td></tr>
  2161. <tr><th><a name="Indices_cp_letter-D">D</a></th><td></td><td></td></tr>
  2162. <tr><td></td><td valign="top"><a href="#index-diag"><code>diag</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2163. <tr><td colspan="4"> <hr></td></tr>
  2164. <tr><th><a name="Indices_cp_letter-E">E</a></th><td></td><td></td></tr>
  2165. <tr><td></td><td valign="top"><a href="#index-early"><code>early</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2166. <tr><td></td><td valign="top"><a href="#index-error">error</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Functions">Functions</a></td></tr>
  2167. <tr><td></td><td valign="top"><a href="#index-every"><code>every</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2168. <tr><td></td><td valign="top"><a href="#index-explode"><code>explode</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2169. <tr><td colspan="4"> <hr></td></tr>
  2170. <tr><th><a name="Indices_cp_letter-F">F</a></th><td></td><td></td></tr>
  2171. <tr><td></td><td valign="top"><a href="#index-FFTW">FFTW</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2172. <tr><td></td><td valign="top"><a href="#index-foreign-type">foreign type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2173. <tr><td></td><td valign="top"><a href="#index-format_005farray"><code>format_array</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2174. <tr><td></td><td valign="top"><a href="#index-for_005feach"><code>for_each</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2175. <tr><td></td><td valign="top"><a href="#index-frame">frame</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
  2176. <tr><td></td><td valign="top"><a href="#index-from"><code>from</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2177. <tr><td colspan="4"> <hr></td></tr>
  2178. <tr><th><a name="Indices_cp_letter-G">G</a></th><td></td><td></td></tr>
  2179. <tr><td></td><td valign="top"><a href="#index-Guile">Guile</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2180. <tr><td colspan="4"> <hr></td></tr>
  2181. <tr><th><a name="Indices_cp_letter-I">I</a></th><td></td><td></td></tr>
  2182. <tr><td></td><td valign="top"><a href="#index-imag_005fpart"><code>imag_part</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2183. <tr><td></td><td valign="top"><a href="#index-iter"><code>iter</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2184. <tr><td colspan="4"> <hr></td></tr>
  2185. <tr><th><a name="Indices_cp_letter-J">J</a></th><td></td><td></td></tr>
  2186. <tr><td></td><td valign="top"><a href="#index-J">J</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Why-C_002b_002b">Why C++</a></td></tr>
  2187. <tr><td colspan="4"> <hr></td></tr>
  2188. <tr><th><a name="Indices_cp_letter-N">N</a></th><td></td><td></td></tr>
  2189. <tr><td></td><td valign="top"><a href="#index-none"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2190. <tr><td></td><td valign="top"><a href="#index-none-1"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2191. <tr><td></td><td valign="top"><a href="#index-noshape"><code>noshape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2192. <tr><td></td><td valign="top"><a href="#index-Numpy">Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
  2193. <tr><td></td><td valign="top"><a href="#index-Numpy-1">Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
  2194. <tr><td></td><td valign="top"><a href="#index-Numpy-2">Numpy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2195. <tr><td colspan="4"> <hr></td></tr>
  2196. <tr><th><a name="Indices_cp_letter-O">O</a></th><td></td><td></td></tr>
  2197. <tr><td></td><td valign="top"><a href="#index-OpenGL">OpenGL</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2198. <tr><td></td><td valign="top"><a href="#index-order_002c-column_002dmajor">order, column-major</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2199. <tr><td></td><td valign="top"><a href="#index-order_002c-row_002dmajor">order, row-major</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
  2200. <tr><td></td><td valign="top"><a href="#index-other-libraries_002c-interfacing-with">other libraries, interfacing with</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2201. <tr><td colspan="4"> <hr></td></tr>
  2202. <tr><th><a name="Indices_cp_letter-P">P</a></th><td></td><td></td></tr>
  2203. <tr><td></td><td valign="top"><a href="#index-pack"><code>pack</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2204. <tr><td></td><td valign="top"><a href="#index-pick"><code>pick</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2205. <tr><td></td><td valign="top"><a href="#index-ply"><code>ply</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2206. <tr><td></td><td valign="top"><a href="#index-prod"><code>prod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2207. <tr><td></td><td valign="top"><a href="#index-ptr"><code>ptr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2208. <tr><td></td><td valign="top"><a href="#index-Python">Python</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2209. <tr><td colspan="4"> <hr></td></tr>
  2210. <tr><th><a name="Indices_cp_letter-R">R</a></th><td></td><td></td></tr>
  2211. <tr><td></td><td valign="top"><a href="#index-rank_002c-dynamic">rank, dynamic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2212. <tr><td></td><td valign="top"><a href="#index-rank_002c-static">rank, static</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2213. <tr><td></td><td valign="top"><a href="#index-real_005fpart"><code>real_part</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2214. <tr><td></td><td valign="top"><a href="#index-rel_005ferror"><code>rel_error</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2215. <tr><td></td><td valign="top"><a href="#index-reverse"><code>reverse</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2216. <tr><td colspan="4"> <hr></td></tr>
  2217. <tr><th><a name="Indices_cp_letter-S">S</a></th><td></td><td></td></tr>
  2218. <tr><td></td><td valign="top"><a href="#index-scalar"><code>scalar</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2219. <tr><td></td><td valign="top"><a href="#index-shape"><code>shape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2220. <tr><td></td><td valign="top"><a href="#index-shape-agreement_002c-prefix">shape agreement, prefix</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
  2221. <tr><td></td><td valign="top"><a href="#index-shape-agreement_002c-suffix">shape agreement, suffix</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
  2222. <tr><td></td><td valign="top"><a href="#index-size"><code>size</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2223. <tr><td></td><td valign="top"><a href="#index-sqr"><code>sqr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2224. <tr><td></td><td valign="top"><a href="#index-sqrm"><code>sqrm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2225. <tr><td></td><td valign="top"><a href="#index-start"><code>start</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
  2226. <tr><td></td><td valign="top"><a href="#index-start-1"><code>start</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2227. <tr><td></td><td valign="top"><a href="#index-stencil"><code>stencil</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2228. <tr><td></td><td valign="top"><a href="#index-sum"><code>sum</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2229. <tr><td colspan="4"> <hr></td></tr>
  2230. <tr><th><a name="Indices_cp_letter-T">T</a></th><td></td><td></td></tr>
  2231. <tr><td></td><td valign="top"><a href="#index-transpose"><code>transpose</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2232. <tr><td colspan="4"> <hr></td></tr>
  2233. <tr><th><a name="Indices_cp_letter-U">U</a></th><td></td><td></td></tr>
  2234. <tr><td></td><td valign="top"><a href="#index-uninitialized-container">uninitialized container</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2235. <tr><td colspan="4"> <hr></td></tr>
  2236. <tr><th><a name="Indices_cp_letter-V">V</a></th><td></td><td></td></tr>
  2237. <tr><td></td><td valign="top"><a href="#index-view">view</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2238. <tr><td></td><td valign="top"><a href="#index-view_002c-rank-0">view, rank 0</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2239. <tr><td colspan="4"> <hr></td></tr>
  2240. <tr><th><a name="Indices_cp_letter-W">W</a></th><td></td><td></td></tr>
  2241. <tr><td></td><td valign="top"><a href="#index-where"><code>where</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2242. <tr><td></td><td valign="top"><a href="#index-withshape"><code>withshape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2243. <tr><td></td><td valign="top"><a href="#index-wrank"><code>wrank</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2244. <tr><td colspan="4"> <hr></td></tr>
  2245. <tr><th><a name="Indices_cp_letter-X">X</a></th><td></td><td></td></tr>
  2246. <tr><td></td><td valign="top"><a href="#index-xI"><code>xI</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2247. <tr><td colspan="4"> <hr></td></tr>
  2248. </table>
  2249. <table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
  2250. &nbsp;
  2251. <a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
  2252. &nbsp;
  2253. <a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
  2254. &nbsp;
  2255. <a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
  2256. &nbsp;
  2257. <a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
  2258. &nbsp;
  2259. <a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
  2260. &nbsp;
  2261. <a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
  2262. &nbsp;
  2263. <a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
  2264. &nbsp;
  2265. <a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
  2266. &nbsp;
  2267. <a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
  2268. &nbsp;
  2269. <a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
  2270. &nbsp;
  2271. <a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
  2272. &nbsp;
  2273. <a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
  2274. &nbsp;
  2275. <a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
  2276. &nbsp;
  2277. <a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
  2278. &nbsp;
  2279. <a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
  2280. &nbsp;
  2281. <a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
  2282. &nbsp;
  2283. <a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
  2284. &nbsp;
  2285. <a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
  2286. &nbsp;
  2287. </td></tr></table>
  2288. <hr>
  2289. <a name="Notes"></a>
  2290. <div class="header">
  2291. <p>
  2292. Previous: <a href="#Indices" accesskey="p" rel="prev">Indices</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  2293. </div>
  2294. <a name="Notes-1"></a>
  2295. <h2 class="unnumbered">Notes</h2>
  2296. <ol>
  2297. <li> <code>ra::</code> uses the following features that are not standard (but that all major compilers support): <code>#pragma once</code>, and <code>__COUNTER__</code>.
  2298. </li></ol>
  2299. <div class="footnote">
  2300. <hr>
  2301. <h4 class="footnotes-heading">Footnotes</h4>
  2302. <h3><a name="FOOT1" href="#DOCF1">(1)</a></h3>
  2303. <p>/ə&rsquo;ɹ-eɪ/, I guess.</p>
  2304. <h3><a name="FOOT2" href="#DOCF2">(2)</a></h3>
  2305. <p>Cf. <a href="https://en.wikipedia.org/wiki/Dope_vector"><em>dope vector</em></a></p>
  2306. <h3><a name="FOOT3" href="#DOCF3">(3)</a></h3>
  2307. <p>Examples given without context assume that one has declared <code>using std::cout;</code>, etc.</p>
  2308. <h3><a name="FOOT4" href="#DOCF4">(4)</a></h3>
  2309. <p>The brace-list constructors of rank 2 and higher aren&rsquo;t supported on types of runtime rank, because in the C++ grammar, a nested initializer list doesn&rsquo;t always define a rank unambiguously.</p>
  2310. <h3><a name="FOOT5" href="#DOCF5">(5)</a></h3>
  2311. <p>You can still use pointers or <code>std::initializer_list</code>s for shape by wrapping them in the functions <code>ptr</code> or <code>vector</code>, respectively.</p>
  2312. <h3><a name="FOOT6" href="#DOCF6">(6)</a></h3>
  2313. <p>The brace-list constructors aren&rsquo;t rank extending, because giving the ravel is incompatible with rank extension. They are size-strict —you must give every element.</p>
  2314. <h3><a name="FOOT7" href="#DOCF7">(7)</a></h3>
  2315. <p>Prefix agreement is chosen for <code>ra::</code> because of the availability of a <a href="#The-rank-conjunction">rank conjunction</a> [<a href="#Sources">Ber87</a>]
  2316. and <a href="#Cell-iteration">cell iterators of arbitrary rank</a>. This allows rank extension to be performed at multiple axes of an array expression.</p>
  2317. <h3><a name="FOOT8" href="#DOCF8">(8)</a></h3>
  2318. <p><code>&amp;&amp;</code>, <code>||</code> are short-circuiting as array operations; the elements of the second operand won&rsquo;t be evaluated if the elements of the first one evaluate to <code>false</code> or <code>true</code>, respectively.
  2319. Note that if both operands are of rank 0 and at least one of them is an <code>ra::</code> object, they is no way to preserve the behavior of <code>&amp;&amp;</code> and <code>||</code> with built in types and avoid evaluating both, since the overloaded operators <a href="http://en.cppreference.com/w/cpp/language/operators">are normal functions</a>.</p>
  2320. </div>
  2321. <hr>
  2322. </body>
  2323. </html>