Skip to content

[AUD-024][High] Make condition-number detection scale invariant #320

Description

@gabrielsantosphilips

Audit finding

  • ID: AUD-024
  • Status: Verified defect
  • Severity: High
  • Confidence: High
  • Audited revision: 2f479320d805a1f9f35ebe4afaaeeded48913a94

Problem

ConditionNumber() applies an absolute 1e-12 zero-row/norm threshold. This makes classification depend on matrix units rather than conditioning. It also screens only near-zero rows before calling assert-based Gaussian elimination.

Source:

{
namespace detail
{
template<typename T, std::size_t N>
bool HasZeroRow(const math::SquareMatrix<T, N>& a)
{
for (std::size_t i = 0; i < N; ++i)
{
bool allZero = true;
for (std::size_t j = 0; j < N; ++j)
{
if (math::Abs(a.at(i, j)) > static_cast<T>(1e-12))
{
allZero = false;
break;
}
}
if (allZero)
return true;
}
return false;
}
}
template<typename T, std::size_t N>
[[nodiscard]] OPTIMIZE_FOR_SPEED std::optional<T> ConditionNumber(const math::SquareMatrix<T, N>& a)
{
static_assert(std::is_floating_point_v<T>, "ConditionNumber supports floating-point types");
if (detail::HasZeroRow(a))
return std::nullopt;
T normA = math::OneNorm(a);
if (normA < static_cast<T>(1e-12))
return std::nullopt;
auto invA = SolveSystem(a, math::SquareMatrix<T, N>::Identity());
return normA * math::OneNorm(invA);
}
}

Reproduction

diag(5e-13, 5e-13) is invertible with condition number one, but returns nullopt. A dependent-row matrix with no near-zero row passes the screen and reaches a zero pivot.

Acceptance criteria

  • Rank/pivot detection is relative to matrix scale.
  • Singular factorization returns status instead of asserting.
  • Multiplying a nonsingular matrix by nonzero scalar does not change its reported condition number.
  • Add multi-decade scale and dependent-row tests.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions