All Downloads are FREE. Search and download functionalities are using the official Maven repository.

JSci.maths.matrices.DoubleFileSquareMatrix Maven / Gradle / Ivy

Go to download

JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software. It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ... Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).

The newest version!
package JSci.maths.matrices;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Uses an underlying file as storage.
 * @author Mark
 */
public class DoubleFileSquareMatrix extends AbstractDoubleSquareMatrix {
    private static final int DEFAULT_ROW_BUFFERS = 3;
    private static final boolean DEFAULT_DIAG_BUFFER = true;
    private static final int DOUBLE_BYTE_SIZE = 8;
    private final RandomAccessFile file;
    private final int[] rowHistory;
    private final int[] colHistory;
    private final int[] bufferRows;
    private final int[] bufferStartCols;
    private final int[] bufferEndCols;
    private final double[][] buffers;
    private final double[] diagBuffer;

    /**
     * @param numBuffers number of row buffers
     */
    public DoubleFileSquareMatrix(int size, File f, int numBuffers, boolean bufferDiag) throws IOException {
        this(size, f, numBuffers, bufferDiag, null);
    }
    private DoubleFileSquareMatrix(int size, File f, int numBuffers, boolean bufferDiag, double[][] initArray) throws IOException {
        super(size);
        rowHistory = new int[numBuffers];
        colHistory = new int[numBuffers];
        bufferRows = new int[numBuffers];
        bufferStartCols = new int[numBuffers];
        bufferEndCols = new int[numBuffers];
        buffers = new double[numBuffers][];
        for(int i=0; i= oldCol) {
            // cache forward
            startCol = j;
            endCol = numCols;
        } else {
            // cache backward
            startCol = 0;
            endCol = j+1;
        }
        readBuffer(0, i, startCol, endCol);
        bufferRows[0] = i;
        bufferStartCols[0] = startCol;
        bufferEndCols[0] = endCol;
        return buffers[0];
    }
    private double[] extendBuffer(int bufferRow, int i, int j) throws IOException {
        int startCol = bufferStartCols[bufferRow];
        int endCol = bufferEndCols[bufferRow];
        if(j < startCol) {
            readBuffer(bufferRow, i, j, startCol);
            bufferStartCols[bufferRow] = j;
        } else if(j >= endCol) {
            readBuffer(bufferRow, i, endCol, j+1);
            bufferEndCols[bufferRow] = j+1;
        }
        return buffers[bufferRow];
    }

    public double getElement(int i, int j) {
        if(i>=0 && i=0 && j=0 && i=0 && j 0) {
            System.arraycopy(rowHistory, 0, rowHistory, 1, rowHistory.length-1);
            System.arraycopy(colHistory, 0, colHistory, 1, colHistory.length-1);
            rowHistory[0] = i;
            colHistory[0] = j;
        }
    }

    public void close() throws IOException {
        for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy