001    package aima.learning.neural;
002    
003    import java.util.List;
004    
005    import aima.util.Matrix;
006    
007    public class Vector extends Matrix {
008    
009            // Vector is modelled as a matrix with a single column;
010            public Vector(int size) {
011                    super(size, 1);
012            }
013    
014            public Vector(List<Double> lst) {
015                    super(lst.size(), 1);
016                    for (int i = 0; i < lst.size(); i++) {
017                            setValue(i, lst.get(i));
018                    }
019            }
020    
021            public double getValue(int i) {
022                    return super.get(i, 0);
023            }
024    
025            public void setValue(int index, double value) {
026                    super.set(index, 0, value);
027            }
028    
029            public Vector copyVector() {
030                    Vector result = new Vector(getRowDimension());
031                    for (int i = 0; i < getRowDimension(); i++) {
032                            result.setValue(i, getValue(i));
033                    }
034                    return result;
035            }
036    
037            public int size() {
038                    return getRowDimension();
039            }
040    
041            public Vector minus(Vector v) {
042                    Vector result = new Vector(size());
043                    for (int i = 0; i < size(); i++) {
044                            result.setValue(i, getValue(i) - v.getValue(i));
045                    }
046                    return result;
047            }
048    
049            public Vector plus(Vector v) {
050                    Vector result = new Vector(size());
051                    for (int i = 0; i < size(); i++) {
052                            result.setValue(i, getValue(i) + v.getValue(i));
053                    }
054                    return result;
055            }
056    
057            public int indexHavingMaxValue() {
058                    if (size() <= 0) {
059                            throw new RuntimeException("can't perform this op on empty vector");
060                    }
061                    int res = 0;
062                    for (int i = 0; i < size(); i++) {
063                            if (getValue(i) > getValue(res)) {
064                                    res = i;
065                            }
066                    }
067                    return res;
068            }
069    
070    }