Demo.java
/*
* $Id: Demo.java,v 1.19 2008/07/18 14:33:14 koga Exp $
*
* Copyright (C) 2004 Koga Laboratory. All rights reserved.
*
*/
package org.mklab.tool.graph;
import java.io.IOException;
import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.util.Pause;
import org.mklab.tool.Menu;
import org.mklab.tool.graph.gnuplot.Canvas;
import org.mklab.tool.graph.gnuplot.Gnuplot;
/**
* {@link Gnuplot}クラスのデモンストレーションを行うクラスです。
*
* @author koga
* @version $Revision: 1.19 $, 2004/06/10
*/
public class Demo {
/**
* メインメソッド
*
* @param args コマンドライン引数
*/
public static void main(String[] args) {
new Demo().run();
}
/**
* デモンストレーションを行います。
*/
public void run() {
String title = Messages.getString("Demo.0"); //$NON-NLS-1$
String[] options = new String[] {Messages.getString("Demo.1"), Messages.getString("Demo.2"), Messages.getString("Demo.3"), Messages.getString("Demo.4"), Messages.getString("Demo.5")}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
int i = 1;
for (;;) {
try {
i = Menu.menu(title, options, i);
switch (i) {
case 1:
graph2D();
break;
case 2:
graph3D();
break;
case 3:
simpleGraph();
break;
case 4:
interestGraph();
break;
default:
throw new IllegalArgumentException();
}
} catch (@SuppressWarnings("unused") final InterruptedException e) {
//
} catch (@SuppressWarnings("unused") final IOException e) {
//
}
if (i == 0 || i == options.length) {
break;
}
}
}
/**
* 三次元プロットのデモンストレーションを行います。
*
* @throws IOException キーボードから入力できない場合
*/
private void graph3D() throws IOException {
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.7")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.9")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.10")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tMatrix z = DoubleMatrix.series(0, 10 * Math.PI, Math.PI / 50);"); //$NON-NLS-1$
System.out.println("\tMatrix x = z.sinElementWise();"); //$NON-NLS-1$
System.out.println("\tMatrix y = z.cosElementWise();"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
DoubleMatrix z = DoubleMatrix.series(0, 10 * Math.PI, Math.PI / 50);
DoubleMatrix x = z.sinElementWise();
DoubleMatrix y = z.cosElementWise();
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.17")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tGnuplot gp = new Gnuplot();"); //$NON-NLS-1$
System.out.println("\tgp.plot(x, y, z, new String[]{\"demo\"});"); //$NON-NLS-1$
Pause.pause();
Gnuplot gp = new Gnuplot();
Canvas canvas = gp.createCanvas();
canvas.plot(x, y, z, new String[] {"demo"}); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.23")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.24")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tz = DoubleMatrix.randomUniform(10);"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
z = DoubleMatrix.uniformRandom(10);
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.29")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tgp.plotSurface(z);"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
canvas.reset();
canvas.plotSurface(z);
Pause.pause();
gp.close();
}
/**
* 二次元プロットのデモンストレーションを行います。
*
* @throws IOException キーボードから入力できない場合
*/
private void graph2D() throws IOException {
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.34")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.36")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.37")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tMatrix x = DoubleMatrix.series(0, 20 * Math.PI, Math.PI / 10);"); //$NON-NLS-1$
System.out.println("\tMatrix y = x.sinElementWise();"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
DoubleMatrix x = DoubleMatrix.series(0, 20 * Math.PI, Math.PI / 10);
DoubleMatrix y = x.sinElementWise();
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.43")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tGnuplot gp = new Gnuplot();"); //$NON-NLS-1$
System.out.println("\tgp.plot(x, y, new String[] { \"sin(x)\" });"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
Gnuplot gp = null, gp2 = null;
gp = new Gnuplot();
Canvas canvas = gp.createCanvas();
canvas.plot(x, y, new String[] {"sin(x)"}); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.50")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tgp.command(\"set xrange [0:20*pi]\");"); //$NON-NLS-1$
System.out.println("\tgp.command(\"set yrange [-1:1]\");"); //$NON-NLS-1$
System.out.println("\tgp.update();"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
canvas.setXRange(0, 20 * Math.PI);
canvas.setYRange(-1, 1);
canvas.redraw();
System.out.println(Messages.getString("Demo.56")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tgp.grid(true);"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
canvas.setGridVisible(true);
canvas.redraw();
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.61")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tx = DoubleMatrix.series(0, 20 * Math.PI, Math.PI / 10);"); //$NON-NLS-1$
System.out.println("\ty = x.sinElementWise();"); //$NON-NLS-1$
System.out.println("\tMatrix y2 = x.multiply(2).sinElementWise().multiply(0.5);"); //$NON-NLS-1$
System.out.println("\tgp.command(\"set xrange [0:20*pi]\");"); //$NON-NLS-1$
System.out.println("\tgp.command(\"set yrange [-1:1]\");"); //$NON-NLS-1$
System.out.println("\tgp.grid(true);"); //$NON-NLS-1$
System.out.println("\tgp.plot(x, y.appendDown(y2), new String[] { \"sin(x)\", \"0.5*sin(2*x)\" });"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
x = DoubleMatrix.series(0, 20 * Math.PI, Math.PI / 10);
y = x.sinElementWise();
DoubleMatrix y2 = x.multiply(2).sinElementWise().multiply(0.5);
canvas.reset();
canvas.doCommand("set xrange [0:20*pi]"); //$NON-NLS-1$
canvas.doCommand("set yrange [-1:1]"); //$NON-NLS-1$
canvas.setGridVisible(true);
canvas.plot(x, y.appendDown(y2), new String[] {"sin(x)", "0.5*sin(2*x)"}); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.76")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tgp.title(\"Sine Plot\");"); //$NON-NLS-1$
System.out.println("\tgp.xlabel(\"x value\");"); //$NON-NLS-1$
System.out.println("\tgp.ylabel(\"y & y2 value\");"); //$NON-NLS-1$
System.out.println("\tgp.update();"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
canvas.setTitle("Sine Plot"); //$NON-NLS-1$
canvas.setXLabel("x value"); //$NON-NLS-1$
canvas.setYLabel("y & y2 value"); //$NON-NLS-1$
canvas.redraw();
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.6")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tMatrix x2 = DoubleMatrix.series(Math.PI / 100, 20 * Math.PI, Math.PI / 10);"); //$NON-NLS-1$
System.out.println("\ty2 = x2.sinElementWise();"); //$NON-NLS-1$
System.out.println("\tgp.command(\"set xrange [pi/100:20*pi]\");"); //$NON-NLS-1$
System.out.println("\tgp.command(\"set yrange [-1:1]\");"); //$NON-NLS-1$
System.out.println("\tgp.grid(true);"); //$NON-NLS-1$
System.out.println("\tgp.semilogx(x2, y2, new String[] { \"sin(x)\" });"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
DoubleMatrix x2 = DoubleMatrix.series(Math.PI / 100, 20 * Math.PI, Math.PI / 10);
y2 = x2.sinElementWise();
canvas.reset();
canvas.doCommand("set xrange [pi/100:20*pi]"); //$NON-NLS-1$
canvas.doCommand("set yrange [-1:1]"); //$NON-NLS-1$
canvas.setGridVisible(true);
canvas.semilogx(x2, y2, new String[] {"sin(x)"}); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.100")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tgp.command(\"set xrange [pi/100:20*pi]\");"); //$NON-NLS-1$
System.out.println("\tgp.command(\"set yrange [-1:1]\");"); //$NON-NLS-1$
System.out.println("\tgp.grid(true);"); //$NON-NLS-1$
System.out.println("\tgp.plot(x, y, new String[] { \"sin(x)\" });"); //$NON-NLS-1$
Pause.pause();
canvas.reset();
canvas.doCommand("set xrange [pi/100:20*pi]"); //$NON-NLS-1$
canvas.doCommand("set yrange [-1:1]"); //$NON-NLS-1$
canvas.setGridVisible(true);
canvas.plot(x, y, new String[] {"sin(x)"}); //$NON-NLS-1$
System.out.println("\tGnuplot gp2 = new Gnuplot();"); //$NON-NLS-1$
System.out.println("\tgp2.command(\"set xrange [pi/100:20*pi]\");"); //$NON-NLS-1$
System.out.println("\tgp2.command(\"set yrange [-1:1]\");"); //$NON-NLS-1$
System.out.println("\tgp2.grid(true);"); //$NON-NLS-1$
System.out.println("\tgp2.semilogx(x2, y2, new String[] { \"sin(x)\" });"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
gp2 = new Gnuplot();
Canvas canvas2 = gp2.createCanvas();
canvas2.doCommand("set xrange [pi/100:20*pi]"); //$NON-NLS-1$
canvas2.doCommand("set yrange [-1:1]"); //$NON-NLS-1$
canvas2.setGridVisible(true);
canvas2.semilogx(x2, y2, new String[] {"sin(x)"}); //$NON-NLS-1$
Pause.pause();
gp.close();
gp2.close();
}
/**
* 簡単なプロットのデモンストレーションを行います。
*
* @throws InterruptedException 強制終了された場合
* @throws IOException キーボードから入力できない場合
*/
private void simpleGraph() throws InterruptedException, IOException {
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.119")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.120")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
DoubleMatrix t = DoubleMatrix.series(0, 10, 0.3);
DoubleMatrix y = t.sinElementWise();
Gnuplot gp = null;
try {
gp = new Gnuplot();
Canvas canvas = gp.createCanvas();
canvas.setGridVisible(false);
canvas.setTitle("A simple X-Y plot"); //$NON-NLS-1$
canvas.plot(t, y, new String[] {""}); //$NON-NLS-1$
Pause.pause(1.0);
canvas.setTitle("Now with a dot, and with grid lines"); //$NON-NLS-1$
canvas.setGridVisible(true);
canvas.setXLabel("I do labels too."); //$NON-NLS-1$
canvas.setYLabel("Hello, World."); //$NON-NLS-1$
canvas.setHolding(true);
canvas.plot(t, y, new String[] {""}, new String[] {"w dots"}); //$NON-NLS-1$ //$NON-NLS-2$
canvas.setHolding(false);
Pause.pause(1.0);
canvas.reset();
canvas.setTitle("Four different line-types"); //$NON-NLS-1$
canvas.plot(t, y.appendDown(y.multiply(2)).appendDown(y.multiply(3)).appendDown(y.multiply(4)), new String[] {"", "", "", ""}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
Pause.pause(1.0);
t = DoubleMatrix.series(0.0, 10, 0.5);
String[] lines = new String[] {"w boxes", "w points", "w lines", "w dots", "w linespoints"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
String[] names = new String[] {"", "", "", "", ""}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
DoubleMatrix data = t.appendDown(t.multiply(2).addElementWise(3)).appendDown(t.multiply(3).addElementWise(6)).appendDown(t.multiply(4).addElementWise(9))
.appendDown(t.multiply(5).addElementWise(12));
canvas.reset();
canvas.setTitle("Five different marker-types"); //$NON-NLS-1$
canvas.plot(t, data, names, lines);
Pause.pause(1.0);
t = DoubleMatrix.series(0.1, 3, 0.1);
canvas.reset();
canvas.setTitle("i do loglog and semilog plots"); //$NON-NLS-1$
canvas.loglog(t.expElementWise(), t.multiplyElementWise(t).expElementWise(), new String[] {""}); //$NON-NLS-1$
Pause.pause(1.0);
t = DoubleMatrix.series(0, 30, 0.3);
canvas.reset();
gp.createCanvas(2, 2);
gp.getCanvas(0, 0).plot(t, t.sinElementWise(), new String[] {"sin(t)"}); //$NON-NLS-1$
gp.getCanvas(0, 1).plot(t, t.multiplyElementWise(t.sinElementWise()), new String[] {"t*sin(t)"}); //$NON-NLS-1$
gp.getCanvas(1, 0).plot(t, t.multiplyElementWise(t.sinElementWise().powerElementWise(2)), new String[] {"t*sin(t)^2"}); //$NON-NLS-1$
gp.getCanvas(1, 1).plot(t, t.powerElementWise(2).multiplyElementWise(t.sinElementWise().powerElementWise(2)), new String[] {"t^2*sin(t)^2"}); //$NON-NLS-1$
Pause.pause(1.0);
Pause.pause();
} finally {
if (gp != null) {
gp.close();
}
}
}
/**
* 楽しいプロットのデモンストレーションを行う
*
* @throws IOException キーボードから入力できない場合
*/
private void interestGraph() throws IOException {
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.152")); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.153")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tMatrix t = DoubleMatrix.series(0,500,0.99*Math.PI/2);"); //$NON-NLS-1$
System.out.println("\tMatrix x = t.multiplyElementWise(t.cosElementWise());"); //$NON-NLS-1$
System.out.println("\tGnuplot gp = new Gnuplot();"); //$NON-NLS-1$
System.out.println("\tgp.plot(x,t,new String[]{\"t * cos(t)\"},new String[]{\"w points\"});"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
DoubleMatrix t = DoubleMatrix.series(0, 500, 0.99 * Math.PI / 2);
DoubleMatrix x = t.multiplyElementWise(t.cosElementWise());
Gnuplot gp = null;
gp = new Gnuplot();
Canvas canvas = gp.createCanvas();
canvas.plot(x, t, new String[] {"t * cos(t)"}, new String[] {"w points"}); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println(Messages.getString("Demo.162")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tgp.plot(x,t,new String[]{\"t * cos(t)\"});"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
canvas.reset();
canvas.plot(x, t, new String[] {"t * cos(t)"}); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println(Messages.getString("Demo.168")); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
System.out.println("\tMatrix y = t.multiplyElementWise(t.sinElementWise());"); //$NON-NLS-1$
System.out.println("\tgp.plot(x,y,new String[]{\"t * sin(t)\"});"); //$NON-NLS-1$
System.out.println(""); //$NON-NLS-1$
Pause.pause();
DoubleMatrix y = t.multiplyElementWise(t.sinElementWise());
canvas.reset();
canvas.plot(x, y, new String[] {"t * sin(t)"}); //$NON-NLS-1$
Pause.pause();
gp.close();
}
}