choice of data structure

J

Jeremy Watts

I need to construct some sort of data structure which is a two
dimensional
square array, consisting of elements which are an arbitrary length of
'BigIntegers'.

Anyone any idea how to do this?? Is it possible to construct a 2d
array of
BigInteger Lists somehow? I have no idea how.

This comes from me needing to represent something called a 'Sylvester
matrix' which is a 2d array of elements which are themselves
univariate
polynomial expressions, so something like :-


Sylvester = 4x^2 - 3 8x + 9 7x^3 - 9x^2 + 8
0 5x^2 + 1 8x - 7
0 0
4x^3 -
6x

This isnt a genunine Sylvester matrix, but you get the picture.


Any help appreciated
Jeremy Watts
 
F

Faton Berisha

Jeremy said:
I need to construct some sort of data structure which is a two
dimensional
square array, consisting of elements which are an arbitrary length of
'BigIntegers'.

Anyone any idea how to do this??

If you need a square matrix of elements of type BigInteger, then
something like

int dim = 3;
BigInteger[][] matrix = new BigInteger[dim][dim];

would do.
Is it possible to construct a 2d
array of
BigInteger Lists somehow?

If you previously know the lengths of the "lists", the simplest solution
would be

int dim = 3;
BigInteger[][][] matrix = new BigInteger[dim][dim][];

int length00 = 10;
matrix[0][0] = new BigInteger[length00];
//...

If you want to dynamically change the length, a simple solution (not the
best one) would be

int dim = 3;
ArrayList<BigInteger>[][] matrix = new ArrayList[dim][dim];

int length00 = 10;
matrix[0][0] = new ArrayList<BigInteger>(length00);

The best solution would probably be building a class of your own (e.g.,
Matrix) modeling a matrix of lists.
Any help appreciated
Jeremy Watts

Hope it helps,
Faton Berisha
 
L

Lew

Faton said:
Jeremy said:
I need to construct some sort of data structure which is a two
dimensional
square array, consisting of elements which are an arbitrary length of
'BigIntegers'.

Anyone any idea how to do this??

If you need a square matrix of elements of type BigInteger, then
something like

int dim = 3;
BigInteger[][] matrix = new BigInteger[dim][dim];

would do.
Is it possible to construct a 2d
array of
BigInteger Lists somehow?

If you previously know the lengths of the "lists", the simplest solution
would be

int dim = 3;
BigInteger[][][] matrix = new BigInteger[dim][dim][];

I don't think you want a third array dimension. Use an appropriate object as
the base type of the "2-D array". As I understand the OP, they want a 2-D
matrix of BigInteger, so you don't need a third dimension.
 
J

Jeremy Watts

Lew said:
Faton said:
Jeremy said:
I need to construct some sort of data structure which is a two
dimensional
square array, consisting of elements which are an arbitrary length of
'BigIntegers'.

Anyone any idea how to do this??

If you need a square matrix of elements of type BigInteger, then
something like

int dim = 3;
BigInteger[][] matrix = new BigInteger[dim][dim];

would do.
Is it possible to construct a 2d
array of
BigInteger Lists somehow?

If you previously know the lengths of the "lists", the simplest solution
would be

int dim = 3;
BigInteger[][][] matrix = new BigInteger[dim][dim][];

I don't think you want a third array dimension. Use an appropriate object
as the base type of the "2-D array". As I understand the OP, they want a
2-D matrix of BigInteger, so you don't need a third dimension.

What I actually need is a 2D array of elements which are of 'type' "some
sort of list of BigIntegers of arbitrary length" - the list here consists of
the coefficients the univariate polynomial.

To Faton, thanks for the reply, I did try your suggestion but it kept
returning an error message on compiling. I think I tried something similar
before ie. attempting to create a 2D array of 'lists' in effect and could
never get it to work.

What I have done now is to create a static array, where each element is a 1D
array that is of length equal to the maximum degree of the polynomial used.

Jeremy
 
L

Lew

Jeremy said:
What I actually need is a 2D array of elements which are of 'type' "some
sort of list of BigIntegers of arbitrary length" - the list here consists of
the coefficients the univariate polynomial.

To Faton, thanks for the reply, I did try your suggestion but it kept
returning an error message on compiling. I think I tried something similar
before ie. attempting to create a 2D array of 'lists' in effect and could
never get it to work.

Perhaps if you copy and paste the actual message we could assist you.
What I have done now is to create a static array, where each element is a 1D
array that is of length equal to the maximum degree of the polynomial used.

You could do exactly the same thing with a dynamically-allocated array.

When I put together an SSCCE I was reminded that generics and arrays are
uneasy bedfellows, so indeed an array of coefficients is better if you are
making an array represent the matrix. (If you use Lists of Lists that is OK,
too.) This makes a 3-D array of BigIntegers, in which the first two
dimensions are fixed to the square matrix dimension, and the third is dynamic
depending on the coefficient vector. (Not Vector.)

BigInteger [] [] [] matrix;

To initialize, we fix the first two dimensions to a parameter 'dimen' and
leave the third open.

matrix = new BigInteger [dimen] [dimen] [];

At this point a direct reference to a particular coefficient will throw a NPE:

mat [0] [0] [0] = BigInteger.ZERO; // NullPointerException

We have to initialize mat [0] [0] to a BigInteger [] first.

This example is a bit long but illustrates the points:

<sscce>
/* PolyMatrix.java */
import java.math.BigInteger;

/** PolyMatrix - square matrix of coefficients. */
public class PolyMatrix
{
private final BigInteger [] [] [] matrix;

/** Constructor with dimensionality.
* @param dimen <code>int</code> dimensionality of square matrix.
*/
public PolyMatrix( int dimen )
{
matrix = new BigInteger [dimen] [dimen] [];
}
/** Retrieve the matrix.
* @return <code>BigInteger [] [] []</code> the matrix.
*/
public final BigInteger [] [] [] getMatrix()
{
return matrix;
}

@Override public final String toString()
{
StringBuilder sb = new StringBuilder();
for ( int ax = 0; ax < matrix.length; ++ax )
{
sb.append("{\n");
if ( matrix [ax] == null )
{
sb.append( "null " );
}
else for ( int bx = 0; bx < matrix [ax].length; ++bx )
{
sb.append("\t{ ");
if ( matrix [ax] [bx] == null )
{
sb.append( "null " );
}
else for ( int cx = 0; cx < matrix [ax] [bx].length; ++cx )
{
if ( matrix [ax] [bx] [cx] == null )
{
sb.append( "null" );
}
else
{
sb.append( matrix [ax] [bx] [cx] );
}
sb.append(", ");
}
sb.append("}\n");
}
sb.append("}\n");
}
return sb.toString();
}
/** PolyMatrix main method.
* @param args <code>String []</code> command line arguments
*/
public static void main( String [] args )
{
System.out.println();

PolyMatrix poly = new PolyMatrix( 3 );
BigInteger [] [] [] mat = poly.getMatrix();

try
{
mat [0] [0] [0] = BigInteger.ZERO;
}
catch ( NullPointerException exc )
{
System.out.println( "Didn't initialize entire matrix" );
System.out.println();
}

mat [0] [0] = new BigInteger [4];
for ( int ix = 0; ix < mat [0] [0].length; ++ix )
{
mat [0] [0] [ix] = BigInteger.valueOf((long) ix );
}

mat [0] [1] = new BigInteger [5];
for ( int ix = 0; ix < mat [0] [1].length; ++ix )
{
mat [0] [1] [ix] = BigInteger.valueOf( 9L - (long) ix );
}

mat [2] [0] = new BigInteger [2];
for ( int ix = 0; ix < mat [2] [0].length; ++ix )
{
mat [2] [0] [ix] = BigInteger.valueOf( 5L + (long) ix );
}

mat [2] [2] = new BigInteger [0];
for ( int ix = 0; ix < mat [2] [2] .length; ++ix )
{
mat [2] [2] [ix] = BigInteger.valueOf( 7L - (long) ix );
}

System.out.println( poly );
}
}
</sscce>
 
J

Jeremy Watts

Lew said:
Jeremy said:
What I actually need is a 2D array of elements which are of 'type' "some
sort of list of BigIntegers of arbitrary length" - the list here consists
of the coefficients the univariate polynomial.

To Faton, thanks for the reply, I did try your suggestion but it kept
returning an error message on compiling. I think I tried something
similar before ie. attempting to create a 2D array of 'lists' in effect
and could never get it to work.

Perhaps if you copy and paste the actual message we could assist you.
What I have done now is to create a static array, where each element is a
1D array that is of length equal to the maximum degree of the polynomial
used.

You could do exactly the same thing with a dynamically-allocated array.

When I put together an SSCCE I was reminded that generics and arrays are
uneasy bedfellows, so indeed an array of coefficients is better if you are
making an array represent the matrix. (If you use Lists of Lists that is
OK, too.) This makes a 3-D array of BigIntegers, in which the first two
dimensions are fixed to the square matrix dimension, and the third is
dynamic depending on the coefficient vector. (Not Vector.)

BigInteger [] [] [] matrix;

To initialize, we fix the first two dimensions to a parameter 'dimen' and
leave the third open.

matrix = new BigInteger [dimen] [dimen] [];

At this point a direct reference to a particular coefficient will throw a
NPE:

mat [0] [0] [0] = BigInteger.ZERO; // NullPointerException

We have to initialize mat [0] [0] to a BigInteger [] first.

This example is a bit long but illustrates the points:

<sscce>
/* PolyMatrix.java */
import java.math.BigInteger;

/** PolyMatrix - square matrix of coefficients. */
public class PolyMatrix
{
private final BigInteger [] [] [] matrix;

/** Constructor with dimensionality.
* @param dimen <code>int</code> dimensionality of square matrix.
*/
public PolyMatrix( int dimen )
{
matrix = new BigInteger [dimen] [dimen] [];
}
/** Retrieve the matrix.
* @return <code>BigInteger [] [] []</code> the matrix.
*/
public final BigInteger [] [] [] getMatrix()
{
return matrix;
}

@Override public final String toString()
{
StringBuilder sb = new StringBuilder();
for ( int ax = 0; ax < matrix.length; ++ax )
{
sb.append("{\n");
if ( matrix [ax] == null )
{
sb.append( "null " );
}
else for ( int bx = 0; bx < matrix [ax].length; ++bx )
{
sb.append("\t{ ");
if ( matrix [ax] [bx] == null )
{
sb.append( "null " );
}
else for ( int cx = 0; cx < matrix [ax] [bx].length;
++cx )
{
if ( matrix [ax] [bx] [cx] == null )
{
sb.append( "null" );
}
else
{
sb.append( matrix [ax] [bx] [cx] );
}
sb.append(", ");
}
sb.append("}\n");
}
sb.append("}\n");
}
return sb.toString();
}
/** PolyMatrix main method.
* @param args <code>String []</code> command line arguments
*/
public static void main( String [] args )
{
System.out.println();

PolyMatrix poly = new PolyMatrix( 3 );
BigInteger [] [] [] mat = poly.getMatrix();

try
{
mat [0] [0] [0] = BigInteger.ZERO;
}
catch ( NullPointerException exc )
{
System.out.println( "Didn't initialize entire matrix" );
System.out.println();
}

mat [0] [0] = new BigInteger [4];
for ( int ix = 0; ix < mat [0] [0].length; ++ix )
{
mat [0] [0] [ix] = BigInteger.valueOf((long) ix );
}

mat [0] [1] = new BigInteger [5];
for ( int ix = 0; ix < mat [0] [1].length; ++ix )
{
mat [0] [1] [ix] = BigInteger.valueOf( 9L - (long) ix );
}

mat [2] [0] = new BigInteger [2];
for ( int ix = 0; ix < mat [2] [0].length; ++ix )
{
mat [2] [0] [ix] = BigInteger.valueOf( 5L + (long) ix );
}

mat [2] [2] = new BigInteger [0];
for ( int ix = 0; ix < mat [2] [2] .length; ++ix )
{
mat [2] [2] [ix] = BigInteger.valueOf( 7L - (long) ix );
}

System.out.println( poly );
}
}
</sscce>

many thanks :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top