2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,17 +1,24 @@
|
|||
The Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence [http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf as described here]:
|
||||
The Fibonacci Word may be created in a manner analogous to the Fibonacci Sequence [http://hal.archives-ouvertes.fr/docs/00/36/79/72/PDF/The_Fibonacci_word_fractal.pdf as described here]:
|
||||
|
||||
: Define F_Word<sub>1</sub> as 1;
|
||||
: Define F_Word<sub>2</sub> as 0;
|
||||
: Form F_Word<sub>3</sub> as F_Word<sub>2</sub> concatenated with F_Word<sub>1</sub> i.e "01"
|
||||
: Form F_Word<sub>n</sub> as F_Word<sub>n-1</sub> concatenated with F_word<sub>n-2</sub>
|
||||
Define F_Word<sub>1</sub> as '''1'''
|
||||
Define F_Word<sub>2</sub> as '''0'''
|
||||
Form F_Word<sub>3</sub> as F_Word<sub>2</sub> concatenated with F_Word<sub>1</sub> i.e.: '''01'''
|
||||
Form F_Word<sub>n</sub> as F_Word<sub>n-1</sub> concatenated with F_word<sub>n-2</sub>
|
||||
|
||||
For this task we shall do this for n = 37. You may display the first few but not the larger values of n, doing so will get me into trouble with them what be (again!).
|
||||
|
||||
Instead create a table for F_Words 1 to 37 which shows:
|
||||
:The number of characters in the word
|
||||
:The word's [[Entropy]].
|
||||
;Task:
|
||||
Perform the above steps for n = 37.
|
||||
|
||||
Related Tasks:
|
||||
You may display the first few but not the larger values of n.
|
||||
<br><small>{Doing so will get the task's author into trouble with them what be (again!).} </small>
|
||||
|
||||
:::* [[Entropy]]
|
||||
:::* [[Entropy/Narcissist]]
|
||||
Instead, create a table for F_Words '''1''' to '''37''' which shows:
|
||||
::* The number of characters in the word
|
||||
::* The word's [[Entropy]]
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Fibonacci_word/fractal|Fibonacci word/fractal]]
|
||||
* [[Entropy]]
|
||||
* [[Entropy/Narcissist]]
|
||||
<br><br>
|
||||
|
|
|
|||
3
Task/Fibonacci-word/APL/fibonacci-word.apl
Normal file
3
Task/Fibonacci-word/APL/fibonacci-word.apl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
F_WORD←{{⍵,,/⌽¯2↑⍵}⍣(0⌈⍺-2),¨⍵}
|
||||
ENTROPY←{-+/R×2⍟R←(+⌿⍵∘.=∪⍵)÷⍴⍵}
|
||||
FORMAT←{'N' 'LENGTH' 'ENTROPY'⍪(⍳⍵),↑{(⍴⍵),ENTROPY ⍵}¨⍵ F_WORD 1 0}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
defmodule RC do
|
||||
def entropy(str) do
|
||||
leng = String.length(str)
|
||||
String.to_char_list(str)
|
||||
|> Enum.reduce(Map.new, fn c,acc -> Dict.update(acc, c, 1, &(&1+1)) end)
|
||||
|> Dict.values
|
||||
String.to_charlist(str)
|
||||
|> Enum.reduce(Map.new, fn c,acc -> Map.update(acc, c, 1, &(&1+1)) end)
|
||||
|> Map.values
|
||||
|> Enum.reduce(0, fn count, entropy ->
|
||||
freq = count / leng
|
||||
entropy - freq * :math.log2(freq) # log2 was added with Erlang/OTP 18
|
||||
|
|
|
|||
35
Task/Fibonacci-word/Lua/fibonacci-word.lua
Normal file
35
Task/Fibonacci-word/Lua/fibonacci-word.lua
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
-- Return the base two logarithm of x
|
||||
function log2 (x) return math.log(x) / math.log(2) end
|
||||
|
||||
-- Return the Shannon entropy of X
|
||||
function entropy (X)
|
||||
local N, count, sum, i = X:len(), {}, 0
|
||||
for char = 1, N do
|
||||
i = X:sub(char, char)
|
||||
if count[i] then
|
||||
count[i] = count[i] + 1
|
||||
else
|
||||
count[i] = 1
|
||||
end
|
||||
end
|
||||
for n_i, count_i in pairs(count) do
|
||||
sum = sum + count_i / N * log2(count_i / N)
|
||||
end
|
||||
return -sum
|
||||
end
|
||||
|
||||
-- Return a table of the first n Fibonacci words
|
||||
function fibWords (n)
|
||||
local fw = {1, 0}
|
||||
while #fw < n do fw[#fw + 1] = fw[#fw] .. fw[#fw - 1] end
|
||||
return fw
|
||||
end
|
||||
|
||||
-- Main procedure
|
||||
print("n\tWord length\tEntropy")
|
||||
for k, v in pairs(fibWords(37)) do
|
||||
v = tostring(v)
|
||||
io.write(k .. "\t" .. #v)
|
||||
if string.len(#v) < 8 then io.write("\t") end
|
||||
print("\t" .. entropy(v))
|
||||
end
|
||||
|
|
@ -1,34 +1,32 @@
|
|||
/*REXX program lists number of chars in a fibonacci word, the word's entropy. */
|
||||
d=20; de=d+6; numeric digits d /*use more precision (the default is 9)*/
|
||||
parse arg N . /*get optional argument from the C.L. */
|
||||
if N=='' then N=42 /*Not specified? Then use the default.*/
|
||||
@.1=1; @.2=0 /*define some initial values of FIBword*/
|
||||
say center('N',5) center('length',12) center('entropy',de) center('Fib word',56)
|
||||
say copies('─',5) copies('─' ,12) copies('─' ,de) copies('─' ,56)
|
||||
/* [↓] display N fibonacci words. */
|
||||
do j=1 for N; j1=j-1; j2=j-2 /*use temporary variables for @ indices*/
|
||||
if j>2 then @.j=@.j1 || @.j2 /*calculate the FIBword if we need to.*/
|
||||
L=length(@.j)
|
||||
if L<56 then Fw= @.j
|
||||
else Fw= '{the word is too wide to display.}'
|
||||
say right(j,4) right(L,12) ' ' entropy() ' ' Fw; drop @.j2
|
||||
end /*j*/ /*display text msg; free memory of @.j2*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
entropy: if L==1 then return left(0,d+2) /*handle special case of 1 character*/
|
||||
!.0=length(space(translate(@.j, , 1), 0)) /*this is a fast way to count zeroes*/
|
||||
!.1=L-!.0 /*also, calculate the number of ones*/
|
||||
S=0; do i=1 for 2; _=i-1 /*construct character from the ether*/
|
||||
S=S-!._/L*log2(!._/L) /*add (negatively) the entropies. */
|
||||
end /*i*/
|
||||
if S=1 then return left(1,d+2) /*return a left─justified "1" (one).*/
|
||||
return format(S,,d) /*normalize the sum (S) number. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
log2: procedure; parse arg x 1 xx; ig= x>1.5; is=1-2*(ig\==1); ii=0
|
||||
numeric digits digits()+5 /* [↓] precision of E must be >digits().*/
|
||||
e=2.7182818284590452353602874713526624977572470936999595749669676277240766303535
|
||||
do while ig & xx>1.5 | \ig&xx<.5; _=e; do j=-1; iz=xx* _**-is
|
||||
if j>=0 then if ig & iz<1 | \ig&iz>.5 then leave; _=_*_; izz=iz; end /*j*/
|
||||
xx=izz; ii=ii+is*2**j; end /*while*/; x=x* e**-ii-1; z=0; _=-1; p=z
|
||||
do k=1; _=-_*x; z=z+_/k; if z=p then leave; p=z; end /*k*/
|
||||
r=z+ii; if arg()==2 then return r; return r/log2(2,0)
|
||||
/*REXX program displays the number of chars in a fibonacci word, and the word's entropy.*/
|
||||
d=20; de=d+6; numeric digits de /*use more precision (the default is 9)*/
|
||||
parse arg N . /*get optional argument from the C.L. */
|
||||
if N=='' | N=="," then N=42 /*Not specified? Then use the default.*/
|
||||
say center('N', 5) center("length", 12) center('entropy', de) center("Fib word", 56)
|
||||
say copies('─', 5) copies("─" , 12) copies('─' , de) copies("─" , 56)
|
||||
c=1 /* [↓] display N fibonacci words. */
|
||||
do j=1 for N; if j==2 then c=0 /*test for the case of J equals 2. */
|
||||
if j==3 then parse value 1 0 with a b /* " " " " " " " 3. */
|
||||
if j>2 then c=b || a; L=length(c) /*calculate the FIBword if we need to.*/
|
||||
if L<56 then Fw= c
|
||||
else Fw= '{the word is too wide to display, length is: ' L"}"
|
||||
say right(j,4) right(L,12) ' ' entropy() " " Fw
|
||||
a=b; b=c /*define the new values for A and B.*/
|
||||
end /*j*/ /*display text msg; */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
entropy: if L==1 then return left(0, d+2) /*handle special case of one character.*/
|
||||
!.0=length( space( translate(c,,1), 0)) /*efficient way to count the "zeroes".*/
|
||||
!.1=L-!.0; $=0; do i=1 for 2; _=i-1 /*construct character from the ether. */
|
||||
$=$ -!._/L*log2(!._/L) /*add (negatively) the entropies. */
|
||||
end /*i*/
|
||||
if $=1 then return left(1, d+2) /*return a left─justified "1" (one). */
|
||||
return format($,,d) /*normalize the sum (S) number. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
log2: procedure; parse arg x 1 xx; ig=x>1.5; is=1-2*(ig\==1); numeric digits 5+digits()
|
||||
e=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759
|
||||
m=0; do while ig & xx>1.5 | \ig&xx<.5; _=e; do j=-1; iz=xx* _ ** - is
|
||||
if j>=0 then if ig & iz<1 | \ig&iz>.5 then leave; _=_*_; izz=iz; end /*j*/
|
||||
xx=izz; m=m+is*2**j; end /*while*/; x=x* e** -m -1; z=0; _=-1; p=z
|
||||
do k=1; _=-_*x; z=z+_/k; if z=p then leave; p=z; end /*k*/
|
||||
r=z+m; if arg()==2 then return r; return r / log2(2,.)
|
||||
|
|
|
|||
35
Task/Fibonacci-word/ZX-Spectrum-Basic/fibonacci-word.zx
Normal file
35
Task/Fibonacci-word/ZX-Spectrum-Basic/fibonacci-word.zx
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
10 LET x$="1": LET y$="0": LET z$=""
|
||||
20 PRINT "N, Length, Entropy, Word"
|
||||
30 LET n=1
|
||||
40 PRINT n;" ";LEN x$;" ";
|
||||
50 LET s$=x$: LET base=2: GO SUB 1000
|
||||
60 PRINT entropy
|
||||
70 PRINT x$
|
||||
80 LET n=2
|
||||
90 PRINT n;" ";LEN y$;" ";
|
||||
100 LET s$=y$: GO SUB 1000
|
||||
110 PRINT entropy
|
||||
120 PRINT y$
|
||||
130 FOR n=1 TO 18
|
||||
140 LET x$="1": LET y$="0"
|
||||
150 FOR i=1 TO n
|
||||
160 LET z$=y$+x$
|
||||
170 LET p$=x$: LET x$=y$: LET y$=p$
|
||||
180 LET p$=y$: LET y$=z$: LET z$=p$
|
||||
190 NEXT i
|
||||
200 LET x$="": LET z$=""
|
||||
210 LET s$=y$: GO SUB 1000
|
||||
220 PRINT n+2;" ";LEN y$;" ";entropy
|
||||
230 PRINT y$ AND (LEN y$<32)
|
||||
240 NEXT n
|
||||
250 STOP
|
||||
1000 REM Calculate entropy
|
||||
1010 LET sourcelen=LEN s$: LET entropy=0
|
||||
1020 DIM t(255)
|
||||
1030 FOR j=1 TO sourcelen
|
||||
1040 LET digit=VAL s$(j)+1: LET t(digit)=t(digit)+1
|
||||
1050 NEXT j
|
||||
1060 FOR j=1 TO 255
|
||||
1070 IF t(j)>0 THEN LET prop=t(j)/sourcelen: LET entropy=entropy-(prop*LN (prop)/LN (base))
|
||||
1080 NEXT j
|
||||
1090 RETURN
|
||||
Loading…
Add table
Add a link
Reference in a new issue