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 &lt; 0 or $r &lt; 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 &lt; 0 or $r &lt; 0">NaN</xsl:when>
    <xsl:when test="$n = 0">0</xsl:when>
    <xsl:otherwise>
      <xsl:variable name="min" select="($r &lt;= $n - $r) * $r + ($r &gt; $n - $r) * $n - $r"/>
      <xsl:variable name="max" select="($r &gt;= $n - $r) * $r + ($r &lt; $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).

  

 

 

 

back to top