/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

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