With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Computing Sums and Products

Problem

You need to sum or multiply functions of numbers contained in a node set.

Solution

The abstract form of sum for processors that support tail-recursive optimization is as follows:

```<xsl:template name="math:sum">
<!-- Initialize nodes to empty node set -->
<xsl:param name="nodes" select="/.."/>
<xsl:param name="result" select="0"/>
<xsl:choose>
<xsl:when test="not(\$nodes)">
<xsl:value-of select="\$result"/>
</xsl:when>
<xsl:otherwise>
<!-- call or apply template that will determine value of node
unless the node is literally the value to be summed -->
<xsl:variable name="value">
<xsl:call-template name="some-function-of-a-node">
<xsl:with-param name="node" select="\$nodes[1]"/>
</xsl:call-template>
</xsl:variable>
<!-- recurse to sum rest -->
<xsl:call-template name="math:sum">
<xsl:with-param name="nodes" select="\$nodes[position(  ) != 1]"/>
<xsl:with-param name="result" select="\$result + \$value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>```

Two techniques can handle a large number of nodes in the absence of tail-recursive optimization. The first is commonly called `divide and conquer`. The idea behind this technique is to reduce the amount of work by at least a factor of two on each recursive step:

`<xsl:template name="math:sum-dvc"> <xsl:param name="nodes" select="/.."/> <xsl:param name="result" select="0"/> <xsl:param name="dvc-threshold" select="100"/> <xsl:choose> <xsl:when test="count(\$nodes) &lt;= \$dvc-threshold"> <xsl:call-template ...`

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.safaribooksonline.com/library/view/xslt-cookbook/0596003722/ch02s06.html", "fetch_time": 1532084390000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2018-30/segments/1531676591578.1/warc/CC-MAIN-20180720100114-20180720120114-00460.warc.gz", "warc_record_offset": 962239449, "warc_record_length": 8524, "token_count": 490, "char_count": 1898, "score": 3.765625, "int_score": 4, "crawl": "CC-MAIN-2018-30", "snapshot_type": "latest", "language": "en", "language_score": 0.5740891695022583, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00042-of-00064.parquet"}