14

Background:

  • "experienced" Pascal-programmer (DOS-era, never got into OOP)
  • proud owner of Mathematica ver 11.2
  • have used Mathematica for years to do my share of number crunching and plotting to produce material for my university courses (as well as for Math.SE :-)

Next task:

I need to add a few things to my Mathematica repertoire. The immediate goal is to build the necessary Mathematica functions to produce animated GIFs like

enter image description here

That is, animated sequences of moves on a Rubik cube. The shown sequence of moves cyclically permutes the positions of $3$ corner pieces. The GIF is included here to give the general idea, and also to prove that I know enough to code this :-)

Currently the animation is achieved by manipulating a variable descriptively named vertexlist that contains the 3D-positions of all the eight corners of all the small moving parts. It is clear that to produce the desired animations, I simply need to manipulate the contents of this variable, and have Mathematica Show the cube in all the intermediate states in sequence.

The snake in the paradise is that currently vertexlist is, in old-timer-speak, a global variable. I probably can do everything I need in the immediate future that way, but I have reached a point, where that feels A) unprofessional, B) inefficient. For example, in a classroom setting I surely want to have several different Rubik's cubes of varying sizes, and in various current states. To that end I need a data structure not unlike a Pascal record (similar to struct in C), say (I know this is unsyntactic, because the value of a field cannot really be used as a limit of an array, but we can safely ignore that here I think):

cube=RECORD
 size:2..5;
 vertexlist: ARRAY[1..size,1..size,1..size,1..8] OF...
 ...(* information about which vertices form a polygon of which color *)
END;

With that taken care of I can then easily code functions roughly like MicroRotation[c_, Axes_, Layer_, Angle_], where c would be the cube I want to modify.

How do I achieve something like that type declaration in Mathematica? I know that in Mathematica everything really is a list, and that I can simply define my objects with a new list header like JyrkisRubikCube (ok, that would be kludgy, but anyway). But doing that would not really help here! For I need my code to refer to and modify the individual fields of the data structure. If I use a List, I guess I can, using the freedom built into Mathematica's lists just assume that the first entry is the size, the second is the vertexlist et cetera. Is that really the only way to go? Is it the recommended way? Having descriptive names for the fields of the record would, at the very least, help me if I want to return to this project a few years down the road...

Searching the site gave some promising looking hits:

I will keep studying those. If I try and use Association as described by Szabolcs how do I:

  1. create/declare a variable of the prescribed type,
  2. access the chosen field of a variable of this type, and
  3. modify the value of (a component of) the chosen field of a variable of this type?

I realize that this question may be too broad to be answered well in the limited space available here, but I also appreciate pointers and links. Even a key buzzword might do. Given that outside academia (and in spite of the best efforts of Borland) Pascal never got much traction, probably eplaining why my searches did not produce anything very useful.

Edit: The posts I found leave me with the impression that using Association creates a data structure that is very kludgy to modify. Consider the following snippet from my current notebook. This function rotates the layer number v (an integer in the range from $1$ to size) by an angle given by the variable x

 rotateX[v_, x_] := Module[{i, j, k, t},
  m = {{1, 0, 0}, {0, Cos[x], -Sin[x]}, {0, Sin[x], Cos[x]}};
  For[i = 1, i < size + 1, i++,
   For[j = 1, j < size + 1, j++,
    For[k = 1, k < size + 1, k++,
     If[Floor[vertexlist[[i, j, k]][[1]][[1]]] == v,
      For[t = 1, t < 9, t++,
       vertexlist[[i, j, k]][[t]] = 
        m.(vertexlist[[i, j, k]][[t]] - {0, center, center}) + {0, 
          center, center}
       ]]]]]]

You see that I modify the components of the global vertexlist in the 4-fold loop according to whether the small part is currently in the layer being rotated.

I need a way of modifying not the global vertexlist but the vertexlist of a cube of my choice to be passed as a third parameter to rotateX. Something like rotateX[c_,x_,cube_] that will modify cube.vertexlist instead of vertexlist, where cube is a data structure that has at least size and vertexlist as fields.

Is there a way of doing this other than using, say, cube[[2]], everywhere in places of the more natural cube.vertexlist?


One more animated sequence of moves. I am using ListAnimate to generate these. I chose to do eight frames per a quarter turn, so an animation of 8 quarter turns has 64 frames. The sequence of moves below also cyclically permutes the positions of three small cubes. This time those small cubes are on the faces of the big cube. Two of those small cubes show a white face and one of them shows a blue face. Because one white cube moved to the place initially occupied by another white cube, visually the result looks like a blue and a white piece traded places, but, in fact, it is a 3-cycle. Meaning, that you need to perform the sequence of moves three times to return the cube to its initial state. The algebra of permutations plays out in a way that 3-cycles are simple to produce. Ask me, if you want to know more :-)

enter image description here

Jyrki Lahtonen
  • 1,059
  • 7
  • 15
  • 1
    The tags are fine. – Szabolcs Apr 12 '19 at 11:27
  • 2
    What is your main goal here? Is it to work with a Rubik's cube as a combinatorial object? If so, then things like continuous spatial coordinates are irrelevant for analysing the moves. They are only relevant for vsualization. Or are you focusing specifically on the visualization and on creating animations? – Szabolcs Apr 12 '19 at 11:33
  • 2
    Do take a look at this demonstration, which seems to have all of this implemented. http://demonstrations.wolfram.com/RubiksCube/ The author is an expert Mathematica programmer and I would put trust in his design. – Szabolcs Apr 12 '19 at 11:34
  • 1
    @JyrkiLahtonen Indeed, I would suggest to use Association wrapped by a head to indicate the "class". So an instance of the class would look like X = JyrkisRubikCube[Association[...]]. Then you can use TagSetDelayed for defining and overloading methods for this class. A field/property "foo" of X can then accessed with X[[1]][["foo"]] and set with X[[1]][["foo"]] = bar. It is not like you have a fixed set of allowed keys in the association; fields can be created and deleted (KeyDrop) dynamically. But I guess that's what comes closed to what you ask for. – Henrik Schumacher Apr 12 '19 at 11:38
  • @Szabolcs Visualization is exactly the goal here. I plan to use this to make a few concepts from a first course on groups more concrete to a bunch of people at freshman level. I won't be ignoring the algebra, because I teach algebra for living. Of course, this is also just for fun. – Jyrki Lahtonen Apr 12 '19 at 11:38
  • Wow! Can't say I would surprised to learn that somebody has already done this :-) Will take a look. Anyway, I¨m leaving the question open for I also want to learn how to do this. – Jyrki Lahtonen Apr 12 '19 at 11:40
  • @HenrikSchumacher Thanks, but slow down a bit. A method? Is that not something from object oriented programming? That came after I learned to code :-) – Jyrki Lahtonen Apr 12 '19 at 11:42
  • The professional animation seems to be fine. Works as expected after I downloaded it (stalled in the browser). It doesn't seem to give the kind of control I would like to have: animate sequences of several moves, switch to a different size, have several instance of the cube available simultaneously. I do predict that studying the code will help me with the desire to have a branching animation! – Jyrki Lahtonen Apr 12 '19 at 11:51
  • @JyrkiLahtonen per your edit: Mathematica doesn't support mutability of anything except symbols. But if you have a = <|"Vertices"->{1, 2, 3}|> you can do a[["Vertices", 2] += 1 no problem. By the way you do know that For is about the slowest possible looping construct in Mathematica, right? – b3m2a1 Apr 12 '19 at 18:15
  • @b3m2a1 No, I did not know about Forbeing slow. That does not make any sense to me. What is faster? Probably not critical here, because the slow part in my code is the rendering of intermediate frames. And your first sentence does not really make much sense to me either. I can modify, for example, arrays as I please in Mathematica. Why not some other pieces of data lumped together? I guess I am missing something major. – Jyrki Lahtonen Apr 12 '19 at 18:25
  • @JyrkiLahtonen you can modify Association as you please too. I guess I don't know what type of modification you're trying to do, but see AssociateTo or Append or Merge or AssociationMap. Anything else is faster than For. Do is the easiest replacement, but you can get better performance out of the others. See this. – b3m2a1 Apr 12 '19 at 18:28
  • The type of modifications I want to do here are the updates to vertex lists local to a given cube (instead of the global symbol vertexlist) - see my snippet. Won't those Append-commands irrepairably mutate the internal memory layout of a tidy structure like an array or a record/struct? Rendering them useles due to slow access? Thanks for the link about the disadvantages of For. Will study it. – Jyrki Lahtonen Apr 12 '19 at 18:35
  • @JyrkiLahtonen AssociateTo won't. Also the internal layout is definitely way more complicated than a simple struct and Mathematica is designed to have very fast immutable manipulation. In fact generally in Mathematica immutable ops are faster than mutable ones. – b3m2a1 Apr 12 '19 at 20:56

1 Answers1

15

Update 2

Per request, I extended this to handle arbitrary sizes and rotations. It was a huge hassle to figure out how to get the appropriate permutations for the individual rotations for arbitrary sized cubes, but it worked out. Here's what it looks like:

r1 = RubiksCube["Size" -> 4];

r1@"Colors" = ColorData["Atoms"] /@ {6, 7, 8, 9, 11, 13, 18}; r1@"Show"[Method -> {"ShrinkWrap" -> True}]

enter image description here

And we can visualize these with different kinds of rotations and different origins:

r2 = RubiksCube["Origin" -> {10, 0, 0}, "Size" -> 10];

Show[ r1@"Twist"[.5, {"Y", 2}]@"Twist"[.5, {"Y", 4}]@"Show"[], r2@"Show"[], PlotRange -> All ]

enter image description here

Fullish Imp

I took Roman Maeder's Rubiks Cube Demo and recast it in an OOP manner using the package I talk about below.

I put this on GitHub here so people can check it out.

You'll need the InterfaceObjects package to make this work, but once you have it you can try it out like:

Get["https://github.com/b3m2a1/mathematica-tools/raw/master/RubiksCube.wl"]

new = RubiksCube[]

enter image description here

Then use it like:

new@"Show"[]

enter image description here

Or:

Manipulate[
 Fold[
   #@"Twist"[#2[[1]], #2[[2]]] &,
   new,
   Thread[
    {
     {b, f, l, r, d, u},
     {"Back", "Front", "Left", "Right", "Down", "Up"}
     }
    ]
   ]@"Show"[],
 {b, 0, 2 π, .01},
 {f, 0, 2 π, .01},
 {l, 0, 2 π, .01},
 {r, 0, 2 π, .01},
 {d, 0, 2 π, .01},
 {u, 0, 2 π, .01},
 DisplayAllSteps -> True
 ]

enter image description here

And just to see how deep the OOP runs, each cube inside that thing is its own object:

new["Cuboids"][[1, 1, 1]]

enter image description here

Finally, maybe you prefer a different colored cube:

new@"Colors" = ColorData[97] /@ Range[7]

enter image description here

This is what OOP makes easy for you

Original

It sounds as if you're really trying to do OOP in Mathematica. Honestly, the language isn't great for that, but there are things like SparseArray and friends that support some OOP and methods and stuff. So I wrote a package to automate that. Maybe it'll be useful. You can get it from here.

To use it we "register" a new object:

<< InterfaceObjects`

RegisterInterface[ RubiksCube, { "Size", "VertexList" }, "Constructor" -> constructRubiksCube, "MutationFunctions" -> {"Keys", "Parts"} ]

RubiksCube

This tells us that we have a new type with required attributes "Size" and "VertexList" and which uses constructRubiksCube as its base constructor. It can be mutated on either its "Keys" or "Parts".

Next we define some functions to act on the data stored in this object as well as our constructor:

constructRubiksCube[size : _?NumberQ : .1, 
   vertextList : _List | Automatic : Automatic] :=
  <|

"Size" -> size, "VertexList" -> Replace[vertextList, Automatic -> RandomReal[{}, {9, 3}]] |>; newVertices[r_RubiksCube] :=

InterfaceModify[RubiksCube, (* this is here just for type safety stuff *) r, Function[{properties}, ReplacePart[properties, "VertexList" -> RandomReal[{}, {9, 3}] ] ] ]; displayCube[r_RubiksCube] :=

With[{v = r["VertexList"], s = r["Size"]}, Graphics3D[ Map[Scale[Cuboid[#], s] &, v] ] ];

That InterfaceModify function basically just allows you to change the state of the object. Keep in mind that it returns a new object since Mathematica doesn't do OOP for real.

Then we attach these as methods to our object:

InterfaceMethod[RubiksCube]@
   r_RubiksCube["Show"][] := displayCube[r];
InterfaceMethod[RubiksCube]@
   r_RubiksCube["NewVertices"][] := newVertices[r];

And now we can make a cube:

r = RubiksCube[];

Query props:

r@"Size"

0.1

r@"VertexList"

{{0.471592, 0.554128, 0.669796}, {0.360993, 0.228342, 0.337433}, {0.0738407, 0.522903, 0.0469278}, {0.992347, 0.84807, 0.83663}, {0.451908, 0.667543, 0.01672}, {0.181584, 0.660202, 0.100972}, {0.857532, 0.474982, 0.684844}, {0.905125, 0.127964, 0.81153}, {0.654156, 0.0892593, 0.493546}}

Discover properties / methods:

r@"Methods"

{"Show", "NewVertices"}

r@"Properties"

{"Size", "VertexList", "Version", "Properties", "Methods"}

Call our methods:

r@"Show"[]

enter image description here

r@"NewVertices"[]@"Show"[]

enter image description here

And modify things:

r@"Size" = 2;

r@"Show"[]

enter image description here

Dunno if this will be useful for you but I use it in lots of my packages to define outward facing interfaces.

b3m2a1
  • 46,870
  • 3
  • 92
  • 239
  • I appreciate the effort you put into this. However, I don't grok OOP AT ALL, so I cannot build my demos on top of this. Obviously I can learn a lot from studying Roman's and your code, but before I get there I would need to learn something else first. – Jyrki Lahtonen Apr 12 '19 at 21:59
  • If I use your code can I then have two different size Rubik cube's in visible at the same time. – Jyrki Lahtonen Apr 12 '19 at 22:01
  • 1
    @JyrkiLahtonen give OOP a try. You were basically asking about how to do OOP in your question. Your Pascal "record" is just an object. A C struct is just an object but with fewer features. – b3m2a1 Apr 12 '19 at 22:01
  • Also, you didn't seem to do anything with the variable size, As in size=3 produces a 3x3x3 cube, size=4 a 4x4x4 cube etc. – Jyrki Lahtonen Apr 12 '19 at 22:02
  • @JyrkiLahtonen ah didn't get that that's what you wanted to do. Shouldn't be too hard to change up. I'll take a minute or two and if it's easy I'll do it. – b3m2a1 Apr 12 '19 at 22:03
  • The way I saw it I was not asking about how to do OOP at all. I wanted to use the same snippet of code on several "records". Those may be like "objects", but I guess I don't know enough about the inner workings of Matematica :-( – Jyrki Lahtonen Apr 12 '19 at 22:10
  • 2
    @JyrkiLahtonen Mathematica doesn't actually support OOP. I made it emulate OOP because OOP is the cleanest way I've found to think about programming. "Use the same snippet of code on several objects" == define a method for an object. – b3m2a1 Apr 12 '19 at 22:11
  • I need to think about this. I'm not gonna learn a new language in a month. Anyway, much appreciated. – Jyrki Lahtonen Apr 12 '19 at 22:21
  • @JyrkiLahtonen take a look now. It was a pain but I guess it was kinda interesting to figure out the necessary permutations. – b3m2a1 Apr 13 '19 at 00:26
  • Good morning! Thanks. As a math guy, permutations are no problem. Actually in my code I don't do any! I simply move the small cubes around. Meaning that I'm not checking whether the cube is "solved". May be I should produce another animation to prove that :-) – Jyrki Lahtonen Apr 13 '19 at 03:57
  • But, I admire your commitment to this task. Accepting this for now. Would be worth a bounty, but I still entertain hopes that I don't need to learn OOP. To a non-professional hobbyist programmer crossing the bridge from the universe, where "your code is the only thing in existence" to the "event driven world" is a huge task. I was already in my 40s, when Microsoft dropped the support to the "safe" environment where I was used to living, and unlearning was too painful. – Jyrki Lahtonen Apr 13 '19 at 04:43
  • Anyway, for me "use the same snippet on several objects"=="write a subroutine that can take the said object as a parameter, and modify it" – Jyrki Lahtonen Apr 18 '19 at 05:02