gpt4 book ai didi

Java "The type DenseMatrix must implement the inherited abstract method Matrix.multiply(Vector)"

转载 作者:行者123 更新时间:2023-12-01 11:02:58 25 4
gpt4 key购买 nike

我正在为学校做一个项目,其中给我们一个抽象类Matrix,我们必须在两个不同的类中实现Matrix 的方法。我正在开发一个类 DenseMatrix 并且不断收到错误:

The type DenseMatrix must implement the inherited abstract method Matrix.multiply(Vector)

即使我在DenseMatrix中有一个方法multiply(Vector)

代码(首先是DenseMatrix,然后是Matrix):

import java.util.Vector;
import java.util.*;

public class DenseMatrix implements Matrix{

private int size=0; // size of the matrix- number of rows/columns
private int nnz=0; // number of non-zero elements
private double[][] data;

// Constructor used to initialize the matrix (all elements to zero)
DenseMatrix(int size){
this.size=size;
data=new double[size][size]; // all elements take the values 0.0d
}

// Constructor with Random generator (using nnz random non-zero numbers between 0.01<= x < 1.01
// on any random row,column coordinates)
DenseMatrix(int size,int nnz){
///=====> TO COMPLETE <===========///
Random r = new Random();
for(int i =0; i<nnz;i++){
double randomValue = (double)(.01 + r.nextDouble());
int xCord = (int)(r.nextInt()*size);
int yCord = (int)(r.nextInt()*size);
data[xCord][yCord] = randomValue;
}
}

// Constructor from any other matrix storage using the interface to Dense storage
DenseMatrix(Matrix A){
this.size=A.getSize();
this.nnz=A.getNnz();
data=new double[size][size];
for (int i = 0; i < size; i++){
for (int j=0;j<size;j++)
data[i][j]=A.get(i,j);
}
}

// get the size of the matrix
public int getSize(){return size;}

// get the number of non-zero elements
public int getNnz(){return nnz;}

// Assign the value x to element i,j
public void set(int i,int j, double x){
if ((data[i][j]==0)&&(x!=0.0)) nnz++;
if ((data[i][j]!=0)&&(x==0.0)) nnz--;
data[i][j]=x;
}

// get the value of the element i,j
public double get(int i, int j){
return(data[i][j]);}

// Print matrix using a specific format
public void display(){
System.out.println();
System.out.println("Display in dense format");
for (int i = 0; i < size; i++){
for (int j=0;j<size;j++)
System.out.format("%.4f ",get(i,j));
System.out.println();
}
System.out.println();
}

// get the elements of the diagonal
public double[] getDiagonal(){
///=====> TO COMPLETE <===========///
double[] diag = new double[size];
for(int i = 0;i<size;i++){
diag[i] = get(i,i);}
return diag;
}

public Vector multiply(Vector B){
///=====> TO COMPLETE <===========///
double temp[] = new double[size];
Vector result = new Vector(size);
for(int y = 0;y<size;y++){
for(int x = 0;x<size;x++){
temp[y] = temp[y] + get(x,y);
}
}
for(int i = 0;i<size;i++){
result.set(i,(double)B.get(i) * temp[i]);
}
return result;
}

}



public interface Matrix {
// Assign the value x to element i,j
void set(int i,int j, double x);
// get the value of the element i,j
double get(int i, int j);
// Extract the diagonal of the matrix
double[] getDiagonal();
// get the size of the matrix-- number of rows
int getSize();
// get the number of non-zero elements
int getNnz();
// Multiply a matrix by a vector
Vector multiply(Vector B);
// Print matrix using a specific format
void display();
}

最佳答案

您可能使用了两个不同的Vector 类。方法 DenseMatrix.multiply(Vector)import java.util.Vector; (从文件开头开始)实际上是 DenseMatrix.multiply(java .util.Vector)。如果您在包 mypackage 中有另一个 Vector 类,并且您在 Matrix 接口(interface)中使用该类,则该方法将为 Matrix .multiply(mypackage.Vector)multiply(java.util.Vector)multiply(mypackage.Vector) 不同。您需要确保对 DenseMatrix 和 Matrix 使用相同的 Vector 类。

关于Java "The type DenseMatrix must implement the inherited abstract method Matrix.multiply(Vector)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33201748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com