samer@0: /* samer@0: * Matrix.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths; samer@0: import samer.core.*; samer@0: import samer.core.util.*; samer@0: import java.util.*; samer@0: import java.io.*; samer@0: samer@0: /** samer@0: Represents a bundling of a Jama.Matrix with Viewable bits. samer@0: Create one of these and you can either keep it invisible, samer@0: expose its UI yourself, or let the Viewable framework samer@0: expose a default viewer automatically samer@0: */ samer@0: samer@0: public class Matrix extends Jama.Matrix implements Mat, Saveable samer@0: { samer@0: Viewable viewable; samer@0: samer@0: // ............... Viewable bits ................. samer@0: samer@0: private class Vbl extends Viewable { samer@0: Vbl(String nm) { super(nm); } samer@0: Vbl() { super("matrix"); } samer@0: samer@0: /** returns current Agent if there is one, creates a MatrixAgent if not */ samer@0: public Agent getAgent() { samer@0: if (super.getAgent()==null) setAgent(new MatrixAgent(Matrix.this)); samer@0: return super.getAgent(); samer@0: } samer@0: samer@0: public Matrix getMatrix() { return Matrix.this; } samer@0: public Viewer getViewer() samer@0: { samer@0: BaseViewer vwr = new BaseViewer(this); samer@0: StringBuffer buf = new StringBuffer(); samer@0: samer@0: buf.append(getLabel()) samer@0: .append(" (").append(getRowDimension()) samer@0: .append(" by ").append(getColumnDimension()) samer@0: .append(")"); samer@0: vwr.setText(buf.toString()); samer@0: samer@0: return vwr; samer@0: } samer@0: } samer@0: samer@0: public Observable observable() { return viewable; } samer@0: public Viewable viewable() { return viewable; } samer@0: public boolean equals(Observable o) { return viewable==o; } samer@0: public Node getNode() { return viewable.getNode(); } samer@0: samer@0: public final void changed(Object o) { viewable.changed(o); } samer@0: public final void changed() { viewable.changed(); } samer@0: public void addObserver(Observer o) { viewable.addObserver(o); } samer@0: public void deleteObserver(Observer o) { viewable.deleteObserver(o); } samer@0: public void dispose() { viewable.dispose(); } samer@0: samer@0: public void copyFrom(double[][] src) { samer@0: double[][] dest=getArray(); samer@0: for (int i=0; im ? m : B.length; samer@0: int nmax = B[0].length>n ? n : B[0].length; samer@0: samer@0: for (int i = 0; i < mmax; i++) { samer@0: System.arraycopy(B[i],0,A[i],0,nmax); samer@0: } samer@0: } samer@0: samer@0: // Saveable implementation samer@0: samer@0: public void load(InputStream in) throws Exception { samer@0: ObjectInputStream o=new ObjectInputStream(in); samer@0: assign((Jama.Matrix)o.readObject()); samer@0: changed(); samer@0: } samer@0: samer@0: public void save(OutputStream out) throws Exception { samer@0: ObjectOutputStream o=new ObjectOutputStream(out); samer@0: o.writeObject(new Jama.Matrix(getArray())); samer@0: } samer@0: samer@0: public void write(Writer wr) throws Exception { samer@0: print(new PrintWriter(wr,true), new DoubleFormat(16),4); samer@0: } samer@0: samer@0: public void read(Reader rdr) throws Exception { samer@0: Jama.Matrix A=Jama.Matrix.read(new BufferedReader(rdr)); samer@0: assign(A); changed(); samer@0: } samer@0: samer@0: /** Symmetrise in place preserving trace. samer@0: Will fail if matrix is not square. */ samer@0: public void symmetrise() samer@0: { samer@0: double a, _G[][]=getArray(); samer@0: for (int i=0; i