package de.kleppmann.maniation.maths;

import java.text.DecimalFormat;

public class VectorImpl implements Vector {

    private double values[];
    
    public VectorImpl(int dimension) {
        dimension = Math.max(dimension, 1);
        values = new double[dimension];
        for (int i=0; i<dimension; i++) values[i] = 0.0;
    }
    
    public VectorImpl(double[] values) {
        this.values = values;
        if ((values == null) || (values.length == 0)) {
            this.values = new double[1];
            this.values[0] = 0.0;
        }
    }

    @Override
    public String toString() {
        DecimalFormat format = new DecimalFormat("######0.000000000000000");
        String result = "";
        for (int i=0; i<getDimension(); i++) {
            if (!result.equals("")) result += ", ";
            result += format.format(getComponent(i));
        }
        return "Vector(" + result + ")";
    }

    public int getDimension() {
        return values.length;
    }
    
    public double getComponent(int index) {
        return values[index];
    }
    
    public Vector mult(double scalar) {
        double[] newv = new double[getDimension()];
        for (int i=0; i<getDimension(); i++) newv[i] = scalar*getComponent(i);
        return new VectorImpl(newv);
    }
    
    public double mult(Vector v) {
        if (v.getDimension() != getDimension()) throw new IllegalArgumentException();
        double result = 0.0;
        for (int i=0; i<getDimension(); i++)
            result += this.getComponent(i) * v.getComponent(i);
        return result;
    }
    
    public Vector multComponents(Vector v) {
        if (v.getDimension() != getDimension()) throw new IllegalArgumentException();
        double[] newv = new double[getDimension()];
        for (int i=0; i<getDimension(); i++)
            newv[i] = this.getComponent(i) * v.getComponent(i);
        return new VectorImpl(newv);
    }

    public Vector add(Vector v) {
        if (v.getDimension() != getDimension()) throw new IllegalArgumentException();
        double[] newv = new double[getDimension()];
        for (int i=0; i<getDimension(); i++)
            newv[i] = this.getComponent(i) + v.getComponent(i);
        return new VectorImpl(newv);
    }
    
    public Vector subtract(Vector v) {
        if (v.getDimension() != getDimension()) throw new IllegalArgumentException();
        double[] newv = new double[getDimension()];
        for (int i=0; i<getDimension(); i++)
            newv[i] = this.getComponent(i) - v.getComponent(i);
        return new VectorImpl(newv);
    }
    
    public void toDoubleArray(double[] array, int offset) {
        for (int i=getDimension()-1; i>=0; i--) array[offset+i] = getComponent(i);
    }
}

@@ -63,16 +63,39 @@
 lFormat;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -571,24 +571,1273 @@
      %7D%0A    %7D
+%0A    %0A    public VectorImpl(String filename) %7B%0A        try %7B%0A            List%3CDouble%3E list = new java.util.ArrayList%3CDouble%3E();%0A            java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(filename));%0A            java.io.StreamTokenizer tok = new java.io.StreamTokenizer(reader);%0A            tok.ordinaryChars('0', '9');%0A            tok.ordinaryChar('.');%0A            tok.ordinaryChar('-');%0A            tok.wordChars(0x0021, 0x00ff);%0A            tok.whitespaceChars(0x0000, 0x0020);%0A            tok.commentChar('#');%0A            tok.eolIsSignificant(true);%0A            while (true) %7B%0A                int token = tok.nextToken();%0A                if (token == java.io.StreamTokenizer.TT_EOF) break;%0A                if ((token == java.io.StreamTokenizer.TT_EOL) && (list.size() %3E 0)) break;%0A                if (token == java.io.StreamTokenizer.TT_NUMBER) list.add(tok.nval);%0A                if (token == java.io.StreamTokenizer.TT_WORD)%0A                    list.add(Double.parseDouble(tok.sval));%0A            %7D%0A            values = new double%5Blist.size()%5D;%0A            for (int i=0; i%3Cvalues.length; i++) values%5Bi%5D = list.get(i);%0A        %7D catch (java.io.IOException e) %7B%0A            this.values = null;%0A        %7D%0A    %7D
 %0A%0A    @Overr

Allow reading of a vector from a text file (for restarting simulations).