Created
April 21, 2013 06:09
-
-
Save aishraj/5428658 to your computer and use it in GitHub Desktop.
A project stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package trapeziumIntegral; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
import mathematics.IntervalInterface; | |
import mathematics.MathFunctionInterface; | |
/* | |
* @author AishRaj | |
* | |
*/ | |
public class TrapeziumIntegration implements MCAreaIntegralMultiInterface<Float> { | |
private MathFunctionInterface<Float> function; | |
private final int MAX_ITEM_SIZE = 1024; | |
private int bufferSize = MAX_ITEM_SIZE; | |
private ArrayList<IntervalInterface<Float>> intervals = new ArrayList<IntervalInterface<Float>>(bufferSize); | |
@Override | |
public int getBufferSize() { | |
return this.bufferSize; | |
} | |
@Override | |
public Float getIntegral(IntervalInterface<Float> interval, int resolution) { | |
float offset = interval.getEnd() - interval.getBegin(); | |
int n = resolution; | |
float h = offset / n; | |
float result = 0; | |
float start = interval.getBegin(); | |
result += function.getValue(start) / 2; | |
for (int i = 1; i < n; i++) { | |
result += function.getValue(start + h * i); | |
} | |
result += function.getValue(start + n * h) / 2; | |
return result * h; | |
} | |
@Override | |
public int getBufferCount() { | |
return this.intervals.size(); | |
} | |
@Override | |
public int getMaxItemSize() { | |
return MAX_ITEM_SIZE; | |
} | |
@Override | |
public int getItemCount() { | |
return this.intervals.size(); | |
} | |
@Override | |
public void setFunction(MathFunctionInterface<Float> function) { | |
this.function = function; | |
} | |
@Override | |
public boolean put(IntervalInterface<Float> v) { | |
if (this.intervals.size() < bufferSize) { | |
this.intervals.add(v); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
@Override | |
public int getCurrentMaxItemSize() { | |
return this.bufferSize; | |
} | |
@Override | |
public int reset(int expectedItemSize) { | |
bufferSize = expectedItemSize < MAX_ITEM_SIZE ? expectedItemSize | |
: MAX_ITEM_SIZE; | |
this.intervals.clear(); | |
this.intervals.ensureCapacity(bufferSize); | |
return bufferSize; | |
} | |
@Override | |
public int reset() { | |
return reset(MAX_ITEM_SIZE); | |
} | |
@Override | |
public List<Float> getIntegrals(int resolution) { | |
List<Float> results = new LinkedList<Float>(); | |
for (IntervalInterface<Float> interval : intervals) | |
results.add(getIntegral(interval, resolution)); | |
return results; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment