Plot1.java :  » Science » jmatlab » test » Java Open Source

Java Open Source » Science » jmatlab 
jmatlab » test » Plot1.java
/*
 * Created on Nov 4, 2004
 *
 */
package test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jmatlab.linalg.IMatrix;
import org.jmatlab.semantic.SemanticException;
import org.jmatlab.semantic.Symbol;


/**
 * @author Ali
 *
 */
public class Plot1 extends AbstractPLPlot {

  public Plot1() {
    super();
  }
  
  protected void exec() {
    int size = input.size();
    if (size != 1) {
      throw new SemanticException("Invalid number of arguments");
    }
    Symbol sym = (Symbol) input.get(0);
    IMatrix x = sym.getMatrix();
    int rows = x.getRows();
    int cols = x.getCols();
    boolean isAllReal = x.isAllReal();
    boolean isAnyComplex = x.isAnyComplex();
    double[] m = null;
    double[] n = null;
    if (rows == 1) {
      if (isAllReal) {
        m = x.getRealRow(1);
        plotVector(m);
      } else if (isAnyComplex) {
        m = x.getRealRow(1);
        n = x.getImagRow(1);
        plotVector(m, n);
      }
    } else if (cols == 1) {
      if (isAllReal) {
        m = x.getRealCol(1);
        plotVector(m);
      } else if (isAnyComplex) {
        m = x.getRealCol(1);
        n = x.getImagCol(1);
        plotVector(m, n);
      }      
    } else {
      List colList = new ArrayList();
      for (int i=1; i<=cols; i++) {
        colList.add(x.getRealCol(i));
      }
      plotMatrix(colList);      
    }
    
    
  }
  
  private void plotVector(double[] m) {
    int n = m.length;
    double xmin = 1;
    double xmax = n;
    double ymin = min(m);
    double ymax = max(m);
    double x[] = domainVector(n);
    int axis = 0;
    if (grid) {
      axis = 2;
    }
    pls.col0(1);
    pls.env(xmin, xmax, ymin, ymax, 0, axis);
    pls.col0(2);
    pls.line(x, m);
    pls.col0(3);
  }

  private void plotVector(double[] m, double[] n) {
    double xmin = min(m);
    double xmax = max(m);
    double ymin = min(n);
    double ymax = max(n);
    int axis = 0;
    if (grid) {
      axis = 2;
    }
    pls.env(xmin, xmax, ymin, ymax, 0, axis);
    pls.col0(2);
    pls.line(m, n);
  }

  private void plotMatrix(List l) {
    double[] temp = (double[]) l.get(0);
    int n = temp.length;
    double xmin = 1;
    double xmax = n;
    double x[] = domainVector(n);      
    Iterator iter = l.iterator();
    double ymin = Double.MAX_VALUE;
    double ymax = Double.MIN_VALUE;
    while (iter.hasNext()) {
      double[] m = (double[]) iter.next();
      double min = min(m);
      if (min < ymin) {
        ymin = min; 
      }
      double max = max(m);
      if (max > ymax) {
        ymax = max;
      }
    }    
    int axis = 0;
    if (grid) {
      axis = 2;
    }
    pls.env( xmin, xmax, ymin, ymax, 0, axis );
    iter = l.iterator();
    int color = 1;
    while (iter.hasNext()) {
      double[] m = (double[]) iter.next();
      pls.col0(color++);
      pls.line(x, m);      
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.