123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- Created by GNU Texinfo 6.8, https://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <!-- ra:: (version 28, updated 2024 March 20)
- (c) Daniel Llorens 2005-2024
- Permission is granted to copy, distribute and/or modify this document
- under the terms of the GNU Free Documentation License, Version 1.3 or
- any later version published by the Free Software Foundation; with no
- Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. -->
- <title>ra:: — An array library for C++20</title>
- <meta name="description" content="ra:: — An array library for C++20">
- <meta name="keywords" content="ra:: — An array library for C++20">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <link href="#Top" rel="start" title="Top">
- <link href="#Indices" rel="index" title="Indices">
- <link href="#Overview" rel="next" title="Overview">
- <style type="text/css">
- <!--
- a.copiable-anchor {visibility: hidden; text-decoration: none; line-height: 0em}
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- span:hover a.copiable-anchor {visibility: visible}
- ul.no-bullet {list-style: none}
- -->
- </style>
- <script type='text/javascript'>
- MathJax = {
- options: {
- skipHtmlTags: {'[-]': ['pre']},
- ignoreHtmlClass: 'tex2jax_ignore',
- processHtmlClass: 'tex2jax_process'
- },
- };
- </script><script type="text/javascript" id="MathJax-script" async
- src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
- </script>
- </head>
- <body lang="en">
- <h1 class="settitle" align="center">ra:: — An array library for C++20</h1>
- <div class="top" id="Top">
- <div class="header">
- <p>
- Next: <a href="#Overview" accesskey="n" rel="next">Overview</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="ra_003a_003a"></span><h1 class="top"><code>ra::</code></h1>
- <p><code>ra::</code> (version 28, updated 2024 March 20)
- </p>
- <p>(c) Daniel Llorens 2005–2024
- </p>
- <div class="display">
- <pre class="display">Permission is granted to copy, distribute and/or modify this document
- under the terms of the GNU Free Documentation License, Version 1.3 or
- any later version published by the Free Software Foundation; with no
- Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
- </pre></div>
- <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. This manual is a work in progress.
- </p>
- <ul class="section-toc">
- <li><a href="#Overview" accesskey="1">Overview</a></li>
- <li><a href="#Usage" accesskey="2">Usage</a></li>
- <li><a href="#Extras" accesskey="3">Extras</a></li>
- <li><a href="#Hazards" accesskey="4">Hazards</a></li>
- <li><a href="#Internals" accesskey="5">Internals</a></li>
- <li><a href="#The-future" accesskey="6">The future</a></li>
- <li><a href="#Reference" accesskey="7">Reference</a></li>
- <li><a href="#Sources" accesskey="8">Sources</a></li>
- <li><a href="#Indices" accesskey="9">Indices</a></li>
- <li><a href="#Notes">Notes</a></li>
- </ul>
- <hr>
- <div class="chapter" id="Overview">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Overview-1"></span><h2 class="chapter">1 Overview</h2>
- <span id="index-length"></span>
- <span id="index-rank"></span>
- <span id="index-shape"></span>
- <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.
- </p>
- <p>Often we deal with multidimensional <em>expressions</em> where the elements aren’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.<a id="DOCF2" href="#FOOT2"><sup>2</sup></a>
- </p>
- <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’s very useful to be able to manipulate arrays as individual entities rather than as aggregates. Not only is
- </p>
- <pre class="verbatim">A = B+C;
- </pre>
- <p>much more compact and easier to read than
- </p>
- <pre class="verbatim">for (int i=0; i!=m; ++i)
- for (int j=0; j!=n; ++j)
- for (int k=0; k!=p; ++k)
- A(i, j, k) = B(i, j, k)+C(i, j, k);
- </pre>
- <p>but it’s also safer and less redundant. For example, the order of the loops may be something you don’t really care about.
- </p>
- <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.
- </p>
- <span id="index-Blitz_002b_002b"></span>
- <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>]
- , or ‘deforestation’ [<a href="#Sources">Wad90</a>]
- . 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>]
- . 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.
- </p>
- <ul class="section-toc">
- <li><a href="#Rank-polymorphism" accesskey="1">Rank polymorphism</a></li>
- <li><a href="#Drag-along-and-beating" accesskey="2">Drag along and beating</a></li>
- <li><a href="#Why-C_002b_002b" accesskey="3">Why C++</a></li>
- <li><a href="#Guidelines" accesskey="4">Guidelines</a></li>
- <li><a href="#Other-libraries" accesskey="5">Other array libraries</a></li>
- </ul>
- <hr>
- <div class="section" id="Rank-polymorphism">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Rank-polymorphism-1"></span><h3 class="section">1.1 Rank polymorphism</h3>
- <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.
- </p>
- <span id="index-cell"></span>
- <span id="index-frame"></span>
- <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>.
- </p>
- <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
- </p>
- <blockquote>
- <p>A: [A(0), A(1), ...]
- </p></blockquote>
- <p>and the items of A(i₀), etc. are in turn stored in the same way, so
- </p>
- <blockquote>
- <p>A: [A(0): [A(0, 0), A(0, 1) ...], ...]
- </p></blockquote>
- <p>and the same for the items of A(i₀, i₁), etc.
- </p>
- <blockquote>
- <p>A: [[A(0, 0): [A(0, 0, 0), A(0, 0, 1) ...], A(0, 1): [A(0, 1, 0), A(0, 1, 1) ...]], ...]
- </p></blockquote>
- <span id="index-order_002c-row_002dmajor"></span>
- <p>This way to lay out an array in memory is called <em>row-major order</em> or <em>C-order</em>, since it’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:
- </p>
- <span id="x_002dsteps"></span><blockquote>
- <p>A(i₀, i₁, ...) = (storage-of-A) [(((i₀n₁ + i₁)n₂ + i₂)n₃ + ...)+iᵣ₋₁] = (storage-of-A) [o + s₀i₀ + s₁i₁ + ...]
- </p></blockquote>
- <span id="index-step"></span>
- <span id="index-stride"></span>
- <p>where the numbers (s₀, s₁, ...) are called the <em>steps</em><a id="DOCF3" href="#FOOT3"><sup>3</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
- </p>
- <blockquote>
- <p>A ≡ ((storage-of-A), o, (s₀, s₁, ...))
- </p></blockquote>
- <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.
- </p>
- <p>A basic example is obtaining the i₀-th item of A:
- </p>
- <blockquote>
- <p>A(i₀) ≡ ((storage-of-A), o+s₀i₀, (s₁, ...))
- </p></blockquote>
- <p>Note that we can iterate over these items by simply bumping the pointer o+s₀i₀. This means that iterating over (k>0)-cells doesn’t cost any more than iterating over 0-cells (see <a href="#Cell-iteration">Cell iteration</a>).
- </p>
- <hr>
- </div>
- <div class="section" id="Drag-along-and-beating">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Drag-along-and-beating-1"></span><h3 class="section">1.2 Drag along and beating</h3>
- <p>These two fundamental array optimizations are described in [<a href="#Sources">Abr70</a>]
- .
- </p>
- <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’t an optimization in and of itself; it simply preserves the necessary information up to the point where the expression can be executed efficiently.
- </p>
- <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.
- </p>
- <hr>
- </div>
- <div class="section" id="Why-C_002b_002b">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Why-C_002b_002b-1"></span><h3 class="section">1.3 Why C++</h3>
- <p>Of course the main reason is that (this being a personal project) I’m more familiar with C++ than with other languages to which the following might apply.
- </p>
- <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.
- </p>
- <span id="index-APL"></span>
- <span id="index-J"></span>
- <p>The classic array languages, APL [<a href="#Sources">FI73</a>]
- and J [<a href="#Sources">Ric08</a>]
- , 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’s case [<a href="#Sources">num17</a>]
- this is both for reasons of flexibility (e.g. to obtain predictable memory layout and machine types) and of performance.
- </p>
- <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.
- </p>
- <p>In C++, a library is enough.
- </p>
- <hr>
- </div>
- <div class="section" id="Guidelines">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Guidelines-1"></span><h3 class="section">1.4 Guidelines</h3>
- <p><code>ra::</code> attempts to be general, consistent, and transparent.
- </p>
- <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.
- </p>
- <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’t create a higher rank iterator on a <code>ViewSmall</code>, even though you could do it on a <code>View</code>; this was a bug.
- </p>
- <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’t) is more important.
- </p>
- <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’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.
- </p>
- <p>That isn’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’ll be able to guarantee that an array is stored in compact columns, or that the real parts are interleaved with the imaginary parts.
- </p>
- <hr>
- </div>
- <div class="section" id="Other-libraries">
- <div class="header">
- <p>
- Previous: <a href="#Guidelines" accesskey="p" rel="prev">Guidelines</a>, Up: <a href="#Overview" accesskey="u" rel="up">Overview</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Other-array-libraries"></span><h3 class="section">1.5 Other array libraries</h3>
- <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.
- </p>
- <p>Since the C++ standard library doesn’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’t C++!
- </p>
- <ul class="section-toc">
- <li><a href="#Standard-C_002b_002b" accesskey="1">Standard C++</a></li>
- <li><a href="#Blitz_002b_002b" accesskey="2">Blitz++</a></li>
- <li><a href="#Other-C_002b_002b-libraries" accesskey="3">Other C++ libraries</a></li>
- <li><a href="#Other-languages" accesskey="4">Other languages</a></li>
- </ul>
- <div class="subsection" id="Standard-C_002b_002b">
- <h4 class="subsection">1.5.1 Standard C++</h4>
- <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’t know their own shape or rank at runtime, and are generally too limited.
- </p>
- <p>The C++ standard library also offers a number of contiguous storage containers that can be used as 1-arrays: <code><array></code>, <code><vector></code> and <code><valarray></code>. Neither supports higher ranks out of the box, but <code><valarray></code> offers array operations for 1-arrays. <code>ra::</code> makes use of <code><array></code> and <code><vector></code> internally and for storage.
- </p>
- <p><code>ra::</code> accepts built-in arrays and standard library types as array objects (see <a href="#Compatibility">Compatibility</a>).
- </p>
- </div>
- <div class="subsection" id="Blitz_002b_002b">
- <h4 class="subsection">1.5.2 Blitz++</h4>
- <span id="index-Blitz_002b_002b-1"></span>
- <p>Blitz++ [<a href="#Sources">bli17</a>]
- 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.
- </p>
- <p>However, the implementation had to fight the limitations of C++98, and it offered no general rank extension mechanism.
- </p>
- <p>One important difference between Blitz++ and <code>ra::</code> is that Blitz++’s arrays were reference counted. <code>ra::</code> doesn’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.
- </p>
- </div>
- <div class="subsection" id="Other-C_002b_002b-libraries">
- <h4 class="subsection">1.5.3 Other C++ libraries</h4>
- </div>
- <div class="subsection" id="Other-languages">
- <h4 class="subsection">1.5.4 Other languages</h4>
- <hr>
- </div>
- </div>
- </div>
- <div class="chapter" id="Usage">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Usage-1"></span><h2 class="chapter">2 Usage</h2>
- <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>.
- </p>
- <ul class="section-toc">
- <li><a href="#Using-the-library" accesskey="1">Using <code>ra::</code></a></li>
- <li><a href="#Containers-and-views" accesskey="2">Containers and views</a></li>
- <li><a href="#Array-operations" accesskey="3">Array operations</a></li>
- <li><a href="#Rank-extension" accesskey="4">Rank extension</a></li>
- <li><a href="#Cell-iteration" accesskey="5">Cell iteration</a></li>
- <li><a href="#Slicing" accesskey="6">Slicing</a></li>
- <li><a href="#Functions" accesskey="7">Functions</a></li>
- <li><a href="#The-rank-conjunction" accesskey="8">The rank conjunction</a></li>
- <li><a href="#Compatibility" accesskey="9">Compatibility</a></li>
- <li><a href="#Extension">Extension</a></li>
- <li><a href="#Error-handling">Error handling</a></li>
- </ul>
- <hr>
- <div class="section" id="Using-the-library">
- <div class="header">
- <p>
- Next: <a href="#Containers-and-views" accesskey="n" rel="next">Containers and views</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Using-ra_003a_003a"></span><h3 class="section">2.1 Using <code>ra::</code></h3>
- <p><code>ra::</code> is a header only library with no dependencies other than the standard library, so you just need to place the ‘<samp>ra/</samp>’ folder somewhere in your include path and add <code>#include "ra/ra.hh"</code> at the top of your sources.
- </p>
- <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.
- </p>
- <p>Here is a minimal program<a id="DOCF4" href="#FOOT4"><sup>4</sup></a>:
- </p>
- <div class="example">
- <pre class="verbatim">#include "ra/ra.hh"
- #include <iostream>
- int main()
- {
- ra::Big<char, 2> A({2, 5}, "helloworld");
- std::cout << ra::noshape << format_array(transpose<1, 0>(A), {.sep0="|"}) << std::endl;
- }
- </pre><pre class="example">-| h|w
- e|o
- l|r
- l|l
- d|d
- </pre></div>
- <p>The following headers are <em>not</em> included by default:
- </p><ul>
- <li> <code>"ra/dual.hh"</code>:
- A dual number type for simple uses of automatic differentiation.
- </li><li> <code>"ra/test.hh"</code>:
- Used by the test suite.
- </li></ul>
- <p>The header <code>"ra/base.hh"</code> can be used to configure <a href="#Error-handling">Error handling</a>. You don’t need to modify the header, but the configuration depends on including <code>"ra/base.hh"</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>.
- </p>
- <p>The following <code>#define</code>s affect the behavior of <code>ra::</code>.
- </p>
- <ul>
- <li> <code>RA_DO_CHECK</code> (default 1)
- <p>If 1, check shape agreement (e.g. <code>Big<int, 1> {2, 3} + Big<int, 1> {1, 2, 3}</code>) and random array accesses (e.g. <code>Small<int, 2> a = 0; int i = 10; a[i] = 0;</code>). See <a href="#Error-handling">Error handling</a>.
- </p>
- </li><li> <code>RA_DO_OPT_SMALLVECTOR</code> (default 0)
- <p>If 1, perform immediately certain operations on <code>ra::Small</code> objects, using small vector intrinsics. Currently this only works on <b>gcc</b> and doesn’t necessarily result in improved performance.
- </p>
- </li><li> <code>RA_DO_FMA</code> (default <code>FP_FAST_FMA</code> if defined, else 0)
- <p>If 1, use <code>fma</code> in certain array reductions such as <a href="#x_002ddot"><code>dot</code></a>, <a href="#x_002dgemm"><code>gemm</code></a>, etc.
- </p></li></ul>
- <span id="index-container"></span>
- <hr>
- </div>
- <div class="section" id="Containers-and-views">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Containers-and-views-1"></span><h3 class="section">2.2 Containers and views</h3>
- <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.
- </p>
- <span id="index-compile_002dtime"></span>
- <span id="index-ct"></span>
- <span id="index-runtime"></span>
- <span id="index-rt"></span>
- <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.
- </p>
- <p>For example:
- </p>
- <div class="example">
- <pre class="verbatim">{
- ra::Small<double, 2, 3> a(0.); // a ct size 2x3 array
- ra::Big<double, 2> b({2, 3}, 0.); // a rt size 2x3 array
- ra::Big<double> c({2, 3}, 0.); // a rt rank 2x3 array
- // a, b, c destroyed at end of scope
- }
- </pre></div>
- <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.
- </p>
- <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="DOCF5" href="#FOOT5"><sup>5</sup></a>
- </p>
- <div class="example">
- <pre class="verbatim">using ra::Small, ra::Big;
- Small<int, 2, 2> a = {{1, 2}, {3, 4}}; // explicit contents
- Big<int, 2> a1 = {{1, 2}, {3, 4}}; // explicit contents
- Small<int, 2, 2> a2 = {{1, 2}}; // error: bad shape
- Small<int, 2, 2> b = 7; // 7 is copied into b
- Small<int, 2, 2> c = a; // the contents of a are copied into c
- Big<int> d = a; // d takes the shape of a and a is copied into d
- Big<int> e = 0; // e is a 0-array with one element f()==0.
- </pre></div>
- <p>The second form takes two arguments, one giving the shape, the second the contents.
- </p>
- <span id="index-none"></span>
- <span id="index-uninitialized-container"></span>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 2> a({2, 3}, 1.); // a has shape [2 3], filled with 1.
- ra::Big<double> b({2, 3}, ra::none); // b has shape [2 3], default initialized
- ra::Big<double> c({2, 3}, a); // c has shape [2 3], a is copied into c
- </pre></div>
- <p>The last example may result in an error if the shape of <code>a</code> and (2, <!-- /@w -->3) don’t match. Here the shape of <code>1.</code> [which is ()] matches (2, <!-- /@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’s elements.
- </p>
- <p>The shape argument can have rank 0 only for rank 1 arrays.
- </p>
- <span id="index-none-1"></span>
- <span id="index-uninitialized-container-1"></span>
- <div class="example">
- <pre class="verbatim">ra::Big<int> c(3, 0); // ok {0, 0, 0}, same as ra::Big<int> c({3}, 0)
- ra::Big<int, 1> c(3, 0); // ok {0, 0, 0}, same as ra::Big<int, 1> c({3}, 0)
- ra::Big<int, 2> c({3}, 0); // error: bad length for shape
- ra::Big<int, 2> c(3, 0); // error: bad length for shape
- </pre></div>
- <p>When the content argument is a pointer or a 1D brace list, it’s handled especially, not for shape<a id="DOCF6" href="#FOOT6"><sup>6</sup></a>, but only as the (row-major) ravel of the content. The pointer constructor is unsafe —use at your own risk!<a id="DOCF7" href="#FOOT7"><sup>7</sup></a>
- </p>
- <span id="index-order_002c-column_002dmajor"></span>
- <div class="example">
- <pre class="verbatim">Small<int, 2, 2> aa = {1, 2, 3, 4}; // ravel of the content
- ra::Big<double, 2> a({2, 3}, {1, 2, 3, 4, 5, 6}); // same as a = {{1, 2, 3}, {4, 5, 6}}
- </pre></div>
- <div class="example">
- <pre class="verbatim">double bx[6] = {1, 2, 3, 4, 5, 6}
- ra::Big<double, 2> b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}
- double cx[4] = {1, 2, 3, 4}
- ra::Big<double, 2> c({3, 2}, cx); // *** WHO NOSE ***
- </pre></div>
- <div class="example">
- <pre class="verbatim">using lens = mp::int_list<2, 3>;
- using steps = mp::int_list<1, 2>;
- ra::SmallArray<double, lens, steps> a {{1, 2, 3}, {4, 5, 6}}; // stored column-major: 1 4 2 5 3 6
- </pre></div>
- <p>These produce compile time errors:
- </p>
- <div class="example">
- <pre class="verbatim">Big<int, 2> b = {1, 2, 3, 4}; // error: shape cannot be deduced from ravel
- Small<int, 2, 2> b = {1, 2, 3, 4 5}; // error: bad size
- Small<int, 2, 2> b = {1, 2, 3}; // error: bad size
- </pre></div>
- <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>
- <div class="example">
- <pre class="verbatim">ra::Big<char const *, 1> A({3}, "hello"); // error: try to convert char to char const *
- ra::Big<char const *, 1> A({3}, ra::scalar("hello")); // ok, "hello" is a single item
- cout << ra::noshape << format_array(A, {.sep0="|"}) << endl;
- </pre><pre class="example">-| hello|hello|hello
- </pre></div>
- <span id="index-view"></span>
- <p>A <em>view</em> is similar to a container in that it points to actual data in memory. However, the view doesn’t own that data and destroying the view won’t affect it. For example:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double> c({2, 3}, 0.); // a rt rank 2x3 array
- {
- auto c1 = c(1); // the second row of array c
- // c1 is destroyed here
- }
- cout << c(1, 1) << endl; // ok
- </pre></div>
- <p>The data accessed through a view is the data of the ‘root’ container, so modifying the former will be reflected in the latter.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double> c({2, 3}, 0.);
- auto c1 = c(1);
- c1(2) = 9.; // c(1, 2) = 9.
- </pre></div>
- <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:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double> c {{1, 2, 3}, {4, 5, 6}};
- auto ct = transpose<1, 0>(c); // {{1, 4}, {2, 5}, {3, 6}}
- auto cr = reverse(c, 0); // {{4, 5, 6}, {1, 2, 3}}
- </pre></div>
- <p>However, views can point to anywhere in memory and that memory doesn’t have to belong to an <code>ra::</code> container. For example:
- </p>
- <div class="example">
- <pre class="verbatim">int raw[6] = {1, 2, 3, 4, 5, 6};
- ra::View<int> v1({{2, 3}, {3, 1}}, raw); // view with shape [2, 3] steps [3, 1]
- ra::View<int> v2({2, 3}, raw); // same, default C (row-major) steps
- </pre></div>
- <p>Containers can be treated as views of the same kind (rt or ct) . If you declare a function
- </p>
- <div class="example">
- <pre class="verbatim">void f(ra::View<int, 3> & v);
- </pre></div>
- <p>you may pass it an object of type <code>ra::Big<int, 3></code>.
- </p>
- <hr>
- </div>
- <div class="section" id="Array-operations">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Array-operations-1"></span><h3 class="section">2.3 Array operations</h3>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Small<double, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
- double s = 0.;
- for_each([&s](auto && a) { s+=a; }, a);
- </pre><pre class="example">⇒ s = 21.
- </pre></div>
- <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’s assigned to a view, printed out, etc.
- </p>
- <div class="example">
- <pre class="verbatim">using T = ra::Small<double, 2, 2>;
- T a = {{1, 2}, {3, 4}};
- T b = {{10, 20}, {30, 40}};
- T c = map([](auto && a, auto && b) { return a+b; }, a, b); // (1)
- </pre><pre class="example">⇒ c = {{11, 22}, {33, 44}}
- </pre></div>
- <p>Expressions may take any number of arguments and be nested arbitrarily.
- </p>
- <div class="example">
- <pre class="verbatim">T d = 0;
- for_each([](auto && a, auto && b, auto && d) { d = a+b; },
- a, b, d); // same as (1)
- for_each([](auto && ab, auto && d) { d = ab; },
- map([](auto && a, auto && b) { return a+b; },
- a, b),
- d); // same as (1)
- </pre></div>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">T d = 0;
- map([](auto & d) -> decltype(auto) { return d; }, d) // just pass d along
- = map([](auto && a, auto && b) { return a+b; }, a, b); // same as (1)
- </pre></div>
- <p><code>ra::</code> defines many shortcuts for common array operations. You can of course just do:
- </p>
- <div class="example">
- <pre class="verbatim">T c = a+b; // same as (1)
- </pre></div>
- <hr>
- </div>
- <div class="section" id="Rank-extension">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Rank-extension-1"></span><h3 class="section">2.4 Rank extension</h3>
- <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.
- </p>
- <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
- </p>
- <blockquote>
- <p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ᵣ₋₁₎)</code>
- </p></blockquote>
- <p>where <code>r</code> is the rank of <code>R</code>.
- </p>
- <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
- </p>
- <blockquote>
- <p><code>(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S()</code>.
- </p></blockquote>
- <p>The two rules above are supported by all primitive array languages, e.g. Matlab [<a href="#Sources">Mat</a>]
- . But suppose that <code>S</code> has rank <code>s</code>, where <code>0<s<r</code>. Looking at the expressions above, it seems natural to define <code>R+S</code> by
- </p>
- <blockquote>
- <p><code>(R+S)(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ₛ₋₁₎)</code>.
- </p></blockquote>
- <p>That is, after we run out of indices in <code>S</code>, we simply repeat the elements. We have aligned the shapes so:
- </p>
- <blockquote>
- <pre class="verbatim">[n₀ n₁ ... n₍ₛ₋₁₎ ... n₍ᵣ₋₁₎]
- [n₀ n₁ ... n₍ₛ₋₁₎]
- </pre></blockquote>
- <span id="index-shape-agreement_002c-prefix"></span>
- <span id="index-shape-agreement_002c-suffix"></span>
- <span id="index-Numpy"></span>
- <p>This rank extension rule is used by the J language [<a href="#Sources">J S</a>]
- 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>]
- <a id="DOCF8" href="#FOOT8"><sup>8</sup></a>.
- </p>
- <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,
- </p>
- <div class="example">
- <pre class="verbatim">ra::Small<int, 3> x {3, 5, 9};
- ra::Small<int, 3, 2> a = x; // assign x(i) to each a(i, j)
- </pre><pre class="example">⇒ a = {{3, 3}, {5, 5}, {9, 9}}
- </pre></div>
- <div class="example">
- <pre class="verbatim">ra::Small<int, 3> x(0.);
- ra::Small<int, 3, 2> a = {{1, 2}, {3, 4}, {5, 6}};
- x += a; // sum the rows of a
- </pre><pre class="example">⇒ x = {3, 7, 11}
- </pre></div>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 3> a({5, 3, 3}, ra::_0);
- ra::Big<double, 1> b({5}, 0.);
- b += transpose<0, 1, 1>(a); // b(i) = ∑ⱼ a(i, j, j)
- </pre><pre class="example">⇒ b = {0, 3, 6, 9, 12}
- </pre></div>
- <span id="index-Numpy-1"></span>
- <span id="index-broadcasting_002c-singleton_002c-newaxis"></span>
- <p>An weakness of prefix agreement is that the axes you want to match aren’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’s implemented in Numpy [<a href="#Sources">num17</a>]
- , 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.
- </p>
- <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’t obvious why 1≠2 shouldn’t be. To avoid this problem, <code>ra::</code> supports broadcasting with undefined length axes (see <a href="#x_002dinsert"><code>insert</code></a>).
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 3> a({5, 3}, ra::_0);
- ra::Big<double, 1> b({3}, 0.);
- ra::Big<double, 3> c({1, 3}, ra::_0);
- // b(?, i) += a(j, i) → b(i) = ∑ⱼ a(j, i) (sum columns)
- b(ra::insert<1>) += a;
- c = a; // 1 ≠ 5, still an agreement error
- </pre></div>
- <p>Still another way to align array axes is provided by the <a href="#The-rank-conjunction">rank conjunction</a>.
- </p>
- <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.
- <a href="#x_002dtranspose">Transposing</a> the axes before extension is a possible workaround.
- </p>
- <hr>
- </div>
- <div class="section" id="Cell-iteration">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Cell-iteration-1"></span><h3 class="section">2.5 Cell iteration</h3>
- <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:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 3> a({5, 4, 3}, ra::_0);
- for_each([](auto && b) { /* b has shape (5 4 3) */ }, iter<3>(a));
- for_each([](auto && b) { /* b has shape (4 3) */ }, iter<2>(a));
- for_each([](auto && b) { /* b has shape (3) */ }, iter<1>(a));
- for_each([](auto && b) { /* b has shape () */ }, iter<0>(a)); // elements
- for_each([](auto && b) { /* b has shape () */ }, a); // same as iter<0>(a); default
- </pre></div>
- <p>One may specify the <em>frame</em> rank instead:
- </p>
- <div class="example">
- <pre class="verbatim">for_each([](auto && b) { /* b has shape () */ }, iter<-3>(a)); // same as iter<0>(a)
- for_each([](auto && b) { /* b has shape (3) */ }, iter<-2>(a)); // same as iter<1>(a)
- for_each([](auto && b) { /* b has shape (4 3) */ }, iter<-1>(a)); // same as iter<2>(a)
- </pre></div>
- <p>In this way it is possible to match shapes in various ways. Compare
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
- ra::Big<double, 1> b = {10, 20};
- ra::Big<double, 2> c = a * b; // multiply (each item of a) by (each item of b)
- </pre><pre class="example">⇒ a = {{10, 20, 30}, {80, 100, 120}}
- </pre></div>
- <p>with
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
- ra::Big<double, 1> b = {10, 20, 30};
- ra::Big<double, 2> c({2, 3}, 0.);
- iter<1>(c) = iter<1>(a) * iter<1>(b); // multiply (each item of a) by (b)
- </pre><pre class="example">⇒ a = {{10, 40, 90}, {40, 100, 180}}
- </pre></div>
- <p>Note that in this case we cannot construct <code>c</code> directly from <code>iter<1>(a) * iter<1>(b)</code>, since the constructor for <code>ra::Big</code> matches its argument using (the equivalent of) <code>iter<0>(*this)</code>. See <a href="#x_002diter"><code>iter</code></a> for more examples.
- </p>
- <p>Cell iteration is appropriate when the operations take naturally operands of rank > 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>).
- </p>
- <p>FIXME More examples.
- </p>
- <hr>
- </div>
- <div class="section" id="Slicing">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Slicing-1"></span><h3 class="section">2.6 Slicing</h3>
- <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.
- </p>
- <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="DOCF9" href="#FOOT9"><sup>9</sup></a>. An array extension is immediately available through <code>map</code>. For example:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 1> a = {1., 2., 3., 4.};
- ra::Big<int, 1> i = {1, 3};
- map(a, i) = 77.;
- </pre><pre class="example">⇒ a = {1., 77., 3, 77.}
- </pre></div>
- <p>Just as with any use of <code>map</code>, array arguments are subject to the prefix agreement rule.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 2> a({2, 2}, {1., 2., 3., 4.});
- ra::Big<int, 1> i = {1, 0};
- ra::Big<double, 1> b = map(a, i, 0);
- </pre><pre class="example">⇒ b = {3., 1.} // {a(1, 0), a(0, 0)}
- </pre></div>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> j = {0, 1};
- b = map(a, i, j);
- </pre><pre class="example">⇒ b = {3., 2.} // {a(1, 0), a(0, 1)}
- </pre></div>
- <p>The latter is a form of sparse subscripting.
- </p>
- <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:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double, 2> A {{1., 2.}, {3., 4.}};
- ra::Big<int, 1> i = {1, 0};
- ra::Big<int, 1> j = {0, 1};
- // {{A(i₀, j₀), A(i₀, j₁)}, {A(i₁, j₀), A(i₁, j₁)}}
- ra::Big<double, 2> b = A(i, j);
- </pre><pre class="example">⇒ b = {{3., 4.}, {1., 2.}}
- </pre></div>
- <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>.
- </p>
- <span id="index-elision_002c-index"></span>
- <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):
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 3> a({2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8});
- auto a0 = a(0); // same as a(0, ra::all, ra::all)
- auto a10 = a(1, 0); // same as a(1, 0, ra::all)
- </pre><pre class="example">⇒ a0 = {{1, 2}, {3, 4}}
- ⇒ a10 = {5, 6}
- </pre></div>
- <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>).
- </p>
- <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>.
- </p>
- <p><code>ra::</code> isn’t smart enough to know when an arbitrary expression might be a linear range, so the following special objects are provided:
- </p>
- <span id="x_002diota"></span><dl class="def">
- <dt id="index-iota"><span class="category">Special object<!-- /@w -->: </span><span><strong>iota</strong> <em>count [start:0 [step:1]]</em><a href='#index-iota' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create a linear range <code>start, start+step, ... start+step*(count-1)</code>.
- </p>
- <p>This can used anywhere an array expression is expected.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> a = ra::iota(4, 3 -2);
- </pre><pre class="example">⇒ a = {3, 1, -1, -3}
- </pre></div>
- <p>Here, <code>b</code> and <code>c</code> are <code>View</code>s (see <a href="#Containers-and-views">Containers and views</a>).
- </p><div class="example">
- <pre class="verbatim">ra::Big<int, 1> a = {1, 2, 3, 4, 5, 6};
- auto b = a(iota(3));
- auto c = a(iota(3, 3));
- </pre><pre class="example">⇒ a = {1, 2, 3}
- ⇒ a = {4, 5, 6}
- </pre></div>
- <span id="index-TensorIndex"></span>
- <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<n>()</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++’s <code>TensorIndex</code>.
- </p>
- <p><code>ra::</code> offers the shortcut <code>ra::_0</code> for <code>ra::iota<0>()</code>, etc.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> v = {1, 2, 3};
- cout << (v - ra::_0) << endl; // { 1-0, 2-1, 3-2 }
- // cout << (ra::_0) << endl; // error: undefined length
- // cout << (v - ra::_1) << endl; // error: undefined length on axis 1
- ra::Big<int, 2> a({3, 2}, 0);
- cout << (a + ra::_0 - ra::_1) << endl; // {{0, -1, -2}, {1, 0, -1}, {2, 1, 0}}
- </pre></div>
- <p>When undefined length <code>iota()</code> is used as a subscript by itself, the result isn’t a <code>View</code>. This allows <code>view(iota())</code> to match with expressions of different lengths, as in the following example.
- </p><div class="example">
- <pre class="verbatim">ra::Big<int, 1> a = {1, 2, 3, 4, 5, 6};
- ra::Big<int, 1> b = {1, 2, 3};
- cout << (b + a(iota())) << endl; // a(iota()) is not a View
- </pre><pre class="example">-| 3
- 2 4 6
- </pre></div>
- <p>Note the difference between
- </p><ul>
- <li> <code>ra::iota<3>()</code> —
- an expression of rank 4 and undefined length, representing a linear sequence over the tensor index of axis 3
- </li><li> <code>ra::iota(3)</code> ≡ <code>ra::iota<0>(3)</code> —
- an expression of rank 1, representing the sequence <code>0, 1, 2</code>.
- </li></ul>
- </dd></dl>
- <span id="x_002dall"></span><dl class="def">
- <dt id="index-all"><span class="category">Special object<!-- /@w -->: </span><span><strong>all</strong><a href='#index-all' class='copiable-anchor'> ¶</a></span></dt>
- <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>.
- </p>
- <p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
- auto b = a(ra::all, ra::all); // (1) a view of the whole of a
- auto c = a(iota(3), iota(2)); // same as (1)
- auto d = a(iota(3), ra::all); // same as (1)
- auto e = a(ra:all, iota(2)); // same as (1)
- auto f = a(0, ra::all); // first row of a
- auto g = a(ra::all, 1); // second column of a
- auto g = a(ra::all, ra::dots<0>, 1); // same
- </pre></div>
- <p><code>all</code> is a special case (<code>dots<1></code>) of the more general object <code>dots</code>.
- </p>
- </dd></dl>
- <span id="x_002ddots"></span><dl class="def">
- <dt id="index-dots_003cn_003e"><span class="category">Special object<!-- /@w -->: </span><span><strong>dots<n></strong><a href='#index-dots_003cn_003e' class='copiable-anchor'> ¶</a></span></dt>
- <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.
- </p>
- <p>If <var>n</var> is defaulted (<code>dots<></code>), all available places will be used; this can only be done once in a given subscript list.
- </p>
- <p>This object cannot stand alone as an array expression. All the examples below result in <code>View</code> objects:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 3> a({3, 2, 4}, ...);
- auto h = a(); // all of a
- auto b = a(ra::all, ra::all, ra::all); // (1) all of a
- auto c = a(ra::dots<3>); // same as (1)
- auto d = a(ra::all, ra::dots<2>); // same as (1)
- auto e = a(ra::dots<2>, ra::all); // same as (1)
- auto f = a(ra::dots<>); // same as (1)
- auto j0 = a(0, ra::dots<2>); // first page of a
- auto j1 = a(0); // same
- auto j2 = a(0, ra::dots<>); // same
- auto k0 = a(ra::all, 0); // first row of a
- auto k1 = a(ra::all, 0, ra::all); // same
- auto k2 = a(ra::all, 0, ra::dots<>); // same
- auto k3 = a(ra::dots<>, 0, ra::all); // same
- // auto k = a(ra::dots<>, 0, ra::dots<>); // error
- auto l0 = a(ra::all, ra::all, 0); // first column of a
- auto l1 = a(ra::dots<2>, 0); // same
- auto l2 = a(ra::dots<>, 0); // same
- </pre></div>
- <p>This is useful when writing rank-generic code, see <code>examples/maxwell.cc</code> in the distribution for an example.
- </p>
- </dd></dl>
- <p>The following special objects aren’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.
- </p>
- <span id="index-len"></span>
- <span id="index-end_002c-Octave_002fMatlab"></span>
- <span id="x_002dlen"></span><dl class="def">
- <dt id="index-len-1"><span class="category">Special object<!-- /@w -->: </span><span><strong>len</strong><a href='#index-len-1' class='copiable-anchor'> ¶</a></span></dt>
- <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.
- </p>
- <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>.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> a({10, 10}, 100 + ra::_0 - ra::_1);
- auto a0 = a(ra::len-1); // last row of a; ra::len is a.len(0)
- auto a1 = a(ra::all, ra::len-1); // last column a; ra::len is a.len(1)
- 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)
- auto a3 = a(ra::all, ra::iota(2, ra::len-2)); // last two columns of a
- auto a4 = a(ra::iota(ra::len/2, 1, 2)); // odd rows of a
- a(ra::len - std::array {1, 3, 4}) = 0; // set to 0 the 1st, 3rd and 4th rows of a, counting from the end
- </pre></div>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 3> b({2, 3, 4}, ...);
- auto b0 = b(ra::dots<2>, ra::len-1); // ra::len is a.len(2)
- auto b1 = b(ra::insert<1>, ra::len-1); // ra::len is a.len(0)
- </pre></div>
- </dd></dl>
- <span id="index-insert"></span>
- <span id="x_002dinsert"></span><dl class="def">
- <dt id="index-insert_003cn_003e"><span class="category">Special object<!-- /@w -->: </span><span><strong>insert<n></strong><a href='#index-insert_003cn_003e' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Inserts <code>n</code> new axes at the subscript position. <code>n</code> must not be negative.
- </p>
- <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:
- </p>
- <div class="example">
- <pre class="verbatim">auto h = a(insert<0>); // same as (1)
- auto k = a(insert<1>); // shape [undefined, 3, 2]
- </pre></div>
- <span id="index-broadcasting_002c-singleton_002c-Numpy"></span>
- <p><code>insert<n></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’t special, so broadcasting requires the use of <code>insert</code>. For example: </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> x = {1, 10};
- // match shapes [2, U, U] with [U, 3, 2] to produce [2, 3, 2]
- cout << x(ra::all, ra::insert<2>) * a(insert<1>) << endl;
- </pre><pre class="example">-| 2 3 2
- 1 2
- 3 4
- 5 6
- 10 20
- 30 40
- 50 60
- </pre></div>
- </dd></dl>
- <p>We were speaking earlier of the outer product of the subscripts with operator <code>A</code>. Here’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.
- </p><div class="example">
- <pre class="verbatim">cout << (A(ra::dots<A.rank()>, ra::insert<B.rank()>) * B(ra::insert<A.rank()>, ra::dots<B.rank()>)) << endl;
- cout << (A(ra::dots<>, ra::insert<B.rank()>) * B(ra::insert<A.rank()>, ra::dots<>)) << endl; // default dots<>
- cout << (A * B(ra::insert<A.rank()>)) << endl; // index elision + prefix matching
- </pre></div>
- <ul class="section-toc">
- <li><a href="#Subscripting-and-rank_002d0-views" accesskey="1">Subscripting and rank-0 views</a></li>
- </ul>
- <div class="subsection" id="Subscripting-and-rank_002d0-views">
- <h4 class="subsection">2.6.1 Subscripting and rank-0 views</h4>
- <span id="index-view_002c-rank-0"></span>
- <span id="index-rank_002c-runtime"></span>
- <span id="index-rank_002c-compile_002dtime"></span>
- <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’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.
- </p>
- <div class="example">
- <pre class="verbatim">using T = std::complex<double>;
- int f(T &);
- Big<T, 2> a({2, 3}, 0); // ct rank
- Big<T> b({2, 3}, 0); // rt rank
- cout << a(0, 0).real_part() << endl; // ok, a(0, 0) returns complex &
- // cout << b(0, 0).real_part() << endl; // error, View<T> has no member real_part
- cout << ((T &)(b(0, 0))).real_part() << endl; // ok, manual conversion to T &
- cout << f(b(0, 0)) << endl; // ok, automatic conversion from View<T> to T &
- // cout << f(a(0)) << endl; // compile time error, conversion failed since ct rank of a(0) is not 0
- // cout << f(b(0)) << endl; // runtime error, conversion failed since rt rank of b(0) is not 0
- </pre></div>
- <hr>
- </div>
- </div>
- <div class="section" id="Functions">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Functions-1"></span><h3 class="section">2.7 Functions</h3>
- <p>You don’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.
- </p>
- <span id="x_002dscalar_002dops"></span><ul class="section-toc">
- <li><a href="#Standard-scalar-operations" accesskey="1">Standard scalar operations</a></li>
- <li><a href="#Conditional-operations" accesskey="2">Conditional operations</a></li>
- <li><a href="#Special-operations" accesskey="3">Special operations</a></li>
- <li><a href="#Elementwise-reductions" accesskey="4">Elementwise reductions</a></li>
- <li><a href="#Special-reductions" accesskey="5">Special reductions</a></li>
- <li><a href="#Shortcut-reductions" accesskey="6">Shortcut reductions</a></li>
- </ul>
- <div class="subsection" id="Standard-scalar-operations">
- <h4 class="subsection">2.7.1 Standard scalar operations</h4>
- <p><code>ra::</code> defines array extensions for <code>+</code>, <code>-</code> (both unary and binary), <code>*</code>, <code>/</code>, <code>!</code>, <code>&&</code>, <code>||</code><a id="DOCF10" href="#FOOT10"><sup>10</sup></a>, <code>></code>, <code><</code>, <code>>=</code>, <code><=</code>, <code><=></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>, <code>lerp</code>, and <code>fma</code>.
- 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>.
- </p>
- <p>For example:
- </p><div class="example">
- <pre class="verbatim">cout << exp(ra::Small<double, 3> {4, 5, 6}) << endl;
- </pre><pre class="example"> -| 54.5982 148.413 403.429
- </pre></div>
- </div>
- <div class="subsection" id="Conditional-operations">
- <h4 class="subsection">2.7.2 Conditional operations</h4>
- <p><a href="#x_002dmap"><code>map</code></a> evaluates all of its arguments before passing them along to its operator. This isn’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.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double> x ...
- ra::Big<double> y = where(x>0, expensive_expr_1(x), expensive_expr_2(x));
- </pre></div>
- <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
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<double> y = map([](auto && w, auto && t, auto && f) -> decltype(auto) { return w ? t : f; }
- x>0, expensive_expr_1(x), expensive_function_2(x));
- </pre></div>
- <p>If the expressions have side effects, then <code>map</code> won’t even give the right result.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> o = {};
- ra::Big<int, 1> e = {};
- ra::Big<int, 1> n = {1, 2, 7, 9, 12};
- ply(where(odd(n), map([&o](auto && x) { o.push_back(x); }, n), map([&e](auto && x) { e.push_back(x); }, n)));
- cout << "o: " << ra::noshape << o << ", e: " << ra::noshape << e << endl;
- </pre><pre class="example">-| o: 1 7 9, e: 2 12
- </pre></div>
- <p>FIXME Artificial example.
- FIXME Do we want to expose ply(); this is the only example in the manual that uses it.
- </p>
- <p>When the choice is between more than two expressions, there’s <a href="#x_002dpick"><code>pick</code></a>, which operates similarly, but accepts an integer instead of a boolean selector.
- </p>
- </div>
- <div class="subsection" id="Special-operations">
- <h4 class="subsection">2.7.3 Special operations</h4>
- <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.
- </p>
- <p>FIXME
- </p>
- </div>
- <div class="subsection" id="Elementwise-reductions">
- <h4 class="subsection">2.7.4 Elementwise reductions</h4>
- <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.
- </p>
- <p>You can define reductions the same way <code>ra::</code> does:
- </p>
- <div class="example">
- <pre class="verbatim">template <class A>
- inline auto op_reduce(A && a)
- {
- T c = op_default;
- for_each([&c](auto && a) { c = op(c, a); }, a);
- return c;
- }
- </pre></div>
- <span id="x_002daxis_002dreductions"></span><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:
- </p>
- <div class="example">
- <pre class="verbatim"> ra::Big<double, 2> a({m, n}, ...);
- ra::Big<double, 1> sum_rows({n}, 0.);
- iter<1>(sum_rows) += iter<1>(a);
- // for_each(ra::wrank<1, 1>([](auto & c, auto && a) { c += a; }), sum_rows, a) // alternative
- // sum_rows += transpose<1, 0>(a); // another
- ra::Big<double, 1> sum_cols({m}, 0.);
- sum_cols += a;
- </pre></div>
- <p>FIXME example with assignment op
- </p>
- <p>A few common operations of this type are already packaged in <code>ra::</code>.
- </p>
- </div>
- <div class="subsection" id="Special-reductions">
- <h4 class="subsection">2.7.5 Special reductions</h4>
- <p><code>ra::</code> defines the following special reductions.
- </p>
- <p>FIXME
- </p>
- </div>
- <div class="subsection" id="Shortcut-reductions">
- <h4 class="subsection">2.7.6 Shortcut reductions</h4>
- <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>&&</code> and <code>||</code>, which <code>ra::</code> defines as <code>every</code> and <code>any</code>.
- </p>
- <p>FIXME
- </p>
- <p>These operations are defined on top of another function <code>early</code>.
- </p>
- <p>FIXME early
- </p>
- <p>The following is often useful.
- </p>
- <p>FIXME lexicographical compare etc.
- </p>
- <hr>
- </div>
- </div>
- <div class="section" id="The-rank-conjunction">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="The-rank-conjunction-1"></span><h3 class="section">2.8 The rank conjunction</h3>
- <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<cell rank></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.
- </p>
- <div class="example">
- <pre class="verbatim">for_each(my_sort, iter<1>(A)); // (in ra::) my_sort is a regular function, cell rank must be given
- for_each(my_sort, iter<0>(A)); // (in ra::) error, bad cell rank
- </pre></div>
- <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<cell rank></code> is not needed.
- </p>
- <div class="example">
- <pre class="verbatim">for_each(sort_rows, A); // (not in ra::) will iterate over 1-cells of A, sort_rows knows
- </pre></div>
- <p><code>ra::</code> doesn’t have ‘verb ranks’ yet. In practice one can think of <code>ra::</code>’s operations as having a verb rank of 0. However, <code>ra::</code> supports a limited form of J’s <em>rank conjunction</em> with the function <a href="#x_002dwrank"><code>wrank</code></a>.
- </p>
- <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.
- </p>
- <p>A standard example is ‘outer product’.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> a = {1, 2, 3};
- ra::Big<int, 1> b = {40, 50};
- ra::Big<int, 2> axb = map(ra::wrank<0, 1>([](auto && a, auto && b) { return a*b; }),
- a, b)
- </pre><pre class="example">⇒ axb = {{40, 80, 120}, {50, 100, 150}}
- </pre></div>
- <p>It works like this. The verb <code>ra::wrank<0, 1>([](auto && a, auto && 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:
- </p>
- <div class="example">
- <pre class="verbatim">a: 3 |
- b: | 2
- </pre></div>
- <p>Now the frames are rank-extended through prefix agreement.
- </p>
- <div class="example">
- <pre class="verbatim">a: 3 |
- b: 3 | 2
- </pre></div>
- <p>Now we need to perform the operation on each cell. The verb <code>[](auto && a, auto && 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)).
- </p>
- <div class="example">
- <pre class="verbatim">a: 3 | 2
- b: 3 | 2
- </pre></div>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 1> a = {1, 2, 3};
- ra::Big<int, 1> b = {40, 50};
- ra::Big<int, 2> axb = from([](auto && a, auto && b) { return a*b; }), a, b)
- </pre><pre class="example">⇒ axb = {{40, 80, 120}, {50, 100, 150}}
- </pre></div>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">C: m | | n
- A: m | p |
- B: | p | n
- </pre></div>
- <p>First we’ll align the first axes of C and A using the cell ranks (1, 1, 2). The cell shapes are:
- </p>
- <div class="example">
- <pre class="verbatim">C: m | n
- A: m | p
- B: | p n
- </pre></div>
- <p>Then we’ll use the ranks (1, 0, 1) on the cells:
- </p>
- <div class="example">
- <pre class="verbatim">C: m | | n
- A: m | p |
- B: | p | n
- </pre></div>
- <p>The final operation is a standard operation on arrays of scalars. In actual <code>ra::</code> syntax:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big A({3, 2}, {1, 2, 3, 4, 5, 6});
- ra::Big B({2, 3}, {7, 8, 9, 10, 11, 12});
- ra::Big C({3, 3}, 0.);
- for_each(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto && c, auto && a, auto && b) { c += a*b; })), C, A, B);
- </pre><pre class="example">⇒ C = {{27, 30, 33}, {61, 68, 75}, {95, 106, 117}}
- </pre></div>
- <p>Note that <code>wrank</code> cannot be used to transpose axes in general.
- </p>
- <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.
- </p>
- <hr>
- </div>
- <div class="section" id="Compatibility">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Compatibility-1"></span><h3 class="section">2.9 Compatibility</h3>
- <ul class="section-toc">
- <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>
- <li><a href="#Using-ra_003a_003a-types-with-the-STL" accesskey="2">Using <code>ra::</code> types with the STL</a></li>
- <li><a href="#Using-ra_003a_003a-types-with-other-libraries" accesskey="3">Using <code>ra::</code> types with other libraries</a></li>
- </ul>
- <div class="subsection" id="Using-other-C-and-C_002b_002b-types-with-ra_003a_003a">
- <h4 class="subsection">2.9.1 Using other C and C++ types with <code>ra::</code></h4>
- <span id="index-foreign-type"></span>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">std::vector<int> x = {1, 2, 3};
- ra::Small<int, 3> y = {6, 5, 4};
- cout << (x-y) << endl;
- </pre><pre class="example">-| -5 -3 -1
- </pre></div>
- <span id="index-start"></span>
- <p>Foreign types can be brought into <code>ra::</code> explicitly with the function <a href="#x_002dstart"><code>start</code></a>.
- </p>
- <div class="example">
- <pre class="verbatim">std::vector<int> x = {1, 2, 3};
- // cout << sum(x) << endl; // error, sum not found
- cout << sum(ra::start(x)) << endl;
- cout << ra::sum(x) << endl;
- </pre><pre class="example">-| 6
- </pre></div>
- <p>The following types are accepted as foreign types:
- </p>
- <span id="index-built_002din-array"></span>
- <ul>
- <li> Built-in arrays
- produce an expression of positive rank and ct size.
- </li><li> <code>std::array</code>
- produces an expression of rank 1 and ct size.
- </li><li> Other types conforming to <code>std::ranges::bidirectional_range</code>, such as <code>std::vector</code>, <code>std::string</code>, or <code>std::map</code>
- produce an expression of rank 1 and rt size.
- </li></ul>
- <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.
- </p>
- <p>Compare:
- </p>
- <div class="example">
- <pre class="verbatim">int p[] = {1, 2, 3};
- int * z = p;
- ra::Big<int, 1> q {1, 2, 3};
- q += p; // ok, q is ra::, p is foreign object with shape info
- ra::start(p) += q; // can't redefine operator+=(int[]), foreign needs ra::start()
- // z += q; // error: raw pointer needs ra::ptr()
- ra::ptr(z) += p; // ok, shape is determined by foreign object p
- </pre></div>
- <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<T></code>. You can add your own types as scalar types with the following declaration:
- </p>
- <blockquote>
- <pre class="verbatim">template <> constexpr bool ra::is_scalar_def<MYTYPE> = true;
- </pre></blockquote>
- <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>.
- </p>
- <hr>
- </div>
- <div class="subsection" id="Using-ra_003a_003a-types-with-the-STL">
- <div class="header">
- <p>
- Up: <a href="#Compatibility" accesskey="u" rel="up">Compatibility</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Using-ra_003a_003a-types-with-the-STL-1"></span><h4 class="subsection">2.9.2 Using <code>ra::</code> types with the STL</h4>
- <p>STL compatible input/output iterators and ranges can be obtained from general <code>ra::</code> expressions through the functions <a href="#x_002dbegin"><code>begin</code></a>, <a href="#x_002dend"><code>end</code></a>, and <a href="#x_002drange"><code>range</code></a>. These objects traverse the elements of the expression (0-cells) in row major order.<a id="DOCF11" href="#FOOT11"><sup>11</sup></a>
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> A = {{3, 0, 0}, {4, 5, 6}, {0, 5, 6}};
- std::accumulate(ra::begin(A), ra::end(A), 0); // or just sum(A)
- </pre><pre class="example">⇒ 29
- </pre></div>
- <p>For temporary expressions that are stated once, the ranges interface is more appropriate.
- </p>
- <div class="example">
- <pre class="verbatim">cout << std::ranges::count(range(A>3), true) << endl; // or sum(cast<int>(A>3))
- </pre><pre class="example">⇒ 5
- </pre></div>
- <p>One can create ranges from higher rank <code>ra::</code> iterators and thus use STL algorithms over cells of any rank, but note that in the current version of <code>ra::</code>, <a href="#x_002diter"><code>iter<k>()</code></a> only works on views, not on general expressions.
- </p>
- <div class="example">
- <pre class="verbatim">// count rows with 0s in them
- cout << std::ranges::count_if(range(iter<1>(A)), [](auto const & x) { return any(x==0); }) << endl;
- </pre><pre class="example">⇒ 2
- </pre></div>
- <p>For <a href="#Containers-and-views">containers</a>, <code>ra::</code> <code>begin</code>/<code>end</code>/<code>range</code> provide random access iterators and ranges, which is handy for functions such as <code>std::sort</code>. These could be provided for general expressions, but they wouldn’t be efficient for ranks above 1, and I haven’t implemented them. The container <code>std::random_access_iterator</code>s that are provided are in fact raw pointers.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int> x {3, 2, 1}; // x is a Container
- auto y = x(); // y is a View on x
- // std::sort(ra::begin(y), ra::end(y)); // error: begin(y) is not std::random_access_iterator
- std::sort(ra::begin(x), ra::end(x)); // ok, we know x is stored in row-major order
- </pre><pre class="example">⇒ x = {1, 2, 3}
- </pre></div>
- <span id="index-other-libraries_002c-interfacing-with"></span>
- </div>
- <div class="subsection" id="Using-ra_003a_003a-types-with-other-libraries">
- <h4 class="subsection">2.9.3 Using <code>ra::</code> types with other libraries</h4>
- <p>When you have to pass arrays back and forth between your program and an external library, or perhaps 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 piece of memory.
- </p>
- <ul class="section-toc">
- <li><a href="#The-good-array-citizen" accesskey="1">The good array citizen</a></li>
- <li><a href="#The-bad-array-citizen" accesskey="2">The bad array citizen</a></li>
- </ul>
- <div class="subsubsection" id="The-good-array-citizen">
- <h4 class="subsubsection">2.9.3.1 The good array citizen</h4>
- <span id="index-BLIS"></span>
- <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’t have to worry about special cases. Say <a href="https://github.com/flame/blis">BLIS</a>:
- </p>
- <blockquote>
- <pre class="verbatim">#include <blis.h>
- ra::Big<double, 2> A({M, K}, ...);
- ra::Big<double, 2> B({K, N}, ...);
- ra::Big<double, 2> C({M, N}, ...);
- double alpha = ...;
- double beta = ...;
- // C := βC + αAB
- bli_dgemm(BLIS_NO_TRANSPOSE, BLIS_NO_TRANSPOSE, M, N, K, &alpha,
- A.data(), A.step(0), A.step(1),
- B.data(), B.step(0), B.step(1),
- &beta, C.data(), C.step(0), C.step(1));
- </pre></blockquote>
- <span id="index-FFTW"></span>
- <p>Another good array citizen, <a href="http://fftw.org">FFTW</a> handles arbitrary rank:
- </p>
- <blockquote>
- <pre class="verbatim">#include <fftw3.h>
- ...
- using complex = std::complex<double>;
- static_assert(sizeof(complex)==sizeof(fftw_complex));
- // forward DFT over the last r axes of a -> b
- void fftw(int r, ra::View<complex> const a, ra::View<complex> b)
- {
- int const rank = a.rank();
- assert(r>0 && r<=rank);
- assert(every(ra::start(shape(a))==shape(b)));
- fftw_iodim dims[r];
- fftw_iodim howmany_dims[rank-r];
- for (int i=0; i!=rank; ++i) {
- if (i>=rank-r) {
- dims[i-rank+r].n = a.len(i);
- dims[i-rank+r].is = a.step(i);
- dims[i-rank+r].os = b.step(i);
- } else {
- howmany_dims[i].n = a.len(i);
- howmany_dims[i].is = a.step(i);
- howmany_dims[i].os = b.step(i);
- }
- }
- fftw_plan p;
- p = fftw_plan_guru_dft(r, dims, rank-r, howmany_dims,
- (fftw_complex *)(a.data()), (fftw_complex *)(b.data()),
- FFTW_FORWARD, FFTW_ESTIMATE);
- fftw_execute(p);
- fftw_destroy_plan(p);
- }
- </pre></blockquote>
- <span id="index-Guile"></span>
- <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:
- </p>
- <blockquote>
- <pre class="verbatim"> SCM a; // say a is #nf64(...)
- ...
- scm_t_array_handle h;
- scm_array_get_handle(a, &h);
- scm_t_array_dim const * dims = scm_array_handle_dims(&h);
- View<double> v(map([](int i) { return ra::Dim { dims[i].ubnd-dims[i].lbnd+1, dims[i].inc }; },
- ra::iota(scm_array_handle_rank(&h))),
- scm_array_handle_f64_writable_elements(&h));
- ...
- scm_array_handle_release(&h);
- </pre></blockquote>
- <span id="index-Numpy-2"></span>
- <span id="index-Python"></span>
- <p>Numpy’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’s <code>scm_t_array_handle</code> in the example above.
- </p>
- <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>).
- </p>
- </div>
- <div class="subsubsection" id="The-bad-array-citizen">
- <h4 class="subsubsection">2.9.3.2 The bad array citizen</h4>
- <p>Unfortunately there are many libraries that don’t accept arbitrary array parameters, or that do strange things with particular values of lengths and/or steps.
- </p>
- <p>The most common case is that a library doesn’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.
- </p>
- <span id="index-BLAS"></span>
- <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>’ <code>cblas_dgemm</code> has this prototype:
- </p>
- <blockquote>
- <pre class="verbatim">cblas_dgemm(order, transA, transB, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
- </pre></blockquote>
- <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’t understand <code>lda</code> as a arbitrary step, but rather as the dimension of a larger array that you’re slicing <code>A</code> from, and some implementations will mishandle negative or zero <code>lda</code>.
- </p>
- <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.
- </p>
- <span id="index-OpenGL"></span>
- <p>OpenGL is another library that requires <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml">contortions:</a>. Quote:
- </p>
- <blockquote>
- <pre class="verbatim">void glVertexAttribPointer(GLuint index,
- GLint size,
- GLenum type,
- GLboolean normalized,
- GLsizei step,
- const GLvoid * pointer);
- </pre>
- <p>[...]
- </p>
- <p><em>step</em>
- </p>
- <blockquote>
- <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.
- </p></blockquote>
- </blockquote>
- <p>It isn’t clear whether negative steps are legal, either. So just as with CBLAS, passing arbitrary array views may require copies.
- </p>
- <hr>
- </div>
- </div>
- </div>
- <div class="section" id="Extension">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Extension-1"></span><h3 class="section">2.10 Extension</h3>
- <ul class="section-toc">
- <li><a href="#New-scalar-types" accesskey="1">New scalar types</a></li>
- <li><a href="#New-array-operations" accesskey="2">New array operations</a></li>
- </ul>
- <div class="subsection" id="New-scalar-types">
- <h4 class="subsection">2.10.1 New scalar types</h4>
- <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>.
- </p>
- <div class="example">
- <pre class="verbatim">struct W { int x; }
- ra::Big<W, 2> w = {{ {4}, {2} }, { {1}, {3} }};
- cout << W(1, 1).x << endl;
- cout << amin(map([](auto && x) { return w.x; }, w)) << endl;
- </pre><pre class="example">-| 3
- 1
- </pre></div>
- <p>However, if you want to mix arbitrary types in array operations, you’ll need to tell <code>ra::</code> that that is actually what you want. This is to avoid conflicts with other libraries.
- </p>
- <div class="example">
- <pre class="verbatim">template <> constexpr bool ra::is_scalar_def<W> = true;
- ...
- W ww {11};
- for_each([](auto && x, auto && y) { cout << (x.x + y.y) << " "; }, w, ww); // ok
- </pre><pre class="example">-| 15 13 12 14
- </pre></div>
- <p>but
- </p>
- <div class="example">
- <pre class="verbatim">struct U { int x; }
- U uu {11};
- for_each([](auto && x, auto && y) { cout << (x.x + y.y) << " "; }, w, uu); // error: can't find ra::start(U)
- </pre></div>
- <span id="x_002dnew_002darray_002doperations"></span></div>
- <div class="subsection" id="New-array-operations">
- <h4 class="subsection">2.10.2 New array operations</h4>
- <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):
- </p>
- <div class="example">
- <pre class="verbatim">return_type my_fun(...) { };
- ...
- namespace ra {
- template <class ... A> inline auto
- my_fun(A && ... a)
- {
- return map(::my_fun, std::forward<A>(a) ...);
- }
- } // namespace ra
- </pre></div>
- <span id="index-overload-set"></span>
- <p>If <code>my_fun</code> is an overload set, you can use<a id="DOCF12" href="#FOOT12"><sup>12</sup></a>
- </p>
- <div class="example">
- <pre class="verbatim">namespace ra {
- template <class ... A> inline auto
- my_fun(A && ... a)
- {
- return map([](auto && ... a) { return ::my_fun(a ...); }, std::forward<A>(a) ...);
- }
- } // namespace ra
- </pre></div>
- <span id="index-error"></span>
- <span id="index-assert"></span>
- <hr>
- </div>
- </div>
- <div class="section" id="Error-handling">
- <div class="header">
- <p>
- Previous: <a href="#Extension" accesskey="p" rel="prev">Extension</a>, Up: <a href="#Usage" accesskey="u" rel="up">Usage</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Error-handling-1"></span><h3 class="section">2.11 Error handling</h3>
- <p><code>ra::</code> tries to detect a many errors as possible at compile time, but some errors, such as <a href="#Rank-extension">mismatch</a> between expressions of runtime shape or <a href="#Slicing">out of range indices</a>, aren’t apparent until runtime.
- </p>
- <p>Runtime error handling in <code>ra::</code> is controlled by two macros.
- </p>
- <ul>
- <li> <code>RA_ASSERT(cond, ...)</code> —
- check that <code>cond</code> evaluates to true in the <code>ra::</code> namespace. The other arguments are informative.
- </li><li> <code>RA_DO_CHECK</code> —
- must have one of the values 0, 1, or 2.
- </li></ul>
- <p>They work as follows:
- </p>
- <ul>
- <li> If <code>RA_DO_CHECK</code> is 0, runtime checks are skipped.
- </li><li> If <code>RA_DO_CHECK</code> is not 0, runtime checks are done.
- <ul>
- <li> If <code>RA_ASSERT</code> is defined, using <code>RA_ASSERT</code>.
- </li><li> If <code>RA_ASSERT</code> isn’t defined, the method depends on the value of <code>RA_DO_CHECK</code>. The two options are 1 (plain <code>assert</code>) and 2 (prints the informative arguments and aborts). Other values are an error.
- </li></ul>
- </li></ul>
- <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>, but note that <samp>-DNDEBUG</samp> will also disable any <code>assert</code>s that are a result of <code>RA_DO_CHECK=1</code>.
- </p>
- <p>The performance cost of the runtime checks depends on the program. Without custom <code>RA_ASSERT</code>, <code>RA_DO_CHECK=1</code> usually has an acceptable cost, but <code>RA_DO_CHECK=2</code> may be more expensive. The default is <code>RA_DO_CHECK=1</code>.
- </p>
- <p>The following example shows how errors might be reported depending on <code>RA_DO_CHECK</code>.
- </p>
- <table class="cartouche" border="1"><tr><td>
- <pre class="verbatim">ra::Big<int, 2> a({10, 3}, 1);
- ra::Big<int, 2> b({40, 3}, 2);
- cout << sum(a+b) << endl;
- </pre></td></tr></table>
- <ul>
- <li> <code>RA_DO_CHECK=2</code>
- <pre class="verbatim">*** ra::./ra/expr.hh:389,17 (check()) Mismatched shapes [10 3] [40 3]. ***}
- </pre></li><li> <code>RA_DO_CHECK=1</code>
- <pre class="verbatim">./ra/expr.hh:389: constexpr ra::Match<checkp, std::tuple<_UTypes ...>, std::tuple<std::integral_constant<int, I>...> >::Match(P ...)
- [with bool checkp = true; P = {ra::Cell<int, const ra::SmallArray<ra::Dim, std::tuple<std::integral_constant<int, 2> >,
- std::tuple<std::integral_constant<int, 1> >, std::tuple<ra::Dim, ra::Dim> >&, std::integral_constant<int, 0> >, ra::Cell<int, const
- ra::SmallArray<ra::Dim, std::tuple<std::integral_constant<int, 2> >, std::tuple<std::integral_constant<int, 1> >, std::tuple<ra::Dim,
- ra::Dim> >&, std::integral_constant<int, 0> >}; int ...I = {0, 1}]: Assertion `check()' failed.
- </pre></li></ul>
- <span id="index-exception"></span>
- <p>You can redefine <code>RA_ASSERT</code> to something that is more appropriate for your program. For example, if you run <code>ra::</code> code under a shell, an abort may not be acceptable. <code>examples/throw.cc</code> shows how to throw a user-defined exception instead.
- </p>
- <hr>
- </div>
- </div>
- <div class="chapter" id="Extras">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Extras-1"></span><h2 class="chapter">3 Extras</h2>
- <hr>
- </div>
- <div class="chapter" id="Hazards">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Hazards-1"></span><h2 class="chapter">4 Hazards</h2>
- <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.
- </p>
- <ul class="section-toc">
- <li><a href="#Reuse-of-expression-objects" accesskey="1">Reuse of expression objects</a></li>
- <li><a href="#Assignment-to-views" accesskey="2">Assignment to views</a></li>
- <li><a href="#View-of-const-vs-const-view" accesskey="3">View of const vs const view</a></li>
- <li><a href="#Rank-extension-in-assignments" accesskey="4">Rank extension in assignments</a></li>
- <li><a href="#Performance-pitfalls-of-rank-extension" accesskey="5">Performance pitfalls of rank extension</a></li>
- <li><a href="#Initialization-of-nested-types" accesskey="6">Initialization of nested types</a></li>
- <li><a href="#Chained-assignment" accesskey="7">Chained assignment</a></li>
- <li><a href="#Unregistered-scalar-types" accesskey="8">Unregistered scalar types</a></li>
- <li><a href="#The-cost-of-RA_005fDO_005fCHECK" accesskey="9">The cost of <code>RA_DO_CHECK</code></a></li>
- </ul>
- <div class="section" id="Reuse-of-expression-objects">
- <h3 class="section">4.1 Reuse of expression objects</h3>
- <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:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> B({3, 3}, ra::_1 + ra::_0*3); // {{0 1 2} {3 4 5} {6 7 8}}
- std::array<int, 2> l = { 1, 2 };
- cout << B(ra::ptr(l), ra::ptr(l)) << endl; // ok => {{4 5} {7 8}}
- auto ll = ra::ptr(l);
- cout << B(ll, ll) << endl; // ??
- </pre></div>
- </div>
- <div class="section" id="Assignment-to-views">
- <h3 class="section">4.2 Assignment to views</h3>
- <p>FIXME
- 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>>></code> container may work; do not rely on it.
- </p>
- </div>
- <div class="section" id="View-of-const-vs-const-view">
- <h3 class="section">4.3 View of const vs const view</h3>
- <p>FIXME
- Passing view arguments by reference
- </p>
- </div>
- <div class="section" id="Rank-extension-in-assignments">
- <h3 class="section">4.4 Rank extension in assignments</h3>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">int a = 0;
- ra::scalar(a) = 3 + ra::Small<int, 3> {4, 5, 6}; // ?
- </pre><pre class="example"> ⇒ a = 9
- </pre></div>
- <p>Compare with
- </p>
- <div class="example">
- <pre class="verbatim">int a = 0;
- ra::scalar(a) += 3 + ra::Small<int, 3> {4, 5, 6}; // 0 + 3 + 4 + 5 + 6
- </pre><pre class="example"> ⇒ a = 18
- </pre></div>
- <hr>
- </div>
- <div class="section" id="Performance-pitfalls-of-rank-extension">
- <div class="header">
- <p>
- Next: <a href="#Initialization-of-nested-types" accesskey="n" rel="next">Initialization of nested types</a>, Up: <a href="#Hazards" accesskey="u" rel="up">Hazards</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Performance-pitfalls-of-rank-extension-1"></span><h3 class="section">4.5 Performance pitfalls of rank extension</h3>
- <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’t have side effects. In such cases it might be preferrable to write the outer loop explicitly, or to do some precomputation.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> a = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}};
- ra::Big<int, 1> b = {1, 2, 3};
- ra::Big<int, 2> c = map(f, b) + a;
- </pre></div>
- <hr>
- </div>
- <div class="section" id="Initialization-of-nested-types">
- <div class="header">
- <p>
- Previous: <a href="#Performance-pitfalls-of-rank-extension" accesskey="p" rel="prev">Performance pitfalls of rank extension</a>, Up: <a href="#Hazards" accesskey="u" rel="up">Hazards</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Initialization-of-nested-types-1"></span><h3 class="section">4.6 Initialization of nested types</h3>
- <p>This doesn’t do what you probably think it does.
- </p><div class="example">
- <pre class="verbatim">ra::Big<int2, 0> b({}, int2 {1, 3}); // b = {int2 {3, 3}} (*)
- </pre></div>
- <p>What happens here is that rank-0 <code>b</code> is prefix matched to rank-1 <code>int2 {1, 3}</code> which results in two assignments, first <code>b</code> ← 1 and then <code>b</code> ← 3. Compare with
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int2, 0> b({}, ra::scalar(int2 {1, 3})); // b = {int2 {1, 3}} @c [ma116]
- </pre></div>
- <p>The use of <a href="#x_002dscalar"><code>scalar</code></a> forces <code>b</code> to match the whole of <code>int2 {1, 3}</code> as a rank-0 object. After that, the single element in <code>b</code> is matched to <code>int2 {1, 3}</code>.
- </p>
- <p>Assignment or initialization from higher rank to lower rank is something of a footgun. Assignments have some <a href="#x_002daxis_002dreductions">legitimate uses</a>, but initializations like <code>(*)</code> may become errors in future versions of <code>ra::</code>.
- </p>
- </div>
- <div class="section" id="Chained-assignment">
- <h3 class="section">4.7 Chained assignment</h3>
- <p>FIXME
- When <code>a=b=c</code> works, it operates as <code>b=c; a=b;</code> and not as an array expression.
- </p>
- </div>
- <div class="section" id="Unregistered-scalar-types">
- <h3 class="section">4.8 Unregistered scalar types</h3>
- <p>FIXME
- <code>View<T, N> x; x = T()</code> fails if <code>T</code> isn’t registered as <code>is_scalar</code>.
- </p>
- <ol>
- <li> Item 0
- </li><li> Item 1
- </li><li> Item 2
- </li></ol>
- </div>
- <div class="section" id="The-cost-of-RA_005fDO_005fCHECK">
- <h3 class="section">4.9 The cost of <code>RA_DO_CHECK</code></h3>
- <p>FIXME
- </p>
- <hr>
- </div>
- </div>
- <div class="chapter" id="Internals">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Internals-1"></span><h2 class="chapter">5 Internals</h2>
- <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).
- </p>
- <ul class="section-toc">
- <li><a href="#Headers" accesskey="1">Headers</a></li>
- <li><a href="#Type-hierarchy" accesskey="2">Type hierarchy</a></li>
- <li><a href="#Term-agreement" accesskey="3">Term agreement</a></li>
- <li><a href="#Traversal" accesskey="4">Traversal</a></li>
- <li><a href="#Introspection" accesskey="5">Introspection</a></li>
- <li><a href="#Building-the-test-suite" accesskey="6">Building the test suite</a></li>
- </ul>
- <hr>
- <div class="section" id="Headers">
- <div class="header">
- <p>
- Next: <a href="#Type-hierarchy" accesskey="n" rel="next">Type hierarchy</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Headers-1"></span><h3 class="section">5.1 Headers</h3>
- <p>The header structure of <code>ra::</code> is as follows.</p>
- <ul>
- <li> <code>tuples.hh</code> -
- Generic macros and tuple library.
- </li><li> <code>base.hh</code> -
- Basic types and concepts, introspection.
- </li><li> <code>expr.hh</code> -
- Expression template nodes.
- </li><li> <code>ply.hh</code> -
- Traversal, including I/O.
- </li><li> <code>small.hh</code> -
- Array type with compile time dimensions.
- </li><li> <code>big.hh</code> -
- Array type with run time dimensions.
- </li><li> <code>ra.hh</code> -
- Functions and operators, main header.
- </li><li> <code>test.hh</code> -
- (accessory) Testing/benchmarking library.
- </li><li> <code>dual.hh</code> -
- (accessory) Dual number type and operations.
- </li></ul>
- <hr>
- </div>
- <div class="section" id="Type-hierarchy">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Type-hierarchy-1"></span><h3 class="section">5.2 Type hierarchy</h3>
- <p>Some of the categories below are C++20 ‘concepts’, some are still informal.
- </p>
- <ul>
- <li> <b>Container</b> — <code>Big</code>, <code>Shared</code>, <code>Unique</code>, <code>Small</code>
- <p>These are array types that own their data in one way or another.
- </p>
- </li><li> <b>View</b> — <code>ViewBig</code>, <code>ViewSmall</code>
- <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 into memory that has been allocated independently.
- </p>
- </li><li> <b>Iterator</b> — <code>CellBig</code>, <code>CellSmall</code>, <code>Iota</code>, <code>Ptr</code>, <code>Scalar</code>, <code>Expr</code>, <code>Pick</code>
- <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.
- </p>
- <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()</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 nested loop, especially if the inner dimensions of the loop are small.
- </p>
- <p><b>Iterator</b>s also provide an <code>at(i ...)</code> method for random access to any element.
- </p>
- </li></ul>
- <hr>
- </div>
- <div class="section" id="Term-agreement">
- <div class="header">
- <p>
- Next: <a href="#Traversal" accesskey="n" rel="next">Traversal</a>, Previous: <a href="#Type-hierarchy" accesskey="p" rel="prev">Type hierarchy</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Term-agreement-1"></span><h3 class="section">5.3 Term agreement</h3>
- <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.
- </p>
- <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 (v28) there is no heuristic to choose a dimension order, so traversal always happens in row-major order (which shouldn’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.
- </p>
- <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.
- </p>
- <hr>
- </div>
- <div class="section" id="Traversal">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Traversal-1"></span><h3 class="section">5.4 Traversal</h3>
- <hr>
- </div>
- <div class="section" id="Introspection">
- <div class="header">
- <p>
- Next: <a href="#Building-the-test-suite" accesskey="n" rel="next">Building the test suite</a>, Previous: <a href="#Traversal" accesskey="p" rel="prev">Traversal</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Introspection-1"></span><h3 class="section">5.5 Introspection</h3>
- <p>The following functions are available to query the properties of <code>ra::</code> objects.
- </p>
- <span id="index-rank-1"></span>
- <span id="x_002drank"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd>
- <p>Return the rank of expression <var>e</var>.
- </p>
- </dd></dl>
- <span id="index-shape-1"></span>
- <span id="x_002dshape"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dt id="index-shape-3"><span class="category">Function<!-- /@w -->: </span><span><em>expr</em> <strong>shape</strong> <em>e k</em><a href='#index-shape-3' class='copiable-anchor'> ¶</a></span></dt>
- <dd>
- <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>. It is possible to use <a href="#x_002dlen"><code>len</code></a> in <var>k</var> to mean the rank of <var>e</var>.
- </p>
- <div class="example">
- <pre class="verbatim"> ra::Small<int, 2, 3, 4> A;
- cout << shape(A, ra::len-1) << endl; // length of last axis
- </pre><pre class="example">-| 4
- </pre></div>
- <p>If <var>k</var> is not a scalar, <code>shape(e, k)</code> returns a map of <var>k</var> over <code>shape(e, ·)</code>.
- </p>
- <div class="example">
- <pre class="verbatim"> cout << shape(A, ra::iota(2, ra::len-2)) << endl; // last two lengths
- </pre><pre class="example">-| 3 4
- </pre></div>
- <p>Note that the return type <var>array</var> of <code>shape(e)</code> might be a <a href="#x_002dforeign_002dtype">foreign type</a> (such as <code>std::array</code> or <code>std::vector</code>) instead of a <code>ra::</code> type. In that case, <code>shape(A)(ra::iota(2, ra::len-2))</code> will not work.
- </p>
- </dd></dl>
- <hr>
- </div>
- <div class="section" id="Building-the-test-suite">
- <div class="header">
- <p>
- Previous: <a href="#Introspection" accesskey="p" rel="prev">Introspection</a>, Up: <a href="#Internals" accesskey="u" rel="up">Internals</a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Building-the-test-suite-1"></span><h3 class="section">5.6 Building the test suite</h3>
- <p><code>ra::</code> uses its own test and benchmark suites and it comes with three kinds of tests: proper tests, benchmarks, and examples. To build and run them all, run the following from the top directory:
- </p>
- <blockquote>
- <pre class="verbatim">CXXFLAGS=-O3 scons
- </pre></blockquote>
- <p>or
- </p><blockquote>
- <pre class="verbatim">CXXFLAGS=-O3 cmake . && make && make test
- </pre></blockquote>
- <p><code>ra::</code> is highly dependent on the optimization level and the test suite may run much slower with <samp>-O0</samp> or <samp>-O1</samp>.
- </p>
- <p>The SCons/CMake scripts accept the following options:
- </p>
- <span id="index-BLAS-1"></span>
- <ul>
- <li> <code>scons --no-sanitize</code> / <code>cmake -DSANITIZE=0</code>
- <p>Remove the sanitizer flags. This is necessary because appending <code>-fno-sanitize=all</code> to <code>CXXFLAGS</code>, as in <code>-fsanitize=... -fno-sanitize=all</code>, doesn’t completely remove the sanitizers. The default is to build with sanitizers.
- </p>
- </li><li> <code>scons --blas</code>
- <p>Try to use BLAS in the benchmarks. The CMake script tries to detect BLAS automatically, but the SCons script doesn’t, hence this option.
- </p></li></ul>
- <hr>
- </div>
- </div>
- <div class="chapter" id="The-future">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="The-future-1"></span><h2 class="chapter">6 The future</h2>
- <ul class="section-toc">
- <li><a href="#Error-messages" accesskey="1">Error messages</a></li>
- <li><a href="#Reductions" accesskey="2">Reductions</a></li>
- <li><a href="#Etc" accesskey="3">Etc</a></li>
- </ul>
- <div class="section" id="Error-messages">
- <h3 class="section">6.1 Error messages</h3>
- <p>FIXME
- </p>
- </div>
- <div class="section" id="Reductions">
- <h3 class="section">6.2 Reductions</h3>
- <p>FIXME
- </p>
- </div>
- <div class="section" id="Etc">
- <h3 class="section">6.3 Etc</h3>
- <p>FIXME
- </p>
- <hr>
- </div>
- </div>
- <div class="chapter" id="Reference">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Reference-1"></span><h2 class="chapter">7 Reference</h2>
- <span id="index-agree"></span>
- <span id="x_002dagree"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Return true if the shapes of arguments <var>arg...</var> match (see <a href="#Rank-extension">Rank extension</a>), else return false.
- </p>
- <p>This is useful when <a href="#Error-handling">error checking</a> is enabled and one wants to avoid the failure response.
- </p><div class="example">
- <pre class="verbatim"> ra::Small<int, 2, 3> A;
- ra::Small<int, 2> B;
- ra::Small<int, 3> C;
- agree(A, B); // -> true
- static_assert(agree(A, B)); // ok for ct shapes
- cout << (A+B) << endl; // ok
- agree(A, C); // -> false
- cout << (A+C) << endl; // error. Maybe abort, maybe throw - cf Error Handling
- </pre></div>
- </dd></dl>
- <span id="index-agree_005fop"></span>
- <span id="x_002dagree_005fop"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p>
- <p>This differs from <a href="#x_002dagree"><code>agree</code></a> when <var>op</var> has non-zero argument ranks. For example:
- </p><div class="example">
- <pre class="verbatim"> ra::Big<real, 1> a({3}, 0.);
- ra::Big<real, 2> b({2, 3}, 0.n);
- agree(a, b); // -> false
- cout << (a+b) << endl; // error
- agree_op(ra::wrank<1, 1>(std::plus()), a, b); // -> true
- cout << map(ra::wrank<1, 1>(std::plus()), a, b) << endl; // ok
- </pre></div>
- </dd></dl>
- <span id="index-at"></span>
- <span id="x_002dat"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Look up <var>expr</var> at each element of <var>indices</var>, which shall be a multi-index into <var>expr</var>.
- </p>
- <p>This can be used for sparse subscripting. For example:
- </p><div class="example">
- <pre class="verbatim"> ra::Big<int, 2> A = {{100, 101}, {110, 111}, {120, 121}};
- ra::Big<ra::Small<int, 2>, 2> i = {{{0, 1}, {2, 0}}, {{1, 0}, {2, 1}}};
- ra::Big<int, 2> B = at(A, i);
- </pre><pre class="example"> ⇒ B = {{101, 120}, {110, 121}}
- </pre></div>
- </dd></dl>
- <span id="index-begin"></span>
- <span id="x_002dbegin"></span><dl class="def">
- <dt id="index-begin-1"><span class="category">Function: </span><span><strong>begin</strong> <em>expr</em><a href='#index-begin-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create STL iterator from <var>expr</var>.
- </p>
- <p>See <a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a>.
- </p>
- <p>See also <a href="#x_002dend"><code>end</code></a>, <a href="#x_002drange"><code>range</code></a>.
- </p></dd></dl>
- <span id="index-cast"></span>
- <span id="x_002dcast"></span><dl class="def">
- <dt id="index-cast-1"><span class="category">Function: </span><span><strong>cast</strong> <em><type> expr</em><a href='#index-cast-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create an array expression that casts <var>expr</var> into <var>type</var>.
- </p></dd></dl>
- <span id="index-collapse"></span>
- <span id="x_002dcollapse"></span><dl class="def">
- <dt id="index-collapse-1"><span class="category">Function: </span><span><strong>collapse</strong><a href='#index-collapse-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>See also <a href="#x_002dexplode"><code>explode</code></a>.
- </p></dd></dl>
- <span id="index-concrete"></span>
- <span id="x_002dconcrete"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Convert the argument to a container of the same shape as <var>a</var>.
- </p>
- <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.
- </p>
- </dd></dl>
- <span id="index-diag"></span>
- <span id="x_002ddiag"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Equivalent to <code>transpose<0, 0>(view)</code>.
- </p></dd></dl>
- <span id="index-dot"></span>
- <span id="x_002ddot"></span><dl class="def">
- <dt id="index-dot-1"><span class="category">Function: </span><span><strong>dot</strong> <em>a b</em><a href='#index-dot-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Compute dot product of expressions <var>a</var> and <var>b</var>.
- </p>
- </dd></dl>
- <span id="index-explode"></span>
- <span id="x_002dexplode"></span><dl class="def">
- <dt id="index-explode-1"><span class="category">Function: </span><span><strong>explode</strong><a href='#index-explode-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd>
- <p>See also <a href="#x_002dcollapse"><code>collapse</code></a>.
- </p></dd></dl>
- <span id="index-end"></span>
- <span id="x_002dend"></span><dl class="def">
- <dt id="index-end-1"><span class="category">Function: </span><span><strong>end</strong> <em>expr</em><a href='#index-end-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create STL end iterator from <var>expr</var>.
- </p>
- <p>See <a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a>.
- </p>
- <p>See also <a href="#x_002dbegin"><code>begin</code></a>, <a href="#x_002drange"><code>range</code></a>.
- </p></dd></dl>
- <span id="index-for_005feach"></span>
- <span id="x_002dfor_005feach"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p>
- <p>For example:
- </p><div class="example">
- <pre class="verbatim">double s = 0.;
- for_each([&s](auto && a) { s+=a; }, ra::Small<double, 1> {1., 2., 3})
- </pre><pre class="example">⇒ s = 6.
- </pre></div>
- <p>See also <a href="#x_002dmap"><code>map</code></a>.
- </p>
- </dd></dl>
- <span id="index-format_005farray"></span>
- <span id="x_002dformat_005farray"></span><dl class="def">
- <dt id="index-format_005farray-1"><span class="category">Function: </span><span><strong>format_array</strong> <em>expr fmt</em><a href='#index-format_005farray-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Format expression for character output. <var>fmt</var> is a struct with the following fields:
- </p>
- <ul>
- <li> <code>shape</code> (<code>bool</code>):
- Whether to print the shape of <var>expr</var> before <var>expr</var> itself.
- </li><li> <code>sep0</code> (<code>char const *</code>):
- Separator between 0-cells.
- </li><li> <code>sepn</code> (<code>char const *</code>):
- Separator between cells of rank > 0. Printed once.
- </li><li> <code>rep</code> (<code>char const *</code>):
- Separator between cells of rank > 1. Printed <var>c</var>-1 times, where <var>c</var> is the rank of cells.
- </li><li> <code>open</code> (<code>char const *</code>):
- Dimension opener.
- </li><li> <code>end</code> (<code>char const *</code>):
- Dimension closer.
- </li><li> <code>align</code> (<code>bool</code>):
- Align the dimension openers. This works better (or at all) when <code>sepn</code> ends in a newline.
- </li></ul>
- <p>The shape that might be printed depending on <code>.shape</code> is not subject to these separators and is always printed as if <code>{.open="", .close="", .sep0=" "}</code>.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Small<int, 2, 2> A = {{1, 2}, {3, 4}};
- cout << "case a:\n" << A << endl;
- cout << "\ncase b:\n" << format_array(A) << endl;
- cout << "\ncase c:\n" << format_array(A, {.sep0="|", .sepn="-"}) << endl;
- cout << "\ncase d:\n" << format_array(A, {.shape=noshape, .open="{", .close="}", .sep0=", ", .sepn=",\n", .rep="", .align=true}) << endl;
- </pre><pre class="example">-|
- </pre><pre class="verbatim">case a:
- 1 2
- 3 4
- case b:
- 1 2
- 3 4
- case c:
- 1|2-3|4
- case d:
- {{1, 2},
- {3, 4}}
- </pre></div>
- <span id="index-jstyle"></span>
- <span id="index-cstyle"></span>
- <span id="index-lstyle"></span>
- <span id="index-pstyle"></span>
- <p>The following styles are predefined: <code>jstyle</code> (the default), <code>cstyle</code>, <code>lstyle</code>, and <code>pstyle</code>. Currently <code>ra::operator>>(std::istream &)</code> can only read whitespace formats, like <code>jstyle</code>.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int> A = {{{1, 2, 3}, {3, 4, 5}}, {{4, 5, 6}, {7, 8, 9}}};
- cout << format_array(A, ra::pstyle) << endl;
- // cout << ra::pstyle << A << endl; // variant
- </pre><pre class="example">-|
- </pre><pre class="verbatim">[[[1, 2, 3],
- [3, 4, 5]],
- [[4, 5, 6],
- [7, 8, 9]]]
- </pre></div>
- <p>See also <a href="#x_002dnoshape"><code>noshape</code></a>, <a href="#x_002dwithshape"><code>withshape</code></a>.
- </p>
- </dd></dl>
- <span id="index-from"></span>
- <span id="x_002dfrom"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Create outer product expression. This is defined as
- </p>
- <div class="display">
- <pre class="display"><em class='tex2jax_process'>\(E = \mathrm{from}(\mathrm{op}, \mathrm{expr}_0, \mathrm{expr}_1 ...) ⇒ E(i_{00}, i_{01} ..., i_{10}, i_{11}, ..., ...) = \mathrm{op}\big(\mathrm{expr}_0(i_{00}, i_{01}, ...), \mathrm{expr}_1(i_{10}, i_{11}, ...), ...\big)\)</em>.
- </pre></div>
- <p>For example:
- </p><div class="example">
- <pre class="verbatim"> ra::Big<double, 1> a {1, 2, 3};
- ra::Big<double, 1> b {10, 20, 30};
- ra::Big<double, 2> axb = from([](auto && a, auto && b) { return a*b; }, a, b)
- </pre><pre class="example"> ⇒ axb = {{10, 20, 30}, {20, 40, 60}, {30, 60, 90}}
- </pre></div>
- <div class="example">
- <pre class="verbatim"> ra::Big<int, 1> i {2, 1};
- ra::Big<int, 1> j {0, 1};
- ra::Big<double, 2> A = {{1, 2}, {3, 4}, {5, 6}};
- ra::Big<double, 2> Aij = from(A, i, j)
- </pre><pre class="example"> ⇒ Aij = {{6, 5}, {4, 3}}
- </pre></div>
- <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>).
- </p>
- </dd></dl>
- <span id="index-gemm"></span>
- <span id="x_002dgemm"></span><dl class="def">
- <dt id="index-gemm-1"><span class="category">Function: </span><span><strong>gemm</strong> <em>a b</em><a href='#index-gemm-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Compute matrix-matrix product of expressions <var>a</var> and <var>b</var>. This function returns a container.
- </p>
- <p>See <a href="#Performance-pitfalls-of-rank-extension">Performance pitfalls of rank extension</a>.
- </p>
- <p>See also <a href="#x_002dgemv"><code>gemv</code></a>, <a href="#x_002dgevm"><code>gevm</code></a>.
- </p></dd></dl>
- <span id="index-gemv"></span>
- <span id="x_002dgemv"></span><dl class="def">
- <dt id="index-gemv-1"><span class="category">Function: </span><span><strong>gemv</strong> <em>a b</em><a href='#index-gemv-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Compute matrix-vector product of expressions <var>a</var> and <var>b</var>. This function returns a container.
- </p>
- <p>See <a href="#Performance-pitfalls-of-rank-extension">Performance pitfalls of rank extension</a>.
- </p>
- <p>See also <a href="#x_002dgemm"><code>gemm</code></a>, <a href="#x_002dgevm"><code>gevm</code></a>.
- </p></dd></dl>
- <span id="index-gevm"></span>
- <span id="x_002dgevm"></span><dl class="def">
- <dt id="index-gevm-1"><span class="category">Function: </span><span><strong>gevm</strong> <em>a b</em><a href='#index-gevm-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Compute vector-matrix product of expressions <var>a</var> and <var>b</var>. This function returns a container.
- </p>
- <p>See <a href="#Performance-pitfalls-of-rank-extension">Performance pitfalls of rank extension</a>.
- </p>
- <p>See also <a href="#x_002dgemm"><code>gemm</code></a>, <a href="#x_002dgemv"><code>gemv</code></a>.
- </p></dd></dl>
- <span id="index-imag_005fpart"></span>
- <span id="x_002dimag_005fpart"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Take imaginary part of a complex number. This can be used as reference.
- </p>
- <p>For example: </p>
- <div class="example">
- <pre class="verbatim">ra::Small<std::complex<double>, 2, 2> A = {{1., 2.}, {3., 4.}};
- imag_part(A) = -2*real_part(A);
- cout << A << endl;
- </pre><pre class="example">-|
- (1, -2) (2, -4)
- (3, -6) (4, -8)
- </pre></div>
- <p>See also <a href="#x_002dreal_005fpart"><code>real_part</code></a>.
- </p></dd></dl>
- <span id="index-map"></span>
- <span id="x_002dmap"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Create an array expression that applies callable <var>op</var> to <var>expr</var> ...
- </p>
- <p>For example:
- </p><div class="example">
- <pre class="verbatim">ra::Big<double, 1> x = map(cos, std::array {0.});
- </pre><pre class="example">⇒ x = { 1. }
- </pre></div>
- <p><var>op</var> can return a reference. For example:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> x = {{3, 3}, 0.};
- ra::Big<int, 2> i = {0, 1, 1, 2};
- ra::Big<int, 2> j = {1, 0, 2, 1};
- map(x, i, j) = 1;
- </pre><pre class="example">⇒ x = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}}
- </pre></div>
- <p><var>op</var> can be any callable. For example:
- </p><div class="example">
- <pre class="verbatim">struct A { int a, b; };
- std::vector<A> v = {{1, 2}, {3, 4}};
- ra::map(&A::a, v) = -ra::map(&A::b, v); // pointer to member
- </pre><pre class="example">⇒ v = {{-2, 2}, {-4, 4}}
- </pre></div>
- <p>Operations defined on scalar arguments are usually extended to higher rank arguments through <code>op(i ...)</code> ≡ <code>map(op, i ...)</code>, but note that <a href="#x_002dsubscript_002douter_002dproduct">this is not the case</a> when <var>op</var> is a view.
- </p>
- <p>See also <a href="#x_002dfor_005feach"><code>for_each</code></a>.
- </p>
- </dd></dl>
- <span id="index-pack"></span>
- <span id="x_002dpack"></span><dl class="def">
- <dt id="index-pack-1"><span class="category">Function: </span><span><strong>pack</strong> <em><type> expr ...</em><a href='#index-pack-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create an array expression that brace-constructs <var>type</var> from <var>expr</var> ...
- </p></dd></dl>
- <span id="index-pick"></span>
- <span id="x_002dpick"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p>
- <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>.
- </p>
- <p>For example:
- </p><div class="example">
- <pre class="verbatim"> ra::Small<int, 3> s {2, 1, 0};
- ra::Small<double, 3> z = pick(s, s*s, s+s, sqrt(s));
- </pre><pre class="example"> ⇒ z = {1.41421, 2, 0}
- </pre></div>
- <p>See also <a href="#x_002dwhere"><code>where</code></a>.
- </p>
- </dd></dl>
- <span id="index-ply"></span>
- <span id="x_002dply"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Traverse <var>expr</var>. <code>ply</code> returns <code>void</code> so <var>expr</var> should be run for effect.
- </p>
- <p>It’s 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.
- </p>
- <div class="example">
- <pre class="verbatim">double s = 0.;
- ply(map([&s](auto && a) { s+=a; }, ra::Small<double, 1> {1., 2., 3})) // same as for_each
- </pre><pre class="example">⇒ s = 6.
- </pre></div>
- </dd></dl>
- <span id="index-real_005fpart"></span>
- <span id="x_002dreal_005fpart"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Take real part of a complex number. This can be used as reference.
- </p>
- <p>See also <a href="#x_002dimag_005fpart"><code>imag_part</code></a>.
- </p></dd></dl>
- <span id="index-reverse"></span>
- <span id="x_002dreverse"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Create a new view by reversing axis <var>k</var> of <var>view</var>.
- </p>
- <p>This is equivalent to <code>view(ra::dots<k>, ra::iota(ra::len, ra::len-1, -1))</code>.
- </p>
- <p>This operation does not work on arbitrary array expressions yet. </p>
- </dd></dl>
- <span id="index-size"></span>
- <span id="x_002dsize"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Get the total size of an <code>ra::</code> object: the product of all its lengths.
- </p></dd></dl>
- <span id="index-stencil"></span>
- <span id="x_002dstencil"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Create a stencil on <var>view</var> with lower bounds <var>lo</var> and higher bounds <var>hi</var>.
- </p>
- <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>.
- </p>
- <p>This operation does not work on arbitrary array expressions yet. </p>
- </dd></dl>
- <span id="index-swap"></span>
- <span id="x_002dswap"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Swap the contents of containers <var>a</var> and <var>b</var>.
- </p>
- <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.
- </p>
- <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>).
- </p></dd></dl>
- <div class="example">
- <pre class="verbatim"> ra::Big<int> a ({2, 3}, 1 + ra::_0 - ra::_1); // (1)
- ra::Big<int> b ({2, 3, 4}, 1 - ra::_0 + ra::_1 + ra::_2); // (2)
- swap(a, b);
- // as if (1) had b and (2) had a
- </pre></div>
- <span id="index-transpose"></span>
- <span id="x_002dtranspose"></span><dl class="def">
- <dt id="index-transpose-1"><span class="category">Function: </span><span><strong>transpose</strong> <em><axes ...> (view) | (axes, view)</em><a href='#index-transpose-1' class='copiable-anchor'> ¶</a></span></dt>
- <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>.
- </p>
- <p><code>axes</code> are the <em>destination</em> axes, that is, axis <em class='tex2jax_process'>\(i\)</em> of <var>view</var> matches axis <var>axes</var><em class='tex2jax_process'>\(_i\)</em> of the result. For example:
- </p>
- <div class="example">
- <pre class="verbatim"> ra::Unique<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
- cout << transpose<1, 0>(a) << endl;
- </pre><pre class="example"> -|
- 3 2
- 1 4
- 2 5
- 3 6
- </pre></div>
- <p>The rank of the result is <em class='tex2jax_process'>\(1+\mathrm{max}ᵢ(\)</em><var>axes</var><em class='tex2jax_process'>\(_i\)</em><em class='tex2jax_process'>\()\)</em> and it may be smaller or larger than that of <var>view</var>. If an axis is repeated, the step on the destination is the sum of the steps of the source axes and the length is the smallest of the lengths of the source axes. For example:
- </p>
- <div class="example">
- <pre class="verbatim"> ra::Unique<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
- cout << transpose<0, 0>(a) << endl; // { a(0, 0), a(1, 1) }
- </pre><pre class="example"> -|
- 2
- 1 5
- </pre></div>
- <p>If one of the destination axes isn’t listed 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:
- </p>
- <div class="example">
- <pre class="verbatim"> ra::Unique<double, 1> a = {1, 2, 3};
- cout << ((a*10) + transpose<1>(a)) << endl;
- </pre><pre class="example"> -|
- 3 3
- 11 21 31
- 12 22 32
- 13 23 33
- </pre></div>
- <p>The two argument form lets you specify the axis list at runtime. In that case the result will have runtime rank as well. For example: </p>
- <div class="example">
- <pre class="verbatim"> ra::Small<int, 2> axes = {0, 1};
- ra::Unique<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
- cout << "A: " << transpose(axes, a) << endl;
- axes = {1, 0};
- cout << "B: " << transpose(axes, a) << endl;
- </pre><pre class="example"> -|
- A: 2
- 2 3
- 1 2 3
- 4 5 6
- B: 2
- 3 2
- 1 4
- 2 5
- 3 6
- </pre></div>
- <p>This operation does not work on arbitrary array expressions yet. </p>
- </dd></dl>
- <span id="index-where"></span>
- <span id="index-Masking"></span>
- <span id="x_002dwhere"></span><dl class="def">
- <dt id="index-where-1"><span class="category">Function: </span><span><strong>where</strong> <em>expred extrue exfalse</em><a href='#index-where-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create an array expression that selects <var>extrue</var> if <var>expred</var> is <code>true</code>, and <var>exfalse</var> if <var>expred</var> is <code>false</code>. The expression that is not selected is not looked up.
- </p>
- <p>For example:
- </p><div class="example">
- <pre class="verbatim"> ra::Big<double, 1> s {1, -1, 3, 2};
- s = where(s>=2, 2, s); // saturate s
- </pre><pre class="example"> ⇒ s = {1, -1, 2, 2}
- </pre></div>
- <p>See also <a href="#x_002dpick"><code>pick</code></a>.
- </p>
- </dd></dl>
- <span id="index-wrank"></span>
- <span id="x_002dwrank"></span><dl class="def">
- <dt id="index-wrank-1"><span class="category">Function: </span><span><strong>wrank</strong> <em><input_rank ...> op</em><a href='#index-wrank-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Wrap <var>op</var> using a rank conjunction (see <a href="#The-rank-conjunction">The rank conjunction</a>).
- </p>
- </dd></dl>
- <span id="index-noshape"></span>
- <span id="index-withshape"></span>
- <span id="x_002dnoshape"></span><span id="x_002dwithshape"></span><dl class="def">
- <dt id="index-withshape-noshape"><span class="category">Special object<!-- /@w -->: </span><span><strong>withshape noshape</strong><a href='#index-withshape-noshape' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>If either of these objects is sent to a <code>std::ostream</code> before an expression object, the shape of that object will or will not be printed.
- </p>
- <p>If the object has ct shape, the default is not to print the shape, so <code>noshape</code> isn’t necessary, and conversely for <code>withshape</code> if the object has rt shape. Note that the array readers [<code>operator>>(std::istream &, ...)</code>] require the default setting.
- </p>
- <p>For example:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Small<int, 2, 2> A = {77, 99};
- cout << "case a:\n" << A << endl;
- cout << "case b:\n" << ra::noshape << A << endl;
- cout << "case c:\n" << ra::withshape << A << endl;
- </pre><pre class="example">-| case a:
- 77 99
- case b:
- 77 99
- case c:
- 2
- 77 99
- </pre></div>
- <p>but:
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int> A = {77, 99};
- cout << "case a:\n" << A << endl;
- cout << "case b:\n" << ra::noshape << A << endl;
- cout << "case c:\n" << ra::withshape << A << endl;
- </pre><pre class="example">-| case a:
- 1
- 2
- 77 99
- case b:
- 77 99
- case c:
- 1
- 2
- 77 99
- </pre></div>
- <p>Note that in the last example the very shape of <code>ra::Big<int></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>.
- </p>
- <p>See also <a href="#x_002dformat_005farray"><code>format_array</code></a>.
- </p>
- </dd></dl>
- <span id="index-ptr"></span>
- <span id="x_002dptr"></span><dl class="def">
- <dt id="index-ptr-1"><span class="category">Function<!-- /@w -->: </span><span><strong>ptr</strong> <em>bidirectional_iterator [len [step]]</em><a href='#index-ptr-1' class='copiable-anchor'> ¶</a></span></dt>
- <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'> ¶</a></span></dt>
- <dd><p>Create rank-1 expression from foreign object.
- </p>
- <p>If <code>len</code> is not given for <var>bidirectional_iterator</var>, the expression has undefined length, and needs to be matched with other expressions whose length is defined. <code>ra::</code> doesn’t know what is actually accessible through the iterator, so be careful. For instance:
- </p>
- <div class="example">
- <pre class="verbatim">int pp[] = {1, 2, 3};
- int * p = pp; // erase length
- ra::Big<int, 1> v3 {1, 2, 3};
- ra::Big<int, 1> v4 {1, 2, 3, 4};
- v3 += ra::ptr(p); // ok, shape (3): v3 = {2, 4, 6}
- v4 += ra::ptr(p); // undefined, shape (4): bad access to p[3]
- // cout << (ra::ptr(p)+ra::iota()) << endl; // ct error, expression has undefined shape
- cout << (ra::ptr(p, 3)+ra::iota()) << endl; // ok, prints { 1, 3, 5 }
- cout << (ra::ptr(p, 4)+ra::iota()) << endl; // undefined, bad access at p[4]
- </pre></div>
- <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.
- </p>
- <div class="example">
- <pre class="verbatim">v3 += pp; // ok, shapes match
- v4 += pp; // error checked by ra::, shape clash (4) += (3)
- cout << (pp + ra::iota()) << endl; // ok, shape from pp
- </pre></div>
- <p><var>len</var> and <var>step</var> can be constant types. For example:
- </p>
- <div class="example">
- <pre class="verbatim">char const * s = "hello";
- auto p = ra::ptr(s, std::integral_constant<int, 2> {});
- static_assert(2==ra::size(p)); // p not constexpr, but still ok
- </pre></div>
- <p><code>ra::ptr</code> is often equivalent to <a href="#x_002dstart"><code>start</code></a>, and can be omitted in the same way, but raw pointers require <code>ra::ptr</code>.
- </p>
- <p>See also <a href="#x_002dstart"><code>start</code></a>, <a href="#x_002dscalar"><code>scalar</code></a>, <a href="#x_002diota"><code>iota</code></a>.
- </p>
- </dd></dl>
- <span id="index-range"></span>
- <span id="x_002drange"></span><dl class="def">
- <dt id="index-range-1"><span class="category">Function: </span><span><strong>range</strong> <em>expr</em><a href='#index-range-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create STL range iterator from <var>expr</var>.
- </p>
- <p>See <a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a>.
- </p>
- <p>See also <a href="#x_002dbegin"><code>begin</code></a>, <a href="#x_002dend"><code>end</code></a>.
- </p></dd></dl>
- <span id="index-start-1"></span>
- <span id="x_002dstart"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Create array expression from <var>foreign_object</var>.
- </p>
- <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>).
- </p>
- <p>The resulting expresion has shape according to the original object. Compare this with <a href="#x_002dscalar"><code>scalar</code></a>, which only produces rank 0 expressions, or <a href="#x_002dptr"><code>ptr</code></a>, which only produces rank 1 expressions.
- </p>
- <p>Generally one can mix these types with <code>ra::</code> expressions without needing <code>ra::start</code>, but sometimes this isn’t possible, for example for operators that must be class members.
- </p>
- <div class="example">
- <pre class="verbatim">std::vector<int> x = {1, 2, 3};
- ra::Big<int, 1> y = {10, 20, 30};
- cout << (x+y) << endl; // same as ra::start(x)+y
- // x += y; // error: no match for operator+=
- ra::start(x) += y; // ok
- </pre><pre class="example">-| 3
- 11 22 33
- ⇒ x = { 11, 22, 33 }
- </pre></div>
- <p>See also <a href="#x_002dscalar"><code>scalar</code></a>, <a href="#x_002dptr"><code>ptr</code></a>.
- </p></dd></dl>
- <span id="index-scalar"></span>
- <span id="x_002dscalar"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Create scalar expression from <var>expr</var>.
- </p>
- <p>The primary use of this function is to bring a scalar object into the <code>ra::</code> namespace. A somewhat artificial example:
- </p>
- <div class="example">
- <pre class="verbatim">struct W { int x; }
- ra::Big<W, 1> w { {1}, {2}, {3} };
- // error: no matching function for call to start(W)
- // for_each([](auto && a, auto && b) { cout << (a.x + b.x) << endl; }, w, W {7});
- // bring W into ra:: with ra::scalar
- for_each([](auto && a, auto && b) { cout << (a.x + b.x) << endl; }, w, ra::scalar(W {7}));
- </pre><pre class="example">-| 8
- 9
- 10
- </pre></div>
- <p>See also <a href="#x_002dscalar_002dchar_002dstar"><code>this example</code></a>.
- </p>
- <p>Since <code>scalar</code> produces an object with rank 0, it’s also useful when dealing with nested arrays, even for objects that are already in <code>ra::</code>. Consider:
- </p><div class="example">
- <pre class="verbatim">using Vec2 = ra::Small<double, 2>;
- Vec2 x {-1, 1};
- ra::Big<Vec2, 1> c { {1, 2}, {2, 3}, {3, 4} };
- // c += x // error: x has shape (2) and c has shape (3)
- c += ra::scalar(x); // ok: scalar(x) has shape () and matches c.
- </pre><pre class="example"> ⇒ c = { {0, 3}, {1, 4}, {2, 5} }
- </pre></div>
- <p>The result is {c(0)+x, c(1)+x, c(2)+x}. Compare this with
- </p><div class="example">
- <pre class="verbatim">c(ra::iota(2)) += x; // c(ra::iota(2)) with shape (2) matches x with shape (2)
- </pre><pre class="example"> ⇒ c = { {-1, 2}, {2, 5}, {2, 5} }
- </pre></div>
- <p>where the result is {c(0)+x(0), c(1)+x(1), c(2)}.
- </p>
- <p>See also <a href="#x_002dstart"><code>start</code></a>.
- </p></dd></dl>
- <span id="index-iter"></span>
- <span id="x_002diter"></span><dl class="def">
- <dt id="index-iter-1"><span class="category">Function: </span><span><strong>iter</strong> <em><k> (view)</em><a href='#index-iter-1' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Create iterator over the <var>k</var>-cells of <var>view</var>. If <var>k</var> is negative, it’s 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.
- </p>
- <div class="example">
- <pre class="verbatim">ra::Big<int, 2> c {{1, 3, 2}, {7, 1, 3}};
- cout << "max of each row: " << map([](auto && a) { return amax(a); }, iter<1>(c)) << endl;
- ra::Big<int, 1> m({3}, 0);
- scalar(m) = max(scalar(m), iter<1>(c));
- cout << "max of each column: " << m << endl;
- m = 0;
- for_each([&m](auto && a) { m = max(m, a); }, iter<1>(c));
- cout << "max of each column again: " << m << endl;
- </pre><pre class="example">-| max of each row: 2
- 3 7
- max of each column: 3
- 7 3 3
- max of each column again: 3
- 7 3 3
- </pre></div>
- <p>In the following example, <code>iter</code> emulates <a href="#x_002dscalar"><code>scalar</code></a>. Note that the shape () of <code>iter<1>(m)</code> matches the shape (3) of <code>iter<1>(c)</code>. Thus, each of the 1-cells of <code>c</code> matches against the single 1-cell of <code>m</code>.
- </p>
- <div class="example">
- <pre class="verbatim">m = 0;
- iter<1>(m) = max(iter<1>(m), iter<1>(c));
- cout << "max of each column yet again: " << m << endl;
- </pre><pre class="example">-| max of each column again: 3
- 7 3 3
- </pre></div>
- <p>The following example computes the trace of each of the items [(-1)-cells] of <code>c</code>. </p>
- <div class="example">
- <pre class="verbatim">ra::Small<int, 3, 2, 2> c = ra::_0 - ra::_1 - 2*ra::_2;
- cout << "c: " << c << endl;
- cout << "s: " << map([](auto && a) { return sum(diag(a)); }, iter<-1>(c)) << endl;
- </pre><pre class="example">-| c: 0 -2
- -1 -3
- 1 -1
- 0 -2
- 2 0
- 1 -1
- s: -3 -1 -1
- </pre></div>
- </dd></dl>
- <span id="index-sum"></span>
- <span id="x_002dsum"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p></dd></dl>
- <span id="index-prod"></span>
- <span id="x_002dprod"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p></dd></dl>
- <span id="index-amax"></span>
- <span id="x_002damax"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Return the maximum of the elements of <var>expr</var>. If <var>expr</var> is empty, return <code>-std::numeric_limits<T>::infinity()</code> if the type supports it, otherwise <code>std::numeric_limits<T>::lowest()</code>, where <code>T</code> is the value type of the elements of <var>expr</var>.
- </p></dd></dl>
- <span id="index-amin"></span>
- <span id="x_002damin"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Return the minimum of the elements of <var>expr</var>. If <var>expr</var> is empty, return <code>+std::numeric_limits<T>::infinity()</code> if the type supports it, otherwise <code>std::numeric_limits<T>::max()</code>, where <code>T</code> is the value type of the elements of <var>expr</var>.
- </p></dd></dl>
- <span id="index-early"></span>
- <span id="x_002dearly"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p><var>expr</var> is an array expression that returns <code>std::optional<T></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>
- <p>The following definition of elementwise <code>lexicographical_compare</code> relies on <code>early</code>.
- </p>
- <div class="example">
- <pre class="verbatim">template <class A, class B>
- inline bool
- lexicographical_compare(A && a, B && b)
- {
- return early(map([](auto && a, auto && b) { return a==b ? std::nullopt : std::make_optional(a<b); },
- std::forward<A>(a), std::forward<B>(b)),
- false);
- }
- </pre></div>
- </dd></dl>
- <span id="index-any"></span>
- <span id="x_002dany"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p></dd></dl>
- <span id="index-every"></span>
- <span id="x_002devery"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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.
- </p></dd></dl>
- <span id="index-sqr"></span>
- <span id="x_002dsqr"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Compute the square of the elements of <var>expr</var>.
- </p></dd></dl>
- <span id="index-sqrm"></span>
- <span id="x_002dsqrm"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Compute the square of the norm-2 of the elements of <var>expr</var>, that is, <code>conj(expr)*expr</code>.
- </p></dd></dl>
- <span id="index-conj"></span>
- <span id="x_002dconj"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Compute the complex conjugate of <var>expr</var>.
- </p></dd></dl>
- <span id="index-xi"></span>
- <span id="x_002dxi"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <dd><p>Compute <code>(0+1j)</code> times <var>expr</var>.
- </p></dd></dl>
- <span id="index-rel_005ferror"></span>
- <span id="x_002drel_002derror"></span><dl class="def">
- <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'> ¶</a></span></dt>
- <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
- </p>
- <p><code>(a==0. && b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b))</code>
- </p>
- </dd></dl>
- <span id="index-none-2"></span>
- <span id="x_002dnone"></span><dl class="def">
- <dt id="index-none-3"><span class="category">Special object<!-- /@w -->: </span><span><strong>none</strong><a href='#index-none-3' class='copiable-anchor'> ¶</a></span></dt>
- <dd><p>Pass <code>none</code> to container constructors to indicate that the contents shouldn’t be initialized. This is appropriate when the initialization you have in mind doesn’t fit in a constructor argument. For example:
- </p>
- <div class="example">
- <pre class="verbatim">void foreign_initializer(int m, int n, double *);
- ra::Big<double> b({2, 3}, ra::none);
- foreign_initializer(2, 3, b.data());
- </pre></div>
- </dd></dl>
- <hr>
- </div>
- <div class="chapter" id="Sources">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Sources-1"></span><h2 class="chapter">8 Sources</h2>
- <table>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <tr><td width="10%"><span id="Cha86"></span>[Cha86]</td><td width="90%">Gregory J. Chaitin. Physics in APL2, June 1986.</td></tr>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <tr><td width="10%"><span id="Ric08"></span>[Ric08]</td><td width="90%">Henry Rich. J for C programmers, February 2008.</td></tr>
- <tr><td width="10%"></td></tr>
- <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>
- <tr><td width="10%"></td></tr>
- <tr><td width="10%"><span id="Vel01"></span>[Vel01]</td><td width="90%">Todd Veldhuizen. Blitz++ user’s guide, February 2001.</td></tr>
- <tr><td width="10%"></td></tr>
- <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–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>
- </table>
- <hr>
- </div>
- <div class="unnumbered" id="Indices">
- <div class="header">
- <p>
- 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> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Indices-1"></span><h2 class="unnumbered">Indices</h2>
- <table><tr><th valign="top">Jump to: </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-L"><b>L</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-M"><b>M</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
-
- </td></tr></table>
- <table class="index-cp" border="0">
- <tr><td></td><th align="left">Index Entry</th><td> </td><th align="left"> Section</th></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-A">A</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-agree"><code>agree</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-agree_005fop"><code>agree_op</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-amax"><code>amax</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-amin"><code>amin</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-any"><code>any</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-APL">APL</a>:</td><td> </td><td valign="top"><a href="#Why-C_002b_002b">Why C++</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-arity">arity</a>:</td><td> </td><td valign="top"><a href="#Overview">Overview</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-assert">assert</a>:</td><td> </td><td valign="top"><a href="#Extension">Extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-at"><code>at</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-B">B</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-begin"><code>begin</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-BLAS">BLAS</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-BLAS-1">BLAS</a>:</td><td> </td><td valign="top"><a href="#Building-the-test-suite">Building the test suite</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-BLIS">BLIS</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b">Blitz++</a>:</td><td> </td><td valign="top"><a href="#Overview">Overview</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Blitz_002b_002b-1">Blitz++</a>:</td><td> </td><td valign="top"><a href="#Other-libraries">Other libraries</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-broadcasting_002c-singleton_002c-newaxis">broadcasting, singleton, newaxis</a>:</td><td> </td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-broadcasting_002c-singleton_002c-Numpy">broadcasting, singleton, Numpy</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-built_002din-array">built-in array</a>:</td><td> </td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-C">C</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-cast"><code>cast</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-cell">cell</a>:</td><td> </td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-collapse"><code>collapse</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-compile_002dtime">compile-time</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-concrete"><code>concrete</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-conj"><code>conj</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-container">container</a>:</td><td> </td><td valign="top"><a href="#Using-the-library">Using the library</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-cstyle"><code>cstyle</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-ct">ct</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-D">D</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-diag"><code>diag</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-dot"><code>dot</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-E">E</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-early"><code>early</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-elision_002c-index">elision, index</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-end"><code>end</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-end_002c-Octave_002fMatlab"><code>end</code>, Octave/Matlab</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-error">error</a>:</td><td> </td><td valign="top"><a href="#Extension">Extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-every"><code>every</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-exception">exception</a>:</td><td> </td><td valign="top"><a href="#Error-handling">Error handling</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-explode"><code>explode</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-F">F</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-FFTW">FFTW</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-foreign-type">foreign type</a>:</td><td> </td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-format_005farray"><code>format_array</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-for_005feach"><code>for_each</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-frame">frame</a>:</td><td> </td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-from"><code>from</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-G">G</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-gemm"><code>gemm</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-gemv"><code>gemv</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-gevm"><code>gevm</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Guile">Guile</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-I">I</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-imag_005fpart"><code>imag_part</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-insert"><code>insert</code></a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-iter"><code>iter</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-J">J</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-J">J</a>:</td><td> </td><td valign="top"><a href="#Why-C_002b_002b">Why C++</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-jstyle"><code>jstyle</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-L">L</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-len"><code>len</code></a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-length">length</a>:</td><td> </td><td valign="top"><a href="#Overview">Overview</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-lstyle"><code>lstyle</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-M">M</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-map"><code>map</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Masking">Masking</a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-N">N</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-none"><code>none</code></a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-none-1"><code>none</code></a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-none-2"><code>none</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-noshape"><code>noshape</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Numpy">Numpy</a>:</td><td> </td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Numpy-1">Numpy</a>:</td><td> </td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Numpy-2">Numpy</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-O">O</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-OpenGL">OpenGL</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-order_002c-column_002dmajor">order, column-major</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-order_002c-row_002dmajor">order, row-major</a>:</td><td> </td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-other-libraries_002c-interfacing-with">other libraries, interfacing with</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-overload-set">overload set</a>:</td><td> </td><td valign="top"><a href="#Extension">Extension</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-P">P</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-pack"><code>pack</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-pick"><code>pick</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-ply"><code>ply</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-prod"><code>prod</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-pstyle"><code>pstyle</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-ptr"><code>ptr</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-Python">Python</a>:</td><td> </td><td valign="top"><a href="#Using-ra_003a_003a-types-with-the-STL">Using <code>ra::</code> types with the STL</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-R">R</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-range"><code>range</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-rank">rank</a>:</td><td> </td><td valign="top"><a href="#Overview">Overview</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-rank-1"><code>rank</code></a>:</td><td> </td><td valign="top"><a href="#Introspection">Introspection</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-rank_002c-compile_002dtime">rank, compile-time</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-rank_002c-runtime">rank, runtime</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-real_005fpart"><code>real_part</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-rel_005ferror"><code>rel_error</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-reverse"><code>reverse</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-rt">rt</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-runtime">runtime</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-S">S</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-scalar"><code>scalar</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-shape">shape</a>:</td><td> </td><td valign="top"><a href="#Overview">Overview</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-shape-1"><code>shape</code></a>:</td><td> </td><td valign="top"><a href="#Introspection">Introspection</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-shape-agreement_002c-prefix">shape agreement, prefix</a>:</td><td> </td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-shape-agreement_002c-suffix">shape agreement, suffix</a>:</td><td> </td><td valign="top"><a href="#Rank-extension">Rank extension</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-size"><code>size</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-sqr"><code>sqr</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-sqrm"><code>sqrm</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-start"><code>start</code></a>:</td><td> </td><td valign="top"><a href="#Compatibility">Compatibility</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-start-1"><code>start</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-stencil"><code>stencil</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-step">step</a>:</td><td> </td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-stride">stride</a>:</td><td> </td><td valign="top"><a href="#Rank-polymorphism">Rank polymorphism</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-sum"><code>sum</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-swap"><code>swap</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-T">T</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-TensorIndex">TensorIndex</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-transpose"><code>transpose</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-U">U</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-uninitialized-container">uninitialized container</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-uninitialized-container-1">uninitialized container</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-V">V</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-view">view</a>:</td><td> </td><td valign="top"><a href="#Containers-and-views">Containers and views</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-view_002c-rank-0">view, rank 0</a>:</td><td> </td><td valign="top"><a href="#Slicing">Slicing</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-W">W</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-where"><code>where</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-withshape"><code>withshape</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td></td><td valign="top"><a href="#index-wrank"><code>wrank</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- <tr><th id="Indices_cp_letter-X">X</th><td></td><td></td></tr>
- <tr><td></td><td valign="top"><a href="#index-xi"><code>xi</code></a>:</td><td> </td><td valign="top"><a href="#Reference">Reference</a></td></tr>
- <tr><td colspan="4"> <hr></td></tr>
- </table>
- <table><tr><th valign="top">Jump to: </th><td><a class="summary-letter" href="#Indices_cp_letter-A"><b>A</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-B"><b>B</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-C"><b>C</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-D"><b>D</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-E"><b>E</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-F"><b>F</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-G"><b>G</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-I"><b>I</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-J"><b>J</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-L"><b>L</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-M"><b>M</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-N"><b>N</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-O"><b>O</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-P"><b>P</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-R"><b>R</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-S"><b>S</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-T"><b>T</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-U"><b>U</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-V"><b>V</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-W"><b>W</b></a>
-
- <a class="summary-letter" href="#Indices_cp_letter-X"><b>X</b></a>
-
- </td></tr></table>
- <hr>
- </div>
- <div class="unnumbered" id="Notes">
- <div class="header">
- <p>
- Previous: <a href="#Indices" accesskey="p" rel="prev">Indices</a>, Up: <a href="#Top" accesskey="u" rel="up"><code>ra::</code></a> [<a href="#Indices" title="Index" rel="index">Index</a>]</p>
- </div>
- <span id="Notes-1"></span><h2 class="unnumbered">Notes</h2>
- <ol>
- <li> <code>ra::</code> uses the non standard <code>#pragma once</code> (supported on all major compilers).
- </li></ol>
- </div>
- </div>
- <div class="footnote">
- <hr>
- <h4 class="footnotes-heading">Footnotes</h4>
- <h5><a id="FOOT1" href="#DOCF1">(1)</a></h5>
- <p>/ə’ɹ-eɪ/</p>
- <h5><a id="FOOT2" href="#DOCF2">(2)</a></h5>
- <span id="index-arity"></span>
- <p>This leads to ‘rank’ being called ‘arity’ in some contexts, especially when there is risk of confusion with the meaning of ‘rank’ in linear algebra.</p>
- <h5><a id="FOOT3" href="#DOCF3">(3)</a></h5>
- <p>Sometimes ‘strides’. Cf. <a href="https://en.wikipedia.org/wiki/Dope_vector"><em>dope vector</em></a></p>
- <h5><a id="FOOT4" href="#DOCF4">(4)</a></h5>
- <p>Examples given without context assume that one has declared <code>using std::cout;</code>, etc.</p>
- <h5><a id="FOOT5" href="#DOCF5">(5)</a></h5>
- <p>The brace-list constructors of rank 2 and higher aren’t supported on types of rt rank, because in the C++ grammar, a nested initializer list doesn’t always define a rank unambiguously.</p>
- <h5><a id="FOOT6" href="#DOCF6">(6)</a></h5>
- <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>
- <h5><a id="FOOT7" href="#DOCF7">(7)</a></h5>
- <p>The brace-list constructors aren’t rank extending, because giving the ravel is incompatible with rank extension. They are shape-strict —you must give every element.</p>
- <h5><a id="FOOT8" href="#DOCF8">(8)</a></h5>
- <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>]
- 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>
- <h5><a id="FOOT9" href="#DOCF9">(9)</a></h5>
- <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>
- <h5><a id="FOOT10" href="#DOCF10">(10)</a></h5>
- <p><code>&&</code>, <code>||</code> are short-circuiting as array operations; the elements of the second operand won’t be evaluated if the elements of the first one evaluate to <code>false</code> or <code>true</code>, respectively.
- 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>&&</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>
- <h5><a id="FOOT11" href="#DOCF11">(11)</a></h5>
- <p>Unqualified <code>begin()</code> would find <code>std::begin</code> through ADL since <code>Big</code> is parameterized by <code>std::vector</code>. This still works since <code>std::begin</code> forwards to <code>A.begin()</code>. It’s up to you if you want to rely on such things.</p>
- <h5><a id="FOOT12" href="#DOCF12">(12)</a></h5>
- <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>
- </div>
- <a href='js_licenses.html' rel='jslicense'><small>JavaScript license information</small></a>
- </body>
- </html>
|