Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* #%L
* Nuiton Matrix :: API
* %%
* Copyright (C) 2004 - 2022 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
* .
* #L%
*/
package org.nuiton.math.matrix;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Stack;
/**
* Contains usefull methods to get information on matrix.
*
* Created: 28 oct. 2004
*
* @author Benjamin Poussin <[email protected]>
* @version $Revision$
*
* Mise a jour: $Date$
* par : $Author$
*/
public class MatrixHelper { // MatrixHelper
public static boolean isValidCoordinates(int[] dim, int[] pos) {
boolean result = dim.length == pos.length;
for (int i = 0; result && i < pos.length; i++) {
result = 0 <= pos[i] && pos[i] < dim[i];
}
return result;
}
public static boolean isValidCoordinates(List[] semantics, Object[] pos) {
boolean result = semantics.length == pos.length;
for (int i = 0; result && i < pos.length; i++) {
List> semantic = semantics[i];
result = semantic.contains(pos[i]);
}
return result;
}
/**
* Retourne le linearFactor, ce tableau sert a convertir une position donnee
* par les dimensions d'une matrice en une position dans un Vecteur.
* @param dimensions
* @return
*/
public static long[] getLinearFactor(int[] dimensions) {
// calcul du linearFactor
long[] linearFactor = new long[dimensions.length];
linearFactor[linearFactor.length - 1] = 1;
for (int i = linearFactor.length - 2; i >= 0; i--) {
linearFactor[i] = linearFactor[i + 1] * dimensions[i + 1];
}
return linearFactor;
}
/**
* Retourne la taille que doit avoir un vecteur pour etre utilise avec
* les dimensions et le linearFactor en argument
* @param dimensions
* @param linearFactor calculer prealablement par getLinearFactor
* @return
*/
public static long getVectorSize(int[] dimensions, long[] linearFactor) {
long result = linearFactor[0] * dimensions[0];
return result;
}
/**
* Retourne la taille que doit avoir un vecteur pour etre utilise avec
* les dimensions en argument
* @param dimensions
* @return
*/
public static long getVectorSize(int[] dimensions) {
long result = getLinearFactor(dimensions)[0] * dimensions[0];
return result;
}
/**
* Convert Matrix to identity matrix must have 2 dimensions. If dimension
* haven't same length, then the small dimension is used.
*
* @param mat matrix nd to convert
* @return converted matrix
*/
public static MatrixND convertToId(MatrixND mat) {
int size = mat.getDim(0);
if (size > mat.getDim(1)) {
size = mat.getDim(1);
}
fill(mat, 0);
for (int i = 0; i < size; i++) {
mat.setValue(i, i, 1);
}
return mat;
}
/**
* Permet de relire une chaine du type [[[1, 2], [3, 4]],[[3, 5], [1, 4]]]
*
* Remarque: une premiere implantantion avait ete faite en utilisant
* {@link StreamTokenizer} mais en fait il y a un bug dedans, il ne sait pas
* parser les chiffres avec un exposant: 5.0E-7 par exemple est lu comme 5.0
* :(
*
* Remarque: une autre implantation de remplacement a ete faite en utilisant
* le {@link org.nuiton.util.StringUtil#split(String, String)} mais elle
* etait moins performante (x2)
*
* @param s la chaine representant les listes de liste
* @return une liste de liste ... de Double
*/
public static List> convertStringToList(String s) {
List