samer@0
|
1 /*
|
samer@0
|
2 * RowColumn.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@0
|
12 package samer.maths;
|
samer@0
|
13
|
samer@0
|
14 import samer.core.*;
|
samer@0
|
15 import samer.core.types.*;
|
samer@0
|
16 import samer.core.util.*;
|
samer@0
|
17 import java.util.*;
|
samer@0
|
18
|
samer@0
|
19 /**
|
samer@0
|
20 Looks like a vector externally, but reads data
|
samer@0
|
21 from a row or column of a matrix. Provides
|
samer@0
|
22 commands for setting which row or column to use,
|
samer@0
|
23 and for switching between rows and columns.
|
samer@0
|
24 */
|
samer@0
|
25
|
samer@0
|
26 public class RowColumn extends VVector implements Mat, Observer
|
samer@0
|
27 {
|
samer@0
|
28 Matrix M;
|
samer@0
|
29 VInteger num;
|
samer@0
|
30 int rowcol;
|
samer@0
|
31 Mat mat;
|
samer@0
|
32
|
samer@0
|
33 public final static Object FLIPPED = new Object();
|
samer@0
|
34 public final static Object SWITCHED = new Object();
|
samer@0
|
35
|
samer@0
|
36 public RowColumn(Matrix A) { this( new Node("rowcolumn",A.getNode()), A); }
|
samer@0
|
37 public RowColumn(Node n, Matrix A)
|
samer@0
|
38 {
|
samer@0
|
39 super(n,A.getColumn(0));
|
samer@0
|
40
|
samer@0
|
41 Shell.push(node);
|
samer@0
|
42 M=A; rowcol=COL;
|
samer@0
|
43 boolean rowwise=Shell.getBoolean("rowwise",(A.getRowDimension()==1));
|
samer@0
|
44 if (rowwise) { vec=A.getRow(0); rowcol=ROW; }
|
samer@0
|
45 Shell.pop();
|
samer@0
|
46
|
samer@0
|
47 if (rowcol==ROW) createVRow();
|
samer@0
|
48 else createVColumn();
|
samer@0
|
49
|
samer@0
|
50 M.addObserver(this);
|
samer@0
|
51 }
|
samer@0
|
52
|
samer@0
|
53 // Vec implementation
|
samer@0
|
54 public double[] array() { return null; }
|
samer@0
|
55 public Vec getVec() { return this; }
|
samer@0
|
56 public Mat mat() { if (mat==null) mat=vec.mat(); return this; }
|
samer@0
|
57
|
samer@0
|
58 // Mat implementation
|
samer@0
|
59 public int width() { return mat.width(); }
|
samer@0
|
60 public int height() { return mat.height(); }
|
samer@0
|
61 public double get( int i, int j) { return mat.get(i,j); }
|
samer@0
|
62 public void set( int i, int j, double t) { mat.set(i,j,t); }
|
samer@0
|
63
|
samer@0
|
64 private void createVRow()
|
samer@0
|
65 {
|
samer@0
|
66 Shell.push(getNode().getParent());
|
samer@0
|
67 num = new VInteger("row",0,Variable.NOINIT);
|
samer@0
|
68 num.setRange(0,M.getRowDimension()-1);
|
samer@0
|
69 num.addObserver(this);
|
samer@0
|
70 Shell.registerViewable(num);
|
samer@0
|
71 Shell.pop();
|
samer@0
|
72 }
|
samer@0
|
73
|
samer@0
|
74 private void createVColumn()
|
samer@0
|
75 {
|
samer@0
|
76 Shell.push(getNode().getParent());
|
samer@0
|
77 num = new VInteger("column",0,Variable.NOINIT);
|
samer@0
|
78 num.setRange(0,M.getColumnDimension()-1);
|
samer@0
|
79 num.addObserver(this);
|
samer@0
|
80 Shell.registerViewable(num);
|
samer@0
|
81 Shell.pop();
|
samer@0
|
82 }
|
samer@0
|
83
|
samer@0
|
84 public void dispose()
|
samer@0
|
85 {
|
samer@0
|
86 Shell.deregisterViewable(num);
|
samer@0
|
87 M.deleteObserver(this);
|
samer@0
|
88 num.dispose();
|
samer@0
|
89 super.dispose();
|
samer@0
|
90 }
|
samer@0
|
91
|
samer@0
|
92 public void setNum(int n) { num.value=n; num.changed(); }
|
samer@0
|
93
|
samer@0
|
94 public void setRow(int r) {
|
samer@0
|
95 num.value=r; vec=M.getRow(r);
|
samer@0
|
96 if (mat!=null) mat=vec.mat();
|
samer@0
|
97 if (rowcol==ROW) changed(SWITCHED);
|
samer@0
|
98 else { rowcol=ROW; changed(FLIPPED); }
|
samer@0
|
99 }
|
samer@0
|
100
|
samer@0
|
101 public void setColumn(int r) {
|
samer@0
|
102 num.value=r; vec=M.getColumn(r);
|
samer@0
|
103 if (mat!=null) mat=vec.mat();
|
samer@0
|
104 if (rowcol==COL) changed(SWITCHED);
|
samer@0
|
105 else { rowcol=COL; changed(FLIPPED); }
|
samer@0
|
106 }
|
samer@0
|
107
|
samer@0
|
108 public void getCommands(Agent.Registry r) {
|
samer@0
|
109 super.getCommands(r); r.group();
|
samer@0
|
110 r.add("row").add("column").add("dispose");
|
samer@0
|
111 }
|
samer@0
|
112
|
samer@0
|
113 public void execute(String cmd, Environment env) throws Exception
|
samer@0
|
114 {
|
samer@0
|
115 if (cmd.equals("row")) {
|
samer@0
|
116
|
samer@0
|
117 if (rowcol==COL) {
|
samer@0
|
118 num.deleteObserver(this);
|
samer@0
|
119 num.dispose();
|
samer@0
|
120 createVRow();
|
samer@0
|
121 setRow(0);
|
samer@0
|
122 }
|
samer@0
|
123
|
samer@0
|
124 // no need to get number: num is automatically exposed
|
samer@0
|
125 // env.datum().get(num);
|
samer@0
|
126
|
samer@0
|
127 } else if (cmd.equals("column")) {
|
samer@0
|
128
|
samer@0
|
129 if (rowcol==ROW) {
|
samer@0
|
130 num.deleteObserver(this);
|
samer@0
|
131 num.dispose();
|
samer@0
|
132 createVColumn();
|
samer@0
|
133 setColumn(0);
|
samer@0
|
134 }
|
samer@0
|
135
|
samer@0
|
136 // no need to get number: num is automatically exposed
|
samer@0
|
137 // env.datum().get(num);
|
samer@0
|
138 } else if (cmd.equals("dispose")) {
|
samer@0
|
139 dispose();
|
samer@0
|
140 } else super.execute(cmd,env);
|
samer@0
|
141 }
|
samer@0
|
142
|
samer@0
|
143 public void notifyObservers(Object s) {
|
samer@0
|
144 super.notifyObservers(s);
|
samer@0
|
145 if (s!=Viewable.DISPOSING && s!=RowColumn.SWITCHED) {
|
samer@0
|
146 M.changed(this); // pass notification onto matrix observers
|
samer@0
|
147 }
|
samer@0
|
148 }
|
samer@0
|
149
|
samer@0
|
150 public void update(Observable o, Object s) { // &&&
|
samer@0
|
151 if (M.equals(o)) { // Matrix changed
|
samer@0
|
152 if (s==Viewable.DISPOSING) dispose();
|
samer@0
|
153 else if (s!=this) super.notifyObservers(this);
|
samer@0
|
154 } else if (num.equals(o)) {
|
samer@0
|
155 if (rowcol==ROW) setRow(num.value);
|
samer@0
|
156 else setColumn(num.value);
|
samer@0
|
157 }
|
samer@0
|
158 }
|
samer@0
|
159
|
samer@0
|
160 private final static int ROW = 1;
|
samer@0
|
161 private final static int COL = 2;
|
samer@0
|
162 }
|