Demo.java

/*
 * $Id: Demo.java,v 1.18 2008/07/16 08:00:37 koga Exp $
 *
 * Copyright (C) 2004 Koga Laboratory. All rights reserved.
 */
package org.mklab.tool.control.util;

import java.io.IOException;

import org.mklab.nfc.matrix.DoubleComplexMatrix;
import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.matrix.DoublePolynomialMatrix;
import org.mklab.nfc.matrix.DoubleRationalPolynomialMatrix;
import org.mklab.nfc.matrix.IntMatrix;
import org.mklab.nfc.matx.MatxList;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.nfc.scalar.DoubleComplexNumber;
import org.mklab.nfc.scalar.DoublePolynomial;
import org.mklab.nfc.scalar.DoubleRationalPolynomial;
import org.mklab.nfc.util.Pause;
import org.mklab.tool.Menu;


/**
 * デモンストレーションを実行するクラス
 * 
 * @author koga
 * @version $Revision: 1.18 $
 */
public class Demo {

  /**
   * デモンストレーションを実行します。
   * 
   * @param args コマンドライン引数
   * @throws SolverStopException ソルバーが停止された場合
   */
  public static void main(String[] args) throws SolverStopException {
    new Demo().run();
  }

  /**
   * デモンストレーションのメニューを表示します。
   * 
   * @throws SolverStopException ソルバーが停止された場合
   */
  public void run() throws SolverStopException {
    String title = Messages.getString("Demo.0"); //$NON-NLS-1$

    String[] options = {Messages.getString("Demo.1"), // 1 //$NON-NLS-1$
        Messages.getString("Demo.2"), // 2 //$NON-NLS-1$
        Messages.getString("Demo.3"), // 3 //$NON-NLS-1$
        Messages.getString("Demo.5")}; //$NON-NLS-1$

    int i = 1;
    for (;;) {
      try {
        i = Menu.menu(title, options, i);
        switch (i) {
          case 1:
            runDataTypeDemonstration();
            break;
          case 2:
            new org.mklab.tool.graph.Demo().run();
            break;
          case 3:
            new Bench().run();
            break;
          default:
            throw new IllegalArgumentException();
        }

        if (i == 0 || i == options.length) {
          break;
        }
      } catch (@SuppressWarnings("unused") final IOException e) {
        //
      }
    }
  }

  /**
   * データ型に関するデモンストレーションのメニューを表示します。
   */
  private void runDataTypeDemonstration() {
    String title = Messages.getString("Demo.6"); //$NON-NLS-1$

    String[] options = {Messages.getString("Demo.7"), // 1 //$NON-NLS-1$
        Messages.getString("Demo.8"), // 2 //$NON-NLS-1$
        Messages.getString("Demo.9"), // 3 //$NON-NLS-1$
        Messages.getString("Demo.10")}; //$NON-NLS-1$

    int i = 1;
    for (;;) {
      try {
        i = Menu.menu(title, options, i);

        switch (i) {
          case 1:
            showAllDataTypes();
            break;
          case 2:
            new org.mklab.tool.matrix.Demo().run();
            break;
          case 3:
            runSymbolicDemonstration();
            break;
          default:
            throw new IllegalArgumentException();
        }
      } catch (@SuppressWarnings("unused") final IOException e) {
        //
      }

      if (i == 0 || i == options.length) {
        break;
      }
    }
  }

  /**
   * 全てのデータ型について説明します。
   * 
   * @throws IOException キーボードから入力できない場合
   */
  private void showAllDataTypes() throws IOException {
    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.12")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.14")); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.16")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tint a = 1;"); //$NON-NLS-1$
    System.out.println("\tSystem.out.println(\"a = \" + a);"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    int a = 1;
    System.out.println("a = " + a); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.23")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tdouble b = 1.4142;"); //$NON-NLS-1$
    System.out.println("\tSystem.out.println(\"b = \" + b);"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    double b = 1.41412;
    System.out.println("b = " + b); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.30")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tComplex i = new Complex(0,1);"); //$NON-NLS-1$
    System.out.println("\ti.print(\"i\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    DoubleComplexNumber i = new DoubleComplexNumber(0, 1);
    i.print("i"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.37")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tString str=\"string\";"); //$NON-NLS-1$
    System.out.println("\tSystem.out.println(\"str = \" + str);"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    String str = "string"; //$NON-NLS-1$
    System.out.println("str = " + str); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.45")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tPolynomial s = new DoublePolynomial(\"s\");"); //$NON-NLS-1$
    System.out.println("\ts.print(\"s\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    DoublePolynomial s = new DoublePolynomial("s"); //$NON-NLS-1$
    s.print("s"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.53")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tPolynomial p = s.power(2).multiply(4.5).add(s.multiply(3)).add(1);"); //$NON-NLS-1$
    System.out.println("\tp.print(\"p\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    DoublePolynomial p = s.power(2).multiply(4.5).add(s.multiply(3)).add(1);
    p.print("p"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.60")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tRationalPolynomial rr = s.divide(p);"); //$NON-NLS-1$
    System.out.println("\trr.print(\"rr\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
    DoubleRationalPolynomial rr = new DoubleRationalPolynomial(s, p);
    rr.print("rr"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.67")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.70")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$

    System.out.println("\tMatrix A = new DoubleMatrix(new double[][]{{a+1, 2,            3},"); //$NON-NLS-1$
    System.out.println("\t                                         {  0, 0, Math.atan(1)},"); //$NON-NLS-1$
    System.out.println("\t                                         {  5, 9,           -1}});"); //$NON-NLS-1$
    System.out.println("\tA.print(\"A\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleMatrix A = new DoubleMatrix(new double[][] { {a + 1, 2, 3}, {0, 0, Math.atan(1)}, {5, 9, -1}});
    A.print("A"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.79")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix AC = new ComplexMatrix(new Complex[][]{{i.multiply(2).add(1), i.multiply(4).add(3)},"); //$NON-NLS-1$
    System.out.println("\t                                              {new Complex(3,4)    ,    new Complex(1,2)}});"); //$NON-NLS-1$
    System.out.println("\tAC.print(\"AC\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleComplexMatrix AC = new DoubleComplexMatrix(new DoubleComplexNumber[][] { {i.multiply(2).add(1), i.multiply(4).add(3)}, {new DoubleComplexNumber(3, 4), new DoubleComplexNumber(1, 2)}});
    AC.print("AC"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.87")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix AP = new DoublePolynomialMatrix(new DoublePolynomial[]{p, s.unaryMinus().add(1)});"); //$NON-NLS-1$
    System.out.println("\tAP.print(\"AP\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoublePolynomialMatrix AP = new DoublePolynomialMatrix(new DoublePolynomial[] {p, s.unaryMinus().add(1)});
    AP.print("AP"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.94")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix AR = AP.divide(s.power(3).subtract(s.power(2).multiply(3)).add(s.multiply(4)).subtract(2));"); //$NON-NLS-1$
    System.out.println("\tAR.print(\"AR\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleRationalPolynomialMatrix AR = new DoubleRationalPolynomialMatrix(AP).divide(new DoubleRationalPolynomial(s.power(3).subtract(s.power(2).multiply(3)).add(s.multiply(4)).subtract(2)));
    AR.print("AR"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.101")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix idx = new IntegerMatrix(new int[]{1,3,5,7});"); //$NON-NLS-1$
    System.out.println("\tidx.print(\"idx\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    IntMatrix idx = new IntMatrix(new int[] {1, 3, 5, 7});
    idx.print("idx"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.108")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix Aa = DoubleMatrix.series(0, 1.0, 0.1);"); //$NON-NLS-1$
    System.out.println("\tAa.getSubVector(idx).print(\"Aa\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleMatrix Aa = DoubleMatrix.series(0, 1.0, 0.1);
    Aa.getSubVector(idx).print("Aa"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.115")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.118")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatxList l = new MatxList(new Object[]{A, p, rr, \"this is a list\"});"); //$NON-NLS-1$
    System.out.println("\tl.print(\"l\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    MatxList l = new MatxList(new Object[] {A, p, rr, "this is a list"}); //$NON-NLS-1$
    l.print("l"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.126")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tl.getMatrix(1).print(\"l(1)\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    l.getMatrix(1).print("l(1)"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.132")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tl.getPolynomial(2).print(\"l(2)\");"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    l.getPolynomial(2).print("l(2)"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
  }

  /**
   * 多項式、有理多項式などの数式処理について説明します。
   * 
   * @throws IOException キーボードから入力できない場合
   */
  private void runSymbolicDemonstration() throws IOException {
    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.139")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.141")); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.142")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tPolynomial s = new DoublePolynomial(\"s\");"); //$NON-NLS-1$
    System.out.println("\tPolynomial p = s.power(2).add(s.multiply(2)).add(3);"); //$NON-NLS-1$
    System.out.println("\tPolynomial q = s.multiply(4).add(5);"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoublePolynomial s = new DoublePolynomial("s"); //$NON-NLS-1$
    DoublePolynomial p = s.power(2).add(s.multiply(2)).add(3);
    DoublePolynomial q = s.multiply(4).add(5);
    p.print("p"); //$NON-NLS-1$
    q.print("q"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.152")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tPolynomial r = p.multiply(q);"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoublePolynomial r = p.multiply(q);
    r.print("p * q"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.158")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tRationalPolynomial rr = p.divide(q);"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleRationalPolynomial rr = new DoubleRationalPolynomial(p, q);
    rr.print("p / q"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println(Messages.getString("Demo.164")); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix ro1 = p.roots();"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleComplexMatrix ro1 = p.getRoots();
    ro1.print("roots(p)"); //$NON-NLS-1$

    System.out.println(""); //$NON-NLS-1$
    System.out.println("\tMatrix ro2 = q.roots();"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();

    DoubleComplexMatrix ro2 = q.getRoots();
    ro2.print("roots(q)"); //$NON-NLS-1$
    System.out.println(""); //$NON-NLS-1$
    Pause.pause();
  }

}