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
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