samer@0: /* samer@0: * Vec.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: samer@0: /** samer@0: Interface for a vector that can be access via either samer@0: an iterator (read only), or a double array (read/write) samer@0: or via a Mat interface. samer@0: */ samer@0: samer@0: public interface Vec samer@0: { samer@0: int size(); samer@0: Iterator iterator(); samer@0: double[] array(); samer@0: Mat mat(); samer@0: samer@0: /** access vector by iterator */ samer@0: public interface Iterator { samer@0: boolean more(); samer@0: double next(); samer@0: } samer@0: samer@0: public interface InputIterator { samer@0: boolean more(); samer@0: void next(double t); samer@0: } samer@0: samer@0: /** samer@0: An implementation of Vec that uses an array to samer@0: store the values. samer@0: */ samer@0: samer@0: public static class ForArray implements Vec, Mat, java.io.Serializable samer@0: { samer@0: double [] x; samer@0: public ForArray( double[] a) { x=a; } samer@0: public ForArray( int n) { x=new double[n]; } samer@0: samer@0: public int size() { return x.length; } samer@0: public double[] array() { return x; } samer@0: public Vec.Iterator iterator() { return new Iterator(); } samer@0: public Mat mat() { return this; } samer@0: public int width() { return size(); } samer@0: public int height() { return 1; } samer@0: public double get( int i, int j) { return x[j]; } samer@0: public void set( int i, int j, double t) { x[j]=t; } samer@0: samer@0: class Iterator implements Vec.Iterator { samer@0: int i=0; samer@0: samer@0: public boolean more() { return i