Faz qualquer coisa...

Sequência de Kolakoski
Notas sobre a sequência de Kolakoski

2019/12/17-20:07:54

A sucessão de Kolakoski1 é definida por:

Um algoritmo simples é o seguinte (em 2 está a versão em GNU/Octave):

a=[1 2 2]
  i=3:n
    j=1:a(i)
      a=[a 1+mod(i-1,2)]

o que dá

 1  2 2  1 1  2  1  2 2  1  2 2  1 1  2
 -  ---  ---  -  -  ---  -  ---  ---  - (...)  
 1   2    2   1  1   2   1   2    2   1   

O algoritmo anterior está definido de uma forma imperativa e tem uma tradução para LISP com forma

(defun kolakoski. (n)
  "Kolakoski sequence."
  (let ((a '(1 2 2)))
    (do ((i 2 (+ i 1)))
        ((= i n))
      (do ((j 1 (+ j 1)))
          ((= j (+ 1 (car (nthcdr i a)))))
        (setq a (concatenate 'list a (list (+ 1 (mod i 2)))))))
    a))

Vejamos então como construir uma forma funcional para gerar a sequência. A ideia é obter o valor de m (ver figura acima) definido como o mínimo, fixado um n, de todas as somas maiores ou iguais a n o valor do n-ésimo termo da sucessão é então dado por

((lambda (x)  (/ (+ 3 (expt -1 x)) 2)) 'm)

. Antes de construirmos essa função precisamos de algumas funções auxiliares

(defun sum (x)
  "Sum of elements of X."
  (cond (x
         (+ (car x) (sum (cdr x))))
        (t 0)))

(defun lin-seq (n)
  "Retuns (1 2 3 ... n)."
  (cond ((= n 1)
         (list 1))
        (t
         (append (lin-seq (- n 1)) (list n)))))

(defun nest (op xo n)
  "Applies OP to XO N times."
  (cond ((= n 1)
        xo)
        (t (nest op (funcall op xo) (- n 1)))))

(defun partial-sum (lst)
  "Partial sums of LST."
  (reverse (maplist #'sum (reverse lst))))

Vejamos então como funciona a função find-m definida por

(defun find-m (lst n)
  (+ 1 (length  
        (mapcan #'(lambda (y) (and y (list y)))
                (mapcar 
                 #'(lambda (x) (<= x n)) (partial-sum lst))))))
É composta de duas partes: dada uma lista lst, a primeira, verifica se a condição <=n x é t ou nil, por exemplo, dada uma lista (1 2 3 4) com somas parciais (1 3 6 10) a aplicação da condição (< x 3)=, onde x é o valor de cada entrada da lista, dá
> (mapcar #'(lambda (x) (<= x 3)) (partial-sum '(1 2 3 4)))

(t t nil nil)
A segunda parte da função remove as entradas nil através de
> (mapcan #'(lambda (y) (and y (list y))) '(t t nil nil))

(t t)
e, logo, o valor de m é o comprimento desta última lista mais um.

De seguida verifica-se se está tudo bem calculando o valor dos sucessivos m

> (mapcar #'(lambda (x) (find-m (kolakoski. 20) x)) 
                '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20))

(1 2 2 3 3 4 5 6 6 7 8 8 9 9 10 11 11 12 12 13 14)
e desfaz-se a transformação de modo a obter a sequência original
> (kolakoski. 20)

(1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 2 1 1 2 1 2 2 1 1 2)
através de
> (mapcar #'(lambda (x)  (/ (+ 3 (expt -1 x)) 2))
        (mapcar #'(lambda (x) (find-m (kolakoski. 20) x)) 
                '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)))

(1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 2)

A construção da sequência de Kolakoski vem então definida por

(defun kolakoski (seq)
  (append seq (list (/ (+ 3 (expt -1
                        (find-m seq  (length seq) ))) 2))))
Exemplificando
(kolakoski '(1 2 2))
e
> (nest 'kolakoski '(1 2 2) 20)

(1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1 2 1)

1. The American Mathematical Monthly, Vol. 72, No. 6 (Jun. - Jul., 1965), pp. 673-67

2.

function retval = kolakoski (n)
  retval=[1 2 2];
  for i=3:n
    for j=1:retval(i)
      retval=[retval 1+mod((i-1),2)];
    endfor;
  endfor;
endfunction;

3. O número de termos não está correcto, a sequência final tem 22 termos e não 20, isto resolve-se com a indexação correcta de cada termo (índice que indexa as entradas de cada lista começa em 0).

Etiquetas/Tags: Kolakoski, Lisp, matemática, self-generating, GNU/Octave


Movimento
Movimento com atrito de um ponto material sobre uma superfície sujeito apenas ao seu próprio peso

2019/12/17-19:28:38

latex2png equation

Ref.: Principle of least constraint: Equações de movimento e resolução numérica do movimento de um ponto material sobre uma superfície

Etiquetas/Tags: matemática, física, Gauss, Principle of least constraint


Almost a unison
Almost a unison from a harmonograph

2019/11/20-22:03:44

Almost a Unison.
Almost a Unison.

Etiquetas/Tags: unison, harmonograph


Almost a unison, again
Almost a unison from a harmonograph

2019/11/20-22:03:37

Almost a Unison.
Almost a Unison.

Etiquetas/Tags: unison, harmonograph


POV-Ray ray tracing program
... which generates images from a text-based scene description

2019/11/20-11:38:51

#version 3.6; // 3.7;
#global_settings{ assumed_gamma 1.0 }
#default{ finish{ ambient 0.1 diffuse 0.9 }}

#include "colors.inc"
#include "textures.inc"

//------------------------------------------------------------
// camera
//------------------------------------------------------------

#declare Cam0 = camera {perspective angle 40
                        location  < 0.0 , 4.5 ,8>
                        right     x*image_width/image_height
                        look_at   <0.4 , 1 , 0.0>}
camera{Cam0}
// sun ---------------------------------------------------------------
light_source{<1500,2500,-2500> color rgb<1,1,1>}
// sky ---------------------------------------------------------------
sphere{<0,0,0>,1 hollow
    texture{pigment{gradient <0,1,0>
        color_map{[0.0 color White  ]
          [0.2 color SkyBlue]
          [.9 color rgb<0,.1,1>]
          
          }
                       quick_color White }
               finish {ambient .5 diffuse 1}
               }
       scale 10000}
//------------------------------------------------------------
// the Water
//------------------------------------------------------------

difference{
plane{<0,1,0>, 0 }

   texture{Polished_Chrome
                    normal { crackle 1 scale 10
                             turbulence 1 translate<0,-10,0>}
                    finish { diffuse 0.5 reflection .75}}
          }

#version 3.6; // 3.7;
#global_settings{ assumed_gamma 1.0 }
#default{ finish{ ambient 0.1 diffuse 0.9 }}

#include "colors.inc"
#include "textures.inc"

//------------------------------------------------------------
// camera
//------------------------------------------------------------

#declare Cam0 = camera {perspective angle 30
                        location  < 0.0 , 4.5 ,8>
                        right     x*image_width/image_height
                        look_at   <0.4 , 1 , 0.0>}
camera{Cam0}

//------------------------------------------------------------
//sun
//------------------------------------------------------------
//light_source{<1500,2500,-2500> color rgb<1,1,1>}
light_source{<100,200,-200> color rgb<1,1,1>}

//------------------------------------------------------------
// sky
//------------------------------------------------------------
sphere{<0,1.2,-5>,1 hollow
    texture{pigment{gradient <1,1,0>
        color_map{[0.0 color White  ]
          [.5 color rgb<1,1,1>]

          
          }
      quick_color White }
    finish {ambient .5 diffuse 1}
  }
  scale 1}

sphere{<3,2,-4>,1 hollow
    texture{pigment{gradient <1,1,0>
        color_map{[0.0 color Red  ]
          [.5 color rgb<1,0,0>]

          
          }
      quick_color White }
    finish {ambient .5 diffuse 1}
  }
  scale .618}

//------------------------------------------------------------
// Water
//------------------------------------------------------------

difference{
plane{<0,1,0>, 0 }

   texture{Polished_Chrome
                    normal { crackle 1 scale 7
                             turbulence 5 translate<0,-10,0>}
                    finish { diffuse 0.5 reflection .75}}
          }// end of difference

Ref.: https://www.povray.org/

Etiquetas/Tags: povray, images, ray tracing


BoomBox
What was the fastest-adopted gadget of the last 50 years?

2019/06/15-23:13:31

Pop quiz. What was the fastest-adopted gadget of the last 50 years? The Color TV? The mobile phone? The DVD player? No, believe it or not, it was the boombox. — Sprite

Etiquetas/Tags: hifi, audio, boombox, diy


Light_00
... my take

2019/05/19-00:52:30

Light_00 is the winning entry to the Distributed Design Challenge 2018 which was part of the London design festival. One open source product design is shared amongst platform members across Europe, who each fabricate that same design using local materials. Being open source, the design may be changed or updated to allow the best possible version to be created at each location.

My take on it!

Happy hacking!

// Author: Tiago Charters de Azevedo 
// Maintainer: Tiago Charters de Azevedo 

// Copyright (c) - 2018 Tiago Charters de Azevedo

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

////////////////////////////////////////////////////////////
// https://www.milomg.com/Light_00
// Light_00 is the winning entry to the
// Distributed Design Challenge 2018
// which was part of the London design festival. 
////////////////////////////////////////////////////////////
// One open source product design is shared amongst
// platform members across Europe, who each fabricate that
// same design using local materials. Being open source,
// the design may be changed or updated to allow the best
// possible version to be created at each location.
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// My take on it!
// Happy hacking!
////////////////////////////////////////////////////////////


$fn=64;
phi=(1+sqrt(5))/2;

// PVC tube
//d=16;
inch=16.4;
//inch=25.4;
r=inch/2;
thickness=.4*5;//inch*phi/10;
ro=r+thickness;

L=2*pow(inch,2)/10;

theta=0;
alpha=-25.4;


////////////////////////////////////////////////////////////
module huba(){
    difference(){
        union(){
            difference(){
                hull(){
                    translate([0,0,L]) sphere(ro-.01);
                    translate([0,0,L/(3*2)]) sphere(ro-.01);}
                hull(){
                    translate([0,0,L/2-6]) sphere(r);
                    translate([0,0,-L/(3*2)]) sphere(r);}}
            
            
            translate([0,0,L])
            rotate([-alpha*2,0,0])
            translate([0,0,-r])
            hull(){
                translate([0,0,L/2]) sphere(ro+.01);
                translate([0,0,-L/2]) sphere(ro+.01);}}
        
        translate([0,0,L])
        rotate([-2*alpha,0,0])
        translate([0,0,-r])
        hull(){
            translate([0,0,L/2+r]) sphere(r);
            translate([0,0,-L/2-r]) sphere(r);}}}

module hubb(){
    A=[0,0,L/2];
    B=[L*cos(90-alpha),0,-L/2];

    difference(){
        hull()  {
        translate([0,0,L/2]) sphere(ro);
        for(i=[0:2]){
            rotate([0,0,i*360/3])
            translate(B)
            sphere(ro);}}

    hull(){
        translate([0,0,L/1.5]) sphere(r);
        translate([0,0,L/(3*2)]) sphere(r);}
    
    for(i=[0:2]){
        rotate([0,0,i*360/3])
        hull(){
            translate(B+(B-A)/4) sphere(r);
            translate((B-A)/1.5+A) sphere(r);}}}}


module hubbvar(){
    A=[0,0,L/2];
    B=[L*cos(90-alpha),0,-L/2];

    difference(){
        for(i=[0:2]){
            hull()      {
                translate([0,0,L/2]) sphere(ro);
                rotate([0,0,i*360/3])
                translate(B)
                sphere(ro);}}
        
        hull(){
            translate([0,0,L/1.5]) sphere(r);
            translate([0,0,L/(3*2)]) sphere(r);}
        
        for(i=[0:2]){
            rotate([0,0,i*360/3])
            hull(){
                translate(B+(B-A)/4) sphere(r);
                translate((B-A)/1.5+A) sphere(r);}}}}


module cap(){
    difference(){
        hull(){
            translate([0,0,L/4]) sphere(ro);
            translate([0,0,-L/4]) sphere(ro);}
        hull(){
            translate([0,0,L/4]) sphere(r);
            translate([0,0,-L/2]) sphere(r);}}}

module capj(beta=0){
    difference(){
        hull(){
            translate([0,0,L/4]) sphere(ro);
            translate([0,0,-L/4]) sphere(ro);}
        hull(){
            translate([0,0,L/4]) sphere(r);
            translate([0,0,-L/2]) sphere(r);}

        translate([0,0,L/(2*phi)])
        rotate([90,0,90])
        cylinder(L,r=r/1.5,center=true);}}


module capphone(){
    difference(){
        hull(){
            translate([0,0,L]) sphere(25);
            translate([0,0,-L/4]) sphere(ro);}

        translate([0,0,-L/2]) 
        cylinder(L,r=r,center=true);
    
        translate([0,0,50+thickness]) cube([100,12,100],center=true);
        translate([0,25+5,50+thickness+2*ro])cube([100,50,100],center=true);


    }}

////////////////////////////////////////////////////////////

module show(){
    hubbvar();
    pipe=250;
    A=[0,0,L/2];
    B=[L*cos(90-alpha),0,-L/2];
    
    
    color([0.80392,0.66667,0.49020])
    for(i=[0:2]){
        rotate([0,0,i*360/3])
        hull(){
            translate(B+pipe*(B-A)/norm(B-A)) sphere(r);
            translate((B-A)/1.5+A) sphere(r);}}
    

    color([0.80392,0.66667,0.49020])
    hull(){
        translate([0,0,pipe]) sphere(r);
        translate([0,0,L/(3*2)]) sphere(r);}
    
    
    translate([0,0,pipe]) {
        huba();
        
        color([0.80392,0.66667,0.49020]){    
            translate([0,0,L])
            rotate([-alpha*2,0,0])
            translate([0,0,-r])
            hull(){
                translate([0,0,pipe/2]) sphere(r);
                translate([0,0,-pipe/2]) sphere(r);}}}

    for(i=[0:2]){
        rotate([0,0,i*360/3])
        translate(B+(pipe-inch)*(B-A)/norm(B-A)) rotate([0,180-alpha,0]) cap();}

    translate([0,0,pipe+L])
    rotate([-alpha*2,0,0])
    translate([0,0,0])
    translate([0,0,pipe/2]) capphone();

    translate([0,0,pipe+L])
    rotate([-alpha*2,0,0])
    translate([0,0,-r])
    translate([0,0,-pipe/2]) rotate([180,0,0])capj();
    

translate([0,-(pipe)*cos(-alpha)/2,L+(pipe)*sin(-alpha)-35])
rotate([0,0,180])
translate([-77.8/2,-14,pipe+8])
rotate([-alpha+14,0,0])
color("Red") import("iphone.stl");
}

show();


////////////////////////////////////////////////////////////
// Printed parts
////////////////////////////////////////////////////////////

// huba();
// hubbvar();
// capj();
// cap();
//capphone();

Etiquetas/Tags: 3dprinting, design, open source, hack, light, lamp


Some ideas for a smart bike pollution meter

2019/04/14-22:32:22

Hardware:

Some refs.:

Etiquetas/Tags:


Celebrate the Riemann Hypothesis proof
... and print the graph of the Gamma function

2018/09/24-16:06:43

Get it here: https://www.youmagine.com/designs/complex-gamma-function

Etiquetas/Tags: 3d, 3dprint, reprap, math, Riemann


Two motors, two currents, two frequencies
... simple experiment

2018/09/18-13:50:06

Etiquetas/Tags: dc motor, dc, motor, electronics


Palavras chave/keywords: página pessoal, blog

Criado/Created: NaN

Última actualização/Last updated: 18-12-2019 [12:40]


Voltar à página inicial.


GNU/Emacs Creative Commons License

(c) Tiago Charters de Azevedo