001: /*
002: * Created on Nov 29, 2004
003: *
004: */
005: package test;
006:
007: import java.io.*;
008: import java.util.*;
009:
010: import org.jmatlab.linalg.IMatrix;
011: import org.jmatlab.semantic.Symbol;
012:
013: /**
014: * @author Ali
015: *
016: */
017: public class GNUPlot {
018:
019: private static PrintStream out;
020: private static String gnuplot = "c:\\gnuplot\\bin\\pgnuplot.exe";
021:
022: static {
023: Process p = null;
024: try {
025: p = Runtime.getRuntime().exec(gnuplot);
026: } catch (IOException e) {
027: e.printStackTrace();
028: }
029: out = new PrintStream(p.getOutputStream());
030: }
031:
032: public static void PLOT(Vector v) {
033: }
034:
035: private static List figureList = new ArrayList();
036: private static boolean figureCalled = false;
037:
038: public static int FIGURE(Vector v) {
039: int n = 0;
040: int size = v.size();
041: String gnuterm = null;
042: if (size == 0) {
043: n = figureList.size() + 1;
044: } else {
045: n = ((Symbol) v.get(0)).getDouble().intValue();
046: }
047: if (size < 2) {
048: gnuterm = "windows";
049: oneplot();
050: }
051: figureList.add(new Integer(n));
052: out.println("set terminal " + gnuterm + " " + n);
053: if (!figureCalled) {
054: figureCalled = true;
055: }
056: out.flush();
057: return n;
058: }
059:
060: private static boolean multiplotMode = false;
061:
062: private static void oneplot() {
063: out.println("unset multiplot");
064: out.println("set size 1, 1");
065: out.println("set origin 0, 0");
066: multiplotMode = false;
067: }
068:
069: public static void BAR(Vector v) {
070: double[] tmp_xb = null;
071: double[] tmp_yb = null;
072: int size = v.size();
073: double[] x = null;
074: double[] y = null;
075: int len = 0;
076: if (size == 1) {
077: IMatrix m = ((Symbol) v.get(0)).getMatrix();
078: int rows = m.getRows();
079: int cols = m.getCols();
080: if (rows == 1 || cols == 1) {
081: if (rows == 1) {
082: x = m.getRealCol(1);
083: } else {
084: x = m.getRealRow(1);
085: }
086: len = 3 * x.length + 1;
087: tmp_xb = new double[len];
088: tmp_yb = new double[len];
089: tmp_xb[0] = 0.5;
090: tmp_yb[0] = 0;
091: int k = 1;
092: for (int i = 2; i <= len; i = i + 3) {
093: tmp_xb[i] = k - 0.5;
094: tmp_xb[i + 1] = k + 0.5;
095: tmp_xb[i + 2] = k + 0.5;
096: tmp_yb[i] = x[k];
097: tmp_yb[i + 1] = x[k];
098: tmp_yb[i + 2] = 0.0;
099: k++;
100: }
101: } else {
102: return;
103: }
104: } else if (size == 2) {
105: IMatrix m = ((Symbol) v.get(0)).getMatrix();
106: int rows = m.getRows();
107: int cols = m.getCols();
108: int xlen = 0;
109: if (rows == 1 || cols == 1) {
110: int length = 0;
111: if (rows == 1) {
112: x = m.getRealCol(1);
113: } else {
114: x = m.getRealRow(1);
115: }
116: xlen = x.length;
117: }
118:
119: IMatrix n = ((Symbol) v.get(1)).getMatrix();
120: rows = n.getRows();
121: cols = n.getCols();
122: int ylen = 0;
123: if (rows == 1 || cols == 1) {
124: int length = 0;
125: if (rows == 1) {
126: y = n.getRealCol(1);
127: } else {
128: y = n.getRealRow(1);
129: }
130: ylen = y.length;
131: }
132:
133: if (xlen == ylen) {
134: len = 3 * xlen + 1;
135: tmp_xb = new double[len];
136: tmp_yb = new double[len];
137: double[][] cutoff = new double[1][xlen];
138: for (int i = 0; i < xlen - 2; i++) {
139: cutoff[i][0] = (x[i] + x[i + 1]) / 2.0;
140: }
141: double delta_p = cutoff[0][0] - x[0];
142: double delta_m = delta_p;
143: tmp_xb[0] = x[0] - delta_m;
144: tmp_yb[0] = 0.0;
145: int k = 0;
146: for (int i = 1; i < len; i = i + 3) {
147: tmp_xb[i] = tmp_xb[i - 1];
148: tmp_xb[i + 1] = x[k] + delta_p;
149: tmp_xb[i + 2] = tmp_xb[i + 1];
150: tmp_yb[i] = y[k];
151: tmp_yb[i + 1] = y[k];
152: tmp_yb[i + 2] = 0.0;
153: if (k < xlen) {
154: if (x[k + 1] < x[k]) {
155: return;
156: }
157: delta_m = x[k + 1] - cutoff[k][0];
158: k++;
159: if (k < xlen) {
160: delta_p = cutoff[k][0] - x[k];
161: } else {
162: delta_p = delta_m;
163: }
164: }
165: }
166: } else {
167: return;
168: }
169: } else {
170: return;
171: }
172:
173: }
174:
175: private static void axisLabel(String caller, String text) {
176: out.println("set " + caller + " \"" + text + "\"");
177: }
178:
179: private static boolean firstPlot = false;
180: private static boolean holdState = false;
181:
182: private static void plt(String caller, Vector v) {
183: int size = v.size();
184: int nargs = size + 1;
185: IMatrix x = null;
186: IMatrix y = null;
187: String fmt = "";
188: int k = 0;
189: if (size == 1) {
190: x = ((Symbol) v.get(k++)).getMatrix();
191: plt1(x, "");
192: }
193: if (size > 1) {
194: firstPlot = true;
195:
196: nargs = nargs - 2;
197: boolean xset = true;
198: boolean yset = false;
199: while (nargs-- > 0) {
200: Object temp = v.get(k++);
201: if (temp instanceof String) {
202: if (!xset) {
203: return;
204: }
205: fmt = pltopt(caller, temp);
206: if (!yset) {
207: plt1(x, fmt);
208: } else {
209: y = ((Symbol) v.get(k++)).getMatrix();
210: plt2(x, y, fmt);
211: }
212: holdState = true;
213: xset = false;
214: yset = false;
215: } else if (xset) {
216: if (yset) {
217: plt2(x, y, fmt);
218: holdState = true;
219: x = ((Symbol) temp).getMatrix();
220: yset = false;
221: } else {
222: y = ((Symbol) temp).getMatrix();
223: yset = true;
224: }
225: } else {
226: x = ((Symbol) temp).getMatrix();
227: xset = true;
228: }
229: }
230:
231: if (xset) {
232: if (yset) {
233: plt2(x, y, fmt);
234: } else {
235: plt1(x, fmt);
236: }
237: }
238:
239: } else {
240: return;
241: }
242: }
243:
244: private static String pltopt(String caller, Object temp) {
245: return null;
246: }
247:
248: private static double[] domainVector(int n) {
249: double[] rtn = new double[n];
250: for (int i = 0; i < n; i++) {
251: rtn[i] = i + 1;
252: }
253: return rtn;
254: }
255:
256: private static void plt1(IMatrix x, String fmt) {
257: int rows = x.getRows();
258: int cols = x.getCols();
259: double[] x1 = null;
260: double[] x2 = null;
261: if (rows == 1) {
262: if (x.isAnyComplex()) {
263: x1 = x.getRealCol(1);
264: x2 = x.getImagCol(1);
265: } else {
266: x2 = x.getRealCol(1);
267: x1 = domainVector(cols);
268: }
269: } else if (cols == 1) {
270: if (x.isAnyComplex()) {
271: x1 = x.getRealRow(1);
272: x2 = x.getImagRow(1);
273: } else {
274: x2 = x.getRealRow(1);
275: x1 = domainVector(cols);
276: }
277: }
278: plt2(x1, x2, fmt);
279: }
280:
281: private static void plt2(IMatrix x, IMatrix y, String fmt) {
282:
283: }
284:
285: private static String[][] pltopt(String caller, String[][] opt) {
286: int length = opt.length;
287: int width = opt[0].length;
288: String[][] rtn = new String[length][width];
289: for (int i = 0; i < length; i++) {
290: for (int j = 0; j < width; j++) {
291: rtn[i][j] = pltopt1(caller, opt[i][j]);
292: }
293: }
294: return rtn;
295: }
296:
297: private static boolean setColor;
298: private static boolean setSymbol;
299: private static boolean setLines;
300: private static boolean setDots;
301: private static boolean setPoints;
302: private static boolean setImpulses;
303: private static boolean setSteps;
304: private static boolean setBoxes;
305: private static boolean setYerrbars;
306: private static boolean setXerrbars;
307: private static String setLinestyle = "solid";
308: private static boolean moreOpts;
309:
310: private static String pltopt1(String caller, String opt) {
311: String fmt = "";
312: String keyTitle = "";
313: moreOpts = true;
314: String WITH = "w";
315: String LINES = "l";
316: String LINESPOINTS = "linesp";
317: String BOXERRORBARS = "boxer";
318: String BOXES = "boxes";
319: String BOXXY = "boxxy";
320: String POINTS = "p";
321: String DOTS = "d";
322: String IMPULSES = "i";
323: String STEPS = "s";
324: String YERRORBARS = "yerr";
325: String XERRORBARS = "xerr";
326: String XYERRORBARS = "xyerr";
327: String TITLE = "title";
328: String ch = null;
329: String symbol = null;
330: String color = null;
331: while (moreOpts) {
332: ch = opt;
333: moreOpts = false;
334:
335: if ("-".equals(ch)) {
336: if (setLines) {
337: setLinestyle = "dash";
338: } else {
339: setLines = true;
340: }
341: } else if (".".equals(ch)) {
342: if (setLines) {
343: setLinestyle = "dashdot";
344: } else {
345: setLines = true;
346: }
347: } else if (":".equals(ch)) {
348: setLines = true;
349: setLinestyle = "dot";
350: } else if ("@".equals(ch)) {
351: setPoints = true;
352: } else if ("^".equals(ch)) {
353: setImpulses = true;
354: } else if ("L".equals(ch)) {
355: setSteps = true;
356: } else if ("~".equals(ch)) {
357: setYerrbars = true;
358: } else if (">".equals(ch)) {
359: setXerrbars = true;
360: } else if ("#".equals(ch)) {
361: setBoxes = true;
362: } else if ("0".equals(ch) || "1".equals(ch)
363: || "2".equals(ch) || "3".equals(ch)
364: || "4".equals(ch) || "5".equals(ch)
365: || "6".equals(ch) || "7".equals(ch)
366: || "8".equals(ch) || "9".equals(ch)) {
367: if (setColor) {
368: setPoints = true;
369: symbol = ch;
370: setSymbol = true;
371: } else {
372: color = ch;
373: setColor = true;
374: }
375: } else if ("r".equals(ch)) {
376: setColor = true;
377: color = "1";
378: } else if ("g".equals(ch)) {
379: setColor = true;
380: color = "2";
381: } else if ("b".equals(ch)) {
382: setColor = true;
383: color = "3";
384: } else if ("m".equals(ch)) {
385: setColor = true;
386: color = "4";
387: } else if ("c".equals(ch)) {
388: setColor = true;
389: color = "5";
390: } else if ("w".equals(ch) || "k".equals(ch)) {
391: setColor = true;
392: color = "6";
393: } else if ("*".equals(ch)) {
394: setPoints = true;
395: setSymbol = true;
396: symbol = "6";
397: } else if ("+".equals(ch)) {
398: setPoints = true;
399: setSymbol = true;
400: symbol = "2";
401: } else if ("o".equals(ch)) {
402: setPoints = true;
403: setSymbol = true;
404: symbol = "1";
405: } else if ("x".equals(ch)) {
406: setPoints = true;
407: setSymbol = true;
408: symbol = "4";
409: } else if (";".equals(ch)) {
410: boolean working = true;
411: while (working) {
412: ch = opt;
413: moreOpts = false;
414: working = false;
415: }
416: } else if (ch.trim().equals("")) {
417: //do nothing
418: } else {
419: //error
420: } //end if
421: } //end while
422:
423: fmt = "WITH";
424: if (setLines) {
425: if (setPoints) {
426: fmt = fmt + " " + LINESPOINTS;
427: } else {
428: fmt = fmt + " " + LINES;
429: }
430: } else if (setBoxes) {
431: if (setYerrbars && setXerrbars) {
432: fmt = fmt + " " + BOXXY;
433: } else if (setYerrbars) {
434: fmt = fmt + " " + BOXERRORBARS;
435: } else {
436: fmt = fmt + " " + BOXES;
437: }
438: } else if (setPoints) {
439: fmt = fmt + " " + POINTS;
440: } else if (setDots) {
441: fmt = fmt + " " + DOTS;
442: } else if (setImpulses) {
443: fmt = fmt + " " + IMPULSES;
444: } else if (setSteps) {
445: fmt = fmt + " " + STEPS;
446: } else if (setYerrbars) {
447: if (setXerrbars) {
448: fmt = fmt + " " + XYERRORBARS;
449: } else {
450: fmt = fmt + " " + YERRORBARS;
451: }
452: } else if (setXerrbars) {
453: fmt = fmt + " " + XERRORBARS;
454: }
455: if ("WITH".equals(fmt)) {
456: if ("errplot".equals(caller)) {
457: fmt = fmt + " " + YERRORBARS;
458: } else {
459: fmt = fmt + " " + LINES;
460: }
461: }
462: if (setColor) {
463: fmt = fmt + " " + color;
464: if (setSymbol) {
465: fmt = fmt + " " + symbol;
466: }
467: } else if (setSymbol) {
468: fmt = fmt + " 1 " + symbol;
469: }
470:
471: fmt = fmt + " " + TITLE + "\"" + keyTitle + "\"";
472: return fmt;
473: }
474:
475: private static void errplot() {
476:
477: }
478:
479: private static void plt2(double[] x, double[] y, String fmt) {
480:
481: }
482:
483: private static void pltvv(double[] x, double[] y, String fmt) {
484:
485: }
486:
487: public static void main(String[] args) {
488: }
489: }
|