/// <summary> /// Creates an instance with a sampled (parameterised) curve. /// </summary> /// <param name="samplePoints"> the points where we sample the curve </param> /// <param name="curve"> a parameterised curve </param> public ParameterizedCurveVectorFunction(double[] samplePoints, ParameterizedCurve curve) { ArgChecker.notEmpty(samplePoints, "samplePoints"); ArgChecker.notNull(curve, "curve"); _samplePoints = Arrays.copyOf(samplePoints, samplePoints.Length); _curve = curve; }
/// <summary> /// Solves the system Ax = y for the unknown vector x, where A is a tridiagonal matrix and y is a vector. /// This takes order n operations where n is the size of the system /// (number of linear equations), as opposed to order n^3 for the general problem. </summary> /// <param name="aM"> tridiagonal matrix </param> /// <param name="b"> known vector (must be same length as rows/columns of matrix) </param> /// <returns> vector (as an array of doubles) with same length as y </returns> public static double[] solvTriDag(TridiagonalMatrix aM, double[] b) { ArgChecker.notNull(aM, "null matrix"); ArgChecker.notNull(b, "null vector"); double[] d = aM.Diagonal; //b is modified, so get copy of diagonal int n = d.Length; ArgChecker.isTrue(n == b.Length, "vector y wrong length for matrix"); double[] y = Arrays.copyOf(b, n); double[] l = aM.LowerSubDiagonalData; double[] u = aM.UpperSubDiagonalData; double[] x = new double[n]; for (int i = 1; i < n; i++) { double m = l[i - 1] / d[i - 1]; d[i] = d[i] - m * u[i - 1]; y[i] = y[i] - m * y[i - 1]; } x[n - 1] = y[n - 1] / d[n - 1]; for (int i = n - 2; i >= 0; i--) { x[i] = (y[i] - u[i] * x[i + 1]) / d[i]; } return(x); }
/// <summary> /// Adds two matrices. This operation can only be performed if the matrices are of the same type and dimensions. </summary> /// <param name="m1"> The first matrix, not null </param> /// <param name="m2"> The second matrix, not null </param> /// <returns> The sum of the two matrices </returns> /// <exception cref="IllegalArgumentException"> If the matrices are not of the same type, if the matrices are not the same shape. </exception> public virtual Matrix add(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); if (m1 is DoubleArray) { if (m2 is DoubleArray) { DoubleArray array1 = (DoubleArray)m1; DoubleArray array2 = (DoubleArray)m2; return(array1.plus(array2)); } throw new System.ArgumentException("Tried to add a " + m1.GetType() + " and " + m2.GetType()); } else if (m1 is DoubleMatrix) { if (m2 is DoubleMatrix) { DoubleMatrix matrix1 = (DoubleMatrix)m1; DoubleMatrix matrix2 = (DoubleMatrix)m2; return(matrix1.plus(matrix2)); } throw new System.ArgumentException("Tried to add a " + m1.GetType() + " and " + m2.GetType()); } throw new System.NotSupportedException(); }
//------------------------------------------------------------------------- public virtual System.Func <DoubleArray, DoubleMatrix[]> differentiate(System.Func <DoubleArray, DoubleArray> function, System.Func <DoubleArray, bool> domain) { ArgChecker.notNull(function, "function"); System.Func <DoubleArray, DoubleMatrix> jacFunc = vectorFieldDiff.differentiate(function, domain); System.Func <DoubleArray, DoubleMatrix[]> hFunc = maxtrixFieldDiff.differentiate(jacFunc, domain); return(new FuncAnonymousInnerClass2(this, hFunc)); }
/// <summary> /// Compute $A^T A$, where A is a matrix. </summary> /// <param name="a"> The matrix </param> /// <returns> The result of $A^T A$ </returns> public virtual DoubleMatrix matrixTransposeMultiplyMatrix(DoubleMatrix a) { ArgChecker.notNull(a, "a"); int n = a.rowCount(); int m = a.columnCount(); //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: double[][] data = new double[m][m]; double[][] data = RectangularArrays.ReturnRectangularDoubleArray(m, m); for (int i = 0; i < m; i++) { double sum = 0d; for (int k = 0; k < n; k++) { sum += a.get(k, i) * a.get(k, i); } data[i][i] = sum; for (int j = i + 1; j < m; j++) { sum = 0d; for (int k = 0; k < n; k++) { sum += a.get(k, i) * a.get(k, j); } data[i][j] = sum; data[j][i] = sum; } } return(DoubleMatrix.ofUnsafe(data)); }
/// <summary> /// Creates an instance. /// <para> /// If the size of the domain is very small or very large, consider re-scaling first. /// If this value is too small, the result will most likely be dominated by noise. /// Use around 10<sup>-5</sup> times the domain size. /// /// </para> /// </summary> /// <param name="differenceType"> the differencing type to be used in calculating the gradient function </param> /// <param name="eps"> the step size used to approximate the derivative </param> public VectorFieldFirstOrderDifferentiator(FiniteDifferenceType differenceType, double eps) { ArgChecker.notNull(differenceType, "differenceType"); this.differenceType = differenceType; this.eps = eps; this.twoEps = 2 * eps; }
public override DoubleMatrix apply(TridiagonalMatrix x) { ArgChecker.notNull(x, "x"); double[] a = x.DiagonalData; double[] b = x.UpperSubDiagonalData; double[] c = x.LowerSubDiagonalData; int n = a.Length; int i, j, k; double[] theta = new double[n + 1]; double[] phi = new double[n]; theta[0] = 1.0; theta[1] = a[0]; for (i = 2; i <= n; i++) { theta[i] = a[i - 1] * theta[i - 1] - b[i - 2] * c[i - 2] * theta[i - 2]; } if (theta[n] == 0.0) { throw new MathException("Zero determinant. Cannot invert the matrix"); } phi[n - 1] = 1.0; phi[n - 2] = a[n - 1]; for (i = n - 3; i >= 0; i--) { phi[i] = a[i + 1] * phi[i + 1] - b[i + 1] * c[i + 1] * phi[i + 2]; } double product; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: double[][] res = new double[n][n]; double[][] res = RectangularArrays.ReturnRectangularDoubleArray(n, n); for (j = 0; j < n; j++) { for (i = 0; i <= j; i++) { product = 1.0; for (k = i; k < j; k++) { product *= b[k]; } res[i][j] = ((i + j) % 2 == 0 ? 1 : -1) * product * theta[i] * phi[j] / theta[n]; } for (i = j + 1; i < n; i++) { product = 1.0; for (k = j; k < i; k++) { product *= c[k]; } res[i][j] = ((i + j) % 2 == 0 ? 1 : -1) * product * theta[j] * phi[i] / theta[n]; } } return(DoubleMatrix.copyOf(res)); }
/// <summary> /// Finds the node sensitivity. /// </summary> /// <param name="pp"> the <seealso cref="PiecewisePolynomialResultsWithSensitivity"/> </param> /// <param name="xKey"> the key </param> /// <returns> Node sensitivity value at x=xKey </returns> public virtual DoubleArray nodeSensitivity(PiecewisePolynomialResultsWithSensitivity pp, double xKey) { ArgChecker.notNull(pp, "null pp"); ArgChecker.isFalse(double.IsNaN(xKey), "xKey containing NaN"); ArgChecker.isFalse(double.IsInfinity(xKey), "xKey containing Infinity"); if (pp.Dimensions > 1) { throw new System.NotSupportedException(); } DoubleArray knots = pp.Knots; int nKnots = knots.size(); int interval = FunctionUtils.getLowerBoundIndex(knots, xKey); if (interval == nKnots - 1) { interval--; // there is 1 less interval that knots } double s = xKey - knots.get(interval); DoubleMatrix a = pp.getCoefficientSensitivity(interval); int nCoefs = a.rowCount(); DoubleArray res = a.row(0); for (int i = 1; i < nCoefs; i++) { res = (DoubleArray)MA.scale(res, s); res = (DoubleArray)MA.add(res, a.row(i)); } return(res); }
public static int[] fromTensorIndex(int index, int[] dimensions) { ArgChecker.notNull(dimensions, "dimensions"); int dim = dimensions.Length; int[] res = new int[dim]; int product = 1; int[] products = new int[dim - 1]; for (int i = 0; i < dim - 1; i++) { product *= dimensions[i]; products[i] = product; } int a = index; for (int i = dim - 1; i > 0; i--) { res[i] = a / products[i - 1]; a -= res[i] * products[i - 1]; } res[0] = a; return(res); }
public virtual double?[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coeffs = function.Coefficients; int l = coeffs.Length - 1; //JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java: //ORIGINAL LINE: double[][] hessianDeref = new double[l][l]; double[][] hessianDeref = RectangularArrays.ReturnRectangularDoubleArray(l, l); for (int i = 0; i < l; i++) { hessianDeref[0][i] = -coeffs[l - i - 1] / coeffs[l]; for (int j = 1; j < l; j++) { hessianDeref[j][i] = 0; if (i != l - 1) { hessianDeref[i + 1][i] = 1; } } } RealMatrix hessian = new Array2DRowRealMatrix(hessianDeref); double[] d = (new EigenDecomposition(hessian)).RealEigenvalues; double?[] result = new double?[d.Length]; for (int i = 0; i < d.Length; i++) { result[i] = d[i]; } return(result); }
/// <summary> /// Creates an instance. /// </summary> /// <param name="qr"> The result of the QR decomposition, not null </param> public QRDecompositionCommonsResult(QRDecomposition qr) { ArgChecker.notNull(qr, "qr"); _q = CommonsMathWrapper.unwrap(qr.Q); _r = CommonsMathWrapper.unwrap(qr.R); _qTranspose = _q.transpose(); _solver = qr.Solver; }
/// <summary> /// Uses the parameters to create a function. /// </summary> /// <param name="x"> the value at which the function is to be evaluated, not null </param> /// <returns> a function that is always evaluated at <i>x</i> for different values of the parameters </returns> public virtual System.Func <T, U> asFunctionOfParameters(S x) { ArgChecker.notNull(x, "x"); return((T @params) => { return ParameterizedFunction.this.evaluate(x, @params); }); }
public override double?apply(double[] x) { ArgChecker.notNull(x, "x"); int n = x.Length; ArgChecker.isTrue(n >= 2, "Need at least two points to calculate the population variance"); return(_variance.apply(x) * (n - 1) / n); }
/// <summary> /// Returns the quotient of two matrices $C = \frac{A}{B} = AB^{-1}$, where /// $B^{-1}$ is the pseudo-inverse of $B$ i.e. $BB^{-1} = \mathbb{1}$. </summary> /// <param name="m1"> The numerator matrix, not null. This matrix must be a <seealso cref="DoubleMatrix"/>. </param> /// <param name="m2"> The denominator, not null. This matrix must be a <seealso cref="DoubleMatrix"/>. </param> /// <returns> The result </returns> public virtual Matrix divide(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); ArgChecker.isTrue(m1 is DoubleMatrix, "Can only divide a 2D matrix"); ArgChecker.isTrue(m2 is DoubleMatrix, "Can only perform division with a 2D matrix"); return(multiply(m1, getInverse(m2))); }
/// <summary> /// Interpolate. /// </summary> /// <param name="xValues"> the values </param> /// <param name="yValues"> the values </param> /// <param name="xMatrix"> the matrix </param> /// <returns> Values of the underlying cubic spline function at the values of x </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleMatrix interpolate(final double[] xValues, final double[] yValues, final double[][] xMatrix) public virtual DoubleMatrix interpolate(double[] xValues, double[] yValues, double[][] xMatrix) { ArgChecker.notNull(xMatrix, "xMatrix"); DoubleMatrix matrix = DoubleMatrix.copyOf(xMatrix); return(DoubleMatrix.ofArrayObjects(xMatrix.Length, xMatrix[0].Length, i => interpolate(xValues, yValues, matrix.rowArray(i)))); }
/// <summary> /// Creates an instance. /// </summary> /// <param name="base"> the base interpolator </param> /// <param name="extrapolatorLeft"> the extrapolator for x-values on the left </param> /// <param name="extrapolatorRight"> the extrapolator for x-values on the right </param> protected internal AbstractBoundCurveInterpolator(AbstractBoundCurveInterpolator @base, BoundCurveExtrapolator extrapolatorLeft, BoundCurveExtrapolator extrapolatorRight) { this.extrapolatorLeft = ArgChecker.notNull(extrapolatorLeft, "extrapolatorLeft"); this.extrapolatorRight = ArgChecker.notNull(extrapolatorRight, "extrapolatorRight"); this.firstXValue = @base.firstXValue; this.lastXValue = @base.lastXValue; this.lastYValue = @base.lastYValue; }
/// <summary> /// Uses the parameters to create a function. /// </summary> /// <param name="params"> the parameters for which the function is to be evaluated, not null </param> /// <returns> a function that can be evaluated at different <i>x</i> with the input parameters </returns> public virtual System.Func <S, U> asFunctionOfArguments(T @params) { ArgChecker.notNull(@params, "params"); return((S x) => { return ParameterizedFunction.this.evaluate(x, @params); }); }
protected internal virtual void checkInputs(System.Func <double, double> f, double xLower, double xUpper) { ArgChecker.notNull(f, "function"); if (DoubleMath.fuzzyEquals(xLower, xUpper, ZERO)) { throw new System.ArgumentException("Lower and upper values were not distinct"); } }
/// <summary> /// Tests that the inputs to the root-finder are not null, and that a root is bracketed by the bounding values. /// </summary> /// <param name="function"> The function, not null </param> /// <param name="x1"> The first bound, not null </param> /// <param name="x2"> The second bound, not null, must be greater than x1 </param> /// <exception cref="IllegalArgumentException"> if x1 and x2 do not bracket a root </exception> protected internal virtual void checkInputs(DoubleFunction1D function, double?x1, double?x2) { ArgChecker.notNull(function, "function"); ArgChecker.notNull(x1, "x1"); ArgChecker.notNull(x2, "x2"); ArgChecker.isTrue(x1 <= x2, "x1 must be less or equal to x2"); ArgChecker.isTrue(function.applyAsDouble(x1) * function.applyAsDouble(x2) <= 0, "x1 and x2 do not bracket a root"); }
/// <summary> /// Constructor. </summary> /// <param name="ch"> The result of the Cholesky decomposition. </param> public CholeskyDecompositionCommonsResult(CholeskyDecomposition ch) { ArgChecker.notNull(ch, "Cholesky decomposition"); _determinant = ch.Determinant; _l = CommonsMathWrapper.unwrap(ch.L); _lt = CommonsMathWrapper.unwrap(ch.LT); _solver = ch.Solver; }
/// <summary> /// Creates an instance. /// </summary> /// <param name="n"> The number of sample points to be used in the integration, not negative or zero </param> /// <param name="generator"> The generator of weights and abscissas </param> public GaussianQuadratureIntegrator1D(int n, QuadratureWeightAndAbscissaFunction generator) { ArgChecker.isTrue(n > 0, "number of intervals must be > 0"); ArgChecker.notNull(generator, "generating function"); this.size = n; this.generator = generator; this.quadrature = generator.generate(size); }
/// <summary> /// {@inheritDoc} /// </summary> public virtual double?integrate(System.Func <double, double> function, double?lower, double?upper) { ArgChecker.notNull(function, "function"); ArgChecker.notNull(lower, "lower"); ArgChecker.notNull(upper, "upper"); System.Func <double, double> integral = getIntegralFunction(function, lower, upper); return(integrateFromPolyFunc(integral)); }
public virtual LUDecompositionResult apply(DoubleMatrix x) { ArgChecker.notNull(x, "x"); RealMatrix temp = CommonsMathWrapper.wrap(x); LUDecomposition lu = new LUDecomposition(temp); return(new LUDecompositionCommonsResult(lu)); }
/// <param name="abscissas"> An array containing the abscissas, not null </param> /// <param name="weights"> An array containing the weights, not null, must be the same length as the abscissa array </param> public GaussianQuadratureData(double[] abscissas, double[] weights) { ArgChecker.notNull(abscissas, "abscissas"); ArgChecker.notNull(weights, "weights"); ArgChecker.isTrue(abscissas.Length == weights.Length, "Abscissa and weight arrays must be the same length"); _weights = weights; _abscissas = abscissas; }
public virtual SVDecompositionResult apply(DoubleMatrix x) { ArgChecker.notNull(x, "x"); MatrixValidate.notNaNOrInfinite(x); RealMatrix commonsMatrix = CommonsMathWrapper.wrap(x); SingularValueDecomposition svd = new SingularValueDecomposition(commonsMatrix); return(new SVDecompositionCommonsResult(svd)); }
/// <summary> /// Creates an instance. /// </summary> /// <param name="cmsParams"> the CMS parameters </param> internal CmsMeasureCalculations(CmsSabrExtrapolationParams cmsParams) { SabrExtrapolationReplicationCmsPeriodPricer periodPricer = SabrExtrapolationReplicationCmsPeriodPricer.of(cmsParams.CutOffStrike, cmsParams.Mu); SabrExtrapolationReplicationCmsLegPricer legPricer = new SabrExtrapolationReplicationCmsLegPricer(periodPricer); SabrExtrapolationReplicationCmsProductPricer productPricer = new SabrExtrapolationReplicationCmsProductPricer(legPricer); SabrExtrapolationReplicationCmsTradePricer tradePricer = new SabrExtrapolationReplicationCmsTradePricer(productPricer); this.tradePricer = ArgChecker.notNull(tradePricer, "tradePricer"); }
/// <summary> /// Interpolate. /// </summary> /// <param name="xValues"> X values of data </param> /// <param name="yValues"> Y values of data </param> /// <param name="xKeys"> the keys </param> /// <returns> Values of the underlying cubic spline function at the values of x </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleArray interpolate(final double[] xValues, final double[] yValues, final double[] xKeys) public virtual DoubleArray interpolate(double[] xValues, double[] yValues, double[] xKeys) { ArgChecker.notNull(xKeys, "xKeys"); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int keyLength = xKeys.length; int keyLength = xKeys.Length; for (int i = 0; i < keyLength; ++i) { ArgChecker.isFalse(double.IsNaN(xKeys[i]), "xKeys containing NaN"); ArgChecker.isFalse(double.IsInfinity(xKeys[i]), "xKeys containing Infinity"); } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final PiecewisePolynomialResult result = this.interpolate(xValues, yValues); PiecewisePolynomialResult result = this.interpolate(xValues, yValues); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray knots = result.getKnots(); DoubleArray knots = result.Knots; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int nKnots = knots.size(); int nKnots = knots.size(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix coefMatrix = result.getCoefMatrix(); DoubleMatrix coefMatrix = result.CoefMatrix; double[] res = new double[keyLength]; for (int j = 0; j < keyLength; ++j) { int indicator = 0; if (xKeys[j] < knots.get(1)) { indicator = 0; } else { for (int i = 1; i < nKnots - 1; ++i) { if (knots.get(i) <= xKeys[j]) { indicator = i; } } } //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray coefs = coefMatrix.row(indicator); DoubleArray coefs = coefMatrix.row(indicator); res[j] = getValue(coefs, xKeys[j], knots.get(indicator)); ArgChecker.isFalse(double.IsInfinity(res[j]), "Too large input"); ArgChecker.isFalse(double.IsNaN(res[j]), "Too large input"); } return(DoubleArray.copyOf(res)); }
//------------------------------------------------------------------------- public virtual System.Func <DoubleArray, DoubleMatrix> differentiate(System.Func <DoubleArray, DoubleArray> function, System.Func <DoubleArray, bool> domain) { ArgChecker.notNull(function, "function"); ArgChecker.notNull(domain, "domain"); double[] wFwd = new double[] { -3.0, 4.0, -1.0 }; double[] wCent = new double[] { -1.0, 0.0, 1.0 }; double[] wBack = new double[] { 1.0, -4.0, 3.0 }; return(new FuncAnonymousInnerClass4(this, function, domain, wFwd, wCent, wBack)); }
/// <summary> /// Interpolate. /// </summary> /// <param name="xValues"> the values </param> /// <param name="yValuesMatrix"> the matrix </param> /// <param name="x"> the s </param> /// <returns> Values of the underlying cubic spline functions interpolating {yValuesMatrix.RowVectors} at the values of x </returns> //JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET: //ORIGINAL LINE: public com.opengamma.strata.collect.array.DoubleMatrix interpolate(final double[] xValues, final double[][] yValuesMatrix, final double[] x) public virtual DoubleMatrix interpolate(double[] xValues, double[][] yValuesMatrix, double[] x) { ArgChecker.notNull(x, "x"); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleMatrix matrix = com.opengamma.strata.collect.array.DoubleMatrix.copyOf(yValuesMatrix); DoubleMatrix matrix = DoubleMatrix.copyOf(yValuesMatrix); return(DoubleMatrix.ofArrayObjects(yValuesMatrix.Length, x.Length, i => interpolate(xValues, matrix.rowArray(i), x))); }
public override double?apply(double?x) { ArgChecker.notNull(x, "x"); ArgChecker.notNegative(x, "x"); if (DoubleMath.fuzzyEquals(x, 0.5, 1e-14)) { return(0.5); } return(_dist.getInverseCDF(x.Value)); }