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-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009
010package imagingbook.common.util;
011
012import imagingbook.common.math.PrintPrecision;
013
014import java.util.Formatter;
015import java.util.Locale;
016
017/**
018 * This class provides some static methods for formatting Java arrays (representing vectors, matrices or measurements)
019 * for copy-pasting to Mathematica.
020 *
021 * @author WB
022 * @version 2014/12/03
023 */
024public abstract class MathematicaIO {
025        
026        private MathematicaIO() {}
027
028        /**
029         * Generates a string holding the named definition of a 1D double array for Mathematica in the form name = {A[0],
030         * A[1], ...,A[m-1]};
031         *
032         * @param name the identifier to be used in Mathematica.
033         * @param A the array to be encoded (of length m).
034         * @return a String holding the Mathematica definition.
035         */
036        public static String listArray(String name, double[] A) {
037                StringBuilder sb = new StringBuilder();
038                Formatter formatter = new Formatter(sb, Locale.US);
039                String fs = PrintPrecision.getFormatStringFloat();
040                formatter.format(name + " = {");
041                for (int i = 0; i < A.length; i++) {
042                        if (i > 0)
043                                formatter.format(", ");
044                        formatter.format(fs, A[i]);
045                }
046                formatter.format("};\n");
047                String result = formatter.toString();
048                formatter.close();
049                return result;
050        }
051
052        /**
053         * Generates a string holding the named definition of a 1D float array for Mathematica in the form
054         * {@code name = {A[0], A[1], ...,A[m-1]};}
055         *
056         * @param name the name (Mathematica symbol) for the resulting array
057         * @param A the array to be encoded (of length m).
058         * @return a String holding the Mathematica definition.
059         */
060        public static String listArray(String name, float[] A) {
061                StringBuilder sb = new StringBuilder();
062                Formatter formatter = new Formatter(sb, Locale.US);
063                String fs = PrintPrecision.getFormatStringFloat();
064                formatter.format(name + " = {");
065                for (int i = 0; i < A.length; i++) {
066                        if (i > 0)
067                                formatter.format(", ");
068                        formatter.format(fs, A[i]);
069                }
070                formatter.format("};\n");
071                String result = formatter.toString();
072                formatter.close();
073                return result;
074        }
075
076        /**
077         * Generates a string holding the named definition of a 1D int array for Mathematica in the form
078         * {@code name = {A[0], A[1], ...,A[m-1]};}
079         *
080         * @param name the name (Mathematica symbol) for the resulting array
081         * @param A the array to be encoded (of length m).
082         * @return a String holding the Mathematica definition.
083         */
084        public static String listArray(String name, int[] A) {
085                StringBuilder sb = new StringBuilder();
086                Formatter formatter = new Formatter(sb, Locale.US);
087                formatter.format(name + " = {");
088                for (int i = 0; i < A.length; i++) {
089                        if (i > 0)
090                                formatter.format(", ");
091                        formatter.format("%d", A[i]);
092                }
093                formatter.format("};\n");
094                String result = formatter.toString();
095                formatter.close();
096                return result;
097        }
098
099        // TODO: CHECK i/j indices!!!
100
101        /**
102         * Generates a string holding the named definition of a 2D double array for Mathematica in the form name =
103         * {{A[0][0],...,A[0][m-1]}, {A[1][0],...,A[1][m-1]}, ..., {A[n-1][0], A[n-1][1], ...,A[n-1][m-1]}};
104         *
105         * @param name the identifier to be used in Mathematica.
106         * @param A the array to be encoded (of length m).
107         * @return a String holding the Mathematica definition.
108         */
109        public static String listArray(String name, double[][] A) {
110                StringBuilder sb = new StringBuilder();
111                Formatter formatter = new Formatter(sb, Locale.US);
112                String fs = PrintPrecision.getFormatStringFloat();
113                formatter.format(name + " = {");
114                for (int i = 0; i < A.length; i++) {
115                        if (i == 0)
116                                formatter.format("{");
117                        else
118                                formatter.format(", \n{");
119                        for (int j = 0; j < A[i].length; j++) {
120                                if (j == 0) 
121                                        formatter.format(fs, A[i][j]);
122                                else
123                                        formatter.format(", " + fs, A[i][j]);
124                        }
125                        formatter.format("}");
126                }
127                formatter.format("};\n");
128                String result = formatter.toString();
129                formatter.close();
130                return result;
131        }
132
133        /**
134         * Generates a string holding the named definition of a 2D float array for Mathematica in the form name =
135         * {{A[0][0],...,A[0][m-1]}, {A[1][0],...,A[1][m-1]}, ..., {A[n-1][0], A[n-1][1], ...,A[n-1][m-1]}};
136         *
137         * @param name the identifier to be used in Mathematica.
138         * @param A the array to be encoded (of length m).
139         * @return a String holding the Mathematica definition.
140         */
141        public static String listArray(String name, float[][] A) {
142                StringBuilder sb = new StringBuilder();
143                Formatter formatter = new Formatter(sb, Locale.US);
144                String fs = PrintPrecision.getFormatStringFloat();
145                formatter.format(name + " = {");
146                for (int i = 0; i < A.length; i++) {
147                        if (i == 0)
148                                formatter.format("{");
149                        else
150                                formatter.format(", \n{");
151                        for (int j = 0; j < A[i].length; j++) {
152                                if (j == 0) 
153                                        formatter.format(fs, A[j][i]);
154                                else
155                                        formatter.format(", " + fs, A[j][i]);
156                        }
157                        formatter.format("}");
158                }
159                formatter.format("};\n");
160                String result = formatter.toString();
161                formatter.close();
162                return result;
163        }
164
165}