Need help for optimization of my code?
Ok guys i have implemented some code for matix multimplication and some other fnctions as isDiagonal() Transpos, Symetric,etc etc , but the problem is that i have to optimise it to achieve benchmark of the requested from our teacher.
My code i have done is taking too much time, do u have any idea gys how can i reduce time ???
Code:
public RMatrix<E> getTranspose() {
// TODO Auto-generated method stub
SquareMatrix<E> a = new SquareMatrix(size);
for(int i= 0; i < size; i++){
for(int j=0; j < size; j++){
a.vals[i][j] = this.vals[j][i];
}
}
return a;
}
public RMatrix<E> getOne() {
// TODO Auto-generated method stub
Ring<E>[][] result = new Ring[size][size];
for(int i= 0; i < this.size; i++){
for(int j=0; j < this.size; j++){
if(i == j){
result[i][j] = this.getElementAt(i, j).getOne();
}
else{
result[i][j] = this.getElementAt(i, j).getNull();
}
}
}
SquareMatrix<E> oneResult = new SquareMatrix<E>(result);
return oneResult;
}
private RMatrix<E> NaiveMult(RMatrix<E> a ,RMatrix<E> e) {
// TODO Auto-generated method stub
Ring<E>[][] result = new Ring[a.size()][a.size()];
Ring<E>[][] multVal = new Ring[a.size()][a.size()];
int counter;
if(!(a.size() == e.size())) throw new NotCompatibleException("Matrices cannot be multiplied!");
for(int i= 0; i < a.size(); i++){
for(int j=0; j < a.size() ; j++){
counter = 0;
for(int k = 0; k < a.size(); k++){
multVal[i][j] = a.getElementAt(i, k).mult( e.getElementAt(k, j));
if(counter == 0){
result[i][j] = multVal[i][j] ;
counter++;
}else{
result[i][j] = result[i][j].plus((E)multVal[i][j]);
counter++;
}
}
}
}
SquareMatrix<E> multResult = new SquareMatrix<E>(result) ;
return multResult;
}
public RMatrix<E> plus(RMatrix<E> e) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Ring<E>[][] result = new Ring[size][size];
if(this.size != e.size()) throw new NotCompatibleException("Matrices cannot be added!");
//Id the matrix for add is empty then no need to do calculations
if(e.equals(e.getNull())){ return this.getCopy(); }
for(int i= 0; i < this.size; i = i+1){
for(int j=0; j < this.size; j = j + 1){
result[i][j] = this.getElementAt(i, j).plus(e.getElementAt(i, j));
// result[i+1][j+1] = this.getElementAt(i+1, j+1).plus(e.getElementAt(i+1, j+1));
// result[i+2][j+2] = this.getElementAt(i+2, j+2).plus(e.getElementAt(i+2, j+2));
// result[i+3][j+3] = this.getElementAt(i+3, j+3).plus(e.getElementAt(i+3, j+3));
// result[i+4][j+4] = this.getElementAt(i+4, j+4).plus(e.getElementAt(i+4, j+4));
// result[i+5][j+5] = this.getElementAt(i+5, j+5).plus(e.getElementAt(i+5, j+5));
// result[i+6][j+6] = this.getElementAt(i+6, j+6).plus(e.getElementAt(i+6, j+6));
// result[i+7][j+7] = this.getElementAt(i+7, j+7).plus(e.getElementAt(i+7, j+7));
//
}
}
SquareMatrix<E> plusResult = new SquareMatrix<E>(result) ;
return plusResult;
So any help from u will safe my life , at least where can i optimise the code??.