本文整理汇总了C#中Vector.size方法的典型用法代码示例。如果您正苦于以下问题:C# Vector.size方法的具体用法?C# Vector.size怎么用?C# Vector.size使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector
的用法示例。
在下文中一共展示了Vector.size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TransformedGrid
public TransformedGrid (Vector grid) {
grid_ = (Vector)grid.Clone();
transformedGrid_ = (Vector)grid.Clone();
dxm_= new Vector(grid.size());
dxp_ = new Vector(grid.size());
dx_ = new Vector(grid.size());
for (int i=1; i < transformedGrid_.size() -1 ; i++) {
dxm_[i] = transformedGrid_[i] - transformedGrid_[i-1];
dxp_[i] = transformedGrid_[i+1] - transformedGrid_[i];
dx_[i] = dxm_[i] + dxp_[i];
}
}
示例2: minimize
public override EndCriteria.Type minimize(Problem P, EndCriteria endCriteria) {
EndCriteria.Type ecType = EndCriteria.Type.None;
P.reset();
Vector x_ = P.currentValue();
currentProblem_ = P;
initCostValues_ = P.costFunction().values(x_);
int m = initCostValues_.size();
int n = x_.size();
Vector xx = new Vector(x_);
Vector fvec = new Vector(m), diag = new Vector(n);
int mode = 1;
double factor = 1;
int nprint = 0;
int info = 0;
int nfev =0;
Matrix fjac = new Matrix(m, n);
int ldfjac = m;
List<int> ipvt = new InitializedList<int>(n);
Vector qtf = new Vector(n), wa1 = new Vector(n), wa2 = new Vector(n), wa3 = new Vector(n), wa4 = new Vector(m);
// call lmdif to minimize the sum of the squares of m functions
// in n variables by the Levenberg-Marquardt algorithm.
MINPACK.lmdif(m, n, xx, ref fvec,
endCriteria.functionEpsilon(),
xtol_,
gtol_,
endCriteria.maxIterations(),
epsfcn_,
diag, mode, factor,
nprint, ref info, ref nfev, ref fjac,
ldfjac, ref ipvt, ref qtf,
wa1, wa2, wa3, wa4,
fcn);
info_ = info;
// check requirements & endCriteria evaluation
if(info == 0) throw new ApplicationException("MINPACK: improper input parameters");
//if(info == 6) throw new ApplicationException("MINPACK: ftol is too small. no further " +
// "reduction in the sum of squares is possible.");
if (info != 6) ecType = EndCriteria.Type.StationaryFunctionValue;
//QL_REQUIRE(info != 5, "MINPACK: number of calls to fcn has reached or exceeded maxfev.");
endCriteria.checkMaxIterations(nfev, ref ecType);
if(info == 7) throw new ApplicationException("MINPACK: xtol is too small. no further " +
"improvement in the approximate " +
"solution x is possible.");
if(info == 8) throw new ApplicationException("MINPACK: gtol is too small. fvec is " +
"orthogonal to the columns of the " +
"jacobian to machine precision.");
// set problem
x_ = new Vector(xx.GetRange(0, n));
P.setCurrentValue(x_);
P.setFunctionValue(P.costFunction().value(x_));
return ecType;
}
示例3: BSMOperator
public BSMOperator(Vector grid, GeneralizedBlackScholesProcess process, double residualTime)
: base(grid.size())
{
//PdeBSM::grid_type logGrid(grid);
LogGrid logGrid = new LogGrid(grid);
var cc = new PdeConstantCoeff<PdeBSM>(process, residualTime, process.stateVariable().link.value());
cc.generateOperator(residualTime, logGrid, this);
}
示例4: gradient
//! compute vector of derivatives of the least square function
public void gradient(ref Vector grad_f, Vector x)
{
// size of target and function to fit vectors
Vector target = new Vector(lsp_.size());
Vector fct2fit = new Vector(lsp_.size());
// size of gradient matrix
Matrix grad_fct2fit = new Matrix(lsp_.size(), x.size());
// compute its values
lsp_.targetValueAndGradient(x, ref grad_fct2fit, ref target, ref fct2fit);
// do the difference
Vector diff = target - fct2fit;
// compute derivative
grad_f = -2.0 * (Matrix.transpose(grad_fct2fit) * diff);
}
示例5: LmFixedVolatilityModel
public LmFixedVolatilityModel(Vector volatilities,
List<double> startTimes)
: base(startTimes.Count, 0)
{
volatilities_ = volatilities;
startTimes_ = startTimes;
if (!(startTimes_.Count > 1))
throw new ApplicationException("too few dates");
if (!(volatilities_.size() == startTimes_.Count))
throw new ApplicationException("volatility array and fixing time array have to have the same size");
for (int i = 1; i < startTimes_.Count; i++) {
if (!(startTimes_[i] > startTimes_[i-1]))
throw new ApplicationException( "invalid time ("+startTimes_[i]+", vs "+startTimes_[i-1]+")");
}
}
示例6: stdDeviation
public override Matrix stdDeviation(double t0, Vector x0, double dt)
{
#if QL_EXTRA_SAFETY_CHECKS
QL_REQUIRE(x0.size() == 1, () => "1-D array required");
#endif
Matrix m = new Matrix(1, 1, stdDeviation(t0, x0[0], dt));
return m;
}
示例7: evolve
public virtual Vector evolve(double t0, ref Vector x0, double dt, ref Vector dw)
{
#if QL_EXTRA_SAFETY_CHECKS
QL_REQUIRE(x0.size() == 1, () => "1-D array required");
QL_REQUIRE(dw.size() == 1, () => "1-D array required");
#endif
Vector a = new Vector(1, evolve(t0, x0[0], dt, dw[0]));
return a;
}
示例8: diffusion
public override Matrix diffusion(double t, Vector x)
{
#if QL_EXTRA_SAFETY_CHECKS
QL_REQUIRE(x.size() == 1, () => "1-D array required");
#endif
Matrix m = new Matrix(1, 1, diffusion(t, x[0]));
return m;
}
示例9: value
public override double value(Vector x)
{
if(x.size()!=1) throw new ApplicationException("independent variable must be 1 dimensional");
double y = 0;
for (int i=0; i<=polynomialDegree_; ++i)
y += coefficients_[i]*Utils.Pow(x[0],i);
return y;
}
示例10: OneDimensionalPolynomialDegreeN
public OneDimensionalPolynomialDegreeN(Vector coefficients)
{
coefficients_ = new Vector(coefficients);
polynomialDegree_ = coefficients.size()-1;
}
示例11: testBSMOperatorConsistency
public void testBSMOperatorConsistency()
{
//("Testing consistency of BSM operators...");
Vector grid = new Vector(10);
double price = 20.0;
double factor = 1.1;
for (int i = 0; i < grid.size(); i++)
{
grid[i] = price;
price *= factor;
}
double dx = Math.Log(factor);
double r = 0.05;
double q = 0.01;
double sigma = 0.5;
BSMOperator refer = new BSMOperator(grid.size(), dx, r, q, sigma);
DayCounter dc = new Actual360();
Date today = Date.Today;
Date exercise = today + new Period(2, TimeUnit.Years);
double residualTime = dc.yearFraction(today, exercise);
SimpleQuote spot = new SimpleQuote(0.0);
YieldTermStructure qTS = Utilities.flatRate(today, q, dc);
YieldTermStructure rTS = Utilities.flatRate(today, r, dc);
BlackVolTermStructure volTS = Utilities.flatVol(today, sigma, dc);
GeneralizedBlackScholesProcess stochProcess = new GeneralizedBlackScholesProcess(
new Handle<Quote>(spot),
new Handle<YieldTermStructure>(qTS),
new Handle<YieldTermStructure>(rTS),
new Handle<BlackVolTermStructure>(volTS));
BSMOperator op1 = new BSMOperator(grid, stochProcess, residualTime);
PdeOperator<PdeBSM> op2 = new PdeOperator<PdeBSM>(grid, stochProcess, residualTime);
double tolerance = 1.0e-6;
Vector lderror = refer.lowerDiagonal() - op1.lowerDiagonal();
Vector derror = refer.diagonal() - op1.diagonal();
Vector uderror = refer.upperDiagonal() - op1.upperDiagonal();
for (int i = 2; i < grid.size() - 2; i++)
{
if (Math.Abs(lderror[i]) > tolerance ||
Math.Abs(derror[i]) > tolerance ||
Math.Abs(uderror[i]) > tolerance)
{
Assert.Fail("inconsistency between BSM operators:\n"
+ i + " row:\n"
+ "expected: "
+ refer.lowerDiagonal()[i] + ", "
+ refer.diagonal()[i] + ", "
+ refer.upperDiagonal()[i] + "\n"
+ "calculated: "
+ op1.lowerDiagonal()[i] + ", "
+ op1.diagonal()[i] + ", "
+ op1.upperDiagonal()[i]);
}
}
lderror = refer.lowerDiagonal() - op2.lowerDiagonal();
derror = refer.diagonal() - op2.diagonal();
uderror = refer.upperDiagonal() - op2.upperDiagonal();
for (int i = 2; i < grid.size() - 2; i++)
{
if (Math.Abs(lderror[i]) > tolerance ||
Math.Abs(derror[i]) > tolerance ||
Math.Abs(uderror[i]) > tolerance)
{
Assert.Fail("inconsistency between BSM operators:\n"
+ i + " row:\n"
+ "expected: "
+ refer.lowerDiagonal()[i] + ", "
+ refer.diagonal()[i] + ", "
+ refer.upperDiagonal()[i] + "\n"
+ "calculated: "
+ op2.lowerDiagonal()[i] + ", "
+ op2.diagonal()[i] + ", "
+ op2.upperDiagonal()[i]);
}
}
}
示例12: testOperatorConsistency
public void testOperatorConsistency()
{
//("Testing differential operators...");
NormalDistribution normal = new NormalDistribution(average, sigma);
CumulativeNormalDistribution cum = new CumulativeNormalDistribution(average, sigma);
double xMin = average - 4 * sigma,
xMax = average + 4 * sigma;
int N = 10001;
double h = (xMax - xMin) / (N - 1);
Vector x = new Vector(N),
y = new Vector(N),
yi = new Vector(N),
yd = new Vector(N),
temp = new Vector(N),
diff = new Vector(N);
for (int i = 0; i < N; i++)
x[i] = xMin + h * i;
for (int i = 0; i < x.Count; i++)
y[i] = normal.value(x[i]);
for (int i = 0; i < x.Count; i++)
yi[i] = cum.value(x[i]);
for (int i = 0; i < x.size(); i++)
yd[i] = normal.derivative(x[i]);
// define the differential operators
DZero D = new DZero(N, h);
DPlusDMinus D2 = new DPlusDMinus(N, h);
// check that the derivative of cum is Gaussian
temp = D.applyTo(yi);
for (int i = 0; i < y.Count; i++)
diff[i] = y[i] - temp[i];
double e = Utilities.norm(diff, diff.size(), h);
if (e > 1.0e-6)
{
Assert.Fail("norm of 1st derivative of cum minus Gaussian: " + e + "\ntolerance exceeded");
}
// check that the second derivative of cum is normal.derivative
temp = D2.applyTo(yi);
for (int i = 0; i < yd.Count; i++)
diff[i] = yd[i] - temp[i];
e = Utilities.norm(diff, diff.size(), h);
if (e > 1.0e-4)
{
Assert.Fail("norm of 2nd derivative of cum minus Gaussian derivative: " + e + "\ntolerance exceeded");
}
}
示例13: discountBond
public virtual double discountBond(double now,
double maturity,
Vector factors)
{
if (!(factors.size() > 1))
throw new ApplicationException("g2 model needs two factors to compute discount bond");
return discountBond(now, maturity, factors[0], factors[1]);
}
示例14: valueAndGradient
//! compute value and gradient of the least square function
public double valueAndGradient(ref Vector grad_f, Vector x)
{
// size of target and function to fit vectors
Vector target = new Vector(lsp_.size());
Vector fct2fit = new Vector(lsp_.size());
// size of gradient matrix
Matrix grad_fct2fit = new Matrix(lsp_.size(), x.size());
// compute its values
lsp_.targetValueAndGradient(x, ref grad_fct2fit, ref target, ref fct2fit);
// do the difference
Vector diff = target - fct2fit;
// compute derivative
grad_f = -2.0 * (Matrix.transpose(grad_fct2fit) * diff);
// and compute the scalar product (square of the norm)
return Vector.DotProduct(diff, diff);
}
示例15: copyChangedValues
void copyChangedValues(DBAdapter db)
{
updateAllAttribChanges();
copyTable("changed_values", m_dbStorage, db.m_dbStorage );
{
Vector<int> arOldSrcs = new Vector<int>();
{
IDBResult resSrc = db.executeSQL( "SELECT DISTINCT(source_id) FROM changed_values" );
for ( ; !resSrc.isEnd(); resSrc.next() )
arOldSrcs.addElement( resSrc.getIntByIdx(0) );
}
for( int i = 0; i < arOldSrcs.size(); i++)
{
int nOldSrcID = arOldSrcs.elementAt(i);
IDBResult res = executeSQL("SELECT name from sources WHERE source_id=?", nOldSrcID);
if ( !res.isEnd() )
{
String strSrcName = res.getStringByIdx(0);
IDBResult res2 = db.executeSQL("SELECT source_id from sources WHERE name=?", strSrcName );
if ( !res2.isEnd() )
{
if ( nOldSrcID != res2.getIntByIdx(0) )
{
db.executeSQL("UPDATE changed_values SET source_id=? WHERE source_id=?", res2.getIntByIdx(0), nOldSrcID);
}
continue;
}
}
//source not exist in new partition, remove this changes
db.executeSQL("DELETE FROM changed_values WHERE source_id=?", nOldSrcID);
}
}
}