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

org.nuiton.math.matrix.MatrixNDImpl Maven / Gradle / Ivy

The newest version!
/* *##% NuitonMatrix
 * Copyright (C) 2004 - 2009 CodeLutin
 *
 * This program 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 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * . ##%*/

package org.nuiton.math.matrix;

import java.util.Arrays;
import java.util.List;

/**
 * MatrixNDImpl.
 *
 * Created: 29 oct. 2004
 *
 * @author Benjamin Poussin 
 * @version $Revision: 187 $
 *
 * Mise a jour: $Date: 2009-10-16 19:17:29 +0200 (ven., 16 oct. 2009) $
 * par : $Author: tchemit $
 */
public class MatrixNDImpl extends AbstractMatrixND { // MatrixNDImpl

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    protected BasicMatrix matrix = null;

    protected MatrixNDImpl(MatrixFactory factory, int[] dim) {
        super(factory, dim);
        matrix = new BasicMatrix(factory, dim);
    }

    protected MatrixNDImpl(MatrixFactory factory, List[] semantics) {
        super(factory, semantics);
        matrix = new BasicMatrix(factory, dim);
    }

    protected MatrixNDImpl(MatrixFactory factory, String name, int[] dim) {
        super(factory, name, dim);
        matrix = new BasicMatrix(factory, dim);
    }

    protected MatrixNDImpl(MatrixFactory factory, String name, int[] dim,
            String[] dimNames) {
        super(factory, name, dim, dimNames);
        matrix = new BasicMatrix(factory, dim);
    }

    protected MatrixNDImpl(MatrixFactory factory, String name,
            List[] semantics) {
        super(factory, name, semantics);
        matrix = new BasicMatrix(factory, dim);
    }

    protected MatrixNDImpl(MatrixFactory factory, String name,
            List[] semantics, String[] dimNames) {
        super(factory, name, semantics, dimNames);
        matrix = new BasicMatrix(factory, dim);
    }

    protected MatrixNDImpl(MatrixFactory factory, MatrixND matrix) {
        super(factory, matrix.getName(), matrix.getSemantics(), matrix
                .getDimensionNames());
        this.matrix = new BasicMatrix(factory, dim);
        this.paste(matrix);
    }

    @Override
    public MatrixIterator iterator() {
        return new MatrixIteratorImpl(matrix.iterator(), getSemantics());
    }

    @Override
    public double getValue(int[] coordinates) {
        return matrix.getValue(coordinates);
    }

    @Override
    public void setValue(int[] coordinates, double d) {
        matrix.setValue(coordinates, d);
    }

    @Override
    public double getMaxOccurence() {
        return matrix.getMaxOccurence();
    }

    @Override
    public String toString() {
        return getName() + " " + matrix.toString();
    }

    // Un peu d'optimisation pour les matrices entieres (pas de sub matrix)
    /**
     * Si le vector utilise par la BasicMatix supporte la method map, on
     * l'utilise pour gagner du temps
     */
    @Override
    public MatrixND map(MapFunction f) {
        if (matrix.data.isImplementedMap()) {
            matrix.data.map(f);
        } else {
            super.map(f);
        }
        return this;
    }

    /**
     * Si on peut on utilise le paste du vector de BasicMatix, sinon on utilise
     * la methode de AbstractMatrixND.
     */
    @Override
    public MatrixND paste(int[] origin, MatrixND mat) {
        // permet de savoir si l'origin est bien le point 0 de la matrice
        boolean origin0 = true;
        for (int i = 0; i < origin.length && origin0; i++) {
            origin0 = origin0 && origin[i] == 0;
        }
        if (origin0
                && mat instanceof MatrixNDImpl
                && Arrays.equals(mat.getDim(), this.getDim())
                && matrix.data
                        .isImplementedPaste(((MatrixNDImpl) mat).matrix.data)) {
            matrix.data.paste(((MatrixNDImpl) mat).matrix.data);
        } else {
            super.paste(origin, mat);
        }
        return this;
    }

    @Override
    public MatrixND add(MatrixND m) {
        if (m instanceof MatrixNDImpl
                && matrix.data.isImplementedAdd(((MatrixNDImpl) m).matrix.data)) {
            matrix.data.add(((MatrixNDImpl) m).matrix.data);
        } else {
            super.add(m);
        }
        return this;
    }

    /**
     * Modifie la matrice actuelle en lui soustrayant les valeurs de la matrice
     * passé en parametre. La matrice passé en parametre doit avoir le meme
     * nombre de dimension, et chacune de ses dimensions doit avoir un nombre
     * d'element au moins egal a cette matrice.
     */
    @Override
    public MatrixND minus(MatrixND m) {
        if (m instanceof MatrixNDImpl
                && matrix.data
                        .isImplementedMinus(((MatrixNDImpl) m).matrix.data)) {
            matrix.data.minus(((MatrixNDImpl) m).matrix.data);
        } else {
            super.minus(m);
        }
        return this;
    }
} // MatrixNDImpl





© 2015 - 2024 Weber Informatics LLC | Privacy Policy