XML technologies / XSLT / XSLT Kochbuch / Numbers and calculations / Calculating combinatorial functions
(Excerpt from "XSLT Kochbuch" by Sal Mangano)
Problem
You need to calculate the number of permutations or combinations of size r of a given set.
Solution
XSLT 1.0
If you know that the formula for permutations of size r is N! / r! and the formula for combinations is N! / r! * (N-r)! you might skip this example because this book has already included an example of factorials. However, factorials can become very large very quickly, so you will probably need a few tricks to get the most out of your calculations:
<xsl:template name="ckbk:P">
<xsl:param name="n" select="1"/>
<xsl:param name="r" select="1"/>
<xsl:choose>
<xsl:when test="$n < 0 or $r < 0">NaN</xsl:when>
<xsl:when test="$n = 0">0</xsl:when>
<xsl:otherwise>
<xsl:call-template name="prod-range">
<xsl:with-param name="start" select="$r + 1"/>
<xsl:with-param name="end" select="$n"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ckbk:C">
<xsl:param name="n" select="1"/>
<xsl:param name="r" select="1"/>
<xsl:choose>
<xsl:when test="$n < 0 or $r < 0">NaN</xsl:when>
<xsl:when test="$n = 0">0</xsl:when>
<xsl:otherwise>
<xsl:variable name="min" select="($r <= $n - $r) * $r + ($r > $n - $r) * $n - $r"/>
<xsl:variable name="max" select="($r >= $n - $r) * $r + ($r < $n - $r) * $n - $r"/>
<xsl:variable name="numerator">
<xsl:call-template name="prod-range">
<xsl:with-param name="start" select="$max + 1"/>
<xsl:with-param name="end" select="$n"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="denominator">
<xsl:call-template name="ckbk:fact">
<xsl:with-param name="number" select="$min"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$numerator div $denominator"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>XSLT 2.0
<xsl:function name="ckbk:P" as="xs:decimal">
<xsl:param name="r" as="xs:integer"/>
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="if ($n eq 0) then 0 else ckbk:prod-range($r + 1, $n)"/>
</xsl:function>
<xsl:function name="ckbk:C" as="xs:decimal">
<xsl:param name="r" as="xs:integer"/>
<xsl:param name="n" as="xs:integer"/>
<xsl:variable name="min" select="min( ($r, $n - $r) )" as="xs:integer"/>
<xsl:variable name="max" select="max( ($r, $n - $r) )" as="xs:integer"/>
<xsl:sequence select="if ($n eq 0) then 0 else ckbk:prod-range($max + 1, $n) div ckbk:factorial($min)"/>
</xsl:function>Discussion
The solutions are designed to reduce the number of multiplications. When a factorial is divided by a smaller factorial, the smaller factorial effectively cancels out the corresponding multiplications in the larger one. It is therefore better to implement such functions using prod-range (see the recipe Implementing common mathematical functions) rather than factorials. The combinatorial function is slightly more complex because you want to cancel out much of r and (n – r).
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from "XSLT Kochbuch" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights are reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de