如何在方法中编辑数组?
How to edit the array within a method?
所以这个方法叫做微分,它的目的是return一个由双精度数组组成的Poly对象,这个数组应该包含微分多项式的系数,例如如果提供一个poly对象包含带有 [2.0, 3.0, 2.0]
的数组,该方法将 return [4, 3, 0]
因为 2x^2 + 3x^1 + 2.0
的系数就是那些。
public static Poly polyObject;
public static String differentiate(Poly polyObject) {
double[] array = polyObject.getDoubleArray();
int counterVariable = array.length - 1;
for (int i = 0; i < array.length; i++) {
array[i] = array[i] * counterVariable;
counterVariable--;
}
}
不确定从这里要做什么才能更改数组的系数。
您可以 return 一个新的整数数组 int[]
例如:
public static int[] differentiate(Poly polyObject) {
double[] array = polyObject.getDoubleArray();
int counterVariable = array.length - 1;
int[] coeffArray = new int[array.length];
for(int i = 0; i < array.length; i++) {
coeffArray[i] = (int) array[i] * counterVariable;
counterVariable--;
}
return coeffArray;
}
或更改同一个数组,但您将具有 double
类型值而不是 int
。这正是您的代码,但不是 return 类型 String
而是将其更改为 void
.
public static void differentiate(Poly polyObject) {
double[] array = polyObject.getDoubleArray();
int counterVariable = array.length - 1;
for(int i=0; i < array.length; i++) {
array[i] = array[i] * counterVariable;
counterVariable--;
}
}
应用Horner's method即可得到结果。这也显示了结果系数。
- 原方程=
y = 2x^3 + 6x^2 +4x + 3
- 推导后=
y' = 6x^2 + 12x + 4
- 给定
x = 3
,结果是54 + 36 + 4 = 94
使用Horner's Method
解决让result = 0
result = result * x + 6 = 6 ( exp = 2)
result = result * x + 12 = 30 (exp = 1)
result = result * x + 4 = 94 (exp = 0) - done!
double[] coefs = { 2., 6., 4., 3 };
int exp = coefs.length-1;
double result = 0;
int i = 0;
int x = 3; // the value to be solve
while(i < exp) {
coefs[i] *= (exp-i);
result = result * x + coefs[i++];
}
// y = 2x^3 + 6x^2 +4x + 3
// After derivation. coefs = 6, 12, 4
// y' = 6x^2 + 12x + 4 = 54 + 36 + 4
coefs = Arrays.copyOf(coefs,coefs.length-1);
System.out.println(Arrays.toString(coefs));
System.out.println("result = " + result);
版画
[6.0, 12.0, 4.0]
result = 94.0
所以这个方法叫做微分,它的目的是return一个由双精度数组组成的Poly对象,这个数组应该包含微分多项式的系数,例如如果提供一个poly对象包含带有 [2.0, 3.0, 2.0]
的数组,该方法将 return [4, 3, 0]
因为 2x^2 + 3x^1 + 2.0
的系数就是那些。
public static Poly polyObject;
public static String differentiate(Poly polyObject) {
double[] array = polyObject.getDoubleArray();
int counterVariable = array.length - 1;
for (int i = 0; i < array.length; i++) {
array[i] = array[i] * counterVariable;
counterVariable--;
}
}
不确定从这里要做什么才能更改数组的系数。
您可以 return 一个新的整数数组 int[]
例如:
public static int[] differentiate(Poly polyObject) {
double[] array = polyObject.getDoubleArray();
int counterVariable = array.length - 1;
int[] coeffArray = new int[array.length];
for(int i = 0; i < array.length; i++) {
coeffArray[i] = (int) array[i] * counterVariable;
counterVariable--;
}
return coeffArray;
}
或更改同一个数组,但您将具有 double
类型值而不是 int
。这正是您的代码,但不是 return 类型 String
而是将其更改为 void
.
public static void differentiate(Poly polyObject) {
double[] array = polyObject.getDoubleArray();
int counterVariable = array.length - 1;
for(int i=0; i < array.length; i++) {
array[i] = array[i] * counterVariable;
counterVariable--;
}
}
应用Horner's method即可得到结果。这也显示了结果系数。
- 原方程=
y = 2x^3 + 6x^2 +4x + 3
- 推导后=
y' = 6x^2 + 12x + 4
- 给定
x = 3
,结果是54 + 36 + 4 = 94
使用Horner's Method
解决让result = 0
result = result * x + 6 = 6 ( exp = 2)
result = result * x + 12 = 30 (exp = 1)
result = result * x + 4 = 94 (exp = 0) - done!
double[] coefs = { 2., 6., 4., 3 };
int exp = coefs.length-1;
double result = 0;
int i = 0;
int x = 3; // the value to be solve
while(i < exp) {
coefs[i] *= (exp-i);
result = result * x + coefs[i++];
}
// y = 2x^3 + 6x^2 +4x + 3
// After derivation. coefs = 6, 12, 4
// y' = 6x^2 + 12x + 4 = 54 + 36 + 4
coefs = Arrays.copyOf(coefs,coefs.length-1);
System.out.println(Arrays.toString(coefs));
System.out.println("result = " + result);
版画
[6.0, 12.0, 4.0]
result = 94.0