Team

Hodrick-Prescott-Filter for Scala

hpFilterCode.html HTML, 36 KB

Dateiinhalt

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta charset="UTF-8">
<html>
<head>
<title>/Users/Krugman/Documents/[Paper]_Krug_(2013)/Code/hptest/HPfilter.scala</title>
<style>
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #408080; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #BC7A00 } /* Comment.Preproc */
.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #808080 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0040D0 } /* Generic.Traceback */
.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008000 } /* Keyword.Pseudo */
.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #B00040 } /* Keyword.Type */
.highlight .m { color: #666666 } /* Literal.Number */
.highlight .s { color: #BA2121 } /* Literal.String */
.highlight .na { color: #7D9029 } /* Name.Attribute */
.highlight .nb { color: #008000 } /* Name.Builtin */
.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
.highlight .no { color: #880000 } /* Name.Constant */
.highlight .nd { color: #AA22FF } /* Name.Decorator */
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0000FF } /* Name.Function */
.highlight .nl { color: #A0A000 } /* Name.Label */
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #19177C } /* Name.Variable */
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #666666 } /* Literal.Number.Float */
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
.highlight .sc { color: #BA2121 } /* Literal.String.Char */
.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #BA2121 } /* Literal.String.Double */
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight .sx { color: #008000 } /* Literal.String.Other */
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight .s1 { color: #BA2121 } /* Literal.String.Single */
.highlight .ss { color: #19177C } /* Literal.String.Symbol */
.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #19177C } /* Name.Variable.Class */
.highlight .vg { color: #19177C } /* Name.Variable.Global */
.highlight .vi { color: #19177C } /* Name.Variable.Instance */
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
.highlight * { font-family: monospace; }
.highlight { font-size: 90%; }
.highlight { line-height: normal; }
.highlight .err { border: none !important }
</style>
</head>
<body>
<div class="highlight"><pre><span class="lineno">  1</span> <span class="cm">/*</span>
<span class="lineno">  2</span> <span class="cm"> * Hodrick-Prescott-Filter</span>
<span class="lineno">  3</span> <span class="cm"> * copyright: Sebastian Krug (2014) and Kurt Annen (2004)</span>
<span class="lineno">  4</span> <span class="cm"> * Version:   1.0</span>
<span class="lineno">  5</span> <span class="cm"> * </span>
<span class="lineno">  6</span> <span class="cm"> * This code is based on the Java version of the HP-filter written by Kurt Annen in 2004.</span>
<span class="lineno">  7</span> <span class="cm"> * General usage (command line):</span>
<span class="lineno">  8</span> <span class="cm"> * 1. go to the folder consisting of this &quot;HPfilter.scala&quot;-file and the file containing the data (for example &quot;inputFile.csv&quot;)</span>
<span class="lineno">  9</span> <span class="cm"> * 2. compile the scala code with: scalac HPfilter.scala</span>
<span class="lineno"> 10</span> <span class="cm"> * 3. now type use it with: scala hpFilter /l 14400 /s:c /i inputFile.csv /o outputFile.csv</span>
<span class="lineno"> 11</span> <span class="cm"> *    or</span>
<span class="lineno"> 12</span> <span class="cm"> *    provide data as argument directly via: scala hpFilter /l 14400 /s:c /s 1.3 2.1 3.7 4.7 5.3 /o outputFile.csv</span>
<span class="lineno"> 13</span> <span class="cm"> * 4. If you need further info, type: scala hpFilter /help</span>
<span class="lineno"> 14</span> <span class="cm"> * </span>
<span class="lineno"> 15</span> <span class="cm">*/</span>
<span class="lineno"> 16</span> 
<span class="lineno"> 17</span> <span class="k">import</span> <span class="nn">java.io._</span>
<span class="lineno"> 18</span> <span class="k">import</span> <span class="nn">java.util._</span>
<span class="lineno"> 19</span> <span class="k">import</span> <span class="nn">collection.mutable._</span>
<span class="lineno"> 20</span> <span class="k">import</span> <span class="nn">scala.collection.JavaConversions._</span>
<span class="lineno"> 21</span> 
<span class="lineno"> 22</span> <span class="k">object</span> <span class="nc">hpFilter</span> <span class="o">{</span>
<span class="lineno"> 23</span> 
<span class="lineno"> 24</span>   <span class="k">var</span> <span class="n">filterData</span> <span class="k">=</span> <span class="kc">true</span>
<span class="lineno"> 25</span>   <span class="k">var</span> <span class="n">str</span>        <span class="k">=</span> <span class="s">&quot;&quot;</span>
<span class="lineno"> 26</span>   <span class="k">var</span> <span class="n">lambda</span>     <span class="k">=</span> <span class="mi">1600</span>
<span class="lineno"> 27</span>   <span class="k">var</span> <span class="n">inputFile</span>  <span class="k">=</span> <span class="s">&quot;&quot;</span>
<span class="lineno"> 28</span>   <span class="k">var</span> <span class="n">outputFile</span> <span class="k">=</span> <span class="s">&quot;&quot;</span>
<span class="lineno"> 29</span>   <span class="k">var</span> <span class="n">separator</span>  <span class="k">=</span> <span class="s">&quot;\n&quot;</span>
<span class="lineno"> 30</span> 
<span class="lineno"> 31</span> 
<span class="lineno"> 32</span> 
<span class="lineno"> 33</span>   <span class="cm">/** </span>
<span class="lineno"> 34</span> <span class="cm">   *    This function reads the data to filter and applies the HP-filter on it.</span>
<span class="lineno"> 35</span> <span class="cm">   *    */</span>
<span class="lineno"> 36</span>   <span class="k">def</span> <span class="n">main</span> <span class="o">(</span><span class="n">args</span><span class="k">:</span> <span class="kt">Array</span><span class="o">[</span><span class="kt">String</span><span class="o">])</span> <span class="o">{</span>
<span class="lineno"> 37</span>     <span class="k">val</span> <span class="n">writeOutputToFile</span> <span class="k">=</span> <span class="k">if</span><span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="n">contains</span><span class="o">(</span><span class="s">&quot;/o&quot;</span><span class="o">))</span> <span class="kc">true</span> <span class="k">else</span> <span class="kc">false</span>
<span class="lineno"> 38</span> 
<span class="lineno"> 39</span>     <span class="n">args</span><span class="o">.</span><span class="n">length</span> <span class="k">match</span> <span class="o">{</span>
<span class="lineno"> 40</span>         <span class="k">case</span> <span class="mi">0</span> <span class="k">=&gt;</span> <span class="n">printHelp</span>
<span class="lineno"> 41</span> 
<span class="lineno"> 42</span>         <span class="k">case</span> <span class="k">_</span> <span class="k">=&gt;</span>
<span class="lineno"> 43</span>           <span class="k">if</span><span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="n">contains</span><span class="o">(</span><span class="s">&quot;/h&quot;</span><span class="o">)</span> <span class="o">||</span> <span class="n">args</span><span class="o">.</span><span class="n">contains</span><span class="o">(</span><span class="s">&quot;/help&quot;</span><span class="o">))</span> <span class="n">printHelp</span> <span class="k">else</span> <span class="o">{</span>
<span class="lineno"> 44</span>             <span class="n">args</span><span class="o">.</span><span class="n">foreach</span><span class="o">{</span>
<span class="lineno"> 45</span>               <span class="n">i</span> <span class="k">=&gt;</span>
<span class="lineno"> 46</span>                 <span class="n">i</span> <span class="k">match</span> <span class="o">{</span>
<span class="lineno"> 47</span>                   <span class="k">case</span> <span class="s">&quot;/i&quot;</span>   <span class="k">=&gt;</span> <span class="n">inputFile</span>     <span class="k">=</span> <span class="n">args</span><span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="n">indexOf</span><span class="o">(</span><span class="n">i</span><span class="o">)</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span>
<span class="lineno"> 48</span>                   <span class="k">case</span> <span class="s">&quot;/l&quot;</span>   <span class="k">=&gt;</span> <span class="n">lambda</span>    <span class="k">=</span> <span class="n">args</span><span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="n">indexOf</span><span class="o">(</span><span class="n">i</span><span class="o">)</span> <span class="o">+</span> <span class="mi">1</span><span class="o">).</span><span class="n">toInt</span>
<span class="lineno"> 49</span>                   <span class="k">case</span> <span class="s">&quot;/s:f&quot;</span> <span class="k">=&gt;</span> <span class="n">separator</span> <span class="k">=</span> <span class="s">&quot; &quot;</span>
<span class="lineno"> 50</span>                   <span class="k">case</span> <span class="s">&quot;/s:n&quot;</span> <span class="k">=&gt;</span> <span class="n">separator</span> <span class="k">=</span> <span class="s">&quot;\n&quot;</span>
<span class="lineno"> 51</span>                   <span class="k">case</span> <span class="s">&quot;/s:c&quot;</span> <span class="k">=&gt;</span> <span class="n">separator</span> <span class="k">=</span> <span class="s">&quot;, &quot;</span>
<span class="lineno"> 52</span>                   <span class="k">case</span> <span class="s">&quot;/s:s&quot;</span> <span class="k">=&gt;</span> <span class="n">separator</span> <span class="k">=</span> <span class="s">&quot;; &quot;</span>
<span class="lineno"> 53</span>                   <span class="k">case</span> <span class="s">&quot;/s&quot;</span>   <span class="k">=&gt;</span>
<span class="lineno"> 54</span>                     <span class="k">var</span> <span class="n">firstIndexOfInputStream</span>    <span class="k">=</span> <span class="n">args</span><span class="o">.</span><span class="n">indexOf</span><span class="o">(</span><span class="n">i</span><span class="o">)</span> <span class="o">+</span> <span class="mi">1</span>
<span class="lineno"> 55</span>                     <span class="k">var</span> <span class="n">firstIndexAfterInputStream</span> <span class="k">=</span> <span class="k">if</span><span class="o">(</span><span class="n">writeOutputToFile</span><span class="o">)</span> <span class="n">args</span><span class="o">.</span><span class="n">length</span> <span class="o">-</span> <span class="mi">2</span> <span class="k">else</span> <span class="n">args</span><span class="o">.</span><span class="n">length</span>
<span class="lineno"> 56</span>                     <span class="n">outputFile</span> <span class="k">=</span> <span class="k">if</span><span class="o">(</span><span class="n">writeOutputToFile</span><span class="o">)</span> <span class="n">args</span><span class="o">.</span><span class="n">last</span> <span class="k">else</span> <span class="s">&quot;&quot;</span>
<span class="lineno"> 57</span>                     <span class="n">args</span><span class="o">.</span><span class="n">slice</span><span class="o">(</span><span class="n">firstIndexOfInputStream</span><span class="o">,</span> <span class="n">firstIndexAfterInputStream</span><span class="o">).</span><span class="n">foreach</span><span class="o">(</span><span class="n">x</span> <span class="k">=&gt;</span> <span class="n">str</span> <span class="o">+=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">separator</span><span class="o">)</span>
<span class="lineno"> 58</span>                   <span class="k">case</span> <span class="s">&quot;/o&quot;</span> <span class="k">=&gt;</span> <span class="n">outputFile</span> <span class="k">=</span> <span class="n">args</span><span class="o">.</span><span class="n">last</span>
<span class="lineno"> 59</span>                   <span class="k">case</span> <span class="k">_</span>    <span class="k">=&gt;</span>
<span class="lineno"> 60</span>                 <span class="o">}</span>
<span class="lineno"> 61</span>             <span class="o">}</span>
<span class="lineno"> 62</span>           <span class="o">}</span>
<span class="lineno"> 63</span>     <span class="o">}</span>
<span class="lineno"> 64</span> 
<span class="lineno"> 65</span> 
<span class="lineno"> 66</span>     <span class="k">val</span> <span class="n">buffer</span> <span class="k">=</span> <span class="nc">Array</span><span class="o">.</span><span class="n">ofDim</span><span class="o">[</span><span class="kt">Byte</span><span class="o">](</span><span class="mi">400000</span><span class="o">)</span>
<span class="lineno"> 67</span>     <span class="k">if</span><span class="o">(</span><span class="n">filterData</span><span class="o">)</span> <span class="o">{</span>
<span class="lineno"> 68</span>       <span class="k">try</span> <span class="o">{</span>
<span class="lineno"> 69</span>         <span class="k">if</span><span class="o">(</span><span class="n">inputFile</span> <span class="o">!=</span> <span class="s">&quot;&quot;</span><span class="o">)</span> <span class="o">{</span>
<span class="lineno"> 70</span>           <span class="k">if</span><span class="o">(</span><span class="n">writeOutputToFile</span><span class="o">)</span> <span class="n">println</span><span class="o">(</span><span class="n">s</span><span class="s">&quot;HP-filtered data of $inputFile saved to $outputFile&quot;</span><span class="o">)</span> <span class="k">else</span> <span class="n">println</span><span class="o">(</span><span class="n">s</span><span class="s">&quot;HP-filter data of $inputFile yields&quot;</span><span class="o">)</span>
<span class="lineno"> 71</span>           <span class="k">var</span> <span class="n">in</span>   <span class="k">=</span> <span class="k">new</span> <span class="nc">FileInputStream</span><span class="o">(</span><span class="n">inputFile</span><span class="o">)</span>
<span class="lineno"> 72</span>           <span class="k">val</span> <span class="n">len</span>  <span class="k">=</span> <span class="n">in</span><span class="o">.</span><span class="n">read</span><span class="o">(</span><span class="n">buffer</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">400000</span><span class="o">)</span>
<span class="lineno"> 73</span>           <span class="k">val</span> <span class="n">stri</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">String</span><span class="o">(</span><span class="n">buffer</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="n">len</span><span class="o">)</span>
<span class="lineno"> 74</span>           <span class="n">str</span> <span class="k">=</span> <span class="n">stri</span>
<span class="lineno"> 75</span>           <span class="n">in</span><span class="o">.</span><span class="n">close</span><span class="o">()</span>
<span class="lineno"> 76</span>         <span class="o">}</span>
<span class="lineno"> 77</span>         <span class="k">val</span> <span class="n">datame</span> <span class="k">=</span> <span class="n">str</span><span class="o">.</span><span class="n">split</span><span class="o">(</span><span class="n">separator</span><span class="o">)</span>
<span class="lineno"> 78</span>         <span class="k">val</span> <span class="n">N</span>      <span class="k">=</span> <span class="n">datame</span><span class="o">.</span><span class="n">size</span>
<span class="lineno"> 79</span>         <span class="k">var</span> <span class="n">data</span>   <span class="k">=</span> <span class="n">datame</span><span class="o">.</span><span class="n">map</span><span class="o">(</span><span class="k">_</span><span class="o">.</span><span class="n">toDouble</span><span class="o">)</span>
<span class="lineno"> 80</span> 
<span class="lineno"> 81</span>         <span class="k">val</span> <span class="n">a</span> <span class="k">=</span> <span class="nc">ArrayBuffer</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">lambda</span><span class="o">,</span> <span class="mi">5</span> <span class="o">*</span> <span class="n">lambda</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">++=</span> <span class="nc">Array</span><span class="o">.</span><span class="n">fill</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span><span class="n">N</span><span class="o">-</span><span class="mi">4</span><span class="o">)(</span><span class="mi">6</span> <span class="o">*</span> <span class="n">lambda</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">++=</span> <span class="nc">Array</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span><span class="mi">5</span> <span class="o">*</span> <span class="n">lambda</span> <span class="o">+</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">lambda</span><span class="o">)</span>
<span class="lineno"> 82</span>         <span class="k">val</span> <span class="n">b</span> <span class="k">=</span> <span class="nc">ArrayBuffer</span><span class="o">[</span><span class="kt">Double</span><span class="o">](-</span><span class="mi">2</span> <span class="o">*</span> <span class="n">lambda</span><span class="o">)</span> <span class="o">++=</span> <span class="nc">Array</span><span class="o">.</span><span class="n">fill</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span><span class="n">N</span><span class="o">-</span><span class="mi">3</span><span class="o">)(-</span><span class="mi">4</span> <span class="o">*</span> <span class="n">lambda</span><span class="o">)</span> <span class="o">++=</span> <span class="nc">Array</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span> <span class="o">-</span><span class="mi">2</span> <span class="o">*</span> <span class="n">lambda</span><span class="o">,</span> <span class="mi">0</span><span class="o">)</span>
<span class="lineno"> 83</span>         <span class="k">val</span> <span class="n">c</span> <span class="k">=</span> <span class="nc">ArrayBuffer</span><span class="o">[</span><span class="kt">Double</span><span class="o">]()</span> <span class="o">++=</span> <span class="nc">Array</span><span class="o">.</span><span class="n">fill</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span><span class="n">N</span><span class="o">-</span><span class="mi">2</span><span class="o">)(</span><span class="n">lambda</span><span class="o">)</span> <span class="o">++=</span> <span class="nc">Array</span><span class="o">[</span><span class="kt">Double</span><span class="o">](</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">0</span><span class="o">)</span>
<span class="lineno"> 84</span>         
<span class="lineno"> 85</span>         <span class="n">data</span> <span class="k">=</span> <span class="n">pentas</span><span class="o">(</span><span class="n">a</span><span class="o">,</span> <span class="n">b</span><span class="o">,</span> <span class="n">c</span><span class="o">,</span> <span class="n">data</span><span class="o">,</span> <span class="n">N</span><span class="o">)</span>
<span class="lineno"> 86</span> 
<span class="lineno"> 87</span>         <span class="k">if</span><span class="o">(</span><span class="n">writeOutputToFile</span> <span class="o">==</span> <span class="kc">false</span><span class="o">)</span> <span class="n">println</span><span class="o">(</span><span class="n">data</span><span class="o">.</span><span class="n">mkString</span><span class="o">(</span><span class="n">separator</span><span class="o">))</span> <span class="k">else</span> <span class="o">{</span>
<span class="lineno"> 88</span>           <span class="k">val</span> <span class="n">out</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">FileWriter</span><span class="o">(</span><span class="n">outputFile</span><span class="o">)</span>
<span class="lineno"> 89</span>           <span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="o">(</span><span class="n">data</span><span class="o">.</span><span class="n">mkString</span><span class="o">(</span><span class="n">separator</span><span class="o">))</span>
<span class="lineno"> 90</span>           <span class="n">out</span><span class="o">.</span><span class="n">close</span><span class="o">()</span>          
<span class="lineno"> 91</span>         <span class="o">}</span>
<span class="lineno"> 92</span>         
<span class="lineno"> 93</span>       <span class="o">}</span> <span class="k">catch</span> <span class="o">{</span>
<span class="lineno"> 94</span>         <span class="k">case</span> <span class="n">e</span><span class="k">:</span> <span class="kt">Exception</span> <span class="o">=&gt;</span> <span class="n">println</span><span class="o">(</span><span class="n">e</span><span class="o">.</span><span class="n">toString</span><span class="o">)</span>
<span class="lineno"> 95</span>       <span class="o">}</span>
<span class="lineno"> 96</span>     <span class="o">}</span>
<span class="lineno"> 97</span>   <span class="o">}</span>
<span class="lineno"> 98</span> 
<span class="lineno"> 99</span> 
<span class="lineno">100</span> 
<span class="lineno">101</span>   <span class="cm">/** </span>
<span class="lineno">102</span> <span class="cm">   *    This function just prints some helping information.</span>
<span class="lineno">103</span> <span class="cm">   *    */</span>
<span class="lineno">104</span>   <span class="k">def</span> <span class="n">printHelp</span> <span class="o">{</span>
<span class="lineno">105</span>     <span class="n">filterData</span> <span class="k">=</span> <span class="kc">false</span>
<span class="lineno">106</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; \n&quot;</span><span class="o">)</span>
<span class="lineno">107</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;Scala - Hodrick-Prescott-Filter (Version 1.0) \t [Copyright (c) Sebastian Krug (2014)]\n&quot;</span><span class="o">)</span>
<span class="lineno">108</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;scala hp [/switch] [/input] [/output]&quot;</span><span class="o">)</span>
<span class="lineno">109</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; /i \t - reads data to filter from input file&quot;</span><span class="o">)</span>
<span class="lineno">110</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; /o \t - save filtered data in output file&quot;</span><span class="o">)</span>
<span class="lineno">111</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; /s \t - reads data to filter from Stream&quot;</span><span class="o">)</span>
<span class="lineno">112</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; /l \t - Lambda (standard is 1600) &quot;</span><span class="o">)</span>
<span class="lineno">113</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; /h \t - prints help text&quot;</span><span class="o">)</span>
<span class="lineno">114</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; /s:x \t - separator (standard is new line) &quot;</span><span class="o">)</span>
<span class="lineno">115</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;\t   n - new lines&quot;</span><span class="o">)</span>
<span class="lineno">116</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;\t   f - spaces&quot;</span><span class="o">)</span>
<span class="lineno">117</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;\t   c - commas&quot;</span><span class="o">)</span>
<span class="lineno">118</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;\t   s - Semi-colons\n&quot;</span><span class="o">)</span>
<span class="lineno">119</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;Examples for usage:&quot;</span><span class="o">)</span>
<span class="lineno">120</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;scala hpFilter /l 14400 /s:c /i data.csv /o hpdata.csv&quot;</span><span class="o">)</span>
<span class="lineno">121</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;scala hpFilter /l 14400 /s:n /s 1.3 2.1 3.7 4.7 5.3 /o hpdata.txt\n&quot;</span><span class="o">)</span>
<span class="lineno">122</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;Hint: Without the switch /o you can redirect the output with \&quot;&gt;&gt;\&quot;.&quot;</span><span class="o">)</span>
<span class="lineno">123</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot;For questions, mail me to krug84@gmail.com.&quot;</span><span class="o">)</span>
<span class="lineno">124</span>     <span class="n">println</span><span class="o">(</span><span class="s">&quot; \n&quot;</span><span class="o">)</span>
<span class="lineno">125</span>   <span class="o">}</span>
<span class="lineno">126</span> 
<span class="lineno">127</span> 
<span class="lineno">128</span> 
<span class="lineno">129</span> 
<span class="lineno">130</span>   <span class="cm">/** </span>
<span class="lineno">131</span> <span class="cm">   * This function solves the linear equation system BxX=Y with B being a pentadiagonal matrix.</span>
<span class="lineno">132</span> <span class="cm">   */</span>
<span class="lineno">133</span>   <span class="k">def</span> <span class="n">pentas</span> <span class="o">(</span><span class="n">a</span><span class="k">:</span><span class="kt">ArrayBuffer</span><span class="o">[</span><span class="kt">Double</span><span class="o">],</span> <span class="n">b</span><span class="k">:</span><span class="kt">ArrayBuffer</span><span class="o">[</span><span class="kt">Double</span><span class="o">],</span> <span class="n">c</span><span class="k">:</span><span class="kt">ArrayBuffer</span><span class="o">[</span><span class="kt">Double</span><span class="o">],</span> <span class="n">data</span><span class="k">:</span><span class="kt">Array</span><span class="o">[</span><span class="kt">Double</span><span class="o">],</span> <span class="n">N</span><span class="k">:</span><span class="kt">Int</span><span class="o">)</span><span class="k">:</span><span class="kt">Array</span><span class="o">[</span><span class="kt">Double</span><span class="o">]</span> <span class="k">=</span> <span class="o">{</span>
<span class="lineno">134</span>     <span class="k">var</span> <span class="n">H1</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">135</span>     <span class="k">var</span> <span class="n">H2</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">136</span>     <span class="k">var</span> <span class="n">H3</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">137</span>     <span class="k">var</span> <span class="n">H4</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">138</span>     <span class="k">var</span> <span class="n">H5</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">139</span>     <span class="k">var</span> <span class="nc">HH1</span> <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">140</span>     <span class="k">var</span> <span class="nc">HH2</span> <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">141</span>     <span class="k">var</span> <span class="nc">HH3</span> <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">142</span>     <span class="k">var</span> <span class="nc">HH5</span> <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">143</span>     <span class="k">var</span> <span class="n">Z</span>   <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">144</span>     <span class="k">var</span> <span class="nc">HB</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">145</span>     <span class="k">var</span> <span class="nc">HC</span>  <span class="k">=</span> <span class="mf">0.0</span>
<span class="lineno">146</span> 
<span class="lineno">147</span>     <span class="k">for</span><span class="o">(</span><span class="n">x</span> <span class="k">&lt;-</span> <span class="mi">0</span> <span class="n">until</span> <span class="n">N</span><span class="o">){</span>
<span class="lineno">148</span>       <span class="n">Z</span>    <span class="k">=</span> <span class="n">a</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">-</span> <span class="n">H4</span> <span class="o">*</span> <span class="n">H1</span> <span class="o">-</span> <span class="nc">HH5</span> <span class="o">*</span> <span class="nc">HH2</span>
<span class="lineno">149</span>       <span class="nc">HB</span>   <span class="k">=</span> <span class="n">b</span><span class="o">(</span><span class="n">x</span><span class="o">)</span>
<span class="lineno">150</span>       <span class="nc">HH1</span>  <span class="k">=</span> <span class="n">H1</span>
<span class="lineno">151</span>       <span class="n">H1</span>   <span class="k">=</span> <span class="o">(</span><span class="nc">HB</span> <span class="o">-</span> <span class="n">H4</span> <span class="o">*</span> <span class="n">H2</span><span class="o">)</span> <span class="o">/</span> <span class="n">Z</span>
<span class="lineno">152</span>       <span class="n">b</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="k">=</span> <span class="n">H1</span>
<span class="lineno">153</span>       <span class="nc">HC</span>   <span class="k">=</span> <span class="n">c</span><span class="o">(</span><span class="n">x</span><span class="o">)</span>
<span class="lineno">154</span>       <span class="nc">HH2</span>  <span class="k">=</span> <span class="n">H2</span>
<span class="lineno">155</span>       <span class="n">H2</span>   <span class="k">=</span> <span class="nc">HC</span> <span class="o">/</span> <span class="n">Z</span>
<span class="lineno">156</span>       <span class="n">c</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="k">=</span> <span class="n">H2</span>
<span class="lineno">157</span>       <span class="n">a</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="k">=</span> <span class="o">(</span><span class="n">data</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">-</span> <span class="nc">HH3</span> <span class="o">*</span> <span class="nc">HH5</span> <span class="o">-</span> <span class="n">H3</span> <span class="o">*</span> <span class="n">H4</span><span class="o">)</span> <span class="o">/</span> <span class="n">Z</span>
<span class="lineno">158</span>       <span class="nc">HH3</span>  <span class="k">=</span> <span class="n">H3</span>
<span class="lineno">159</span>       <span class="n">H3</span>   <span class="k">=</span> <span class="n">a</span><span class="o">(</span><span class="n">x</span><span class="o">)</span>
<span class="lineno">160</span>       <span class="n">H4</span>   <span class="k">=</span> <span class="nc">HB</span> <span class="o">-</span> <span class="n">H5</span> <span class="o">*</span> <span class="nc">HH1</span>
<span class="lineno">161</span>       <span class="nc">HH5</span>  <span class="k">=</span> <span class="n">H5</span>
<span class="lineno">162</span>       <span class="n">H5</span>   <span class="k">=</span> <span class="nc">HC</span> 
<span class="lineno">163</span>     <span class="o">}</span>
<span class="lineno">164</span>     <span class="n">H2</span> <span class="k">=</span> <span class="mi">0</span>
<span class="lineno">165</span>     <span class="n">H1</span> <span class="k">=</span> <span class="n">a</span><span class="o">(</span><span class="n">N</span> <span class="o">-</span> <span class="mi">1</span><span class="o">)</span>
<span class="lineno">166</span>     <span class="n">data</span><span class="o">(</span><span class="n">N</span> <span class="o">-</span> <span class="mi">1</span><span class="o">)</span> <span class="k">=</span> <span class="n">H1</span>
<span class="lineno">167</span>     <span class="k">for</span><span class="o">(</span><span class="n">x</span> <span class="k">&lt;-</span> <span class="n">N</span><span class="o">-</span><span class="mi">2</span> <span class="n">to</span> <span class="mi">0</span> <span class="n">by</span> <span class="o">-</span><span class="mi">1</span><span class="o">){</span>
<span class="lineno">168</span>       <span class="n">data</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="k">=</span> <span class="n">a</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">-</span> <span class="n">b</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">*</span> <span class="n">H1</span> <span class="o">-</span> <span class="n">c</span><span class="o">(</span><span class="n">x</span><span class="o">)</span> <span class="o">*</span> <span class="n">H2</span>
<span class="lineno">169</span>       <span class="n">H2</span> <span class="k">=</span> <span class="n">H1</span>
<span class="lineno">170</span>       <span class="n">H1</span> <span class="k">=</span> <span class="n">data</span><span class="o">(</span><span class="n">x</span><span class="o">)</span>
<span class="lineno">171</span>     <span class="o">}</span>
<span class="lineno">172</span>     <span class="n">data</span>
<span class="lineno">173</span>   <span class="o">}</span>
<span class="lineno">174</span> 
<span class="lineno">175</span> 
<span class="lineno">176</span> <span class="o">}</span>
</pre></div>

</body>
</html>