2012-01-10 11 views
0

私は関数を構築したい、その引数の1つはシリーズ{x[i], i = 0, 1, ..., inf}です。例えば数学シリーズを表現する方法

:このシリーズではx_m = 1/(1+x_(m-1)), m=1,2....inf

、私はそれは私が(私がいるかわからないのnそれは満足します)を定義しますいくつかの条件を満たしている場合、[i]は、すべてのxについて確認したいです。例えば

x_0 < 5場合、私はX_0の値を返します。

もしそうでない場合は、x_1を確認します。 x_1<5の場合は、x_1の値を返します。

など..

注:計算にx_0の値を知る必要がx_1

Javaでこのシリーズを簡単にrepresntする方法はありますか?

+8

あなたは抽象的な概念の話をしています。あなたがシリーズで何をしたいか教えてください。そして、あなたがその表現方法を教えてください。 –

+0

@ JasonS:質問を編集します。それは明確ですか? –

答えて

2

以下は、あなたがしようとしていることを表現する方法です。しかし、私が提供した和関数は、確かにしません。

あなたのシーケンスは繰り返しであり、ここで再帰的に実装されます。あなたはそれをメモしておきたいでしょう。私はSeriesUtilにそれを行う何かを加えることを提案します。

さらに重要なことに、あなたは明らかに$ \ infinity $を上限に持つことはできません。ご存知の通り、これは問題ではありません。つまり、知られている解決策を持つのようなSeriesを実装する様々な抽象クラスを持つことができ、シリーズ型の適切な戦略を使用できます。

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; 
    } 
} 
2

これは部分的にしか答えです:数学者として

、私はこの方法double getElement(int m)と多分もう少しで 、あなたはおそらくシリーズ用のインターフェイスを定義したいことを言うと思います。 次に、シリーズに応じて異なるクラスを実装することができます。 FormulaSeriesまたはRecursiveSeriesがあります。ここで、系列はいくつかの再帰式によって与えられます(例のように)。 このクラスは中間値を後で使用するために内部的に保存することができます(メモ化)。

目的に応じて、有限個の既知の値しか持たない部分系列、 を実装することができます。この値はリストまたは配列に格納できます。

RecursiveSeriesに焦点を当てた場合、計算式が実際に計算するのが難しい場合は、計算された値を内部で静的Mapに保存し、複数の要素が必要になることがあります。これは理解するのが難しいことではありません。

+0

私たちは同じ行に沿って考えていたように見えます。 – Ray

0

明らかに、Ray & Paxinumが必要とするガイドを提供しています。あなたのために調整するだけです。

しかし、学術目的で、講師が詳細なスクリプトを参照する必要がある場合は、 と考えてください。後者とは異なり、 の制限が再定義できるため、Arrayの代わりにArrayListが使用されることに注意してください。 次のコードをコピー&ペーストして、どのように動作するかを確認できます。必要に応じて、お客様の仕様を に編集することを検討してください。私もコメントを提供しました - 彼らはできるだけコードを説明します。乾杯!

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(); 
} 

}

関連する問題