Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-02-13 14:38:08 -0500
committerMike Gerwitz <mike.gerwitz@rtspecialty.com>2019-02-13 14:38:08 -0500
commit46b7c234dda2d87802337d1337bc3b87da966303 (patch)
treed13f2cef2dfa1d4ca70bc60af53c22a1745fd740 /core
parentbd73ea0121f0f67480101ede75c6aac78f6316b8 (diff)
downloadtame-46b7c234dda2d87802337d1337bc3b87da966303.tar.gz
tame-46b7c234dda2d87802337d1337bc3b87da966303.tar.bz2
tame-46b7c234dda2d87802337d1337bc3b87da966303.zip
core: vector/minmax/_minreduce_: New template
* test/core/suite.xml: Import `test/core/vector/minmax'. * test/core/vector/minmax.xml: New package. * vector/minmax.xml (_minreduce_, _minreduce): New template, function.
Diffstat (limited to 'core')
-rw-r--r--core/test/core/suite.xml1
-rw-r--r--core/test/core/vector/minmax.xml72
-rw-r--r--core/vector/minmax.xml98
3 files changed, 168 insertions, 3 deletions
diff --git a/core/test/core/suite.xml b/core/test/core/suite.xml
index 1375934..146ef2e 100644
--- a/core/test/core/suite.xml
+++ b/core/test/core/suite.xml
@@ -33,6 +33,7 @@
<import package="vector/fold" />
<import package="vector/interpolate" />
<import package="vector/length" />
+ <import package="vector/minmax" />
<import package="vector/table" />
<import package="aggregate" />
diff --git a/core/test/core/vector/minmax.xml b/core/test/core/vector/minmax.xml
new file mode 100644
index 0000000..eadcec7
--- /dev/null
+++ b/core/test/core/vector/minmax.xml
@@ -0,0 +1,72 @@
+<?xml version="1.0"?>
+<!--
+ Copyright (C) 2014-2019 Ryan Specialty Group, 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"
+ desc="Testing minimum and maximum values of vectors">
+
+ <import package="../../spec" />
+
+ <import package="../../../base" />
+ <import package="../../../vector/minmax" />
+
+
+ <t:describe name="_minreduce_">
+ <t:it desc="reduces empty vector to 0">
+ <t:given>
+ <t:minreduce>
+ </t:minreduce>
+ </t:given>
+
+ <t:expect>
+ <t:match-result eq="0" />
+ </t:expect>
+ </t:it>
+
+
+ <t:it desc="reduces one-element vector to its only element">
+ <t:given>
+ <t:minreduce>
+ <c:value-of name="#3" />
+ </t:minreduce>
+ </t:given>
+
+ <t:expect>
+ <t:match-result eq="3" />
+ </t:expect>
+ </t:it>
+
+
+ <t:it desc="reduces n-element vector to its minimum">
+ <t:given>
+ <t:minreduce>
+ <c:value-of name="#7" />
+ <c:value-of name="#3" />
+ <c:value-of name="#1" />
+ <c:value-of name="#6" />
+ </t:minreduce>
+ </t:given>
+
+ <t:expect>
+ <t:match-result eq="1" />
+ </t:expect>
+ </t:it>
+ </t:describe>
+</package>
diff --git a/core/vector/minmax.xml b/core/vector/minmax.xml
index 0a28d4f..9176384 100644
--- a/core/vector/minmax.xml
+++ b/core/vector/minmax.xml
@@ -24,14 +24,104 @@
title="Maximum and Minimum Elements">
<import package="../base" />
+ <import package="../when" />
<import package="../numeric/common" />
<import package="../numeric/minmax" />
<section title="Vector Reduction">
- Core currently only offers a~maximum reduction on
- a~vector. \todo{Add a~minimum reduction.} \ref{_maxreduce_}
- provides a convenient template-based abstraction.
+ Two types of reductions are provided for minimum and maximum respectively:
+ \ref{_minreduce_} and \ref{_maxreduce_}.\footnote{
+ This is because TAME does not have first-class functions.}
+ They both produce scalar values of the minimums and maximums
+ (respectively) of vectors.
+
+ <template name="_minreduce_"
+ desc="Reduce a vector to its minimum">
+ <param name="@values@" desc="Values to reduce" />
+ <param name="@isvector@" desc="Set to 'true' if the nodes should
+ not be wrapped in c:vector" />
+
+ <param name="@label@" desc="Application label">
+ <!-- default empty -->
+ <text></text>
+ </param>
+
+
+ <c:apply name="_minreduce" label="@label@">
+ <c:arg name="vector">
+ <if name="@isvector@" eq="true">
+ <param-copy name="@values@" />
+ </if>
+ <unless name="@isvector@" eq="true">
+ <c:vector>
+ <param-copy name="@values@" />
+ </c:vector>
+ </unless>
+ </c:arg>
+ </c:apply>
+ </template>
+
+
+ <function name="_minreduce"
+ desc="Minimum value in a vector">
+ <param name="vector" type="float" set="vector"
+ desc="Vector to find minimum of" />
+
+ <c:let>
+ <c:values>
+ <c:value name="length" type="integer"
+ desc="Length of vector">
+ <c:length-of>
+ <c:value-of name="vector" />
+ </c:length-of>
+ </c:value>
+ </c:values>
+
+ <c:cases>
+ <c:case label="Empty vector">
+ <t:when-eq name="length" value="#0" />
+
+ <c:value-of name="#0" />
+ </c:case>
+
+
+ <c:case label="Single-element vector">
+ <t:when-eq name="length" value="#1" />
+
+ <c:value-of name="vector">
+ <c:index>
+ <c:value-of name="#0" />
+ </c:index>
+ </c:value-of>
+ </c:case>
+
+
+ <c:otherwise label="Non-empty vector">
+ <c:apply name="min">
+ <c:arg name="min1">
+ <c:value-of name="vector">
+ <c:index>
+ <c:value-of name="#0" />
+ </c:index>
+ </c:value-of>
+ </c:arg>
+
+ <c:arg name="min2">
+ <c:recurse>
+ <c:arg name="vector">
+ <c:cdr>
+ <c:value-of name="vector" />
+ </c:cdr>
+ </c:arg>
+ </c:recurse>
+ </c:arg>
+ </c:apply>
+ </c:otherwise>
+ </c:cases>
+ </c:let>
+ </function>
+
<template name="_maxreduce_"
desc="Reduce a set to its maximum">
@@ -72,6 +162,8 @@
let~expressions and other convenience templates. It has since
been refactored slightly, but can be made to be more concise.}
+ <!-- TODO: rewrite this to be more concise, with the more lisp-like
+ recursive strategy of minreduce -->
<function name="maxreduce" desc="Reduce a set to its maximum">
<param name="maxreduce_set" type="float" set="vector"
desc="Set to find max of" />