cost - Maple Help

codegen

 cost
 operation evaluation count

 Calling Sequence cost(x1, x2,...,xn)

Parameters

 xk - of type algebraic, name = algebraic, array(algebraic)

Description

 • The cost command is used to compute an operation count for the numerical evaluation of the given arguments. The operation count is expressed as a polynomial in the names additions, multiplications, assignments, divisions, functions, storage, subscripts, integrals, products and assignments with non-negative integer coefficients.
 • Assignment of positive real values to these global names yields a weighted cost.
 • Note that the cost used for computing powers is as follows. For an integral power, repeated multiplication is assumed. For a general power it is assumed to be computed using exp and ln.
 • The command with(codegen,cost) allows the use of the abbreviated form of this command.

Examples

 > $\mathrm{with}\left(\mathrm{codegen},\mathrm{cost},\mathrm{optimize}\right):$
 > $a≔x+{x}^{2}+{x}^{3}+{x}^{4}:$
 > $\mathrm{cost}\left(a\right)$
 ${3}{}{\mathrm{additions}}{+}{6}{}{\mathrm{multiplications}}$ (1)
 > $a≔\mathrm{convert}\left(a,\mathrm{horner}\right):$
 > $\mathrm{cost}\left(a\right)$
 ${3}{}{\mathrm{multiplications}}{+}{3}{}{\mathrm{additions}}$ (2)

Using numeric values to express the relative costs of various operations, you can derive a numerical measure of the cost of an expression or procedure. This can then be used to evaluate whether or not to use an optimized version.

 > $\mathrm{rp}≔\mathrm{randpoly}\left(\left[x,y,z\right]\right):$
 > $p≔\mathrm{unapply}\left(\mathrm{rp},\left[x,y,z\right]\right):$
 > $\mathrm{po1}≔\mathrm{optimize}\left(p\right):$$\mathrm{po2}≔\mathrm{optimize}\left(p,':-\mathrm{tryhard}'\right):$
 > $c≔\mathrm{cost}\left(p\right):$
 > $\mathrm{c1}≔\mathrm{cost}\left(\mathrm{po1}\right):$
 > $\mathrm{c2}≔\mathrm{cost}\left(\mathrm{po2}\right):$
 > $c-\mathrm{c1};$$c-\mathrm{c2};$$\mathrm{c1}-\mathrm{c2}$
 ${4}{}{\mathrm{multiplications}}{-}{5}{}{\mathrm{storage}}{-}{5}{}{\mathrm{assignments}}$
 ${8}{}{\mathrm{multiplications}}{-}{2}{}{\mathrm{storage}}{-}{2}{}{\mathrm{assignments}}$
 ${3}{}{\mathrm{storage}}{+}{3}{}{\mathrm{assignments}}{+}{4}{}{\mathrm{multiplications}}$ (3)
 > $V≔\left\{\mathrm{storage}=2,\mathrm{additions}=1,\mathrm{assignments}=1,\mathrm{functions}=100,\mathrm{multiplications}=20\right\}:$
 > $\mathrm{eval}\left(c,V\right)$
 ${424}$ (4)
 > $\mathrm{eval}\left(\mathrm{c1},V\right)$
 ${359}$ (5)
 > $\mathrm{eval}\left(\mathrm{c2},V\right)$
 ${270}$ (6)