我的代码返回了一个值而不是一个向量

时间:2019-07-20 10:14:58

标签: r

这是代码。我试图找出几何级数的总和。对于n的4个值,它应该返回4个值,但仅返回一个带有警告消息的值。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"

    version="3.0">

    <xsl:mode streamable="yes" on-no-match="shallow-copy"/>

    <xsl:template match="source">
        <xsl:copy>
            <xsl:apply-templates select="main!copy-of()" mode="main"/>
        </xsl:copy>
    </xsl:template>

    <xsl:output indent="yes"/>

    <xsl:function name="mf:date" as="xs:date">
        <xsl:param name="input-date" as="xs:string"/>
        <xsl:sequence
            select="xs:date(replace($input-date, '([0-9]{4})([0-9]{2})([0-9]{2})', '$1-$2-$3'))"/>
    </xsl:function>

    <xsl:function name="mf:select-valid-info" as="element()*">
        <xsl:param name="infos" as="element()*"/>
        <xsl:sequence
            select="$infos[name/normalize-space()
            and mf:date(start_date) lt current-date()
            and mf:date(end_date) gt current-date()]"/>
    </xsl:function>

    <xsl:function name="mf:valid-main" as="xs:boolean">
        <xsl:param name="main" as="element(main)"/>
        <xsl:sequence
            select="let $valid-blocks := mf:select-valid-info($main/block_information),
            $valid-cities := mf:select-valid-info($main/city_information)
            return count($valid-blocks) eq 1 and count($valid-cities) eq 1"/>
    </xsl:function>

    <xsl:mode name="main" on-no-match="shallow-copy"/>

    <xsl:template match="main[not(mf:valid-main(.))]" mode="main"/>

    <xsl:template match="main[mf:valid-main(.)]" mode="main">
        <xsl:copy>
            <xsl:apply-templates 
                select="id,
                mf:select-valid-info(block_information)/name,
                mf:select-valid-info(city_information)/name,
                phone" mode="#current"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="block_information/name | city_information/name" mode="main">
        <xsl:element name="{substring-before(local-name(..), '_')}_name">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="main/phone[type = 'C']" mode="main">
        <contact_phone>
            <xsl:value-of select="number[current()/normalize-space(name)]"/>
        </contact_phone>
        <contact_name>
            <xsl:value-of select="name"/>
        </contact_name>
    </xsl:template>

    <xsl:template match="main/phone[type = 'P']" mode="main">
        <phone>
            <xsl:value-of select="number"/>
        </phone>
    </xsl:template>

    <xsl:template match="main/phone[type = 'M']" mode="main">
        <cellphone>
            <xsl:value-of select="number"/>
        </cellphone>
    </xsl:template>

</xsl:stylesheet>
  

警告信息:   在1:n中:数值表达式包含3个元素:仅第一个使用`

1 个答案:

答案 0 :(得分:1)

更直接的方法是仅使用cumsum对序列求和,并采用向量n表示的值。因此,rn的定义如上...

cumsum(r^(1:max(n)))[n]    #i.e. sum 40 values and take 10th, 20th, 30th and 40th of them

[1]  13.97164  38.99273  83.80168 164.04768
相关问题