/* (non-Javadoc)
@see net.finmath.stochastic.RandomVariable#accrue(net.finmath.stochastic.RandomVariable, double)
186308
@Override
	public RandomVariable accrue(RandomVariable rate, double periodLength) {
		return new RandomVariableDifferentiableAADStochasticNonOptimized(
				getValues().accrue(rate, periodLength),
				Arrays.asList(this, rate, new RandomVariableFromDoubleArray(periodLength)),
				OperatorType.ACCRUE);
	}
315370
public static boolean isReadLocalsFromTuple(final RuleBuildContext context,
                                                final AccumulateDescr accumDescr,
                                                final RuleConditionElement source) {
        if (accumDescr.isMultiPattern()) {
            return true;
        }

        PatternDescr inputPattern = accumDescr.getInputPattern();
        if (inputPattern == null) {
            context.addError(new DescrBuildError(context.getParentDescr(),
                                                 accumDescr,
                                                 null,
                                                 "Invalid accumulate pattern in rule '" + context.getRule().getName() + "'."));
            return true;
        }

        if (source instanceof Pattern) {
            if ( ((Pattern) source).hasXPath() ) {
                return true;
            }
            if ( ((Pattern) source).getSource() instanceof EntryPointId ) {
                return false;
            }
        }

        if ( inputPattern.getSource() != null &&
                !( inputPattern.getSource() instanceof WindowReferenceDescr ) &&
                !( inputPattern.getSource() instanceof EntryPointDescr ) ) {
            return true;
        }

        return source instanceof QueryElement ||
               ( source.getNestedElements().size() == 1 && source.getNestedElements().get( 0 ) instanceof QueryElement );
    }
276889
@Override
    public void accept(double value)
    {
        writeLock.lock();
        try
        {
            eliminate();
            int count = count();
            if (count >= size)
            {
                grow();
            }
            assign(endMod(), value);
            endIncr();
        }
        finally
        {
            writeLock.unlock();
        }
    }
689984
@Override
	public RandomVariable add(RandomVariable randomVariable) {
		if(randomVariable.getTypePriority() > this.getTypePriority()) {
			// Check type priority
			return randomVariable.add(this);
		}

		return new RandomVariableDifferentiableAAD(
				getValues().add(randomVariable.getValues()),
				Arrays.asList(this.getOperatorTreeNode(), OperatorTreeNode.of(randomVariable)),
				Arrays.asList(null /* this.getValues() */, null /* randomVariable.getValues() */),		// For ADD we do not need all arguments to evaluate the differential
				null,
				OperatorType.ADD,
				getFactory(),
				typePriorityDefault);
	}
1165767
public SDVariable pow(String varName, double scalar) {
        SDVariable ret = f().pow(this, scalar);
        return sameDiff.updateVariableNameAndReference(ret, varName);
    }
1282239
static double usingXArrAndYStride(
      final double[] xArr, final double yStride, final double x) {
    final int xArrLen = xArr.length;
    final int xArrLenM1 = xArrLen - 1;

    final int offset;
    assert ((xArrLen >= 4) && (x >= xArr[0]) && (x <= xArr[xArrLenM1]));

    if (x ==  xArr[xArrLenM1]) { /* corner case */
      return (yStride * (xArrLenM1));
    }

    offset = findStraddle(xArr, x); //uses recursion
    final int xArrLenM2 = xArrLen - 2;
    assert ((offset >= 0) && (offset <= (xArrLenM2)));

    if (offset == 0) { /* corner case */
      return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 0), x));
    }
    else if (offset == xArrLenM2) { /* corner case */
      return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 2), x));
    }
    /* main case */
    return (interpolateUsingXArrAndYStride(xArr, yStride, (offset - 1), x));
  }
114419
@Override
	public RandomVariable pow(double exponent) {
		return apply(new DoubleUnaryOperator() {
			@Override
			public double applyAsDouble(double x) {
				return FastMath.pow(x, exponent);
			}
		});
	}
312768
@Override
	public RandomVariable getValue(double evaluationTime, MonteCarloSimulationModel model) throws CalculationException {
		return getValue(evaluationTime, (LIBORMarketModelFromCovarianceModel)((LIBORModelMonteCarloSimulationModel) model).getModel());
	}
685873
private void callFindAssociation(Java.METHOD method, String result, Variable variable, Member member) {

		String setName = variable.getName() + "$" + member.getName();
		method.addS("Number " + result + " = findAssociation("
				+ variable.getName() + ", "
				+ variable.getFrom() + ", "
				+ variable.getTo() + ", "
				+ variable.getStep() + ", "
				+ setName + ")");
	}
929687
@Override
	public RandomVariable mult(RandomVariable randomVariable) {
		return apply(OperatorType.MULT, new RandomVariable[]{this, randomVariable});
	}
341825
public void add(double element) {
	// overridden for performance only.  
	if (size == elements.length) ensureCapacity(size + 1); 
	elements[size++] = element;
}
596434
@Override
    public double compute(double a, double b) {
        if (Op.isEq(a + b, 0.0)) return 0.0;
        return (a * b) / (a + b - a * b);
    }
528670
public static void shuffle(double[] doubleArray) {
		int swapPlace = -1;

		for(int i = 0; i < doubleArray.length; i++) {
            swapPlace = (int) (Math.random() * (doubleArray.length - 1 ));
            TrivialSwap.swap(doubleArray, i, swapPlace);
		}
	}
347367
@Override
	public RandomVariable div(double value) {
		return apply(OperatorType.DIV, new RandomVariable[]{this, constructNewAADRandomVariable(value)});
	}
462579
public void add(double x, double y) {
        if (x < minX) minX = x;
        if (y < minY) minY = y;
        if (x > maxX) maxX = x;
        if (y > maxY) maxY = y;
    }
963169
@Override
	public RandomVariable add(RandomVariable randomVariable) {
		if(randomVariable.getTypePriority() > this.getTypePriority()) {
			// Check type priority
			return randomVariable.add(this);
		}

		// Set time of this random variable to maximum of time with respect to which measurability is known.
		double newTime = Math.max(time, randomVariable.getFiltrationTime());

		if(isDeterministic() && randomVariable.isDeterministic()) {
			double newValueIfNonStochastic = valueIfNonStochastic + randomVariable.get(0);
			return new RandomVariableFromDoubleArray(newTime, newValueIfNonStochastic);
		}
		else if(isDeterministic()) {
			double[] newRealizations = new double[Math.max(size(), randomVariable.size())];
			for(int i=0; i<newRealizations.length; i++) {
				newRealizations[i]		 = valueIfNonStochastic + randomVariable.get(i);
			}
			return new RandomVariableFromDoubleArray(newTime, newRealizations);
		} else {
			double[] newRealizations = new double[Math.max(size(), randomVariable.size())];
			for(int i=0; i<newRealizations.length; i++) {
				newRealizations[i]		 = realizations[i] + randomVariable.get(i);
			}
			return new RandomVariableFromDoubleArray(newTime, newRealizations);
		}
	}
692318
public void accumulate(AverageData data,
                           Object value) {
        if (value != null) {
            data.count++;
            data.total = data.total.add( (BigDecimal) value );
        }
    }