Thursday, June 17, 2010

Passing Thought: Applying Computer Science Theory to Economic Theory

     So I have been reading The Collected Works of F. A. Hayek Volume 1:  The Fatal Conceit - The Errors of Socialism.  Haven't quite finished the first chapter, even but it brought some interesting thoughts that I wanted to write down before I forgot them.

-- Hayek makes a reference back to Adam Smith's discussion of basic supply and demand, "the invisible hand" and all... I got to thinking, maybe one of the reasons our economy does so well at what it does (with pricing serving as an indicator that serves to automatically adjusts supply and demand to prevent overproduction and overconsumption) is essentially a form of information hiding and modularity.

I suppose I should explain those terms.  Information hiding is best explained in terms of functions and arguments.  For a programmer, when you're making use of another programmer's function, your usual concern is what the function returns and what arguments are required in what order.  If you should decide to tool around with this other programmer's code without understanding how and why he programmed it the way he did, you will almost certainly end up with a function that is less efficient - and worse, you may break the function altogether.  Thus, there is a need to prevent the first programmer from changing the second programmer's code.  I won't bore you with the variety of methods how you might go about this in an object-oriented context, but I think you get the concept.

Modularity fits along the same lines -- basically it's the concept of breaking up a program from one big "main" function into a bunch of smaller ones; each usually accomplishing a very simple, easy-to-define task.  Under these circumstances it is much easier to debug a malfunctioning program because you can usually narrow down the function that is problematic and fix it accordingly.

Now, how does this all fit in with economics?  Well in early times, and even today, the consumer rarely knows how a particular product arrived at a given store.  He usually doesn't know how it was manufactured, sometimes he doesn't even know all the contents!  My thought is this: what if this not knowing is part of why our market system works so well?  And if that is so, isn't it all the more reason command economies (such as Socialism/Fascism/Communism/Mercantilism/Absolutism/etc) rarely if ever produce positive results (nevermind trying to get results better than what the market system provides)?

Let me know in the comments what you think of the idea. -- I'll go back to reading.

Wednesday, June 16, 2010

Iterated Circles - Mod of Sierpenski Square program

/* CMPT 300X  Chaos  Working with Fractals: Iterated Circle
 *
 * rewrite of Sierpinski Square code to iterate a circular fractal
 Author: Daniel Tanner
 *Note you can mess around with the code here a bit to avoid printing circles inside each other to generate a different fractal pattern (which I kinda like better but... eh).
 */

import javax.swing.JFrame;     //imports for the frame
import java.awt.*;         //imports the colour
import java.awt.Graphics;    //imports the graphics
import java.util.Random;  // imports the random class for random colour

import javax.swing.JPanel;

public class IteratedCircle extends JPanel
{
  public Graphics g;
  public Color c =Color.WHITE; // set colour to white to create the blanks
  public Random r = new Random();
   
  public static void main (String[]args)
  {
     JFrame SS = new JFrame("Drawing with Recursion");
     SS.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     SS.setVisible(true);   //Show the first drawing.
     IteratedCircle S = new IteratedCircle();  //call default constructor (not written);
     SS.setBackground(Color.BLACK);
     SS.add(S);
     SS.setSize(500, 500);  //(width ,height )   */

    //new SFrame();  // call to the frame, program
  }

  public void paint(Graphics g)
  {
    this.g = g;
    // start recursion with first call to draw square
    drawCircle( getWidth() / 2, getHeight() / 2, getWidth() / 4);    
   }
 
  private void drawCircle ( int x, int y ,int radius)
  {
      if (radius > 3) //control when to stop: when size is 2x2 pixels
      {      
      g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
      drawCircle((int)(x + radius * .75), y, radius / 2);  //draws and fill the square, bigger one
      drawCircle((int)(x - radius * .75), y, radius / 2);
      drawCircle(x, (int)(y + radius * .75), radius / 2);
      drawCircle(x, (int)(y - radius * .75), radius / 2);
      //repaint();
      }
  }
 
  private void drawUpCircle( int x, int y ,int radius)
  {
      if (radius > 1) //control when to stop: when size is 2x2 pixels
      {      
      g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
      drawUpCircle(x, (int)(y + radius * .75), radius / 2);
      drawRightCircle((int)(x + radius * .75), y, radius / 2);
      drawLeftCircle((int)(x - radius * .75), y, radius / 2);
      //repaint();
      }
  }
 
  private void drawDownCircle( int x, int y ,int radius)
  {
      if (radius > 1) //control when to stop: when size is 2x2 pixels
      {      
      g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
      drawDownCircle(x, (int)(y - radius * .75), radius / 2);
      drawRightCircle((int)(x + radius * .75), y, radius / 2);
      drawLeftCircle((int)(x - radius * .75), y, radius / 2);
      //repaint();
      }
  }
 
  private void drawLeftCircle( int x, int y ,int radius)
  {
      if (radius > 1) //control when to stop: when size is 2x2 pixels
      {      
          g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
      drawUpCircle(x, (int)(y + radius * .75), radius / 2);
      drawDownCircle(x, (int)(y - radius * .75), radius / 2);
      drawLeftCircle((int)(x - radius * .75), y, radius / 2);
      //repaint();
      }
  }
 
  private void drawRightCircle( int x, int y ,int radius)
  {
      if (radius > 1) //control when to stop: when size is 2x2 pixels
      {      
          g.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      g.drawRoundRect(x - radius / 2, y - radius / 2, radius, radius, radius * radius, radius / 2);
      drawUpCircle(x, (int)(y + radius * .75), radius / 2);
      drawRightCircle((int)(x + radius * .75), y, radius / 2);
      drawDownCircle(x, (int)(y - radius * .75), radius / 2);
      //repaint();
      }
  }
}//end of class

The Dragon Curve

/*
 * Author: Daniel Tanner
 * Dragon Curve Program
 *
 * This program runs a recursive program to keep replacing a given
 * string such as "f" with "f+h" and "h" with "f-h",
 *  and after a certain number of said recursions, translates that
 *  string into a physical display.  The result is known as the
 *  Dragon Curve.
 */

import java.awt.*;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class DragonCurve extends JPanel {
 
 public Graphics g1;
 public Color c =Color.WHITE; // set colour to white to create the blanks
 public Random r = new Random();

 public static double degToRad(int deg) { //degree to radian conversion
  return deg * Math.PI / 180;
 }


 //paint method is automatically called for us by tree recursive (will be called 4 times! Once for each call to drawFractal
 public void paint(final Graphics g) {
  this.g1 = g;
  g1.setColor(c);
  int numRecursions = 14;
  double startAngle = degToRad(0);
  int distance = 3;
   //First call to do the drawing, this call draws the trunk: start(X,Y) at base, angle 0, len calculated)
  drawCurve(getWidth() / 2, getHeight() / 2, numRecursions, startAngle, distance);  //first call
     //System.out.println("in paint, number of calls to drawFractal is " + count +"\n");
   }

 //dragon curve method -- starts at the middle and applies rules to curve
 public void drawCurve(int x0, int y0, int numRecursions, double startAngle, int distance)
 {
    String dragonString = "f";
    for(int i = 0; i < numRecursions; i++)
    {
        String tempString = "";
        for(int j = 0; j < dragonString.length(); j++)
        {    //replace the f with f + h
            if(dragonString.charAt(j) == 'f')
                tempString += "f-h";
            else if(dragonString.charAt(j) == 'h')
                tempString += "f+h";
            else
                tempString += dragonString.charAt(j);
        }
        dragonString = tempString;
    }
    System.out.println(dragonString);
    //now draw the image
    for(int index = 0; index < dragonString.length(); index++)
    {
        if(dragonString.charAt(index) == 'f' || dragonString.charAt(index) == 'h')
        {
            g1.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
            int x1 = (int)(x0 + distance * Math.cos(startAngle));
            int y1 = (int)(y0 + distance * Math.sin(startAngle));
            g1.drawLine(x0, y0, x1, y1);
            x0 = x1; y0 = y1;
        }
        else if(dragonString.charAt(index) == 'g')
        {
            g1.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
            int x1 = (int)(x0 + distance * Math.cos(startAngle));
            int y1 = (int)(y0 + distance * Math.sin(startAngle));
            x0 = x1; y0 = y1;
        }
        else if(dragonString.charAt(index) == '+')
            startAngle = startAngle - Math.PI / 2.0;
        else if(dragonString.charAt(index) == '-')
            startAngle = startAngle + Math.PI / 2.0;
    }
 }

 //main method
 public static void main(String args[]) {
   // System.out.println("count begins with at " + count +"\n");
  JFrame FF = new JFrame("Drawing the Dragon Curve");
  FF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  FF.setVisible(true);   //Show the first drawing.
  DragonCurve F = new DragonCurve();  //call default constructor (not written); F is the Tree.
  FF.setBackground(Color.BLACK);
  FF.add(F);
  FF.setSize(1000, 1000);  //(width 1200,height 1000)   
 }
}

Chaos Program - Julia Orbits

/*Author: Daniel Tanner
 *Julia Orbits Display Program
 *This program displays all the orbit diagrams for a given julia set
 *(which the user can change by clicking on the viewing window)
 *
 *Basically displaying a line following the points when you take a
 *pair of starting values for a complex number 'c' (IE, x + yi), plug
 *it into the function (z^2 + c) where z is the starting point, and then
 *plug the result back into the function until the result is > 2
 */

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JuliaOrbit extends JPanel implements MouseListener, WindowListener {
 
 public Graphics g1;
 public Color c =Color.WHITE; // set colour to white to create the blanks
 public Random r = new Random();
 private double cX = .360248, cY = .100376;

 public static double degToRad(int deg)
 { //degree to radian conversion
  return deg * Math.PI / 180;
 }


 //paint method is automatically called for us by tree recursive (will be called 4 times! Once for each call to drawFractal
 public void paint(Graphics g)
 {
  this.g1 = g;
  g1.setColor(c);
  double initX, initY;
   //First call to do the drawing, this call draws the trunk: start(X,Y) at base, angle 0, len calculated)
  for(initX = -2.0; initX <= 2.0; initX += 0.01)
  {
      for(initY = -2.0; initY <= 2.0; initY += 0.01)
      {
          drawJuliaOrbit(cX, cY, initX, initY);  //first call
          g1.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      }
  }
 }

 //dragon curve method -- starts at the middle and applies rules to curve
 public void drawJuliaOrbit(double cX, double cY, double initX, double initY)
 {
    int m0 = (int)((initX + 1.0) * getWidth() / 2);
    int n0 = (int)((1.0 - initY) * getHeight() / 2);
   
    int iterations = 0;
    double q = Math.sqrt(initX * initX + initY * initY);
    while(q < 2 && iterations < 35)
    {
        iterations++;
        double y1 = 2.0 * (initX * initY);
        double x1 = (initX * initX) + -(initY * initY);
        initX = x1 + cX;
        initY = y1 + cY;
        int m1 = (int)((initX + 2.0) * getWidth() / 4);
        int n1 = (int)((2.0 - initY) * getHeight() / 4);
        g1.drawLine(m0, n0, m1, n1);
        q = Math.sqrt(initX * initX + initY * initY);
        m0 = m1; n0 = n1;
    }
 }

 //main method
 public static void main(String args[]) {
   // System.out.println("count begins with at " + count +"\n");
  JFrame FF = new JFrame("Drawing the Julia Orbit");
  FF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  FF.setVisible(true);   //Show the first drawing.
  JuliaOrbit F = new JuliaOrbit();  //call default constructor (not written); F is the Tree.
  FF.setBackground(Color.BLACK);
  FF.addMouseListener(F);
  FF.add(F);
  FF.setSize(200, 200);  //(width 1200,height 1000)   
 }

 public void mousePressed(MouseEvent e) { }

 public void mouseReleased(MouseEvent e)
 {
     if(e.getButton() == MouseEvent.BUTTON1)
     {
         cX = Double.parseDouble(JOptionPane.showInputDialog("Enter a new cX value:"));
         cY = Double.parseDouble(JOptionPane.showInputDialog("Enter a new cY value:"));
         this.repaint();
     }
 }

 public void mouseEntered(MouseEvent e) { }

 public void mouseExited(MouseEvent e) { }

 public void mouseClicked(MouseEvent e) {
 }


@Override
public void windowActivated(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowClosed(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowDeactivated(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowOpened(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}
}