samer@0
|
1 /*
|
samer@0
|
2 * Vec.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 /**
|
samer@0
|
15 Interface for a vector that can be access via either
|
samer@0
|
16 an iterator (read only), or a double array (read/write)
|
samer@0
|
17 or via a Mat interface.
|
samer@0
|
18 */
|
samer@0
|
19
|
samer@0
|
20 public interface Vec
|
samer@0
|
21 {
|
samer@0
|
22 int size();
|
samer@0
|
23 Iterator iterator();
|
samer@0
|
24 double[] array();
|
samer@0
|
25 Mat mat();
|
samer@0
|
26
|
samer@0
|
27 /** access vector by iterator */
|
samer@0
|
28 public interface Iterator {
|
samer@0
|
29 boolean more();
|
samer@0
|
30 double next();
|
samer@0
|
31 }
|
samer@0
|
32
|
samer@0
|
33 public interface InputIterator {
|
samer@0
|
34 boolean more();
|
samer@0
|
35 void next(double t);
|
samer@0
|
36 }
|
samer@0
|
37
|
samer@0
|
38 /**
|
samer@0
|
39 An implementation of Vec that uses an array to
|
samer@0
|
40 store the values.
|
samer@0
|
41 */
|
samer@0
|
42
|
samer@0
|
43 public static class ForArray implements Vec, Mat, java.io.Serializable
|
samer@0
|
44 {
|
samer@0
|
45 double [] x;
|
samer@0
|
46 public ForArray( double[] a) { x=a; }
|
samer@0
|
47 public ForArray( int n) { x=new double[n]; }
|
samer@0
|
48
|
samer@0
|
49 public int size() { return x.length; }
|
samer@0
|
50 public double[] array() { return x; }
|
samer@0
|
51 public Vec.Iterator iterator() { return new Iterator(); }
|
samer@0
|
52 public Mat mat() { return this; }
|
samer@0
|
53 public int width() { return size(); }
|
samer@0
|
54 public int height() { return 1; }
|
samer@0
|
55 public double get( int i, int j) { return x[j]; }
|
samer@0
|
56 public void set( int i, int j, double t) { x[j]=t; }
|
samer@0
|
57
|
samer@0
|
58 class Iterator implements Vec.Iterator {
|
samer@0
|
59 int i=0;
|
samer@0
|
60
|
samer@0
|
61 public boolean more() { return i<x.length; }
|
samer@0
|
62 public double next() { return x[i++]; }
|
samer@0
|
63 };
|
samer@0
|
64
|
samer@0
|
65 class InputIterator implements Vec.InputIterator {
|
samer@0
|
66 int i=0;
|
samer@0
|
67
|
samer@0
|
68 public boolean more() { return i<x.length; }
|
samer@0
|
69 public void next(double t) { x[i++]=t; }
|
samer@0
|
70 };
|
samer@0
|
71 }
|
samer@0
|
72 }
|
samer@0
|
73
|