Skip to content

Commit 64f8845

Browse files
author
Alen John
committed
Move Method -Set 2
1 parent ea7248c commit 64f8845

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/main/java/net/finmath/montecarlo/GammaProcess.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class GammaProcess implements IndependentIncrements, Serializable {
5555

5656
private final RandomVariableFactory randomVariableFactory = new RandomVariableFromArrayFactory();
5757

58-
private transient RandomVariable[][] gammaIncrements;
58+
private transient RandomVariable[][] gammaIncrements;
5959

6060
/**
6161
* Construct a Gamma process with a given shape parameter.
@@ -122,12 +122,12 @@ public RandomVariable getIncrement(final int timeIndex, final int factor) {
122122
doGenerateGammaIncrements();
123123
}
124124
}
125-
126125
/*
127126
* For performance reasons we return directly the stored data (no defensive copy).
128127
* We return an immutable object to ensure that the receiver does not alter the data.
129128
*/
130-
return gammaIncrements[timeIndex][factor];
129+
RandomVariableLazyEvaluation randomVariableLazyEvaluation = new RandomVariableLazyEvaluation(null);
130+
return randomVariableLazyEvaluation.getIncrement(timeIndex, factor);
131131
}
132132

133133
/**

src/main/java/net/finmath/montecarlo/RandomVariableLazyEvaluation.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public class RandomVariableLazyEvaluation implements RandomVariable {
3636
/**
3737
*
3838
*/
39+
40+
3941
private static final long serialVersionUID = 8413020544732461630L;
42+
private transient RandomVariable[][] gammaIncrements;
4043

4144
private final double time; // Time (filtration)
4245

@@ -59,6 +62,7 @@ public RandomVariableLazyEvaluation(final RandomVariable value) {
5962
realizations = value.isDeterministic() ? null : value::get;
6063
size = value.size();
6164
valueIfNonStochastic = value.isDeterministic() ? value.get(0) : Double.NaN;
65+
gammaIncrements = null; // Lazy initialization
6266
}
6367

6468
/**
@@ -68,8 +72,9 @@ public RandomVariableLazyEvaluation(final RandomVariable value) {
6872
*/
6973
public RandomVariableLazyEvaluation(final double value) {
7074
this(0.0, value);
71-
}
75+
gammaIncrements = null; // Lazy initialization
7276

77+
}
7378
/**
7479
* Create a random variable by applying a function to a given other implementation of <code>RandomVariable</code>.
7580
*
@@ -87,6 +92,8 @@ public double applyAsDouble(final int i) {
8792
};
8893
size = value.size();
8994
valueIfNonStochastic = value.isDeterministic() ? function.applyAsDouble(value.get(0)) : Double.NaN;
95+
gammaIncrements = null; // Lazy initialization
96+
9097
}
9198

9299
/**
@@ -101,6 +108,17 @@ public RandomVariableLazyEvaluation(final double time, final double value) {
101108
realizations = null;
102109
size = 1;
103110
valueIfNonStochastic = value;
111+
gammaIncrements = null; // Lazy initialization
112+
113+
}
114+
115+
public RandomVariable getIncrement(final int timeIndex, final int factor) {
116+
// Thread safe lazy initialization
117+
/*
118+
* For performance reasons we return directly the stored data (no defensive copy).
119+
* We return an immutable object to ensure that the receiver does not alter the data.
120+
*/
121+
return gammaIncrements[timeIndex][factor];
104122
}
105123

106124
/**

0 commit comments

Comments
 (0)