Rank (computer programming): Difference between revisions

Content deleted Content added
Medialinked array.
changed category; added some more material - please expand/improve the APL section: I can take care of the C++ parts but I know pretty much nothing of APL
Line 1:
In [[computer programming]], the '''rank of an array''' with no further specifications is usually defineda tosynonym befor the(or numberrefers ofto) [[dimension]]s"number of thatdimensions"; array.thus, Forfor exampleinstance, a [[matrix (mathematics)|matrix]] is anbi-dimensional [[array]] ofhas rank 2. In some languages, such as [[APL programming language|APL]]''two'', a [[scalar]]three-dimensional isarray definedhas asrank an''three'' arrayand of rankso 0on.
Strictly, no formal definition can be provided which applies to every [[programming language]], since each of them has its own concepts, [[Formal semantics of programming languages | semantics]] and terminology; the term may not even be applicable or, to the contrary, applied with a very specific meaning in the context of a given language.
 
In the case of [[APL programming language|APL]] the notion applies to every operand; and [[dyad|dyads]] ("binary functions") have a ''left rank'' and a ''right rank''.
 
The box below instead shows how ''rank of a type'' and ''rank of an array expression'' could be defined (in a semi-formal style) for C++ and illustrates a simple way to calculate them at compile time.
 
<code>
#include <cstddef>
// Rank of a type
// -------------
//
// Let the 'rank' of a type T be the number of its dimensions if
// it is an array; zero otherwise (which is the usual convention)
//
template <typename t> struct rank
{ static const std::size_t value = 0; };
template<typename t, std::size_t n>
struct rank<t[n]>
{ static const std::size_t value = 1 + rank<t>::value; };
// Rank of an expression
//
// Let the rank of an expression be the rank of its type
//
template <typename t, std::size_t n>
char(&rankof(t(&)[n]))[n];
</code>
Given the code above the rank of a type T can be calculated at compile time by
:<code>rank<T>::value</code>
and the rank of an array-expression ''expr'' by
:<code>sizeof(rankof(expr))</code>
 
In APL, the unary ''&rho;'' ([[rho (letter)|rho]]) operator can be used to find the dimensions of an array; thus, ''&rho;&rho;A'' is the rank of ''A''.
 
==See also==
*[[Rank (linear algebra)]], for a different definition of "''rank"'' as applied to [[matrix (mathematics)|matrices]]
 
 
{{compu-lang-stub}}
 
<!--[[Category:Data types]]-->
[[Category:Programming languages]]