95namespace matrix_chain_detail {
96inline size_t checked_add(
const size_t a,
const size_t b,
const char *ctx)
99 <<
"matrix_chain_order: overflow while computing " << ctx;
103inline size_t checked_mul(
const size_t a,
const size_t b,
const char *ctx)
105 if (a == 0 or b == 0)
109 <<
"matrix_chain_order: overflow while computing " << ctx;
113inline size_t scalar_cost(
const size_t di,
const size_t dk,
const size_t dj)
115 const size_t lhs =
checked_mul(di, dk,
"dims[i] * dims[k+1]");
116 return checked_mul(lhs, dj,
"dims[i] * dims[k+1] * dims[j+1]");
124 out += std::to_string(i + 1);
156 for (
const size_t dim : dims)
159 const size_t n = dims.
size() - 1;
166 for (
size_t i = 0; i < n; ++i)
169 for (
size_t j = 0; j < n; ++j)
176 for (
size_t i = 0; i < n; ++i)
179 for (
size_t j = 0; j < n; ++j)
185 for (
size_t l = 2;
l <= n; ++
l)
186 for (
size_t i = 0; i <= n -
l; ++i)
188 const size_t j = i +
l - 1;
189 dp[i][j] = std::numeric_limits<size_t>::max();
190 for (
size_t k = i;
k < j; ++
k)
192 const size_t mul_cost
194 const size_t subtotal
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t checked_add(const size_t a, const size_t b, const char *ctx)
size_t scalar_cost(const size_t di, const size_t dk, const size_t dj)
size_t checked_mul(const size_t a, const size_t b, const char *ctx)
void build_parens(const Array< Array< size_t > > &s, size_t i, size_t j, std::string &out)
Main namespace for Aleph-w library functions.
Matrix_Chain_Result matrix_chain_order(const Array< size_t > &dims)
Compute the optimal matrix-chain multiplication order.
size_t matrix_chain_min_cost(const Array< size_t > &dims)
Compute only the minimum multiplication cost (value only).
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
Result of matrix-chain multiplication optimization.
Array< Array< size_t > > split
Internal split table used for reconstruction.
size_t min_multiplications
Minimum scalar multiplications required.
std::string parenthesization
Optimal parenthesization string (e.g., "(A1 (A2 A3))").
Dynamic array container with automatic resizing.