Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:20:54

0001 // Copyright (c) 2025 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _MathUtils_Types_HeaderFile
0015 #define _MathUtils_Types_HeaderFile
0016 
0017 #include <math_Vector.hxx>
0018 #include <math_Matrix.hxx>
0019 
0020 #include <array>
0021 #include <optional>
0022 
0023 //! Modern math solver types and result structures.
0024 namespace MathUtils
0025 {
0026 
0027 //! Computation status for all math solvers.
0028 //! Provides detailed information about solver outcome.
0029 enum class Status
0030 {
0031   OK,                  //!< Computation successful, solution found
0032   NotConverged,        //!< Did not converge within tolerance
0033   MaxIterations,       //!< Maximum iterations reached without convergence
0034   NumericalError,      //!< Numerical issue (overflow, NaN, division by zero, etc.)
0035   InvalidInput,        //!< Invalid input parameters
0036   InfiniteSolutions,   //!< Infinite number of solutions (degenerate case)
0037   NoSolution,          //!< No solution exists
0038   NotPositiveDefinite, //!< Matrix not positive definite (for Cholesky, Newton, etc.)
0039   Singular,            //!< Matrix is singular or nearly singular
0040   NonDescentDirection  //!< Search direction is not a descent direction for merit function
0041 };
0042 
0043 //! Result for scalar (1D) root finding and minimization.
0044 //! Contains the found root/minimum location and diagnostic information.
0045 struct ScalarResult
0046 {
0047   MathUtils::Status     Status       = MathUtils::Status::NotConverged; //!< Computation status
0048   size_t                NbIterations = 0; //!< Number of iterations performed
0049   std::optional<double> Root;             //!< Found root or minimum location
0050   std::optional<double> Value;            //!< Function value at root/minimum
0051   std::optional<double> Derivative;       //!< Derivative at root (if computed)
0052 
0053   //! Returns true if computation succeeded.
0054   bool IsDone() const { return Status == MathUtils::Status::OK; }
0055 
0056   //! Conversion to bool for convenient checking.
0057   //! Example: if (aResult) { use *aResult.Root; }
0058   explicit operator bool() const { return IsDone(); }
0059 };
0060 
0061 //! Result for polynomial root finding.
0062 //! Supports up to 4 real roots (for quartic equations).
0063 struct PolyResult
0064 {
0065   MathUtils::Status     Status  = MathUtils::Status::NotConverged; //!< Computation status
0066   size_t                NbRoots = 0;                               //!< Number of real roots found
0067   std::array<double, 4> Roots   = {0.0, 0.0, 0.0, 0.0};            //!< Array of real roots (sorted)
0068 
0069   //! Returns true if computation succeeded.
0070   bool IsDone() const { return Status == MathUtils::Status::OK; }
0071 
0072   //! Conversion to bool for convenient checking.
0073   explicit operator bool() const { return IsDone(); }
0074 
0075   //! Access root by index (0-based).
0076   //! @param theIndex root index (0 to NbRoots-1)
0077   //! @return root value
0078   double operator[](int theIndex) const { return Roots[theIndex]; }
0079 };
0080 
0081 //! Result for N-dimensional optimization and system solving.
0082 //! Contains the solution vector and optional gradient/Jacobian information.
0083 struct VectorResult
0084 {
0085   MathUtils::Status          Status       = MathUtils::Status::NotConverged; //!< Computation status
0086   size_t                     NbIterations = 0; //!< Number of iterations performed
0087   std::optional<math_Vector> Solution;         //!< Solution vector (set by solver on success)
0088   std::optional<double>      Value;            //!< Function value at solution (if computed)
0089   std::optional<math_Vector> Gradient;         //!< Gradient at solution (if computed)
0090   std::optional<math_Matrix> Jacobian;         //!< Jacobian at solution (if computed)
0091 
0092   //! Returns true if computation succeeded.
0093   bool IsDone() const { return Status == MathUtils::Status::OK; }
0094 
0095   //! Conversion to bool for convenient checking.
0096   explicit operator bool() const { return IsDone(); }
0097 };
0098 
0099 //! Result for linear system solving (Ax = b).
0100 //! Contains the solution vector and matrix determinant if computed.
0101 struct LinearResult
0102 {
0103   MathUtils::Status          Status = MathUtils::Status::NotConverged; //!< Computation status
0104   std::optional<math_Vector> Solution;    //!< Solution vector X in AX = B (set by solver)
0105   std::optional<double>      Determinant; //!< Determinant of matrix (if computed)
0106 
0107   //! Returns true if computation succeeded.
0108   bool IsDone() const { return Status == MathUtils::Status::OK; }
0109 
0110   //! Conversion to bool for convenient checking.
0111   explicit operator bool() const { return IsDone(); }
0112 };
0113 
0114 //! Result for multiple linear systems solving (AX = B with matrix RHS).
0115 //! Contains the full solution matrix and determinant if computed.
0116 struct LinearMultipleResult
0117 {
0118   MathUtils::Status          Status = MathUtils::Status::NotConverged; //!< Computation status
0119   std::optional<math_Matrix> Solutions;   //!< Solution matrix X in AX = B (set by solver)
0120   std::optional<double>      Determinant; //!< Determinant of matrix (if computed)
0121 
0122   //! Returns true if computation succeeded.
0123   bool IsDone() const { return Status == MathUtils::Status::OK; }
0124 
0125   //! Conversion to bool for convenient checking.
0126   explicit operator bool() const { return IsDone(); }
0127 };
0128 
0129 //! Result for eigenvalue/eigenvector computation.
0130 //! Contains eigenvalues and optionally eigenvectors.
0131 struct EigenResult
0132 {
0133   MathUtils::Status          Status       = MathUtils::Status::NotConverged; //!< Computation status
0134   size_t                     NbIterations = 0; //!< Number of iterations performed
0135   std::optional<math_Vector> EigenValues;      //!< Computed eigenvalues (set by solver)
0136   std::optional<math_Matrix> EigenVectors;     //!< Computed eigenvectors (set by solver)
0137 
0138   //! Returns true if computation succeeded.
0139   bool IsDone() const { return Status == MathUtils::Status::OK; }
0140 
0141   //! Conversion to bool for convenient checking.
0142   explicit operator bool() const { return IsDone(); }
0143 };
0144 
0145 //! Result for matrix decomposition (LU, SVD, QR).
0146 //! Structure depends on decomposition type.
0147 struct DecompResult
0148 {
0149   MathUtils::Status          Status = MathUtils::Status::NotConverged; //!< Computation status
0150   std::optional<math_Matrix> L;           //!< Lower triangular (LU) or left singular vectors (SVD)
0151   std::optional<math_Matrix> U;           //!< Upper triangular (LU) or right singular vectors (SVD)
0152   std::optional<math_Vector> D;           //!< Diagonal elements or singular values
0153   std::optional<double>      Determinant; //!< Matrix determinant (if computed)
0154 
0155   //! Returns true if decomposition succeeded.
0156   bool IsDone() const { return Status == MathUtils::Status::OK; }
0157 
0158   //! Conversion to bool for convenient checking.
0159   explicit operator bool() const { return IsDone(); }
0160 };
0161 
0162 //! Result for numerical integration.
0163 //! Contains integral value and error estimates.
0164 struct IntegResult
0165 {
0166   MathUtils::Status     Status       = MathUtils::Status::NotConverged; //!< Computation status
0167   size_t                NbIterations = 0; //!< Number of adaptive iterations
0168   size_t                NbPoints     = 0; //!< Total number of quadrature points used
0169   std::optional<double> Value;            //!< Computed integral value
0170   std::optional<double> AbsoluteError;    //!< Estimated absolute error (if computed)
0171   std::optional<double> RelativeError;    //!< Estimated relative error (if computed)
0172 
0173   //! Returns true if integration succeeded.
0174   bool IsDone() const { return Status == MathUtils::Status::OK; }
0175 
0176   //! Conversion to bool for convenient checking.
0177   explicit operator bool() const { return IsDone(); }
0178 };
0179 
0180 //! Result for matrix inverse computation.
0181 //! Contains the inverse matrix if computation succeeded.
0182 struct InverseResult
0183 {
0184   MathUtils::Status          Status = MathUtils::Status::NotConverged; //!< Computation status
0185   std::optional<math_Matrix> Inverse;                                  //!< Computed inverse matrix
0186   std::optional<double>      Determinant; //!< Determinant of matrix (if computed)
0187 
0188   //! Returns true if inversion succeeded.
0189   bool IsDone() const { return Status == MathUtils::Status::OK; }
0190 
0191   //! Conversion to bool for convenient checking.
0192   explicit operator bool() const { return IsDone(); }
0193 };
0194 
0195 } // namespace MathUtils
0196 
0197 #endif // _MathUtils_Types_HeaderFile