Java 如何表示数学级数
我想构建一个函数,其中一个参数是seriesJava 如何表示数学级数,java,Java,我想构建一个函数,其中一个参数是series{x[I],I=0,1,…,inf} 例如:xm=1/(1+xm-1)),m=1,2…inf 对于这个系列,我想检查每个x[I],如果它满足我将定义的某些条件(我不知道它将满足哪个n) 例如: 如果x_0
{x[I],I=0,1,…,inf}
例如:xm=1/(1+xm-1)),m=1,2…inf
对于这个系列,我想检查每个x[I],如果它满足我将定义的某些条件(我不知道它将满足哪个n)
例如:
如果x_0<5
,则我将返回x_0的值
如果没有-我将检查它的
x\u 1
。如果x_1这只是部分答案:
作为一个数学家,我想说你可能想为一个系列定义一个接口,
使用方法double-getElement(int-m)
或者更多。
然后,您可能希望根据系列使用不同的类和实现。
您可能有FormulaSeries
或RecursiveSeries
,其中序列由某个递归公式给出(如示例中所示)。
此类可以在内部存储中间值,以供进一步使用(记忆)
根据您的目标,您可以实施部分系列,
其中只有有限数量的已知值,这些值可以存储在列表或数组中
如果我们关注的是递归序列
,那么如果公式真的很难计算,并且需要多次元素,那么您可能希望将计算出的值存储在静态映射中。这应该不难理解。这只是一个部分答案:
作为一个数学家,我想说你可能想为一个系列定义一个接口,
使用方法double-getElement(int-m)
或者更多。
然后,您可能希望根据系列使用不同的类和实现。
您可能有FormulaSeries
或RecursiveSeries
,其中序列由某个递归公式给出(如示例中所示)。
此类可以在内部存储中间值,以供进一步使用(记忆)
根据您的目标,您可以实施部分系列,
其中只有有限数量的已知值,这些值可以存储在列表或数组中
如果我们关注的是递归序列,那么如果公式真的很难计算,并且需要多次元素,那么您可能希望将计算出的值存储在静态映射中。这应该不太难理解。下面是一种方式,您可以表示您正在尝试做什么。然而,我提供的求和函数肯定不行 首先,您的序列是递归的,并且在这里递归地实现。你会想记住这一点。我建议在
SeriesUtil
中添加这样的内容
更重要的是,您显然无法将$\infinity$作为上限。你肯定知道,这是一个非常重要的问题。也就是说,您可以使用各种抽象类来实现系列
,例如几何系列
,它们具有已知的解决方案,并在给定系列类型的情况下使用适当的策略
interface Series {
double x(int m);
}
class ExampleSeries implements Series {
public double x(int m) {
if (m == 0) {
return 0;
}
return 1 / (1 + x(m - 1));
}
}
class SeriesUtil {
public static double sum(Series series, int lowerBound, int upperBound) {
int sum = 0;
for (int i = lowerBound; i <= upperBound) {
sum += x(i);
}
return sum;
}
}
接口系列{
双x(整数m);
}
类ExampleSeries实现系列{
公共双x(整数m){
如果(m==0){
返回0;
}
收益率1/(1+x(m-1));
}
}
类SeriesUtil{
公共静态双和(系列,整数下限,整数上限){
整数和=0;
下面的for(int i=lowerBound;i是一种表示您尝试执行的操作的方式。但是,我提供的求和函数肯定不会执行
首先,您的序列是一个递归序列,并且在这里递归地实现。您需要记住这一点。我建议在SeriesUtil
中添加这样的内容
更重要的是,您显然不能将$\infinity$作为上限。您当然知道,这是一个非常重要的问题。也就是说,您可以让各种抽象类实现系列
,例如几何系列
,它们具有已知的解决方案,并在给定系列t的情况下使用适当的策略类型
interface Series {
double x(int m);
}
class ExampleSeries implements Series {
public double x(int m) {
if (m == 0) {
return 0;
}
return 1 / (1 + x(m - 1));
}
}
class SeriesUtil {
public static double sum(Series series, int lowerBound, int upperBound) {
int sum = 0;
for (int i = lowerBound; i <= upperBound) {
sum += x(i);
}
return sum;
}
}
接口系列{
双x(整数m);
}
类ExampleSeries实现系列{
公共双x(整数m){
如果(m==0){
返回0;
}
收益率1/(1+x(m-1));
}
}
类SeriesUtil{
公共静态双和(系列,整数下限,整数上限){
整数和=0;
对于(inti=lowerBound;i显然,Ray&Paxinum提供了您需要的指南。您所要做的就是调整它以适合您的情况
但如果是为了学术目的,你的讲师需要看你的详细剧本,
您可能会考虑这一点。注意,使用数组来代替数组,因为
与后者不同,前者的限制可以重新定义。
您可以复制并粘贴下面的代码并查看它是如何工作的。
如果需要,请提供您的规格。我也提供了意见-他们尽可能多地解释代码。干杯
import java.util.ArrayList;
import java.util.Scanner;
public class SeriesTest
{
// This internal class "Index", will represent an index Object that holds a
// value and returns the value when needed
class Index
{
private double idxElement;
private void setIdxElement(double value)
{
idxElement = value;
}
private double getIdxElement()
{
return idxElement;
}
}
// Create an Object index of type Index (i.e the class defined above)
Index index;
// Create an ArrayList, x_ , that will reference the index Objects.
ArrayList<Index> x_ = new ArrayList<Index>();
public void calculateResult()
{
System.out.println("How many elements do you want the series to have?");
Scanner input = new Scanner(System.in);
int NoOfelements = input.nextInt();
int value = 0;
// create a new instance of type Index
index = new Index();
// set the element held by this index as 0(zero)
// You can set this to any value depending on your Series' requirements
index.setIdxElement(value);
/*add the index object to the ArrayList
*This represents your x_m
*Note - This references the index Object that holds the value = 0 )*/
x_.add(index);
/* To do the same thing for the rest of the elements using
* your specified equation ---> x_m = 1/[ 1 + x_(m-1) ]*/
for (int m = 1; m < NoOfelements; m++ )
{
index = new Index();
// sets the value of the element referenced by the index object
// This represents your x_m = 1/[ 1 + x_(m-1) ]
index.setIdxElement( 1 / ( 1 + x_.get(m - 1).getIdxElement() ) );
// adds the index object to the ArrayList
x_.add(index);
}
}
public void displayResults()
{
// displays the series as referenced by the the ArrayList
// Note - ArrayList indexes start at 0(zero)
System.out.println("The series is:");
for ( int n = 0; n < x_.size(); n++ )
{
System.out.printf( "%.2f, ", x_.get(n).getIdxElement() );
}
}
public static void main(String[] args)
{
SeriesTest series = new SeriesTest();
series.calculateResult();
series.displayResults();
}
import java.util.ArrayList;
导入java.util.Scanner;
公共类序列测试
{
//这个内部类“Index”将表示一个包含
//值,并在需要时返回值
类索引
{
私人双重身份;
私有void setIdxElement(双值)
{
idxElement=值;
}
私有双getIdxElement()
{
返回idxElement;
}
}
//创建索引类型的对象索引(即上面定义的类)
指数;
//创建一个ArrayList x_3;,它将引用索引对象。
ArrayList x_u2;=新的ArrayList();
public void calculateResult()
{
System.out.println(“您希望该系列包含多少元素?”);
扫描仪输入=新扫描仪(System.in);
int NoOfelements=input.nextInt();
int值=0;
//创建一个新的