gpt4 book ai didi

Java - 复数矩阵

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:45 25 4
gpt4 key购买 nike

我想创建一个类 ComplexMatrix,其字段为 ComplexNumber 类型的数组 [NxN]。我已经创建了一个 ComplexNumber 类,如下所示:

public class ComplexNumber {

private double real;
private double img;

// Getters and setters
public double getReal() {
return real;
}

public void setReal(double real) {
this.real = real;
}

public double getImg() {
return img;
}

public void setImg(double img) {
this.img = img;
}

// Constructor
public ComplexNumber(double real, double img) {
this.real = real;
this.img = img;
}

// Add
public ComplexNumber addComp(ComplexNumber num) {

ComplexNumber num1 = new ComplexNumber(real + num.real, img + num.img);

return num1;
}

// Subtract
public ComplexNumber subtractComp(ComplexNumber num) {

ComplexNumber num1 = new ComplexNumber(real - num.real, img - num.img);

return num1;
}

// Multiply
public ComplexNumber multiplyComp(ComplexNumber num) {

ComplexNumber num1 = new ComplexNumber(real*num.real-img*num.img,real*num.img+img*num.real);

return num1;
}

// Is Equals
boolean equals(ComplexNumber num) {

ComplexNumber num1 = new ComplexNumber(real, img);
if (num.real == num1.real && num.img == num1.img) {
return true;
} else {
return false;
}

}

@Override
public String toString() {

if (img > 0) {
return getReal() + " + " + Math.abs(getImg()) + "i";
}
if (img < 0) {
return getReal() + " - " + Math.abs(getImg()) + "i";
}
if (real==0) {
return getImg() + "i";
}
if (img==0) {
return getReal() + "";
}
return null;
}

}

它有一些方法和它自己的 toString,打印如下:

  3.51 + 1.87i
2.35 - 5.61i
-8.45 + 2.65i

实现的 ComplexMatrix 有一个数组 [M][N] 作为字段,并且 cronstructor public ComplexMatrix(int rows, int cols) 给出该数组使用方法computeRandom从一到十随机数。然后一个toString() 函数(也实现了 ComplexNumber 类中的 toSting()),应该打印随机数数组。所需的 ComplexMatrix 打印结果为:

[1.24 + 2.55i, -0.32 + 2.00i, 1.35 - 5.88i;
-5.71 - 5.91i, 0.29 – 9.14i, 0.00 + 3.51i;
6.44 + 0.00i, -3.51 – 0.67i, 2.10 + 4.20i;]

这是 ComplexMatrix 类:

         import java.util.Random;

public class ComplexMatrix {

private ComplexNumber[][] complexArray;

// Default Constructor
public ComplexMatrix() {
super();
this.complexArray = null;
}

// Copy Constructor
public ComplexMatrix(ComplexMatrix original) {
super();
original.complexArray = complexArray;
}

private Random rand = new Random();

private double computeRandom() {
int randomNum = (int) ((rand.nextDouble() - 0.5) * rand.nextInt(20) * 100);
return randomNum / 100.0;
}

// Random Numbers Constructor
public ComplexMatrix(int rows, int cols) {

double real = 0;
double img = 0;

ComplexNumber[][] complexArray= new ComplexNumber[rows][cols];

for (int i = 0; i < rows; i++) {
System.out.print("\n");
for (int j = 0; j < cols; j++) {
real = computeRandom();
img = computeRandom();
complexArray[i][j] = new ComplexNumber(real, img);
//edw peiramatizomai..to print 8a ginetai sthn toString()
System.out.print(complexArray[i][j].toString());
System.out.print("\t");
}
}

}

@Override
public String toString() {
int rows = 0,cols = 0;
//ComplexNumber[][] complexArray= new ComplexNumber[rows][cols];
ComplexMatrix s = new ComplexMatrix(rows,cols);

String out = "[";
//????????????????????????????????????
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
//????????
out = s.toString();

}
out += "]";
return out;

}
}

最佳答案

我将给出伪代码(部分假设这是一个作业)。但希望这足以让您开始实现,而无需简单地给出解决方案。

@Override
public String toString() {
out = "[";
for (row : rows) {
if (notFirstRow) {
out += "\n"; // New line
}
for (column : columns) {
if (notFirstColumn) {
out += ", ";
}
out += matrix[row][column].toString();
}
}
out += "]";
return out;
}

警告,我没有声称这是最佳的,或者没有更好的方法来格式化矩阵(或复数)......但是如果你能充实它以实际编译,我怀疑你会得到你想要的东西。

关于Java - 复数矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55777103/

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