/// Compute the integral. /// @param totalCount number of Monte Carlo trials. /// @param upperLimit value larger than the maximum of the function over the integral range. /// @return integral value public double Result(int totalCount, double upperLimit) { int n = _a.Length; double[] x = new double[n]; int hits = 0; Random random = new Random(); for (int count = 0; count < totalCount; count++) { for (int i = 0; i < n; i++) { x[i] = _a[i] + random.NextDouble() * _range[i]; } if (random.NextDouble() * upperLimit <= _f.Value(x)) { hits++; } } double answer = upperLimit * (hits / (double)totalCount); for (int i = 0; i < n; i++) { answer *= _range[i]; } return(answer); }
/// @return double value of the function /// @param x double distance from the origin in unit of direction. public double Value(double x) { try { return(_f.Value(ArgumentAt(x).ToComponents())); } catch (DhbIllegalDimension) { return(double.NaN); } }
/// (used by the Simplex algorithm). /// @param v double[] public void ContractFrom(double[] v) { for (int i = 0; i < _position.Length; i++) { _position[i] += v[i]; _position[i] *= 0.5; } _value = _f.Value(_position); }
/// Constructor method. /// @param v double[] /// @param f DhbInterfaces.IManyVariableFunction public OptimizingVector(double[] v, IManyVariableFunction func) { _position = v; _f = func; _value = _f.Value(_position); }