creation of 2d matrix in colt library

H

harryos

hi
i am using colt library(http://acs.lbl.gov/~hoschek/colt/) to do
matrix ops
i want to create a 2d matrix from a double[] .But the library provides
only DenseDoubleMatrix2D constructors that take in double[][].Is there
any way i can create a 2D matrix with this library?

Do i have to extend the class and add a constructor to take double[]?

i did something like

import cern.colt.matrix.impl.DenseDoubleMatrix2D;

class MyMatrix2D extends DenseDoubleMatrix2D{
public MyMatrix2D(double[] vals ,int rows){
super(rows,(rows != 0 ? vals.length/rows : 0));
int columns=(rows != 0 ? vals.length/rows : 0);
if (rows*columns != vals.length) {
throw new IllegalArgumentException("Array length must be a
multiple of "+rows);
}
double[][]data = new double[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[j] = vals[i+j*rows];
}
}
super.assign(data);
}

public MyMatrix2D(double[][] data){
super(data);
}


}


this works.and i can create MyMatrix2D objects from a double[] and an
'expected number of rows' value.but i am not sure if i am reinventing
the wheel..Any experts in colt library pls advise.
thanks
harry
 
R

Robert

harryos a écrit :
hi
i am using colt library(http://acs.lbl.gov/~hoschek/colt/) to do
matrix ops
i want to create a 2d matrix from a double[] .But the library provides
only DenseDoubleMatrix2D constructors that take in double[][].Is there
any way i can create a 2D matrix with this library?

Do i have to extend the class and add a constructor to take double[]?

i did something like

import cern.colt.matrix.impl.DenseDoubleMatrix2D;

class MyMatrix2D extends DenseDoubleMatrix2D{
public MyMatrix2D(double[] vals ,int rows){
super(rows,(rows != 0 ? vals.length/rows : 0));
int columns=(rows != 0 ? vals.length/rows : 0);
if (rows*columns != vals.length) {
throw new IllegalArgumentException("Array length must be a
multiple of "+rows);
}
double[][]data = new double[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
data[j] = vals[i+j*rows];
}
}
super.assign(data);
}

public MyMatrix2D(double[][] data){
super(data);
}


}


this works.and i can create MyMatrix2D objects from a double[] and an
'expected number of rows' value.but i am not sure if i am reinventing
the wheel..Any experts in colt library pls advise.
thanks


It seems, you find the shortest code. You could use a factory method,
sparring you to add your class.
 
R

Roland de Ruiter

hi
i am using colt library(http://acs.lbl.gov/~hoschek/colt/) to do
matrix ops
i want to create a 2d matrix from a double[] .But the library provides
only DenseDoubleMatrix2D constructors that take in double[][].Is there
any way i can create a 2D matrix with this library?

Use the factory object, in your case probably the factory that creates
dense matrices.
<http://acs.lbl.gov/~hoschek/colt/api/cern/colt/matrix/DoubleFactory2D.html#dense>


import cern.colt.matrix.DoubleFactory2D

DoubleFactory2D matrixFactory = DoubleFactory2D.dense;
// or = DoubleFactory2D.sparse;
// or = DoubleFactory2D.rowCompressed;

double[] vals = ...;
int rows = ...;
DoubleMatrix2D yourMatrix = matrixFactory.make(vals ,rows);
 

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

Staff online

Members online

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top