Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: c478971ca46441633f82c286242843b9dd1688f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?xml version="1.0"?>
<!--
  Copyright (C) 2018 R-T Specialty, LLC.

  This file is part of tame-core.

  tame-core is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<package xmlns="http://www.lovullo.com/rater"
         xmlns:c="http://www.lovullo.com/calc"
         xmlns:t="http://www.lovullo.com/rater/apply-template"
         core="true"
         desc="Folding and Unfolding of Vectors">

  <import package="../base" />
  <import package="filter" export="true" />


  The term ``fold'' is also referred to as ``reduce''---%
    they are synonymous.
  Unless otherwise specified,
    folding occurs left-to-right.

  \emph{Unfolding} is the opposite of a reduction---%
    it \emph{generates} values from existing values.


  <section title="Matrix Folding">
    \ref{_fold-matrix_} folds a matrix into a vector by summing each of
      the~rows.

    <template name="_fold-matrix_"
              desc="Folds matrix into a vector by summing rows">
      <param name="@name@"      desc="Matrix to fold" />
      <param name="@class@"     desc="Iteration vector of desired length" />
      <param name="@generates@" desc="Generator name (to yield)" />

      <param name="@yields@" desc="Yield name">
        <text></text>
      </param>


      <rate-each class="@class@" yields="@yields@"
                 generates="@generates@" index="k">
        <c:let>
          <c:values>
            <c:value name="row" type="float" set="vector"
                     desc="Matrix row">
              <c:value-of name="@name@" index="k" />
            </c:value>
          </c:values>

          <c:sum of="row" />
        </c:let>
      </rate-each>
    </template>
  </section>


  <section title="Matrix Generation">
    \ref{_unfold-vector-grouped_} generates a matrix from a vector---%
      that is, it generates vectors within a vector---%
      by grouping values.

    The \tt{@class@} is used both as a predicate and as a determination of
      the resulting vector's length
        (the~number of rows in the resulting matrix).
    If non-matching,
      no columns will be produced for that respective row.

    \tt{@src@} is the vector to be unfolded,
      containing the raw values to be grouped.

    \tt{@grouping@} \should be the same length as~\ref{@src@} and determines
      the group~(row) in which the respective value should appear.

    \ref{@generates@} names the resulting matrix and~\ref{@desc@} provides
      its description.

    <template name="_unfold-vector-grouped_"
              desc="Unfold vector into a matrix by grouping">
      <param name="@class@"     desc="Iteration vector of desired length" />
      <param name="@src@"       desc="Source vector" />
      <param name="@grouping@"  desc="Grouping vector" />
      <param name="@generates@" desc="Generator name (to yield)" />

      <param name="@desc@" desc="Generator description">
        <text>Unfolded vector </text>
        <param-value name="@src@" />
        <text> grouped by </text>
        <param-value name="@grouping@" />
      </param>

      <param name="@lengthv@" desc="Length vector (of desired length)">
        <param-class-to-yields name="@class@" />
      </param>


      <rate yields="_{@generates@}">
        <c:sum of="@lengthv@" dim="matrix"
               generates="@generates@" index="k"
               desc="@desc@">
          <c:cases>
            <c:case label="Unfold on class vector match">
              <c:when name="@lengthv@" index="k" />
              <c:apply name="vfilter_lookup"
                       vector_pred="@grouping@" vector_src="@src@"
                       value="k"
                       start_index="#0" />
            </c:case>

            <c:otherwise label="Ignore on class vector non-match">
              <c:vector />
            </c:otherwise>
          </c:cases>
        </c:sum>
      </rate>
    </template>
  </section>
</package>