[math] How to Convert 'RealMatrix' Object to Array

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[math] How to Convert 'RealMatrix' Object to Array

Oscar Bastidas-2
Hello,

Would someone please tell me how I can go about converting a 'RealMatrix'
object to an array?  When I just print out the RealMatrix object I get
something that looks like:


Array2DRowRealMatrix{{123},{456},{789}}


I would like to be able to selectively cycle through each number
individually.

Thanks.

Oscar

Oscar Bastidas, Ph.D.
Postdoctoral Research Associate
University of Minnesota
Reply | Threaded
Open this post in threaded view
|

Re: [math] How to Convert 'RealMatrix' Object to Array

Alex Herbert
The methods:

    double[][] getData();
    double[][] getDataRef();

Provide access to the underlying 2D matrix as a copy or a direct reference.

The method:

   double getEntry(int, int);

Provides access to an element of the matrix.

So you can do for example:

    Array2DRowRealMatrix m = ...;

    int cols = m.getColumnDimension();
    int rows = m.getRowDimension();
    double[][] data = m.getDataRef();
    for (int row = 0; row < rows; row++) {
      for (int column = 0; column < cols; column++) {
        double d = m.getEntry(row, column);
        double d2 = data[row][column];
        assert d == d2;
      }
    }

If you need to visit a part of the matrix then you can use the visitor API.
For example to sum a sub-matrix:

    int startRow = 2;
    int endRow = 5;
    int startColumn = 15;
    int endColumn = 20;
    double sum = m.walkInOptimizedOrder(
        new DefaultRealMatrixPreservingVisitor() {
          double s = 0;
          @Override
          public void visit(int row, int column, double value) {
            s += value;
          }
          @Override
          public double end() {
            return s;
          }
        }, startRow, endRow, startColumn, endColumn);

You can read about the available operations in the API here:

https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/index.html

Regards,

Alex



On Tue, 16 Feb 2021 at 09:28, Oscar Bastidas <[hidden email]>
wrote:

> Hello,
>
> Would someone please tell me how I can go about converting a 'RealMatrix'
> object to an array?  When I just print out the RealMatrix object I get
> something that looks like:
>
>
> Array2DRowRealMatrix{{123},{456},{789}}
>
>
> I would like to be able to selectively cycle through each number
> individually.
>
> Thanks.
>
> Oscar
>
> Oscar Bastidas, Ph.D.
> Postdoctoral Research Associate
> University of Minnesota
>
Reply | Threaded
Open this post in threaded view
|

Re: [math] How to Convert 'RealMatrix' Object to Array

Oscar Bastidas-2
Awesome!  Thank you so much for this.

Oscar

Oscar Bastidas, Ph.D.
Postdoctoral Research Associate
University of Minnesota

On Tue, Feb 16, 2021, 5:25 AM Alex Herbert <[hidden email]> wrote:

> The methods:
>
>     double[][] getData();
>     double[][] getDataRef();
>
> Provide access to the underlying 2D matrix as a copy or a direct reference.
>
> The method:
>
>    double getEntry(int, int);
>
> Provides access to an element of the matrix.
>
> So you can do for example:
>
>     Array2DRowRealMatrix m = ...;
>
>     int cols = m.getColumnDimension();
>     int rows = m.getRowDimension();
>     double[][] data = m.getDataRef();
>     for (int row = 0; row < rows; row++) {
>       for (int column = 0; column < cols; column++) {
>         double d = m.getEntry(row, column);
>         double d2 = data[row][column];
>         assert d == d2;
>       }
>     }
>
> If you need to visit a part of the matrix then you can use the visitor API.
> For example to sum a sub-matrix:
>
>     int startRow = 2;
>     int endRow = 5;
>     int startColumn = 15;
>     int endColumn = 20;
>     double sum = m.walkInOptimizedOrder(
>         new DefaultRealMatrixPreservingVisitor() {
>           double s = 0;
>           @Override
>           public void visit(int row, int column, double value) {
>             s += value;
>           }
>           @Override
>           public double end() {
>             return s;
>           }
>         }, startRow, endRow, startColumn, endColumn);
>
> You can read about the available operations in the API here:
>
>
> https://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/index.html
>
> Regards,
>
> Alex
>
>
>
> On Tue, 16 Feb 2021 at 09:28, Oscar Bastidas <[hidden email]>
> wrote:
>
> > Hello,
> >
> > Would someone please tell me how I can go about converting a 'RealMatrix'
> > object to an array?  When I just print out the RealMatrix object I get
> > something that looks like:
> >
> >
> > Array2DRowRealMatrix{{123},{456},{789}}
> >
> >
> > I would like to be able to selectively cycle through each number
> > individually.
> >
> > Thanks.
> >
> > Oscar
> >
> > Oscar Bastidas, Ph.D.
> > Postdoctoral Research Associate
> > University of Minnesota
> >
>