本文整理汇总了C#中DenseVector.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# DenseVector.Clear方法的具体用法?C# DenseVector.Clear怎么用?C# DenseVector.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DenseVector
的用法示例。
在下文中一共展示了DenseVector.Clear方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Approximate
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
public void Approximate(Vector rhs, Vector lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if (_decompositionLU == null)
{
throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
}
if ((lhs.Count != rhs.Count) || (lhs.Count != _decompositionLU.RowCount))
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
// Solve:
// Lz = y
// Which gives
// for (int i = 1; i < matrix.RowLength; i++)
// {
// z_i = l_ii^-1 * (y_i - SUM_(j<i) l_ij * z_j)
// }
// NOTE: l_ii should be 1 because u_ii has to be the value
Vector rowValues = new DenseVector(_decompositionLU.RowCount);
for (var i = 0; i < _decompositionLU.RowCount; i++)
{
// Clear the rowValues
rowValues.Clear();
_decompositionLU.Row(i, rowValues);
var sum = 0.0;
for (var j = 0; j < i; j++)
{
sum += rowValues[j] * lhs[j];
}
lhs[i] = rhs[i] - sum;
}
// Solve:
// Ux = z
// Which gives
// for (int i = matrix.RowLength - 1; i > -1; i--)
// {
// x_i = u_ii^-1 * (z_i - SUM_(j > i) u_ij * x_j)
// }
for (var i = _decompositionLU.RowCount - 1; i > -1; i--)
{
_decompositionLU.Row(i, rowValues);
var sum = 0.0;
for (var j = _decompositionLU.RowCount - 1; j > i; j--)
{
sum += rowValues[j] * lhs[j];
}
lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
}
}
示例2: Solve
//.........这里部分代码省略.........
utemp.Multiply(-rho, temp);
xtemp.Add(temp, temp2);
temp2.CopyTo(xtemp);
gtemp.Multiply(alpha, gtemp);
xtemp.Add(gtemp, temp2);
temp2.CopyTo(xtemp);
// Check convergence and stop if we are converged.
if (!ShouldContinue(iterationNumber, xtemp, input, residuals))
{
// Calculate the true residual
CalculateTrueResidual(matrix, residuals, xtemp, input);
// Now recheck the convergence
if (!ShouldContinue(iterationNumber, xtemp, input, residuals))
{
// We're all good now.
// Exit from the while loop.
break;
}
}
// FOR (i = 1,2, ...., k)
for (var i = 0; i < k; i++)
{
// z_d = u_(jk+1)
u.CopyTo(zd);
// z_g = r_(jk+i)
residuals.CopyTo(zg);
// z_w = 0
zw.Clear();
// FOR (s = i, ...., k-1) AND j >= 1
Complex beta;
if (iterationNumber >= 1)
{
for (var s = i; s < k - 1; s++)
{
// beta^(jk+i)_((j-1)k+s) = -q^t_(s+1) z_d / c_((j-1)k+s)
beta = -_startingVectors[s + 1].DotProduct(zd) / c[s];
// z_d = z_d + beta^(jk+i)_((j-1)k+s) d_((j-1)k+s)
d[s].Multiply(beta, temp);
zd.Add(temp, temp2);
temp2.CopyTo(zd);
// z_g = z_g + beta^(jk+i)_((j-1)k+s) g_((j-1)k+s)
g[s].Multiply(beta, temp);
zg.Add(temp, temp2);
temp2.CopyTo(zg);
// z_w = z_w + beta^(jk+i)_((j-1)k+s) w_((j-1)k+s)
w[s].Multiply(beta, temp);
zw.Add(temp, temp2);
temp2.CopyTo(zw);
}
}
beta = rho * c[k - 1];
if (beta.Real.AlmostEqual(0, 1) && beta.Imaginary.AlmostEqual(0, 1))
{
throw new Exception("Iterative solver experience a numerical break down");
}