domingo, 19 de junio de 2016

LINQ Recipe No. 2-10: Recursive Series and Patterns - How to Show the Step-by-Step Growth of Algae

Contents

1. Introduction
2. Keywords
3. Problem
4. Solution
5. Discussion
5.1 Fibonacci series
6. Practic: Step-by-Step Algae Growth
7. Conclusions
8. Literature & Links

1. Introduction

What if we want to see the steps of algae during its growth? This LINQ recipe will modify slightly the previous one to store in a KeyValuePair data structure each of those steps which experiments the algae before reach a certain point of its development. A more interesting code will demonstrate that the length of the algae at each growth stage forms a Fibonacci number.

2. Keywords

  • Cellular biology
  • Data structure
  • Fibonacci

3. Problem

Show the growth of the algae at each development stage.

4. Solution

At each iteration (development stage) both the iteration number and the algae growth will be store in a KeyValuePair data structure.

5. Discussion

5.1 Fibonacci series

The Fibonacci serie is sequence of integer positive values. These numbers are defined by the recursive or recurrence relation ("Fibonacci number", 2016) 

Fibonacci recurrence relation
The base cases or seed values are 

For example, these are the first twelve Fibonacci numbers: 

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

There are a lot of interesting mathematical facts which are based on this recursive relation; for example: 
  • Divisibility properties, 
  • Fibonacci primes, 
  • Periodicity modulo n
  • Primality testing
But this serie is expressed in nature: the length of the algae string is at each growth stage is equal to Fibonacci numbers. The coming up recipe demonstrates this by modifying slightly the code from the previous recipe.

6. Practice: Step-by-Step Algae Growth

This LINQ recipe show the algal growth by stages: 

// The axiom - initial string:
string algae = "A";

// Replace functions:
Func<string, string> transformA = x => x.Replace("A", "AB");
Func<string, string> markBs = x => x.Replace("B", "[B]");
Func<string, string> transformB = x => x.Replace("[B]", "A");

// Number of iterations:
int length = 7;
Enumerable.Range(1, length)
.Select(iteration => new KeyValuePair<int, string>(
iteration, algae = transformB(transformA(markBs(algae)))))
.Dump("Plant Cells Growth at Each Stage");

Output in LINQPad
Step-by-Step algae growth
Illustration 1. Step-by-Step algae growth.
An alternative solution which computes the algal string length is 

// The axiom - initial string:
string algae = "A";

// Replace functions:
Func<string, string> transformA = x => x.Replace("A", "AB");
Func<string, string> markBs = x => x.Replace("B", "[B]");
Func<string, string> transformB = x => x.Replace("[B]", "A");

// Number of iterations:
int length = 7;
Enumerable.Range(1, length)
.Select(iteration => new Tuple<int, string, int>(
iteration, algae = transformB(transformA(markBs(algae))), algae.Length))
.Dump("Plant Cells Growth at Each Stage with Its Length");

Note that for this case, the Tuple generic structure is use to store the required three values: iteration number, the algae growth, and the length of the string.
Illustration 2. Step-by-Step algae growth with string length.

7. Conclusions

We have understood how the string length of the algae growth is intrinsically related with the Fibonacci series: each Fibonacci number corresponds to the algae string length at certain iteration number.

In the next we will explore how to generate commands to draw a Koch curve.

8. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
Fibonacci number (2016, June 19). Retrieved from: https://en.wikipedia.org/wiki/Fibonacci_number


V

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.