# commons-user mailing list archives

##### Site index · List index
Message view
Top
From Luc Maisonobe <Luc.Maison...@free.fr>
Subject Re: [math] Re: complex matrix operation
Date Sat, 16 Oct 2010 21:15:08 GMT
```Le 16/10/2010 22:47, Cuong P. Nguyen a écrit :
> Hi Luc and friends,
>
> I have a question more related to java matrix storage. If I have a
> 3-dimentional m matrix say 2 by 2 by 2, I want to access the matrix

Matrices are limited to 2 dimensions. We don't have general k-th order
tensors in commons-math. For higher dimensions, you have to use simple
Java arrays, but you won't have any operations on them.

> efficiently so I would like to know the order in which the data is stored in
> the memory. I am not sure if it is stored consecutively this way in the
> memory register:
> m(1,1,1), m(1,1,2), m(1,2,1), m(1,2,2), m(2,1,1), m(2,1,2), m(2,2,1),
> m(2,2,2)

In Java, arrays are not a simple bunch of elements packed together with
a fancy numbering scheme like you have with other languages. A
three-dimensional array is really an array of two-dimensional arrays,
each of which being an array of one-dimensional arrays, which finally
are a bunch of elements with some meta-data. The lowest level arrays
correspond to the rightmost index while the top level array correspond
to the leftmost index. So m(1,1,1) and m(1,1,2) are certainly close
together in memory as they belong to the same lowest level array but
m(1,2,1) may be a few bytes away. Depending on the way you create your
array, it is even possible to have two consecutive rows be really widely
separated.

>
> I also wonder if the package could store sparse vectors, matrices and
> perform basic operations on them?

Yes, there are sparse vectors and matrices.

Luc

>
> Thanks,
> Cuong
>
> -----Original Message-----
> From: Luc Maisonobe [mailto:Luc.Maisonobe@free.fr]
> Sent: Saturday, October 16, 2010 2:00 PM
> To: Commons Users List
> Subject: Re: [math] Re: complex matrix operation
>
> Le 16/10/2010 20:38, Cuong P. Nguyen a écrit :
>> Hi Luc and friends,
>>
>> Thanks a lot for the quick response
>>
>> Could you give me any simple code example of Complex-type vector-matrix
>> multiplication or matrix-matrix multiplication ?
>
> Here is a matrix vector multiplication:
>
> // create a 2x2 complex matrix
> Complex[][] matrixData = new Complex[][] {
>     { new Complex(1.0,  0.0), new Complex( 0.0, 1.0) },
>     { new Complex(0.0, -1.0), new Complex(-1.0, 0.0) }
> };
> FieldMatrix<Complex> m = new Array2DRowFieldMatrix<Complex>(matrixData);
>
> // create a vector
> Complex[] vectorData = new Complex[] {
>     new Complex(1.0, 2.0),
>     new Complex(3.0, 4.0),
> };
> FieldVector<Complex> u = new ArrayFieldVector<Complex>(vectorData);
>
> // perform matrix-vector multiplication
> FieldVector<Complex> v = m.operate(u);
>
> // print the initial vector
> for (int i = 0; i < u.getDimension(); ++i) {
>     System.out.println(ComplexFormat.formatComplex(u.getEntry(i)));
> }
>
> System.out.println();
>
> // print the result
> for (int i = 0; i < v.getDimension(); ++i) {
>     System.out.println(ComplexFormat.formatComplex(v.getEntry(i)));
> }
>
>
>
> Luc
>
>>
>> I am a new java beginner as well as new to Commons Math community.
>>
>> Thanks,
>>
>> Cuong
>>
>> -----Original Message-----
>> From: Luc Maisonobe [mailto:Luc.Maisonobe@free.fr]
>> Sent: Saturday, October 16, 2010 1:25 PM
>> To: Commons Users List
>> Subject: [math] Re: complex matrix operation
>>
>> Le 16/10/2010 18:53, Cuong P. Nguyen a écrit :
>>> Hi,
>>
>> Hi Cuong,
>>
>> First, please use a [math] marker on the subject line when posting to
>> this list for the commons-math component. The list is shared among
>> several commons components and these markers help filtering.
>>
>>> can I use the package for basic complex-number matrix operations
>>
>> You can use the linear algebra packe from commons-math with complex
>> matrices. The appropriate interface is FieldMatrix<Complex> which has
>> two implementations: Array2DRowFieldMatrix<Complex> and
>> BlockFieldMatrix<Complex>. The former should be preferred for small
>> sizes and the later for large sizes. You can also use complex vectors
>> and LU decomposition with these matrices.
>>
>> Luc
>>
>>>
>>> Thanks
>>>
>>> Cuong
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org