Dynamics Matrices

I am trying to make a class of something called tensors. What the user puts in is an integer which defines the rank of the tensor.

What the rank does is it tells us how many "[]" operators there will be. So a rank 1 tensor would have

private Object[] myStuff;

a rank 2 tensor

private Object[][] myStuff;

rank three

private Object[][][] myStuff;

and so on.

I would prefer not using multiple classes for each rank tensor (have a rankOneTensor class, a rankTwoTensor class, etc.), but I don't see how this can be done quickly and effeciently!

I thought about having a init() method defined as

protected void init(Object[] myStuff, int ranker)

{

//myStuff = new Object[4];

//if ranker>1, then have each Object in the myStuff array be an array of

//objects and call the init() method for these arrays with a

//decremented ranker

//else make the Objects 0

}

but I had compiling errors (what luck!). I googled other peoples methods and they defined other classes (the problem is what if I have a rank, say, 20 tensor?) which seemed rather tedious.

Is there any dynamic way to define the number of "[]" operators there can be? That is my next guess on doing this effeciently.

[1302 byte] By [compadrea] at [2007-10-2 18:56:48]
# 1

You can do this dynamically using reflection, or you can (assuming non-sparse rectangular tensors) flatten the tensor into a single array, much like a fortran or lisp multidimesional array, and calculate the offset into this array from the indices:

public class Tensor {

final int rank;

final int[] dims;

final int size;

final Object[] data;

public Tensor (final int...dims) {

this.rank = dims.length;

this.dims = dims.clone();

int size = 1;

for (int dim : dims) { size *= dim; }

this.size = size;

this.data = new Object[size];

}

public Object get (final int...indices) {

return data[flatten(indices)];

}

public void set (final Object value, final int...indices) {

data[flatten(indices)] = value;

}

private int flatten (final int...indices) {

if (indices.length != rank) {

throw new IllegalArgumentException("incorrect number of indices, should be " + rank);

}

int index = 0;

for (int i = rank - 1; i >= 0; --i) {

final int i_i = indices[i];

final int dim_i = dims[i];

if (i_i < 0) {

throw new IndexOutOfBoundsException("i[" + i + "] less than zero : " + i_i);

}

if (i_i > dim_i) {

throw new IndexOutOfBoundsException("i[" + i + "] greater than " + dim_i + " : " + i_i);

}

index = (index * dim_i) + i_i;

}

return index;

}

public static void main (final String...args) {

Tensor t = new Tensor(2, 3, 4);

for (int i2 = 0; i2 < 4; ++i2) {

for (int i1 = 0; i1 < 3; ++i1) {

for (int i0 = 0; i0 < 2; ++i0) {

System.out.print(t.flatten(i0, i1, i2) + ", ");

t.set("(" + i0 + "," + i1 + "," + i2 + ")", i0, i1, i2);

}

}

}

System.out.println();

for (int i2 = 0; i2 < 4; ++i2) {

for (int i1 = 0; i1 < 3; ++i1) {

for (int i0 = 0; i0 < 2; ++i0) {

if (!("(" + i0 + "," + i1 + "," + i2 + ")").equals(t.get(i0, i1, i2))) {

System.out.println("oops");

}

}

}

}

}

}

pm_kirkhama at 2007-7-13 20:35:03 > top of Java-index,Other Topics,Algorithms...