Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Tuple vs. Array

  1. May 29, 2010 #1
    I am wondering if anyone can describe to me the correct use of a couple words. I am programming a very large application, and I like to name my variables so that they are very descriptive. Before I begin, I would like to know what is the difference between an array and a tuple? Are they the same? If I have a list of symbols, such as <B,0,1>, is it more descriptive to say "symbol array", or "symbol tuple"? Also, as all C programmers know, I will need to be naming the array size in a separate variable. Should I say "symbol tuple size", "symbol array size", or something else? What about for sets? "symbol set" and "symbol set size"? Or is there something better? Normally I would just name them whatever, but this application is gigantic, and I want to make sure I get the naming right the first time. Thanks all.
  2. jcsd
  3. May 29, 2010 #2
    A tuple, also called a sequence, is an ordered list of items, such as (0,1,1), which is a different tuple from (1,1,0) and (0,1).

    A set is a collection of distinct items with no specified order, so {0,1} represents the same set as {1,0} and {0,1,1}.

    An array is a collection of items ordered in any number of dimensions. For example, a tuple is a 1-dimensional array (it takes one index to locate an item), and a matrix is a 2-dimensional array (it takes two indices to locate an item).
  4. May 29, 2010 #3
    Thank you Rasalhague :) That clears up a lot. What about the number of items in a tuple or set. Do I say count, size, cardinality, or something else. I am not sure on how to say "the number of elements" in a single word. Is it different for sets and tuples?
  5. May 29, 2010 #4
    The Wikipedia entry Tuple talks about the elements and length of a tuple. The Wikipedia entry Sequence talks about the members, elements or terms of a sequence, and about its length. (The tuple article says that a tuple is a sequence; and the sequence article says that a sequence is also called a tuple.) So length looks promising - but maybe someone can give us a more definitive answer. Wolfram Mathworld doesn't go into that much detail.

    I've seen several ways of defining tuples in terms of sets. For example, some sources define the tuples recursively using nested sets like this:

    (a,b) = {a,{a,b}}
    (a,b,c) = ((a,b),c) = {{a,{a,b}},{(a,b),c}} = {{a,{a,b}},{{a,{a,b}},c}}

    or if the possibility of a 0-tuple is included:

    (a,b) = {{Ø{Ø,a}},{{Ø,{Ø,a}},b}}

    and then define a function as a tuple (D,C,r), where D and C are sets, the domain and codomain of the function, and r is the rule relating them. But an alternative approach defines a sequence as a function from the set {1,2,...,n} to a set S, and I don't know how a function is defined in this latter view.

    This makes me hesitate to talk about the "elements" of a tuple, in case a tuple is really a set whose elements are other sets. So instead, I've been calling them "terms" or "entries" or more vaguely "items". Maybe I'm being pernickity there, and I don't suppose there's really any ambiguity, but I'm just learning about these things, and I want to get it right... Obviously the Wikipedia writer didn't see any contradiction with calling them elements or members.
  6. May 30, 2010 #5
    Thanks again Rasalhague. I will use "length" throughout my program :)
  7. May 30, 2010 #6
    You're welcome.

    MATLAB calls it length too:

    A possible source of ambiguity, in some contexts, is that the magnitude or norm of a vector is often referred to, informally, as its length. In the case of the vector space of real coordinate vectors with the usual rules:

    [tex]\text{norm}(\mathbf{a})=\sqrt{\mathbf{a} \cdot \mathbf{a}}=\sqrt{\sum_{i=1}^{n}(a_i)^2}[/tex]


  8. May 30, 2010 #7
    Luckily, my program is not going to be using vectors, so ambiguity in this case will not be a problem. Although it will be good to consider this ambiguity in the creation of future programs which may involve vectors. Thank you for taking the time to answer my question so in-depth. I really appreciate your guidance.
  9. May 30, 2010 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Size is a common term for the number of elements in a collection. It's enouraged by the language too -- C++ containers use size() to get the number of elements they contain, and C has the type size_t. (Though I suppose in a few contexts, there could be an ambiguity about size referring to number of bytes -- e.g. that's what sizeof returns)

    Of course, length is also common. Java uses that for its arrays, and python uses the abbreviation len for all of its container types.
  10. May 31, 2010 #9
    Thanks Hurkyl. I will be using length. If I am coding with a vector data type I will default to size. It is interesting that you noted the ambiguity of size referring to a number of bytes. That is actually why I started this thread :) I was looking for something different than size, because I knew size could mean the number of bytes, in C language.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook