import java.io.FileNotFoundException;

/**
 * This class is the main main method, entry point of QuanShi(ft)
 */
public class ShiftMain {
	private String 	_filePrefix;
	private int 	_numBits;
	private int  	_shift;
	private int		_lenOfSEO;
	private double 	_error;

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

		
		
    	ShiftSEO_writer 	writer = new ShiftSEO_writer(
    		filePrefix,
            numBits, 
        	shift);

    	
    	ShiftUinMat   Uin = new ShiftUinMat(numBits, shift);
    	
    	SEO_reader	reader = new SEO_reader(
    	    filePrefix + "_qshi",
    	    numBits, 
    	    false); // useSparse
    	_lenOfSEO = reader.getLenOfSEO();
    	//System.out.println(reader);
    	
    	_error = 0.0;
    	int row, col;
    	for(row=0; row<numRows; row++){
    		for(col=0; col<numRows; col++){
    			_error += Complex.abs(Complex.minus(
    					Uin.getEntry(row,col), 
    					reader.getEntry(row,col)
    					));
    		}
    	}
    	
    	java.io.PrintWriter   logOut = null;
    	try{
	        java.io.File    logFile = 
	            new java.io.File(filePrefix + "_qshi_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("Shift = " + shift);

        logOut.println("");
        logOut.println("Outputs:");
        logOut.println(filePrefix + "_qshi_eng.txt");
        logOut.println(filePrefix + "_qshi_pic.txt");
        logOut.println(filePrefix + "_qshi_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;
    	int shift = 5;//1/Math.sqrt(2);
	  	String 	filePrefix = "test";
    	
    	ShiftMain  m  = new ShiftMain(
    			filePrefix, numBits, shift);
    	
    	
/*    	
    	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);
   	

    }
}
