Slowing Down is the Key…..

slow down

Slowing down is the key to increased speed.

Past couple of months I was dabbing with fortran GUI and trying pgplot graphics library. I have produced gui’s in c, vb and then integrated them with fortran, but creating GUIs from fortran was new to me.

As the exploration began I took the fire aim adjust approach!! Dived deep into the tutorials and anything that I could lay my hands on.

Quickly from tutorials I graduated to actually creating my own little programs. This went on for a couple of months.

In the beginning I was sprinting as hard as possible. Learning, doing, getting stuck, reading and then doing again. The pace was fast.

But as I become comfortable, my approach shifted. I slowed.

I wrote a program and pondered how and what am I actually doing. This slowing down and pondering doubled my learning. It felt like I was learning at greater pace with this slowdown.

So the technique I want to advocate to anyone learning a new programming language, a new analysis tool or cad software, is to sprint in the first few weeks. Race and learn as much as you are able to handle. Dive deep and continue the pace as long as you are able to.

When exhaustion, sense of acheivement begins to creep in, slow down. Become deliberate in what you do? Question why and what you are doing?

I hope applying this method will help you as much as it has helped me.

What are your views, do let it out in the comments.

Pick-up Sticks

pick-up-sticksAbout a year ago, I was brushing my java programming skills. Mr. K, my colleague from the next cubicle, a seasoned java professional was helping me.

At this very time, my mom came to visit us and she brought this game for my daughter.

So getting bored of creating dummy classes for fictitious banks and its clients to learn java oops programming, I decided to make the game my daughter was playing with physical skicks to something that can be played on the browser.

This is the code that I developed. Not complete game but a decent effort that taught me more than I ever learnt from those dry tutorial examples.

And if you would like to play then, visit this page.

Here’s the full code of the game for anyone to play and improve upon.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;


import javax.swing.JApplet;


public class StickFree extends JApplet implements MouseListener {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	Image img;
	Graphics off;
	int w,h,nsticks,stickdetected;
    Stick sticks[];



	public void init(){
		int x1,y1,x2,y2,tmp;
		w=400;
		h=320;
		nsticks=8;
		stickdetected = 0;
		setBackground(Color.black);
        setSize(w,h);
	    img = createImage(w,h);
	    off = img.getGraphics();
        addMouseListener(this);
        sticks = new Stick[nsticks];
		for (int i=0;i<nsticks;i++){  //for (int i=nsticks-1;i>=0;i--){
		  x1 = (int) (Math.random()*w/2-1);
		  y1 = (int) (Math.random()*h-1);
		  x2 = (int) x1+w/2 ;//(Math.random()*w-1);
		  y2 = (int) (Math.random()*h-1);
		  tmp = (int) (Math.random()*5+1);


		  switch(tmp) {
          case 1:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.red, off);break;
          case 2:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.green, off);break;
          case 3:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.white, off);break;
          case 4:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.yellow, off);break;
          default:sticks[i] = new Stick(i,x1,y1,x2,y2, Color.pink, off);break;
          }


         sticks[i].paint();
		}


	}

//	public boolean isontop(int k){
//		boolean ans =true;
//		int i=0;
//
//		for (i=nsticks-1;i>=0;i--){
//			if(i == k) continue;
//			if(sticks[i].isVisible()){
//
//			}
//		}
//
//
//		return ans;
//
//	}

	
	public boolean isontop(Stick sk1){
		int index = sk1.getId();
		
		System.out.println("Index ...from isontop ....."+index);
		
		//boolean ans;
		Stick sk2 = null;
		
		double x ;
		double y ;
		
		for (int i= index; i<nsticks-2;i++){
			sk2 = sticks[i+1];
			
			if(sk2.isVisible()){
				x = (sk2.intercept - sk1.intercept) / (sk1.slope - sk2.slope);
				y = (sk2.slope * x) + sk2.intercept;
				
				
				////
				double a,b,c;

		        a = sk1.slope*x+sk1.intercept-y;
				b = Math.pow(Math.pow(sk1.slope,2.0)+1.0,0.5);

				c = Math.abs(a/b);
//				System.out.println("c = "+c);
				if(c <= 2.0) {
					return false;
				}

//		        System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
				//return ans;
				////
			}
		}


		return true;

	}

	public void paint(Graphics g){
		 g.drawImage(img, 0, 0, this);
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {


	}

	public void mousePressed(MouseEvent e) {


	}

	public void mouseReleased(MouseEvent e) {
        int xx,yy;
		xx=e.getX();
		yy=e.getY();
 	    System.out.println(xx+ " "+ yy+"  n "+stickdetected);

       if(stickdetected >=nsticks) return;

    	   for(int k=0;k<nsticks;k++){
    		   System.out.println(k+"  ....  order ...  ....."+sticks[k].getId());
    		            if(sticks[k].isVisible()){
    		            	
    		            	
    	                 if(sticks[k].isinside(xx,yy)){
    	                		System.out.println("Index ...  ....."+k);
    	                	if(isontop(sticks[k])){
    	                		sticks[k].setVisible(false);
    	                    	   stickdetected++;
    	                    	  break;	
    	                	}
    	                 }
    		          }
    	   }


                       repaint();
                       paintall();
      }

	public void paintall(){
      for(int i=0;i<nsticks;i++){
    	  sticks[i].paint();
      }
	}




}


class Stick {

	private int x0,y0,x1,y1,id,top,down;
	private Color col;
	private Graphics g;
	private boolean visible;
	double slope,intercept;
	Line2D lineShape = null;
	
	public Stick(int id_,int x0_,int y0_,int x1_,int y1_,Color  col_,Graphics g_){
		id = id_;
		x0=x0_;
		y0=y0_;
		x1=x1_;
		y1=y1_;
		col =col_;
		g=g_;
		visible = true;

		slope = ((double)(y1-y0)/(double)(x1-x0));
		intercept = y0-slope*x0;
		top=-1;
		down=-1;
//		System.out.println("id "+id+" x0 "+x0+" x1 "+x1+" y0 "+y0+" y1 "+y1+" slop "+slope+ " in "+ intercept);

		lineShape = new Line2D.Float((float)x0,(float)y0,(float)x1,(float)y1);
	}

	public void paint(){
     if(visible){
    	 
    	
    	 //((Graphics2D)g).
	   g.setColor(col);
	   ((Graphics2D)g).setStroke(new BasicStroke(3));
	   for(int i=0;i<2;i++){
		      g.drawLine(x0+i, y0+i, x1+i, y1+i);
		    }
	    g.drawString(""+this.id, x0, y0);
     }else{
    	 g.setColor(Color.black);

     }



	}

	public void clear(){
		  g.setColor(Color.black);
		    for(int i=0;i<2;i++){
		      g.drawLine(x0+i, y0+i, x1+i, y1+i);
		    }

	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.clear();
		this.visible = visible;
	}

	public boolean isinside(int x,int y){
		boolean ans=false;
		double a,b,c;

        a = slope*x+intercept-y;
		b = Math.pow(Math.pow(slope,2.0)+1.0,0.5);

		c = Math.abs(a/b);
//		System.out.println("c = "+c);
		if(c <= 2.0) ans=true;

//        System.out.println("cccc"+c+" "+b+" "+a+"slope "+slope+"in "+intercept);
		return ans;
		
//		double yyyy =  slope * (x0-x)  + y;
//		
//		System.out.println("x0  "+x0+" y0 "+y0+"  & x1 "+x1+" y1 "+y1+"    ------------ x "+x+"  y "+y+"   ???  "+ yyyy);
//return (y0==yyyy);
	}

	public Line2D getLineShape() {
		return lineShape;
	}

	public int getId() {
		return id;
	}

}



And if you would like to play then, visit this page.

I wish WordPress.com allowed embedding java applets. ;(

Monte Carlo Java Calculating the value of pi

MonteCarlo Java

Monte Carlo. Can’t get enough of it,  so here’s the Java version of the program that calculates value of pi with Monte Carlo.

Have done it in Fortran using pgplot as the GUI engine. You can see it action here.

See the Java applet in action

One thing though, the Java program takes more time to converge on the  value of pi than the Fortran. Maybe my rusty Java program.

 

Neutron Collisions, Stanislaw Ulam , Solitares and my Pgplot practice.

 

Java code for calculating the value of pi using Monte Carlo.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MonteCarlo extends JApplet implements Runnable  {

    private static final long serialVersionUID = 1L;

    int width,height,pcnt=1,pin=1;
    double pival,rad=100.0,radi;
    double eps=0.000001,krad;
    double diff;
    int ranx,rany;
    Thread t = null;
    boolean threadSuspended;
    Image offscreenImage;
    Graphics offscreenGraphics;
    int xbar=2,ybar=2,barwid,barheigt=21;
    double bartop =3.04;
    double barbot = 3.21;
    double barmid = 3.14;
    double barval;
    int line;

   public void init(){
       setSize((int)(2*rad),(int) (2*rad+25));

       krad=Math.pow(rad,2.0);
       width= getSize().width;
       height = getSize().height;
       setBackground(Color.black);
       barwid = width -4;
       offscreenImage = createImage(width,height);
       offscreenGraphics = offscreenImage.getGraphics();

    }

   public void start() {
          if ( t == null ) {
             t = new Thread( this );
             t.setPriority( Thread.MIN_PRIORITY );
             threadSuspended = false;
             t.start();
          }
          else {
             if ( threadSuspended ) {
                threadSuspended = false;
                synchronized( this ) {
                   notify();
                }
             }
          }
       }

   public void stop() {
          threadSuspended = true;
       }
public void paint(Graphics g){

         int x0,y0,tick=1;

         x0=getX()+(int)(width/2.0);
         y0=getY()+(int)((height+25)/2.0);
          ranx=x0+(int) (2*rad*Math.random()-rad);
          rany=y0+ (int) (2*rad*Math.random()-rad);

 

          offscreenGraphics.setColor(Color.green);
          offscreenGraphics.drawLine(ranx, rany, ranx, rany);
          pcnt++;
          radi=Math.pow(ranx-x0, 2.0)+Math.pow(rany-y0, 2.0);

          if(radi < krad){
              offscreenGraphics.setColor(Color.yellow);
              offscreenGraphics.drawLine(ranx, rany, ranx, rany);
              pival=4.0 * (double)pin/pcnt;

 

              pin++;
          }
           diff=pival-3.14159265;

           offscreenGraphics.setColor(Color.gray);

          offscreenGraphics.fillRect(xbar, ybar, barwid, barheigt);
          offscreenGraphics.setColor(Color.white);
          offscreenGraphics.drawString(""+bartop, xbar, ybar+8);
          offscreenGraphics.drawString(""+barbot, barwid-22, ybar+8);
          offscreenGraphics.drawString(""+barmid, (barwid-22)/2, ybar+8);

          barval = ((barwid-xbar-22)/(barbot-bartop))*(pival-bartop)  ;
              line=(int) barval;
          offscreenGraphics.setColor(Color.orange);
          offscreenGraphics.drawLine(line, ybar, line,barheigt);

           g.drawImage(offscreenImage, 0, 0, this);
}

public void run() {
    try {
        while(true){
        repaint();
        t.sleep(0);

        }
    } catch (InterruptedException e) {
        // TODO: handle exception
    }

}

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

    private void pause(int pauseLength) {
        try { Thread.sleep(pauseLength); } catch (InterruptedException e) {}
    }

}

Integrating Fortran with Java in Windows

Fortran from Java

A lot has been written about mixing java with Fortran, but everything scattered and isn’t very current and most of it not for windows environment.

So here’s a little start up beginners guide to integrate java and Fortran in windows.

For Fortran compilation i am using Intel Fortran Compiler and java is java. Hope this PowerPoint will help you get started..
or watch it from here

Your smartphone matches Mars rover

Curiosity may be the most sophisticated spacecraft ever sent to another planet, but the 900-kilogram rover runs on a computer with the same power as a smartphone. The main computer that controls the rover’s post-landing software has a processing speed of just 200 megahertz, less than half the power of the latest iPhones.

Source: Iphone and Mars Rover

And how we are using the power of our smart phones!!

Industry best practice and fortran

I don’t know about you but when I know something is undone and unfinished, I don’t feel so enthusiastic in doing anything else. This is the current state of my mind. But this post isn’t about me.

Last saturday read about lisping at jpl and I felt the same about how fortran is shoved around in the name of best practice. . Here’s the relevant paragraph.

The situation is particularly ironic because the argument that has been advanced for discarding Lisp in favor of C++ (and now for Java) is that JPL should use “industry best practice.” The problem with this argument is twofold: first, we’re confusing best practice with standard practice. The two are not the same. And second, we’re assuming that best (or even standard) practice is an invariant with respect to the task, that the best way to write a word processor is also the best way to write a spacecacraft control system. It isn’t.

This is the same argument that is thrown for fortran. I agree to the second point mentioned above. There isn’t an universal language for all tasks. Specific tasks and application require specific languages. Java has it’s use and so does fortran.

What are your thoughts?