matrix - the value of the object is null once I get out of the function (Java) -
import java.util.*; public class algorithm { public static class matrix{ private double[][] x; } public static scanner scan = new scanner(system.in); private static string str; public static void read_data(double[] radians, double[] l) { l[0] = 0.0; int i; (i = 0; < 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 if (i < 9) str = str.substring(0, str.length()-1); //traiem la coma radians[i] = math.toradians(double.parsedouble(str)); } radians[i] = 0.0; } public static void print_matrix(double[][] m) { (int k = 0; k < 4; k++) { system.out.print("\n"); (int j = 0; j < 4; j++) { system.out.print(string.valueof(m[k][j]) + " , "); } } } public static void print_jacobian(double[][] m) { system.out.print("jacobian matrix ="); (int k = 0; k < 3; k++) { system.out.print("\n"); (int j = 0; j < 9; j++) { system.out.print(string.valueof(m[k][j]) + " , "); } } } public static void init_matrix(double[][] m, int i, double[] radians, double[] l) { m[0][3] = l[i]; m[0][0] = math.cos(radians[i]); m[0][1] = -math.sin(radians[i]); m[1][0] = math.sin(radians[i]); m[1][1] = math.cos(radians[i]); (int k = 0; k < 4; k++) { (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; //system.out.print(string.valueof(m[k][j]) + "\n"); } } } public static void init_ultima_matrix(double[][] m, int i, double[] l) { m[0][3] = l[i]; (int k = 0; k < 4; k++) { (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[] radians) { m[0][0] = -math.sin(radians[i]); m[0][1] = -math.cos(radians[i]); m[1][0] = math.cos(radians[i]); m[1][1] = -math.sin(radians[i]); (int k = 0; k < 4; k++) { (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) { (int k = 0; k < 4; k++) { (int j = 0; j < 4; j++) { m[k][j] = 0.0; } } } public static void fulfill_ts(matrix[] ts, double[] radians, double[] l) { int i; (i = 0; < 9; i++) { ts[i].x = new double[4][4]; init_matrix(ts[i].x, i, radians, l); //print_matrix(ts[i].x); } init_ultima_matrix(ts[i].x, i, l); } public static void fulfill_ds(matrix[] ds, double[] radians) { int i; (i = 0; < 9; i++) { ds[i].x = new double[4][4]; init_derivada(ds[i].x, i, radians); } 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 = 0; < 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_t(matrix[] ts, double[][] t) { t = ts[0].x; (int j = 1; j < 10; j++) { t = product(t, ts[j].x); } } 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; (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]; (int = 0; < 10; i++) { ts[i] = new matrix(); ts[i].x = new double[4][4]; ds[i] = new matrix(); ds[i].x = new double[4][4]; } double[] radians = new double[10]; double[] l = new double[10]; read_data(radians, l); fulfill_ts(ts, radians, l); fulfill_ds(ds, radians); matrix t = new matrix(); t.x = new double[4][4]; system.out.print("\n ts matrix ="); (int q=0; q<10; ++q) print_matrix(ts[q].x); calc_t(ts, t.x); system.out.print("\n t matrix ="); print_matrix(t.x); matrix jacobian = new matrix(); jacobian.x = new double[3][9]; (int j=0; j<9; j++) calc_jacobian(ts, ds, j, jacobian.x); print_jacobian(jacobian.x); //la matriu jacobiana hauria d'estar acabada } }
hello here's code. problem in function "calc_t(matrix[] ts, double[][] t)", if print value of t before exit function, it's ok; if once i've gone out of function, says:
t matrix = null , null , null , null , null , null , null , null , null , null , null , null , null , null , null , null
can me?
thank much
this problem:
private static void calc_t(matrix[] ts, double[][] t) { t = ts[0].x; ... }
arguments in java passed by value. basically, you're ignoring original value of t
, assigning new value it, , doing nothing - that's not going change caller. want like:
private static double[][] calc_t(matrix[] ts) { double[] t = ts[0].x; (int j = 1; j < 10; j++) { t = product(t, ts[j].x); } return t; }
then call as:
t.x = calc_t(ts);
you should strongly consider using double[][]
instead of double[][]
unless need able represent nulls, , start following java naming conventions.
Comments
Post a Comment