001: /*
002: * Created on Nov 4, 2004
003: *
004: */
005: package test;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import org.jmatlab.linalg.IMatrix;
012: import org.jmatlab.semantic.SemanticException;
013: import org.jmatlab.semantic.Symbol;
014:
015: /**
016: * @author Ali
017: *
018: */
019: public class Plot1 extends AbstractPLPlot {
020:
021: public Plot1() {
022: super ();
023: }
024:
025: protected void exec() {
026: int size = input.size();
027: if (size != 1) {
028: throw new SemanticException("Invalid number of arguments");
029: }
030: Symbol sym = (Symbol) input.get(0);
031: IMatrix x = sym.getMatrix();
032: int rows = x.getRows();
033: int cols = x.getCols();
034: boolean isAllReal = x.isAllReal();
035: boolean isAnyComplex = x.isAnyComplex();
036: double[] m = null;
037: double[] n = null;
038: if (rows == 1) {
039: if (isAllReal) {
040: m = x.getRealRow(1);
041: plotVector(m);
042: } else if (isAnyComplex) {
043: m = x.getRealRow(1);
044: n = x.getImagRow(1);
045: plotVector(m, n);
046: }
047: } else if (cols == 1) {
048: if (isAllReal) {
049: m = x.getRealCol(1);
050: plotVector(m);
051: } else if (isAnyComplex) {
052: m = x.getRealCol(1);
053: n = x.getImagCol(1);
054: plotVector(m, n);
055: }
056: } else {
057: List colList = new ArrayList();
058: for (int i = 1; i <= cols; i++) {
059: colList.add(x.getRealCol(i));
060: }
061: plotMatrix(colList);
062: }
063:
064: }
065:
066: private void plotVector(double[] m) {
067: int n = m.length;
068: double xmin = 1;
069: double xmax = n;
070: double ymin = min(m);
071: double ymax = max(m);
072: double x[] = domainVector(n);
073: int axis = 0;
074: if (grid) {
075: axis = 2;
076: }
077: pls.col0(1);
078: pls.env(xmin, xmax, ymin, ymax, 0, axis);
079: pls.col0(2);
080: pls.line(x, m);
081: pls.col0(3);
082: }
083:
084: private void plotVector(double[] m, double[] n) {
085: double xmin = min(m);
086: double xmax = max(m);
087: double ymin = min(n);
088: double ymax = max(n);
089: int axis = 0;
090: if (grid) {
091: axis = 2;
092: }
093: pls.env(xmin, xmax, ymin, ymax, 0, axis);
094: pls.col0(2);
095: pls.line(m, n);
096: }
097:
098: private void plotMatrix(List l) {
099: double[] temp = (double[]) l.get(0);
100: int n = temp.length;
101: double xmin = 1;
102: double xmax = n;
103: double x[] = domainVector(n);
104: Iterator iter = l.iterator();
105: double ymin = Double.MAX_VALUE;
106: double ymax = Double.MIN_VALUE;
107: while (iter.hasNext()) {
108: double[] m = (double[]) iter.next();
109: double min = min(m);
110: if (min < ymin) {
111: ymin = min;
112: }
113: double max = max(m);
114: if (max > ymax) {
115: ymax = max;
116: }
117: }
118: int axis = 0;
119: if (grid) {
120: axis = 2;
121: }
122: pls.env(xmin, xmax, ymin, ymax, 0, axis);
123: iter = l.iterator();
124: int color = 1;
125: while (iter.hasNext()) {
126: double[] m = (double[]) iter.next();
127: pls.col0(color++);
128: pls.line(x, m);
129: }
130: }
131:
132: }
|