Pages

Sunday 5 January 2014

Randomized Permutations and Partitions

A little while ago, I posted the Permutating Friends of 3 Scarf Pattern. I also joined the Woolly Thoughts group on Ravelry, and started talking to the great folks there. Woolly Thoughts is one of a kind: combining math and generative knitting, and it turned out that at some point, they too had thought about putting together knitting with permutations. And so, some crazy new thoughts emerged in my head.

I was basically pondering on the idea of scaling and visualizing permutations and partitions: how quickly things scale up in mathematics, (for example, see this link on MathStackExchange), and therefore, how quickly it becomes completely impossible to visualize these permutations and patterns exhaustively. So, if I left out the constraint on exhaustive visualization, the design challenge is to come up with a pattern that plays with any number of partitions of any given number. So, relaxing the requirements on exhaustive visualization where ALL possible k-partitions have to be visualized, could I simply use randomization as a generative idea? I also deliberately wanted to leave k and n as parameters, since that way using a single idea we can produce as many patterns as we would like, but at the same time ensuring that no two patterns are the same! 

So, I quickly put together some Matlab code: a function to generate randomized permutations of k-partitions of any given number n. Code is attached at the end of this post, please feel free to use it or modify it in any way, but please acknowledge this original source if you do. 

Here are some interesting results:

Say you want to generate randomized partitions of the number 30 into 2 colours. Cast on 30 stitches in colour 1 (blue), use the good old garter stitch, and simply follow the chart here (this came out of the Matlab program by setting n = 30, k = 1). The magic of this type of computation, of course, is that every time you run the program, it will show you a different pattern.

To follow the chart, every row shown needs to be knit twice in garter stitch. So, for example for the square below:


  1. Row1: Beginning from the bottom right, k1 in colour 1 (blue), change working yarn, and k29 in colour 2 (red). 
  2. Row 2: Turn, k29 in colour 2, k1 in colour 1.
  3. Row 3: In the next row, there are 18 stitches of colour 1, 12 of colour 2. So, k18 in colour 1, carrying along the colour 2 yarn, then k12 in colour 2. 
  4. Row 4: Turn, k12 with colour2, and k18 with colour 1. Sine in the subsequent row, the pattern is k10 with colour 1, and k20 in colour 2, carry colour 2 along loosely by knitting the first 4 stitches in colour 1, bringing yarn forward, twining colour 2 yarn around working yarn, taking working yarn back again, and continuing to do this, till you reach the colour 2 yarn to the correct stitch. 
  5. Row 5: K10 with colour 1, k20 with colour 2. If you have brought yarn forward from the previous row, colour 2 yarn should be available to knit at the correct stitch. 
  6. Row 6: K20 with colour 2, k10 with colour 1. 
  7. Row 7: K18 with colour 1, when you come to colour 2 after 10 stitches (refer to previous row), carry along colour 2 yarn along back of work in the usual way of twining around the colour 2 yarn around main working yarn for 8 more stitches, then k12 with colour 2. 
Continue in this way, bearing in mind to look at the subsequent row and deciding to what stitch number you must carry along the other yarn. I will try and post a photograph of a completed square soon. 


Here is a 3 colour pattern of 30 stitches:


Here is a 4-colour pattern of 30 stitches:


And then, the obvious next step is that you can start putting them together to bring out afghan patterns or wall hangings. Here is one in which 9 different squares of 100 stitches on standard 4 mm needles can be put together, with 3 colours (all the 9 squares are unique): 


Here is one in which the same square is knitted 9 times, and put together one after the other:


And here is one in which the same square can be knitted 12 times, and you can rotate, invert, or do anything you like to produce interesting variations:


Here is where you can go crazy, set n = 300 (the number of stitches, say for a full afghan on 4 mm needles), set k = 9 (this means you are looking at 9 partitions of the number 300, and using 10 colours to represent them) and follow the number chart to produce this: 


At this point you must be thinking, as the numbers and sizes go larger, how do we follow the pattern as we knit? The program below produces a matrix of numbers called R, along with the visual representation. After you choose and fix a colour paletter, R tells you the count of stitches, row by row, that you knit for each successive colour. So, for a 3 colour pattern, if the first row of R = [4, 15], this means knit 4 stitches in colour A, then knit upto the 15th stitch with colour B, and the last remaining stitches in colour C. A bit tedious for some, but not a great deal for those who are used to following charts, or doing any kind of fair-isle, or lace knitting. 

What excites me most about this kind of designing, is that, just like many known biological or mathematical generative rule based systems, all the patterns look "similar" or belonging to the same "style" or "typology" of patterns, and yet, no two of them are exactly alike! Instead of the randomization idea, the other extreme would be use a regularization idea, and put together partitions in some deterministic organized and growing way, so that regular visual patterns can emerge.

If anyone decides to knit anything using these ideas, I would love to see links to your work! If you are unable to run programs and would like me to send you some R matrices and charts, set to some number specifications, please do let me know. If someone develops another version of the program, say in something universally available, like Python, that would be most exciting too! I think the code is working fine, but if there are errors, let me know too. Enjoy :)

Ravelry link to pattern.

PS: Special note for those new to garter stitch special properties: Each coloured row in any of the above patterns here to be produced by using two rows of garter knitting, that will produce 1 "ridge". Thus, each coloured row means two rows of knitting. Carry along the yarns as needed for the next row. 


function [A,R] = randomizedPartitionSquare(n,k)
%Function to generate randomized partition squares
% Author: Somwrita Sarkar
% Version: 1
% Date: 6 Jan 2014
% n is the number of stitches, that is the size of your knitted square
% k is the number of partitions. For example, if k = 1, then a number can
% be divided into 2 parts, for example, 5+5 = 10; if k = 2, one possible
% partition is 1 + 1 + 8 = 10; and so on. The number of colors needed to
% knit the square is equal to k+1. 
% Run the program as many number of times as the needed numbers of squares.
% Since each square pattern is going to be different, you can think of
% innovative ways to put them together. 
% Examples to run code:

% Generate initial matrix / square
A = zeros(n,n);

% Now generate the square
for i = 1:n % for each row
    for j = 1:k
        r(j) = floor(rand*n); % generate partition vector
    end
    r = sort(r); % sort it in ascending order
    R(i,:) = r;
    p = 1; % set initial counter for number of columns
    c = 1; % set initial color counter, starting color 1
    for m = 1:k 
        A(i,p:r(m)) = c; % first partition set to color 1
        p = r(m)+1; % then add number of columns, i.e., for next loop, p goes to (r(1) + 1:r(2));
        c = c+1; % for next loop, next color, i.e., color 1 goes to color 2, etc. 
    end
end

figure
imagesc(A)

% % Initial code for a 3 partition
% for i = 1:n
% r1 = floor(rand*30); % partition 1
% r2 = floor(rand*30); % partition 2
% if r1 < r2
% A(i, 1:r1) = 1;
% A(i,(r1+1):r2) = 2;
% end
% if r1 > r2
% A(i, 1:r2) = 1;
% A(i,(r2+1):r1) = 2;
% end
% end
% figure
% imagesc(A)

end

Thursday 2 January 2014

Moderne, Part 2

Mindless and meditative holiday knitting, as I play with my little man, write some code, cook, clean home, and do some home-moving packing...Yeah, its growing nicely, from what it was last week. Excited and close to adding in the fourth colour, the dark green!


Do you know what this type of knitting reminds me of, and gives me a feel of? Remember those thick markers, thick brushes, and thick paints in childhood art classes or school time art? There was some strange kind of joy and pleasure in slowly and gradually filling in a blank sheet of paper with solid, opaque colours, and watching an abstract play of colours simply emerge. I would lose myself in the thick quality of the paint, and how it felt on the sheets of paper (this was very a very different feel from say thin water colours on the same sheet of paper). That is what it feels like while knitting something like this: like I am holding a thick marker or brush in my hand, and slowly and surely filling up the blank sheet of air with opaque colour. 

Wednesday 1 January 2014

Permutating Friends of 3 Scarf Pattern

Every once in a while, you "unvent" something, in Elizabeth Zimmerman's words. To me it means new ways of doing old things, but also new ways to explain old things, and seeing relationships between diverse things that may on the face of it have nothing to do with each other.

I was recently playing Friends of 10 with my 5 year old son. The game introduces kids to the total number of permutations or ways in which you can put two positive numbers together to make a bigger number. So, for example, there are 11 ways (not counting commutativity, in math terms, since 1+ 9 is the same as 9+1 in our number system), by which you can make the number 10:

  • 0 + 10
  • 1 + 9
  • 2 + 8
  • 3 + 7
  • 4 + 6
  • 5 + 5
  • 6 + 4
  • 7 + 3
  • 8 + 2
  • 9 + 1
  • 10 + 0
The most interesting thing is that counting this way, there are always an odd number of ways in which you can produce an even number. So, for example, there are 11 ways (count the bullets above) to make the number 10. And, in beautiful contrast, there are always an even number of ways in which you can produce an odd number. So, for example, there are 2 ways to produce the number 1: (0+1), (1+0). 

So, what does this have to do with a pattern? Well, here goes. I was crocheting as I played with him, and I kind discovered this pattern. Then, afterwards, I searched on the net, and found several similar variants, of what is called the "waves" pattern. But, re-discovering it from the first principles, starting from a math game, really tickled me. 

Permutating Friends of 3 Scarf Pattern

We will do friends of 3, and let a slip stitch (ss) stand for 0, single crochet (sc) for 1, double crochet (dc) for 2, and treble crochet (tr) for 3. So, now, lets look at the 4 combinations of making 3: 
  • 0+3
  • 1+2
  • 2+1
  • 3+0
Are you getting the idea? It's so exciting, isn't it? 

Cast on a multiple of 7+1 stitches. For the scarf cast on a number of stitches long enough to cover the length of the scarf, and depending on the needle size.  

Now, a simple two row repeat plus a foundation row, based on our friends of 3 permutations above: 
  • R1: Foundation row: ss into second chain from hook, sc, dc, tr, dc, sc, ss, *ss, sc, dc, tr, dc, sc, ss*, repeat the 7 st pattern from * to *. Turn work. 
  • R2: Chain 4 (stands for the first tr), now, working only in the back loops of the previous row, dc, sc, ss, sc, dc, tr, *tr, dc, sc, ss, sc, dc, tr*, repeat the 7 st pattern from * to *. Turn work. 
  • R3: Chain 1, working only in the front loops of the previous row, ss into first chain from hook, sc, dc, tr, dc, sc, ss, *ss, sc, dc, tr, dc, sc, ss*, repeat the 7 st pattern from * to *. Turn work. 
Repeat rows 2 and 3 for pattern. Make the width of the scarf as thick as you want. Cast off and finish. 

In the first row we go singing: 0-1-2-3-2-1-0
Ravelry link to project

And in the second row we go singing: 3-2-1-0-1-2-3
Ravelry link to project

Ravelry link to project

And every stitch on the two rows together makes friends of 3, so the height effectively remains constant over the two rows, and we, nevertheless, get the waves. Plus, working into the back and front loops gives a raised pronounced look that defines the waves. How exciting! 
Ravelry link to project

By the way, this is just plain old kindergarten math leading to the explanation of a crochet pattern from first principles. There is a whole section in combinatorics and higher mathematics that looks at what is called partitions: computing the numbers of ways you can put together k numbers to add up to a number n. So, technically, we were looking at a 2-way partition. Can we look at multi-way partitions for designing patterns? Always, the simple builds bit by bit to build to the complex, and this never fails to inspire me and put the wonder back into me! Hope you enjoy the pattern!