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

no.uib.cipr.matrix.io.MatrixInfo Maven / Gradle / Ivy

/*
 * Copyright (C) 2003-2006 Bjørn-Ove Heimsund
 * 
 * This file is part of MTJ.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package no.uib.cipr.matrix.io;

/**
 * Contains information on a matrix in the Matrix Market exchange format.
 * Supports all valid matrices.
 */
public class MatrixInfo {

    /**
     * What kind of numbers are stored
     */
    public enum MatrixField {

        /**
         * Real numbers
         */
        Real,

        /**
         * Integers
         */
        Integer,

        /**
         * Complex numbers
         */
        Complex,

        /**
         * Pattern matrix. No numbers stored
         */
        Pattern;
    }

    /**
     * Symmetry structure of the matrix, if any
     */
    public enum MatrixSymmetry {

        /**
         * General matrix, no symmetry
         */
        General,

        /**
         * Symmetrical matrix
         */
        Symmetric,

        /**
         * Skew symmetrical matrix
         */
        SkewSymmetric,

        /**
         * Hermitian matrix. Only applicable for complex entris
         */
        Hermitian;
    }

    /**
     * True if the matrix is sparse, else false
     */
    private boolean sparse;

    /**
     * Type of data stored
     */
    private MatrixField field;

    /**
     * Matrix symmetry
     */
    private MatrixSymmetry symmetry;

    /**
     * Creates a specific type
     * 
     * @param sparse
     *            True for sparse matrices, else false
     * @param field
     *            Type of data stored
     * @param symmetry
     *            Matrix symmetry
     */
    public MatrixInfo(boolean sparse, MatrixField field, MatrixSymmetry symmetry) {
        this.sparse = sparse;
        this.field = field;
        this.symmetry = symmetry;

        validate();
    }

    /**
     * Validates the representation
     */
    private void validate() {
        if (isDense() && isPattern())
            throw new IllegalArgumentException(
                    "Matrix cannot be dense with pattern storage");
        if (isReal() && isHermitian())
            throw new IllegalArgumentException(
                    "Data cannot be real with hermitian symmetry");
        if (!isComplex() && isHermitian())
            throw new IllegalArgumentException(
                    "Data must be complex with hermitian symmetry");
        if (isPattern() && isSkewSymmetric())
            throw new IllegalArgumentException(
                    "Storage cannot be pattern and skew symmetrical");
    }

    /**
     * Returns true if the matrix is in coordinate format, else
     * false
     */
    public boolean isSparse() {
        return sparse;
    }

    /**
     * Returns true if the matrix is in coordinate format, else
     * false
     */
    public boolean isCoordinate() {
        return sparse;
    }

    /**
     * Returns true if the matrix is in array format, else
     * false
     */
    public boolean isDense() {
        return !sparse;
    }

    /**
     * Returns true if the matrix is in array format, else
     * false
     */
    public boolean isArray() {
        return !sparse;
    }

    /**
     * Returns true if the matrix stores real numbers, else
     * false
     */
    public boolean isReal() {
        return field == MatrixField.Real;
    }

    /**
     * Returns true if the matrix stores integers, else
     * false
     */
    public boolean isInteger() {
        return field == MatrixField.Integer;
    }

    /**
     * Returns true if the matrix stores complex numbers, else
     * false
     */
    public boolean isComplex() {
        return field == MatrixField.Complex;
    }

    /**
     * Returns true if the matrix does not store any numbers, else
     * false
     */
    public boolean isPattern() {
        return field == MatrixField.Pattern;
    }

    /**
     * Returns true if the matrix form is general, else
     * false
     */
    public boolean isGeneral() {
        return symmetry == MatrixSymmetry.General;
    }

    /**
     * Returns true if the matrix is symmetrical, else
     * false
     */
    public boolean isSymmetric() {
        return symmetry == MatrixSymmetry.Symmetric;
    }

    /**
     * Returns true if the matrix is skew-symmetrical, else
     * false
     */
    public boolean isSkewSymmetric() {
        return symmetry == MatrixSymmetry.SkewSymmetric;
    }

    /**
     * Returns true if the matrix is Hermitian, else
     * false
     */
    public boolean isHermitian() {
        return symmetry == MatrixSymmetry.Hermitian;
    }

    /**
     * Returns a string representation of the specifier. Can be used to provide
     * a header for writing to a file. It is a two-line output, which can look
     * like this:
     * 
     * 
     *       %%MatrixMarket matrix coordinate real general
     * 
*/ @Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append("%%MatrixMarket matrix "); if (isSparse()) buf.append("coordinate "); else buf.append("array "); if (isReal()) buf.append("real "); else if (isComplex()) buf.append("complex "); else if (isPattern()) buf.append("pattern "); else if (isInteger()) buf.append("integer "); else // This should never happen throw new IllegalArgumentException("Unknown field specification"); if (isGeneral()) buf.append("general\n"); else if (isSymmetric()) buf.append("symmetric\n"); else if (isSkewSymmetric()) buf.append("skew-symmetric\n"); else if (isHermitian()) buf.append("Hermitian\n"); else // This should never happen throw new IllegalArgumentException("Unknown symmetry specification"); return buf.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy