Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica, alphabet generation

If I have an alphabet, lets say sigma = {1,2,3,4,a,b,c,d,e,f,g} and want to generate all words of length n, is there a way to do that?

I can do Flatten[Outer[Function[{x, y}, {x, y}], sigma, sigma], 1] to do it for wordlength=2 but it doesnt geenralize to more letters. And permutations doesnt work since it doesnt include doubles. Permutations[sigma, {2}] doesnt give {a,a} for example.

Is there an easy way to do this or I have to write a function for it?

This is correct for n=2 (but I want for arbitrary n):

{{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, a}, {1, b}, {1, c}, {1, d}, {1, 
  e}, {1, f}, {1, g}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, a}, {2, 
  b}, {2, c}, {2, d}, {2, e}, {2, f}, {2, g}, {3, 1}, {3, 2}, {3, 
  3}, {3, 4}, {3, a}, {3, b}, {3, c}, {3, d}, {3, e}, {3, f}, {3, 
  g}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, a}, {4, b}, {4, c}, {4, 
  d}, {4, e}, {4, f}, {4, g}, {a, 1}, {a, 2}, {a, 3}, {a, 4}, {a, 
  a}, {a, b}, {a, c}, {a, d}, {a, e}, {a, f}, {a, g}, {b, 1}, {b, 
  2}, {b, 3}, {b, 4}, {b, a}, {b, b}, {b, c}, {b, d}, {b, e}, {b, 
  f}, {b, g}, {c, 1}, {c, 2}, {c, 3}, {c, 4}, {c, a}, {c, b}, {c, 
  c}, {c, d}, {c, e}, {c, f}, {c, g}, {d, 1}, {d, 2}, {d, 3}, {d, 
  4}, {d, a}, {d, b}, {d, c}, {d, d}, {d, e}, {d, f}, {d, g}, {e, 
  1}, {e, 2}, {e, 3}, {e, 4}, {e, a}, {e, b}, {e, c}, {e, d}, {e, 
  e}, {e, f}, {e, g}, {f, 1}, {f, 2}, {f, 3}, {f, 4}, {f, a}, {f, 
  b}, {f, c}, {f, d}, {f, e}, {f, f}, {f, g}, {g, 1}, {g, 2}, {g, 
  3}, {g, 4}, {g, a}, {g, b}, {g, c}, {g, d}, {g, e}, {g, f}, {g, g}}
like image 587
Smith Johnson Avatar asked Dec 05 '11 18:12

Smith Johnson


People also ask

How do you type alpha in Mathematica?

Type \[ and the start of a character name. Choose the character from the autocomplete menu: See the Letters and Letter-like Forms tutorial for a complete list of named characters. If you are using a text interface, type the complete character name; for example, \ [Alpha].

How do you put E into Mathematica?

E can be entered in StandardForm and InputForm as , ee or \[ExponentialE]. In StandardForm and TraditionalForm, E is printed as .

How do you write a suffix in Mathematica?

Type a subscript with (Insert ▶ Typesetting ▶ Subscript). Exit from typing math with : Exit from a subscript but continue typing math with (Insert ▶ Typesetting ▶ End Subexpression): Typing a subscript in text automatically enters math mode.


2 Answers

Tuples[sigma, n]

like image 165
kennytm Avatar answered Sep 30 '22 17:09

kennytm


While Tuples is surely the better way to go, I think it is valuable to show you a way to make your existing method work for arbitrary n values.

Flatten[Outer[List, ##], n - 1] & @@ ConstantArray[sigma, n]

This works by creating n instances of sigma and sequencing (##) them into Outer. Flatten is used to remove all but that last level of brackets.

Observe that your Function[{x, y}, {x, y}] can be replaced with simply List.

You could also use the Distribute function for this, assuming that your alphabet does not itself contain lists.

Distribute[ConstantArray[sigma, n], List]
like image 38
Mr.Wizard Avatar answered Sep 30 '22 17:09

Mr.Wizard