PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/farmR/src/java/src/jfm/utils/MathPrint.java

https://code.google.com/p/javawfm/
Java | 149 lines | 131 code | 15 blank | 3 comment | 19 complexity | 2f2680b403aa38b0fc1672b7cda4e44d MD5 | raw file
  1package jfm.utils;
  2
  3
  4import java.text.DecimalFormat;
  5
  6/** Defines a range of functions to print vectors and arrays. 
  7 * Also defines a random collections of basic mathematical functions */
  8public final class MathPrint {
  9
 10	public static DecimalFormat d1 = new DecimalFormat("#0");
 11	public static DecimalFormat d3 = new DecimalFormat("#000");
 12	public static DecimalFormat f0 = new DecimalFormat("#.0");
 13	public static DecimalFormat f1 = new DecimalFormat("#0.0");
 14	public static DecimalFormat f2 = new DecimalFormat("#00.0");
 15	public static DecimalFormat f3 = new DecimalFormat("#000.0");
 16	public static DecimalFormat df2 = new DecimalFormat("#0.00");
 17	public static DecimalFormat df3 = new DecimalFormat("#0.000");
 18	
 19	public static String prettyPrint(double[] values,String[] names){
 20		StringBuffer buff = new StringBuffer();
 21		for ( int i = 0 ; i < values.length;i++){
 22			buff.append(names[i]+": "+values[i]+"\n");
 23		}
 24		return buff.toString();
 25	}
 26	
 27
 28	
 29	public static String printVector(int[] m){
 30		StringBuffer outstring = new StringBuffer();
 31		outstring.append("   ");
 32		for ( int x=0;x<m.length;x++){
 33			if ( m[x] >= 0 ){
 34					outstring.append("  ");
 35			} else {	
 36				outstring.append(" ");
 37			}
 38			outstring.append(d3.format(m[x]));				
 39		}
 40		outstring.append("\n");
 41		return outstring.toString();
 42	}
 43	public static String printVector(double[] m,int mxdigits,char sep){
 44		StringBuffer outstring = new StringBuffer();
 45		DecimalFormat f;
 46		for ( int x=0;x<m.length;x++){
 47			switch (mxdigits){
 48			case 0:
 49				f=f0;
 50				break;
 51			case 1:
 52				f=f1;
 53				break;
 54			case 2:
 55				f=f2;
 56				break;
 57			case 3:
 58				f=f3;
 59				break;
 60			default:
 61				f=f0;
 62			}
 63			outstring.append(f.format(m[x]));
 64				outstring.append(sep);
 65		}
 66	//	outstring.append("\n");
 67		return outstring.toString();
 68	}
 69	
 70	public static String printMatrix(int[][] m){
 71		StringBuffer outstring = new StringBuffer();
 72		for ( int x=0;x<m.length;x++){
 73			for (int y=0;y<m[0].length;y++){
 74				outstring.append(d3.format(m[x][y]));
 75				outstring.append(" ");
 76			}
 77			outstring.append("\n");
 78		}
 79		return outstring.toString();
 80	}
 81	
 82	public static String printMatrix(double[][] m){
 83		StringBuffer outstring = new StringBuffer();
 84		for ( int x=0;x<m.length;x++){
 85			for (int y=0;y<m[0].length;y++){
 86				outstring.append(d3.format(m[x][y]));
 87				outstring.append(" ");
 88			}
 89			outstring.append("\n");
 90		}
 91		return outstring.toString();
 92	}
 93	
 94	public static String printMatrixDF3(double[][] m){
 95		StringBuffer outstring = new StringBuffer();
 96		for ( int x=0;x<m.length;x++){
 97			for (int y=0;y<m[0].length;y++){
 98				outstring.append(df3.format(m[x][y]));
 99				outstring.append(" ");
100			}
101			outstring.append("\n");
102		}
103		return outstring.toString();
104	}
105	
106
107	public static String printMatrix(double[] m,int nr,int nc){
108		if ( m.length < nr*nc ){ 
109			throw new Error("The vector of size "+m.length+" cannot be printed as a "+nr+"x"+nc+" matrix");
110		}
111		StringBuffer outstring = new StringBuffer();
112		for ( int x=0;x<nr;x++){
113			for (int y=0;y<nc;y++){
114				if ( m[x*nc+y]>=0){
115					outstring.append(" ");
116				}
117				outstring.append(MathPrint.d1.format(m[x*nc+y]));
118				outstring.append(" ");
119			}
120			outstring.append("\n");
121		}
122		return outstring.toString();
123	}
124	
125	public static String printMatrix(String[] cnames,int[][] m){
126		StringBuffer outstring = new StringBuffer();
127		outstring.append("     ");
128		for ( int x=0;x<m.length;x++){
129			outstring.append(cnames[x]);
130			outstring.append(" ");
131		}
132		outstring.append("\n");
133		for ( int x=0;x<m.length;x++){
134			outstring.append(cnames[x]);
135			outstring.append(" ");
136			for (int y=0;y<m[0].length;y++){
137				outstring.append(d3.format(m[x][y]));
138				outstring.append("  ");
139			}
140			outstring.append("\n");
141		}
142		return outstring.toString();
143	}
144	public static boolean isMultiple(int factor,int value){
145		double frac = value/(double)factor;
146		return JFMMath.isZero(frac - Math.floor(frac));
147	}
148	
149}