Adam Sh
Adam Sh

Reputation: 8577

How to represent a mathematical series

I want to build a function, that one of it arguments is series {x[i], i = 0, 1, ..., inf}.

For example: x_m = 1/(1+x_(m-1)), m=1,2....inf

With this series, I want to check for every x[i], if it satisfy some condition that I will define (I don't know for which n it will satisfied).

for example:

If x_0 < 5, then I will return the value of x_0.

If not - I will check it for x_1. If x_1<5, I will return the value of x_1.

and so on..

NOTE: x_1 need to know the value of x_0 for it calculation.

There is a easy way to represnt this series in Java?

Upvotes: 1

Views: 844

Answers (3)

Ray
Ray

Reputation: 4879

Below is a way you might represent what you're trying to do. The sum function I provided, however, will surely not do.

For one, your sequence is a recurrence, and is implemented here recursively. You will want to memoize that. I would suggest adding something to do that in SeriesUtil.

More importantly, you're obviously not going to be able to have $\infinity$ as an upper bound. As you surely know, this is a nontrivial problem. That said, you could have various abstract classes implementing Series, such as GeometricSeries, which have known solutions, and use an appropriate strategy given the series type.

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

Upvotes: 2

SourceVisor
SourceVisor

Reputation: 1996

Obviously, Ray & Paxinum have provided the guide you need. All you have to do is adjust it to suit your case.

But in case its for academic purposes and your lecturer needs to see your detailed script, you might consider this. Note that an ArrayList is used instead of an Array because the limits for the former can be redefined unlike the latter. You could copy and paste the following code and see how it works. Consider editing it to your specifications if need be. I provided comments too - they explain the codes as much as I could. Cheers!

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

}

Upvotes: 0

Per Alexandersson
Per Alexandersson

Reputation: 2533

This is a partial answer only:

As a mathematician, I'd say that you probably want to define an interface for a series, with the method double getElement(int m) and maybe some more. Then, you might want to have different classes with implementation depending on the series. You might have FormulaSeries or RecursiveSeries where the series is given by some recursive formula (as in your example). This class might store intermediate values internally, for further use (memoization).

Depending on what you are aiming for, you might implement partial series, where you only have a finite number of known values, which can then be stored in a list or array.

If we focus on the RecursiveSeries, you might want to store the computed values internally in a static Map if the formula is really hard to compute, and you will need elements multiple times. This should not be too hard to figure out.

Upvotes: 2

Related Questions