Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
(→‎Joy: add)
(Merge RPL/2 and RPL, CLEAR added to first implementation to make it always work)
Line 3,220: Line 3,220:


=={{header|RPL}}==
=={{header|RPL}}==
This is a simple rewrite of the dc version above. This works on an HP 48. "→" is a single right arrow character on the 48. Feel free to alter this code as necessary to work on RPL/2.
{{works with|Halcyon Calc|4.2.7}}

<syntaxhighlight lang="rpl/2">1 2 3 5 7
AMEAN
<< CLEAR DEPTH DUP 'N' STO →LIST ΣLIST N / >>
3.6</syntaxhighlight>

===Hard-working approach===
===Hard-working approach===
Works for all RPL versions.
≪ DUP SIZE SWAP OVER
≪ DUP SIZE SWAP OVER
0 1 ROT FOR j
0 1 ROT '''FOR''' j
OVER j GET +
OVER j GET + '''NEXT'''
NEXT
ROT / SWAP DROP
ROT / SWAP DROP
Line 3,231: Line 3,237:
No significant impact on program size or speed, but much more readable
No significant impact on program size or speed, but much more readable
≪ DUP SIZE → vector n
≪ DUP SIZE → vector n
≪ 0 1 n FOR j
≪ 0 1 n '''FOR''' j
vector j GET +
vector j GET + '''NEXT'''
NEXT
n /
n /
≫ ≫
≫ ≫
Line 3,239: Line 3,244:
The dot product of any vector with [1 1 ... 1] gives the sum of its elements.
The dot product of any vector with [1 1 ... 1] gives the sum of its elements.
≪ SIZE LAST DUP 1 CON DOT SWAP / ≫
≪ SIZE LAST DUP 1 CON DOT SWAP / ≫
'AMEAN' STO
''''AMEAN'''' STO


===Using built-in statistics features===
===Using built-in statistics features===
Most of the code is dedicated to store the input array according to built-in statistics requirements, which requires a matrix with one line per record. Main benefit of this approach is that you can then easily calculate standard deviation and variance by calling resp. <code>SDEV</code> and <code>VAR</code> functions.
Most of the code is dedicated to store the input array according to built-in statistics requirements, which requires a matrix with one line per record. Main benefit of this approach is that you can then easily calculate standard deviation and variance by calling resp. <code>SDEV</code> and <code>VAR</code> functions.
≪ { 1 } OVER SIZE + RDM TRN '∑DAT' STO MEAN ≫
≪ { 1 } OVER SIZE + RDM TRN '∑DAT' STO MEAN ≫ ''''AMEAN'''' STO
'AMEAN' STO


[ 1 5 0 -4 6 ] AMEAN
[ 1 5 0 -4 6 ] '''AMEAN'''
{{out}}
{{out}}
<pre>
<pre>
1: 1.6
1: 1.6
</pre>
</pre>

=={{header|RPL/2}}==

This is a simple rewrite of the dc version above. This works on an HP 48. "→" is a single right arrow character on the 48. Feel free to alter this code as necessary to work on RPL/2.

<syntaxhighlight lang="rpl/2">1 2 3 5 7
AMEAN
<< DEPTH DUP 'N' STO →LIST ΣLIST N / >>
3.6</syntaxhighlight>


=={{header|Ruby}}==
=={{header|Ruby}}==