Nugroho's blog.: programming
Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Thursday, March 14, 2019

Create Res2DMod File from Delphi

I post the full source code below

 
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit13: TEdit;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Button1: TButton;
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i,j:integer;
begin
  edit1.Text:='Fault Model';
  edit2.Text:='51';
  edit3.Text:='15';
  edit4.Text:='0';
  edit5.Text:='1.0';
  edit6.Text:='2';
  edit7.Text:='0,200,3';
  edit8.Text:='4';
  edit9.Text:='200.0, 350.0, 175.0';
  edit10.Text:='14';
  edit11.Text:='0.2500, 0.5000, 0.7125, 1.1875, 1.6875, 2.3125,'+
                '3.1875, 4.4375, 6.4375, 10.4375, 18.4375, 34.4375,'+
                '66.4375, 130.4375 ';
  edit12.Text:='3';
  edit13.Text:='0';
  edit14.Text:='0';
  edit15.Text:='0';
  edit16.Text:='0';
  edit17.Text:='0';
  for i:=0 to 13 do begin
    for j:=0 to 209 do begin
      stringgrid1.Cells[j,i]:='0';
    end;
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  if stringgrid1.Cells[ACol,Arow]='0' then
    stringgrid1.Cells[ACol,Arow]:='1' else
      stringgrid1.Cells[ACol,Arow]:='0';
end;

procedure TForm1.Button1Click(Sender: TObject);
var f:textfile;
    s:string;
    i,j:integer;
begin
  assignfile(f,'data.mod');
  rewrite(f);
  writeln(f,edit1.text);
  writeln(f,edit2.text);
  writeln(f,edit3.text);
  writeln(f,edit4.text);
  writeln(f,edit5.text);
  writeln(f,edit6.text);
  writeln(f,edit7.text);
  writeln(f,edit8.text);
  writeln(f,edit9.text);
  writeln(f,edit10.text);
  writeln(f,edit11.text);
  for i:=0 to 13 do begin
    s:='';
    for j:=0 to 209 do begin
      s:=s+stringgrid1.Cells[j,i];
    end;
    writeln(f,s);
  end;
  writeln(f,edit12.text);
  writeln(f,edit13.text);
  writeln(f,edit14.text);
  writeln(f,edit15.text);
  writeln(f,edit16.text);
  writeln(f,edit17.text);

  closefile(f);
end;

end.

.

Parsing a String to Get a certain Substring (Serial Number, Product Keys, etc)

I have a question from a new friend on my instagram

Basically, he needs only certain substring.

Here's my code to parse the string.

 

The main code is

s          := edit1.text;
p          := pos('ME',s);
edit2.text := copy(s,p+2,13);
.

Monday, June 5, 2017

Using Kivy on MacOS


Here's my first kivy program after a while.



Thursday, May 18, 2017

Playing with Memo in Delphi


I change the font in memo (tMemo) into courier. With this change, it's easy to do string manipulation with old Pascal style.

Below, I create small program with read input from edit into n variable. The input can only contain number 1 to 9.

The output is displayed on Memo. It's just number 123456789 (yeah, it has type string, but its number).

It's that all? No. The number's forming a 'cross' centered on number specified in edit. :)



Palindrom Checker


This simple program is using an edit as input, a button to trigger processing and a memo for output.

I used edit.text as s value and then reverse its value using for command and saved to rs variable.

We compared s and rs to determine that s is palindrom or not.



Here's the code

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}
procedure tform1.proses;
var s,rs:string;
  i:integer;
  palindrom:boolean;
begin
  memo1.Text:='';
  palindrom:=true;
  s:=edit1.Text;
  for i:=length(s) downto 1 do begin
    rs:=rs+s[i];
  end;
  for i:=1 to length(s) do begin
    if s[i]<>rs[i] then begin
      palindrom:=false;
      break;
    end;
  end;
  memo1.Lines.Append(s);
  memo1.Lines.Append(rs);
  if palindrom=true then
    memo1.Lines.Append('is palindrom')
      else
        memo1.Lines.Append('is not palindrom');

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  memo1.Text:='';
end;

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

end.


.

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.

Gauss Naif in Python

Okay, we've done the manual one, how about automatize it?

It's actually just a matter of finding the pattern on that code and after we found the loop, we just have to well... loop it, :)



Wednesday, April 26, 2017

Manual Gauss Jordan in Python.

What if we didn't do back substitution on Gauss Naif method but eliminate the rest instead? Nah, we get the Gauss Jordan here.

The idea is after we do operation to make the  lower-triangle have zero value,  we continue the operation until all the component in the upper-triangle have zero value too, and the diagonal have value of one.

Basically, the matrix becomes identity matrix. This way, we didn't need subtitution at all since all variables already has the exact value on the right side, :)

Tuesday, April 25, 2017

Manual Gauss Naif Elimination using Python

How about some manual matrix using manual Gauss just like always, but in Python? Okay, here it is.

I use tuple, I think it's just the same as array for this purpose.

I created matrix a with random value.  It's like linear equation system; three unknown variables with three equation. The purpose of this code is to find x1, x2 and x3.

Oh, in this case, its x0, x1 and x2, :)

Monday, April 24, 2017

That's Not Fair!


Maybe that's something come to our mind when we read this code. Yeah, that's forward dfference. It's designed to get the difference value using the point we calculate and the next one. That means the value will "lopsided" by nature, :)



Friday, April 21, 2017

Searching Multiple Roots Numerically.

This Python code only works with function that crossing x-axis.

The idea is we started from x=0 and walking to the positive direction and evaluating f(x) as we walk.

If there's change of the sign of f(x) from + to -, or vice versa, there must be a root in that area.

We began to surround it to find the-x that correspond to f(x)=0. That x value is the root.

After the root is found, we began to walk along x-axis again until found any sign change of f(x), or until x limit set on code.

Thursday, April 20, 2017

Manual Gauss Elimination on 3x3 Matrices in Delphi

I use this code in order to find its pattern.

Yes, there is many Gauss code out there. I plan to write it on next post about it. The dynamic Gauss Elimination code that could be implemented to any size of matrices.

But for now, let just settle on this.

https://youtu.be/csiFpdsrzzQ


Wednesday, April 19, 2017

Lagrange Polynomial Interpolation on Python.


It's a whole a lot easier than Newton's divided differences interpolation polynomial, because there is no divided difference part that need a recursive function.



Sunday, April 16, 2017

Differentiation

We may already knew that on computational physic,  differentiation is used in Euler method to compute integration, or used in finite different method.

How about slope of the function? How do we use differentiation to differentiate a function?

If we have y = f(x), we will have slope value on, say, (x0 , f(x(0)) by differentiate it.

m = dy/dx = df(x)/dx.



For slope on x0, just compute it.

We could plot the linear function that have form

y = m x + c

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)