gpt4 book ai didi

java - 从输入字符串中提取多项式系数

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:47 26 4
gpt4 key购买 nike

我编写了这段代码来提取多项式系数并评估一个点中的方程,并且它有效。现在,我想修改它,以便用户可以输入任何形状的多项式方程。

在我的代码中,您必须输入如下方程:

2*x^2+3*x^1+4

我想更改此设置以便它接受:

2*x^5+1*x+6

此外,如果存在具有相同幂的项,则必须将它们的系数相加。

这是我的java代码:

package Priest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class Equation {

private String Eq;
private final String[] C;
private int Deg;
private final String EqHolder;

public Equation(String Equation) {
this.Eq = Equation;
EqHolder = Equation;
Eq = Eq.replaceAll("[^0-9\\-\\.]+", " ");
Eq = Eq.replaceAll("-", " -");
this.C = Eq.split(" ");
}

public String SourceEquation() {
return EqHolder.toUpperCase().replaceAll("\\*", "").replaceAll("[a-zA-Z]", "\\*(X)").replaceAll("\\+", "\\ + ").replaceAll("\\-", "\\ - ");
}

public List<BigDecimal> CaptureCoeff() {
getDegree();
List<BigDecimal> Temp = new ArrayList<>();
for (String S : C) {
Temp.add(new BigDecimal(S));
}
int Location = Temp.indexOf(BigDecimal.valueOf(Deg));
List<BigDecimal> Coeffs = new ArrayList<>();
for (int Counter = Location - 1; Counter < Temp.size(); Counter += 2) {
Coeffs.add(Temp.get(Counter));
}
return Coeffs;
}

public int getDegree() {
int Degree = 0;
for (int Counter = 0; Counter < C.length; Counter += 2) {
if ((new Double(C[Counter])) != 0) {
Degree = new Integer(C[Counter + 1]);
this.Deg = Degree;
break;
}
}
return Degree;
}

public BigDecimal Evaluate(List<BigDecimal> Coefficients, double EvalPoint) {
BigDecimal Output = BigDecimal.ZERO;
for (int Index = 0; Index < Coefficients.size(); Index++) {
Output = Output.add(Coefficients.get(Index).multiply(BigDecimal.valueOf(EvalPoint).pow(Deg--)));
}
return Output;
}
}

和主类:

package Priest;

import java.math.RoundingMode;

public class MainClass {

public static void main(String[] args) {
long Start = System.nanoTime();
String Str = "3.1415x^5-12.6x^4+6x^3+12*x^2-6*x^1-0";
Equation E = new Equation(Str);
System.out.println("Equation is: " + E.SourceEquation());
System.out.println("Coefficients :" + E.CaptureCoeff());
System.out.println("Polynomial Degree: " + E.getDegree());
double Target = 47.784;
System.out.println("Equation @ (X:" + Target + ")= " + E.Evaluate(E.CaptureCoeff(), Target).setScale(15, RoundingMode.HALF_UP));
System.out.println("Elapsed Time: " + String.format("%.20G", (System.nanoTime() - Start) / 1.0e6) + " ms.");
}
}

输出:

run:
Equation is: 3.1415*(X)^5 - 12.6*(X)^4 + 6*(X)^3 + 12*(X)^2 - 6*(X)^1 - 0
Coefficients :[3.1415, -12.6, 6, 12, -6, 0]
Polynomial Degree: 5
Equation @ (X:47.784)= 717609084.382589022327914
Elapsed Time: 32.306242000000000000 ms.
BUILD SUCCESSFUL (total time: 0 seconds)

最佳答案

让我们使用以下等式String Str2 = "3.1415x^5+6x^2+12*x-5";这是我在您的代码中添加的代码,以便预处理该方程并使其与您的实际逻辑兼容,以便它在不对您的代码进行任何重大更改的情况下处理它。

为了完全准确,我必须在方程类中更改以下内容:

public List<BigDecimal> CaptureCoeff() {
getDegree();
List<BigDecimal> Temp = new ArrayList<BigDecimal>();
for (String S : C) {
if (! "".equals(S.trim())) {
Temp.add(new BigDecimal(S));
}
}

所以我添加了控件来检查这些 S 字符串是否没有修剪 - 空。

这是我的预处理代码。

我添加了一个名为 powerSplit 的方法,该方法允许根据“^”字符拆分方程。

然后我创建了另一个名为 generateNullCoeffPolynomeWithDegree 的方法,它生成 0*X^k 形式的单体。以及一个类似的,它在较大功率和较小功率之间生成所有相似的中间单体

示例: 字符串 str3 =generateAllNullCoeffPolynomesWithDegreeExclusiveBetween(5, 2); System.out.println("所有的poly = "+ str3);

将生成:all poly = 0*x^4+0*x^3

然后我创建了一个 buildPreProcessedPolynome,它采用初始方程并对其进行预处理,以生成其中包含空单体的方程。然后我把它交给你的方程程序,它可以很好地处理它!!!

这里是代码和调用示例,全部在 MainClass 中完成

import java.math.RoundingMode;

导入java.util.ArrayList;导入java.util.List;

公共(public)类主类{

private static List<String> workList = new ArrayList<String>();

public static void powerSplitt(String equationText) {

char[] charsList = equationText.toCharArray();

boolean foundTargetChar = false;
int index = 0;

for (int i = 0; i < charsList.length; i++) {
index = i;
if (charsList[i] == '^') {

foundTargetChar = true;
break;
}
}
if (foundTargetChar) {

workList.add(equationText.substring(0, index));
if (index +1 < equationText.length()) {
powerSplitt(equationText.substring(index+1));
} else {
workList.add(equationText);
return;
}
} else {
workList.add(equationText);
}

}


public static String generateNullCoeffPolynomeWithDegree(int degree) {
return "0*x^" + degree;
}

public static String generateAllNullCoeffPolynomesWithDegreeExclusiveBetween(int startDegree, int endDegree) {
if (startDegree-endDegree <= 1) {
return "";
}

int index = 0;
StringBuilder builder = new StringBuilder();
for (int i = startDegree -1; i > endDegree; i--) {
if (index > 0) {
builder.append("+");
}
builder.append(generateNullCoeffPolynomeWithDegree(i));
index++;
}

return builder.toString();
}

public static String buildPreProcessedPolynome(String initialEquationText) {
workList.clear();
powerSplitt(initialEquationText);


StringBuilder resultBuilder = new StringBuilder();
assert workList.size() >= 3;
resultBuilder.append(workList.get(0));

for (int i = 1; i <= workList.size()-2; i++) {
int actualPower = Integer.parseInt( workList.get(i).substring(0,1));

int nextFoundPower = Integer.parseInt( workList.get(i+1).substring(0,1));
System.out.print("actual power = " + actualPower + " and next power = " + nextFoundPower);
System.out.println();

String additionalPolyParts = generateAllNullCoeffPolynomesWithDegreeExclusiveBetween(actualPower, nextFoundPower);
resultBuilder.append("^" + actualPower);
resultBuilder.append("+");
resultBuilder.append(additionalPolyParts);
resultBuilder.append(workList.get(i).substring(1));

}

resultBuilder.append("^" + workList.get(workList.size()-1));
return resultBuilder.toString();
}

public static void main(String[] args) {
workList.clear();

String Str2 = "3.1415x^5+6x^2+12*x-5";

powerSplitt(Str2);

for (String part: workList) {
System.out.println("PART:" + part);
}

System.out.println("-----------------");

long Start = System.nanoTime();

String str3 = generateAllNullCoeffPolynomesWithDegreeExclusiveBetween(5, 2);
System.out.println("all poly = " + str3);

String preprocessed = buildPreProcessedPolynome(Str2);
System.out.println("preprocessed = " + preprocessed);

System.out.println();

Equation E = new Equation(preprocessed);
System.out.println("Equation is: " + E.SourceEquation());
System.out.println("Coefficients :" + E.CaptureCoeff());
System.out.println("Polynomial Degree: " + E.getDegree());
double Target = 47.784;
System.out.println("Equation @ (X:" + Target + ")= " + E.Evaluate(E.CaptureCoeff(), Target).setScale(15, RoundingMode.HALF_UP));
System.out.println("Elapsed Time: " + String.format("%.20G", (System.nanoTime() - Start) / 1.0e6) + " ms.");
}

}

这是生成的结果(我添加了一些 System.out.println 来检查方法调用的结果。我刚刚注意到我必须将最后一个常量视为 K*X^0 类型的单体,但我会将其留给您):

PART:3.1415x PART:5+6x

PART:2+12*x-5

all poly = 0*x^4+0*x^3 actual power = 5 and next power = 2 preprocessed = 3.1415x^5+0*x^4+0*x^3+6x^2+12*x-5

Equation is: 3.1415*(X)^5 + 0*(X)^4 + 0*(X)^3 + 6*(X)^2 + 12*(X) - 5 Coefficients :[3.1415, 0, 0, 6, 12] Polynomial Degree: 5 Equation @ (X:47.784)= 782631805.485054892561514 Elapsed Time: 18,441978000000000000 ms.

关于java - 从输入字符串中提取多项式系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579410/

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