Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why do I get this exception? How to fix it?
public class NQueen_Pro {
// int[] Fitness;
/*
* int[] NQueens1; int[] NQueens2; int[] NQueens3; int[] NQueens4;
*/
int[][] NQueens;
int[][] CrossOver;
int[] Fitness;
int nFitSelected;
long startingTime ;
/*
* int[] CrossOver1; int[] CrossOver2; int[] CrossOver3; int[] CrossOver4;
*/
Random ran;
int length;
public NQueen_Pro(int n) {
Fitness = new int[n];
/*
* NQueens1 = new int[n]; NQueens2 = new int[n]; NQueens3 = new int[n];
* NQueens4 = new int[n];
*/
NQueens = new int[n][n];
CrossOver = new int[n][n];
nFitSelected = 40;
/*
* CrossOver1 = new int[n]; CrossOver2 = new int[n]; CrossOver3 = new
* int[n]; CrossOver4 = new int[n];
*/
length = n;
startingTime = System.currentTimeMillis();
ran = new Random();
while(true)
genetics();
}
public void fillValues() {
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++)
NQueens[i][j] = ran.nextInt(length);
}
}
private void genetics() {
fillValues();
for (int i = 0; i < length; i++) {
countFitness(i, NQueens[i]);
}
ArrangeFromFitness();
CrossOver();
Mutate();
IsDone();
}
private void IsDone() {
/*
* if (IsFit(CrossOver1)) print(CrossOver1); else if (IsFit(CrossOver2))
* print(CrossOver2); else if (IsFit(CrossOver3)) print(CrossOver3);
* else if (IsFit(CrossOver4)) print(CrossOver4); else{ System.gc();
* generics(); }
*/
int gotFit = 0;
for (int i = 0; i < nFitSelected; i++) {
if (IsFit(CrossOver[i])) {
print(CrossOver[i]);
gotFit = 1;
}
}
if (gotFit == 1) {
System.out.println("Total Time : \"" + ((System.currentTimeMillis()-startingTime)/(float)1000) + "\" Seconds.");
System.exit(1);
}
}
private void print(int[] Array) {
for (int i = 0; i < Array.length; i++) {
for (int j = 0; j < Array.length; j++) {
if (Array[i] == j)
System.out.print(" Q ");
else
System.out.print(" - ");
}
System.out.println();
}
}
private boolean IsFit(int[] crossOver) {
// Vertical Checking
for (int i = 0; i < crossOver.length; i++)
for (int j = i + 1; j < crossOver.length; j++)
if (crossOver[i] == crossOver[j])
return false;
// Diagonal Right Checking
for (int i = 0; i < crossOver.length; i++) {
int temp = 1;
for (int j = i + 1; j < crossOver.length; j++) {
if ((crossOver[i] + temp) == crossOver[j])
return false;
temp++;
}
}
// Diagonal Left Checking
/*
* for (int i = 0; i < crossOver.length; i++) { int temp = 1; for (int j
* = i; j < crossOver.length; j++) { if ((crossOver[i] - temp) ==
* crossOver[j]) return false; temp++; } }
*/
// Diagonal Left Checking
for (int i = 0; i < crossOver.length; i++) {
int temp = 1;
for (int j = i + 1; j < crossOver.length; j++) {
if (crossOver[i] - temp == crossOver[j])
return false;
temp++;
}
}
return true;
}
private void Mutate() {
for (int i = 0; i < nFitSelected; i++) {
int nMutations = ran.nextInt(length);
for (int j = 0; j < nMutations; j++)
CrossOver[i][ran.nextInt(length)] = ran.nextInt(length);
}
/*
* CrossOver1[mutateIndexBit] = ran.nextInt(length); mutateIndexBit =
* ran.nextInt(length); CrossOver2[mutateIndexBit] =
* ran.nextInt(length); mutateIndexBit = ran.nextInt(length);
* CrossOver3[mutateIndexBit] = ran.nextInt(length); mutateIndexBit =
* ran.nextInt(length); CrossOver4[mutateIndexBit] =
* ran.nextInt(length);
*/
}
private void CrossOver() {
int crossOvered = ran.nextInt(length);
for (int i = 0; i < nFitSelected; i += 2) {
for (int j = 0; j < crossOvered; j++) {
CrossOver[i][j] = NQueens[i][j];
CrossOver[i+1][j] = NQueens[i+1][j];
}
for (int j = crossOvered; j < length; j++) {
CrossOver[i+1][j] = NQueens[i][j];
CrossOver[i][j] = NQueens[i+1][j];
}
}
/*
* // FIRST GROUP
* for (int i = 0; i < halfLength; i++)
* CrossOver1[i] = NQueens1[i];
* for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver1[i] = NQueens2[i];
*
* // SECOND GROUP for (int i = 0; i < halfLength; i++) CrossOver2[i] =
* NQueens2[i]; for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver2[i] = NQueens1[i];
*
* // THIRD GROUP for (int i = 0; i < halfLength; i++) CrossOver3[i] =
* NQueens3[i]; for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver3[i] = NQueens4[i];
*
* // FOURTH GROUP for (int i = 0; i < halfLength; i++) CrossOver4[i] =
* NQueens4[i]; for (int i = halfLength; i < NQueens1.length; i++)
* CrossOver4[i] = NQueens3[i];
*/
/*
* for (int i = 0; i < NQueens1.length; i++)
* System.out.print(CrossOver1[i]); System.out.println(); for (int i =
* 0; i < NQueens1.length; i++) System.out.print(CrossOver2[i]);
*/
}
private void ArrangeFromFitness() {
int[] index = new int[length];
for(int i=0;i<length;i++)
index[i] = i;
for (int i = 0; i < Fitness.length; i++) {
for (int j = 0; j < Fitness.length - 1; j++) {
if (Fitness[j] > Fitness[j + 1]) {
int temp = Fitness[j];
Fitness[j] = Fitness[j + 1];
Fitness[j + 1] = temp;
temp = index[j];
index[j] = index[j + 1];
index[j + 1] = temp;
}
}
}
int[][] tempQueens = NQueens;
/*NQueens[0] = tempQueens[index[0]];
NQueens[1] = tempQueens[index[1]];
NQueens[2] = tempQueens[index[0]];
NQueens[3] = tempQueens[index[2]];*/
int temp = 1;
for(int i=0;i<nFitSelected;i+=2){
NQueens[i] = tempQueens[index[0]];
NQueens[i+1] = tempQueens[index[temp]];
temp++;
}
}
/*private void printAll() {
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens1[i]);
System.out.println(" " + Fitness[0]);
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens2[i]);
System.out.println(" " + Fitness[1]);
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens3[i]);
System.out.println(" " + Fitness[2]);
for (int i = 0; i < NQueens1.length; i++)
System.out.print(NQueens4[i]);
System.out.println(" " + Fitness[3]);
}*/
private void countFitness(int nth, int[] Nqueen) {
for (int i = 0; i < Fitness.length; i++)
Fitness[i] = 0;
// Vertical Checking
for (int i = 0; i < Nqueen.length; i++)
for (int j = i + 1; j < Nqueen.length; j++)
if (Nqueen[i] == Nqueen[j])
Fitness[nth]++;
// Diagonal Right Checking
for (int i = 0; i < Nqueen.length; i++) {
int temp = 1;
for (int j = i + 1; j < Nqueen.length; j++) {
if ((Nqueen[i] + temp) == Nqueen[j])
Fitness[nth]++;
temp++;
}
}
// Diagonal Left Checking
for (int i = 0; i < Nqueen.length; i++) {
int temp = 1;
for (int j = i + 1; j < Nqueen.length; j++) {
if (Nqueen[i] - temp == Nqueen[j])
Fitness[nth]++;
temp++;
}
}
}
public static void main(String[] arg) {
new NQueen_Pro(8);
}
}

nFitSelected is 40. The size of NQueens is 8 by 8. You got your condition wrong in the for loop :
for(int i=0;i<nFitSelected;i+=2){
NQueens[i] = tempQueens[index[0]];
NQueens[i+1] = tempQueens[index[temp]];
temp++;
}
If you let i reach as high as 38, NQueens[i] would throw an exception when i reaches 8.
The definitions of the arrays :
public NQueen_Pro(int n) {
Fitness = new int[n];
NQueens = new int[n][n]; // 8 x 8
CrossOver = new int[n][n];
nFitSelected = 40;
...
public static void main(String[] arg)
{
new NQueen_Pro(8); // the value of n
}

Meta-notes: Your code needs better formatting - indentation and removal of commented-out-code.
anyway I like using command line to debug:
Follow what it saws, you need to import that Random class. Where is RAndom? A google search yields It is in Util. So add in:
import java.util.*;

Related

Why am I getting 100% accuracy in my Java MLP program?

I am writing a code in java that uses an MLP algorithm on a digit recognition dataset. I have 2 excel files. One for training and one for testing and they both contain 65 columns and 2810 rows. Below is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Random;
public class Main {
static int hiddenNeurons = 64; // Number of hidden neurons
static int[][] WH = new int[hiddenNeurons][64]; // Weight between the input and the hidden neurons
static int[][] WO = new int[10][hiddenNeurons]; // Weight between the hidden neurons and the output
static double[] outputHidden = new double[hiddenNeurons];
static double[] outputNeuron = new double[10];
static int[] dataSample = new int[64];
static int[] mapTarget = new int[10];
static int[] errorOutput = new int[10];
static int[] errorHidden = new int[hiddenNeurons];
static void initializeWeights(){
Random randomNumber = new Random();
for (int i = 0; i < WH.length; i++) {
for (int j = 0; j < WH[i].length; j++) {
WH[i][j] = randomNumber.nextInt(10);
}
}
for (int i = 0; i < WO.length; i++) {
for (int j = 0; j < WO[i].length; j++) {
WO[i][j] = randomNumber.nextInt(10);
}
}
}
public static void feedForward(){
double sigmoid = 0;
double sumWH = 0;
for (int i = 0; i < hiddenNeurons; i++) {
for (int j = 0; j < dataSample.length; j++) {
sumWH += dataSample[j] * WH[i][j];
}
sigmoid = 1 / (1 + Math.exp(-sumWH));
outputHidden[i] = sigmoid;
}
for (int i = 0; i < outputNeuron.length; i++) {
double sumWO = 0;
for (int j = 0; j < dataSample.length; j++) {
sumWO += outputHidden[j] * WO[i][j];
}
outputNeuron[i] = sumWO >= 0 ? 1 : 0;
}
}
static boolean testError() {
for (int i = 0; i < outputNeuron.length; i++) {
if (mapTarget[i] != outputNeuron[i]) {
return false;
}
}
return true;
}
static void training(){
double learningRate = 0.1;
for (int i = 0; i < outputNeuron.length; i++) {
errorOutput[i] = (int) (mapTarget[i] - outputNeuron[i]);
}
for (int j = 0; j < hiddenNeurons; j++) {
double errorTemp = 0;
for (int i = 0; i < outputNeuron.length; i++) {
errorTemp += errorOutput[i] * WO[i][j]; //try [j][i]
}
errorHidden[j] = (int) (outputHidden[j] * (1-outputHidden[j]) * errorTemp);
}
for (int i = 0; i < outputNeuron.length; i++) {
for (int j = 0; j < hiddenNeurons; j++) {
WO[i][j] = (int) (WO[i][j] + learningRate * outputHidden[j] * errorOutput[i]);
}
}
for (int i = 0; i < hiddenNeurons; i++) {
for (int j = 0; j < dataSample.length; j++) {
WH[i][j] = (int) (WH[i][j] + learningRate * dataSample[j] * errorHidden[i]);
}
}
}
static void testing(){
double success = 0;
try(BufferedReader br = new BufferedReader(new FileReader("test.csv"))) {
String line;
int numOfRows = 0;
boolean errorTested = false;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < 64; i++) {
dataSample[i] = Integer.parseInt(values[i]);
}
int targetOutput = Integer.parseInt(values[values.length - 1]);
for (int i = 0; i < 10; i++) {
mapTarget[i] = 0;
}
numOfRows++;
mapTarget[targetOutput] = 1;
feedForward();
errorTested = testError();
if(!errorTested){
success++;
}
}
double accuracy = success / numOfRows;
System.out.println("testing dataset accuracy: " + accuracy * 100);
}
catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
double success = 0;
initializeWeights();
try (BufferedReader br = new BufferedReader(new FileReader("train.csv"))) {
String line;
int numOfRows = 0;
double accuracy = 0;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int i = 0; i < 64; i++) {
dataSample[i] = Integer.parseInt(values[i]);
}
int targetOutput = Integer.parseInt(values[values.length - 1]);
for (int i = 0; i < 10; i++) {
mapTarget[i] = 0;
}
mapTarget[targetOutput] = 1;
feedForward();
if (testError()) {
training();
} else {
success++;
}
numOfRows++;
accuracy = success / numOfRows;
}
System.out.println("training dataset: " + accuracy*100);
testing();
}
catch (Exception e){
e.printStackTrace();
}
}
}
This code, when run, gives me this output:
training dataset: 100.0
testing dataset accuracy: 100.0
What is the issue that seems to show me 100% accuracy even though that isn't possible.

Inverse 2D FFT is outputting correct values in wrong order

My 2D FFT algorithm is outputting the correct values, but they are in the wrong order. For example, for input:
1050.0 1147.0 1061.0 1143.0
1046.0 1148.0 1118.0 1073.0
1072.0 1111.0 1154.0 1101.0
1078.0 1101.0 1106.0 1062.0
Taking the FFT, and then inverse FFT results in:
1050.0 1143.0 1061.0 1147.0
1078.0 1062.0 1106.0 1101.0
1072.0 1101.0 1154.0 1111.0
1046.0 1073.0 1118.0 1148.0
You can see that if you flip the last 3 columns horizontally, then the last 3 rows vertically, the data will be correct. As far as I can tell this is true for all input sizes so it's an easy (albeit hacky) fix. I am however worried about about computational time of the fix because I may have to perform this on 1024x1024 or even 2048x2048 images in the future.
I am fairly confident that my 1D FFT algorithm doFFT() is correct, and I am getting the expected values for the forward 2D FFT. It is just the inverse 2D FFT that is causing me trouble.
Does anyone see where my error is?
Code
private static double[] cose;
private static double[] sin;
public static void main(String[] args) {
float[][] img = new float[][]{
{ 1050.0f, 1147.0f, 1061.0f, 1143.0f},
{ 1046.0f, 1148.0f, 1118.0f, 1073.0f},
{ 1072.0f, 1111.0f, 1154.0f, 1101.0f},
{ 1078.0f, 1101.0f, 1106.0f, 1062.0f}
};
int size = img.length;
System.out.println("Image");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
System.out.print(img[i][j] + "\t");
}
System.out.println();
}
Complex[][] fft = fft2D(toComplex(img), false);
Complex[][] inverse = fft2D(fft, true);
System.out.println("\nInverse");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
System.out.print(inverse[i][j].getReal() + "\t");
}
System.out.println();
}
}
public static Complex[][] fft2D(Complex[][] pixels, boolean inverse){
int size = pixels.length;
computeCosSin(size);
Complex[][] data = transpose(pixels.clone());
Complex[] temp;
// FFT of rows
for (int i = 0; i < size; i++)
{
temp = doFFT(data[i], size);
data[i] = temp;
}
// FFT of columns
for (int i = 0; i < size; i++)
{
temp = new Complex[size];
for (int j = 0; j < size; j++)
{
temp[j] = data[j][i];
}
Complex[] temp2 = doFFT(temp, size);
for (int j = 0; j < size; j++)
{
data[j][i] = temp2[j];
}
}
if (!inverse)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
data[i][j] = data[i][j].divide(size*size);
}
}
}
return data;
}
public static Complex[] doFFT(Complex[] data, int size){
Complex[] temp = new Complex[size];
int j = 0;
for (int i = 0; i < size; i++) {
temp[i] = data[j];
int k = size / 2;
while ((j >= k) && (k > 0)) {
j -= k;
k /= 2;
}
j += k;
}
Complex n,m,h,f;
for(int i=0; i<size;i+=4){
n = temp[i].add(temp[i+1]);
m = temp[i+2].add(temp[i+3]);
h = temp[i].subtract(temp[i+1]);
f = temp[i+2].subtract(temp[i+3]);
Complex mult = h.add(f.multiply(Complex.I));
Complex sub = h.subtract(f.multiply(Complex.I));
temp[i] = n.add(m);
temp[i+2] = n.subtract(m);
temp[i+1] = sub;
temp[i+3] = mult;
}
int u;
for(int i=4; i< size;i<<=1){
int v = size/(i <<1);
for(int c=0; c< size;c +=i<<1){
for(int x=0; x < i; x++){
u = v*x;
double calc = temp[i+c+x].getReal()*cose[u] - temp[i+c+x].getImaginary()*sin[u];
double calc2 = temp[i+c+x].getReal()*sin[u] + temp[i+c+x].getImaginary()*cose[u];
Complex fftArray = new Complex(calc,calc2);
temp[(i+c+x)] =temp[(c+x)].subtract(fftArray);
temp[(c+x)] = temp[(c+x)].add(fftArray);
}
}
}
return temp;
}
public static Complex[][] toComplex(float[][] arr)
{
Complex[][] newArr = new Complex[arr.length][arr.length];
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr.length; j++)
{
newArr[i][j] = new Complex(arr[i][j], 0.0);
}
}
return newArr;
}
public static Complex[][] transpose(Complex[][] array)
{
for (int i = 0; i < array.length; i++)
{
for (int j = i+1; j < array[i].length; j++)
{
Complex temp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = temp;
}
}
return array;
}
public static void computeCosSin(int size){
double num = (2.0*Math.PI)/size;
double cos = Math.cos(num);
double sine = Math.sin(num);
cose = new double[size];
sin = new double[size];
cose[0] =1.0;
for(int i=1; i<size;i++){
cose[i] = cos*cose[i-1] + sine*sin[i-1];
sin[i] = cos*sin[i-1] - sine*cose[i-1];
}
}
}
This doesn't solve the root problem, but it does change the data I'm getting to the data I expect so it will serve my purpose for now. I do worry it will be incredibly slow on large arrays.
This function swaps row i with row N-i and then swaps every column i with column N-i, for 0 < i < N, (Assuming a square, power of 2 input array)
public Complex[][] inverseFix(Complex[][] array)
{
int size = array.length;
// Swap rows
Complex[] temp;
for (int i = 1; i < size/2; i++)
{
temp = array[i];
array[i] = array[size-i];
array[size-i] = temp;
}
// Swap columns
Complex temp2;
for (int i = 0; i < size; i++)
{
for (int j = 1; j < size/2; j++)
{
temp2 = array[i][j];
array[i][j] = array[i][size-j];
array[i][size-j] = temp2;
}
}
return array;
}

Matrix Multiplication in Java Without Using Imports

I'm trying to make a method that reads input from 2 files, each containing a matrix. The objective is to check if they can be multiplied (if the length of the rows of one is the same as the length of the columns in the other) then create a product matrix of the two inputted matrices. Here is what I have so far. It doesn't work, and I wasn't expecting it to. I just want to get some help on the logic part of the method.
public class MatrixOps {
public static double[][] multiply(double[][] matrix1, double[][] matrix2) {
int matrix1Cols = matrix1.length;
int matrix1Rows = matrix1[0].length;
int matrix2Cols = matrix2.length;
int matrix2Rows = matrix2[0].length;
double[][] productMatrix = new double[0][0];
if (matrix1Rows == matrix2Cols) {
productMatrix = new double[matrix1Cols][matrix2Rows];
for (int i = 0; i < matrix1Cols; i++) {
for (int j = 0; j < matrix1Rows; j++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][i];
for (int k = 0; k < matrix2Rows; k++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
}
}
}
}
if (matrix1Cols == matrix2Rows) {
productMatrix = new double[matrix2Cols][matrix1Rows];
for (int i = 0; i < matrix2Rows; i++) {
for (int j = 0; j < matrix1Cols; j++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][i];
for (int k = 0; k < matrix2Rows; k++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
}
}
}
}
return productMatrix;
}
}
Any ideas on how to get it to work?
First of all, not necessary to do product matrix on both loops:
for (int i = 0; i < matrix1Cols; i++) {
for (int j = 0; j < matrix1Rows; j++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][i];
for (int k = 0; k < matrix2Rows; k++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
}
}
This is sufficient:
for (int i = 0; i < matrix1Cols; i++) {
for (int j = 0; j < matrix1Rows; j++) {
for (int k = 0; k < matrix2Rows; k++) {
productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
}
}
}
Also if you are changing the order of the matrix multiplications, then it must be reflected on your operation (seems you are just doing copy paste)
so on the second part:
if (matrix1Cols == matrix2Rows) {
The matrix multiplication has to change from:
productMatrix[i][j] += matrix1[i][j] * matrix2[j][k];
to:
productMatrix[i][j] += matrix2[i][j] * matrix1[j][k];

Array Index out of Bound Exception while running the program to find largest of an array

Here is the code:Getting Array index out of Bound exception
class Max {
public static void main(String args[]) {
int a[][];
Scanner src = new Scanner(System. in );
System.out.println("Enter the no of rows");
int rows = src.nextInt();
a = new int[rows][5];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = src.nextInt();
}
}
System.out.println("Array is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" " + a[i][j]);
}
System.out.println();
}
int l = a[0][0];
int i;
for (i = 0; i < rows; i++)
l = a[i][0];
for (int j = 0; j < 5; j++)
if (l < a[i][j])
l = a[i][j];
System.out.println("Max" + l);
}
}
on run time it gives the following:
Exception in thread "main" java.lang.Array index out of Bound Exception:3
at Max.main Max.java:33
Can Anyone suggest what is wrong in the code????
You miss a { after the for (i = 0; i < rows; i++) so that the for (int j = 0; j < 5; j++) loop is inside the first one.
Without that, this gives:
for (i = 0; i < rows; i++)
l = a[i][0];
// End of the for i loop, now i = rows.
for (int j = 0; j < 5; j++)
if (l < a[i][j]) // i = rows: bang.
l = a[i][j];
Try this:
public static void main(String args[]) {
int a[][];
Scanner src = new Scanner(System. in );
System.out.println("Enter the no of rows");
int rows = src.nextInt();
a = new int[rows][5];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = src.nextInt();
}
}
System.out.println("Array is");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(" " + a[i][j]);
}
System.out.println();
}
int l = a[0][0];
int i;
for (i = 0; i < rows; i++) {
l = a[i][0];
for (int j = 0; j < 5; j++)
if (l < a[i][j])
l = a[i][j];
}
System.out.println("Max" + l);
}

Java library eigenvalues float numbers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Hi I have the Jama librarry, but this libraray work only with Double numbers..and its very slow. for Android app ..and finally i dont need so high precision of eig decomp..so is there some JAva libaray with float num.....similar in syntax with jama? becouse...I dont want to re-- write again my 440 rows code thanks. eig. Transpose, inverse and so basic Linear algebra operations..
or exist same java library eigenvalues. with threads?
I know of one library called la4j, you might be interested in looking into that. I should mention that, generally, I don't think Java is a good choice if you're planning on doing many matrix manipulations/calculations (I myself have tried and hit a dead-end), you might be better off looking into Python (NumPy) or C++ (Armadillo) for such projects.
Or are you looking for something like this?
import java.util.Arrays;
public class Matrix {
protected int rows;
protected int cols;
double[][] values;
public Matrix(int rows, int cols) {
this.rows = rows;
this.cols = cols;
values = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = 0;
}
public Matrix(int[][] M) {
this.rows = M.length;
this.cols = M[0].length;
values = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = M[i][j];
}
public Matrix(double[][] M) {
this.rows = M.length;
this.cols = M[0].length;
values = new double[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = M[i][j];
}
public void setToEye() {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
values[i][j] = (i == j) ? 1 : 0;
}
public static int[] matrixSize(Matrix M) {
int[] size = new int[2];
size[0] = M.rows;
size[1] = M.cols;
return size;
}
public static double vectMul(double[] A, double[] B) {
double suma = 0;
for (int i = 0; i < A.length; i++)
suma += A[i] * B[i];
return suma;
}
public static Matrix matrixTranspose(Matrix M) {
int[] size = matrixSize(M);
double[][] Mt = new double[size[0]][size[1]];
for (int i = 0; i < size[0]; i++)
for (int j = 0; j < size[1]; j++)
Mt[i][j] = M.getValue(j, i);
return new Matrix(Mt);
}
public static Matrix matrixMul(Matrix A, Matrix B) {
int m1 = matrixSize(A)[0];
int n1 = matrixSize(A)[1];
int m2 = matrixSize(B)[0];
int n2 = matrixSize(B)[1];
double[][] rez;
if (n1 != m2) {
System.err.println("Inner matrix dimensions must agree!");
return null;
}
rez = new double[m1][n2];
for (int i = 0; i < m1; i++)
for (int j = 0; j < n2; j++)
rez[i][j] = vectMul(A.getRow(i), B.getColumn(j));
Matrix r = new Matrix(rez);
return r;
}
public static Matrix matrixMulWithMod(Matrix A, Matrix B, double mod) {
int m1 = matrixSize(A)[0];
int n1 = matrixSize(A)[1];
int m2 = matrixSize(B)[0];
int n2 = matrixSize(B)[1];
double[][] rez;
if (n1 != m2) {
System.err.println("Inner matrix dimensions must agree!");
return null;
}
rez = new double[m1][n2];
for (int i = 0; i < m1; i++)
for (int j = 0; j < n2; j++)
rez[i][j] = vectMul(A.getRow(i), B.getColumn(j)) % mod;
Matrix r = new Matrix(rez);
return r;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.rows; i++) {
sb.append(Arrays.toString(values[i]));
sb.append('\n');
}
String str = sb.toString();
return str;
}
public double[][] getValues() {
return values;
}
public void setValues(double[][] values) {
this.values = values;
}
public void setValues(int[][] values) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
this.values[i][j] = (double) values[i][j];
}
public double getValue(int row, int col) {
return values[row][col];
}
public void setValue(int row, int col, double value) {
values[row][col] = value;
}
public double[] getRow(int row) {
double[] temp = new double[cols];
for (int i = 0; i < temp.length; i++)
temp[i] = values[row][i];
return temp;
}
public double[] getColumn(int col) {
double[] temp = new double[rows];
for (int i = 0; i < temp.length; i++)
temp[i] = values[i][col];
return temp;
}
public double[] toDoubleArray() {
double[] temp = new double[rows * cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
temp[i * (rows + 1) + j] = values[i][j];
return temp;
}
public int[] toIntArray() {
int[] temp = new int[rows * cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
temp[i * (rows + 1) + j] = (int) values[i][j];
return temp;
}
public int getRowCount() {
return this.rows;
}
public int getColumnsCount() {
return this.cols;
}
public static double getMatrixDet(Matrix M) {
int m = M.getRowCount();
int n = M.getColumnsCount();
double D = 0;
if (m != n) {
System.err.println("Matrix must be square!");
System.exit(0);
}
if (n > 1) {
Matrix I = new Matrix(m - 1, n - 1);
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
I.setValue(i - 1, j - 1, M.getValue(i, j));
D = M.getValue(0, 0) * getMatrixDet(I);
} else
D = M.getValue(0, 0);
// za niz , kopira iz niza a elemente 0:i-1 i+1:n sredi za matrcu
Matrix I = new Matrix(m - 1, n - 1);
for (int i = 1; i < n; i++) {
I = M.withoutIthRowAndJthCol(i, 0);
D = D + Math.pow((-1), i) * M.getValue(i, 0) * getMatrixDet(I);
}
return D;
}
public Matrix transpose() {
Matrix temp = new Matrix(this.values);
for (int i = 0; i < this.rows; i++)
for (int j = 0; j < this.cols; j++)
this.values[i][j] = temp.getValue(j, i);
return this;
}
private Matrix withoutIthRowAndJthCol(int row, int col) {
Matrix temp = new Matrix(this.rows - 1, this.cols - 1);
int k = 0, l = 0;
for (int i = 0; i < this.getRowCount(); i++) {
if (i == row)
continue;
for (int j = 0; j < this.getColumnsCount(); j++) {
if (j == col)
continue;
temp.setValue(k, l, this.values[i][j]);
l++;
}
l %= 2;
k++;
}
return temp;
}
public static Matrix getMatrixAdj(Matrix M) {
int m = M.getRowCount();
int n = M.getColumnsCount();
Matrix A = new Matrix(m, n);
if (m != n) {
System.err.println("Matrix must be square!");
System.exit(0);
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
A.setValue(i, j, Math.pow((-1), i + j)
* getMatrixDet(M.withoutIthRowAndJthCol(i, j)));
}
A.transpose();
return A;
}
public static Matrix matrixDiv(Matrix M, double n) {
Matrix temp = M;
for (int i = 0; i < M.getRowCount(); i++)
for (int j = 0; j < M.getColumnsCount(); j++)
temp.setValue(i, j, (M.getValue(i, j) / n));
return temp;
}
public static Matrix getMatrixInv(Matrix M) {
Matrix I = new Matrix(M.getRowCount(), M.getColumnsCount());
if (M.getRowCount() != M.getColumnsCount()) {
System.err.println("Matrix must be square!");
System.exit(0);
}
if (getMatrixDet(M) == 0) {
System.err.println("Matrix is singular!");
System.exit(0);
}
I = matrixDiv(getMatrixAdj(M), getMatrixDet(M));
return I;
}
}
if this is what you wanted, you're welcome.

Categories

Resources