5
$\begingroup$

My question is, what list p in the following statement returns the list q?

Tuples[{{p},{q}}]

If we think of Tuples as a binary operator then p would be the identity for Tuples.

I thought an empty list would work, but evaluating the following

Tuples[{{},{1,2,3}}]

gives {} instead of {1,2,3} or {{1},{2},{3}} as I had hoped.

The following doesn't work either:

Tuples[{{\[EmptySet]},{1,2,3}}]

Certainly, I could write a function like the following

altTuples[p_List,q_List]:= If[Length[p]==0,q,Tuples[{p,q}]

That does exactly what I want, but I want to know if I'm missing something. Is there in fact an identity for Tuples? Is there a way to do what I want with Outer? I've tried the obvious solutions with no luck.

$\endgroup$
3
  • 1
    $\begingroup$ How do you want your altTuples[] to behave when the length of $p$ is not zero? For example altTuples[{a, b, c}, {d, e, f}] returns Tuples[{a, b, c}, {d, e, f}], probably not what you want. $\endgroup$
    – mjw
    Commented Jul 8, 2019 at 1:19
  • $\begingroup$ I corrected altTuples. Actually I want it to return Tuples[{{a, b, c}, {d, e, f}}]. I think Nothing is what I was looking for. $\endgroup$
    – JAS
    Commented Jul 8, 2019 at 23:27
  • $\begingroup$ What you have above is almost correct (up to a typo). Anyway seems to produce what you want. 'AltTuples[{p,q}]` gives the same output as Tuples[p,q] when the first argument is a list with non-zero length. $\endgroup$
    – mjw
    Commented Jul 8, 2019 at 23:36

4 Answers 4

7
$\begingroup$

If {{1},{2},{3}} is fine, you can use Nothing:

Tuples[{Nothing, {1, 2, 3}}]
(* {{1}, {2}, {3}} *)

If you want {1,2,3}, you can Flatten the result, of course.

$\endgroup$
1
  • 1
    $\begingroup$ Thank you, this does exactly what I need. $\endgroup$
    – JAS
    Commented Jul 8, 2019 at 23:20
4
$\begingroup$

You can use Inactive[Sequence][] as identity like this:

Tuples[{{Inactive[Sequence][]},{q}}]//Activate

{{q}}

$\endgroup$
4
$\begingroup$

Use TagSetDelayed to define a function that behaves as desired:

ClearAll[iDentity]
iDentity /: {iDentity[___], a : {__}} := iDentity[a]
iDentity /: Tuples[iDentity[a_]] := a

Tuples[{iDentity[], {q}}]

{q}

Tuples[{iDentity[blah], {1, 2, 3}}]

{1, 2, 3}

Alternatively, define your function altTuples with two signatures:

ClearAll[altTuples]
altTuples[{tuplesIdentity | {} | Nothing, a_List}] := a
altTuples[x_] := Tuples[x]

altTuples[{{x, y}, {1, 2}}]

{{x, 1}, {x, 2}, {y, 1}, {y, 2}}

altTuples[{tuplesIdentity, {1, 2}}]

{1, 2}

altTuples[{{}, {1, 2}}]

{1, 2}

$\endgroup$
0
$\begingroup$

I think this is what you want:

altTuples[p_List, q_List] := If[Length[p] == 0, q, Tuples[{p, q}]]

The statement

altTuples[{}, {d, e, f}]

returns

{d, e, f}

and

altTuples[{a, b, c}, {d, e, f}]

returns

{{a, d}, {a, e}, {a, f}, {b, d}, {b, e}, {b, f}, {c, d}, {c, e}, {c, f}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.