Rank (computer programming): Difference between revisions

Content deleted Content added
m moved Rank of an array to Rank (computer programming): Rank doesn't apply to arrays only; the title is not appropriate
added references section
 
(15 intermediate revisions by 13 users not shown)
Line 1:
{{More citation needed|date=February 2025}}
In [[computer programming]], '''rank''' with no further specifications is usually a synonym for (or refers to) "number of dimensions"; thus, for instance, a bi-dimensional array has rank ''two'', a three-dimensional array has rank ''three'' and so on.
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 [[computer programming]], '''rank''' with no further specifications is usually a synonym for (or refers to) "number of dimensions";<ref>{{Cite thus,web for|title=Vocabulary_with_defintions instance|url=https://files.schudio.com/federation-of-boldmere-schools/files/documents/Vocabulary_with_defintions.docx}}</ref> thus, a bitwo-dimensional array has rank ''two'', a three-dimensional array has rank ''three'' and so on.
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''.
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 [[dyadBinary function|dyadsdyad]]s ("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.
 
<syntaxhighlight lang="cpp">
<code>
#include <cstddeftype_traits>
#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 tT> struct rank
{
{ static const std::size_t value = 0; };
};
template<typename t, std::size_t n>
 
struct rank<t[n]>
{template<typename static constT, std::size_t value = 1 + rank<tN>::value; };
struct rank<tT[nN]>
{
// Rank of an expression
static const std::size_t value = 1 + rank<T>::value;
//
};
// Let the rank of an expression be the rank of its type
 
//
template <typename t, std::size_t nT>
constexpr auto rank_v = rank<T>::value;
char(&rankof(t(&)[n]))[n];
 
</code>
//* Rank of an expression
*
//* Let the rank of an expression be the rank of its type
/*/
 
template <typename t, std::size_t nT>
using unqualified_t = std::remove_cv_t<std::remove_reference_t<T>>;
 
template <typename T>
auto rankof(T&& expr)
{
return rank_v<unqualified_t<T>>;
}
</syntaxhighlight>
Given the code above the rank of a type T can be calculated at compile time by
:<codesyntaxhighlight lang="cpp">rank<T>::value</codesyntaxhighlight>
or the shorter form
and the rank of an array-expression ''expr'' by
:<code>sizeof(rankof(expr))</code>
 
:<syntaxhighlight lang="cpp">rank_v<T></syntaxhighlight>
 
andCalculating the rank of an array-expression ''expr''can bybe done using
:<syntaxhighlight lang="cpp">rankof(expr)</syntaxhighlight>
 
==See also==
*[[Rank (linear algebra)]], for a definition of ''rank'' as applied to [[matrix (mathematics)|matrices]]
*[[Rank (J programming language)]], a concept of the same name in the [[J (programming language)|J programming language]]
 
== References ==
<references />{{DEFAULTSORT:Rank (Computer Programming)}}
<!--[[Category:Data typesArrays]]-->
[[Category:Programming languageslanguage topics]]
 
{{compu-lang-stub}}
 
{{compuCompu-lang-stub}}
<!--[[Category:Data types]]-->
[[Category:Programming languages]]