index.html 168 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <!-- ra:: (version 26, updated 2023 November 21)
  7. (c) Daniel Llorens 2005-2023
  8. Permission is granted to copy, distribute and/or modify this document
  9. under the terms of the GNU Free Documentation License, Version 1.3 or
  10. any later version published by the Free Software Foundation; with no
  11. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. -->
  12. <title>ra:: — An array library for C++20</title>
  13. <meta name="description" content="ra:: — An array library for C++20">
  14. <meta name="keywords" content="ra:: — An array library for C++20">
  15. <meta name="resource-type" content="document">
  16. <meta name="distribution" content="global">
  17. <meta name="Generator" content="makeinfo">
  18. <meta name="viewport" content="width=device-width,initial-scale=1">
  19. <link href="#Top" rel="start" title="Top">
  20. <link href="#Indices" rel="index" title="Indices">
  21. <link href="#Overview" rel="next" title="Overview">
  22. <style type="text/css">
  23. <!--
  24. a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
  25. a.summary-letter {text-decoration: none}
  26. blockquote.indentedblock {margin-right: 0em}
  27. div.display {margin-left: 3.2em}
  28. div.example {margin-left: 3.2em}
  29. kbd {font-style: oblique}
  30. pre.display {font-family: inherit}
  31. pre.format {font-family: inherit}
  32. pre.menu-comment {font-family: serif}
  33. pre.menu-preformatted {font-family: serif}
  34. span.nolinebreak {white-space: nowrap}
  35. span.roman {font-family: initial; font-weight: normal}
  36. span.sansserif {font-family: sans-serif; font-weight: normal}
  37. span:hover a.copiable-anchor {visibility: visible}
  38. ul.no-bullet {list-style: none}
  39. -->
  40. </style>
  41. <script type='text/javascript'>
  42. MathJax = {
  43. options: {
  44. skipHtmlTags: {'[-]': ['pre']},
  45. ignoreHtmlClass: 'tex2jax_ignore',
  46. processHtmlClass: 'tex2jax_process'
  47. },
  48. };
  49. </script><script type="text/javascript" id="MathJax-script" async
  50. src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
  51. </script>
  52. </head>
  53. <body lang="en">
  54. <h1 class="settitle" align="center">ra:: — An array library for C++20</h1>
  55. <div class="top" id="Top">
  56. <div class="header">
  57. <p>
  58. Next: <a href="#Overview" accesskey="n" rel="next">Overview</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  59. </div>
  60. <span id="ra_003a_003a"></span><h1 class="top"><code>ra::</code></h1>
  61. <p><code>ra::</code> (version 26, updated 2023 November 21)
  62. </p>
  63. <p>(c) Daniel Llorens 2005&ndash;2023
  64. </p>
  65. <div class="display">
  66. <pre class="display">Permission is granted to copy, distribute and/or modify this document
  67. under the terms of the GNU Free Documentation License, Version 1.3 or
  68. any later version published by the Free Software Foundation; with no
  69. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  70. </pre></div>
  71. <p><code>ra::</code><a id="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.
  72. </p>
  73. <ul class="section-toc">
  74. <li><a href="#Overview" accesskey="1">Overview</a></li>
  75. <li><a href="#Usage" accesskey="2">Usage</a></li>
  76. <li><a href="#Extras" accesskey="3">Extras</a></li>
  77. <li><a href="#Hazards" accesskey="4">Hazards</a></li>
  78. <li><a href="#Internals" accesskey="5">Internals</a></li>
  79. <li><a href="#The-future" accesskey="6">The future</a></li>
  80. <li><a href="#Reference" accesskey="7">Reference</a></li>
  81. <li><a href="#Sources" accesskey="8">Sources</a></li>
  82. <li><a href="#Indices" accesskey="9">Indices</a></li>
  83. <li><a href="#Notes">Notes</a></li>
  84. </ul>
  85. <hr>
  86. <div class="chapter" id="Overview">
  87. <div class="header">
  88. <p>
  89. Next: <a href="#Usage" accesskey="n" rel="next">Usage</a>, Previous: <a href="#Top" accesskey="p" rel="prev"><code>ra::</code></a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  90. </div>
  91. <span id="Overview-1"></span><h2 class="chapter">1 Overview</h2>
  92. <span id="index-length"></span>
  93. <span id="index-rank"></span>
  94. <span id="index-shape"></span>
  95. <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 of all the <em>lengths</em> (n₀, n₁, ... nᵣ₋₁) is the <em>shape</em> of the array. We speak of a rank-<em class='tex2jax_process'>\(r\)</em> array or of an <em class='tex2jax_process'>\(r\)</em>-array.
  96. </p>
  97. <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.
  98. </p>
  99. <p>Arrays (as a representation of <em>matrices</em>, <em>vectors</em>, or <em>tensors</em>) are common objects in math and programming, and it is very useful to be able to manipulate arrays as individual entities rather than as aggregates. Not only is
  100. </p>
  101. <pre class="verbatim">A = B+C;
  102. </pre>
  103. <p>much more compact and easier to read than
  104. </p>
  105. <pre class="verbatim">for (int i=0; i!=m; ++i)
  106. for (int j=0; j!=n; ++j)
  107. for (int k=0; k!=p; ++k)
  108. A(i, j, k) = B(i, j, k)+C(i, j, k);
  109. </pre>
  110. <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.
  111. </p>
  112. <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>. This is wasteful if the arrays involved are large.
  113. </p>
  114. <span id="index-Blitz_002b_002b"></span>
  115. <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>]
  116. , or ‘deforestation’ [<a href="#Sources">Wad90</a>]
  117. . 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>]
  118. . 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.
  119. </p>
  120. <ul class="section-toc">
  121. <li><a href="#Rank-polymorphism" accesskey="1">Rank polymorphism</a></li>
  122. <li><a href="#Drag-along-and-beating" accesskey="2">Drag along and beating</a></li>
  123. <li><a href="#Why-C_002b_002b" accesskey="3">Why C++</a></li>
  124. <li><a href="#Guidelines" accesskey="4">Guidelines</a></li>
  125. <li><a href="#Other-libraries" accesskey="5">Other array libraries</a></li>
  126. </ul>
  127. <hr>
  128. <div class="section" id="Rank-polymorphism">
  129. <div class="header">
  130. <p>
  131. 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>
  132. </div>
  133. <span id="Rank-polymorphism-1"></span><h3 class="section">1.1 Rank polymorphism</h3>
  134. <p><em>Rank polymorphism</em> is the ability to treat an array of rank <em class='tex2jax_process'>\(r\)</em> as an array of lower rank where the elements are themselves arrays.
  135. </p>
  136. <span id="index-cell"></span>
  137. <span id="index-frame"></span>
  138. <p>For example, think of a matrix A, a 2-array with shape (n₀, n₁) where the elements A(i₀, i₁) are numbers. If we consider the subarrays A(0, ...), A(1, ...), ..., A(n₀-1, ...) as individual elements, then we have a new view of A as a 1-array of length 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 class='tex2jax_process'>\(r\)</em> the (<em class='tex2jax_process'>\(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>.
  139. </p>
  140. <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
  141. </p>
  142. <blockquote>
  143. <p>A: [A(0), A(1), ...]
  144. </p></blockquote>
  145. <p>and the items of A(i₀), etc. are in turn stored in the same way, so
  146. </p>
  147. <blockquote>
  148. <p>A: [A(0): [A(0, 0), A(0, 1) ...], ...]
  149. </p></blockquote>
  150. <p>and the same for the items of A(i₀, i₁), etc.
  151. </p>
  152. <blockquote>
  153. <p>A: [[A(0, 0): [A(0, 0, 0), A(0, 0, 1) ...], A(0, 1): [A(0, 1, 0), A(0, 1, 1) ...]], ...]
  154. </p></blockquote>
  155. <span id="index-order_002c-row_002dmajor"></span>
  156. <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 array libraries</a>). A row-major array A with shape (n₀, n₁, ... nᵣ₋₁) can be looked up like this:
  157. </p>
  158. <span id="x_002dsteps"></span><blockquote>
  159. <p>A(i₀, i₁, ...) = (storage-of-A) [(((i₀n₁ + i₁)n₂ + i₂)n₃ + ...)+iᵣ₋₁] = (storage-of-A) [o + s₀i₀ + s₁i₁ + ...]
  160. </p></blockquote>
  161. <span id="index-step"></span>
  162. <span id="index-stride"></span>
  163. <p>where the numbers (s₀, s₁, ...) are called the <em>steps</em><a id="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
  164. </p>
  165. <blockquote>
  166. <p>A ≡ ((storage-of-A), o, (s₀, s₁, ...))
  167. </p></blockquote>
  168. <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 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.
  169. </p>
  170. <p>A basic example is obtaining the i₀-th item of A:
  171. </p>
  172. <blockquote>
  173. <p>A(i₀) ≡ ((storage-of-A), o+s₀i₀, (s₁, ...))
  174. </p></blockquote>
  175. <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>).
  176. </p>
  177. <hr>
  178. </div>
  179. <div class="section" id="Drag-along-and-beating">
  180. <div class="header">
  181. <p>
  182. 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>
  183. </div>
  184. <span id="Drag-along-and-beating-1"></span><h3 class="section">1.2 Drag along and beating</h3>
  185. <p>These two fundamental array optimizations are described in [<a href="#Sources">Abr70</a>]
  186. .
  187. </p>
  188. <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.
  189. </p>
  190. <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_002dsteps">step</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.
  191. </p>
  192. <hr>
  193. </div>
  194. <div class="section" id="Why-C_002b_002b">
  195. <div class="header">
  196. <p>
  197. 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>
  198. </div>
  199. <span id="Why-C_002b_002b-1"></span><h3 class="section">1.3 Why C++</h3>
  200. <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.
  201. </p>
  202. <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.
  203. </p>
  204. <span id="index-APL"></span>
  205. <span id="index-J"></span>
  206. <p>The classic array languages, APL [<a href="#Sources">FI73</a>]
  207. and J [<a href="#Sources">Ric08</a>]
  208. , 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>]
  209. this is both for reasons of flexibility (e.g. to obtain predictable memory layout and machine types) and of performance.
  210. </p>
  211. <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.
  212. </p>
  213. <p>In C++, a library is enough.
  214. </p>
  215. <hr>
  216. </div>
  217. <div class="section" id="Guidelines">
  218. <div class="header">
  219. <p>
  220. Next: <a href="#Other-libraries" accesskey="n" rel="next">Other array 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>
  221. </div>
  222. <span id="Guidelines-1"></span><h3 class="section">1.4 Guidelines</h3>
  223. <p><code>ra::</code> attempts to be general, consistent, and transparent.
  224. </p>
  225. <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.
  226. </p>
  227. <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 it makes sense. 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.
  228. </p>
  229. <p>Sometimes consistency requires a choice. For example, given array views A and B, <code>A=B</code> copies the contents of view <code>B</code> into view <code>A</code>. To change view <code>A</code> instead (to treat <code>A</code> as a pointer) would be the default meaning of <code>A=B</code> 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.
  230. </p>
  231. <p>Transparency is achieved by avoiding unnecessary abstraction. An array view consists of a pointer and a list of steps and I see no point in hiding it. Manipulating the steps 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.
  232. </p>
  233. <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 usable 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 in compact columns, or that the real parts are interleaved with the imaginary parts.
  234. </p>
  235. <hr>
  236. </div>
  237. <div class="section" id="Other-libraries">
  238. <div class="header">
  239. <p>
  240. 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>
  241. </div>
  242. <span id="Other-array-libraries"></span><h3 class="section">1.5 Other array libraries</h3>
  243. <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.
  244. </p>
  245. <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++!
  246. </p>
  247. <ul class="section-toc">
  248. <li><a href="#Standard-C_002b_002b" accesskey="1">Standard C++</a></li>
  249. <li><a href="#Blitz_002b_002b" accesskey="2">Blitz++</a></li>
  250. <li><a href="#Other-C_002b_002b-libraries" accesskey="3">Other C++ libraries</a></li>
  251. <li><a href="#Other-languages" accesskey="4">Other languages</a></li>
  252. </ul>
  253. <div class="subsection" id="Standard-C_002b_002b">
  254. <h4 class="subsection">1.5.1 Standard C++</h4>
  255. <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 shape or rank at runtime, and are generally too limited.
  256. </p>
  257. <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.
  258. </p>
  259. <p><code>ra::</code> accepts built-in arrays and standard library types as array objects (see <a href="#Compatibility">Compatibility</a>).
  260. </p>
  261. </div>
  262. <div class="subsection" id="Blitz_002b_002b">
  263. <h4 class="subsection">1.5.2 Blitz++</h4>
  264. <span id="index-Blitz_002b_002b-1"></span>
  265. <p>Blitz++ [<a href="#Sources">bli17</a>]
  266. pioneered the use of expression templates in C++. It supported higher rank arrays, as high as it was practical in C++98, but not runtime rank. It also supported small arrays with compile time shape (<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.
  267. </p>
  268. <p>However, the implementation had to fight the limitations of C++98, and it offered no general rank extension mechanism.
  269. </p>
  270. <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 (data-owning) types are values, and views are distinct types. 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.
  271. </p>
  272. </div>
  273. <div class="subsection" id="Other-C_002b_002b-libraries">
  274. <h4 class="subsection">1.5.3 Other C++ libraries</h4>
  275. <p>TODO
  276. </p>
  277. </div>
  278. <div class="subsection" id="Other-languages">
  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 / Python-Numpy).
  281. </p>
  282. <hr>
  283. </div>
  284. </div>
  285. </div>
  286. <div class="chapter" id="Usage">
  287. <div class="header">
  288. <p>
  289. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  290. </div>
  291. <span id="Usage-1"></span><h2 class="chapter">2 Usage</h2>
  292. <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>.
  293. </p>
  294. <ul class="section-toc">
  295. <li><a href="#Using-the-library" accesskey="1">Using <code>ra::</code></a></li>
  296. <li><a href="#Containers-and-views" accesskey="2">Containers and views</a></li>
  297. <li><a href="#Array-operations" accesskey="3">Array operations</a></li>
  298. <li><a href="#Rank-extension" accesskey="4">Rank extension</a></li>
  299. <li><a href="#Cell-iteration" accesskey="5">Cell iteration</a></li>
  300. <li><a href="#Slicing" accesskey="6">Slicing</a></li>
  301. <li><a href="#Functions" accesskey="7">Functions</a></li>
  302. <li><a href="#The-rank-conjunction" accesskey="8">The rank conjunction</a></li>
  303. <li><a href="#Compatibility" accesskey="9">Compatibility</a></li>
  304. <li><a href="#Extension">Extension</a></li>
  305. <li><a href="#Error-handling">Error handling</a></li>
  306. </ul>
  307. <hr>
  308. <div class="section" id="Using-the-library">
  309. <div class="header">
  310. <p>
  311. 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>
  312. </div>
  313. <span id="Using-ra_003a_003a"></span><h3 class="section">2.1 Using <code>ra::</code></h3>
  314. <p><code>ra::</code> is a header only library with no dependencies other than the standard library, 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.
  315. </p>
  316. <p>A compiler with C++20 support is required. For the current version this means at least <b>gcc 11</b> with <samp>-std=c++20</samp>. Some C++23 features are available with <samp>-std=c++2b</samp>. Check the top <code>README.md</code> for more up-to-date information.
  317. </p>
  318. <p>Here is a minimal program<a id="DOCF3" href="#FOOT3"><sup>3</sup></a>:
  319. </p>
  320. <div class="example">
  321. <pre class="verbatim">#include &quot;ra/ra.hh&quot;
  322. #include &lt;iostream&gt;
  323. int main()
  324. {
  325. ra::Big&lt;char, 2&gt; A({2, 5}, &quot;helloworld&quot;);
  326. std::cout &lt;&lt; ra::noshape &lt;&lt; format_array(transpose&lt;1, 0&gt;(A), &quot;|&quot;) &lt;&lt; std::endl;
  327. }
  328. </pre><pre class="example">-| h|w
  329. e|o
  330. l|r
  331. l|l
  332. d|d
  333. </pre></div>
  334. <p>The following headers are <em>not</em> included by default:
  335. </p><ul>
  336. <li> <code>&quot;ra/dual.hh&quot;</code>: A dual number type for simple uses of automatic differentiation.
  337. </li><li> <code>&quot;ra/test.hh&quot;</code>, <code>&quot;ra/bench.hh&quot;</code>: Used by the test and benchmark suites.
  338. </li></ul>
  339. <p>The header <code>&quot;ra/bootstrap.hh&quot;</code> can be used to configure <a href="#Error-handling">Error handling</a>. You don&rsquo;t need to modify the header, but the configuration depends on including <code>&quot;ra/bootstrap.hh&quot;</code> before the rest of <code>ra::</code> in order to override the default handler. All other headers are for internal use by <code>ra::</code>.
  340. </p>
  341. <span id="index-container"></span>
  342. <hr>
  343. </div>
  344. <div class="section" id="Containers-and-views">
  345. <div class="header">
  346. <p>
  347. Next: <a href="#Array-operations" accesskey="n" rel="next">Array operations</a>, Previous: <a href="#Using-the-library" accesskey="p" rel="prev">Using <code>ra::</code></a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  348. </div>
  349. <span id="Containers-and-views-1"></span><h3 class="section">2.2 Containers and views</h3>
  350. <p><code>ra::</code> offers two kinds of data objects. The first kind, the <em>container</em>, owns its data. Creating a container uses up memory and destroying it causes that memory to be freed.
  351. </p>
  352. <span id="index-compile_002dtime"></span>
  353. <span id="index-ct"></span>
  354. <span id="index-runtime"></span>
  355. <span id="index-rt"></span>
  356. <p>There are three kinds of containers (ct: compile-time, rt: runtime): 1) ct size, 2) ct rank/rt shape, and 3) rt rank; rt rank implies rt shape. Some rt size arrays can be resized but rt rank arrays cannot normally have their rank changed. Instead, you create a new container or view with the rank you want.
  357. </p>
  358. <p>For example:
  359. </p>
  360. <div class="example">
  361. <pre class="verbatim">{
  362. ra::Small&lt;double, 2, 3&gt; a(0.); // a ct size 2x3 array
  363. ra::Big&lt;double, 2&gt; b({2, 3}, 0.); // a rt size 2x3 array
  364. ra::Big&lt;double&gt; c({2, 3}, 0.); // a rt rank 2x3 array
  365. // a, b, c destroyed at end of scope
  366. }
  367. </pre></div>
  368. <p>Using the right kind of container can result in better performance. Ct shapes do not need to be stored in memory, which matters when you have many small arrays. Ct shape or ct rank arrays are also safer to use; sometimes <code>ra::</code> will be able to detect errors in the shapes or ranks of array operands at compile time, if the appropriate types are used.
  369. </p>
  370. <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 id="DOCF4" href="#FOOT4"><sup>4</sup></a>
  371. </p>
  372. <div class="example">
  373. <pre class="verbatim">using ra::Small, ra::Big;
  374. Small&lt;int, 2, 2&gt; a = {{1, 2}, {3, 4}}; // explicit contents
  375. Big&lt;int, 2&gt; a1 = {{1, 2}, {3, 4}}; // explicit contents
  376. Small&lt;int, 2, 2&gt; a2 = {{1, 2}}; // error: bad shape
  377. Small&lt;int, 2, 2&gt; b = 7; // 7 is copied into b
  378. Small&lt;int, 2, 2&gt; c = a; // the contents of a are copied into c
  379. Big&lt;int&gt; d = a; // d takes the shape of a and a is copied into d
  380. Big&lt;int&gt; e = 0; // e is a 0-array with one element f()==0.
  381. </pre></div>
  382. <p>The second form takes two arguments, one giving the shape, the second the contents.
  383. </p>
  384. <span id="index-none"></span>
  385. <span id="index-uninitialized-container"></span>
  386. <div class="example">
  387. <pre class="verbatim">ra::Big&lt;double, 2&gt; a({2, 3}, 1.); // a has shape [2 3], filled with 1.
  388. ra::Big&lt;double&gt; b({2, 3}, ra::none); // b has shape [2 3], default initialized
  389. ra::Big&lt;double&gt; c({2, 3}, a); // c has shape [2 3], a is copied into c
  390. </pre></div>
  391. <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.
  392. </p>
  393. <p>The shape argument can have rank 0 only for rank 1 arrays.
  394. </p>
  395. <span id="index-none-1"></span>
  396. <span id="index-uninitialized-container-1"></span>
  397. <div class="example">
  398. <pre class="verbatim">ra::Big&lt;int&gt; c(3, 0); // ok {0, 0, 0}, same as ra::Big&lt;int&gt; c({3}, 0)
  399. ra::Big&lt;int, 1&gt; c(3, 0); // ok {0, 0, 0}, same as ra::Big&lt;int, 1&gt; c({3}, 0)
  400. ra::Big&lt;int, 2&gt; c({3}, 0); // error: bad length for shape
  401. ra::Big&lt;int, 2&gt; c(3, 0); // error: bad length for shape
  402. </pre></div>
  403. <p>When the content argument is a pointer or a 1D brace list, it&rsquo;s handled especially, not for shape<a id="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 id="DOCF6" href="#FOOT6"><sup>6</sup></a>
  404. </p>
  405. <span id="index-order_002c-column_002dmajor"></span>
  406. <div class="example">
  407. <pre class="verbatim">Small&lt;int, 2, 2&gt; aa = {1, 2, 3, 4}; // ravel of the content
  408. ra::Big&lt;double, 2&gt; a({2, 3}, {1, 2, 3, 4, 5, 6}); // same as a = {{1, 2, 3}, {4, 5, 6}}
  409. </pre></div>
  410. <div class="example">
  411. <pre class="verbatim">double bx[6] = {1, 2, 3, 4, 5, 6}
  412. ra::Big&lt;double, 2&gt; b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}
  413. double cx[4] = {1, 2, 3, 4}
  414. ra::Big&lt;double, 2&gt; c({3, 2}, cx); // *** WHO NOSE ***
  415. </pre></div>
  416. <div class="example">
  417. <pre class="verbatim">using lens = mp::int_list&lt;2, 3&gt;;
  418. using steps = mp::int_list&lt;1, 2&gt;;
  419. ra::SmallArray&lt;double, lens, steps&gt; a {{1, 2, 3}, {4, 5, 6}}; // stored column-major: 1 4 2 5 3 6
  420. </pre></div>
  421. <p>These produce compile time errors:
  422. </p>
  423. <div class="example">
  424. <pre class="verbatim">Big&lt;int, 2&gt; b = {1, 2, 3, 4}; // error: shape cannot be deduced from ravel
  425. Small&lt;int, 2, 2&gt; b = {1, 2, 3, 4 5}; // error: bad size
  426. Small&lt;int, 2, 2&gt; b = {1, 2, 3}; // error: bad size
  427. </pre></div>
  428. <span id="x_002dscalar_002dchar_002dstar"></span><p>Sometimes the pointer constructor gets in the way (see <a href="#x_002dscalar"><code>scalar</code></a>): </p>
  429. <div class="example">
  430. <pre class="verbatim">ra::Big&lt;char const *, 1&gt; A({3}, &quot;hello&quot;); // error: try to convert char to char const *
  431. ra::Big&lt;char const *, 1&gt; A({3}, ra::scalar(&quot;hello&quot;)); // ok, &quot;hello&quot; is a single item
  432. cout &lt;&lt; ra::noshape &lt;&lt; format_array(A, &quot;|&quot;) &lt;&lt; endl;
  433. </pre><pre class="example">-| hello|hello|hello
  434. </pre></div>
  435. <span id="index-view"></span>
  436. <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:
  437. </p>
  438. <div class="example">
  439. <pre class="verbatim">ra::Big&lt;double&gt; c({2, 3}, 0.); // a rt rank 2x3 array
  440. {
  441. auto c1 = c(1); // the second row of array c
  442. // c1 is destroyed here
  443. }
  444. cout &lt;&lt; c(1, 1) &lt;&lt; endl; // ok
  445. </pre></div>
  446. <p>The data accessed through a view is the data of the ‘root’ container, so modifying the former will be reflected in the latter.
  447. </p>
  448. <div class="example">
  449. <pre class="verbatim">ra::Big&lt;double&gt; c({2, 3}, 0.);
  450. auto c1 = c(1);
  451. c1(2) = 9.; // c(1, 2) = 9.
  452. </pre></div>
  453. <p>Just as for containers, there are separate types of views depending on whether the shape is known at compile time, the rank is known at compile time but the shape is not, or neither the shape nor the rank are known at compile time. <code>ra::</code> has functions to create the most common kinds of views:
  454. </p>
  455. <div class="example">
  456. <pre class="verbatim">ra::Big&lt;double&gt; c {{1, 2, 3}, {4, 5, 6}};
  457. auto ct = transpose&lt;1, 0&gt;(c); // {{1, 4}, {2, 5}, {3, 6}}
  458. auto cr = reverse(c, 0); // {{4, 5, 6}, {1, 2, 3}}
  459. </pre></div>
  460. <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:
  461. </p>
  462. <div class="example">
  463. <pre class="verbatim">int raw[6] = {1, 2, 3, 4, 5, 6};
  464. ra::View&lt;int&gt; v1({{2, 3}, {3, 1}}, raw); // view with shape [2, 3] steps [3, 1]
  465. ra::View&lt;int&gt; v2({2, 3}, raw); // same, default C (row-major) steps
  466. </pre></div>
  467. <p>Containers can be treated as views of the same kind (rt or ct) . If you declare a function
  468. </p>
  469. <div class="example">
  470. <pre class="verbatim">void f(ra::View&lt;int, 3&gt; &amp; v);
  471. </pre></div>
  472. <p>you may pass it an object of type <code>ra::Big&lt;int, 3&gt;</code>.
  473. </p>
  474. <hr>
  475. </div>
  476. <div class="section" id="Array-operations">
  477. <div class="header">
  478. <p>
  479. 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>
  480. </div>
  481. <span id="Array-operations-1"></span><h3 class="section">2.3 Array operations</h3>
  482. <p>To apply an operation to each element of an array, use the function <a href="#x_002dfor_005feach"><code>for_each</code></a>. The array is traversed in an order that is decided by the library.
  483. </p>
  484. <div class="example">
  485. <pre class="verbatim">ra::Small&lt;double, 2, 3&gt; a = {{1, 2, 3}, {4, 5, 6}};
  486. double s = 0.;
  487. for_each([&amp;s](auto &amp;&amp; a) { s+=a; }, a);
  488. </pre><pre class="example">&rArr; s = 21.
  489. </pre></div>
  490. <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.
  491. </p>
  492. <div class="example">
  493. <pre class="verbatim">using T = ra::Small&lt;double, 2, 2&gt;;
  494. T a = {{1, 2}, {3, 4}};
  495. T b = {{10, 20}, {30, 40}};
  496. T c = map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; }, a, b); // (1)
  497. </pre><pre class="example">&rArr; c = {{11, 22}, {33, 44}}
  498. </pre></div>
  499. <p>Expressions may take any number of arguments and be nested arbitrarily.
  500. </p>
  501. <div class="example">
  502. <pre class="verbatim">T d = 0;
  503. for_each([](auto &amp;&amp; a, auto &amp;&amp; b, auto &amp;&amp; d) { d = a+b; },
  504. a, b, d); // same as (1)
  505. for_each([](auto &amp;&amp; ab, auto &amp;&amp; d) { d = ab; },
  506. map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; },
  507. a, b),
  508. d); // same as (1)
  509. </pre></div>
  510. <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.
  511. </p>
  512. <div class="example">
  513. <pre class="verbatim">T d = 0;
  514. map([](auto &amp; d) -&gt; decltype(auto) { return d; }, d) // just pass d along
  515. = map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a+b; }, a, b); // same as (1)
  516. </pre></div>
  517. <p><code>ra::</code> defines many shortcuts for common array operations. You can of course just do:
  518. </p>
  519. <div class="example">
  520. <pre class="verbatim">T c = a+b; // same as (1)
  521. </pre></div>
  522. <hr>
  523. </div>
  524. <div class="section" id="Rank-extension">
  525. <div class="header">
  526. <p>
  527. 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>
  528. </div>
  529. <span id="Rank-extension-1"></span><h3 class="section">2.4 Rank extension</h3>
  530. <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.
  531. </p>
  532. <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
  533. </p>
  534. <blockquote>
  535. <p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ᵣ₋₁₎)</code>
  536. </p></blockquote>
  537. <p>where <code>r</code> is the rank of <code>R</code>.
  538. </p>
  539. <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
  540. </p>
  541. <blockquote>
  542. <p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S()</code>.
  543. </p></blockquote>
  544. <p>The two rules above are supported by all primitive array languages, e.g. Matlab [<a href="#Sources">Mat</a>]
  545. . 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
  546. </p>
  547. <blockquote>
  548. <p><code>(R+S)(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ₛ₋₁₎)</code>.
  549. </p></blockquote>
  550. <p>That is, after we run out of indices in <code>S</code>, we simply repeat the elements. We have aligned the shapes so:
  551. </p>
  552. <blockquote>
  553. <pre class="verbatim">[n₀ n₁ ... n₍ₛ₋₁₎ ... n₍ᵣ₋₁₎]
  554. [n₀ n₁ ... n₍ₛ₋₁₎]
  555. </pre></blockquote>
  556. <span id="index-shape-agreement_002c-prefix"></span>
  557. <span id="index-shape-agreement_002c-suffix"></span>
  558. <span id="index-Numpy"></span>
  559. <p>This rank extension rule is used by the J language [<a href="#Sources">J S</a>]
  560. 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>]
  561. <a id="DOCF7" href="#FOOT7"><sup>7</sup></a>.
  562. </p>
  563. <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,
  564. </p>
  565. <div class="example">
  566. <pre class="verbatim">ra::Small&lt;int, 3&gt; x {3, 5, 9};
  567. ra::Small&lt;int, 3, 2&gt; a = x; // assign x(i) to each a(i, j)
  568. </pre><pre class="example">&rArr; a = {{3, 3}, {5, 5}, {9, 9}}
  569. </pre></div>
  570. <div class="example">
  571. <pre class="verbatim">ra::Small&lt;int, 3&gt; x(0.);
  572. ra::Small&lt;int, 3, 2&gt; a = {{1, 2}, {3, 4}, {5, 6}};
  573. x += a; // sum the rows of a
  574. </pre><pre class="example">&rArr; x = {3, 7, 11}
  575. </pre></div>
  576. <div class="example">
  577. <pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 3, 3}, ra::_0);
  578. ra::Big&lt;double, 1&gt; b({5}, 0.);
  579. b += transpose&lt;0, 1, 1&gt;(a); // b(i) = ∑ⱼ a(i, j, j)
  580. </pre><pre class="example">&rArr; b = {0, 3, 6, 9, 12}
  581. </pre></div>
  582. <span id="index-Numpy-1"></span>
  583. <span id="index-broadcasting_002c-singleton_002c-newaxis"></span>
  584. <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>]
  585. , 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 length 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.
  586. </p>
  587. <p>A drawback of singleton broadcasting is that it muddles the distinction between a scalar and a vector of length 1. Sometimes, an axis of length 1 is no more than that, and if 2≠3 is a size mismatch, it isn&rsquo;t obvious why 1≠2 shouldn&rsquo;t be. To avoid this problem, <code>ra::</code> supports broadcasting with undefined length axes (see <a href="#x_002dinsert"><code>insert</code></a>).
  588. </p>
  589. <div class="example">
  590. <pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 3}, ra::_0);
  591. ra::Big&lt;double, 1&gt; b({3}, 0.);
  592. ra::Big&lt;double, 3&gt; c({1, 3}, ra::_0);
  593. // b(?, i) += a(j, i) → b(i) = ∑ⱼ a(j, i) (sum columns)
  594. b(ra::insert&lt;1&gt;) += a;
  595. c = a; // 1 ≠ 5, still an agreement error
  596. </pre></div>
  597. <p>Still another way to align array axes is provided by the <a href="#The-rank-conjunction">rank conjunction</a>.
  598. </p>
  599. <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.
  600. <a href="#x_002dtranspose">Transposing</a> the axes before extension is a possible workaround.
  601. </p>
  602. <hr>
  603. </div>
  604. <div class="section" id="Cell-iteration">
  605. <div class="header">
  606. <p>
  607. 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>
  608. </div>
  609. <span id="Cell-iteration-1"></span><h3 class="section">2.5 Cell iteration</h3>
  610. <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:
  611. </p>
  612. <div class="example">
  613. <pre class="verbatim">ra::Big&lt;double, 3&gt; a({5, 4, 3}, ra::_0);
  614. for_each([](auto &amp;&amp; b) { /* b has shape (5 4 3) */ }, iter&lt;3&gt;(a));
  615. for_each([](auto &amp;&amp; b) { /* b has shape (4 3) */ }, iter&lt;2&gt;(a));
  616. for_each([](auto &amp;&amp; b) { /* b has shape (3) */ }, iter&lt;1&gt;(a));
  617. for_each([](auto &amp;&amp; b) { /* b has shape () */ }, iter&lt;0&gt;(a)); // elements
  618. for_each([](auto &amp;&amp; b) { /* b has shape () */ }, a); // same as iter&lt;0&gt;(a); default
  619. </pre></div>
  620. <p>One may specify the <em>frame</em> rank instead:
  621. </p>
  622. <div class="example">
  623. <pre class="verbatim">for_each([](auto &amp;&amp; b) { /* b has shape () */ }, iter&lt;-3&gt;(a)); // same as iter&lt;0&gt;(a)
  624. for_each([](auto &amp;&amp; b) { /* b has shape (3) */ }, iter&lt;-2&gt;(a)); // same as iter&lt;1&gt;(a)
  625. for_each([](auto &amp;&amp; b) { /* b has shape (4 3) */ }, iter&lt;-1&gt;(a)); // same as iter&lt;2&gt;(a)
  626. </pre></div>
  627. <p>In this way it is possible to match shapes in various ways. Compare
  628. </p>
  629. <div class="example">
  630. <pre class="verbatim">ra::Big&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  631. ra::Big&lt;double, 1&gt; b = {10, 20};
  632. ra::Big&lt;double, 2&gt; c = a * b; // multiply (each item of a) by (each item of b)
  633. </pre><pre class="example">&rArr; a = {{10, 20, 30}, {80, 100, 120}}
  634. </pre></div>
  635. <p>with
  636. </p>
  637. <div class="example">
  638. <pre class="verbatim">ra::Big&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  639. ra::Big&lt;double, 1&gt; b = {10, 20, 30};
  640. ra::Big&lt;double, 2&gt; c({2, 3}, 0.);
  641. iter&lt;1&gt;(c) = iter&lt;1&gt;(a) * iter&lt;1&gt;(b); // multiply (each item of a) by (b)
  642. </pre><pre class="example">&rArr; a = {{10, 40, 90}, {40, 100, 180}}
  643. </pre></div>
  644. <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.
  645. </p>
  646. <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>).
  647. </p>
  648. <p>FIXME More examples.
  649. </p>
  650. <hr>
  651. </div>
  652. <div class="section" id="Slicing">
  653. <div class="header">
  654. <p>
  655. Next: <a href="#Functions" accesskey="n" rel="next">Functions</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>
  656. </div>
  657. <span id="Slicing-1"></span><h3 class="section">2.6 Slicing</h3>
  658. <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.
  659. </p>
  660. <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<a id="DOCF8" href="#FOOT8"><sup>8</sup></a>. An array extension is immediately available through <code>map</code>. For example:
  661. </p>
  662. <div class="example">
  663. <pre class="verbatim">ra::Big&lt;double, 1&gt; a = {1., 2., 3., 4.};
  664. ra::Big&lt;int, 1&gt; i = {1, 3};
  665. map(a, i) = 77.;
  666. </pre><pre class="example">&rArr; a = {1., 77., 3, 77.}
  667. </pre></div>
  668. <p>Just as with any use of <code>map</code>, array arguments are subject to the prefix agreement rule.
  669. </p>
  670. <div class="example">
  671. <pre class="verbatim">ra::Big&lt;double, 2&gt; a({2, 2}, {1., 2., 3., 4.});
  672. ra::Big&lt;int, 1&gt; i = {1, 0};
  673. ra::Big&lt;double, 1&gt; b = map(a, i, 0);
  674. </pre><pre class="example">&rArr; b = {3., 1.} // {a(1, 0), a(0, 0)}
  675. </pre></div>
  676. <div class="example">
  677. <pre class="verbatim">ra::Big&lt;int, 1&gt; j = {0, 1};
  678. b = map(a, i, j);
  679. </pre><pre class="example">&rArr; b = {3., 2.} // {a(1, 0), a(0, 1)}
  680. </pre></div>
  681. <p>The latter is a form of sparse subscripting.
  682. </p>
  683. <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:
  684. </p>
  685. <div class="example">
  686. <pre class="verbatim">ra::Big&lt;double, 2&gt; A {{1., 2.}, {3., 4.}};
  687. ra::Big&lt;int, 1&gt; i = {1, 0};
  688. ra::Big&lt;int, 1&gt; j = {0, 1};
  689. // {{A(i₀, j₀), A(i₀, j₁)}, {A(i₁, j₀), A(i₁, j₁)}}
  690. ra::Big&lt;double, 2&gt; b = A(i, j);
  691. </pre><pre class="example">&rArr; b = {{3., 4.}, {1., 2.}}
  692. </pre></div>
  693. <span id="x_002dsubscript_002douter_002dproduct"></span><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>.
  694. </p>
  695. <span id="index-elision_002c-index"></span>
  696. <p>You may give fewer subscripts than the rank of the array. The full extent is assumed for the missing subscripts (cf <a href="#x_002dall"><code>all</code></a> below):
  697. </p>
  698. <div class="example">
  699. <pre class="verbatim">ra::Big&lt;int, 3&gt; a({2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8});
  700. auto a0 = a(0); // same as a(0, ra::all, ra::all)
  701. auto a10 = a(1, 0); // same as a(1, 0, ra::all)
  702. </pre><pre class="example">&rArr; a0 = {{1, 2}, {3, 4}}
  703. &rArr; a10 = {5, 6}
  704. </pre></div>
  705. <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>).
  706. </p>
  707. <p>Besides, when the subscripts <code>i, j, ...</code> are scalars or integer sequences of the form <code>(o, o+s, ..., o+s*(n-1))</code> (<em>linear ranges</em>), 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>.
  708. </p>
  709. <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:
  710. </p>
  711. <span id="x_002diota"></span><dl class="def">
  712. <dt id="index-iota"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>iota</strong> <em>count [start:0 [step:1]]</em><a href='#index-iota' class='copiable-anchor'> &para;</a></span></dt>
  713. <dd><p>Create a linear range <code>start, start+step, ... start+step*(count-1)</code>.
  714. </p>
  715. <p>This can used anywhere an array expression is expected.
  716. </p>
  717. <div class="example">
  718. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = ra::iota(4, 3 -2);
  719. </pre><pre class="example">&rArr; a = {3, 1, -1, -3}
  720. </pre></div>
  721. <p>Here, <code>b</code> and <code>c</code> are <code>View</code>s (see <a href="#Containers-and-views">Containers and views</a>).
  722. </p><div class="example">
  723. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3, 4, 5, 6};
  724. auto b = a(iota(3));
  725. auto c = a(iota(3, 3));
  726. </pre><pre class="example">&rArr; a = {1, 2, 3}
  727. &rArr; a = {4, 5, 6}
  728. </pre></div>
  729. <span id="index-TensorIndex"></span>
  730. <p><code>iota()</code> by itself is an expression of rank 1 and undefined length. It must be used with other terms whose lengths are defined, so that the overall shape of the array expression can be determined. In general, <code>iota&lt;n&gt;()</code> is an array expression of rank <code>n</code>+1 that represents the <code>n</code>-th index of an array expression. This is similar to Blitz++&rsquo;s <code>TensorIndex</code>.
  731. </p>
  732. <p><code>ra::</code> offers the shortcut <code>ra::_0</code> for <code>ra::iota&lt;0&gt;()</code>, etc.
  733. </p>
  734. <div class="example">
  735. <pre class="verbatim">ra::Big&lt;int, 1&gt; v = {1, 2, 3};
  736. cout &lt;&lt; (v - ra::_0) &lt;&lt; endl; // { 1-0, 2-1, 3-2 }
  737. // cout &lt;&lt; (ra::_0) &lt;&lt; endl; // error: undefined length
  738. // cout &lt;&lt; (v - ra::_1) &lt;&lt; endl; // error: undefined length on axis 1
  739. ra::Big&lt;int, 2&gt; a({3, 2}, 0);
  740. cout &lt;&lt; (a + ra::_0 - ra::_1) &lt;&lt; endl; // {{0, -1, -2}, {1, 0, -1}, {2, 1, 0}}
  741. </pre></div>
  742. <p>When undefined length <code>iota()</code> is used as a subscript by itself, the result isn&rsquo;t a <code>View</code>. This allows <code>view(iota())</code> to match with expressions of different lengths, as in the following example.
  743. </p><div class="example">
  744. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3, 4, 5, 6};
  745. ra::Big&lt;int, 1&gt; b = {1, 2, 3};
  746. cout &lt;&lt; (b + a(iota())) &lt;&lt; endl; // a(iota()) is not a View
  747. </pre><pre class="example">-| 3
  748. 2 4 6
  749. </pre></div>
  750. <p>Note the difference between
  751. </p><ul>
  752. <li> <code>ra::iota&lt;3&gt;()</code> —
  753. an expression of rank 4 and undefined length, representing a linear sequence over the tensor index of axis 3
  754. </li><li> <code>ra::iota(3)</code> ≡ <code>ra::iota&lt;0&gt;(3)</code> —
  755. an expression of rank 1, representing the sequence <code>0, 1, 2</code>.
  756. </li></ul>
  757. </dd></dl>
  758. <span id="x_002dall"></span><dl class="def">
  759. <dt id="index-all"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>all</strong><a href='#index-all' class='copiable-anchor'> &para;</a></span></dt>
  760. <dd><p>Create a linear range <code>0, 1, ... (nᵢ-1)</code> when used as a subscript at the <var>i</var>-th place of a subscripting expression. This might not be the <var>i</var>-th argument; see <a href="#x_002dinsert"><code>insert</code></a>, <a href="#x_002ddots"><code>dots</code></a>.
  761. </p>
  762. <p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
  763. </p>
  764. <div class="example">
  765. <pre class="verbatim">ra::Big&lt;int, 2&gt; a({3, 2}, {1, 2, 3, 4, 5, 6});
  766. auto b = a(ra::all, ra::all); // (1) a view of the whole of a
  767. auto c = a(iota(3), iota(2)); // same as (1)
  768. auto d = a(iota(3), ra::all); // same as (1)
  769. auto e = a(ra:all, iota(2)); // same as (1)
  770. auto f = a(0, ra::all); // first row of a
  771. auto g = a(ra::all, 1); // second column of a
  772. auto g = a(ra::all, ra::dots&lt;0&gt;, 1); // same
  773. </pre></div>
  774. <p><code>all</code> is a special case (<code>dots&lt;1&gt;</code>) of the more general object <code>dots</code>.
  775. </p>
  776. </dd></dl>
  777. <span id="x_002ddots"></span><dl class="def">
  778. <dt id="index-dots_003cn_003e"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>dots&lt;n&gt;</strong><a href='#index-dots_003cn_003e' class='copiable-anchor'> &para;</a></span></dt>
  779. <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.
  780. </p>
  781. <p>If <var>n</var> is defaulted (<code>dots&lt;&gt;</code>), all available places will be used; this can only be done once in a given subscript list.
  782. </p>
  783. <p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
  784. </p>
  785. <div class="example">
  786. <pre class="verbatim">ra::Big&lt;int, 3&gt; a({3, 2, 4}, ...);
  787. auto h = a(); // all of a
  788. auto b = a(ra::all, ra::all, ra::all); // (1) all of a
  789. auto c = a(ra::dots&lt;3&gt;); // same as (1)
  790. auto d = a(ra::all, ra::dots&lt;2&gt;); // same as (1)
  791. auto e = a(ra::dots&lt;2&gt;, ra::all); // same as (1)
  792. auto f = a(ra::dots&lt;&gt;); // same as (1)
  793. auto j0 = a(0, ra::dots&lt;2&gt;); // first page of a
  794. auto j1 = a(0); // same
  795. auto j2 = a(0, ra::dots&lt;&gt;); // same
  796. auto k0 = a(ra::all, 0); // first row of a
  797. auto k1 = a(ra::all, 0, ra::all); // same
  798. auto k2 = a(ra::all, 0, ra::dots&lt;&gt;); // same
  799. auto k3 = a(ra::dots&lt;&gt;, 0, ra::all); // same
  800. // auto k = a(ra::dots&lt;&gt;, 0, ra::dots&lt;&gt;); // error
  801. auto l0 = a(ra::all, ra::all, 0); // first column of a
  802. auto l1 = a(ra::dots&lt;2&gt;, 0); // same
  803. auto l2 = a(ra::dots&lt;&gt;, 0); // same
  804. </pre></div>
  805. <p>This is useful when writing rank-generic code, see <code>examples/maxwell.cc</code> in the distribution for an example.
  806. </p>
  807. </dd></dl>
  808. <p>The following special objects aren&rsquo;t related to linear ranges, but they are meant to be used in a subscript context. Using them in other contexts will result in a compile time error.
  809. </p>
  810. <span id="index-len"></span>
  811. <span id="index-end_002c-Octave_002fMatlab"></span>
  812. <span id="x_002dlen"></span><dl class="def">
  813. <dt id="index-len-1"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>len</strong><a href='#index-len-1' class='copiable-anchor'> &para;</a></span></dt>
  814. <dd><p>Represents the length of the <var>i</var>-th axis of a subscripted expression, when used at the <var>i</var>-th place of a subscripting expression.
  815. </p>
  816. <p>This works like <code>end</code> in Octave/Matlab, but note that <code>ra::</code> indices begin at 0, so the last element of a vector <code>a</code> is <code>a(ra::len-1)</code>.
  817. </p>
  818. <div class="example">
  819. <pre class="verbatim">ra::Big&lt;int, 2&gt; a({10, 10}, 100 + ra::_0 - ra::_1);
  820. auto a0 = a(ra::len-1); // last row of a; ra::len is a.len(0)
  821. auto a1 = a(ra::all, ra::len-1); // last column a; ra::len is a.len(1)
  822. auto a2 = a(ra::len-1, ra::len-1); // last element of last row; the first ra::len is a.len(0) and the second one is a.len(1)
  823. auto a3 = a(ra::all, ra::iota(2, ra::len-2)); // last two columns of a
  824. auto a4 = a(ra::iota(ra::len/2, 1, 2)); // odd rows of a
  825. a(ra::len - std::array {1, 3, 4}) = 0; // set to 0 the 1st, 3rd and 4th rows of a, counting from the end
  826. </pre></div>
  827. <div class="example">
  828. <pre class="verbatim">ra::Big&lt;int, 3&gt; b({2, 3, 4}, ...);
  829. auto b0 = b(ra::dots&lt;2&gt;, ra::len-1); // ra::len is a.len(2)
  830. auto b1 = b(ra::insert&lt;1&gt;, ra::len-1); // ra::len is a.len(0)
  831. </pre></div>
  832. </dd></dl>
  833. <span id="index-insert"></span>
  834. <span id="x_002dinsert"></span><dl class="def">
  835. <dt id="index-insert_003cn_003e"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>insert&lt;n&gt;</strong><a href='#index-insert_003cn_003e' class='copiable-anchor'> &para;</a></span></dt>
  836. <dd><p>Inserts <code>n</code> new axes at the subscript position. <code>n</code> must not be negative.
  837. </p>
  838. <p>The new axes have step 0 and undefined length, so they will match any length 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:
  839. </p>
  840. <div class="example">
  841. <pre class="verbatim">auto h = a(insert&lt;0&gt;); // same as (1)
  842. auto k = a(insert&lt;1&gt;); // shape [undefined, 3, 2]
  843. </pre></div>
  844. <span id="index-broadcasting_002c-singleton_002c-Numpy"></span>
  845. <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 length one match dimensions of any length. In <code>ra::</code> singleton dimensions aren&rsquo;t special, so broadcasting requires the use of <code>insert</code>. For example: </p>
  846. <div class="example">
  847. <pre class="verbatim">ra::Big&lt;int, 1&gt; x = {1, 10};
  848. // match shapes [2, U, U] with [U, 3, 2] to produce [2, 3, 2]
  849. cout &lt;&lt; x(ra::all, ra::insert&lt;2&gt;) * a(insert&lt;1&gt;) &lt;&lt; endl;
  850. </pre><pre class="example">-| 2 3 2
  851. 1 2
  852. 3 4
  853. 5 6
  854. 10 20
  855. 30 40
  856. 50 60
  857. </pre></div>
  858. </dd></dl>
  859. <p>We were speaking earlier of the outer product of the subscripts with operator <code>A</code>. Here&rsquo;s a way to perform the actual outer product (with operator <code>*</code>) of two <code>Views</code>, through broadcasting. All three lines are equivalent. See <a href="#x_002dfrom"><code>from</code></a> for a more general way to compute outer products.
  860. </p><div class="example">
  861. <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;
  862. cout &lt;&lt; (A(ra::dots&lt;&gt;, ra::insert&lt;B.rank()&gt;) * B(ra::insert&lt;A.rank()&gt;, ra::dots&lt;&gt;)) &lt;&lt; endl; // default dots&lt;&gt;
  863. cout &lt;&lt; (A * B(ra::insert&lt;A.rank()&gt;)) &lt;&lt; endl; // index elision + prefix matching
  864. </pre></div>
  865. <ul class="section-toc">
  866. <li><a href="#Subscripting-and-rank_002d0-views" accesskey="1">Subscripting and rank-0 views</a></li>
  867. </ul>
  868. <div class="subsection" id="Subscripting-and-rank_002d0-views">
  869. <h4 class="subsection">2.6.1 Subscripting and rank-0 views</h4>
  870. <span id="index-view_002c-rank-0"></span>
  871. <span id="index-rank_002c-runtime"></span>
  872. <span id="index-rank_002c-compile_002dtime"></span>
  873. <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 rt 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.
  874. </p>
  875. <div class="example">
  876. <pre class="verbatim">using T = std::complex&lt;double&gt;;
  877. int f(T &amp;);
  878. Big&lt;T, 2&gt; a({2, 3}, 0); // ct rank
  879. Big&lt;T&gt; b({2, 3}, 0); // rt rank
  880. cout &lt;&lt; a(0, 0).real_part() &lt;&lt; endl; // ok, a(0, 0) returns complex &amp;
  881. // cout &lt;&lt; b(0, 0).real_part() &lt;&lt; endl; // error, View&lt;T&gt; has no member real_part
  882. cout &lt;&lt; ((T &amp;)(b(0, 0))).real_part() &lt;&lt; endl; // ok, manual conversion to T &amp;
  883. cout &lt;&lt; f(b(0, 0)) &lt;&lt; endl; // ok, automatic conversion from View&lt;T&gt; to T &amp;
  884. // cout &lt;&lt; f(a(0)) &lt;&lt; endl; // compile time error, conversion failed since ct rank of a(0) is not 0
  885. // cout &lt;&lt; f(b(0)) &lt;&lt; endl; // runtime error, conversion failed since rt rank of b(0) is not 0
  886. </pre></div>
  887. <hr>
  888. </div>
  889. </div>
  890. <div class="section" id="Functions">
  891. <div class="header">
  892. <p>
  893. 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>
  894. </div>
  895. <span id="Functions-1"></span><h3 class="section">2.7 Functions</h3>
  896. <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.
  897. </p>
  898. <span id="x_002dscalar_002dops"></span><ul class="section-toc">
  899. <li><a href="#Standard-scalar-operations" accesskey="1">Standard scalar operations</a></li>
  900. <li><a href="#Conditional-operations" accesskey="2">Conditional operations</a></li>
  901. <li><a href="#Special-operations" accesskey="3">Special operations</a></li>
  902. <li><a href="#Elementwise-reductions" accesskey="4">Elementwise reductions</a></li>
  903. <li><a href="#Special-reductions" accesskey="5">Special reductions</a></li>
  904. <li><a href="#Shortcut-reductions" accesskey="6">Shortcut reductions</a></li>
  905. </ul>
  906. <div class="subsection" id="Standard-scalar-operations">
  907. <h4 class="subsection">2.7.1 Standard scalar operations</h4>
  908. <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 id="DOCF9" href="#FOOT9"><sup>9</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>asin</code>, <code>acos</code>, <code>atan</code>, <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, and <code>lerp</code>.
  909. 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 <code>odd</code>, <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>.
  910. </p>
  911. <p>For example:
  912. </p><div class="example">
  913. <pre class="verbatim">cout &lt;&lt; exp(ra::Small&lt;double, 3&gt; {4, 5, 6}) &lt;&lt; endl;
  914. </pre><pre class="example"> -| 54.5982 148.413 403.429
  915. </pre></div>
  916. </div>
  917. <div class="subsection" id="Conditional-operations">
  918. <h4 class="subsection">2.7.2 Conditional operations</h4>
  919. <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.
  920. </p>
  921. <div class="example">
  922. <pre class="verbatim">ra::Big&lt;double&gt; x ...
  923. ra::Big&lt;double&gt; y = where(x&gt;0, expensive_expr_1(x), expensive_expr_2(x));
  924. </pre></div>
  925. <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
  926. </p>
  927. <div class="example">
  928. <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; }
  929. x&gt;0, expensive_expr_1(x), expensive_function_2(x));
  930. </pre></div>
  931. <p>If the expressions have side effects, then <code>map</code> won&rsquo;t even give the right result.
  932. </p>
  933. <div class="example">
  934. <pre class="verbatim">ra::Big&lt;int, 1&gt; o = {};
  935. ra::Big&lt;int, 1&gt; e = {};
  936. ra::Big&lt;int, 1&gt; n = {1, 2, 7, 9, 12};
  937. 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)));
  938. 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;
  939. </pre><pre class="example">-| o: 1 7 9, e: 2 12
  940. </pre></div>
  941. <p>FIXME Artificial example.
  942. FIXME Do we want to expose ply(); this is the only example in the manual that uses it.
  943. </p>
  944. <p>When the choice is between more than two expressions, there&rsquo;s <a href="#x_002dpick"><code>pick</code></a>, which operates similarly, but accepts an integer instead of a boolean selector.
  945. </p>
  946. </div>
  947. <div class="subsection" id="Special-operations">
  948. <h4 class="subsection">2.7.3 Special operations</h4>
  949. <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.
  950. </p>
  951. <p>FIXME
  952. </p>
  953. </div>
  954. <div class="subsection" id="Elementwise-reductions">
  955. <h4 class="subsection">2.7.4 Elementwise reductions</h4>
  956. <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.
  957. </p>
  958. <p>You can define reductions the same way <code>ra::</code> does:
  959. </p>
  960. <div class="example">
  961. <pre class="verbatim">template &lt;class A&gt;
  962. inline auto op_reduce(A &amp;&amp; a)
  963. {
  964. T c = op_default;
  965. for_each([&amp;c](auto &amp;&amp; a) { c = op(c, a); }, a);
  966. return c;
  967. }
  968. </pre></div>
  969. <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:
  970. </p>
  971. <div class="example">
  972. <pre class="verbatim"> ra::Big&lt;double, 2&gt; a({m, n}, ...);
  973. ra::Big&lt;double, 1&gt; sum_rows({n}, 0.);
  974. iter&lt;1&gt;(sum_rows) += iter&lt;1&gt;(a);
  975. // for_each(ra::wrank&lt;1, 1&gt;([](auto &amp; c, auto &amp;&amp; a) { c += a; }), sum_rows, a) // alternative
  976. // sum_rows += transpose&lt;1, 0&gt;(a); // another
  977. ra::Big&lt;double, 1&gt; sum_cols({m}, 0.);
  978. sum_cols += a;
  979. </pre></div>
  980. <p>FIXME example with assignment op
  981. </p>
  982. <p>A few common operations of this type are already packaged in <code>ra::</code>.
  983. </p>
  984. </div>
  985. <div class="subsection" id="Special-reductions">
  986. <h4 class="subsection">2.7.5 Special reductions</h4>
  987. <p><code>ra::</code> defines the following special reductions.
  988. </p>
  989. <p>FIXME
  990. </p>
  991. </div>
  992. <div class="subsection" id="Shortcut-reductions">
  993. <h4 class="subsection">2.7.6 Shortcut reductions</h4>
  994. <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>.
  995. </p>
  996. <p>FIXME
  997. </p>
  998. <p>These operations are defined on top of another function <code>early</code>.
  999. </p>
  1000. <p>FIXME early
  1001. </p>
  1002. <p>The following is often useful.
  1003. </p>
  1004. <p>FIXME lexicographical compare etc.
  1005. </p>
  1006. <hr>
  1007. </div>
  1008. </div>
  1009. <div class="section" id="The-rank-conjunction">
  1010. <div class="header">
  1011. <p>
  1012. Next: <a href="#Compatibility" accesskey="n" rel="next">Compatibility</a>, 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>
  1013. </div>
  1014. <span id="The-rank-conjunction-1"></span><h3 class="section">2.8 The rank conjunction</h3>
  1015. <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.
  1016. </p>
  1017. <div class="example">
  1018. <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
  1019. for_each(my_sort, iter&lt;0&gt;(A)); // (in ra::) error, bad cell rank
  1020. </pre></div>
  1021. <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.
  1022. </p>
  1023. <div class="example">
  1024. <pre class="verbatim">for_each(sort_rows, A); // (not in ra::) will iterate over 1-cells of A, sort_rows knows
  1025. </pre></div>
  1026. <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>.
  1027. </p>
  1028. <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.
  1029. </p>
  1030. <p>A standard example is ‘outer product’.
  1031. </p>
  1032. <div class="example">
  1033. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3};
  1034. ra::Big&lt;int, 1&gt; b = {40, 50};
  1035. 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; }),
  1036. a, b)
  1037. </pre><pre class="example">&rArr; axb = {{40, 80, 120}, {50, 100, 150}}
  1038. </pre></div>
  1039. <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:
  1040. </p>
  1041. <div class="example">
  1042. <pre class="verbatim">a: 3 |
  1043. b: | 2
  1044. </pre></div>
  1045. <p>Now the frames are rank-extended through prefix agreement.
  1046. </p>
  1047. <div class="example">
  1048. <pre class="verbatim">a: 3 |
  1049. b: 3 | 2
  1050. </pre></div>
  1051. <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)).
  1052. </p>
  1053. <div class="example">
  1054. <pre class="verbatim">a: 3 | 2
  1055. b: 3 | 2
  1056. </pre></div>
  1057. <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.
  1058. </p>
  1059. <div class="example">
  1060. <pre class="verbatim">ra::Big&lt;int, 1&gt; a = {1, 2, 3};
  1061. ra::Big&lt;int, 1&gt; b = {40, 50};
  1062. ra::Big&lt;int, 2&gt; axb = from([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }), a, b)
  1063. </pre><pre class="example">&rArr; axb = {{40, 80, 120}, {50, 100, 150}}
  1064. </pre></div>
  1065. <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.
  1066. </p>
  1067. <div class="example">
  1068. <pre class="verbatim">C: m | | n
  1069. A: m | p |
  1070. B: | p | n
  1071. </pre></div>
  1072. <p>First we&rsquo;ll align the first axes of C and A using the cell ranks (1, 1, 2). The cell shapes are:
  1073. </p>
  1074. <div class="example">
  1075. <pre class="verbatim">C: m | n
  1076. A: m | p
  1077. B: | p n
  1078. </pre></div>
  1079. <p>Then we&rsquo;ll use the ranks (1, 0, 1) on the cells:
  1080. </p>
  1081. <div class="example">
  1082. <pre class="verbatim">C: m | | n
  1083. A: m | p |
  1084. B: | p | n
  1085. </pre></div>
  1086. <p>The final operation is a standard operation on arrays of scalars. In actual <code>ra::</code> syntax:
  1087. </p>
  1088. <div class="example">
  1089. <pre class="verbatim">ra::Big A({3, 2}, {1, 2, 3, 4, 5, 6});
  1090. ra::Big B({2, 3}, {7, 8, 9, 10, 11, 12});
  1091. ra::Big C({3, 3}, 0.);
  1092. 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);
  1093. </pre><pre class="example">&rArr; C = {{27, 30, 33}, {61, 68, 75}, {95, 106, 117}}
  1094. </pre></div>
  1095. <p>Note that <code>wrank</code> cannot be used to transpose axes in general.
  1096. </p>
  1097. <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.
  1098. </p>
  1099. <hr>
  1100. </div>
  1101. <div class="section" id="Compatibility">
  1102. <div class="header">
  1103. <p>
  1104. 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>
  1105. </div>
  1106. <span id="Compatibility-1"></span><h3 class="section">2.9 Compatibility</h3>
  1107. <ul class="section-toc">
  1108. <li><a href="#Using-other-C-and-C_002b_002b-types-with-ra_003a_003a" accesskey="1">Using other C and C++ types with <code>ra::</code></a></li>
  1109. <li><a href="#Using-ra_003a_003a-types-with-the-STL" accesskey="2">Using <code>ra::</code> types with the STL</a></li>
  1110. <li><a href="#Using-ra_003a_003a-types-with-other-libraries" accesskey="3">Using <code>ra::</code> types with other libraries</a></li>
  1111. </ul>
  1112. <div class="subsection" id="Using-other-C-and-C_002b_002b-types-with-ra_003a_003a">
  1113. <h4 class="subsection">2.9.1 Using other C and C++ types with <code>ra::</code></h4>
  1114. <span id="index-foreign-type"></span>
  1115. <span id="x_002dforeign_002dtype"></span><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.
  1116. </p>
  1117. <div class="example">
  1118. <pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
  1119. ra::Small&lt;int, 3&gt; y = {6, 5, 4};
  1120. cout &lt;&lt; (x-y) &lt;&lt; endl;
  1121. </pre><pre class="example">-| -5 -3 -1
  1122. </pre></div>
  1123. <span id="index-start"></span>
  1124. <p>Foreign types can be brought into <code>ra::</code> explicitly with the function <a href="#x_002dstart"><code>start</code></a>.
  1125. </p>
  1126. <div class="example">
  1127. <pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
  1128. // cout &lt;&lt; sum(x) &lt;&lt; endl; // error, sum not found
  1129. cout &lt;&lt; sum(ra::start(x)) &lt;&lt; endl;
  1130. cout &lt;&lt; ra::sum(x) &lt;&lt; endl;
  1131. </pre><pre class="example">-| 6
  1132. </pre></div>
  1133. <p>The following types are accepted as foreign types:
  1134. </p>
  1135. <ul>
  1136. <li> Built-in arrays <span id="index-built_002din-array"></span>
  1137. produce an expression of positive rank and ct size.
  1138. </li><li> <code>std::array</code>
  1139. produces an expression of rank 1 and ct size.
  1140. </li><li> Other types conforming to <code>std::ranges::random_access_range</code>, including <code>std::vector</code>, <code>std::string</code>, etc.
  1141. produce an expression of rank 1 and rt size.
  1142. </li></ul>
  1143. <p>Raw pointers must be brought into <code>ra::</code> explicitly using the function <a href="#x_002dptr"><code>ptr</code></a>, which produces an expression of rank 1 and <em>undefined</em> size.
  1144. </p>
  1145. <p>Compare:
  1146. </p>
  1147. <div class="example">
  1148. <pre class="verbatim">int p[] = {1, 2, 3};
  1149. int * z = p;
  1150. ra::Big&lt;int, 1&gt; q {1, 2, 3};
  1151. q += p; // ok, q is ra::, p is foreign object with shape info
  1152. ra::start(p) += q; // can't redefine operator+=(int[]), foreign needs ra::start()
  1153. // z += q; // error: raw pointer needs ra::ptr()
  1154. ra::ptr(z) += p; // ok, shape is determined by foreign object p
  1155. </pre></div>
  1156. <span id="x_002dis_002dscalar"></span><p>Some types are accepted automatically as scalars. These include non-pointer types for which <code>std::is_scalar_v</code> is true, like <code>char</code>, <code>int</code>, <code>double</code>, etc. as well as <code>std::complex&lt;T&gt;</code>. You can add your own types as scalar types with the following declaration:
  1157. </p>
  1158. <pre class="verbatim"> namespace ra { template &lt;&gt; constexpr bool is_scalar_def&lt;MYTYPE&gt; = true; }
  1159. </pre>
  1160. <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>.
  1161. </p>
  1162. </div>
  1163. <div class="subsection" id="Using-ra_003a_003a-types-with-the-STL">
  1164. <h4 class="subsection">2.9.2 Using <code>ra::</code> types with the STL</h4>
  1165. <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 storage order of the view.
  1166. </p>
  1167. <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 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.
  1168. </p>
  1169. <div class="example">
  1170. <pre class="verbatim">ra::Big&lt;int&gt; x {3, 2, 1}; // x is a Container
  1171. auto y = x(); // y is a View on x
  1172. // std::sort(y.begin(), y.end()); // error: y.begin() is not RandomAccessIterator
  1173. std::sort(x.begin(), x.end()); // ok, we know x is stored in row-major order
  1174. </pre><pre class="example">&rArr; x = {1, 2, 3}
  1175. </pre></div>
  1176. <span id="index-other-libraries_002c-interfacing-with"></span>
  1177. </div>
  1178. <div class="subsection" id="Using-ra_003a_003a-types-with-other-libraries">
  1179. <h4 class="subsection">2.9.3 Using <code>ra::</code> types with other libraries</h4>
  1180. <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 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.
  1181. </p>
  1182. <ul class="section-toc">
  1183. <li><a href="#The-good-array-citizen" accesskey="1">The good array citizen</a></li>
  1184. <li><a href="#The-bad-array-citizen" accesskey="2">The bad array citizen</a></li>
  1185. </ul>
  1186. <div class="subsubsection" id="The-good-array-citizen">
  1187. <h4 class="subsubsection">2.9.3.1 The good array citizen</h4>
  1188. <span id="index-BLIS"></span>
  1189. <p>The good array citizen describes its arrays with the same parameters as <code>ra::</code>: a pointer, plus a length and a step per dimension. You don&rsquo;t have to worry about special cases. Say <a href="https://github.com/flame/blis">BLIS</a>:
  1190. </p>
  1191. <blockquote>
  1192. <pre class="verbatim">#include &lt;blis.h&gt;
  1193. ra::Big&lt;double, 2&gt; A({M, K}, ...);
  1194. ra::Big&lt;double, 2&gt; B({K, N}, ...);
  1195. ra::Big&lt;double, 2&gt; C({M, N}, ...);
  1196. double alpha = ...;
  1197. double beta = ...;
  1198. // C := βC + αAB
  1199. bli_dgemm(BLIS_NO_TRANSPOSE, BLIS_NO_TRANSPOSE, M, N, K, &amp;alpha,
  1200. A.data(), A.step(0), A.step(1),
  1201. B.data(), B.step(0), B.step(1),
  1202. &amp;beta, C.data(), C.step(0), C.step(1));
  1203. </pre></blockquote>
  1204. <span id="index-FFTW"></span>
  1205. <p>Another good array citizen, <a href="http://fftw.org">FFTW</a> handles arbitrary rank:
  1206. </p>
  1207. <blockquote>
  1208. <pre class="verbatim">#include &lt;fftw3.h&gt;
  1209. ...
  1210. using complex = std::complex&lt;double&gt;;
  1211. static_assert(sizeof(complex)==sizeof(fftw_complex));
  1212. // forward DFT over the last r axes of a -&gt; b
  1213. void fftw(int r, ra::View&lt;complex&gt; const a, ra::View&lt;complex&gt; b)
  1214. {
  1215. int const rank = a.rank();
  1216. assert(r&gt;0 &amp;&amp; r&lt;=rank);
  1217. assert(every(ra::start(shape(a))==shape(b)));
  1218. fftw_iodim dims[r];
  1219. fftw_iodim howmany_dims[rank-r];
  1220. for (int i=0; i!=rank; ++i) {
  1221. if (i&gt;=rank-r) {
  1222. dims[i-rank+r].n = a.len(i);
  1223. dims[i-rank+r].is = a.step(i);
  1224. dims[i-rank+r].os = b.step(i);
  1225. } else {
  1226. howmany_dims[i].n = a.len(i);
  1227. howmany_dims[i].is = a.step(i);
  1228. howmany_dims[i].os = b.step(i);
  1229. }
  1230. }
  1231. fftw_plan p;
  1232. p = fftw_plan_guru_dft(r, dims, rank-r, howmany_dims,
  1233. (fftw_complex *)(a.data()), (fftw_complex *)(b.data()),
  1234. FFTW_FORWARD, FFTW_ESTIMATE);
  1235. fftw_execute(p);
  1236. fftw_destroy_plan(p);
  1237. }
  1238. </pre></blockquote>
  1239. <span id="index-Guile"></span>
  1240. <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:
  1241. </p>
  1242. <blockquote>
  1243. <pre class="verbatim"> SCM a; // say a is #nf64(...)
  1244. ...
  1245. scm_t_array_handle h;
  1246. scm_array_get_handle(a, &amp;h);
  1247. scm_t_array_dim const * dims = scm_array_handle_dims(&amp;h);
  1248. View&lt;double&gt; v(map([](int i) { return ra::Dim { dims[i].ubnd-dims[i].lbnd+1, dims[i].inc }; },
  1249. ra::iota(scm_array_handle_rank(&amp;h))),
  1250. scm_array_handle_f64_writable_elements(&amp;h));
  1251. ...
  1252. scm_array_handle_release(&amp;h);
  1253. </pre></blockquote>
  1254. <span id="index-Numpy-2"></span>
  1255. <span id="index-Python"></span>
  1256. <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.
  1257. </p>
  1258. <p>It is usually 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>).
  1259. </p>
  1260. </div>
  1261. <div class="subsubsection" id="The-bad-array-citizen">
  1262. <h4 class="subsubsection">2.9.3.2 The bad array citizen</h4>
  1263. <p>Unfortunately there are many libraries that don&rsquo;t accept arbitrary array parameters, or that do strange things with particular values of lengths and/or steps.
  1264. </p>
  1265. <p>The most common case is that a library doesn&rsquo;t handle steps at all, and it only accepts unit step 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.
  1266. </p>
  1267. <p>Other libraries do accept steps, but not arbitrary ones. For example <a href="https://www.netlib.org/blas">https://www.netlib.org/blas</a>&rsquo; <code>cblas_dgemm</code> has this prototype:
  1268. </p>
  1269. <blockquote>
  1270. <pre class="verbatim">cblas_dgemm(order, transA, transB, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
  1271. </pre></blockquote>
  1272. <p><code>A</code>, <code>B</code>, <code>C</code> are (pointers to) 2-arrays, but the routine accepts only one step argument for each (<code>lda</code>, etc.). CBLAS also doesn&rsquo;t understand <code>lda</code> as a arbitrary step, but rather as the dimension of a larger array that you&rsquo;re slicing <code>A</code> from, and some implementations will mishandle negative or zero <code>lda</code>.
  1273. </p>
  1274. <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.
  1275. </p>
  1276. <span id="index-OpenGL"></span>
  1277. <p>OpenGL is another library that requires <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml">contortions:</a>
  1278. </p>
  1279. <blockquote>
  1280. <pre class="verbatim">void glVertexAttribPointer(GLuint index,
  1281. GLint size,
  1282. GLenum type,
  1283. GLboolean normalized,
  1284. GLsizei step,
  1285. const GLvoid * pointer);
  1286. </pre>
  1287. <p>[...]
  1288. </p>
  1289. <p><em>step</em>
  1290. </p>
  1291. <blockquote>
  1292. <p>Specifies the byte offset between consecutive generic vertex attributes. If step is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
  1293. </p></blockquote>
  1294. </blockquote>
  1295. <p>It isn&rsquo;t clear whether negative steps are legal, either. So just as with CBLAS, passing arbitrary array views may require copies.
  1296. </p>
  1297. <hr>
  1298. </div>
  1299. </div>
  1300. </div>
  1301. <div class="section" id="Extension">
  1302. <div class="header">
  1303. <p>
  1304. Next: <a href="#Error-handling" accesskey="n" rel="next">Error handling</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>
  1305. </div>
  1306. <span id="Extension-1"></span><h3 class="section">2.10 Extension</h3>
  1307. <ul class="section-toc">
  1308. <li><a href="#New-scalar-types" accesskey="1">New scalar types</a></li>
  1309. <li><a href="#New-array-operations" accesskey="2">New array operations</a></li>
  1310. </ul>
  1311. <div class="subsection" id="New-scalar-types">
  1312. <h4 class="subsection">2.10.1 New scalar types</h4>
  1313. <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>.
  1314. </p>
  1315. <div class="example">
  1316. <pre class="verbatim">struct W { int x; }
  1317. ra::Big&lt;W, 2&gt; w = {{ {4}, {2} }, { {1}, {3} }};
  1318. cout &lt;&lt; W(1, 1).x &lt;&lt; endl;
  1319. cout &lt;&lt; amin(map([](auto &amp;&amp; x) { return w.x; }, w)) &lt;&lt; endl;
  1320. </pre><pre class="example">-| 3
  1321. 1
  1322. </pre></div>
  1323. <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.
  1324. </p>
  1325. <div class="example">
  1326. <pre class="verbatim">namespace ra { template &lt;&gt; constexpr bool is_scalar_def&lt;W&gt; = true; }
  1327. ...
  1328. W ww {11};
  1329. for_each([](auto &amp;&amp; x, auto &amp;&amp; y) { cout &lt;&lt; (x.x + y.y) &lt;&lt; &quot; &quot;; }, w, ww); // ok
  1330. </pre><pre class="example">-| 15 13 12 14
  1331. </pre></div>
  1332. <p>but
  1333. </p>
  1334. <div class="example">
  1335. <pre class="verbatim">struct U { int x; }
  1336. U uu {11};
  1337. 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)
  1338. </pre></div>
  1339. <span id="x_002dnew_002darray_002doperations"></span></div>
  1340. <div class="subsection" id="New-array-operations">
  1341. <h4 class="subsection">2.10.2 New array operations</h4>
  1342. <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):
  1343. </p>
  1344. <div class="example">
  1345. <pre class="verbatim">return_type my_fun(...) { };
  1346. ...
  1347. namespace ra {
  1348. template &lt;class ... A&gt; inline auto
  1349. my_fun(A &amp;&amp; ... a)
  1350. {
  1351. return map(::my_fun, std::forward&lt;A&gt;(a) ...);
  1352. }
  1353. } // namespace ra
  1354. </pre></div>
  1355. <span id="index-overload-set"></span>
  1356. <p>If <code>my_fun</code> is an overload set, you can use<a id="DOCF10" href="#FOOT10"><sup>10</sup></a>
  1357. </p>
  1358. <div class="example">
  1359. <pre class="verbatim">namespace ra {
  1360. template &lt;class ... A&gt; inline auto
  1361. my_fun(A &amp;&amp; ... a)
  1362. {
  1363. return map([](auto &amp;&amp; ... a) { return ::my_fun(a ...); }, std::forward&lt;A&gt;(a) ...);
  1364. }
  1365. } // namespace ra
  1366. </pre></div>
  1367. <span id="index-error"></span>
  1368. <span id="index-assert"></span>
  1369. <hr>
  1370. </div>
  1371. </div>
  1372. <div class="section" id="Error-handling">
  1373. <div class="header">
  1374. <p>
  1375. 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>
  1376. </div>
  1377. <span id="Error-handling-1"></span><h3 class="section">2.11 Error handling</h3>
  1378. <p>Error handling in <code>ra::</code> is controlled by two macros:
  1379. </p>
  1380. <ul>
  1381. <li> <code>RA_DO_CHECK</code>
  1382. is a binary flag that controls runtime checks. The default is 1 which means to check for errors. 0 means not to check. The checks themselves are done with <code>RA_ASSERT</code>.
  1383. </li><li> <code>RA_ASSERT(cond, ...)</code>
  1384. is a function-like macro. <code>cond</code> is an expression that evaluates to true (in the <code>ra::</code> namespace) if the assertion is satisfied. The other arguments are informative and do not need to be used. If the assertion fails, the default definition of <code>RA_ASSERT(cond, ...)</code> prints those arguments to <code>std::cerr</code> and aborts.
  1385. </li></ul>
  1386. <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 <samp>-DNDEBUG</samp>.
  1387. </p>
  1388. <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 a user-defined exception instead.
  1389. </p>
  1390. <hr>
  1391. </div>
  1392. </div>
  1393. <div class="chapter" id="Extras">
  1394. <div class="header">
  1395. <p>
  1396. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1397. </div>
  1398. <span id="Extras-1"></span><h2 class="chapter">3 Extras</h2>
  1399. <hr>
  1400. </div>
  1401. <div class="chapter" id="Hazards">
  1402. <div class="header">
  1403. <p>
  1404. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1405. </div>
  1406. <span id="Hazards-1"></span><h2 class="chapter">4 Hazards</h2>
  1407. <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.
  1408. </p>
  1409. <ul class="section-toc">
  1410. <li><a href="#Reuse-of-expression-objects" accesskey="1">Reuse of expression objects</a></li>
  1411. <li><a href="#Assignment-to-views" accesskey="2">Assignment to views</a></li>
  1412. <li><a href="#View-of-const-vs-const-view" accesskey="3">View of const vs const view</a></li>
  1413. <li><a href="#Rank-extension-in-assignments" accesskey="4">Rank extension in assignments</a></li>
  1414. <li><a href="#Performance-pitfalls-of-rank-extension" accesskey="5">Performance pitfalls of rank extension</a></li>
  1415. <li><a href="#Chained-assignment" accesskey="6">Chained assignment</a></li>
  1416. <li><a href="#Unregistered-scalar-types" accesskey="7">Unregistered scalar types</a></li>
  1417. </ul>
  1418. <div class="section" id="Reuse-of-expression-objects">
  1419. <h3 class="section">4.1 Reuse of expression objects</h3>
  1420. <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>, or <code>ra::ptr</code>. Reuse errors are <em>not</em> checked. For example:
  1421. </p>
  1422. <div class="example">
  1423. <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}}
  1424. std::array&lt;int, 2&gt; l = { 1, 2 };
  1425. cout &lt;&lt; B(ra::ptr(l), ra::ptr(l)) &lt;&lt; endl; // ok =&gt; {{4 5} {7 8}}
  1426. auto ll = ra::ptr(l);
  1427. cout &lt;&lt; B(ll, ll) &lt;&lt; endl; // ??
  1428. </pre></div>
  1429. </div>
  1430. <div class="section" id="Assignment-to-views">
  1431. <h3 class="section">4.2 Assignment to views</h3>
  1432. <p>FIXME
  1433. With rt-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 <code>&gt;&gt;</code> container may work; do not rely on it.
  1434. </p>
  1435. </div>
  1436. <div class="section" id="View-of-const-vs-const-view">
  1437. <h3 class="section">4.3 View of const vs const view</h3>
  1438. <p>FIXME
  1439. Passing view arguments by reference
  1440. </p>
  1441. </div>
  1442. <div class="section" id="Rank-extension-in-assignments">
  1443. <h3 class="section">4.4 Rank extension in assignments</h3>
  1444. <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.
  1445. </p>
  1446. <div class="example">
  1447. <pre class="verbatim">int a = 0;
  1448. ra::scalar(a) = 3 + ra::Small&lt;int, 3&gt; {4, 5, 6}; // ?
  1449. </pre><pre class="example"> &rArr; a = 9
  1450. </pre></div>
  1451. <p>Compare with
  1452. </p>
  1453. <div class="example">
  1454. <pre class="verbatim">int a = 0;
  1455. ra::scalar(a) += 3 + ra::Small&lt;int, 3&gt; {4, 5, 6}; // 0 + 3 + 4 + 5 + 6
  1456. </pre><pre class="example"> &rArr; a = 18
  1457. </pre></div>
  1458. </div>
  1459. <div class="section" id="Performance-pitfalls-of-rank-extension">
  1460. <h3 class="section">4.5 Performance pitfalls of rank extension</h3>
  1461. <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.
  1462. </p>
  1463. <div class="example">
  1464. <pre class="verbatim">ra::Big&lt;int, 2&gt; a = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}};
  1465. ra::Big&lt;int, 1&gt; b = {1, 2, 3};
  1466. ra::Big&lt;int, 2&gt; c = map(f, b) + a;
  1467. </pre></div>
  1468. </div>
  1469. <div class="section" id="Chained-assignment">
  1470. <h3 class="section">4.6 Chained assignment</h3>
  1471. <p>FIXME
  1472. When <code>a=b=c</code> works, it operates as <code>b=c; a=b;</code> and not as an array expression.
  1473. </p>
  1474. </div>
  1475. <div class="section" id="Unregistered-scalar-types">
  1476. <h3 class="section">4.7 Unregistered scalar types</h3>
  1477. <p>FIXME
  1478. <code>View&lt;T, N&gt; x; x = T()</code> fails if <code>T</code> isn&rsquo;t registered as <code>is_scalar</code>.
  1479. </p>
  1480. <ol>
  1481. <li> Item 0
  1482. </li><li> Item 1
  1483. </li><li> Item 2
  1484. </li></ol>
  1485. <hr>
  1486. </div>
  1487. </div>
  1488. <div class="chapter" id="Internals">
  1489. <div class="header">
  1490. <p>
  1491. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1492. </div>
  1493. <span id="Internals-1"></span><h2 class="chapter">5 Internals</h2>
  1494. <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).
  1495. </p>
  1496. <ul class="section-toc">
  1497. <li><a href="#Headers" accesskey="1">Headers</a></li>
  1498. <li><a href="#Type-hierarchy" accesskey="2">Type hierarchy</a></li>
  1499. <li><a href="#Term-agreement" accesskey="3">Term agreement</a></li>
  1500. <li><a href="#Loop-types" accesskey="4">Loop types</a></li>
  1501. <li><a href="#Introspection" accesskey="5">Introspection</a></li>
  1502. <li><a href="#Building-with-ra_003a_003a" accesskey="6">Building with <code>ra::</code></a></li>
  1503. </ul>
  1504. <hr>
  1505. <div class="section" id="Headers">
  1506. <div class="header">
  1507. <p>
  1508. Next: <a href="#Type-hierarchy" accesskey="n" rel="next">Type hierarchy</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1509. </div>
  1510. <span id="Headers-1"></span><h3 class="section">5.1 Headers</h3>
  1511. <p>The header structure of <code>ra::</code> is as follows.<a id="DOCF11" href="#FOOT11"><sup>11</sup></a>
  1512. </p>
  1513. <ul>
  1514. <li> <code>tuples.hh</code> -
  1515. Generic macros and tuple library.
  1516. </li><li> <code>bootstrap.hh</code> -
  1517. Basic types, concepts, type predicates, introspection.
  1518. </li><li> <code>expr.hh</code> -
  1519. Expression template nodes, prefix matching.
  1520. </li><li> <code>ply.hh</code> -
  1521. Traversal, including I/O.
  1522. </li><li> <code>small.hh</code> -
  1523. Array type with compile time dimensions.
  1524. </li><li> <code>big.hh</code> -
  1525. Array type with run time dimensions.
  1526. </li><li> <code>optimize.hh</code> -
  1527. Optimization templates.
  1528. </li><li> <code>ra.hh</code> -
  1529. Functions and operators. Main header.
  1530. </li><li> <code>test.hh</code> -
  1531. (accessory) Testing library.
  1532. </li><li> <code>bench.hh</code> -
  1533. (accessory) Benchmarking library.
  1534. </li><li> <code>dual.hh</code> -
  1535. (accessory) Dual number type and operations.
  1536. </li></ul>
  1537. <img src="headers.png" alt="headers">
  1538. <hr>
  1539. </div>
  1540. <div class="section" id="Type-hierarchy">
  1541. <div class="header">
  1542. <p>
  1543. Next: <a href="#Term-agreement" accesskey="n" rel="next">Term agreement</a>, Previous: <a href="#Headers" accesskey="p" rel="prev">Headers</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1544. </div>
  1545. <span id="Type-hierarchy-1"></span><h3 class="section">5.2 Type hierarchy</h3>
  1546. <p>Some of the categories below are C++20 ‘concepts’, some are still informal.
  1547. </p>
  1548. <ul>
  1549. <li> <b>Container</b> &mdash; <code>Big</code>, <code>Shared</code>, <code>Unique</code>, <code>Small</code>
  1550. <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.
  1551. </p>
  1552. </li><li> <b>View</b> &mdash; <code>View</code>, <code>SmallView</code>
  1553. <p>These are array views into data in memory, which 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.
  1554. </p>
  1555. </li><li> <b>Iterator</b> &mdash; <code>CellBig</code>, <code>CellSmall</code>, <code>Iota</code>, <code>Ptr</code>, <code>Scalar</code>, <code>Expr</code>, <code>Pick</code>
  1556. <p>This is a traversable object. <b>Iterator</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>Iterator</b> itself, so most array expressions are <b>Iterator</b>s. <b>Iterator</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.
  1557. </p>
  1558. <p><b>Iterator</b>s have two traversal functions: <code>.adv(k, d)</code>, moves the iterator along any dimension <var>k</var>, and <code>.mov(d)</code>, is used on linearized views of the array. The methods <code>len()</code>, <code>step()</code>, <code>keep_step()</code> are used to determine the extent of these linearized views. In this way, a loop involving <b>Iterator</b>s can have its inner loop unfolded, which is faster than a multidimensional loop, especially if the inner dimensions of the loop are small.
  1559. </p>
  1560. <p><b>Iterator</b>s also provide an <code>at(i ...)</code> method for random access to any element.
  1561. </p>
  1562. </li></ul>
  1563. <hr>
  1564. </div>
  1565. <div class="section" id="Term-agreement">
  1566. <div class="header">
  1567. <p>
  1568. 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>
  1569. </div>
  1570. <span id="Term-agreement-1"></span><h3 class="section">5.3 Term agreement</h3>
  1571. <p>The execution of an expression template begins with the determination of its shape — the length 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 length (such as <code>iota()</code> or dimensions created with <code>insert</code>) are ignored. All the other terms must match.
  1572. </p>
  1573. <p>Then we select a 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 (v26) 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 flat loop.
  1574. </p>
  1575. <p>Finally we select a traversal method. <code>ra::</code> has two traversal methods: <code>ply_fixed</code> can be used when the rank and the traversal order are known at compile time, and <code>ply_ravel</code> can be used in the general case.
  1576. </p>
  1577. <hr>
  1578. </div>
  1579. <div class="section" id="Loop-types">
  1580. <div class="header">
  1581. <p>
  1582. 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>
  1583. </div>
  1584. <span id="Loop-types-1"></span><h3 class="section">5.4 Loop types</h3>
  1585. <p>TODO
  1586. </p>
  1587. <hr>
  1588. </div>
  1589. <div class="section" id="Introspection">
  1590. <div class="header">
  1591. <p>
  1592. Next: <a href="#Building-with-ra_003a_003a" accesskey="n" rel="next">Building with <code>ra::</code></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>
  1593. </div>
  1594. <span id="Introspection-1"></span><h3 class="section">5.5 Introspection</h3>
  1595. <p>The following functions are available to query the properties of <code>ra::</code> objects.
  1596. </p>
  1597. <span id="index-rank-1"></span>
  1598. <span id="x_002drank"></span><dl class="def">
  1599. <dt id="index-rank-2"><span class="category">Function<!-- /@w -->: </span><span><em>rank_t</em> <strong>rank</strong> <em>e</em><a href='#index-rank-2' class='copiable-anchor'> &para;</a></span></dt>
  1600. <dd>
  1601. <p>Return the rank of expression <var>e</var>.
  1602. </p>
  1603. </dd></dl>
  1604. <span id="index-shape-1"></span>
  1605. <span id="x_002dshape"></span><dl class="def">
  1606. <dt id="index-shape-2"><span class="category">Function<!-- /@w -->: </span><span><em>array</em> <strong>shape</strong> <em>e</em><a href='#index-shape-2' class='copiable-anchor'> &para;</a></span></dt>
  1607. <dt id="index-shape-3"><span class="category">Function<!-- /@w -->: </span><span><em>dim_t</em> <strong>shape</strong> <em>e k</em><a href='#index-shape-3' class='copiable-anchor'> &para;</a></span></dt>
  1608. <dd>
  1609. <p>The first form returns the shape of expression <var>e</var> as an array. The second form returns the length of axis <var>k</var>, i.e. <code>shape(e)[k]</code> ≡ <code>shape(e, k)</code>.
  1610. </p>
  1611. <p><var>array</var> might be a <a href="#x_002dforeign_002dtype"><code>foreign type</code></a> (such as <code>std::array</code> or <code>std::vector</code>) instead of a <code>ra-ra</code> type.
  1612. </p>
  1613. </dd></dl>
  1614. <hr>
  1615. </div>
  1616. <div class="section" id="Building-with-ra_003a_003a">
  1617. <div class="header">
  1618. <p>
  1619. 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>
  1620. </div>
  1621. <span id="Building-with-ra_003a_003a-1"></span><h3 class="section">5.6 Building with <code>ra::</code></h3>
  1622. <p>The following <code>#define</code>s affect the behavior of <code>ra::</code>.
  1623. </p>
  1624. <ul>
  1625. <li> <code>RA_DO_CHECK</code> (default 1): Check shape 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>). See <a href="#Error-handling">Error handling</a>.
  1626. </li><li> <code>RA_DO_OPT</code> (default 1): Sets the default for all <code>RA_DO_OPT_XXX</code> flags.
  1627. </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.
  1628. </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.
  1629. </li></ul>
  1630. <p><code>ra::</code> comes with three kinds of tests: examples, proper tests, and benchmarks. <code>ra::</code> uses its own 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>. <code>ra::</code> is highly dependent on optimization by the compiler and will be much slower with <samp>-O0</samp>.
  1631. </p>
  1632. <p>The following environment variables affect the test suite under SCons:
  1633. </p>
  1634. <ul>
  1635. <li> <code>RA_USE_BLAS</code> (default 0): Use BLAS for <code>gemm</code> and <code>gemv</code> benchmarks.
  1636. </li></ul>
  1637. <hr>
  1638. </div>
  1639. </div>
  1640. <div class="chapter" id="The-future">
  1641. <div class="header">
  1642. <p>
  1643. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1644. </div>
  1645. <span id="The-future-1"></span><h2 class="chapter">6 The future</h2>
  1646. <ul class="section-toc">
  1647. <li><a href="#Error-messages" accesskey="1">Error messages</a></li>
  1648. <li><a href="#Reductions" accesskey="2">Reductions</a></li>
  1649. <li><a href="#Etc" accesskey="3">Etc</a></li>
  1650. </ul>
  1651. <div class="section" id="Error-messages">
  1652. <h3 class="section">6.1 Error messages</h3>
  1653. <p>FIXME
  1654. </p>
  1655. </div>
  1656. <div class="section" id="Reductions">
  1657. <h3 class="section">6.2 Reductions</h3>
  1658. <p>FIXME
  1659. </p>
  1660. </div>
  1661. <div class="section" id="Etc">
  1662. <h3 class="section">6.3 Etc</h3>
  1663. <p>FIXME
  1664. </p>
  1665. <hr>
  1666. </div>
  1667. </div>
  1668. <div class="chapter" id="Reference">
  1669. <div class="header">
  1670. <p>
  1671. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  1672. </div>
  1673. <span id="Reference-1"></span><h2 class="chapter">7 Reference</h2>
  1674. <span id="index-agree"></span>
  1675. <span id="x_002dagree"></span><dl class="def">
  1676. <dt id="index-agree-1"><span class="category">Function: </span><span><strong>agree</strong> <em>arg ...</em><a href='#index-agree-1' class='copiable-anchor'> &para;</a></span></dt>
  1677. <dd><p>Return true if the shapes of arguments <var>arg...</var> match (see <a href="#Rank-extension">Rank extension</a>), else return false.
  1678. </p>
  1679. <p>This is useful when <a href="#Error-handling">error checking</a> is enabled and one wants to avoid the failure response.
  1680. </p><div class="example">
  1681. <pre class="verbatim"> ra::Small&lt;int, 2, 3&gt; A;
  1682. ra::Small&lt;int, 2&gt; B;
  1683. ra::Small&lt;int, 3&gt; C;
  1684. agree(A, B); // -&gt; true
  1685. static_assert(agree(A, B)); // ok for ct shapes
  1686. cout &lt;&lt; (A+B) &lt;&lt; endl; // ok
  1687. agree(A, C); // -&gt; false
  1688. cout &lt;&lt; (A+C) &lt;&lt; endl; // error. Maybe abort, maybe throw - cf Error Handling
  1689. </pre></div>
  1690. </dd></dl>
  1691. <span id="index-agree_005fop"></span>
  1692. <span id="x_002dagree_005fop"></span><dl class="def">
  1693. <dt id="index-agree_005fop-1"><span class="category">Function: </span><span><strong>agree_op</strong> <em>op arg ...</em><a href='#index-agree_005fop-1' class='copiable-anchor'> &para;</a></span></dt>
  1694. <dd><p>Return true if the shapes of arguments <var>arg...</var> match (see <a href="#Rank-extension">Rank extension</a>) relative to operator <var>op</var>, else return false.
  1695. </p>
  1696. <p>This differs from <a href="#x_002dagree"><code>agree</code></a> when <var>op</var> has non-zero argument ranks. For example:
  1697. </p><div class="example">
  1698. <pre class="verbatim"> ra::Big&lt;real, 1&gt; a({3}, 0.);
  1699. ra::Big&lt;real, 2&gt; b({2, 3}, 0.n);
  1700. agree(a, b); // -&gt; false
  1701. cout &lt;&lt; (a+b) &lt;&lt; endl; // error
  1702. agree_op(ra::wrank&lt;1, 1&gt;(std::plus()), a, b); // -&gt; true
  1703. cout &lt;&lt; map(ra::wrank&lt;1, 1&gt;(std::plus()), a, b) &lt;&lt; endl; // ok
  1704. </pre></div>
  1705. </dd></dl>
  1706. <span id="index-at"></span>
  1707. <span id="x_002dat"></span><dl class="def">
  1708. <dt id="index-at-1"><span class="category">Function: </span><span><strong>at</strong> <em>expr indices</em><a href='#index-at-1' class='copiable-anchor'> &para;</a></span></dt>
  1709. <dd><p>Look up <var>expr</var> at each element of <var>indices</var>, which shall be a multi-index into <var>expr</var>.
  1710. </p>
  1711. <p>This can be used for sparse subscripting. For example:
  1712. </p><div class="example">
  1713. <pre class="verbatim"> ra::Big&lt;int, 2&gt; A = {{100, 101}, {110, 111}, {120, 121}};
  1714. ra::Big&lt;ra::Small&lt;int, 2&gt;, 2&gt; i = {{{0, 1}, {2, 0}}, {{1, 0}, {2, 1}}};
  1715. ra::Big&lt;int, 2&gt; B = at(A, i);
  1716. </pre><pre class="example"> &rArr; B = {{101, 120}, {110, 121}}
  1717. </pre></div>
  1718. </dd></dl>
  1719. <span id="index-cast"></span>
  1720. <span id="x_002dcast"></span><dl class="def">
  1721. <dt id="index-cast-1"><span class="category">Function: </span><span><strong>cast</strong> <em>&lt;type&gt; expr</em><a href='#index-cast-1' class='copiable-anchor'> &para;</a></span></dt>
  1722. <dd><p>Create an array expression that casts <var>expr</var> into <var>type</var>.
  1723. </p></dd></dl>
  1724. <span id="index-collapse"></span>
  1725. <span id="x_002dcollapse"></span><dl class="def">
  1726. <dt id="index-collapse-1"><span class="category">Function: </span><span><strong>collapse</strong><a href='#index-collapse-1' class='copiable-anchor'> &para;</a></span></dt>
  1727. <dd><p>TODO
  1728. See also <a href="#x_002dexplode"><code>explode</code></a>.
  1729. </p></dd></dl>
  1730. <span id="index-concrete"></span>
  1731. <span id="x_002dconcrete"></span><dl class="def">
  1732. <dt id="index-concrete-1"><span class="category">Function: </span><span><strong>concrete</strong> <em>a</em><a href='#index-concrete-1' class='copiable-anchor'> &para;</a></span></dt>
  1733. <dd><p>Convert the argument to a container of the same shape as <var>a</var>.
  1734. </p>
  1735. <p>If the argument has rt or ct 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.
  1736. </p>
  1737. </dd></dl>
  1738. <span id="index-diag"></span>
  1739. <span id="x_002ddiag"></span><dl class="def">
  1740. <dt id="index-diag-1"><span class="category">Function: </span><span><strong>diag</strong> <em>view</em><a href='#index-diag-1' class='copiable-anchor'> &para;</a></span></dt>
  1741. <dd><p>Equivalent to <code>transpose&lt;0, 0&gt;(view)</code>.
  1742. </p></dd></dl>
  1743. <span id="index-explode"></span>
  1744. <span id="x_002dexplode"></span><dl class="def">
  1745. <dt id="index-explode-1"><span class="category">Function: </span><span><strong>explode</strong><a href='#index-explode-1' class='copiable-anchor'> &para;</a></span></dt>
  1746. <dd><p>TODO
  1747. </p>
  1748. <p>See also <a href="#x_002dcollapse"><code>collapse</code></a>.
  1749. </p></dd></dl>
  1750. <span id="index-for_005feach"></span>
  1751. <span id="x_002dfor_005feach"></span><dl class="def">
  1752. <dt id="index-for_005feach-1"><span class="category">Function: </span><span><strong>for_each</strong> <em>op expr ...</em><a href='#index-for_005feach-1' class='copiable-anchor'> &para;</a></span></dt>
  1753. <dd><p>Create an array expression that applies <var>op</var> to <var>expr</var> ..., and traverse it. The return value of <var>op</var> is discarded.
  1754. </p>
  1755. <p>For example:
  1756. </p><div class="example">
  1757. <pre class="verbatim">double s = 0.;
  1758. for_each([&amp;s](auto &amp;&amp; a) { s+=a; }, ra::Small&lt;double, 1&gt; {1., 2., 3})
  1759. </pre><pre class="example">&rArr; s = 6.
  1760. </pre></div>
  1761. <p>See also <a href="#x_002dmap"><code>map</code></a>.
  1762. </p>
  1763. </dd></dl>
  1764. <span id="index-format_005farray"></span>
  1765. <span id="x_002dformat_005farray"></span><dl class="def">
  1766. <dt id="index-format_005farray-1"><span class="category">Function: </span><span><strong>format_array</strong> <em>expr [last_axis_separator [second_last_axis_separator ...]]</em><a href='#index-format_005farray-1' class='copiable-anchor'> &para;</a></span></dt>
  1767. <dd><p>Formats an array for character output.
  1768. </p>
  1769. <p>For example:
  1770. </p>
  1771. <div class="example">
  1772. <pre class="verbatim">ra::Small&lt;int, 2, 2&gt; A = {{1, 2}, {3, 4}};
  1773. cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
  1774. cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; format_array(A) &lt;&lt; endl;
  1775. cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; format_array(A, &quot;|&quot;, &quot;-&quot;) &lt;&lt; endl;
  1776. </pre><pre class="example">-| case a:
  1777. 1 2
  1778. 3 4
  1779. case b:
  1780. 1 2
  1781. 3 4
  1782. case c:
  1783. 1|2-3|4
  1784. </pre></div>
  1785. <p>The shape that might be printed before the expression itself (depending on its type) is not subject to these separators.
  1786. </p>
  1787. <p>See also <a href="#x_002dnoshape"><code>noshape</code></a>, <a href="#x_002dwithshape"><code>withshape</code></a>.
  1788. </p>
  1789. </dd></dl>
  1790. <span id="index-from"></span>
  1791. <span id="x_002dfrom"></span><dl class="def">
  1792. <dt id="index-from-1"><span class="category">Function: </span><span><strong>from</strong> <em>op expr ...</em><a href='#index-from-1' class='copiable-anchor'> &para;</a></span></dt>
  1793. <dd><p>Create outer product expression. This is defined as
  1794. </p>
  1795. <div class="display">
  1796. <pre class="display">E = from(op, e₀, e₁ ...) ⇒ E(i₀₀, i₀₁ ..., i₁₀, i₁₁, ..., ...) = op[expr₀(i₀₀, i₀₁, ...), expr₁(i₁₀, i₁₁, ...), ...].
  1797. </pre></div>
  1798. <p>For example:
  1799. </p><div class="example">
  1800. <pre class="verbatim"> ra::Big&lt;double, 1&gt; a {1, 2, 3};
  1801. ra::Big&lt;double, 1&gt; b {10, 20, 30};
  1802. ra::Big&lt;double, 2&gt; axb = from([](auto &amp;&amp; a, auto &amp;&amp; b) { return a*b; }, a, b)
  1803. </pre><pre class="example"> &rArr; axb = {{10, 20, 30}, {20, 40, 60}, {30, 60, 90}}
  1804. </pre></div>
  1805. <div class="example">
  1806. <pre class="verbatim"> ra::Big&lt;int, 1&gt; i {2, 1};
  1807. ra::Big&lt;int, 1&gt; j {0, 1};
  1808. ra::Big&lt;double, 2&gt; A = {{1, 2}, {3, 4}, {5, 6}};
  1809. ra::Big&lt;double, 2&gt; Aij = from(A, i, j)
  1810. </pre><pre class="example"> &rArr; Aij = {{6, 5}, {4, 3}}
  1811. </pre></div>
  1812. <p>The last example is more or less how <code>A(i, j)</code> is implemented for arbitrary subscripts (see <a href="#The-rank-conjunction">The rank conjunction</a>).
  1813. </p>
  1814. </dd></dl>
  1815. <span id="index-imag_005fpart"></span>
  1816. <span id="x_002dimag_005fpart"></span><dl class="def">
  1817. <dt id="index-imag_005fpart-1"><span class="category">Function: </span><span><strong>imag_part</strong><a href='#index-imag_005fpart-1' class='copiable-anchor'> &para;</a></span></dt>
  1818. <dd><p>Take imaginary part of a complex number. This can be used as reference.
  1819. </p>
  1820. <p>For example: </p>
  1821. <div class="example">
  1822. <pre class="verbatim">ra::Small&lt;std::complex&lt;double&gt;, 2, 2&gt; A = {{1., 2.}, {3., 4.}};
  1823. imag_part(A) = -2*real_part(A);
  1824. cout &lt;&lt; A &lt;&lt; endl;
  1825. </pre><pre class="example">-|
  1826. (1, -2) (2, -4)
  1827. (3, -6) (4, -8)
  1828. </pre></div>
  1829. <p>See also <a href="#x_002dreal_005fpart"><code>real_part</code></a>.
  1830. </p></dd></dl>
  1831. <span id="index-map"></span>
  1832. <span id="x_002dmap"></span><dl class="def">
  1833. <dt id="index-map-1"><span class="category">Function: </span><span><strong>map</strong> <em>op expr ...</em><a href='#index-map-1' class='copiable-anchor'> &para;</a></span></dt>
  1834. <dd><p>Create an array expression that applies callable <var>op</var> to <var>expr</var> ...
  1835. </p>
  1836. <p>For example:
  1837. </p><div class="example">
  1838. <pre class="verbatim">ra::Big&lt;double, 1&gt; x = map(cos, std::array {0.});
  1839. </pre><pre class="example">&rArr; x = { 1. }
  1840. </pre></div>
  1841. <p><var>op</var> can return a reference. For example:
  1842. </p>
  1843. <div class="example">
  1844. <pre class="verbatim">ra::Big&lt;int, 2&gt; x = {{3, 3}, 0.};
  1845. ra::Big&lt;int, 2&gt; i = {0, 1, 1, 2};
  1846. ra::Big&lt;int, 2&gt; j = {1, 0, 2, 1};
  1847. map(x, i, j) = 1;
  1848. </pre><pre class="example">&rArr; x = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}
  1849. </pre></div>
  1850. <p>Note that <code>map(x, i, j)</code> is <a href="#x_002dsubscript_002douter_002dproduct">not the same</a> as <code>x(i, j)</code>.
  1851. </p>
  1852. <p><var>op</var> can be any callable. For example:
  1853. </p><div class="example">
  1854. <pre class="verbatim">struct A { int a, b; };
  1855. std::vector&lt;A&gt; v = {{1, 2}, {3, 4}};
  1856. ra::map(&amp;A::a, v) = -ra::map(&amp;A::b, v); // pointer to member
  1857. </pre><pre class="example">&rArr; v = {{-2, 2}, {-4, 4}}
  1858. </pre></div>
  1859. <p>See also <a href="#x_002dfor_005feach"><code>for_each</code></a>.
  1860. </p>
  1861. </dd></dl>
  1862. <span id="index-pack"></span>
  1863. <span id="x_002dpack"></span><dl class="def">
  1864. <dt id="index-pack-1"><span class="category">Function: </span><span><strong>pack</strong> <em>&lt;type&gt; expr ...</em><a href='#index-pack-1' class='copiable-anchor'> &para;</a></span></dt>
  1865. <dd><p>Create an array expression that brace-constructs <var>type</var> from <var>expr</var> ...
  1866. </p></dd></dl>
  1867. <span id="index-pick"></span>
  1868. <span id="x_002dpick"></span><dl class="def">
  1869. <dt id="index-pick-1"><span class="category">Function: </span><span><strong>pick</strong> <em>select_expr expr ...</em><a href='#index-pick-1' class='copiable-anchor'> &para;</a></span></dt>
  1870. <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.
  1871. </p>
  1872. <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>.
  1873. </p>
  1874. <p>For example:
  1875. </p><div class="example">
  1876. <pre class="verbatim"> ra::Small&lt;int, 3&gt; s {2, 1, 0};
  1877. ra::Small&lt;double, 3&gt; z = pick(s, s*s, s+s, sqrt(s));
  1878. </pre><pre class="example"> &rArr; z = {1.41421, 2, 0}
  1879. </pre></div>
  1880. </dd></dl>
  1881. <span id="index-ply"></span>
  1882. <span id="x_002dply"></span><dl class="def">
  1883. <dt id="index-ply-1"><span class="category">Function: </span><span><strong>ply</strong> <em>expr</em><a href='#index-ply-1' class='copiable-anchor'> &para;</a></span></dt>
  1884. <dd><p>Traverse <var>expr</var>. <code>ply</code> returns <code>void</code> so <var>expr</var> should be run for effect.
  1885. </p>
  1886. <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 equivalent to <code>ply(map(...))</code>, should cover most other uses.
  1887. </p>
  1888. <div class="example">
  1889. <pre class="verbatim">double s = 0.;
  1890. ply(map([&amp;s](auto &amp;&amp; a) { s+=a; }, ra::Small&lt;double, 1&gt; {1., 2., 3})) // same as for_each
  1891. </pre><pre class="example">&rArr; s = 6.
  1892. </pre></div>
  1893. </dd></dl>
  1894. <span id="index-real_005fpart"></span>
  1895. <span id="x_002dreal_005fpart"></span><dl class="def">
  1896. <dt id="index-real_005fpart-1"><span class="category">Function: </span><span><strong>real_part</strong><a href='#index-real_005fpart-1' class='copiable-anchor'> &para;</a></span></dt>
  1897. <dd><p>Take real part of a complex number. This can be used as reference.
  1898. </p>
  1899. <p>See also <a href="#x_002dimag_005fpart"><code>imag_part</code></a>.
  1900. </p></dd></dl>
  1901. <span id="index-reverse"></span>
  1902. <span id="x_002dreverse"></span><dl class="def">
  1903. <dt id="index-reverse-1"><span class="category">Function: </span><span><strong>reverse</strong> <em>view axis</em><a href='#index-reverse-1' class='copiable-anchor'> &para;</a></span></dt>
  1904. <dd><p>Create a new view by reversing axis <var>k</var> of <var>view</var>.
  1905. </p>
  1906. <p>This is equivalent to <code>view(ra::dots&lt;k&gt;, ra::iota(ra::len, ra::len-1, -1))</code>.
  1907. </p>
  1908. <p>This operation does not work on arbitrary array expressions yet. TODO FILL
  1909. </p>
  1910. </dd></dl>
  1911. <span id="index-size"></span>
  1912. <span id="x_002dsize"></span><dl class="def">
  1913. <dt id="index-size-1"><span class="category">Function: </span><span><strong>size</strong> <em>a</em><a href='#index-size-1' class='copiable-anchor'> &para;</a></span></dt>
  1914. <dd><p>Get the total size of an <code>ra::</code> object: the product of all its lengths.
  1915. </p></dd></dl>
  1916. <span id="index-stencil"></span>
  1917. <span id="x_002dstencil"></span><dl class="def">
  1918. <dt id="index-stencil-1"><span class="category">Function: </span><span><strong>stencil</strong> <em>view lo hi</em><a href='#index-stencil-1' class='copiable-anchor'> &para;</a></span></dt>
  1919. <dd><p>Create a stencil on <var>view</var> with lower bounds <var>lo</var> and higher bounds <var>hi</var>.
  1920. </p>
  1921. <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 lengths 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>.
  1922. </p>
  1923. <p>This operation does not work on arbitrary array expressions yet. TODO FILL
  1924. </p>
  1925. </dd></dl>
  1926. <span id="index-swap"></span>
  1927. <span id="x_002dswap"></span><dl class="def">
  1928. <dt id="index-swap-1"><span class="category">Function: </span><span><strong>swap</strong> <em>a b</em><a href='#index-swap-1' class='copiable-anchor'> &para;</a></span></dt>
  1929. <dd><p>Swap the contents of containers <var>a</var> and <var>b</var>.
  1930. </p>
  1931. <p>Both containers must be of the same storage type. The containers may have different shapes, but if at least one of them is of ct rank, then both of them must have the same rank.
  1932. </p>
  1933. <p>This function reuses <code>std::swap</code> for same-rank overloads, so it must not be qualified (i.e. use <code>swap(a, b)</code>, not <code>ra::swap(a, b)</code>).
  1934. </p></dd></dl>
  1935. <div class="example">
  1936. <pre class="verbatim"> ra::Big&lt;int&gt; a ({2, 3}, 1 + ra::_0 - ra::_1); // (1)
  1937. ra::Big&lt;int&gt; b ({2, 3, 4}, 1 - ra::_0 + ra::_1 + ra::_2); // (2)
  1938. swap(a, b);
  1939. // as if (1) had b and (2) had a
  1940. </pre></div>
  1941. <span id="index-transpose"></span>
  1942. <span id="x_002dtranspose"></span><dl class="def">
  1943. <dt id="index-transpose-1"><span class="category">Function: </span><span><strong>transpose</strong> <em>&lt;axes ...&gt; (view) | (axes, view)</em><a href='#index-transpose-1' class='copiable-anchor'> &para;</a></span></dt>
  1944. <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>.
  1945. </p>
  1946. <p>For example:
  1947. </p>
  1948. <div class="example">
  1949. <pre class="verbatim"> ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  1950. cout &lt;&lt; transpose&lt;1, 0&gt;(a) &lt;&lt; endl;
  1951. </pre><pre class="example"> -|
  1952. 3 2
  1953. 1 4
  1954. 2 5
  1955. 3 6
  1956. </pre></div>
  1957. <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:
  1958. </p>
  1959. <div class="example">
  1960. <pre class="verbatim"> ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  1961. cout &lt;&lt; transpose&lt;0, 0&gt;(a) &lt;&lt; endl; // { a(0, 0), a(1, 1) }
  1962. </pre><pre class="example"> -|
  1963. 2
  1964. 1 5
  1965. </pre></div>
  1966. <p>If one of the destination axes isn&rsquo;t mentioned in <var>axes</var>, then it becomes a ‘dead’ axis similar to those produced by <a href="#x_002dinsert"><code>insert</code></a>. For example:
  1967. </p>
  1968. <div class="example">
  1969. <pre class="verbatim"> ra::Unique&lt;double, 1&gt; a = {1, 2, 3};
  1970. cout &lt;&lt; ((a*10) + transpose&lt;1&gt;(a)) &lt;&lt; endl;
  1971. </pre><pre class="example"> -|
  1972. 3 3
  1973. 11 21 31
  1974. 12 22 32
  1975. 13 23 33
  1976. </pre></div>
  1977. <p>The two argument form lets you specify the axis list at runtime. In that case the result will have rt rank as well. For example: </p>
  1978. <div class="example">
  1979. <pre class="verbatim"> ra::Small&lt;int, 2&gt; axes = {0, 1};
  1980. ra::Unique&lt;double, 2&gt; a = {{1, 2, 3}, {4, 5, 6}};
  1981. cout &lt;&lt; &quot;A: &quot; &lt;&lt; transpose(axes, a) &lt;&lt; endl;
  1982. axes = {1, 0};
  1983. cout &lt;&lt; &quot;B: &quot; &lt;&lt; transpose(axes, a) &lt;&lt; endl;
  1984. </pre><pre class="example"> -|
  1985. A: 2
  1986. 2 3
  1987. 1 2 3
  1988. 4 5 6
  1989. B: 2
  1990. 3 2
  1991. 1 4
  1992. 2 5
  1993. 3 6
  1994. </pre></div>
  1995. <p>This operation does not work on arbitrary array expressions yet. TODO FILL
  1996. </p>
  1997. </dd></dl>
  1998. <span id="index-where"></span>
  1999. <span id="x_002dwhere"></span><dl class="def">
  2000. <dt id="index-where-1"><span class="category">Function: </span><span><strong>where</strong> <em>pred_expr true_expr false_expr</em><a href='#index-where-1' class='copiable-anchor'> &para;</a></span></dt>
  2001. <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.
  2002. </p>
  2003. <p>For example:
  2004. </p><div class="example">
  2005. <pre class="verbatim"> ra::Big&lt;double, 1&gt; s {1, -1, 3, 2};
  2006. s = where(s&gt;=2, 2, s); // saturate s
  2007. </pre><pre class="example"> &rArr; s = {1, -1, 2, 2}
  2008. </pre></div>
  2009. </dd></dl>
  2010. <span id="index-wrank"></span>
  2011. <span id="x_002dwrank"></span><dl class="def">
  2012. <dt id="index-wrank-1"><span class="category">Function: </span><span><strong>wrank</strong> <em>&lt;input_rank ...&gt; op</em><a href='#index-wrank-1' class='copiable-anchor'> &para;</a></span></dt>
  2013. <dd><p>Wrap <var>op</var> using a rank conjunction (see <a href="#The-rank-conjunction">The rank conjunction</a>).
  2014. </p>
  2015. <p>For example: TODO
  2016. </p><div class="example">
  2017. <pre class="verbatim"></pre><pre class="example"> &rArr; x = 0
  2018. </pre></div>
  2019. </dd></dl>
  2020. <span id="index-noshape"></span>
  2021. <span id="index-withshape"></span>
  2022. <span id="x_002dnoshape"></span><span id="x_002dwithshape"></span><dl class="def">
  2023. <dt id="index-withshape-noshape"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>withshape noshape</strong><a href='#index-withshape-noshape' class='copiable-anchor'> &para;</a></span></dt>
  2024. <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 will not be printed.
  2025. </p>
  2026. <p>If the object has ct 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 rt 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.
  2027. </p>
  2028. <p>For example:
  2029. </p>
  2030. <div class="example">
  2031. <pre class="verbatim">ra::Small&lt;int, 2, 2&gt; A = {77, 99};
  2032. cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
  2033. cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; ra::noshape &lt;&lt; A &lt;&lt; endl;
  2034. cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; ra::withshape &lt;&lt; A &lt;&lt; endl;
  2035. </pre><pre class="example">-| case a:
  2036. 77 99
  2037. case b:
  2038. 77 99
  2039. case c:
  2040. 2
  2041. 77 99
  2042. </pre></div>
  2043. <p>but:
  2044. </p>
  2045. <div class="example">
  2046. <pre class="verbatim">ra::Big&lt;int&gt; A = {77, 99};
  2047. cout &lt;&lt; &quot;case a:\n&quot; &lt;&lt; A &lt;&lt; endl;
  2048. cout &lt;&lt; &quot;case b:\n&quot; &lt;&lt; ra::noshape &lt;&lt; A &lt;&lt; endl;
  2049. cout &lt;&lt; &quot;case c:\n&quot; &lt;&lt; ra::withshape &lt;&lt; A &lt;&lt; endl;
  2050. </pre><pre class="example">-| case a:
  2051. 1
  2052. 2
  2053. 77 99
  2054. case b:
  2055. 77 99
  2056. case c:
  2057. 1
  2058. 2
  2059. 77 99
  2060. </pre></div>
  2061. <p>Note that in the last example the very shape of <code>ra::Big&lt;int&gt;</code> has rt length, so that length (the rank of <code>A</code>, that is 1) is printed as part of the shape of <code>A</code>.
  2062. </p>
  2063. <p>See also <a href="#x_002dformat_005farray"><code>format_array</code></a>.
  2064. </p>
  2065. </dd></dl>
  2066. <span id="index-ptr"></span>
  2067. <span id="x_002dptr"></span><dl class="def">
  2068. <dt id="index-ptr-1"><span class="category">Function<!-- /@w -->: </span><span><strong>ptr</strong> <em>bidirectional_iterator [len]</em><a href='#index-ptr-1' class='copiable-anchor'> &para;</a></span></dt>
  2069. <dt id="index-ptr-2"><span class="category">Function<!-- /@w -->: </span><span><strong>ptr</strong> <em>bidirectional_range</em><a href='#index-ptr-2' class='copiable-anchor'> &para;</a></span></dt>
  2070. <dd><p>Create rank-1 expression from foreign object.
  2071. </p>
  2072. <p>If <code>len</code> is not given for <var>bidirectional_iterator</var>, the expression has undefined length, and it will need to be matched with other expressions whose length is defined. <code>ra::</code> doesn&rsquo;t know what is actually accessible through the iterator, so be careful. For instance:
  2073. </p>
  2074. <div class="example">
  2075. <pre class="verbatim">int pp[] = {1, 2, 3};
  2076. int * p = pp; // erase length
  2077. ra::Big&lt;int, 1&gt; v3 {1, 2, 3};
  2078. ra::Big&lt;int, 1&gt; v4 {1, 2, 3, 4};
  2079. v3 += ra::ptr(p); // ok, shape (3): v3 = {2, 4, 6}
  2080. v4 += ra::ptr(p); // undefined, shape (4): bad access to p[3]
  2081. // cout &lt;&lt; (ra::ptr(p)+ra::iota()) &lt;&lt; endl; // ct error, expression has undefined shape
  2082. cout &lt;&lt; (ra::ptr(p, 3)+ra::iota()) &lt;&lt; endl; // ok, prints { 1, 3, 5 }
  2083. cout &lt;&lt; (ra::ptr(p, 4)+ra::iota()) &lt;&lt; endl; // undefined, bad access at p[4]
  2084. </pre></div>
  2085. <p>Of course in this example one could simply have used <code>pp</code> instead of <code>ra::ptr(p)</code>, since the array type retains shape information.
  2086. </p>
  2087. <div class="example">
  2088. <pre class="verbatim">v3 += pp; // ok
  2089. v4 += pp; // error checked by ra::, shape clash (4) += (3)
  2090. cout &lt;&lt; (p + ra::iota()) &lt;&lt; endl; // ok
  2091. </pre></div>
  2092. <p>You don&rsquo;t need to use <code>ra::ptr</code> on STL containers and built in arrays, which are converted to rank-1 expressions of the right size automatically. See also <a href="#x_002dstart"><code>start</code></a>.
  2093. </p>
  2094. </dd></dl>
  2095. <span id="index-start-1"></span>
  2096. <span id="x_002dstart"></span><dl class="def">
  2097. <dt id="index-start-2"><span class="category">Function: </span><span><strong>start</strong> <em>foreign_object</em><a href='#index-start-2' class='copiable-anchor'> &para;</a></span></dt>
  2098. <dd><p>Create array expression from <var>foreign_object</var>.
  2099. </p>
  2100. <p><var>foreign_object</var> can be a built-in array (e.g. <code>int[3][2]</code>), a <code>std::random_access_range</code> type (including <code>std::vector</code> or <code>std::array</code>, see <a href="#Compatibility">Compatibility</a>), 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 shape 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.
  2101. </p>
  2102. <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.
  2103. </p>
  2104. <div class="example">
  2105. <pre class="verbatim">std::vector&lt;int&gt; x = {1, 2, 3};
  2106. ra::Big&lt;int, 1&gt; y = {10, 20, 30};
  2107. cout &lt;&lt; (x+y) &lt;&lt; endl; // same as ra::start(x)+y
  2108. // x += y; // error: no match for operator+=
  2109. ra::start(x) += y; // ok
  2110. </pre><pre class="example">-| 3
  2111. 11 22 33
  2112. &rArr; x = { 11, 22, 33 }
  2113. </pre></div>
  2114. </dd></dl>
  2115. <span id="index-scalar"></span>
  2116. <span id="x_002dscalar"></span><dl class="def">
  2117. <dt id="index-scalar-1"><span class="category">Function: </span><span><strong>scalar</strong> <em>expr</em><a href='#index-scalar-1' class='copiable-anchor'> &para;</a></span></dt>
  2118. <dd><p>Create scalar expression from <var>expr</var>.
  2119. </p>
  2120. <p>The primary use of this function is to bring a scalar object into the <code>ra::</code> namespace. A somewhat artificial example:
  2121. </p>
  2122. <div class="example">
  2123. <pre class="verbatim">struct W { int x; }
  2124. ra::Big&lt;W, 1&gt; w { {1}, {2}, {3} };
  2125. // error: no matching function for call to start(W)
  2126. // for_each([](auto &amp;&amp; a, auto &amp;&amp; b) { cout &lt;&lt; (a.x + b.x) &lt;&lt; endl; }, w, W {7});
  2127. // bring W into ra:: with ra::scalar
  2128. 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}));
  2129. </pre><pre class="example">-| 8
  2130. 9
  2131. 10
  2132. </pre></div>
  2133. <p>See also <a href="#x_002dscalar_002dchar_002dstar"><code>this example</code></a>.
  2134. </p>
  2135. <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:
  2136. </p><div class="example">
  2137. <pre class="verbatim">using Vec2 = ra::Small&lt;double, 2&gt;;
  2138. Vec2 x {-1, 1};
  2139. ra::Big&lt;Vec2, 1&gt; c { {1, 2}, {2, 3}, {3, 4} };
  2140. // c += x // error: x has shape (2) and c has shape (3)
  2141. c += ra::scalar(x); // ok: scalar(x) has shape () and matches c.
  2142. </pre><pre class="example"> &rArr; c = { {0, 3}, {1, 4}, {2, 5} }
  2143. </pre></div>
  2144. <p>The result is {c(0)+x, c(1)+x, c(2)+x}. Compare this with
  2145. </p><div class="example">
  2146. <pre class="verbatim">c(ra::iota(2)) += x; // c(ra::iota(2)) with shape (2) matches x with shape (2)
  2147. </pre><pre class="example"> &rArr; c = { {-1, 2}, {2, 5}, {2, 5} }
  2148. </pre></div>
  2149. <p>where the result is {c(0)+x(0), c(1)+x(1), c(2)}.
  2150. </p>
  2151. </dd></dl>
  2152. <span id="index-iter"></span>
  2153. <span id="x_002diter"></span><dl class="def">
  2154. <dt id="index-iter-1"><span class="category">Function: </span><span><strong>iter</strong> <em>&lt;k&gt; (view)</em><a href='#index-iter-1' class='copiable-anchor'> &para;</a></span></dt>
  2155. <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 rt or ct shape.
  2156. </p>
  2157. <div class="example">
  2158. <pre class="verbatim">ra::Big&lt;int, 2&gt; c {{1, 3, 2}, {7, 1, 3}};
  2159. 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;
  2160. ra::Big&lt;int, 1&gt; m({3}, 0);
  2161. scalar(m) = max(scalar(m), iter&lt;1&gt;(c));
  2162. cout &lt;&lt; &quot;max of each column: &quot; &lt;&lt; m &lt;&lt; endl;
  2163. m = 0;
  2164. for_each([&amp;m](auto &amp;&amp; a) { m = max(m, a); }, iter&lt;1&gt;(c));
  2165. cout &lt;&lt; &quot;max of each column again: &quot; &lt;&lt; m &lt;&lt; endl;
  2166. </pre><pre class="example">-| max of each row: 2
  2167. 3 7
  2168. max of each column: 3
  2169. 7 3 3
  2170. max of each column again: 3
  2171. 7 3 3
  2172. </pre></div>
  2173. <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>.
  2174. </p>
  2175. <div class="example">
  2176. <pre class="verbatim">m = 0;
  2177. iter&lt;1&gt;(m) = max(iter&lt;1&gt;(m), iter&lt;1&gt;(c));
  2178. cout &lt;&lt; &quot;max of each column yet again: &quot; &lt;&lt; m &lt;&lt; endl;
  2179. </pre><pre class="example">-| max of each column again: 3
  2180. 7 3 3
  2181. </pre></div>
  2182. <p>The following example computes the trace of each of the items [(-1)-cells] of <code>c</code>. </p>
  2183. <div class="example">
  2184. <pre class="verbatim">ra::Small&lt;int, 3, 2, 2&gt; c = ra::_0 - ra::_1 - 2*ra::_2;
  2185. cout &lt;&lt; &quot;c: &quot; &lt;&lt; c &lt;&lt; endl;
  2186. 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;
  2187. </pre><pre class="example">-| c: 0 -2
  2188. -1 -3
  2189. 1 -1
  2190. 0 -2
  2191. 2 0
  2192. 1 -1
  2193. s: -3 -1 -1
  2194. </pre></div>
  2195. </dd></dl>
  2196. <span id="index-sum"></span>
  2197. <span id="x_002dsum"></span><dl class="def">
  2198. <dt id="index-sum-1"><span class="category">Function: </span><span><strong>sum</strong> <em>expr</em><a href='#index-sum-1' class='copiable-anchor'> &para;</a></span></dt>
  2199. <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.
  2200. </p></dd></dl>
  2201. <span id="index-prod"></span>
  2202. <span id="x_002dprod"></span><dl class="def">
  2203. <dt id="index-prod-1"><span class="category">Function: </span><span><strong>prod</strong> <em>expr</em><a href='#index-prod-1' class='copiable-anchor'> &para;</a></span></dt>
  2204. <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.
  2205. </p></dd></dl>
  2206. <span id="index-amax"></span>
  2207. <span id="x_002damax"></span><dl class="def">
  2208. <dt id="index-amax-1"><span class="category">Function: </span><span><strong>amax</strong> <em>expr</em><a href='#index-amax-1' class='copiable-anchor'> &para;</a></span></dt>
  2209. <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>.
  2210. </p></dd></dl>
  2211. <span id="index-amin"></span>
  2212. <span id="x_002damin"></span><dl class="def">
  2213. <dt id="index-amin-1"><span class="category">Function: </span><span><strong>amin</strong> <em>expr</em><a href='#index-amin-1' class='copiable-anchor'> &para;</a></span></dt>
  2214. <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>.
  2215. </p></dd></dl>
  2216. <span id="index-early"></span>
  2217. <span id="x_002dearly"></span><dl class="def">
  2218. <dt id="index-early-1"><span class="category">Function: </span><span><strong>early</strong> <em>expr default</em><a href='#index-early-1' class='copiable-anchor'> &para;</a></span></dt>
  2219. <dd><p><var>expr</var> is an array expression that returns <code>std::optional&lt;T&gt;</code>. <var>expr</var> is traversed as by <code>for_each</code>. If the optional ever contains a value, traversal stops and that value is returned. If traversal ends, <var>default</var> is returned instead. If <code>default</code> is a reference, <code>early</code> will return its value. </p>
  2220. <p>The following definition of elementwise <code>lexicographical_compare</code> relies on <code>early</code>.
  2221. </p>
  2222. <div class="example">
  2223. <pre class="verbatim">template &lt;class A, class B&gt;
  2224. inline bool
  2225. lexicographical_compare(A &amp;&amp; a, B &amp;&amp; b)
  2226. {
  2227. return early(map([](auto &amp;&amp; a, auto &amp;&amp; b) { return a==b ? std::nullopt : std::make_optional(a&lt;b); },
  2228. std::forward&lt;A&gt;(a), std::forward&lt;B&gt;(b)),
  2229. false);
  2230. }
  2231. </pre></div>
  2232. </dd></dl>
  2233. <span id="index-any"></span>
  2234. <span id="x_002dany"></span><dl class="def">
  2235. <dt id="index-any-1"><span class="category">Function: </span><span><strong>any</strong> <em>expr</em><a href='#index-any-1' class='copiable-anchor'> &para;</a></span></dt>
  2236. <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.
  2237. </p></dd></dl>
  2238. <span id="index-every"></span>
  2239. <span id="x_002devery"></span><dl class="def">
  2240. <dt id="index-every-1"><span class="category">Function: </span><span><strong>every</strong> <em>expr</em><a href='#index-every-1' class='copiable-anchor'> &para;</a></span></dt>
  2241. <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.
  2242. </p></dd></dl>
  2243. <span id="index-sqr"></span>
  2244. <span id="x_002dsqr"></span><dl class="def">
  2245. <dt id="index-sqr-1"><span class="category">Function: </span><span><strong>sqr</strong> <em>expr</em><a href='#index-sqr-1' class='copiable-anchor'> &para;</a></span></dt>
  2246. <dd><p>Compute the square of <var>expr</var>.
  2247. </p></dd></dl>
  2248. <span id="index-sqrm"></span>
  2249. <span id="x_002dsqrm"></span><dl class="def">
  2250. <dt id="index-sqrm-1"><span class="category">Function: </span><span><strong>sqrm</strong> <em>expr</em><a href='#index-sqrm-1' class='copiable-anchor'> &para;</a></span></dt>
  2251. <dd><p>Compute the square of the norm-2 of <var>expr</var>, that is, <code>conj(expr)*expr</code>.
  2252. </p></dd></dl>
  2253. <span id="index-conj"></span>
  2254. <span id="x_002dconj"></span><dl class="def">
  2255. <dt id="index-conj-1"><span class="category">Function: </span><span><strong>conj</strong> <em>expr</em><a href='#index-conj-1' class='copiable-anchor'> &para;</a></span></dt>
  2256. <dd><p>Compute the complex conjugate of <var>expr</var>.
  2257. </p></dd></dl>
  2258. <span id="index-xI"></span>
  2259. <span id="x_002dxI"></span><dl class="def">
  2260. <dt id="index-xI-1"><span class="category">Function: </span><span><strong>xI</strong> <em>expr</em><a href='#index-xI-1' class='copiable-anchor'> &para;</a></span></dt>
  2261. <dd><p>Compute <code>(0+1j)</code> times <var>expr</var>.
  2262. </p></dd></dl>
  2263. <span id="index-rel_005ferror"></span>
  2264. <span id="x_002drel_002derror"></span><dl class="def">
  2265. <dt id="index-rel_005ferror-1"><span class="category">Function: </span><span><strong>rel_error</strong> <em>a b</em><a href='#index-rel_005ferror-1' class='copiable-anchor'> &para;</a></span></dt>
  2266. <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
  2267. </p>
  2268. <p><code>(a==0. &amp;&amp; b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b))</code>
  2269. </p>
  2270. </dd></dl>
  2271. <span id="index-none-2"></span>
  2272. <span id="x_002dnone"></span><dl class="def">
  2273. <dt id="index-none-3"><span class="category">Special&nbsp;object<!-- /@w -->: </span><span><strong>none</strong><a href='#index-none-3' class='copiable-anchor'> &para;</a></span></dt>
  2274. <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:
  2275. </p>
  2276. <div class="example">
  2277. <pre class="verbatim">void old_style_initializer(int m, int n, double *);
  2278. ra::Big&lt;double&gt; b({2, 3}, ra::none);
  2279. old_style_initializer(2, 3, b.data());
  2280. </pre></div>
  2281. </dd></dl>
  2282. <hr>
  2283. </div>
  2284. <div class="chapter" id="Sources">
  2285. <div class="header">
  2286. <p>
  2287. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  2288. </div>
  2289. <span id="Sources-1"></span><h2 class="chapter">8 Sources</h2>
  2290. <table>
  2291. <tr><td width="10%"></td></tr>
  2292. <tr><td width="10%"><span id="Abr70"></span>[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>
  2293. <tr><td width="10%"></td></tr>
  2294. <tr><td width="10%"><span id="Ber87"></span>[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>
  2295. <tr><td width="10%"></td></tr>
  2296. <tr><td width="10%"><span id="bli17"></span>[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>
  2297. <tr><td width="10%"></td></tr>
  2298. <tr><td width="10%"><span id="Cha86"></span>[Cha86]</td><td width="90%">Gregory J. Chaitin. Physics in APL2, June 1986.</td></tr>
  2299. <tr><td width="10%"></td></tr>
  2300. <tr><td width="10%"><span id="FI68"></span>[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>
  2301. <tr><td width="10%"></td></tr>
  2302. <tr><td width="10%"><span id="FI73"></span>[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>
  2303. <tr><td width="10%"></td></tr>
  2304. <tr><td width="10%"><span id="FI78"></span>[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>
  2305. <tr><td width="10%"></td></tr>
  2306. <tr><td width="10%"><span id="J-S"></span>[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>
  2307. <tr><td width="10%"></td></tr>
  2308. <tr><td width="10%"><span id="Mat"></span>[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>
  2309. <tr><td width="10%"></td></tr>
  2310. <tr><td width="10%"><span id="num17"></span>[num17]</td><td width="90%">NumPy. <a href="http://www.numpy.org">http://www.numpy.org</a>, November 2017.</td></tr>
  2311. <tr><td width="10%"></td></tr>
  2312. <tr><td width="10%"><span id="Ric08"></span>[Ric08]</td><td width="90%">Henry Rich. J for C programmers, February 2008.</td></tr>
  2313. <tr><td width="10%"></td></tr>
  2314. <tr><td width="10%"><span id="SSM14"></span>[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>
  2315. <tr><td width="10%"></td></tr>
  2316. <tr><td width="10%"><span id="Vel01"></span>[Vel01]</td><td width="90%">Todd Veldhuizen. Blitz++ user’s guide, February 2001.</td></tr>
  2317. <tr><td width="10%"></td></tr>
  2318. <tr><td width="10%"><span id="Wad90"></span>[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>
  2319. </table>
  2320. <hr>
  2321. </div>
  2322. <div class="unnumbered" id="Indices">
  2323. <div class="header">
  2324. <p>
  2325. 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"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  2326. </div>
  2327. <span id="Indices-1"></span><h2 class="unnumbered">Indices</h2>
  2328. <table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
  2329. &nbsp;
  2330. <a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
  2331. &nbsp;
  2332. <a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
  2333. &nbsp;
  2334. <a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
  2335. &nbsp;
  2336. <a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
  2337. &nbsp;
  2338. <a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
  2339. &nbsp;
  2340. <a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
  2341. &nbsp;
  2342. <a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
  2343. &nbsp;
  2344. <a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
  2345. &nbsp;
  2346. <a class="summary-letter" href="#Indices_cp_letter-L"><b>L</b></a>
  2347. &nbsp;
  2348. <a class="summary-letter" href="#Indices_cp_letter-M"><b>M</b></a>
  2349. &nbsp;
  2350. <a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
  2351. &nbsp;
  2352. <a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
  2353. &nbsp;
  2354. <a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
  2355. &nbsp;
  2356. <a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
  2357. &nbsp;
  2358. <a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
  2359. &nbsp;
  2360. <a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
  2361. &nbsp;
  2362. <a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
  2363. &nbsp;
  2364. <a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
  2365. &nbsp;
  2366. <a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
  2367. &nbsp;
  2368. <a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
  2369. &nbsp;
  2370. </td></tr></table>
  2371. <table class="index-cp" border="0">
  2372. <tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
  2373. <tr><td colspan="4"> <hr></td></tr>
  2374. <tr><th id="Indices_cp_letter-A">A</th><td></td><td></td></tr>
  2375. <tr><td></td><td valign="top"><a href="#index-agree"><code>agree</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2376. <tr><td></td><td valign="top"><a href="#index-agree_005fop"><code>agree_op</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2377. <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>
  2378. <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>
  2379. <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>
  2380. <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>
  2381. <tr><td></td><td valign="top"><a href="#index-assert">assert</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
  2382. <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>
  2383. <tr><td colspan="4"> <hr></td></tr>
  2384. <tr><th id="Indices_cp_letter-B">B</th><td></td><td></td></tr>
  2385. <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>
  2386. <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>
  2387. <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>
  2388. <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>
  2389. <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>
  2390. <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>
  2391. <tr><td colspan="4"> <hr></td></tr>
  2392. <tr><th id="Indices_cp_letter-C">C</th><td></td><td></td></tr>
  2393. <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>
  2394. <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>
  2395. <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>
  2396. <tr><td></td><td valign="top"><a href="#index-compile_002dtime">compile-time</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2397. <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>
  2398. <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>
  2399. <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>
  2400. <tr><td></td><td valign="top"><a href="#index-ct">ct</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2401. <tr><td colspan="4"> <hr></td></tr>
  2402. <tr><th id="Indices_cp_letter-D">D</th><td></td><td></td></tr>
  2403. <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>
  2404. <tr><td colspan="4"> <hr></td></tr>
  2405. <tr><th id="Indices_cp_letter-E">E</th><td></td><td></td></tr>
  2406. <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>
  2407. <tr><td></td><td valign="top"><a href="#index-elision_002c-index">elision, index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2408. <tr><td></td><td valign="top"><a href="#index-end_002c-Octave_002fMatlab"><code>end</code>, Octave/Matlab</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2409. <tr><td></td><td valign="top"><a href="#index-error">error</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
  2410. <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>
  2411. <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>
  2412. <tr><td colspan="4"> <hr></td></tr>
  2413. <tr><th id="Indices_cp_letter-F">F</th><td></td><td></td></tr>
  2414. <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>
  2415. <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>
  2416. <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>
  2417. <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>
  2418. <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>
  2419. <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>
  2420. <tr><td colspan="4"> <hr></td></tr>
  2421. <tr><th id="Indices_cp_letter-G">G</th><td></td><td></td></tr>
  2422. <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>
  2423. <tr><td colspan="4"> <hr></td></tr>
  2424. <tr><th id="Indices_cp_letter-I">I</th><td></td><td></td></tr>
  2425. <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>
  2426. <tr><td></td><td valign="top"><a href="#index-insert"><code>insert</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2427. <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>
  2428. <tr><td colspan="4"> <hr></td></tr>
  2429. <tr><th id="Indices_cp_letter-J">J</th><td></td><td></td></tr>
  2430. <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>
  2431. <tr><td colspan="4"> <hr></td></tr>
  2432. <tr><th id="Indices_cp_letter-L">L</th><td></td><td></td></tr>
  2433. <tr><td></td><td valign="top"><a href="#index-len"><code>len</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2434. <tr><td></td><td valign="top"><a href="#index-length">length</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
  2435. <tr><td colspan="4"> <hr></td></tr>
  2436. <tr><th id="Indices_cp_letter-M">M</th><td></td><td></td></tr>
  2437. <tr><td></td><td valign="top"><a href="#index-map"><code>map</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2438. <tr><td colspan="4"> <hr></td></tr>
  2439. <tr><th id="Indices_cp_letter-N">N</th><td></td><td></td></tr>
  2440. <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>
  2441. <tr><td></td><td valign="top"><a href="#index-none-1"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2442. <tr><td></td><td valign="top"><a href="#index-none-2"><code>none</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2443. <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>
  2444. <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>
  2445. <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>
  2446. <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>
  2447. <tr><td colspan="4"> <hr></td></tr>
  2448. <tr><th id="Indices_cp_letter-O">O</th><td></td><td></td></tr>
  2449. <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>
  2450. <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>
  2451. <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>
  2452. <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>
  2453. <tr><td></td><td valign="top"><a href="#index-overload-set">overload set</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Extension">Extension</a></td></tr>
  2454. <tr><td colspan="4"> <hr></td></tr>
  2455. <tr><th id="Indices_cp_letter-P">P</th><td></td><td></td></tr>
  2456. <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>
  2457. <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>
  2458. <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>
  2459. <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>
  2460. <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>
  2461. <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>
  2462. <tr><td colspan="4"> <hr></td></tr>
  2463. <tr><th id="Indices_cp_letter-R">R</th><td></td><td></td></tr>
  2464. <tr><td></td><td valign="top"><a href="#index-rank">rank</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
  2465. <tr><td></td><td valign="top"><a href="#index-rank-1"><code>rank</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Introspection">Introspection</a></td></tr>
  2466. <tr><td></td><td valign="top"><a href="#index-rank_002c-compile_002dtime">rank, compile-time</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2467. <tr><td></td><td valign="top"><a href="#index-rank_002c-runtime">rank, runtime</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2468. <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>
  2469. <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>
  2470. <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>
  2471. <tr><td></td><td valign="top"><a href="#index-rt">rt</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2472. <tr><td></td><td valign="top"><a href="#index-runtime">runtime</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2473. <tr><td colspan="4"> <hr></td></tr>
  2474. <tr><th id="Indices_cp_letter-S">S</th><td></td><td></td></tr>
  2475. <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>
  2476. <tr><td></td><td valign="top"><a href="#index-shape">shape</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Overview">Overview</a></td></tr>
  2477. <tr><td></td><td valign="top"><a href="#index-shape-1"><code>shape</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Introspection">Introspection</a></td></tr>
  2478. <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>
  2479. <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>
  2480. <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>
  2481. <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>
  2482. <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>
  2483. <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>
  2484. <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>
  2485. <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>
  2486. <tr><td></td><td valign="top"><a href="#index-step">step</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
  2487. <tr><td></td><td valign="top"><a href="#index-stride">stride</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
  2488. <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>
  2489. <tr><td></td><td valign="top"><a href="#index-swap"><code>swap</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reference">Reference</a></td></tr>
  2490. <tr><td colspan="4"> <hr></td></tr>
  2491. <tr><th id="Indices_cp_letter-T">T</th><td></td><td></td></tr>
  2492. <tr><td></td><td valign="top"><a href="#index-TensorIndex">TensorIndex</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
  2493. <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>
  2494. <tr><td colspan="4"> <hr></td></tr>
  2495. <tr><th id="Indices_cp_letter-U">U</th><td></td><td></td></tr>
  2496. <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>
  2497. <tr><td></td><td valign="top"><a href="#index-uninitialized-container-1">uninitialized container</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
  2498. <tr><td colspan="4"> <hr></td></tr>
  2499. <tr><th id="Indices_cp_letter-V">V</th><td></td><td></td></tr>
  2500. <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>
  2501. <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>
  2502. <tr><td colspan="4"> <hr></td></tr>
  2503. <tr><th id="Indices_cp_letter-W">W</th><td></td><td></td></tr>
  2504. <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>
  2505. <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>
  2506. <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>
  2507. <tr><td colspan="4"> <hr></td></tr>
  2508. <tr><th id="Indices_cp_letter-X">X</th><td></td><td></td></tr>
  2509. <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>
  2510. <tr><td colspan="4"> <hr></td></tr>
  2511. </table>
  2512. <table><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
  2513. &nbsp;
  2514. <a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
  2515. &nbsp;
  2516. <a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
  2517. &nbsp;
  2518. <a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
  2519. &nbsp;
  2520. <a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
  2521. &nbsp;
  2522. <a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
  2523. &nbsp;
  2524. <a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
  2525. &nbsp;
  2526. <a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
  2527. &nbsp;
  2528. <a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
  2529. &nbsp;
  2530. <a class="summary-letter" href="#Indices_cp_letter-L"><b>L</b></a>
  2531. &nbsp;
  2532. <a class="summary-letter" href="#Indices_cp_letter-M"><b>M</b></a>
  2533. &nbsp;
  2534. <a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
  2535. &nbsp;
  2536. <a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
  2537. &nbsp;
  2538. <a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
  2539. &nbsp;
  2540. <a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
  2541. &nbsp;
  2542. <a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
  2543. &nbsp;
  2544. <a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
  2545. &nbsp;
  2546. <a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
  2547. &nbsp;
  2548. <a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
  2549. &nbsp;
  2550. <a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
  2551. &nbsp;
  2552. <a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
  2553. &nbsp;
  2554. </td></tr></table>
  2555. <hr>
  2556. </div>
  2557. <div class="unnumbered" id="Notes">
  2558. <div class="header">
  2559. <p>
  2560. Previous: <a href="#Indices" accesskey="p" rel="prev">Indices</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> &nbsp; [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
  2561. </div>
  2562. <span id="Notes-1"></span><h2 class="unnumbered">Notes</h2>
  2563. <ol>
  2564. <li> <code>ra::</code> uses the non standard <code>#pragma once</code> (supported on all major compilers).
  2565. </li></ol>
  2566. </div>
  2567. </div>
  2568. <div class="footnote">
  2569. <hr>
  2570. <h4 class="footnotes-heading">Footnotes</h4>
  2571. <h5><a id="FOOT1" href="#DOCF1">(1)</a></h5>
  2572. <p>/ə&rsquo;ɹ-eɪ/, I guess.</p>
  2573. <h5><a id="FOOT2" href="#DOCF2">(2)</a></h5>
  2574. <p>Sometimes &lsquo;strides&rsquo;. Cf. <a href="https://en.wikipedia.org/wiki/Dope_vector"><em>dope vector</em></a></p>
  2575. <h5><a id="FOOT3" href="#DOCF3">(3)</a></h5>
  2576. <p>Examples given without context assume that one has declared <code>using std::cout;</code>, etc.</p>
  2577. <h5><a id="FOOT4" href="#DOCF4">(4)</a></h5>
  2578. <p>The brace-list constructors of rank 2 and higher aren&rsquo;t supported on types of rt rank, because in the C++ grammar, a nested initializer list doesn&rsquo;t always define a rank unambiguously.</p>
  2579. <h5><a id="FOOT5" href="#DOCF5">(5)</a></h5>
  2580. <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>
  2581. <h5><a id="FOOT6" href="#DOCF6">(6)</a></h5>
  2582. <p>The brace-list constructors aren&rsquo;t rank extending, because giving the ravel is incompatible with rank extension. They are shape-strict —you must give every element.</p>
  2583. <h5><a id="FOOT7" href="#DOCF7">(7)</a></h5>
  2584. <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>]
  2585. 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>
  2586. <h5><a id="FOOT8" href="#DOCF8">(8)</a></h5>
  2587. <p>The multi-argument square bracket form <code>A[i₀, i₁, ...]</code> is supported under C++23 compilers (e.g. gcc ≥ 12 with <code>-std=c++2b</code>), with the same meaning as <code>A(i₀, i₁, ...)</code>. Under C++20 only a single-argument square bracket form <code>A[i₀]</code> is available.</p>
  2588. <h5><a id="FOOT9" href="#DOCF9">(9)</a></h5>
  2589. <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.
  2590. 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>
  2591. <h5><a id="FOOT10" href="#DOCF10">(10)</a></h5>
  2592. <p>Simplified; see the references in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1170r0.html">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1170r0.html</a>.</p>
  2593. <h5><a id="FOOT11" href="#DOCF11">(11)</a></h5>
  2594. <p>Diagram generated using Graphviz and <a href="https://www.flourish.org/cinclude2dot">https://www.flourish.org/cinclude2dot</a>.
  2595. </p>
  2596. <pre class="verbatim">cd ra &amp;&amp; cinclude2dot.pl --include . &gt; headers.dot
  2597. dot -Tpng headers.dot -Gdpi=100 &gt; headers.png
  2598. </pre>
  2599. </div>
  2600. <a href='js_licenses.html' rel='jslicense'><small>JavaScript license information</small></a>
  2601. </body>
  2602. </html>