CoreBreaker
Messages postés
540
Date d'inscription
dimanche 23 mars 2003
Statut
Membre
Dernière intervention
5 octobre 2007
1
20 avril 2003 à 05:13
class MatriceError extends Error
{
final static int MATRICE_DIMX_GET= 1;
final static int MATRICE_DIMY_GET= 2;
final static int MATRICE_DIMX_SET= 3;
final static int MATRICE_DIMY_SET= 4;
final static int MATRICE_VIDE= 5;
final static int MATRICE_PAS_INV= 6;
String mMessage= "Type d'exception inconnu";
MatriceError(int aType)
{
this(aType, null);
}
MatriceError(int aType, int[] aParams)
{
switch(aType)
{
case MATRICE_DIMX_GET:
mMessage= "" + aParams[0];
mMessage+= " est un index de colonne invalide";
mMessage+= " pour avoir une valeur";
mMessage+= " (dimension= " + aParams[1] + ")";
break;
case MATRICE_DIMY_GET:
mMessage= "" + aParams[0];
mMessage+= " est un index de ligne invalide";
mMessage+= " pour avoir une valeur";
mMessage+= " (dimension= " + aParams[1] + ")";
break;
case MATRICE_DIMX_SET:
mMessage= "" + aParams[0];
mMessage+= " est un index de colonne invalide";
mMessage+= " pour mettre une valeur";
mMessage+= " (dimension= " + aParams[1] + ")";
break;
case MATRICE_DIMY_SET:
mMessage= "" + aParams[0];
mMessage+= " est un index de ligne invalide";
mMessage+= " pour mettre une valeur";
mMessage+= " (dimension= " + aParams[1] + ")";
break;
case MATRICE_VIDE:
mMessage= "Cette matrice est vide";
break;
case MATRICE_PAS_INV:
mMessage= "Cette matrice n'est pas inversible";
break;
}
}
public String toString()
{
return "Erreur de matrice: " + mMessage;
}
}
public class Matrice
{
// Calcule la signature sur 1 dimension
private static int getSignDim(int aIndex)
{
return ( (aIndex % 2) != 0 )?-1:1;
}
// Calcule la signature sur 2 dimensions
private static int getSign(int aX, int aY)
{
return getSignDim(aX) * getSignDim(aY);
}
private double[] mValues;
private int mDim= 0;
public Matrice(int aDim)
{
mDim= aDim;
if( mDim < 0 )
mDim= 0;
mValues= new double[mDim * mDim];
}
public double getVal(int aX, int aY) throws MatriceError
{
if( aX >= mDim )
throw new MatriceError(MatriceError.MATRICE_DIMX_GET, new int[] {aX, mDim});
if( aY >= mDim )
throw new MatriceError(MatriceError.MATRICE_DIMY_GET, new int[] {aY, mDim});
return mValues[(mDim * aY) + aX];
}
public void setVal(int aX, int aY, double aValue) throws MatriceError
{
if( aX >= mDim )
throw new MatriceError(MatriceError.MATRICE_DIMX_SET, new int[] {aX, mDim});
if( aY >= mDim )
throw new MatriceError(MatriceError.MATRICE_DIMY_SET, new int[] {aY, mDim});
mValues[(mDim * aY) + aX]= aValue;
}
// Extrait la matrice privée de la colonne aX et de la ligne aY
private Matrice extract(int aX, int aY) throws MatriceError
{
Matrice lRes= new Matrice(mDim - 1);
int lI, lJ, lK, lL;
for(lJ= lL= 0; lJ < mDim; lJ++)
{
for(lI= lK= 0; lI < mDim; lI++)
{
if( (lI != aX) && (lJ != aY) )
lRes.setVal(lK, lL, getVal(lI, lJ));
if( lI != aX )
lK++;
}
if( lJ != aY )
lL++;
}
return lRes;
}
public double getDet() throws MatriceError
{
if( mDim <= 0 )
throw new MatriceError(MatriceError.MATRICE_VIDE);
if( mDim == 1 )
return mValues[0];
else
{
int lI;
double lRes= 0.0;
for(lI= 0; lI < mDim; lI++)
lRes+= getSignDim(lI) * getVal(lI, 0) * extract(lI, 0).getDet();
return lRes;
}
}
public Matrice transpose() throws MatriceError
{
Matrice lRes= this;
if( mDim > 0 )
{
if( mDim == 1 )
lRes.setVal(0, 0, getVal(0, 0));
else
{
int lI, lJ;
lRes= new Matrice(mDim);
for(lJ= 0; lJ < mDim; lJ++)
{
lRes.setVal(lJ, lJ, getVal(lJ, lJ));
for(lI= lJ + 1; lI < mDim; lI++)
{
lRes.setVal(lI, lJ, getVal(lJ, lI));
lRes.setVal(lJ, lI, getVal(lI, lJ));
}
}
}
}
return lRes;
}
public Matrice inverse()
{
Matrice lRes= new Matrice(mDim);
if( mDim == 1 )
lRes.setVal(0, 0, 1.0 / getVal(0, 0));
else
{
int lI, lJ;
double lDet= getDet();
if( lDet == 0 )
throw new MatriceError(MatriceError.MATRICE_PAS_INV);
for(lJ= 0; lJ < mDim; lJ++)
for(lI= 0; lI < mDim; lI++)
lRes.setVal(lI, lJ, getSign(lI, lJ) * extract(lI, lJ).getDet() / lDet);
}
return lRes.transpose();
}
public String toString()
{
int lI, lJ;
String lRes= "\n-------------------------------------\nMatrice:\n";
if( mDim > 0 )
{
lRes+= "Dimension= " + mDim + "\n";
for(lI= 0; lI < mDim; lI++)
{
for(lJ= 0; lJ < mDim; lJ++)
lRes+= "" + getVal(lI, lJ) + "\t";
lRes+= "\n";
}
}
else
lRes+= "<Matrice vide>\n";
return lRes + "-------------------------------------\n";
}
public static void main(String[] aArgs)
{
Matrice lMat= new Matrice(2);
lMat.setVal(0, 0, 1.0);
lMat.setVal(0, 1, 2.0);
lMat.setVal(1, 0, 3.0);
lMat.setVal(1, 1, 4.0);
System.out.println("" + lMat);
System.out.println("Det= " + lMat.getDet());
System.out.println("");
lMat= lMat.inverse();
System.out.println("" + lMat);
}
}
Core Breaker :)