ra-ra.texi 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename ra-ra.info
  4. @documentencoding UTF-8
  5. @settitle ra:: —An array library for C++17
  6. @c %**end of header
  7. @c last [ma112]
  8. @c Keep track of
  9. @c http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0834r0.html
  10. @c http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0573r2.html
  11. @c http://open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0356r2.html
  12. @set VERSION 10
  13. @set UPDATED 2019 July 18
  14. @copying
  15. @code{ra::} (version @value{VERSION}, updated @value{UPDATED})
  16. (c) Daniel Llorens 2005--2019
  17. @smalldisplay
  18. Permission is granted to copy, distribute and/or modify this document
  19. under the terms of the GNU Free Documentation License, Version 1.3 or
  20. any later version published by the Free Software Foundation; with no
  21. Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
  22. @end smalldisplay
  23. @end copying
  24. @dircategory C++ libraries
  25. @direntry
  26. * ra-ra: (ra-ra.info). Expression template and multidimensional array library for C++.
  27. @end direntry
  28. @include my-bib-macros.texi
  29. @mybibuselist{Sources}
  30. @titlepage
  31. @title ra::
  32. @subtitle version @value{VERSION}, updated @value{UPDATED}
  33. @author Daniel Llorens
  34. @page
  35. @vskip 0pt plus 1filll
  36. @insertcopying
  37. @end titlepage
  38. @ifnottex
  39. @node Top
  40. @top @code{ra::}
  41. @insertcopying
  42. @code{ra::}@footnote{/ə'ɹ-eɪ/, I guess.} is a general purpose multidimensional array and expression template library for C++17. Please keep in mind that this manual is a work in progress. There are many errors and whole sections unwritten.
  43. @menu
  44. * Overview:: Array programming and C++.
  45. * Usage:: Everything you can do with @code{ra::}.
  46. * Extras:: Additional libraries provided with @code{ra::}.
  47. * Hazards:: User beware.
  48. * Internals:: For all the world to see.
  49. * The future:: Could be even better.
  50. * Reference:: Systematic list of types and functions.
  51. * @mybibnode{}:: It's been done before.
  52. * Indices:: Or try the search function.
  53. @end menu
  54. @end ifnottex
  55. @iftex
  56. @shortcontents
  57. @end iftex
  58. @c ------------------------------------------------
  59. @node Overview
  60. @chapter Overview
  61. @c ------------------------------------------------
  62. 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 @dfn{rank} of the array, and the list (n₀, n₁, ... nᵣ₋₁) is the @dfn{shape} of the array. We speak of a rank-@math{r} array or of an @math{r}-array.
  63. Often we deal with multidimensional @emph{expressions} 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.
  64. Arrays (in the form of @dfn{matrices}, @dfn{vectors}, or @dfn{tensors}) are very common objects in math and programming, and it is enormously useful to be able to manipulate arrays as individual entities rather than as aggregates. Not only is
  65. @verbatim
  66. A = B+C;
  67. @end verbatim
  68. much more compact and easier to read than
  69. @verbatim
  70. for (int i=0; i!=m; ++i)
  71. for (int j=0; j!=n; ++j)
  72. for (int k=0; k!=p; ++k)
  73. A(i, j, k) = B(i, j, k)+C(i, j, k);
  74. @end verbatim
  75. but it's also safer and less redundant. For example, the order of the loops may be something you don't really care about.
  76. However, if array operations are implemented naively, a piece of code such as @code{A=B+C} may result in the creation of a temporary to hold @code{B+C} which is then assigned to @code{A}. Needless to say this is very wasteful if the arrays involved are large.
  77. @cindex Blitz++
  78. Fortunately the problem is almost as old as aggregate data types, and other programming languages have addressed it with optimizations such as @url{https://en.wikipedia.org/wiki/Loop_fission_and_fusion, ‘loop fusion’}, ‘drag along’ @mybibcite{Abr70}, or ‘deforestation’ @mybibcite{Wad90}. In the C++ context the technique of ‘expression templates’ was pioneered in the late 90s by libraries such as Blitz++ @mybibcite{bli17}. It works by making @code{B+C} 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} results (in principle) in the same generated code as the complicated loop nest above.
  79. @menu
  80. * Rank polymorphism:: What makes arrays special.
  81. * Drag along and beating:: The basic array optimizations.
  82. * Why C++:: High level, low level.
  83. * Guidelines:: How @code{ra::} tries to do things.
  84. * Other libraries:: Inspiration and desperation.
  85. @end menu
  86. @c ------------------------------------------------
  87. @node Rank polymorphism
  88. @section Rank polymorphism
  89. @c ------------------------------------------------
  90. @dfn{Rank polymorphism} is the ability to treat an array of rank @math{r} as an array of lower rank where the elements are themselves arrays.
  91. @cindex cell
  92. @cindex frame
  93. For example, think of a matrix A, a 2-array with sizes (n₀, n₁) where the elements A(i₀, i₁) are numbers. If we consider the subarrays (rows) A(0, ...), A(1, ...), ..., A(n₀-1, ...) as individual elements, then we have a new view of A as a 1-array of size n₀ with those rows as elements. We say that the rows A(i₀)≡A(i₀, ...) are the 1-@dfn{cells} of A, and the numbers A(i₀, i₁) are 0-cells of A. For an array of arbitrary rank @math{r} the (@math{r}-1)-cells of A are called its @dfn{items}. The prefix of the shape (n₀, n₁, ... nₙ₋₁₋ₖ) that is not taken up by the k-cell is called the k-@dfn{frame}.
  94. 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
  95. @quotation
  96. A: [A(0), A(1), ...]
  97. @end quotation
  98. and the items of A(i₀), etc. are in turn stored in the same way, so
  99. @quotation
  100. A: [A(0): [A(0, 0), A(0, 1) ...], ...]
  101. @end quotation
  102. and the same for the items of A(i₀, i₁), etc.
  103. @quotation
  104. A: [[A(0, 0): [A(0, 0, 0), A(0, 0, 1) ...], A(0, 1): [A(0, 1, 0), A(0, 1, 1) ...]], ...]
  105. @end quotation
  106. @cindex order, row-major
  107. This way to lay out an array in memory is called @dfn{row-major order} or @dfn{C-order}, since it's the default order for built-in arrays in C (@pxref{Other libraries}). A row-major array A with sizes (n₀, n₁, ... nᵣ₋₁) can be looked up like this:
  108. @anchor{x-strides}
  109. @quotation
  110. A(i₀, i₁, ...) = (storage-of-A) [(((i₀n₁ + i₁)n₂ + i₂)n₃ + ...)+iᵣ₋₁] = (storage-of-A) [o + s₀i₀ + s₁i₁ + ...]
  111. @end quotation
  112. where the numbers (s₀, s₁, ...) are called the @dfn{strides}@footnote{Cf. @url{https://en.wikipedia.org/wiki/Dope_vector, @dfn{dope vector}}}. 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
  113. @quotation
  114. A ≡ ((storage-of-A), o, (s₀, s₁, ...))
  115. @end quotation
  116. then any affine transformation of the indices can be achieved simply by modifying the numbers (o, (s₀, s₁, ...)), with no need to touch the storage. This includes very common operations such as: @ref{x-transpose,transposing} axes, @ref{x-reverse,reversing} the order along an axis, most cases of @ref{Slicing,slicing}, and sometimes even reshaping or tiling the array.
  117. A basic example is obtaining the i₀-th item of A:
  118. @quotation
  119. A(i₀) ≡ ((storage-of-A), o+s₀i₀, (s₁, ...))
  120. @end quotation
  121. 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 (@pxref{Cell iteration}).
  122. @c ------------------------------------------------
  123. @node Drag along and beating
  124. @section Drag along and beating
  125. @c ------------------------------------------------
  126. These two fundamental array optimizations are described in @mybibcite{Abr70}.
  127. @dfn{Drag-along} 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.
  128. @dfn{Beating} is the implementation of certain array operations on the array @ref{Containers and views,view} descriptor instead of on the array contents. For example, if @code{A} is a 1-array, one can implement @ref{x-reverse,@code{reverse(A, 0)}} by negating the @ref{x-strides,stride} 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 @ref{x-iota,@code{1+iota(3, 0)}} can be beaten into @code{iota(3, 1)}, and this can enable further optimizations.
  129. @c ------------------------------------------------
  130. @node Why C++
  131. @section Why C++
  132. @c ------------------------------------------------
  133. 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.
  134. 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.
  135. @cindex APL
  136. @cindex J
  137. The classic array languages, APL @mybibcite{FI73} and J @mybibcite{Ric08}, 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 @mybibcite{num17} this is both for reasons of flexibility (e.g. to obtain predictable memory layout and machine types) and of performance.
  138. 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.
  139. In C++, a library is enough.
  140. @c ------------------------------------------------
  141. @node Guidelines
  142. @section Guidelines
  143. @c ------------------------------------------------
  144. @code{ra::} attempts to be general, consistent, and transparent.
  145. @c @cindex J # TODO makeinfo can't handle an entry appearing more than once (it creates multiple entries in the index).
  146. Generality is achieved by removing arbitrary restrictions and by adopting the rank extension mechanism of J. @code{ra::} 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.
  147. 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::} offers a few different types of views and containers, but it should be possible to use them interchangeably whenever the properties that justify their existence are not involved. When this isn't possible, it's a bug. For example, it used to be the case that you couldn't create a higher rank iterator on a @code{SmallView}, even though you could do it on a @code{View}; this was a bug.
  148. Sometimes consistency requires a choice. For example, given array views A and B, @code{A=B} copies the contents of view B into view A. To change view A instead (to treat A as a pointer) would be the default meaning of A=B for C++ types, and result in better consistency with the rest of the language, but I have decided that having consistency between views and containers (which ‘are’ their contents in a sense that views aren't) is more important.
  149. Transparency is achieved by avoiding opaque types. An array view consists of a pointer and a list of strides and I see no point in hiding that. Manipulating the strides directly is often useful. A container consists of storage and a view and that isn'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.
  150. 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 sweet 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 by compact columns or that the real parts are interleaved with the imaginary parts.
  151. @c ------------------------------------------------
  152. @node Other libraries
  153. @section Other array libraries
  154. @c ------------------------------------------------
  155. 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.
  156. 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++!
  157. @subsection Standard C++
  158. The C++ language offers multidimensional arrays as a legacy feature from C, e.g. @code{int a[3][4]}. These decay to pointers when you do nearly anything with them, don't know their own sizes or rank at runtime, and are generally too limited.
  159. The C++ standard library also offers a number of contiguous storage containers that can be used as 1-arrays: @code{<array>}, @code{<vector>} and @code{<valarray>}. Neither supports higher ranks out of the box, but @code{<valarray>} offers array operations for 1-arrays. @code{ra::} makes use of @code{<array>} and @code{<vector>} for storage and bootstrapping.
  160. @code{ra::} accepts built-in arrays and standard library types as array objects (@pxref{Compatibility}).
  161. @subsection Blitz++
  162. @cindex Blitz++
  163. Blitz++ @mybibcite{bli17} pioneered the use of expression templates in C++. It supported higher rank arrays, as high as it was practical in C++98, but not dynamic rank. It also supported small arrays with compile time sizes (@code{Tiny}), and convenience features such as Fortran-order constructors and arbitrary lower bounds for the array indices (both of which @code{ra::} chooses not to support). It placed a strong emphasis on performance, with array traversal methods such as blocking, space filling curves, etc.
  164. To date it remains, I believe, one of the most general array libraries for C++. However, the implementation had to fight the limitations of C++98, and it offered no general rank extension mechanism.
  165. One important difference between Blitz++ and @code{ra::} is that Blitz++'s arrays were reference counted. @code{ra::} doesn't do any memory management on its own: the default container types are explicitly values (data-owning) or views. You can select your own storage for the data-owning objects, including reference-counted storage (@code{ra::} declares a type using @code{std::shared_ptr}), but this is not the default.
  166. @subsection Other C++ libraries
  167. I guess this is important enough!
  168. @subsection Other languages
  169. TODO Maybe review other languages, at least the big ones (Fortran/APL/J/Matlab/Numpy).
  170. @c ------------------------------------------------
  171. @node Usage
  172. @chapter Usage
  173. @c ------------------------------------------------
  174. This is an extended exposition of the features of @code{ra::} and is probably best read in order. For details on specific functions or types, please @pxref{Reference}.
  175. @menu
  176. * Using the library:: @code{ra::} is a header-only library.
  177. * Containers and views:: Data objects.
  178. * Array operations:: Building and traversing expressions.
  179. * Rank extension:: How array operands are matched.
  180. * Cell iteration:: At any rank.
  181. * Slicing:: Subscripting is a special operation.
  182. * Special objects:: Not arrays, yet arrays.
  183. * The rank conjunction:: J comes to C++.
  184. * Compatibility:: With the STL and other libraries.
  185. * Extension:: Using your own types and more.
  186. * Functions:: Ready to go.
  187. * Error handling:: What to check and what to do.
  188. @end menu
  189. @c ------------------------------------------------
  190. @node Using the library
  191. @section Using @code{ra::}
  192. @c ------------------------------------------------
  193. @code{ra::} is a header only library with no dependencies, so you just need to place the @samp{ra/} folder somewhere in your include path and add @code{#include "ra/ra.H"} at the top of your sources.
  194. A compiler with C++17 support is required. At the time of writing this means @b{gcc 8.0} or @b{clang-5.0} with @option{-std=c++17}. Check the top README.md for more up-to-date information.
  195. Here is a minimal program@footnote{Examples given without context assume that one has declared @code{using std::cout;}, etc.}:
  196. @example @c readme.C [ma101]
  197. @verbatim
  198. #include "ra/ra.H"
  199. #include <iostream>
  200. int main()
  201. {
  202. ra::Big<char, 2> A({2, 5}, "helloworld");
  203. std::cout << ra::noshape << format_array(transpose<1, 0>(A), "|") << std::endl;
  204. }
  205. @end verbatim
  206. @print{} h|w
  207. e|o
  208. l|r
  209. l|l
  210. d|d
  211. @end example
  212. You may want to @code{#include "ra/real.H"} and @code{"ra/complex.H"}. These put some functions in the global namespace that make it easier to work on built-in scalar types or array expressions indistinctly. They are not required for the rest of the library to function.
  213. @cindex container
  214. @c ------------------------------------------------
  215. @node Containers and views
  216. @section Containers and views
  217. @c ------------------------------------------------
  218. @code{ra::} offers two kinds of data objects. The first kind, the @dfn{container}, owns its data. Creating a container requires memory and destroying it causes that memory to be freed.
  219. There are three kinds of containers: static size, static rank/dynamic size, and dynamic rank. Here static means ‘compile time constant’ while dynamic means ‘run time constant’. Some dynamic size arrays can be resized but dynamic rank arrays cannot normally have their rank changed. Instead, you create a new container or view with the rank you want.
  220. For example:
  221. @example
  222. @verbatim
  223. {
  224. ra::Small<double, 2, 3> a(0.); // a static size 2x3 array
  225. ra::Big<double, 2> b({2, 3}, 0.); // a dynamic size 2x3 array
  226. ra::Big<double> c({2, 3}, 0.); // a dynamic rank 2x3 array
  227. // a, b, c destroyed at end of scope
  228. }
  229. @end verbatim
  230. @end example
  231. The main reason to have all these different types is performance; the compiler can do a better job when it knows the size or the rank of the array. Also, the sizes of a static size array do not need to be stored in memory, so when you have thousands of small arrays it pays off to use the static size types. Static size or static rank arrays are also safer to use; sometimes @code{ra::} will be able to detect errors in the sizes or ranks of array operands at compile time, if the appropriate types are used.
  232. 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.@footnote{The brace-list constructors of rank 2 and higher aren't supported on types of runtime rank, because in the C++ grammar, a nested initializer list doesn't always define a rank unambiguously.}
  233. @c [ma111]
  234. @example
  235. @verbatim
  236. using ra::Small, ra::Big;
  237. Small<int, 2, 2> a = {{1, 2}, {3, 4}}; // explicit contents
  238. Big<int, 2> a1 = {{1, 2}, {3, 4}}; // explicit contents
  239. Small<int, 2, 2> a2 = {{1, 2}}; // error: bad size
  240. Small<int, 2, 2> b = 7; // 7 is copied into b
  241. Small<int, 2, 2> c = a; // the contents of a are copied into c
  242. Big<int> d = a; // d takes the shape of a and a is copied into d
  243. Big<int> e = 0; // e is a 0-array with one element f()==0.
  244. @end verbatim
  245. @end example
  246. The second form takes two arguments, one giving the shape, the second the contents.
  247. @cindex @code{none}
  248. @example
  249. @verbatim
  250. ra::Big<double, 2> a({2, 3}, 1.); // a has size 2x3 and be filled with 1.
  251. ra::Big<double> b({2, 3}, ra::none); // b has size 2x3 and contents don't matter
  252. ra::Big<double> c({2, 3}, a); // c has size 2x3 and a is copied into c
  253. @end verbatim
  254. @end example
  255. The last example may result in an error if the shape of @code{a} and (2,@w{ }3) don't match. Here the shape of @code{1.} [which is ()] matches (2,@w{ }3) by a mechanism of rank extension (@pxref{Rank extension}). The special value @code{ra::none} can be used to request @url{https://en.cppreference.com/w/cpp/language/default_initialization, default initialization} of the container's elements.
  256. When the content argument is a pointer or a 1D brace list, it's handled especially, not for shape@footnote{You can still use pointers or @code{std::initializer_list}s for shape by wrapping them in the functions @code{ptr} or @code{vector}, respectively.}, but only as the (row-major) ravel of the content. The pointer constructor is unsafe —use at your own risk!@footnote{The brace-list constructors aren't rank extending, because giving the ravel is incompatible with rank extension. They are size-strict —you must give every element.}
  257. @cindex order, column-major
  258. @example
  259. @verbatim
  260. Small<int, 2, 2> aa = {1, 2, 3, 4}; // ravel of the content
  261. ra::Big<double, 2> a({2, 3}, {1, 2, 3, 4, 5, 6}); // same as a = {{1, 2, 3}, {4, 5, 6}}
  262. @end verbatim
  263. @end example
  264. @c [ma112]
  265. @example
  266. @verbatim
  267. double bx[6] = {1, 2, 3, 4, 5, 6}
  268. ra::Big<double, 2> b({3, 2}, bx); // {{1, 2}, {3, 4}, {5, 6}}
  269. double cx[4] = {1, 2, 3, 4}
  270. ra::Big<double, 2> c({3, 2}, cx); // *** WHO NOSE ***
  271. @end verbatim
  272. @end example
  273. @c [ma114]
  274. @example
  275. @verbatim
  276. using sizes = mp::int_list<2, 3>;
  277. using strides = mp::int_list<1, 2>;
  278. ra::SmallArray<double, sizes, strides> a {{1, 2, 3}, {4, 5, 6}}; // stored column-major: 1 4 2 5 3 6
  279. @end verbatim
  280. @end example
  281. These are compile time errors:
  282. @example
  283. @verbatim
  284. Big<int, 2> b = {1, 2, 3, 4}; // error: shape cannot be deduced from ravel
  285. Small<int, 2, 2> b = {1, 2, 3, 4 5}; // error: bad size
  286. Small<int, 2, 2> b = {1, 2, 3}; // error: bad size
  287. @end verbatim
  288. @end example
  289. @anchor{x-scalar-char-star}
  290. Sometimes the pointer constructor gets in the way (see @ref{x-scalar,@code{scalar}}): @c [ma102]
  291. @example
  292. @verbatim
  293. ra::Big<char const *, 1> A({3}, "hello"); // error: try to convert char to char const *
  294. ra::Big<char const *, 1> A({3}, ra::scalar("hello")); // ok, "hello" is a single item
  295. cout << ra::noshape << format_array(A, "|") << endl;
  296. @end verbatim
  297. @print{} hello|hello|hello
  298. @end example
  299. @cindex view
  300. A @dfn{view} 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:
  301. @example
  302. @verbatim
  303. ra::Big<double> c({2, 3}, 0.); // a dynamic rank 2x3 array
  304. {
  305. auto c1 = c(1); // the second row of array c
  306. // c1 is destroyed here
  307. }
  308. cout << c(1, 1) << endl; // ok
  309. @end verbatim
  310. @end example
  311. The data accessed through a view is the data of the ‘root’ container, so modifying the former will be reflected in the latter.
  312. @example
  313. @verbatim
  314. ra::Big<double> c({2, 3}, 0.);
  315. auto c1 = c(1);
  316. c1(2) = 9.; // c(1, 2) = 9.
  317. @end verbatim
  318. @end example
  319. Just as for containers, there are separate types of views depending on whether the size is known at compile time, the rank is known at compile time but the size is not, or neither the size nor the rank are known at compile time. @code{ra::} has functions to create the most common kinds of views:
  320. @example
  321. @verbatim
  322. ra::Big<double> c {{1, 2, 3}, {4, 5, 6}};
  323. auto ct = transpose<1, 0>(c); // {{1, 4}, {2, 5}, {3, 6}}
  324. auto cr = reverse(c, 0); // {{4, 5, 6}, {1, 2, 3}}
  325. @end verbatim
  326. @end example
  327. However, views can point to anywhere in memory and that memory doesn't have to belong to an @code{ra::} container. For example:
  328. @example
  329. @verbatim
  330. int raw[6] = {1, 2, 3, 4, 5, 6};
  331. ra::View<int> v1({{2, 3}, {3, 1}}, raw); // view with sizes {2, 3} strides {3, 1}
  332. ra::View<int> v2({2, 3}, raw); // same, default C (row-major) strides
  333. @end verbatim
  334. @end example
  335. Containers can be treated as views of the same ‘dynamicness’. If you declare a function
  336. @example
  337. @verbatim
  338. void f(ra::View<int, 3> & v);
  339. @end verbatim
  340. @end example
  341. you may pass it an object of type @code{ra::Big<int, 3>}.
  342. @c ------------------------------------------------
  343. @node Array operations
  344. @section Array operations
  345. @c ------------------------------------------------
  346. To apply an operation to each element of an array, use the function @code{for_each}. The array is traversed in an order that is decided by the library.
  347. @example
  348. @verbatim
  349. ra::Small<double, 2, 3> a = {{1, 2, 3}, {4, 5, 6}};
  350. double s = 0.;
  351. for_each([&s](auto && a) { s+=a; }, a);
  352. @end verbatim
  353. @result{} s = 21.
  354. @end example
  355. To construct an array expression but stop short of traversing it, use the function @code{map}. The expression will be traversed when it is assigned to a view, printed out, etc.
  356. @example
  357. @verbatim
  358. using T = ra::Small<double, 2, 2>;
  359. T a = {{1, 2}, {3, 4}};
  360. T b = {{10, 20}, {30, 40}};
  361. T c = map([](auto && a, auto && b) { return a+b; }, a, b); // (1)
  362. @end verbatim
  363. @result{} c = @{@{11, 22@}, @{33, 44@}@}
  364. @end example
  365. Expressions may take any number of arguments and be nested arbitrarily.
  366. @example
  367. @verbatim
  368. T d = 0;
  369. for_each([](auto && a, auto && b, auto && d) { d = a+b; },
  370. a, b, d); // same as (1)
  371. for_each([](auto && ab, auto && d) { d = ab; },
  372. map([](auto && a, auto && b) { return a+b; },
  373. a, b),
  374. d); // same as (1)
  375. @end verbatim
  376. @end example
  377. The operator of an expression may return a reference and you may assign to an expression in that case. @code{ra::} will complain if the expression is somehow not assignable.
  378. @example
  379. @verbatim
  380. T d = 0;
  381. map([](auto & d) -> decltype(auto) { return d; }, d) // just pass d along
  382. = map([](auto && a, auto && b) { return a+b; }, a, b); // same as (1)
  383. @end verbatim
  384. @end example
  385. @code{ra::} defines many shortcuts for common array operations. You can of course just do:
  386. @example
  387. @verbatim
  388. T c = a+b; // same as (1)
  389. @end verbatim
  390. @end example
  391. @c ------------------------------------------------
  392. @node Rank extension
  393. @section Rank extension
  394. @c ------------------------------------------------
  395. Rank extension is the mechanism that allows @code{R+S} to be defined even when @code{R}, @code{S} may have different ranks. The idea is an interpolation of the following basic cases.
  396. Suppose first that @code{R} and @code{S} have the same rank. We require that the shapes be the same. Then the shape of @code{R+S} will be the same as the shape of either @code{R} or @code{S} and the elements of @code{R+S} will be
  397. @quotation
  398. @code{(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ᵣ₋₁₎)}
  399. @end quotation
  400. where @code{r} is the rank of @code{R}.
  401. Now suppose that @code{S} has rank 0. The shape of @code{R+S} is the same as the shape of @code{R} and the elements of @code{R+S} will be
  402. @quotation
  403. @code{(R+S)(i₀ i₁ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ᵣ₋₁₎) + S()}.
  404. @end quotation
  405. The two rules above are supported by all primitive array languages, e.g. Matlab @mybibcite{Mat}. But suppose that @code{S} has rank @code{s}, where @code{0<s<r}. Looking at the expressions above, it seems natural to define @code{R+S} by
  406. @quotation
  407. @code{(R+S)(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) = R(i₀ i₁ ... i₍ₛ₋₁₎ ... i₍ᵣ₋₁₎) + S(i₀ i₁ ... i₍ₛ₋₁₎)}.
  408. @end quotation
  409. That is, after we run out of indices in @code{S}, we simply repeat the elements. We have aligned the shapes so:
  410. @quotation
  411. @verbatim
  412. [n₀ n₁ ... n₍ₛ₋₁₎ ... n₍ᵣ₋₁₎]
  413. [n₀ n₁ ... n₍ₛ₋₁₎]
  414. @end verbatim
  415. @end quotation
  416. @cindex shape agreement, prefix
  417. @cindex shape agreement, suffix
  418. @c @cindex J
  419. @cindex Numpy
  420. This rank extension rule is used by the J language @mybibcite{J S} and is known as @dfn{prefix agreement}. The opposite rule of @dfn{suffix agreement} is used, for example, in Numpy @mybibcite{num17}@footnote{Prefix agreement is chosen for @code{ra::} because of the availability of a @ref{The rank conjunction,rank conjunction} @mybibcite{Ber87} and @ref{Cell iteration, cell iterators of arbitrary rank}. This allows rank extension to be performed at multiple axes of an array expression.}.
  421. 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::}, even in assignments. For example,
  422. @example
  423. @verbatim
  424. ra::Small<int, 3> x {3, 5, 9};
  425. ra::Small<int, 3, 2> a = x; // assign x(i) to each a(i, j)
  426. @end verbatim
  427. @result{} a = @{@{3, 3@}, @{5, 5@}, @{9, 9@}@}
  428. @end example
  429. @example
  430. @verbatim
  431. ra::Small<int, 3> x(0.);
  432. ra::Small<int, 3, 2> a = {{1, 2}, {3, 4}, {5, 6}};
  433. x += a; // sum the rows of a
  434. @end verbatim
  435. @result{} x = @{3, 7, 11@}
  436. @end example
  437. @example
  438. @verbatim
  439. ra::Big<double, 3> a({5, 3, 3}, ra::_0);
  440. ra::Big<double, 1> b({5}, 0.);
  441. b += transpose<0, 1, 1>(a); // b(i) = ∑ⱼ a(i, j, j)
  442. @end verbatim
  443. @result{} b = @{0, 3, 6, 9, 12@}
  444. @end example
  445. @cindex Numpy
  446. @cindex broadcasting, singleton, newaxis
  447. 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 @mybibcite{num17}, an array of shape [A B 1 D] will match an array of shape [A B C D]. The process of broadcasting consists in inserting so-called ‘singleton dimensions’ (axes with size one) to align the axes that one wishes to match. You can think of prefix agreement as a particular case of broadcasting where the singleton dimensions are added to the end of the shorter shapes automatically.
  448. A drawback of singleton broadcasting is that it muddles the distinction between a scalar and a vector of size 1. Sometimes, an axis of size 1 is no more than that, and if 2≠3 is a size error, it isn't obvious why 1≠2 shouldn't be. To avoid this problem, @code{ra::} supports broadcasting with undefined size axes (see @ref{x-insert,@code{insert}}).
  449. @example
  450. @verbatim
  451. ra::Big<double, 3> a({5, 3}, ra::_0);
  452. ra::Big<double, 1> b({3}, 0.);
  453. ra::Big<double, 3> c({1, 3}, ra::_0);
  454. // b(?, i) += a(j, i) → b(i) = ∑ⱼ a(j, i) (sum columns)
  455. b(ra::insert<1>) += a;
  456. c = a; // 1 ≠ 5, still an agreement error
  457. @end verbatim
  458. @end example
  459. Still another way to align array axes is provided by the @ref{The rank conjunction,rank conjunction}.
  460. Even with axis insertion, it is still necessary that the axes one wishes to match are in the same order in all the arguments.
  461. @ref{x-transpose,Transposing} the axes before extension is a possible workaround.
  462. @c ------------------------------------------------
  463. @node Cell iteration
  464. @section Cell iteration
  465. @c ------------------------------------------------
  466. @code{map} and @code{for_each} 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:
  467. @example
  468. @verbatim
  469. ra::Big<double, 3> a({5, 4, 3}, ra::_0);
  470. for_each([](auto && b) { /* b has shape (5 4 3) */ }, iter<3>(a));
  471. for_each([](auto && b) { /* b has shape (4 3) */ }, iter<2>(a));
  472. for_each([](auto && b) { /* b has shape (3) */ }, iter<1>(a));
  473. for_each([](auto && b) { /* b has shape () */ }, iter<0>(a)); // elements
  474. for_each([](auto && b) { /* b has shape () */ }, a); // same as iter<0>(a); default
  475. @end verbatim
  476. @end example
  477. One may specify the @emph{frame} rank instead:
  478. @example
  479. @verbatim
  480. for_each([](auto && b) { /* b has shape () */ }, iter<-3>(a)); // same as iter<0>(a)
  481. for_each([](auto && b) { /* b has shape (3) */ }, iter<-2>(a)); // same as iter<1>(a)
  482. for_each([](auto && b) { /* b has shape (4 3) */ }, iter<-1>(a)); // same as iter<2>(a)
  483. @end verbatim
  484. @end example
  485. In this way it is possible to match shapes in various ways. Compare
  486. @example
  487. @verbatim
  488. ra::Big<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
  489. ra::Big<double, 1> b = {10, 20};
  490. ra::Big<double, 2> c = a * b; // multiply (each item of a) by (each item of b)
  491. @end verbatim
  492. @result{} a = @{@{10, 20, 30@}, @{80, 100, 120@}@}
  493. @end example
  494. with
  495. @example @c [ma105]
  496. @verbatim
  497. ra::Big<double, 2> a = {{1, 2, 3}, {4, 5, 6}};
  498. ra::Big<double, 1> b = {10, 20, 30};
  499. ra::Big<double, 2> c({2, 3}, 0.);
  500. iter<1>(c) = iter<1>(a) * iter<1>(b); // multiply (each item of a) by (b)
  501. @end verbatim
  502. @result{} a = @{@{10, 40, 90@}, @{40, 100, 180@}@}
  503. @end example
  504. Note that in this case we cannot construct @code{c} directly from @code{iter<1>(a) * iter<1>(b)}, since the constructor for @code{ra::Big} matches its argument using (the equivalent of) @code{iter<0>(*this)}. See @ref{x-iter,@code{iter}} for more examples.
  505. 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{*} above, there may be faster ways to rearrange shapes for matching (@pxref{The rank conjunction}).
  506. FIXME More examples.
  507. @c ------------------------------------------------
  508. @node Slicing
  509. @section Slicing
  510. @c ------------------------------------------------
  511. 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::} supports.
  512. The form of the scripting operator @code{A(i₀, i₁, ...)} makes it plain that @code{A} is a function of @code{rank(A)} integer arguments. An array extension is immediately available through @code{map}. For example:
  513. @example
  514. @verbatim
  515. ra::Big<double, 1> a = {1., 2., 3., 4.};
  516. ra::Big<int, 1> i = {1, 3};
  517. map(a, i) = 77.;
  518. @end verbatim
  519. @result{} a = @{1., 77., 3, 77.@}
  520. @end example
  521. Just as with any use of @code{map}, array arguments are subject to the prefix agreement rule.
  522. @example
  523. @verbatim
  524. ra::Big<double, 2> a({2, 2}, {1., 2., 3., 4.});
  525. ra::Big<int, 1> i = {1, 0};
  526. ra::Big<double, 1> b = map(a, i, 0);
  527. @end verbatim
  528. @result{} b = @{3., 1.@} // @{a(1, 0), a(0, 0)@}
  529. @end example
  530. @example
  531. @verbatim
  532. ra::Big<int, 1> j = {0, 1};
  533. b = map(a, i, j);
  534. @end verbatim
  535. @result{} b = @{3., 2.@} // @{a(1, 0), a(0, 1)@}
  536. @end example
  537. The latter is a form of sparse subscripting.
  538. Most array operations (e.g. @code{+}) are defined through @code{map} in this way. For example, @code{A+B+C} is defined as @code{map(+, A, B, C)} (or the equivalent @code{map(+, map(+, A, B), C)}). Not so for the subscripting operation:
  539. @example
  540. @verbatim
  541. ra::Big<double, 2> A {{1., 2.}, {3., 4.}};
  542. ra::Big<int, 1> i = {1, 0};
  543. ra::Big<int, 1> j = {0, 1};
  544. // {{A(i₀, j₀), A(i₀, j₁)}, {A(i₁, j₀), A(i₁, j₁)}}
  545. ra::Big<double, 2> b = A(i, j);
  546. @end verbatim
  547. @result{} b = @{@{3., 4.@}, @{1., 2.@}@}
  548. @end example
  549. @code{A(i, j, ...)} is defined as the @emph{outer product} of the indices @code{(i, j, ...)} with operator @code{A}, because this operation sees much more use in practice than @code{map(A, i, j ...)}.
  550. Besides, when the subscripts @code{i, j, ...} are scalars or @dfn{linear ranges} (integer sequences of the form @code{(o, o+s, ..., o+s*(n-1))}), the subscripting can be performed inmediately at constant cost and without needing to construct an expression object. This optimization is called @ref{Drag along and beating,@dfn{beating}}.
  551. @code{ra::} isn't smart enough to know when an arbitrary expression might be a linear range, so the following special objects are provided:
  552. @anchor{x-iota}
  553. @deffn @w{Special object} iota count [start:0 [step:1]]
  554. Create a linear range @code{start, start+step, ... start+step*(count-1)}.
  555. @end deffn
  556. This can used anywhere an array expression is expected.
  557. @example
  558. @verbatim
  559. ra::Big<int, 1> a = ra::iota(4, 3 -2);
  560. @end verbatim
  561. @result{} a = @{3, 1, -1, -3@}
  562. @end example
  563. Here, @code{b} and @code{c} are @code{View}s (@pxref{Containers and views}).
  564. @example
  565. @verbatim
  566. ra::Big<int, 1> a = {1, 2, 3, 4, 5, 6};
  567. auto b = a(iota(3));
  568. auto c = a(iota(3, 3));
  569. @end verbatim
  570. @result{} a = @{1, 2, 3@}
  571. @result{} a = @{4, 5, 6@}
  572. @end example
  573. @deffn @w{Special object} all
  574. Create a linear range @code{0, 1, ... (nᵢ-1)} when used as a subscript for the @code{i}-th argument of a subscripting expression.
  575. @end deffn
  576. This object cannot stand alone as an array expression. All the examples below result in @code{View} objects:
  577. @example
  578. @verbatim
  579. ra::Big<int, 2> a({3, 2}, {1, 2, 3, 4, 5, 6});
  580. auto b = a(ra::all, ra::all); // (1) a view of the whole of a
  581. auto c = a(iota(3), iota(2)); // same as (1)
  582. auto d = a(iota(3), ra::all); // same as (1)
  583. auto e = a(ra:all, iota(2)); // same as (1)
  584. auto f = a(0, ra::all); // first row of a
  585. auto g = a(ra::all, 1); // second column of a
  586. @end verbatim
  587. @end example
  588. @code{all} is a special case (@code{dots<1>}) of the more general object @code{dots}.
  589. @deffn @w{Special object} dots<n>
  590. Equivalent to as many instances of @code{ra::all} as indicated by @code{n}, which must not be negative. Each instance takes the place of one argument to the subscripting operation.
  591. @end deffn
  592. This object cannot stand alone as an array expression. All the examples below result in @code{View} objects:
  593. @example
  594. @verbatim
  595. auto h = a(ra::all, ra::all); // same as (1)
  596. auto i = a(ra::all, ra::dots<1>); // same as (1)
  597. auto j = a(ra::dots<2>); // same as (1)
  598. auto k = a(ra::dots<0>, ra::dots<2>); // same as (1)
  599. auto l = a(0, ra::dots<1>); // first row of a
  600. auto m = a(ra::dots<1>, 1); // second column of a
  601. @end verbatim
  602. @end example
  603. This is useful when writing rank-generic code, see @code{examples/maxwell.C} in the distribution for an example.
  604. @anchor{x-insert}
  605. @deffn @w{Special object} insert<n>
  606. Inserts @code{n} new axes at the subscript position. @code{n} must not be negative.
  607. @end deffn
  608. The new axes have stride 0 and undefined size, so they will match any size on those axes by repeating items. @code{insert} objects cannot stand alone as an array expression. The examples below result in @code{View} objects:
  609. @example
  610. @verbatim
  611. auto h = a(insert<0>); // same as (1)
  612. auto k = a(insert<1>); // shape [undefined, 3, 2]
  613. @end verbatim
  614. @end example
  615. @cindex broadcasting, singleton, Numpy
  616. @code{insert<n>} main use is to prepare arguments for broadcasting. In other array systems (e.g. Numpy) broadcasting is done with singleton dimensions, that is, dimensions of size one match dimensions of any size. In @code{ra::} singleton dimensions aren't special, so broadcasting requires the use of @code{insert}. For example: @c [ma115]
  617. @example
  618. @verbatim
  619. ra::Big<int, 1> x = {1, 10};
  620. // match shapes [2, U, U] with [U, 3, 2] to produce [2, 3, 2]
  621. cout << x(ra::all, ra::insert<2>) * a(insert<1>) << endl;
  622. @end verbatim
  623. @print{} 2 3 2
  624. 1 2
  625. 3 4
  626. 5 6
  627. 10 20
  628. 30 40
  629. 50 60
  630. @end example
  631. Here's a way to perform the outer product of two @code{Views} of static rank (but see @ref{x-from,@code{from}} for a more general way):
  632. @example
  633. @verbatim
  634. cout << (a(ra::dots<a.rank()>, ra::insert<b.rank()>) * b(ra::insert<a.rank()>, ra::dots<b.rank()>)) << endl;
  635. // same thing by prefix matching
  636. cout << (a * b(ra::insert<a.rank()>)) << endl;
  637. @end verbatim
  638. @end example
  639. In addition to the special objects listed above, you can also omit any trailing @code{ra::all} subscripts. For example:
  640. @example
  641. @verbatim
  642. ra::Big<int, 3> a({2, 2, 2}, {1, 2, 3, 4, 5, 6, 7, 8});
  643. auto a0 = a(0); // same as a(0, ra::all, ra::all)
  644. auto a10 = a(1, 0); // same as a(1, 0, ra::all)
  645. @end verbatim
  646. @result{} a0 = @{@{1, 2@}, @{3, 4@}@}
  647. @result{} a10 = @{5, 6@}
  648. @end example
  649. This supports the notion (@pxref{Rank polymorphism}) 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 (@pxref{Rank extension}).
  650. @c ------------------------------------------------
  651. @node Special objects
  652. @section Special objects
  653. @c ------------------------------------------------
  654. @deffn @w{Special object} TensorIndex<n, integer_type=ra::dim_t>
  655. @code{TensorIndex<n>} represents the @code{n}-th index of an array expression. @code{TensorIndex<n>} is itself an array expression of rank @code{n}-1 and size undefined. It must be used with other terms whose dimensions are defined, so that the overall shape of the array expression can be determined.
  656. @code{ra::} offers the shortcut @code{ra::_0} for @code{ra::TensorIndex<0>@{@}}, etc.
  657. @end deffn
  658. @example
  659. @verbatim
  660. ra::Big<int, 1> v = {1, 2, 3};
  661. cout << (v - ra::_0) << endl; // { 1-0, 2-1, 3-2 }
  662. // cout << (ra::_0) << endl; // error: TensorIndex cannot drive array expression
  663. // cout << (v - ra::_1) << endl; // error: TensorIndex cannot drive array expression
  664. ra::Big<int, 2> a({3, 2}, 0);
  665. cout << (a + ra::_0 - ra::_1) << endl; // {{0, -1, -2}, {1, 0, -1}, {2, 1, 0}}
  666. @end verbatim
  667. @end example
  668. @c FIXME the rest
  669. @c ------------------------------------------------
  670. @node The rank conjunction
  671. @section The rank conjunction
  672. @c ------------------------------------------------
  673. We have seen in @ref{Cell iteration} that it is possible to treat an r-array as an array of lower rank with subarrays as its elements. With the @ref{x-iter,@code{iter<cell rank>}} 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.
  674. @example
  675. @verbatim
  676. for_each(my_sort, iter<1>(A)); // (in ra::) my_sort is a regular function, cell rank must be given
  677. for_each(my_sort, iter<0>(A)); // (in ra::) error, bad cell rank
  678. @end verbatim
  679. @end example
  680. @c @cindex J
  681. The array language J has instead the concept of @dfn{verb rank}. Every function (or @dfn{verb}) has an associated cell rank for each of its arguments. Therefore @code{iter<cell rank>} is not needed.
  682. @example
  683. @verbatim
  684. for_each(sort_rows, A); // (not in ra::) will iterate over 1-cells of A, sort_rows knows
  685. @end verbatim
  686. @end example
  687. @c @cindex J
  688. @code{ra::} doesn't have ‘verb ranks’ yet. In practice one can think of @code{ra::}'s operations as having a verb rank of 0. However, @code{ra::} supports a limited form of J's @dfn{rank conjunction} with the function @ref{x-wrank,@code{wrank}}.
  689. @c @cindex J
  690. This is an operator that takes one verb (such operators are known as @dfn{adverbs} 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.
  691. A standard example is ‘outer product’.
  692. @example
  693. @verbatim
  694. ra::Big<int, 1> a = {1, 2, 3};
  695. ra::Big<int, 1> b = {40, 50};
  696. ra::Big<int, 2> axb = map(ra::wrank<0, 1>([](auto && a, auto && b) { return a*b; }),
  697. a, b)
  698. @end verbatim
  699. @result{} axb = @{@{40, 80, 120@}, @{50, 100, 150@}@}
  700. @end example
  701. It works like this. The verb @code{ra::wrank<0, 1>([](auto && a, auto && b) @{ return a*b; @})} has verb ranks (0, 1), so the 0-cells of @code{a} are paired with the 1-cells of @code{b}. In this case @code{b} has a single 1-cell. The frames and the cell shapes of each operand are:
  702. @example
  703. @verbatim
  704. a: 3 |
  705. b: | 2
  706. @end verbatim
  707. @end example
  708. Now the frames are rank-extended through prefix agreement.
  709. @example
  710. @verbatim
  711. a: 3 |
  712. b: 3 | 2
  713. @end verbatim
  714. @end example
  715. Now we need to perform the operation on each cell. The verb @code{[](auto && a, auto && b) @{ return a*b; @}} has verb ranks (0, 0). This results in the 0-cells of @code{a} (which have shape ()) being rank-extended to the shape of the 1-cells of @code{b} (which is (2)).
  716. @example
  717. @verbatim
  718. a: 3 | 2
  719. b: 3 | 2
  720. @end verbatim
  721. @end example
  722. This use of the rank conjunction is packaged in @code{ra::} as the @ref{x-from,@code{from}} operator. It supports any number of arguments, not only two.
  723. @example
  724. @verbatim
  725. ra::Big<int, 1> a = {1, 2, 3};
  726. ra::Big<int, 1> b = {40, 50};
  727. ra::Big<int, 2> axb = from([](auto && a, auto && b) { return a*b; }), a, b)
  728. @end verbatim
  729. @result{} axb = @{@{40, 80, 120@}, @{50, 100, 150@}@}
  730. @end example
  731. 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.
  732. @example
  733. @verbatim
  734. C: m | | n
  735. A: m | p |
  736. B: | p | n
  737. @end verbatim
  738. @end example
  739. First we'll align the first axes of C and A using the cell ranks (1, 1, 2). The cell shapes are:
  740. @example
  741. @verbatim
  742. C: m | n
  743. A: m | p
  744. B: | p n
  745. @end verbatim
  746. @end example
  747. Then we'll use the ranks (1, 0, 1) on the cells:
  748. @example
  749. @verbatim
  750. C: m | | n
  751. A: m | p |
  752. B: | p | n
  753. @end verbatim
  754. @end example
  755. The final operation is a standard operation on arrays of scalars. In actual @code{ra::} syntax:
  756. @example @c [ma103]
  757. @verbatim
  758. ra::Big A({3, 2}, {1, 2, 3, 4, 5, 6});
  759. ra::Big B({2, 3}, {7, 8, 9, 10, 11, 12});
  760. ra::Big C({3, 3}, 0.);
  761. for_each(ra::wrank<1, 1, 2>(ra::wrank<1, 0, 1>([](auto && c, auto && a, auto && b) { c += a*b; })), C, A, B);
  762. @end verbatim
  763. @result{} C = @{@{27, 30, 33@}, @{61, 68, 75@}, @{95, 106, 117@}@}
  764. @end example
  765. Note that @code{wrank} cannot be used to transpose axes in general.
  766. I hope that in the future something like @code{C(i, j) += A(i, k)*B(k, j)}, where @code{i, j, k} are special objects, can be automatically translated to the requisite combination of @code{wrank} and perhaps also @ref{x-transpose,@code{transpose}}. For the time being, you have to align or transpose the axes yourself.
  767. @c ------------------------------------------------
  768. @node Compatibility
  769. @section Compatibility
  770. @c ------------------------------------------------
  771. @subsection Using other C and C++ types with @code{ra::}
  772. @cindex foreign type
  773. @code{ra::} accepts certain types from outside @code{ra::} (@dfn{foreign types}) as array expressions. Generally it is enough to mix the foreign type with a type from @code{ra::} and let deduction work.
  774. @example
  775. @verbatim
  776. std::vector<int> x = {1, 2, 3};
  777. ra::Small<int, 3> y = {6, 5, 4};
  778. cout << (x-y) << endl;
  779. @end verbatim
  780. @print{} -5 -3 -1
  781. @end example
  782. @cindex @code{start}
  783. Foreign types can be brought into @code{ra::} explicitly with the function @ref{x-start,@code{start}}.
  784. @example
  785. @verbatim
  786. std::vector<int> x = {1, 2, 3};
  787. // cout << sum(x) << endl; // error, sum not found
  788. cout << sum(ra::start(x)) << endl;
  789. cout << ra::sum(x) << endl;
  790. @end verbatim
  791. @print{} 6
  792. 6
  793. @end example
  794. The following types are accepted as foreign types:
  795. @itemize
  796. @item @code{std::vector}
  797. produces an expression of rank 1 and dynamic size.
  798. @item @code{std::array}
  799. produces an expression of rank 1 and static size.
  800. @item Built-in arrays @cindex built-in array
  801. produce an expression of positive rank and static size.
  802. @item Raw pointers
  803. produce an expression of rank 1 and @emph{undefined} size. Raw pointers must be brought into @code{ra::} explicitly with the function @ref{x-ptr,@code{ptr}}.
  804. @end itemize
  805. Compare:
  806. @example @c [ma106]
  807. @verbatim
  808. int p[] = {1, 2, 3};
  809. int * z = p;
  810. ra::Big<int, 1> q {1, 2, 3};
  811. q += p; // ok, q is ra::, p is foreign object with size info
  812. ra::start(p) += q; // can't redefine operator+=(int[]), foreign needs ra::start()
  813. // z += q; // error: raw pointer needs ra::ptr()
  814. ra::ptr(z) += p; // ok, size is determined by foreign object p
  815. @end verbatim
  816. @end example
  817. @anchor{x-is-scalar}
  818. Some types are accepted automatically as scalars. These include:
  819. @itemize
  820. @item
  821. Any type @code{T} for which @code{std::is_scalar_v<T>} is true, @emph{except} pointers. These include @code{char}, @code{int}, @code{double}, etc.
  822. @item
  823. @code{std::complex<T>}, if you import @code{ra/complex.H}.
  824. @end itemize
  825. You can add your own types as scalar types with the following declaration (see @code{ra/complex.H}):
  826. @verbatim
  827. namespace ra { template <> constexpr bool is_scalar_def<MYTYPE> = true; }
  828. @end verbatim
  829. Otherwise, you can bring a scalar object into @code{ra::} on the spot, with the function @ref{x-scalar,@code{scalar}}.
  830. @subsection Using @code{ra::} types with the STL
  831. General @code{ra::} @ref{Containers and views,views} provide STL compatible @code{ForwardIterator}s through the members @code{begin()} and @code{end()}. These iterators traverse the elements of the array (0-cells) in row major order, regardless of the internal order of the view.
  832. For @ref{Containers and views,containers} @code{begin()} provides @code{RandomAccessIterator}s, which is handy for certain functions such as @code{sort}. There's no reason why all views couldn't provide @code{RandomAccessIterator}, but these wouldn't be efficient in general for ranks above 1, and I haven't implemented them. The container @code{RandomAccessIterator}s that are provided are in fact raw pointers.
  833. @example @c [ma106]
  834. @verbatim
  835. ra::Big<int> x {3, 2, 1}; // x is a Container
  836. auto y = x(); // y is a View on x
  837. // std::sort(y.begin(), y.end()); // error: y.begin() is not RandomAccessIterator
  838. std::sort(x.begin(), x.end()); // ok, we know x is stored in row-major order
  839. @end verbatim
  840. @result{} x = @{1, 2, 3@}
  841. @end example
  842. @cindex other libraries, interfacing with
  843. @subsection Using @code{ra::} types with other libraries
  844. When you have to pass arrays back and forth between your program and an external library, perhaps even another language, it is necessary for both sides to agree on a memory layout. @code{ra::} gives you access to its own memory layout and allows you to obtain an @code{ra::} view on any type of memory.
  845. @subsubsection The good array citizen
  846. FIXME Put these in examples/ and reference them here.
  847. @cindex BLIS
  848. The good array citizen describes its arrays with the same parameters as @code{ra::}, that is: a pointer, plus a size and a stride per dimension. You pass those and you're done; you don't have to worry about special cases. Say @url{https://github.com/flame/blis, BLIS}:
  849. @quotation
  850. @verbatim
  851. #include <blis.h>
  852. ra::Big<double, 2> A({M, K}, ...);
  853. ra::Big<double, 2> B({K, N}, ...);
  854. ra::Big<double, 2> C({M, N}, ...);
  855. double alpha = ...;
  856. double beta = ...;
  857. // C := βC + αAB
  858. bli_dgemm(BLIS_NO_TRANSPOSE, BLIS_NO_TRANSPOSE, M, N, K, &alpha,
  859. A.data(), A.stride(0), A.stride(1),
  860. B.data(), B.stride(0), B.stride(1),
  861. &beta, C.data(), C.stride(0), C.stride(1));
  862. @end verbatim
  863. @end quotation
  864. @cindex FFTW
  865. Another good array citizen, @url{http://fftw.org, FFTW} handles arbitrary rank:
  866. @quotation
  867. @verbatim
  868. #include <fftw3.h>
  869. ...
  870. using complex = std::complex<double>;
  871. static_assert(sizeof(complex)==sizeof(fftw_complex));
  872. // forward DFT over the last r axes of a -> b
  873. void fftw(int r, ra::View<complex> const a, ra::View<complex> b)
  874. {
  875. int const rank = a.rank();
  876. assert(r>0 && r<=rank);
  877. assert(every(shape(a)==shape(b)));
  878. fftw_iodim dims[r];
  879. fftw_iodim howmany_dims[rank-r];
  880. for (int i=0; i!=rank; ++i) {
  881. if (i>=rank-r) {
  882. dims[i-rank+r].n = a.size(i);
  883. dims[i-rank+r].is = a.stride(i);
  884. dims[i-rank+r].os = b.stride(i);
  885. } else {
  886. howmany_dims[i].n = a.size(i);
  887. howmany_dims[i].is = a.stride(i);
  888. howmany_dims[i].os = b.stride(i);
  889. }
  890. }
  891. fftw_plan p;
  892. p = fftw_plan_guru_dft(r, dims, rank-r, howmany_dims,
  893. (fftw_complex *)(a.data()), (fftw_complex *)(b.data()),
  894. FFTW_FORWARD, FFTW_ESTIMATE);
  895. fftw_execute(p);
  896. fftw_destroy_plan(p);
  897. }
  898. @end verbatim
  899. @end quotation
  900. @cindex Guile
  901. Translating array descriptors from a foreign language should be fairly simple. For example, this is how to convert a @url{https://www.gnu.org/software/guile/manual/html_node/Accessing-Arrays-from-C.html#Accessing-Arrays-from-C,Guile} array view into an @code{ra::} view:
  902. @quotation
  903. @verbatim
  904. SCM a; // say a is #nf64(...)
  905. ...
  906. scm_t_array_handle h;
  907. scm_array_get_handle(a, &h);
  908. scm_t_array_dim const * dims = scm_array_handle_dims(&h);
  909. View<double> v(map([](int i) { return ra::Dimv {dim[i].ubnd-dim[i].lbnd+1, dim[i].inc}; },
  910. ra::iota(scm_array_handle_rank(&h))),
  911. scm_array_handle_f64_writable_elements(&h));
  912. ...
  913. scm_array_handle_release(&h);
  914. @end verbatim
  915. @end quotation
  916. @cindex Numpy
  917. @cindex Python
  918. Numpy's C API has the type @url{https://docs.scipy.org/doc/numpy/reference/c-api.array.html,@code{PyArrayObject}} which can be used in the same way as Guile's @code{scm_t_array_handle} in the example above.
  919. Generally it is simpler to let the foreign language handle the memory, even though there should be ways to transfer ownership (e.g. Guile has @url{https://www.gnu.org/software/guile/manual/html_node/SRFI_002d4-API.html#index-scm_005ftake_005ff64vector,@code{scm_take_xxx}}).
  920. @subsubsection The bad array citizen
  921. Unfortunately there are many libraries that don't accept general array parameters, or that do strange things with particular values of sizes and/or strides.
  922. The most common case is that a library doesn't handle strides at all, and it only accepts unit stride for rank 1 arrays, or packed row-major or column-major storage for higher rank arrays. In that case, you might be forced to copy your array before passing it along.
  923. FIXME using is_c_order, etc.
  924. Other libraries do accept strides, but not general ones. For example @url{https://www.netlib.org/blas}' @code{cblas_dgemm} has this prototype:
  925. @quotation
  926. @verbatim
  927. cblas_dgemm(order, transA, transB, m, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
  928. @end verbatim
  929. @end quotation
  930. @code{A}, @code{B}, @code{C} are (pointers to) 2-arrays, but the routine accepts only one stride argument for each (@code{lda}, etc.). CBLAS also doesn't understand @code{lda} as a general stride, but rather as the dimension of a larger array that you're slicing @code{A} from, and some implementations will handle negative or zero @code{lda} in bizarre ways.
  931. Sometimes you can work around this by fiddling with @code{transA} and @code{transB}, but in general you need to check your array parameters and you may need to make copies.
  932. @cindex OpenGL
  933. OpenGL is another library that requires @url{https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribPointer.xhtml,contortions:}
  934. @quotation
  935. @verbatim
  936. void glVertexAttribPointer(GLuint index,
  937. GLint size,
  938. GLenum type,
  939. GLboolean normalized,
  940. GLsizei stride,
  941. const GLvoid * pointer);
  942. @end verbatim
  943. [...]
  944. @emph{stride}
  945. @quotation
  946. Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
  947. @end quotation
  948. @end quotation
  949. It isn't clear whether negative strides are legal, either. So just as with CBLAS, passing general array views will require copies.
  950. @c ------------------------------------------------
  951. @node Extension
  952. @section Extension
  953. @c ------------------------------------------------
  954. @subsection New scalar types
  955. @code{ra::} 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}.
  956. @example
  957. @verbatim
  958. struct W { int x; }
  959. ra::Big<W, 2> w = {{ {4}, {2} }, { {1}, {3} }};
  960. cout << W(1, 1).x << endl;
  961. cout << amin(map([](auto && x) { return w.x; }, w)) << endl;
  962. @end verbatim
  963. @print{} 3
  964. 1
  965. @end example
  966. However, if you want to mix arbitrary types in array operations, you'll need to tell @code{ra::} that that is actually what you want. This is to avoid conflicts with other libraries.
  967. @example
  968. @verbatim
  969. namespace ra { template <> constexpr bool is_scalar_def<W> = true; }
  970. ...
  971. W ww {11};
  972. for_each([](auto && x, auto && y) { cout << (x.x + y.y) << " "; }, w, ww); // ok
  973. @end verbatim
  974. @print{} 15 13 12 14
  975. @end example
  976. but
  977. @example
  978. @verbatim
  979. struct U { int x; }
  980. U uu {11};
  981. for_each([](auto && x, auto && y) { cout << (x.x + y.y) << " "; }, w, uu); // error: can't find ra::start(U)
  982. @end verbatim
  983. @end example
  984. @anchor{x-new-array-operations}
  985. @subsection New array operations
  986. @code{ra::} provides array extensions for standard operations such as @code{+}, @code{*}, @code{cos} @ref{x-scalar-ops,and so on}. You can add array extensions for your own operations in the obvious way, with @ref{x-map,@code{map}} (but note the namespace qualifiers):
  987. @example
  988. @verbatim
  989. return_type my_fun(...) { };
  990. ...
  991. namespace ra {
  992. template <class ... A> inline auto
  993. my_fun(A && ... a)
  994. {
  995. return map(::my_fun, std::forward<A>(a) ...);
  996. }
  997. } // namespace ra
  998. @end verbatim
  999. @end example
  1000. @cindex Blitz++
  1001. If you compare this with what Blitz++ had to do, modern C++ sure has made our lives easier.
  1002. If @code{my_fun} is an overload set, you can use
  1003. @example
  1004. @verbatim
  1005. namespace ra {
  1006. template <class ... A> inline auto
  1007. my_fun(A && ... a)
  1008. {
  1009. return map([](auto && ... a) { return ::my_fun(a ...); }, std::forward<A>(a) ...);
  1010. }
  1011. } // namespace ra
  1012. @end verbatim
  1013. @end example
  1014. @c ------------------------------------------------
  1015. @node Functions
  1016. @section Functions
  1017. @c ------------------------------------------------
  1018. You don't need to use @ref{Array operations,@code{map}} every time you want to do something with arrays in @code{ra::}. A number of array functions are already defined.
  1019. @anchor{x-scalar-ops}
  1020. @subsection Standard scalar operations
  1021. @code{ra::} defines array extensions for @code{+}, @code{-} (both unary and binary), @code{*}, @code{/}, @code{!}, @code{&&}, @code{||}@footnote{@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} or @code{true}, respectively. Note that if both operands are of rank 0 and at least one of them is an @code{ra::} object, they is no way to preserve the behavior of @code{&&} and @code{||} with built in types and avoid evaluating both, since the overloaded operators @url{http://en.cppreference.com/w/cpp/language/operators, are normal functions}.}, @code{>}, @code{<}, @code{>=}, @code{<=}, @code{==}, @code{!=}, @code{pow}, @code{sqr}, @code{abs}, @code{cos}, @code{sin}, @code{exp}, @code{expm1}, @code{sqrt}, @code{log}, @code{log1p}, @code{log10}, @code{isfinite}, @code{isnan}, @code{isinf}, @code{max}, @code{min}, @code{odd}, @code{asin}, @code{acos}, @code{atan}, @code{atan2}, @code{cosh}, @code{sinh}, and @code{tanh}. Extending other scalar operations is straightforward; see @ref{x-new-array-operations,New array operations}. @code{ra::} also defines (and extends) the non-standard functions @ref{x-sqr,@code{sqr}}, @ref{x-sqrm,@code{sqrm}}, @ref{x-conj,@code{conj}}, @ref{x-rel-error,@code{rel_error}}, and @ref{x-xI,@code{xI}}.
  1022. For example:
  1023. @example @c [ma110]
  1024. @verbatim
  1025. cout << exp(ra::Small<double, 3> {4, 5, 6}) << endl;
  1026. @end verbatim
  1027. @print{} 54.5982 148.413 403.429
  1028. @end example
  1029. @subsection Conditional operations
  1030. @ref{x-map,@code{map}} 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)}, which returns an expression that will evaluate @code{iftrue} when @code{condition} is true and @code{iffalse} otherwise.
  1031. @example
  1032. @verbatim
  1033. ra::Big<double> x ...
  1034. ra::Big<double> y = where(x>0, expensive_expr_1(x), expensive_expr_2(x));
  1035. @end verbatim
  1036. @end example
  1037. Here @code{expensive_expr_1} and @code{expensive_expr_2} are array expressions. So the computation of the other arm would be wasted if one were to do instead
  1038. @example
  1039. @verbatim
  1040. ra::Big<double> y = map([](auto && w, auto && t, auto && f) -> decltype(auto) { return w ? t : f; }
  1041. x>0, expensive_expr_1(x), expensive_function_2(x));
  1042. @end verbatim
  1043. @end example
  1044. If the expressions have side effects, then @code{map} won't even give the right result.
  1045. @c [ma109]
  1046. @example
  1047. @verbatim
  1048. ra::Big<int, 1> o = {};
  1049. ra::Big<int, 1> e = {};
  1050. ra::Big<int, 1> n = {1, 2, 7, 9, 12};
  1051. ply(where(odd(n), map([&o](auto && x) { o.push_back(x); }, n), map([&e](auto && x) { e.push_back(x); }, n)));
  1052. cout << "o: " << ra::noshape << o << ", e: " << ra::noshape << e << endl;
  1053. @end verbatim
  1054. @print{} o: 1 7 9, e: 2 12
  1055. @end example
  1056. FIXME Very artificial example.
  1057. FIXME Do we want to expose ply(); this is the only example in the manual that uses it.
  1058. When the choice is between more than two expressions, there's @ref{x-pick,@code{pick}}, which operates similarly.
  1059. @subsection Special operations
  1060. Some operations are essentially scalar operations, but require special syntax and would need a lambda wrapper to be used with @code{map}. @code{ra::} comes with a few of these already defined.
  1061. FIXME
  1062. @subsection Elementwise reductions
  1063. @code{ra::} defines the whole-array one-argument reductions @code{any}, @code{every}, @code{amax}, @code{amin}, @code{sum}, @code{prod} and the two-argument reductions @code{dot} and @code{cdot}. Note that @code{max} and @code{min} are two-argument scalar operations with array extensions, while @code{amax} and @code{amin} are reductions. @code{any} and @code{every} are short-circuiting.
  1064. You can define similar reductions in the same way that @code{ra::} does it:
  1065. @example
  1066. @verbatim
  1067. template <class A>
  1068. inline auto op_reduce(A && a)
  1069. {
  1070. T c = op_default;
  1071. for_each([&c](auto && a) { c = op(c, a); }, a);
  1072. return c;
  1073. }
  1074. @end verbatim
  1075. @end example
  1076. Often enough you need to reduce over particular axes. This is possible by combining assignment operators with the @ref{Rank extension,rank extension} mechanism, or using the @ref{The rank conjunction,rank conjunction}, or iterating over @ref{Cell iteration, cells of higher rank}. For example:
  1077. @example
  1078. @verbatim
  1079. ra::Big<double, 2> a({m, n}, ...);
  1080. ra::Big<double, 1> sum_rows({n}, 0.);
  1081. iter<1>(sum_rows) += iter<1>(a);
  1082. // for_each(ra::wrank<1, 1>([](auto & c, auto && a) { c += a; }), sum_rows, a) // alternative
  1083. // sum_rows += transpose<1, 0>(a); // another
  1084. ra::Big<double, 1> sum_cols({m}, 0.);
  1085. sum_cols += a;
  1086. @end verbatim
  1087. @end example
  1088. FIXME example with assignment op
  1089. A few common operations of this type are already packaged in @code{ra::}.
  1090. @subsection Special reductions
  1091. @code{ra::} defines the following special reductions.
  1092. FIXME
  1093. @subsection Shortcut reductions
  1094. 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{&&} and @code{||}, which @code{ra::} defines as @code{every} and @code{any}.
  1095. FIXME
  1096. These operations are defined on top of another function @code{early}.
  1097. FIXME early
  1098. The following is often useful.
  1099. FIXME lexicographical compare etc.
  1100. @c ------------------------------------------------
  1101. @node Error handling
  1102. @section Error handling
  1103. @c ------------------------------------------------
  1104. Error handling in @code{ra::} is barebones. It is controlled by two macros:
  1105. @itemize
  1106. @item @code{RA_CHECK_BOUNDS}
  1107. is a binary flag. If it is 0, runtime checks are disabled. The default is 1. @code{RA_CHECK_BOUND} controls all runtime checks on input arguments, not only bounds checks. If the checks are enabled,
  1108. @item @code{RA_ASSERT}
  1109. is called with a single argument, an expression that evaluates to true (in the @code{ra::} namespace) if the check passes. The default value of @code{RA_ASSERT} is @code{assert} (from @code{<cassert>}) which will abort the program otherwise.
  1110. @end itemize
  1111. @code{ra::} contains uses of @code{assert} for checking invariants or for sanity checks that are separate from uses of @code{RA_ASSERT}. Those can be disabled in the usual way with @code{-DNDEBUG}. The use of @code{-DNDEBUG} is untested.
  1112. @code{RA_ASSERT} is provided so that you can replace the default @code{assert} with something more appropriate for your program. @code{examples/throw.C} in the distribution shows how to throw an user-defined exception instead.
  1113. @c ------------------------------------------------
  1114. @node Extras
  1115. @chapter Extras
  1116. @c ------------------------------------------------
  1117. @c ------------------------------------------------
  1118. @node Hazards
  1119. @chapter Hazards
  1120. @c ------------------------------------------------
  1121. Some of these issues arise because @code{ra::} applies its principles systematically, which can have surprising results. Still others are the result of unfortunate compromises. And a few are just bugs.
  1122. @section Reuse of expression objects
  1123. Expression objects are meant to be used once. This applies to anything produced with @code{ra::map}, @code{ra::iter}, @code{ra::start}, @code{ra::vector}, or @code{ra::ptr}. Reuse errors are @emph{not} checked. For example:
  1124. @example
  1125. @verbatim
  1126. ra::Big<int, 2> B({3, 3}, ra::_1 + ra::_0*3); // {{0 1 2} {3 4 5} {6 7 8}}
  1127. std::array<int, 2> l = { 1, 2 };
  1128. cout << B(ra::vector(l), ra::vector(l)) << endl; // ok => {{4 5} {7 8}}
  1129. auto ll = ra::vector(l);
  1130. cout << B(ll, ll) << endl; // ??
  1131. @end verbatim
  1132. @end example
  1133. @section Assignment to views
  1134. FIXME
  1135. With dynamic-shape containers (e.g. @code{Big}), @code{operator=} replaces the left hand side instead of writing over its contents. This behavior is inconsistent with @code{View::operator=} and is there only so that istream >> container may work; do not rely on it.
  1136. @section View of const vs const view
  1137. @c See branch ra-viewconst and places in big.H for a description of the problem.
  1138. FIXME
  1139. Passing view arguments by reference
  1140. @section Rank extension in assignments
  1141. Assignment of an expression onto another expression of lower rank may not do what you expect. This example matches @code{a} 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;@}}. You may get a different result depending on traversal order.
  1142. @example @c [ma107]
  1143. @verbatim
  1144. int a = 0;
  1145. ra::scalar(a) = 3 + ra::Small<int, 3> {4, 5, 6}; // ?
  1146. @end verbatim
  1147. @result{} a = 9
  1148. @end example
  1149. Compare with
  1150. @example
  1151. @verbatim
  1152. int a = 0;
  1153. ra::scalar(a) += 3 + ra::Small<int, 3> {4, 5, 6}; // 0 + 3 + 4 + 5 + 6
  1154. @end verbatim
  1155. @result{} a = 18
  1156. @end example
  1157. @section Performance pitfalls of rank extension
  1158. In the following example where @code{b} has its shape extended from (3) to (3, 4), @code{f} is called 12 times, even though only 3 calls are needed if @code{f} doesn't have side effects. In such cases it might be preferrable to write the outer loop explicitly, or to do some precomputation.
  1159. @example
  1160. @verbatim
  1161. ra::Big<int, 2> a = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}};
  1162. ra::Big<int, 1> b = {1, 2, 3};
  1163. ra::Big<int, 2> c = map(f, b) + a;
  1164. @end verbatim
  1165. @end example
  1166. @section Chained assignment
  1167. FIXME
  1168. When @code{a=b=c} works, it operates as @code{b=c; a=b;} and not as an array expression.
  1169. @section Unregistered scalar types
  1170. FIXME
  1171. @code{View<T, N> x; x = T()} fails if @code{T} isn't registered as @code{is_scalar}.
  1172. @enumerate
  1173. @item
  1174. Item 0
  1175. @item
  1176. Item 1
  1177. @item
  1178. Item 2
  1179. @end enumerate
  1180. @c ------------------------------------------------
  1181. @node Internals
  1182. @chapter Internals
  1183. @c ------------------------------------------------
  1184. @code{ra::} 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).
  1185. @menu
  1186. * Type hierarchy:: From @code{Containers} to @code{Flat}.
  1187. * Term agreement:: Abstain or approve.
  1188. * Loop types:: Chosen for performance.
  1189. * Introspection:: Speaking to myself.
  1190. * Compiling and running:: Practical matters.
  1191. @end menu
  1192. @c ------------------------------------------------
  1193. @node Type hierarchy
  1194. @section Type hierarchy
  1195. @c ------------------------------------------------
  1196. This is a rough and possibly not very accurate summary. I'm hoping the planned feature of ‘C++ concepts’ will force me to be more systematic about it all.
  1197. @itemize
  1198. @item @b{Container} --- @code{Big} @code{Shared} @code{Unique} @code{Small}
  1199. These are array types that own their data in one way or another. Creating or destroying these objects may allocate or deallocate memory, respectively.
  1200. @item @b{View} --- @code{View} @code{SmallView}
  1201. These are array views into data in memory. Access to their contents doesn't involve computation and they may be writable. Any of the @b{Container} types can be treated as a @b{View}, but one may also create @b{View}s that aren't associated with any @b{Container}, for example into memory allocated with a different library. Creating and destroying @b{View}s doesn't allocate or deallocate memory for array elements.
  1202. @item @b{ArrayIterator} --- @code{Expr} @code{Ryn} @code{Pick} @code{cell_iterator} @code{cell_iterator_small} @code{Iota} @code{Vector} @code{Scalar}
  1203. This is a traversable object. @b{ArrayIterator}s are accepted by all the array functions such as @code{map}, @code{for_each}, etc. @b{ArrayIterator}s can be created from @b{View}s and from certain foreign array-like types primarily through the function @code{start}. In most cases this is done automatically when those types are used in array expressions.
  1204. @item @b{Ravelable} --- @code{cell_iterator} @code{cell_iterator_small} @code{Iota} @code{Vector} @code{Scalar}
  1205. This is a kind of @b{ArrayIterator} that provides a @code{flat()} method to obtain a linearized view of a section of the array. Together with the methods @code{size()}, @code{stride()}, @code{keep_stride()} and @code{adv()}, a loop involving only @b{Ravelable}s can have its inner loop unfolded and traversed using @b{Flat} objects. This is faster than a multidimensional loop, especially if the inner dimensions of the loop are small.
  1206. @item @b{Indexable} @code{cell_iterator} @code{cell_iterator_small} @code{Iota} @code{Vector} @code{Scalar} @code{TensorIndex}
  1207. This is a kind of @b{ArrayIterator} that provides an @code{at(i ...)} method for random access to any element of the array.@footnote{It used to be the case that @code{TensorIndex} was @b{Indexable} but not @b{Ravelable}, which forced all the other @b{Ravelable} types to provide @code{at(i...)} so that they could be mixed with @code{TensorIndex} in expressions. Now that @code{TensorIndex} is also @b{Ravelable}, this distinction may disappear in the future with @code{at(i...)} being provided at a higher level.}
  1208. @item @b{Flat} @code{Flat} @code{PickFlat} @code{CellFlat} @code{IotaFlat} @code{ScalarFlat}
  1209. These are pointerlike types that are meant to be traversed linearly. They have methods @code{operator+=} (to advance) and @code{operator*} (to derreference). @b{Flat} objects are obtained from @b{Ravelable} objects through a method @code{flat}.
  1210. @end itemize
  1211. @c ------------------------------------------------
  1212. @node Term agreement
  1213. @section Term agreement
  1214. @c ------------------------------------------------
  1215. The execution of an expression template begins with the determination of its shape — the size of each of its dimensions. This is done recursively by traversing the terms of the expression. For a given dimension @code{k}≥0, terms that have rank less or equal than @code{k} are ignored, following the prefix matching principle. Likewise terms where dimension @code{k} has undefined size (such as @code{TensorIndex} or view axes created with @code{insert}) are ignored. All the other terms must match.
  1216. Then we select a traversal method depending on the types of the arguments. @code{ra::} has two traversal methods, both based on pointer-like iterators. @code{ply_ravel} is used for dynamic-size expressions and @code{plyf} for static-size expressions.
  1217. Finally we select an order of traversal. @code{ra::} 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 (v10) 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} will unroll as many innermost dimensions as it can, and in some cases traversal will be executed as a single 1D loop.
  1218. @c ------------------------------------------------
  1219. @node Loop types
  1220. @section Loop types
  1221. @c ------------------------------------------------
  1222. TODO
  1223. @c ------------------------------------------------
  1224. @node Introspection
  1225. @section Introspection
  1226. @c ------------------------------------------------
  1227. @code{ra::ra-traits} is a template defined for Containers and Views, incuding foreign ones. It's not defined for expression types
  1228. @c ------------------------------------------------
  1229. @node Compiling and running
  1230. @section Compiling and running
  1231. @c ------------------------------------------------
  1232. The following boolean @code{#define}s affect the behavior of @code{ra::}.
  1233. @itemize
  1234. @item @code{RA_CHECK_BOUNDS} (default 1): Check bounds on dimension agreement (e.g. @code{Big<int, 1> @{2, 3@} + Big<int, 1> @{1, 2, 3@}}) and random array accesses (e.g. @code{Small<int, 2> a = 0; int i = 10; a[i] = 0;}).
  1235. @item @code{RA_USE_BLAS} (default 0): Try to use BLAS for certain rank 1 and rank 2 operations. Currently this is only used by some of the benchmarks and not by the library itself.
  1236. @item @code{RA_OPTIMIZE} (default 1): Replace certain expressions by others that are expected to perform better. This acts as a global mask on other @code{RA_OPTIMIZE_xxx} flags.
  1237. @item @code{RA_OPTIMIZE_IOTA} (default 1): Perform immediately (beat) certain operations on @code{ra::Iota} objects. For example, @code{ra::Iota(3, 0) + 1} becomes @code{ra::Iota(3, 1)} instead of a two-operand expression template.
  1238. @item @code{RA_OPTIMIZE_SMALLVECTOR} (default 0): Perform immediately certain operations on @code{ra::Small} objects, using small vector intrinsics. Currently this only works on @b{gcc} and doesn't necessarily result in improved performance.
  1239. @end itemize
  1240. @code{ra::} comes with three kinds of tests: examples, proper tests, and benchmarks. @code{ra::} uses its own crude test and benchmark suites. Run @code{CXXFLAGS=-O3 scons} from the top directory of the distribution to build and run them all. Alternatively, you can use @code{CXXFLAGS=-O3 cmake . && make && make test}.
  1241. TODO Flags and notes about different compilers
  1242. @c ------------------------------------------------
  1243. @node The future
  1244. @chapter The future
  1245. @c ------------------------------------------------
  1246. @section Error messages
  1247. FIXME
  1248. @section Reductions
  1249. FIXME
  1250. @section Etc
  1251. FIXME
  1252. @c ------------------------------------------------
  1253. @node Reference
  1254. @chapter Reference
  1255. @c ------------------------------------------------
  1256. @anchor{x-map} @defun map op expr ...
  1257. Create an array expression that applies @var{op} to @var{expr} ...
  1258. @end defun
  1259. For example:
  1260. @example
  1261. @verbatim
  1262. ra::Big<double, 1> x = map(cos, ra::Small<double, 1> {0.});
  1263. @end verbatim
  1264. @result{} x = @{ 1. @}
  1265. @end example
  1266. @var{op} can return a reference. A typical use is subscripting. For example (TODO better example, e.g. using STL types):
  1267. @example
  1268. @verbatim
  1269. ra::Big<int, 2> x = {{3, 3}, 0.};
  1270. map([](auto && i, auto && j) -> int & { return x(i, j); },
  1271. ra::Big<int, 1> {0, 1, 1, 2}, ra::Big<int, 1> {1, 0, 2, 1})
  1272. = 1;
  1273. @end verbatim
  1274. @result{} x = @{@{0, 1, 0@}, @{1, 0, 1@}, @{0, 1, 0@}@}
  1275. @end example
  1276. Here the anonymous function can be replaced by simply @code{x}. Remember that unspecified return type defaults to (value) @code{auto}, so either a explicit type or @code{decltype(auto)} should be used if you want to return a reference.
  1277. @cindex @code{for_each}
  1278. @anchor{x-for_each} @defun for_each op expr ...
  1279. Create an array expression that applies @var{op} to @var{expr} ..., and traverse it.
  1280. @end defun
  1281. @var{op} is run for effect; whatever it returns is discarded. For example:
  1282. @example
  1283. @verbatim
  1284. double s = 0.;
  1285. for_each([&s](auto && a) { s+=a; }, ra::Small<double, 1> {1., 2., 3})
  1286. @end verbatim
  1287. @result{} s = 6.
  1288. @end example
  1289. @cindex @code{ply}
  1290. @anchor{x-ply} @defun ply expr
  1291. Traverse @var{expr}. @code{ply} returns @code{void} so @var{expr} should be run for effect.
  1292. @end defun
  1293. It is rarely necessary to use @code{ply}. Expressions are traversed automatically when they are assigned to views, for example, or printed out. @ref{x-for_each,@code{for_each}}@code{(...)} (which is actually @code{ply(map(...))} should cover most other uses.
  1294. @example
  1295. @verbatim
  1296. double s = 0.;
  1297. ply(map([&s](auto && a) { s+=a; }, ra::Small<double, 1> {1., 2., 3})) // same as for_each
  1298. @end verbatim
  1299. @result{} s = 6.
  1300. @end example
  1301. @cindex @code{pack}
  1302. @anchor{x-pack} @defun pack <type> expr ...
  1303. Create an array expression that brace-constructs @var{type} from @var{expr} ...
  1304. @end defun
  1305. @cindex @code{cast}
  1306. @anchor{x-cast} @defun cast <type> expr
  1307. Create an array expression that casts @var{expr} into @var{type}.
  1308. @end defun
  1309. @cindex @code{pick}
  1310. @anchor{x-pick} @defun pick select_expr expr ...
  1311. Create an array expression that selects the first of @var{expr} ... if @var{select_expr} is 0, the second if @var{select_expr} is 1, and so on. The expressions that are not selected are not looked up.
  1312. @end defun
  1313. This function cannot be defined using @ref{x-map,@code{map}}, because @code{map} looks up each one of its argument expressions before calling @var{op}.
  1314. For example:
  1315. @example @c cf examples/readme.C [ma100].
  1316. @verbatim
  1317. ra::Small<int, 3> s {2, 1, 0};
  1318. ra::Small<double, 3> z = pick(s, s*s, s+s, sqrt(s));
  1319. @end verbatim
  1320. @result{} z = @{1.41421, 2, 0@}
  1321. @end example
  1322. @cindex @code{where}
  1323. @anchor{x-where} @defun where pred_expr true_expr false_expr
  1324. Create an array expression that selects @var{true_expr} if @var{pred_expr} is @code{true}, and @var{false_expr} if @var{pred_expr} is @code{false}. The expression that is not selected is not looked up.
  1325. @end defun
  1326. For example:
  1327. @example
  1328. @verbatim
  1329. ra::Big<double, 1> s {1, -1, 3, 2};
  1330. s = where(s>=2, 2, s); // saturate s
  1331. @end verbatim
  1332. @result{} s = @{1, -1, 2, 2@}
  1333. @end example
  1334. @cindex @code{from}
  1335. @anchor{x-from} @defun from op ... expr
  1336. Create outer product expression. This is defined as @math{E = from(op, e₀, e₁ ...)} ⇒ @math{E(i₀₀, i₀₁ ..., i₁₀, i₁₁, ..., ...) = op[e₀(i₀₀, i₀₁, ...), e₁(i₁₀, i₁₁, ...), ...]}.
  1337. @end defun
  1338. For example:
  1339. @example
  1340. @verbatim
  1341. ra::Big<double, 1> a {1, 2, 3};
  1342. ra::Big<double, 1> b {10, 20, 30};
  1343. ra::Big<double, 2> axb = from([](auto && a, auto && b) { return a*b; }, a, b)
  1344. @end verbatim
  1345. @result{} axb = @{@{10, 20, 30@}, @{20, 40, 60@}, @{30, 60, 90@}@}
  1346. @end example
  1347. @example
  1348. @verbatim
  1349. ra::Big<int, 1> i {2, 1};
  1350. ra::Big<int, 1> j {0, 1};
  1351. ra::Big<double, 2> A = {{1, 2}, {3, 4}, {5, 6}};
  1352. ra::Big<double, 2> Aij = from(A, i, j)
  1353. @end verbatim
  1354. @result{} Aij = @{@{6, 5@}, @{4, 3@}@}
  1355. @end example
  1356. The last example is more or less how @code{A(i, j)} is actually implemented (@pxref{The rank conjunction}).
  1357. @cindex @code{at}
  1358. @anchor{x-at} @defun at expr indices
  1359. Look up @var{expr} at each element of @var{indices}, which shall be a multi-index into @var{expr}.
  1360. @end defun
  1361. This can be used for sparse subscripting. For example:
  1362. @example @c [ra30]
  1363. @verbatim
  1364. ra::Big<int, 2> A = {{100, 101}, {110, 111}, {120, 121}};
  1365. ra::Big<ra::Small<int, 2>, 2> i = {{{0, 1}, {2, 0}}, {{1, 0}, {2, 1}}};
  1366. ra::Big<int, 2> B = at(A, i);
  1367. @end verbatim
  1368. @result{} B = @{@{101, 120@}, @{110, 121@}@}
  1369. @end example
  1370. @cindex @code{shape}
  1371. @anchor{x-shape} @defun shape a
  1372. Get the shape of an @code{ra::} object.
  1373. @end defun
  1374. The shape of a dynamic rank array is a rank-1 object with dynamic size, and the shape of a static rank array is a rank-1 object with static size.
  1375. This function may return an expression object or an array object. If you need to operate on the result, it might be necessary to use @code{concrete}.
  1376. @c FIXME example
  1377. @cindex @code{size}
  1378. @anchor{x-size} @defun size a
  1379. Get the total size of an @code{ra::} object: the product of all its sizes.
  1380. @end defun
  1381. @c FIXME example
  1382. @cindex @code{concrete}
  1383. @anchor{x-concrete} @defun concrete a
  1384. Convert the argument to a container of the same rank and size as @code{a}.
  1385. @end defun
  1386. If the argument has dynamic or static shape, it is the same for the result. The main use of this function is to obtain modifiable copy of an array expression without having to prepare a container beforehand, or compute the appropiate type.
  1387. @c FIXME example
  1388. @cindex @code{wrank}
  1389. @anchor{x-wrank} @defun wrank <input_rank ...> op
  1390. Wrap op using a rank conjunction (@pxref{The rank conjunction}).
  1391. @end defun
  1392. For example: TODO
  1393. @example
  1394. @verbatim
  1395. @end verbatim
  1396. @result{} x = 0
  1397. @end example
  1398. @cindex @code{transpose}
  1399. @anchor{x-transpose} @defun transpose <axes ...> view
  1400. Create a new view by transposing the axes of @var{view}.
  1401. @end defun
  1402. This operation does not work on arbitrary array expressions yet. TODO FILL
  1403. @cindex @code{diag}
  1404. @anchor{x-diag} @defun diag view
  1405. Equivalent to @code{transpose<0, 0>(view)}.
  1406. @end defun
  1407. @cindex @code{reverse}
  1408. @anchor{x-reverse} @defun reverse view axis
  1409. Create a new view by reversing axis @var{k} of @var{view}.
  1410. @end defun
  1411. This is equivalent to @code{view(ra::dots<k>, ra::iota(view.size(k), view.size(k)-1, -1))}.
  1412. This operation does not work on arbitrary array expressions yet. TODO FILL
  1413. @c @anchor{x-reshape}
  1414. @c @defun reshape view shape
  1415. @c Create a new view with shape @var{shape} from the row-major ravel of @var{view}.
  1416. @c @end defun
  1417. @c FIXME fill when the implementation is more mature...
  1418. @c @anchor{x-ravel}
  1419. @c @defun ravel view
  1420. @c Return the ravel of @var{view} as a view on @var{view}.
  1421. @c @end defun
  1422. @c FIXME fill when the implementation is more mature...
  1423. @cindex @code{stencil}
  1424. @anchor{x-stencil} @defun stencil view lo hi
  1425. Create a stencil on @var{view} with lower bounds @var{lo} and higher bounds @var{hi}.
  1426. @end defun
  1427. @var{lo} and @var{hi} are expressions of rank 1 indicating the extent of the stencil on each dimension. Scalars are rank extended, that is, @var{lo}=0 is equivalent to @var{lo}=(0, 0, ..., 0) with length equal to the rank @code{r} of @var{view}. The stencil view has twice as many axes as @var{view}. The first @code{r} dimensions are the same as those of @var{view} except that they have their sizes reduced by @var{lo}+@var{hi}. The last @code{r} dimensions correspond to the stencil around each element of @var{view}; the center element is at @code{s(i0, i1, ..., lo(0), lo(1), ...)}.
  1428. This operation does not work on arbitrary array expressions yet. TODO FILL
  1429. @cindex @code{collapse}
  1430. @anchor{x-collapse} @defun collapse
  1431. TODO
  1432. @end defun
  1433. @cindex @code{explode}
  1434. @anchor{x-explode} @defun explode
  1435. TODO
  1436. @end defun
  1437. @cindex @code{real_part}
  1438. @anchor{x-real_part} @defun real_part
  1439. Take real part of a complex number. This can be used as reference.
  1440. See also @ref{x-imag_part,@code{imag_part}}.
  1441. @end defun
  1442. @cindex @code{imag_part}
  1443. @anchor{x-imag_part} @defun imag_part
  1444. Take imaginary part of a complex number. This can be used as reference.
  1445. For example: @c [ma115]
  1446. @example
  1447. @verbatim
  1448. ra::Small<std::complex<double>, 2, 2> A = {{1., 2.}, {3., 4.}};
  1449. imag_part(A) = -2*real_part(A);
  1450. cout << A << endl;
  1451. @end verbatim
  1452. @print{}
  1453. (1, -2) (2, -4)
  1454. (3, -6) (4, -8)
  1455. @end example
  1456. See also @ref{x-real_part,@code{real_part}}.
  1457. @end defun
  1458. @cindex @code{format_array}
  1459. @anchor{x-format_array} @defun format_array expr [last_axis_separator [second_last_axis_separator ...]]
  1460. Formats an array for character output.
  1461. For example:
  1462. @example
  1463. @verbatim
  1464. ra::Small<int, 2, 2> A = {{1, 2}, {3, 4}};
  1465. cout << "case a:\n" << A << endl;
  1466. cout << "case b:\n" << format_array(A) << endl;
  1467. cout << "case c:\n" << format_array(A, "|", "-") << endl;
  1468. @end verbatim
  1469. @print{} case a:
  1470. 1 2
  1471. 3 4
  1472. case b:
  1473. 1 2
  1474. 3 4
  1475. case c:
  1476. 1|2-3|4
  1477. @end example
  1478. @end defun
  1479. The shape that might be printed before the expression itself (depending on its type) is not subject to these separators. See @ref{x-noshape,@code{noshape}}, @ref{x-withshape,@code{withshape}}.
  1480. @cindex @code{noshape}
  1481. @cindex @code{withshape}
  1482. @anchor{x-noshape}
  1483. @anchor{x-withshape}
  1484. @deffn @w{Special objects} {withshape noshape}
  1485. If either of these objects is sent to @code{std::ostream} before an expression object, the shape of that object will or won't be printed.
  1486. @end deffn
  1487. If the object has static (compile time) shape, the default is not to print the shape, so @code{noshape} isn't necessary, and conversely for @code{withshape} if the object has dynamic (runtime) shape. Note that the array readers [@code{operator>>(std::istream &, ...)}] expect the shape to be present or not according to the default.
  1488. For example:
  1489. @example
  1490. @verbatim
  1491. ra::Small<int, 2, 2> A = {77, 99};
  1492. cout << "case a:\n" << A << endl;
  1493. cout << "case b:\n" << ra::noshape << A << endl;
  1494. cout << "case c:\n" << ra::withshape << A << endl;
  1495. @end verbatim
  1496. @print{} case a:
  1497. 77 99
  1498. case b:
  1499. 77 99
  1500. case c:
  1501. 2
  1502. 77 99
  1503. @end example
  1504. but:
  1505. @example
  1506. @verbatim
  1507. ra::Big<int> A = {77, 99};
  1508. cout << "case a:\n" << A << endl;
  1509. cout << "case b:\n" << ra::noshape << A << endl;
  1510. cout << "case c:\n" << ra::withshape << A << endl;
  1511. @end verbatim
  1512. @print{} case a:
  1513. 1
  1514. 2
  1515. 77 99
  1516. case b:
  1517. 77 99
  1518. case c:
  1519. 1
  1520. 2
  1521. 77 99
  1522. @end example
  1523. Note that in the last example the very shape of @code{ra::Big<int>} has runtime size, so that size (the rank of @code{A}, that is 1) is printed as part of the shape of @code{A}.
  1524. See also @ref{x-format_array,@code{format_array}}.
  1525. @cindex @code{start}
  1526. @anchor{x-start} @defun start foreign_object
  1527. Create a array expression from @var{foreign_object}.
  1528. @end defun
  1529. @var{foreign_object} can be of type @code{std::vector} or @code{std::array}, a built-in array (@code{int[3]}, etc.) or an initializer list, or any object that @code{ra::} accepts as scalar (see @ref{x-is-scalar,@code{here}}). The resulting expresion has rank and size according to the original object. Compare this with @ref{x-scalar,@code{scalar}}, which will always produce an expression of rank 0.
  1530. Generally one can mix these types with @code{ra::} expressions without needing @code{ra::start}, but sometimes this isn't possible, for example for operators that must be class members.
  1531. @example
  1532. @verbatim
  1533. std::vector<int> x = {1, 2, 3};
  1534. ra::Big<int, 1> y = {10, 20, 30};
  1535. cout << (x+y) << endl; // same as ra::start(x)+y
  1536. // x += y; // error: no match for operator+=
  1537. ra::start(x) += y; // ok
  1538. @end verbatim
  1539. @print{} 3
  1540. 11 22 33
  1541. @result{} x = @{ 11, 22, 33 @}
  1542. @end example
  1543. @cindex @code{ptr}
  1544. @anchor{x-ptr} @defun ptr pointer [size]
  1545. Create vector expression from raw @var{pointer}.
  1546. @end defun
  1547. The resulting expression has rank 1 and the size given. If @code{size} is not given, the expression has undefined size, and it will need to be matched with other expressions whose size @emph{is} defined.
  1548. @code{ra::} doesn't know what is actually accessible through the pointer, so be careful. For instance:
  1549. @example
  1550. @verbatim
  1551. int p[] = {1, 2, 3};
  1552. ra::Big<int, 1> v3 {1, 2, 3};
  1553. ra::Big<int, 1> v4 {1, 2, 3, 4};
  1554. v3 += ra::ptr(p); // ok, shape (3): v3 = {2, 4, 6}
  1555. v4 += ra::ptr(p); // undefined, shape (4): bad access to p[3]
  1556. // cout << (ra::ptr(p)+ra::TensorIndex<0>{}) << endl; // ct error, expression has undefined shape
  1557. cout << (ra::ptr(p, 3)+ra::TensorIndex<0>{}) << endl; // ok, prints { 1, 3, 5 }
  1558. cout << (ra::ptr(p, 4)+ra::TensorIndex<0>{}) << endl; // undefined, bad access at p[4]
  1559. @end verbatim
  1560. @end example
  1561. Of course in this example one could simply have used @code{p} instead of @code{ra::ptr(p)}, since the array type retains size information.
  1562. @example
  1563. @verbatim
  1564. v3 += p; // ok
  1565. v4 += p; // error checked by ra::, shape clash (4) += (3)
  1566. cout << (p + ra::TensorIndex<0>{}) << endl; // ok
  1567. @end verbatim
  1568. @end example
  1569. @cindex @code{scalar}
  1570. @anchor{x-scalar} @defun scalar expr
  1571. Create scalar expression from @var{expr}.
  1572. @end defun
  1573. The primary use of this function is to bring a scalar object into the @code{ra::} namespace. A somewhat artificial example:
  1574. @example
  1575. @verbatim
  1576. struct W { int x; }
  1577. ra::Big<W, 1> w { {1}, {2}, {3} };
  1578. // error: no matching function for call to start(W)
  1579. // for_each([](auto && a, auto && b) { cout << (a.x + b.x) << endl; }, w, W {7});
  1580. // bring W into ra:: with ra::scalar
  1581. for_each([](auto && a, auto && b) { cout << (a.x + b.x) << endl; }, w, ra::scalar(W {7}));
  1582. @end verbatim
  1583. @print{} 8
  1584. 9
  1585. 10
  1586. @end example
  1587. See also @ref{x-scalar-char-star,@code{this example}}.
  1588. Since @code{scalar} produces an object with rank 0, it's also useful when dealing with nested arrays, even for objects that are already in @code{ra::}. Consider:
  1589. @example
  1590. @verbatim
  1591. using Vec2 = ra::Small<double, 2>;
  1592. Vec2 x {-1, 1};
  1593. ra::Big<Vec2, 1> c { {1, 2}, {2, 3}, {3, 4} };
  1594. // c += x // error: x has shape (2) and c has shape (3)
  1595. c += ra::scalar(x); // ok: scalar(x) has shape () and matches c.
  1596. @end verbatim
  1597. @result{} c = @{ @{0, 3@}, @{1, 4@}, @{2, 5@} @}
  1598. @end example
  1599. The result is @{c(0)+x, c(1)+x, c(2)+x@}. Compare this with
  1600. @example
  1601. @verbatim
  1602. c(ra::iota(2)) += x; // c(ra::iota(2)) with shape (2) matches x with shape (2)
  1603. @end verbatim
  1604. @result{} c = @{ @{-1, 2@}, @{2, 5@}, @{2, 5@} @}
  1605. @end example
  1606. where the result is @{c(0)+x(0), c(1)+x(1), c(2)@}.
  1607. @cindex @code{iter}
  1608. @anchor{x-iter} @defun iter <k> (view)
  1609. Create iterator over the @var{k}-cells of @var{view}. If @var{k} is negative, it is interpreted as the negative of the frame rank. In the current version of @code{ra::}, @var{view} may have dynamic or static shape.
  1610. @end defun
  1611. @example
  1612. @verbatim
  1613. ra::Big<int, 2> c {{1, 3, 2}, {7, 1, 3}};
  1614. cout << "max of each row: " << map([](auto && a) { return amax(a); }, iter<1>(c)) << endl;
  1615. ra::Big<int, 1> m({3}, 0);
  1616. scalar(m) = max(scalar(m), iter<1>(c));
  1617. cout << "max of each column: " << m << endl;
  1618. m = 0;
  1619. for_each([&m](auto && a) { m = max(m, a); }, iter<1>(c));
  1620. cout << "max of each column again: " << m << endl;
  1621. @end verbatim
  1622. @print{} max of each row: 2
  1623. 3 7
  1624. max of each column: 3
  1625. 7 3 3
  1626. max of each column again: 3
  1627. 7 3 3
  1628. @end example
  1629. @c [ma113]
  1630. In the following example, @code{iter} emulates @code{scalar}. Note that the shape () of @code{iter<1>(m)} matches the shape (3) of @code{iter<1>(c)}. Thus, each of the 1-cells of @code{c} matches against the single 1-cell of @code{m}.
  1631. @example
  1632. @verbatim
  1633. m = 0;
  1634. iter<1>(m) = max(iter<1>(m), iter<1>(c));
  1635. cout << "max of each column yet again: " << m << endl;
  1636. @end verbatim
  1637. @print{} max of each column again: 3
  1638. 7 3 3
  1639. @end example
  1640. The following example computes the trace of each of the items [(-1)-cells] of @code{c}. @c [ma104]
  1641. @example
  1642. @verbatim
  1643. ra::Small<int, 3, 2, 2> c = ra::_0 - ra::_1 - 2*ra::_2;
  1644. cout << "c: " << c << endl;
  1645. cout << "s: " << map([](auto && a) { return sum(diag(a)); }, iter<-1>(c)) << endl;
  1646. @end verbatim
  1647. @print{} c: 0 -2
  1648. -1 -3
  1649. 1 -1
  1650. 0 -2
  1651. 2 0
  1652. 1 -1
  1653. s: -3 -1 -1
  1654. @end example
  1655. @cindex @code{sum}
  1656. @anchor{x-sum} @defun sum expr
  1657. Return the sum (+) of the elements of @var{expr}, or 0 if expr is empty. This sum is performed in unspecified order.
  1658. @end defun
  1659. @cindex @code{prod}
  1660. @anchor{x-prod} @defun prod expr
  1661. Return the product (*) of the elements of @var{expr}, or 1 if expr is empty. This product is performed in unspecified order.
  1662. @end defun
  1663. @cindex @code{amax}
  1664. @anchor{x-amax} @defun amax expr
  1665. Return the maximum of the elements of @var{expr}. If @var{expr} is empty, return @code{-std::numeric_limits<T>::infinity()} if the type supports it, otherwise @code{std::numeric_limits<T>::lowest()}, where @code{T} is the value type of the elements of @var{expr}.
  1666. @end defun
  1667. @cindex @code{amin}
  1668. @anchor{x-amin} @defun amin expr
  1669. Return the minimum of the elements of @var{expr}. If @var{expr} is empty, return @code{+std::numeric_limits<T>::infinity()} if the type supports it, otherwise @code{std::numeric_limits<T>::max()}, where @code{T} is the value type of the elements of @var{expr}.
  1670. @end defun
  1671. @cindex @code{early}
  1672. @anchor{x-early} @defun early expr default
  1673. @var{expr} shall be an array expression that returns @code{std::tuple<bool, T>}. @var{expr} is traversed as by @code{for_each}; if the expression ever returns @code{true} in the first element of the tuple, traversal stops and the second element is returned. If this never happens, @var{default} is returned instead.
  1674. @end defun
  1675. The following definition of elementwise @code{lexicographical_compare} relies on @code{early}.
  1676. @example @c [ma108]
  1677. @verbatim
  1678. template <class A, class B>
  1679. inline bool lexicographical_compare(A && a, B && b)
  1680. {
  1681. return early(map([](auto && a, auto && b)
  1682. { return a==b ? std::make_tuple(false, true) : std::make_tuple(true, a<b); },
  1683. a, b),
  1684. false);
  1685. }
  1686. @end verbatim
  1687. @end example
  1688. @cindex @code{any}
  1689. @anchor{x-any} @defun any expr
  1690. Return @code{true} if any element of @var{expr} is true, @code{false} otherwise. The traversal of the array expression will stop as soon as possible, but the traversal order is not specified.
  1691. @end defun
  1692. @cindex @code{every}
  1693. @anchor{x-every} @defun every expr
  1694. Return @code{true} if every element of @var{expr} is true, @code{false} otherwise. The traversal of the array expression will stop as soon as possible, but the traversal order is not specified.
  1695. @end defun
  1696. @cindex @code{sqr}
  1697. @anchor{x-sqr} @defun sqr expr
  1698. Compute the square of @var{expr}.
  1699. @end defun
  1700. @cindex @code{sqrm}
  1701. @anchor{x-sqrm} @defun sqrm expr
  1702. Compute the square of the norm-2 of @var{expr}, that is, @code{conj(expr)*expr}.
  1703. @end defun
  1704. @cindex @code{conj}
  1705. @anchor{x-conj} @defun conj expr
  1706. Compute the complex conjugate of @var{expr}.
  1707. @end defun
  1708. @cindex @code{xI}
  1709. @anchor{x-xI} @defun xI expr
  1710. Compute @code{(0+1j)} times @var{expr}.
  1711. @end defun
  1712. @cindex @code{rel_error}
  1713. @anchor{x-rel-error} @defun rel_error a b
  1714. @var{a} and @var{b} are arbitrary array expressions. Compute the error of @var{a} relative to @var{b} as
  1715. @code{(a==0. && b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b))}
  1716. @end defun
  1717. @cindex @code{none}
  1718. @anchor{x-none}
  1719. @deffn @w{Special objects} {none}
  1720. Pass @code{none} to container constructors to indicate that the contents shouldn't be initialized. This is appropriate when the initialization you have in mind wouldn't fit in a constructor argument. For example:
  1721. @example
  1722. @verbatim
  1723. void old_style_initializer(int m, int n, double *);
  1724. ra::Big<double> b({2, 3}, ra::none);
  1725. old_style_initializer(2, 3, b.data());
  1726. @end verbatim
  1727. @end example
  1728. @end deffn
  1729. @c ------------------------------------------------
  1730. @node @mybibnode{}
  1731. @chapter Sources
  1732. @c ------------------------------------------------
  1733. @multitable @columnfractions .1 .9
  1734. @item @mybibitem{Abr70} @tab Philip S. Abrams. An APL machine. Technical report SLAC-114 UC-32 (MISC), Stanford Linear Accelerator Center, Stanford University, Stanford, CA, USA, February 1970.
  1735. @item @mybibitem{Ber87} @tab Robert Bernecky. An introduction to function rank. ACM SIGAPL APL Quote Quad, 18(2):39–43, December 1987.
  1736. @item @mybibitem{bli17} @tab The Blitz++ meta-template library. @url{http://blitz.sourceforge.net}, November 2017.
  1737. @item @mybibitem{Cha86} @tab Gregory J. Chaitin. Physics in APL2, June 1986.
  1738. @item @mybibitem{FI68} @tab Adin D. Falkoff and Kenneth Eugene Iverson. APL\360 User’s manual. IBM Thomas J. Watson Research Center, August 1968.
  1739. @item @mybibitem{FI73} @tab Adin D. Falkoff and Kenneth Eugene Iverson. The design of APL. IBM Journal of Research and Development, 17(4):5–14, July 1973.
  1740. @item @mybibitem{FI78} @tab Adin D. Falkoff and Kenneth Eugene Iverson. The evolution of APL. ACM SIGAPL APL, 9(1):30– 44, 1978.
  1741. @item @mybibitem{J S} @tab J Primer. J Software, @url{https://www.jsoftware.com/help/primer/contents.htm}, November 2017.
  1742. @item @mybibitem{Mat} @tab MathWorks. MATLAB documentation, @url{https://www.mathworks.com/help/matlab/}, November 2017.
  1743. @item @mybibitem{num17} @tab NumPy. @url{http://www.numpy.org}, November 2017.
  1744. @item @mybibitem{Ric08} @tab Henry Rich. J for C programmers, February 2008.
  1745. @item @mybibitem{SSM14} @tab 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.
  1746. @item @mybibitem{Vel01} @tab Todd Veldhuizen. Blitz++ user’s guide, February 2001.
  1747. @item @mybibitem{Wad90} @tab Philip Wadler. Deforestation: transforming programs to eliminate trees. Theoretical Computer Science, 73(2): 231--248, June 1990. @url{https://doi.org/10.1016/0304-3975%2890%2990147-A}
  1748. @end multitable
  1749. @c ------------------------------------------------
  1750. @node Indices
  1751. @unnumbered Indices
  1752. @c ------------------------------------------------
  1753. @c @node Concept Index
  1754. @c @unnumbered Concept Index
  1755. @printindex cp
  1756. @c @node Function Index
  1757. @c @unnumbered Function Index
  1758. @c @printindex fn
  1759. @c \nocite{JLangReference,FalkoffIverson1968,Abrams1970,FalkoffIverson1973,FalkoffIverson1978,APLexamples1,ArraysCowan,KonaTheLanguage,blitz++2001}
  1760. @bye