You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Oscar Bastidas <ob...@umn.edu.INVALID> on 2021/02/16 09:26:25 UTC

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

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

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

Posted by Oscar Bastidas <ob...@umn.edu.INVALID>.
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 <al...@gmail.com> 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 <ob...@umn.edu.invalid>
> 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
> >
>

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

Posted by Alex Herbert <al...@gmail.com>.
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 <ob...@umn.edu.invalid>
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
>