Matrix cannot be resolved to a variable - java

import java.util.*;
public class Algorithm {
public class Matrix{
private Double[][] x;
}
public static Scanner scan = new Scanner(System.in);
private static String str;
public static void read_data(Double[] degrees, Double[] l) {
l[0] = 0.0;
int i;
for (i = 0; i < 9; i++) {
str = scan.next(); //passem la primera columna
str = scan.next(); //agafem el valor del desplaçament
str = str.substring(0, str.length()-1); //traiem la coma
l[i+1] = Double.parseDouble(str);
str = scan.next(); //passem la primera columna
str = scan.next(); //agafem el valor del desplaçament
str = str.substring(0, str.length()-1); //traiem la coma
degrees[i] = Double.parseDouble(str);
}
degrees[i] = 0.0;
}
public static void init_Matrix(Double[][] M, int i, Double[] degrees, Double[] l) {
M[0][3] = l[i];
M[0][0] = Math.cos(degrees[i]);
M[0][1] = -Math.sin(degrees[i]);
M[1][0] = Math.sin(degrees[i]);
M[1][1] = Math.cos(degrees[i]);
for (int k = 0; i < 4; k++) {
for (int j = 0; j < 4; j++) {
if (k == j && (M[k][j] == null)) M[k][j] = 1.0;
else if(M[k][j] == null) M[k][j] = 0.0;
}
}
}
public static void init_Ultima_Matrix(Double[][] M, int i, Double[] l) {
M[0][3] = l[i];
for (int k = 0; k < 4; k++) {
for (int j = 0; j < 4; j++) {
if (k == j) M[k][j] = 1.0;
else if(M[k][j] == null) M[k][j] = 0.0;
}
}
}
public static void init_Derivada(Double[][] M, int i, Double[] degrees) {
M[0][0] = -Math.sin(degrees[i]);
M[0][1] = -Math.cos(degrees[i]);
M[1][0] = Math.cos(degrees[i]);
M[1][1] = -Math.sin(degrees[i]);
for (int k = 0; k < 4; k++) {
for (int j = 0; j < 4; j++) {
if(M[k][j] == null) M[k][j] = 0.0;
}
}
}
public static void init_Ultima_Derivada(Double[][] M, int i) {
for (int k = 0; k < 4; k++) {
for (int j = 0; j < 4; j++) {
M[k][j] = 0.0;
}
}
}
public static void fulfill_Ts(Matrix[] Ts, Double[] degrees, Double[] l) {
int i;
for (i = 0; i < 9; i++) {
Ts[i].x = new Double[4][4];
init_Matrix(Ts[i].x, i, degrees, l);
}
init_Ultima_Matrix(Ts[i].x, i, l);
}
public static void fulfill_Ds(Matrix[] Ds, Double[] degrees) {
int i;
for (i = 0; i < 9; i++) {
Ds[i].x = new Double[4][4];
init_Derivada(Ds[i].x, i, degrees);
}
init_Ultima_Derivada(Ds[i].x, i);
}
private static Double[][] product(Double[][] A, Double[][] B){
Double suma = 0.0;
Double result[][] = new Double[4][4];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
suma = 0.0;
for(int k = 0; k < 4; k++){
suma += A[i][k] * B[k][j];
}
result[i][j] = suma;
}
}
return result;
}
private static void calc_Jacobian(Matrix[] Ts, Matrix[] Ds, int i, Double[][] jacobian) {
Double[][] tmp;
if (i == 0) tmp = Ds[0].x;
else tmp = Ts[0].x;
for (int j = 1; j < 10; j++) {
if (j == i) tmp = product(tmp, Ds[j].x);
else tmp = product(tmp, Ts[j].x);
}
jacobian[0][i] = tmp[0][3];
jacobian[1][i] = tmp[1][3];
jacobian[2][i] = tmp[0][0];
}
public static void main(String[] args) {
Matrix[] Ts = new Matrix[10];
Matrix[] Ds = new Matrix[10];
for (int i = 0; i < 10; i++) {
Ts[i].x = new Double[4][4];
Ds[i].x = new Double[4][4];
}
Double[] degrees = new Double[10];
Double[] l = new Double[10];
read_data(degrees, l);
fulfill_Ts(Ts, degrees, l);
fulfill_Ds(Ds, degrees);
Matrix jacobian = new Matrix();
jacobian.x = new Double[3][9];
for (int j=0; j<9; j++)
calc_Jacobian(Ts, Ds, j, jacobian.x);
//La matriu Jacobiana hauria d'estar acabada
}
}
Well, this is my code. The error is in the first line where says "Matrix jacobian = new Matrix();". Have I declared it wrong? The definition of Matrix is at the beginning of the code.
It says: No enclosing instance of type Algorithm is accessible. Must qualify the allocation with an enclosing instance of type Algorithm (e.g. x.new A() where x is an instance of Algorithm).
Moreover, I get an Exception with this: "Matrix[] Ts = new Matrix[10];". Can't I declare an array of elements Matrix?
Thank you very much.

Yes, try this: Matrix M = new Matrix();
This one Matrix[] Ts = new Matrix[10]; is valid but you should also loop through the array elements and initialize them by calling the constructor. Otherwise, they will remain having null values.

Change
public class Matrix
to
public static class Matrix
Creating a nested class without the static modifier creates a closure, which allows you to reference non-static members of the enclosing instance. If you don't declare it static, you need an enclosing instance (ie, a this) to create it, which you don't have in static void main().
Explaining a closure is beyond the scope of this answer, but it would be a private member Algorithm _closure in Matrix, which is implicitly referenced (the same way this is), when you mention a non-static member of Algorithm.

Related

Runtime error UVA

My code is returning Runtime error, but I don't Know how to fix this. This code is about the problem 104. I tried to change somethings but nothing... I read about this error and basically is associated with the Scanner.
Please Help me!
import java.util.Scanner;
public class Main {
static int MAX = 20;
static double LUCRO_MIN = 1.01;
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int n, i, j;
double v;
while(sc.hasNext()) {
double[][] W = new double [MAX][MAX];
n = sc.nextInt();
sc.nextLine();
for(i = 0; i < n; i++) {
int pos = 0;
String linha = sc.nextLine();
String vertice[] = linha.split(" ");
for(j = 0; j < n; j++) {
if(i == j) {
W[i][i] = 0;
continue;
}
v = Double.parseDouble(vertice[pos]);
W[i][j] = v;
pos++;
}
}
Converte(n, W);
}
}
static public void Imprime (int i, int j, int l, int P[][][]) {
if(l == 0)
System.out.printf ("%d\n", i + 1);
else {
System.out.printf ("%d ", i + 1);
Imprime (P[l][i][j], j, l - 1, P);
}
}
static void IniZero (int n, double m[][][], int l) {
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
m[l][i][j] = 0;
}
static void Converte(int n, double W[][]) {
double[][][] B = new double [MAX][MAX][MAX];
int [][][]P = new int [MAX][MAX][MAX];
int l, i, j, k;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++) {
B[1][i][j] = W[i][j];
if(W[i][j] > 0)
P[1][i][j] = j;
}
for(l = 2; l <= n; l++) {
IniZero(n, B, l);
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
for(k = 0; k < n; k++) {
if(B[l][i][j] < W[i][k] * B[l - 1][k][j]) {
B[l][i][j] = W[i][k] * B[l - 1][k][j];
P[l][i][j] = k;
if(B[l][i][i] >= LUCRO_MIN) {
Imprime(i, i, l, P);
return;
}
}
}
}
System.out.printf("no arbitrage sequence exists\n");
}
}
Change scanner like this
Scanner input = new Scanner(System.in);
TO
Scanner input = new Scanner(new FileInputStream(args[0]));
and i recommad you to use input.hasNext() instead of input.nextInt();this can cause nullpointerexception

Floyd Warshall algorithm implementation

I've written code for a 100 x 100 adjacency matrix that represents the following directed graph:
I'm attempting to use a Floyd-Warshall algorithm to find the shortest path for all pairs of blue nodes in the graph. How do you only find the all pairs shortest path for the selected nodes? Here's the code I've written thus far:
public class AdjacencyMatrix
{
public static final int NUM_NODES = 100;
public static final int INF = Integer.MAX_VALUE;
public static boolean even(int num)
{
return num%2==0;
}
public static boolean odd(int num)
{
return num%2==1;
}
public static void initialize(int [][] adjMat, int N)
{
for(int i = 0; i < N; i++)
for(int j = 0; j <N; j++)
adjMat[i][j]=INF;
for(int x = 0; x<N; x++)
{
int row = x/10;
int column = x%10;
if (even(row)) {
if (column!=9)
adjMat[x][x+1]=1;
}
if (odd(row)) {
if (column!=0)
adjMat[x][x-1]=1;
}
if (even(column)){
if (row!=9)
adjMat[x][x+10]=1;
}
if (odd(column)) {
if (row!=0)
adjMat[x][x-10]=1;
}
}
}
public void floydWarshall(int[][] adjMat, int N)
{
adjMat = new int[N][N];
initialize(adjMat, NUM_NODES);
for(int k = 0; k < N; ++k) {
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
}
}
}
}
public static void main(String[] args)
{
int adjMat[][] = new int[NUM_NODES][NUM_NODES];
initialize(adjMat, NUM_NODES);
int A,B,C,D,E,F,G,H,I,W;
A = 20;
B = 18;
C = 47;
D = 44;
E = 53;
F = 67;
G = 95;
H = 93;
I = 88;
W = 66;
System.out.println(adjMat[A][B]);
System.out.println();
}
}
First of all, you should not assign new value to adjMat parameter in floydWarshall(), because the value will not be saved after exiting the method.
The next step is to check adjMat[i][k] and adjMat[k][j] for equality to INF and continue the loop if so:
for(int k = 0; k < N; ++k) {
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
if (adjMat[i][k] != INF && adjMat[k][j] != INF) {
adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
}
Shortest Floyd-Warshall algo implemenation:
for(int k = 0; k < N; ++k) {
for(int i = 0; i < N; ++i) {
for(int j = 0; j < N; ++j) {
adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
}
}
}
After running this piece of cade adjMat will contain shortest distances between every pair of nodes.
Update: to avoid integer overflow, fill the matrix with Integer.MAX_VALUE / 2. In general case, it's dangerous to set the maximum possible value to variable as infinity, because you can't perform addition operation with it.

memoized matrix chain multiplication in Java

I feel like I'm really close with this implementation of a memoized matrix chain algorithm in Java, but I'm getting an array out of bounds error on line 45 and 53. These, for some reason, really seem to mess me up. Maybe there's something I'm continually messing up with, but I dunno, obviously. Can anyone help me out?
public class Lab2 {
//fields
static int p[];
static int m[][];
final static int INFINITY = 999999999;
public Lab2() {
//
}
public static void main(String[] args) {
Lab2 lab2 = new Lab2();
Lab2.m = new int[7][7];
Lab2.p = new int[7];
Lab2.p[0] = 20;
Lab2.p[1] = 8;
Lab2.p[2] = 4;
Lab2.p[3] = 25;
Lab2.p[4] = 30;
Lab2.p[5] = 5;
Lab2.p[6] = 10;
int n = Lab2.p.length-1;
//initialize m array to infinity
for (int i = 1; i <= n; i++){
for (int j = i; j <= n; j++){
Lab2.m[i][j]= INFINITY;
}
}
lab2.lookUpChain(m, p, 1, n);
for (int i = 0; i < 8; i++){
for (int j = 0; j < 8; j++){
System.out.println(m[i][j]);
}
}
}
//
public int lookUpChain(int m[][], int p[], int i, int j ){
if (m[i][j]<INFINITY){
return m[i][j];
}
if (i == j){
m[i][j] = 0;
}
else{
for (int k = i; k <= j; i++){
int q = (lookUpChain(m,p,i,k)) + (lookUpChain(m,p,k+1,j)) + (p[i]*p[k]*p[j]);
if (q < m[i][j]){
m[i][j] = q;
}
}
}
return m[i][j];
}
}
else{
for (int k = i; k <= j; i++)
Change to:
else{
for (int k = i; k <= j; k++) // change i to k

Assigning A Random value 3D Array to an Array of class

I have a class with three fields:
public class CCTest {
public double f;
public double[][][] x;
public double counter;
}
I am trying to assign a random number to it. I have the method below for random data generation:
public static double[][][] getRandomX(int x, int y, int z) {
double[][][] result = new double[x][y][z];
Random r = new Random();
for (int i = 0; i < z; i++) {
for (int j = 0; j < y; j++) {
for (int k = 0; k < x; k++) {
result[k][j][i] = r.nextDouble();
}
}
}
// System.out.println(Arrays.deepToString(result));
return result;
}
As for the issue. I have for example an array with 5 CCTest-objects:
CCTest[] cls = new CCTest[5];
How can I assign a random number to each of the 5 CCTest-objects?
I tried this:
for (int i = 0; i < Size =5; i++) {
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].X= getRandomX(this.c, this.D, this.Size);
}
The result should have following structure:
X(:,:,1) =
0.8909 0.5472
0.9593 0.1386
X(:,:,2) =
0.1493 0.8407
0.2575 0.2543
But the code did not produce it. Could anyone guide me to a solution, please?
The problem is that you haven't made any CCTest-instances.
So after you make the CCTest[] cls = new CCTest[5]; the five CCTest-objects are null. You should create them if they don't exist yet:
CCTest[] cls = new CCTest[5];
for (int i = 0; i < (Size = 5); i++) {
// We create a new CCTest-instance if it doesn't exist yet:
if(cls[i] == null){
cls[i] = new CCTest();
}
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].x = getRandomX(this.c, this.D, this.Size);
}
}
Alternatively, you could create them first, and then do the for-loop to assign the random doubles:
CCTest[] cls = new CCTest[5];
for (int i = 0; i < cls.length; i++) {
cls[i] = new CCTest();
}
for (int i = 0; i < (Size = 5); i++) {
cls[i].x = new double[this.c][this.D][this.Size];
for (int j = 0; j < this.D; j++) {
cls[i].x = getRandomX(this.c, this.D, this.Size);
}
}

Multiplying 2d arrays(matrices) in java

My goal is to print out the Matrix when the two arrays are multiplied together. What am I doing wrong with this code? How do I get it so that it prints out the matrix? (Sorry I do not know what other details i should provide and I cannot submit this post unless I add more detail).
public class Matrices {
static int mRows = 0;
static int mCol = 0;
static int nRows = 0;
static int nCol = 0;
public static int[][] multiplyMatrices(int[][] m, int[][] n){
mRows = m.length;
mCol = m[0].length;
nRows = n.length;
nCol = n[0].length;
if(canBeMultiplied(m,n) == false){
throw new IllegalArgumentException("Cannot multiply arrays");
}
int[][] answer = new int[mRows][nCol];
for(int i = 0; i < mRows; i++){
for(int j = 0; j < nCol; j++){
for(int k = 0; k < mCol; k++){
answer[i][j] += m[i][k] * n[k][j];
}
}
}
return answer;
}
public static boolean canBeMultiplied(int[][] m, int[][]n){
mRows = m.length;
mCol = m[0].length;
nRows = n.length;
nCol = n[0].length;
if(nRows == mCol){
return true;
}
return false;
}
public static void main(String[] args) {
int[][] temp1 = {{1,2,3},{4,5,6}};
int[][] temp2 ={{1},{2},{3}};
for(int i = 0; i < mRows; i++){
for(int j = 0; j < nCol; j++){
System.out.print(multiplyMatrices(temp1,temp2)[i][j]);
}
System.out.print("\n");
}
}
}
Thanks for your help.
This could will loop through the the 2D array and print each element.
static final int ROWS = 2;
static final int COLS = 4;
int[][] a2 = new int[ROWS][COLS];
//... Print array in rectangular form
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}

Categories

Resources