Welcome :Guest

Congratulations!!!

Top 5 Contributors of the Month
Velemoochi
Steve Jackman
Imran Ghani
Santhakumar Munuswamy

 Home >> Articles >> C# >> Post New Resource

# Factory Patterns in Math Computation

Posted By:Shashi Ray       Posted Date: March 02, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com

Factory Patterns in Math Computation

Most people who use Factory patterns tend to think of them as tools for simplifying tangled programming classes. But it is perfectly possible to use them in programs that simply perform mathematical computations. For example, in the Fast Fourier Transform (FFT), you evaluate the

following four equations repeatedly for a large number of point pairs over many passes through the array you are transforming. Because of the way the graphs of these computations are drawn, the following four equations constitute one instance of the FFT "butterfly." These are shown as Equations 1-4.

R1=R1 + R2 cos(y) - I2 sin(y)              (1)

R2=R1 - R2 cos(y) + I2 sin(y)              (2)

I1=I1 + R2 sin(y) +I2 cos(y)                 (3)

I2=I1 - R2 sin(y) - I2 cos(y)                  (4)

However, there are a number of times during each pass through the data where the angle y is zero. In this case, your complex math evaluation reduces to Equations (5-8).

R1=R1 + R2)                (5)

R2=R1 - R2                  (6)

I1=I1 + I2                     (7)

I2=I1 - I2                      (8)

We first define a class to hold complex numbers:

public class Complex {

float real;

float imag;

//---------------------------------

public Complex(float r, float i) {

real = r; imag = i;

}

//---------------------------------

public void setReal(float r) { real = r;}

//---------------------------------

public void setImag(float i) {imag= i;}

//---------------------------------

public float getReal() {return real;}

//---------------------------------

public float getImag() {return imag;}

}

Our basic Buttefly class is an abstract class that can be filled in by one of the implementations of the Execute command:

public abstract class Butterfly {

float y;

public Butterfly() {

}

public Butterfly(float angle) {

y = angle;

}

abstract public void Execute(Complex x, Complex y);

}

We can then make a simple addition Butterfly class which implements the add and subtract methods of equations 5-8:

float oldr1, oldi1;

}

public override void Execute(Complex xi, Complex xj) {

oldr1 = xi.getReal();

oldi1 = xi.getImag();

xi.setReal(oldr1 + xj.getReal());

xj.setReal(oldr1 - xj.getReal());

xi.setImag(oldi1 + xj.getImag());

xj.setImag(oldi1 - xj.getImag());

}

}

The TrigButterfly class is analogous except that the Execute method contains the actual trig functions of Equations 1-4:

public class TrigButterfly:Butterfly {

float y, oldr1, oldi1;

float cosy, siny;

float r2cosy, r2siny, i2cosy, i2siny;

public TrigButterfly(float angle) {

y = angle;

cosy = (float) Math.Cos(y);

siny = (float)Math.Sin(y);

}

public override void Execute(Complex xi, Complex xj) {

oldr1 = xi.getReal();

oldi1 = xi.getImag();

r2cosy = xj.getReal() * cosy;

r2siny = xj.getReal() * siny;

i2cosy = xj.getImag()*cosy;

i2siny = xj.getImag()*siny;

xi.setReal(oldr1 + r2cosy +i2siny);

xi.setImag(oldi1 - r2siny +i2cosy);

xj.setReal(oldr1 - r2cosy - i2siny);

xj.setImag(oldi1 + r2siny - i2cosy);

}

}

Then we can make a simple factory class that decides which class instance to return. Since we are making Butterflies, we'll call our Factory a Cocoon. We never really need to instantiate Cocoon, so we will make its one method static:

public class Cocoon {

static public Butterfly getButterfly(float y) {

if (y != 0)

return new TrigButterfly(y);

else

}

}

Shashi Ray

Responses

No response found. Be the first to respond this post

Post Comment