import java.io.FileNotFoundException;

/**
 * This class is the main main method, entry point of QuanFou(rier)
 */
public class FouMain {
	private String 	_filePrefix;
	private int 	_numBits;
	private int		_lenOfSEO;
	private double 	_error;

/**
 *
 */
	public FouMain(
		String 	filePrefix,
		int numBits)
	{
		_filePrefix = filePrefix;
		_numBits = numBits;
		int numRows = 1<<numBits;

		
		
    	FouSEO_writer 	writer = new FouSEO_writer(
    		filePrefix, 
            numBits);

    	
    	Complex[][] dftMat = new Complex[numRows][numRows];    	
    	int row, col;
    	double angle;
    	for(row=0; row<numRows; row++){
    		for(col=0; col<numRows; col++){
    			angle = (2*Math.PI*row*col)/numRows;
    			dftMat[row][col] = 
    				new Complex(Math.cos(angle)/Math.sqrt(numRows), 
    						Math.sin(angle)/Math.sqrt(numRows));
    			//System.out.println("row=" + row + " ,col=" + col +
    			//		", z = " + dftMat[row][col].toString());
    		}
    	}
    	
    	SEO_reader	reader = new SEO_reader(
    	    filePrefix + "_qfou",
    	    numBits, 
    	    false); // useSparse
    	_lenOfSEO = reader.getLenOfSEO();
    	//System.out.println(reader.toString());
    	
    	_error = 0.0;
    	for(row=0; row<numRows; row++){
    		for(col=0; col<numRows; col++){
    			_error += Complex.abs(Complex.minus(
    					dftMat[row][col], 
    					reader.getEntry(row,col)
    					));
    		}
    	}
    	
    	java.io.PrintWriter   logOut = null;
    	try{
	        java.io.File    logFile = 
	            new java.io.File(filePrefix + "_qfou_log.txt");
	        logOut = new java.io.PrintWriter(logFile);
    	}
    	catch(FileNotFoundException ex){
            ex.printStackTrace();
        }
    	logOut.println("Inputs:");
        logOut.println("File Prefix = " + filePrefix);
        logOut.println("Number of Bits = " + numBits);
        
        logOut.println("");
        logOut.println("Outputs:");
        logOut.println(filePrefix + "_qfou_eng.txt");
        logOut.println(filePrefix + "_qfou_pic.txt");
        logOut.println(filePrefix + "_qfou_log.txt");
        logOut.println("Number of Elem. Ops. = " + _lenOfSEO);
        logOut.printf("Error = %e\n", _error);
        logOut.close();
   	
	}
/**
 *
 */
	public int getLenOfSEO() {
		return _lenOfSEO;
	}
/**
 *
 */
	public double getError(){
		return _error;
	}
/**
 *
 */
    public static void main(String[] args){
    	
		int numBits = 4;
    	String 	filePrefix = "test";
    	
    	FouMain   m =  new FouMain(filePrefix, numBits);
    	
    	
/*    	
    	System.out.println("expiMat");
    	System.out.println(expiMat.toString());

    	System.out.println("SEO product");
    	System.out.println(reader.toString());
*/
    	System.out.println("error is  " + m._error);
   	

    }
}
