001/******************************************************************************* 002 * This software is provided as a supplement to the authors' textbooks on digital 003 * image processing published by Springer-Verlag in various languages and editions. 004 * Permission to use and distribute this software is granted under the BSD 2-Clause 005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit http://imagingbook.com for additional details. 008 *******************************************************************************/ 009 010package imagingbook.lib.util; 011 012import imagingbook.lib.settings.PrintPrecision; 013 014import java.util.Formatter; 015import java.util.Locale; 016 017/** 018 * This class provides some static methods for formatting 019 * Java arrays (representing vectors, matrices or measurements) 020 * for copy-pasting to Mathematica. 021 * @author wilbur 022 * @version 2014/12/03 023 */ 024public abstract class MathematicaIO { 025 026 /** 027 * Generates a string holding the named definition 028 * of a 1D double array for Mathematica in the form 029 * name = {A[0], A[1], ...,A[m-1]}; 030 * @param name the identifier to be used in Mathematica. 031 * @param A the array to be encoded (of length m). 032 * @return a String holding the Mathematica definition. 033 */ 034 public static String listArray(String name, double[] A) { 035 StringBuilder sb = new StringBuilder(); 036 Formatter formatter = new Formatter(sb, Locale.US); 037 String fs = PrintPrecision.getFormatStringFloat(); 038 formatter.format(name + " = {"); 039 for (int i = 0; i < A.length; i++) { 040 if (i > 0) 041 formatter.format(", "); 042 formatter.format(fs, A[i]); 043 } 044 formatter.format("};\n"); 045 String result = formatter.toString(); 046 formatter.close(); 047 return result; 048 } 049 050 /** 051 * Generates a string holding the named definition 052 * of a 1D float array for Mathematica in the form 053 * {@code name = {A[0], A[1], ...,A[m-1]};} 054 * 055 * @param name the name (Mathematica symbol) for the resulting array 056 * @param A the array to be encoded (of length m). 057 * @return a String holding the Mathematica definition. 058 */ 059 public static String listArray(String name, float[] A) { 060 StringBuilder sb = new StringBuilder(); 061 Formatter formatter = new Formatter(sb, Locale.US); 062 String fs = PrintPrecision.getFormatStringFloat(); 063 formatter.format(name + " = {"); 064 for (int i = 0; i < A.length; i++) { 065 if (i > 0) 066 formatter.format(", "); 067 formatter.format(fs, A[i]); 068 } 069 formatter.format("};\n"); 070 String result = formatter.toString(); 071 formatter.close(); 072 return result; 073 } 074 075 076 /** 077 * Generates a string holding the named definition 078 * of a 1D int array for Mathematica in the form 079 * {@code name = {A[0], A[1], ...,A[m-1]};} 080 * 081 * @param name the name (Mathematica symbol) for the resulting array 082 * @param A the array to be encoded (of length m). 083 * @return a String holding the Mathematica definition. 084 */ 085 public static String listArray(String name, int[] A) { 086 StringBuilder sb = new StringBuilder(); 087 Formatter formatter = new Formatter(sb, Locale.US); 088 formatter.format(name + " = {"); 089 for (int i = 0; i < A.length; i++) { 090 if (i > 0) 091 formatter.format(", "); 092 formatter.format("%d", A[i]); 093 } 094 formatter.format("};\n"); 095 String result = formatter.toString(); 096 formatter.close(); 097 return result; 098 } 099 100 // CHECK i/j indices!!! 101 /** 102 * Generates a string holding the named definition 103 * of a 2D double array for Mathematica in the form 104 * name = {{A[0][0],...,A[0][m-1]}, 105 * {A[1][0],...,A[1][m-1]}, ..., 106 * {A[n-1][0], A[n-1][1], ...,A[n-1][m-1]}}; 107 * @param name the identifier to be used in Mathematica. 108 * @param A the array to be encoded (of length m). 109 * @return a String holding the Mathematica definition. 110 */ 111 public static String listArray(String name, double[][] A) { 112 StringBuilder sb = new StringBuilder(); 113 Formatter formatter = new Formatter(sb, Locale.US); 114 String fs = PrintPrecision.getFormatStringFloat(); 115 formatter.format(name + " = {"); 116 for (int i = 0; i < A.length; i++) { 117 if (i == 0) 118 formatter.format("{"); 119 else 120 formatter.format(", \n{"); 121 for (int j = 0; j < A[i].length; j++) { 122 if (j == 0) 123 formatter.format(fs, A[i][j]); 124 else 125 formatter.format(", " + fs, A[i][j]); 126 } 127 formatter.format("}"); 128 } 129 formatter.format("};\n"); 130 String result = formatter.toString(); 131 formatter.close(); 132 return result; 133 } 134 135 136 137 /** 138 * Generates a string holding the named definition 139 * of a 2D float array for Mathematica in the form 140 * name = {{A[0][0],...,A[0][m-1]}, 141 * {A[1][0],...,A[1][m-1]}, ..., 142 * {A[n-1][0], A[n-1][1], ...,A[n-1][m-1]}}; 143 * @param name the identifier to be used in Mathematica. 144 * @param A the array to be encoded (of length m). 145 * @return a String holding the Mathematica definition. 146 */ 147 public static String listArray(String name, float[][] A) { 148 StringBuilder sb = new StringBuilder(); 149 Formatter formatter = new Formatter(sb, Locale.US); 150 String fs = PrintPrecision.getFormatStringFloat(); 151 formatter.format(name + " = {"); 152 for (int i = 0; i < A.length; i++) { 153 if (i == 0) 154 formatter.format("{"); 155 else 156 formatter.format(", \n{"); 157 for (int j = 0; j < A[i].length; j++) { 158 if (j == 0) 159 formatter.format(fs, A[j][i]); 160 else 161 formatter.format(", " + fs, A[j][i]); 162 } 163 formatter.format("}"); 164 } 165 formatter.format("};\n"); 166 String result = formatter.toString(); 167 formatter.close(); 168 return result; 169 } 170 171// ------------------------------------------------------ 172 173// public static void main(String[] args) { 174// double[][] A = new double[10][2]; 175// for (int i = 0; i < A.length; i++) { 176// A[i][0] = i * 1.0/3 - 1.5; 177// A[i][1] = Math.random(); 178// } 179// String str = listArray("A", A); 180// System.out.println(str); 181// 182// } 183 184}