Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Rank (computer programming)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{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";<ref>{{Cite web |title=Vocabulary_with_defintions |url=https://files.schudio.com/federation-of-boldmere-schools/files/documents/Vocabulary_with_defintions.docx}}</ref> thus, a two-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 the case of [[APL programming language|APL]] the notion applies to every operand; and [[Binary function|dyad]]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"> #include <type_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 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; }; template <typename T> constexpr auto rank_v = rank<T>::value; /* Rank of an expression * * Let the rank of an expression be the rank of its type */ template <typename T> 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 :<syntaxhighlight lang="cpp">rank<T>::value</syntaxhighlight> or the shorter form :<syntaxhighlight lang="cpp">rank_v<T></syntaxhighlight> Calculating the rank of an expression can be 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:Arrays]] [[Category:Programming language topics]] {{Compu-lang-stub}}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:Compu-lang-stub
(
edit
)
Template:More citation needed
(
edit
)
Template:Prog-lang-stub
(
edit
)
Template:R shell
(
edit
)