[Jprogramming] APL Exercises
Murray Eisenberg
murray at math.umass.edu
Sat Dec 2 15:50:55 UTC 2017
AOn 1 Dec 2017 05:33:02 +0000,"Dabrowski, Andrew John" <dabrowsa at indiana.edu <mailto:dabrowsa at indiana.edu>> wrote:
>
> On 11/29/2017 11:40 PM, Roger Hui wrote:
>> 2.5 Cantor Set
>>
>> Write a function to compute the Cantor set of order n, n>:0.
>>
>> Cantor 0
>> 1
>> Cantor 1
>> 1 0 1
>> Cantor 2
>> 1 0 1 0 0 0 1 0 1
>> Cantor 3
>> 1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 1
>>
>
> In Mathematica:
>
> cantor[n_] := If[n == 0, {1},
> cantor[n - 1] /. {0 -> Sequence[0, 0, 0], 1 -> Sequence[1, 0, 1]}]
I also tried this in Mathematica. The quickest way is to use one of the “higher order” built-in (i.e., primitive) functions:
SubstitutionSystem[{1 -> {1, 0, 1}, 0 -> {0, 0, 0}}, 1, 2]
{{1}, {1, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 1}}
Or one can go back to more basic built-in functions, much as Andrew John Dabrowski did:
cStep[lis_] := Flatten[lis /. {1 -> {1, 0, 1}, 0 -> {0, 0, 0}}]
NestList[cStep, {1}, 2]
{{1}, {1, 0, 1}, {1, 0, 1, 0, 0, 0, 1, 0, 1}}
Interestingly, Mathematica also has as a built-in function CantorMesh built, which directly returns a graphic display of a collection of subintervals in the construction of the Cantor ternary set, with the depth indicated by the argument supplied to it. Thus
CantorMesh /@ Range[0, 2]
returns as output a list of images — the original unit interval, the 1st and 3rd thirds of that interval, and the 1st, 3rd, 7th and 9th ninths of that interval. (I cannot include graphics here, but the output is show in my post at https://mathematica.stackexchange.com/questions/161024/extract-cell-indicators-from-meshregion/161098#161098 .)
——
Murray Eisenberg murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 240 246-7240 (H)
University of Massachusetts
710 North Pleasant Street
Amherst, MA 01003-9305
More information about the Programming
mailing list