Nugroho's blog.

Tuesday, May 16, 2017

Walking Star in Delphi's Stringgrid.

This code moves the star from cell to cell on string grid.

I use variable s, an array of string type variable.

For delay, or controlling the speed, I use application.processmessages and sleep() combo command.

This code fills cell with blank (space) value that corresponds with s, except one cell. This one cell then "moves" to the right, into the cell next to it.

For this purpose I declare two integer type variable, sx and sy. This variable add itself by one every step. Based on this two variable, the cell that should be filled with star is decided.



Monday, May 15, 2017

Digit Word.

A digit word is a word where, after possibly removing some letters, you are left with one of the single digits:
ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT or NINE.
For example:
• BOUNCE and ANNOUNCE are digit words, since they contain the digit ONE.
• ENCODE is not a digit word, even though it contains an O, N and E, since they are not in order.
 

Here's my code on Delphi


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    procedure proses;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  digit,cdigit:array[1..9] of string;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.Text:='';
  digit[1]:='one';
  digit[2]:='two';
  digit[3]:='three';
  digit[4]:='four';
  digit[5]:='five';
  digit[6]:='six';
  digit[7]:='seven';
  digit[8]:='eight';
  digit[9]:='nine';
end;

procedure tform1.proses;
var s:string;
    i,j,k,n:integer;
    c:array[1..9]of integer;
    ck:array[1..9]of boolean;
begin
  memo1.Text:='';
  s:=edit1.Text;
  memo1.Lines.Append(s);
  memo1.Lines.Append('');
  n:=length(s);
  for i:=1 to 9 do begin
    cdigit[i]:='';
    c[i]:=1;
    ck[i]:=true;
  end;

  //looking for char
  for i:=1 to 9 do begin
    for j:=1 to length(digit[i]) do begin
      if ck[i]=true then begin
       ck[i]:=false;
       for k:=c[i] to n  do begin
        if s[k]=digit[i][j] then begin
          ck[i]:=true;
          cdigit[i]:=cdigit[i]+s[k];
          c[i]:=c[i]+1;
          break;
        end;
       end;
      end;
    end;
  end;
  //compare
  for i:=1 to 9 do begin
    memo1.Lines.Append(cdigit[i]);
  end;

end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  proses;
end;

end.

.

Wednesday, May 10, 2017

Animation using Matplotlib

Suppossed we want to animate our plot, say f(x) = (x-c)^(2) to see the effect of various c value, we could do it in Python using Matplotlib module.

As we could see at the code below that the animation part is in

ani =  animation.FuncAnimation(fig, animate, np.arange(-10,10), interval =  25, blit=False)

What about our own def? We could call it inside animate and use variable i (defined in ani, the np.arange(-10,10) part) to whatever treatment on our self define function f(x). In this case, I use i as c parameter value. I like the result, :)

Sunday, May 7, 2017

What About Unbounded End?


Yeah, what about it? The previous code have the both end bounded.

If we want a free/unbound end, we could set the condition at the with this properties (or we could choose whatever we like)

dy/dx=0

So we will have

y[1]-y[0]=0
y[0] = y[1]

if we want both free ends, we could set the other end as well

y[n] = y[n-1]

So, we just have to modify the original just a bit.

Beware though, with both ends free, we could lost the strings, :)

Saturday, May 6, 2017

Waves Equation Animation in Python

I use matplotlib module to do the animation.

The main code is in def waves(y0,y1,cb) that use finite difference that solved initial value problem and boundary value problem simultaneously.    

code
from pylab import *
import matplotlib.animation as animation

fig,ax = subplots()

def waves(y0,y1,cb):
    y2 = y0
    for i in range(1,len(y0)-1):
        y2[i] = 2*y1[i]-y0[i]+cb*(y1[i+1]-2*y1[i]+y1[i-1])
    return y2

x   = linspace(0.,1.,20)
dx  = 1./(len(x))
y0  = sin(2*pi*x)
vy0 = 12.

b   = 1./32.  #dt2/dx2
dt  = sqrt(b*dx*dx)
print dt

c   = 1.

cb  = c*b

y1  = y0 + vy0*dt

print y0
print y1

line,   = ax.plot(x,y0)
def animate(i):
    global y0,y1,cb
    y2  = waves(y0,y1,cb)
    y0  = y1
    y1  = y2
    line.set_ydata(y0)
    return line,


#plot (x,y0)

ani =  animation.FuncAnimation(fig, animate, np.arange(1,200), interval =  25, blit=False)

grid(True)
ylim(-10,10)
show()


.

Tuesday, May 2, 2017

Gauss Jordan in Python.


Yeah, it's basically Gauss elimination (or we could call it Gauss Naif :) ) but with slight modification at the end.

So, instead using back substitutions after zeroing the lower triangle, we straight on and zeroing upper triangle as well. As addition, we could normalize the diagonal elements so we have identity matrice.

And all is well, :)



Thursday, April 27, 2017

"Auto" Gauss Naif in Delphi.



After do this in Python, now it's time to bring it back to Delphi, where all of this is started, :)


The heart of code lay on this one

procedure tform1.gauss;
var i,j,k:integer; temp:real;
begin
  for i:=1 to 9 do begin
    for j:= 1 to i do begin
      if t[i,j]<>0 then begin
        temp:=t[i,j];
        for k:= 1 to 10 do begin
          if i=j then
            t[j,k]:=t[j,k]/temp
              else t[i,k]:=t[i,k]/temp - t[j,k];
        end;
      end;
    end;
  end;
  //back subtitution
  for i:=9 downto 1 do begin
    x[i]:=t[i,10];
    for j:=9  downto i do begin
      if i<>j then
        x[i]:=x[i]-x[j]*t[i,j];
    end;
  end;



You could say that it consists of zeroing lower tringle and normalizing the diagonal and then subtituting the value.

There's little failsafe code here, that is if we already have zero cell, don't proceed, or it will gave divided by zero error.

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)