Anonymous user
Averages/Root mean square: Difference between revisions
m
→{{header|Elena}}
m (→{{header|R}}) |
imported>Arakov |
||
(17 intermediate revisions by 12 users not shown) | |||
Line 1:
{{task}}
;Task
Compute the [[wp:Root mean square|Root mean square]] of the numbers 1..10.
Line 14:
{{Related tasks/Statistical measures}}
Line 22:
=={{header|11l}}==
{{trans|Python}}
<
R sqrt(sum(num.map(n -> n * n)) / Float(num.len))
print(qmean(1..10))</
{{out}}
<pre>
Line 33:
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<
BYTE FUNC Equal(REAL POINTER a,b)
Line 76:
Print("RMS of 1..10 is ")
PrintRE(x)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Root_mean_square.png Screenshot from Atari 8-bit computer]
Line 84:
=={{header|Ada}}==
<
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
Line 103:
list := (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0);
put( rms(list) , Exp=>0);
end calcrms;</
{{out}}
<pre>
Line 113:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}}
<
MODE RMSFIELD = #LONG...# REAL;
PROC (RMSFIELD)RMSFIELD rms field sqrt = #long...# sqrt;
Line 140:
print(("crude rms(one to ten): ", crude rms(one to ten), new line));
print(("rms(one to ten): ", rms(one to ten), new line))
)</
{{out}}
<pre>
Line 148:
=={{header|ALGOL-M}}==
Because ALGOL-M lacks a built-in square root function, we have to supply our own.
<
BEGIN
Line 184:
WRITE("RMS OF WHOLE NUMBERS 1.0 THROUGH 10.0 =", SQRT(SQMEAN));
END</
{{out}}
Based on the limited precision of the square root function, only the first six decimal places of the output can actually be relied on (but that's more than sufficient for most real world uses).
Line 192:
=={{header|ALGOL W}}==
<
% computes the root-mean-square of an array of numbers with %
% the specified lower bound (lb) and upper bound (ub) %
Line 212:
write( "rms of 1 .. 10: ", rms( testNumbers, 1, 10 ) );
end.</
{{out}}
<pre>
Line 219:
=={{header|APL}}==
<
x←⍳10
rms x
6.204836823</
=={{header|AppleScript}}==
===Functional===
{{Trans|JavaScript}}( ES6 version )
<syntaxhighlight lang="applescript">--------------------- ROOT MEAN SQUARE -------------------
-- rootMeanSquare :: [Num] -> Real
on rootMeanSquare(xs)
script
Line 239 ⟶ 242:
on run
Line 248 ⟶ 251:
-- foldl :: (a -> b -> a) -> a -> [b] -> a
Line 272 ⟶ 275:
end script
end if
end mReturn</
{{Out}}
<pre>6.204836822995</pre>
----
===Straightforward===
<syntaxhighlight lang="applescript">on rootMeanSquare(listOfNumbers)
script o
property lst : listOfNumbers
end script
set r to 0.0
repeat with n in o's lst
set r to r + (n ^ 2)
end repeat
return (r / (count o's lst)) ^ 0.5
end rootMeanSquare
rootMeanSquare({1, 2, 3, 4, 5, 6, 7, 8, 9, 10})</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">6.204836822995</syntaxhighlight>
===Integer range alternative===
{{Trans|AutoHotKey}} '''("Avoiding a loop" solution)'''
<syntaxhighlight lang="applescript">
-- RMS of integer range a to b.
on rootMeanSquare(a, b)
return ((b * (b + 1) * (2 * b + 1) - a * (a - 1) * (2 * a - 1)) / 6 / (b - a + 1)) ^ 0.5
end rootMeanSquare
rootMeanSquare(1, 10)</syntaxhighlight>
{{output}}
<syntaxhighlight lang="applescript">6.204836822995</syntaxhighlight>
=={{header|Arturo}}==
<
sqrt (sum map arr 'i -> i^2) // size arr
print rootMeanSquare 1..10</
{{out}}
Line 288 ⟶ 322:
=={{header|Astro}}==
<syntaxhighlight lang
=={{header|AutoHotkey}}==
===Using a loop===
<
Line 302 ⟶ 336:
Sum += (a + A_Index - 1) ** 2
Return, Sqrt(Sum / n)
}</
Message box shows:
<pre>
Line 313 ⟶ 347:
We can show that:<br>
<math>\sum_{i=a}^b i^2 = \frac{b(b+1)(2b+1)-a(a-1)(2a-1)}{6}</math>
<
Line 320 ⟶ 354:
;---------------------------------------------------------------------------
Return, Sqrt((b*(b+1)*(2*b+1)-a*(a-1)*(2*a-1))/6/(b-a+1))
}</
Message box shows:
<pre>
Line 327 ⟶ 361:
=={{header|AWK}}==
<
# computes RMS of the 1st column of a data file
{
Line 337 ⟶ 371:
END {
print "RMS: ",sqrt(S/N);
}</
=={{header|BASIC}}==
Line 344 ⟶ 378:
Note that this will work in [[Visual Basic]] and the Windows versions of [[PowerBASIC]] by simply wrapping the module-level code into the <code>MAIN</code> function, and changing <code>PRINT</code> to <code>MSGBOX</code>.
<
FOR L0 = 1 TO 10
i(L0) = L0
Line 357 ⟶ 391:
tmp = UBOUND(what) - LBOUND(what) + 1
rms# = SQR(rt / tmp)
END FUNCTION</
See also: [[#BBC BASIC|BBC BASIC]], [[#Liberty BASIC|Liberty BASIC]], [[#PureBasic|PureBasic]], [[#Run BASIC|Run BASIC]]
Line 363 ⟶ 397:
==={{header|Applesoft BASIC}}===
<
20 FOR I = 1 TO N
30 S = S + I * I
40 NEXT
50 X = SQR (S / N)
60 PRINT X</
{{out}}
<pre>6.20483683</pre>
==={{header|Craft Basic}}===
<syntaxhighlight lang="basic">precision 8
let n = 10
for i = 1 to n
let s = s + i * i
next i
print sqrt(s / n)</syntaxhighlight>
{{out| Output}}<pre>6.20483682</pre>
==={{header|IS-BASIC}}===
<
110 DEF RMS(N)
120 LET R=0
Line 381 ⟶ 429:
150 NEXT
160 LET RMS=SQR(R/N)
170 END DEF</
==={{header|Sinclair ZX81 BASIC}}===
<
20 LET RMS=0
30 FOR X=1 TO 10
Line 391 ⟶ 439:
60 LET RMS=SQR (RMS/10)
70 SLOW
80 PRINT RMS</
{{out}}
<pre>6.2048368</pre>
==={{header|BBC BASIC}}===
<
array() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Line 402 ⟶ 450:
END
DEF FNrms(a()) = MOD(a()) / SQR(DIM(a(),1)+1)</
=={{header|BQN}}==
RMS is a tacit function which computes root mean square.
<
RMS 1+↕10</
<syntaxhighlight lang="text">6.2048368229954285</
[https://mlochbaum.github.io/BQN/try.html#code=Uk1TIOKGkCDiiJorwrTiiJjDl8ucw7fiiaAKClJNUyAxK+KGlTEw Try It!]
Another solution is to take the arithmetic mean <code>+´÷≠</code> under the square (<code>ט</code>) function. Under (<code>⌾</code>) squares the arguments, then applies the mean, then inverts the square function.
<
=={{header|C}}==
<
#include <math.h>
Line 434 ⟶ 482:
printf("%f\n", rms(v, sizeof(v)/sizeof(double)));
return 0;
}</
=={{header|C sharp|C#}}==
<
namespace rms
Line 459 ⟶ 507:
}
}
}</
An alternative method demonstrating the more functional style introduced by LINQ and lambda expressions in C# 3.
{{works with|C sharp|C#|3}}
<
using System.Collections.Generic;
using System.Linq;
Line 480 ⟶ 528:
}
}
}</
=={{header|C++}}==
<
#include <vector>
#include <cmath>
Line 495 ⟶ 543:
std::cout << "The quadratic mean of the numbers 1 .. " << numbers.size() << " is " << meansquare << " !\n" ;
return 0 ;
}</
{{out}}
<pre>
Line 502 ⟶ 550:
=={{header|Clojure}}==
<
(defn rms [xs]
(Math/sqrt (/ (reduce + (map #(* % %) xs))
(count xs))))
(println (rms (range 1 11)))</
{{out}}
<pre>
Line 515 ⟶ 563:
=={{header|COBOL}}==
Could be written more succinctly, with an inline loop and more <tt>COMPUTE</tt> statements; but that wouldn't be very COBOLic.
<
PROGRAM-ID. QUADRATIC-MEAN-PROGRAM.
DATA DIVISION.
Line 535 ⟶ 583:
ADD 1 TO N.
MULTIPLY N BY N GIVING N-SQUARED.
ADD N-SQUARED TO RUNNING-TOTAL.</
{{out}}
<pre>6.204836822995428</pre>
Line 541 ⟶ 589:
=={{header|CoffeeScript}}==
{{trans|JavaScript}}
<
sum_of_squares = ary.reduce ((s,x) -> s + x*x), 0
return Math.sqrt(sum_of_squares / ary.length)
alert root_mean_square([1..10])</
=={{header|Common Lisp}}==
<
for xx = (* x x)
for n from 1
summing xx into xx-sum
finally (return (sqrt (/ xx-sum n))))</
Here's a non-iterative solution.
<
(defun root-mean-square (numbers)
"Takes a list of numbers, returns their quadratic mean."
Line 564 ⟶ 612:
(root-mean-square (loop for i from 1 to 10 collect i))
</syntaxhighlight>
=={{header|Crystal}}==
{{trans|Ruby}}
<
Math.sqrt(seq.sum { |x| x*x } / seq.size)
end
puts rms (1..10).to_a</
{{out}}
Line 578 ⟶ 626:
=={{header|D}}==
<
real rms(R)(R d) pure {
Line 586 ⟶ 634:
void main() {
writefln("%.19f", iota(1, 11).rms);
}</
{{out}}
<pre>
Line 593 ⟶ 641:
=={{header|Delphi}}/{{header|Pascal}}==
<
{$APPTYPE CONSOLE}
Line 614 ⟶ 662:
Writeln(MeanSquare(TDoubleDynArray.Create()));
Writeln(MeanSquare(TDoubleDynArray.Create(1,2,3,4,5,6,7,8,9,10)));
end.</
=={{header|E}}==
Using the same generic mean function as used in [[../Pythagorean means#E|pythagorean means]]:
<
return def mean(numbers) {
var count := 0
Line 630 ⟶ 678:
}
def RMS := makeMean(0, fn b,x { b+x**2 }, fn acc,n { (acc/n).sqrt() })</
<
# value: 6.2048368229954285</
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang=easylang>
func rms v[] .
for v in v[]
sum += v * v
.
return sqrt (sum / len v[])
.
v[] = [ 1 2 3 4 5 6 7 8 9 10 ]
print rms v[]
</syntaxhighlight>
=={{header|EchoLisp}}==
<
(define (rms xs)
(sqrt (// (for/sum ((x xs)) (* x x)) (length xs))))
Line 642 ⟶ 703:
(rms (range 1 11))
→ 6.2048368229954285
</syntaxhighlight>
=={{header|Elena}}==
{{trans|C#}}
ELENA
<
import system'routines;
import system'math;
Line 654 ⟶ 715:
{
get RootMeanSquare()
= (self.selectBy::(x => x * x).summarize(Real.new()) / self.Length).sqrt();
}
Line 660 ⟶ 721:
{
console.printLine(new Range(1, 10).RootMeanSquare)
}</
{{out}}
<pre>
Line 667 ⟶ 728:
=={{header|Elixir}}==
<
defmodule RC do
def root_mean_square(enum) do
Line 682 ⟶ 743:
IO.puts RC.root_mean_square(1..10)
</syntaxhighlight>
{{out}}
<pre>
Line 689 ⟶ 750:
=={{header|Emacs Lisp}}==
<
(sqrt (/ (apply '+ (mapcar (lambda (x) (* x x)) nums))
(float (length nums)))))
(rms (number-sequence 1 10))</
{{out}}
Line 699 ⟶ 760:
=={{header|Erlang}}==
<
math:sqrt(lists:foldl(fun(E,S) -> S+E*E end, 0, Nums) / length(Nums)).
rms([1,2,3,4,5,6,7,8,9,10]).</
{{out}}
<pre>6.2048368229954285</pre>
=={{header|ERRE}}==
<syntaxhighlight lang="text">
PROGRAM ROOT_MEAN_SQUARE
BEGIN
Line 717 ⟶ 778:
PRINT("Root mean square is";X)
END PROGRAM
</syntaxhighlight>
You can, obviously, generalize reading data from a DATA line or from a file.
=={{header|Euphoria}}==
<
atom sum
if length(s) = 0 then
Line 734 ⟶ 795:
constant s = {1,2,3,4,5,6,7,8,9,10}
? rms(s)</
{{out}}
<pre>6.204836823</pre>
Line 741 ⟶ 802:
===Cell reference expression===
If values are entered in the cells A1 to A10, the below expression will give the RMS value
<
=SQRT(SUMSQ($A1:$A10)/COUNT($A1:$A10))
</syntaxhighlight>
The RMS of [1,10] is then : 6.204836823 ( Actual displayed value 6.204837)
Line 755 ⟶ 816:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(xs,
SQRT(SUMSQ(xs)/COUNT(xs))
)</
For this test, we assume that the following generic lambda is also bound to the name ENUMFROMTO in the Name Manager:
<
=LAMBDA(a,
LAMBDA(z,
SEQUENCE(1 + z - a, 1, a, 1)
)
)</
{{Out}}
Line 790 ⟶ 851:
=={{header|F Sharp|F#}}==
Uses a lambda expression and function piping.
<
let res = RMS [1.0..10.0]</
Answer (in F# Interactive window):
<pre>val res : float = 6.204836823</pre>
=={{header|Factor}}==
<
[ [ sq ] map-sum ] [ length ] bi / sqrt ;</
( scratchpad ) 10 [1,b] root-mean-square .
Line 804 ⟶ 865:
=={{header|Fantom}}==
<
{
static Float averageRms (Float[] nums)
Line 819 ⟶ 880:
echo ("RMS Average of $a is: " + averageRms(a))
}
}</
=={{header|Forth}}==
<
dup >r 0e
floats bounds do
Line 830 ⟶ 891:
create test 1e f, 2e f, 3e f, 4e f, 5e f, 6e f, 7e f, 8e f, 9e f, 10e f,
test 10 rms f. \ 6.20483682299543</
=={{header|Fortran}}==
Assume <math> x </math> stored in array x.
<
=={{header|FreeBASIC}}==
<
' FB 1.05.0 Win64
Line 858 ⟶ 919:
Print "Press any key to quit the program"
Sleep
</syntaxhighlight>
{{out}}
Line 867 ⟶ 928:
=={{header|Futhark}}==
<syntaxhighlight lang="futhark">
import "futlib/math"
fun main(as: [n]f64): f64 =
f64.sqrt ((reduce (+) 0.0 (map (**2.0) as)) / f64(n))
</syntaxhighlight>
=={{header|GEORGE}}==
<syntaxhighlight lang="george">
1, 10 rep (i)
i i | (v) ;
Line 885 ⟶ 946:
sqrt
print
</syntaxhighlight>
<pre>
6.204836822995428
Line 891 ⟶ 952:
=={{header|Go}}==
<
import (
Line 905 ⟶ 966:
}
fmt.Println(math.Sqrt(sum / n))
}</
{{out}}
<pre>
Line 913 ⟶ 974:
=={{header|Groovy}}==
Solution:
<
list == null \
? null \
Line 919 ⟶ 980:
? 0 \
: ((list.collect { it*it }.sum()) / list.size()) ** 0.5
}</
Test:
<
def Q = quadMean(list)
println """
list: ${list}
Q: ${Q}
"""</
{{out}}
<pre>list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Line 933 ⟶ 994:
=={{header|Haskell}}==
Given the <code>mean</code> function defined in [[Averages/Pythagorean means]]:
<
Or, writing a naive '''mean''' of our own, (but see https://donsbot.wordpress.com/2008/06/04/haskell-as-fast-as-c-working-at-a-high-altitude-for-low-level-performance/):
<
rootMeanSquare :: [Double] -> Double
Line 943 ⟶ 1,004:
main :: IO ()
main = print $ rootMeanSquare [1 .. 10]</
{{Out}}
<pre>6.2048368229954285</pre>
=={{header|HicEst}}==
<
DO i = 1, 10
sum = sum + i^2
ENDDO
WRITE(ClipBoard) "RMS(1..10) = ", (sum/10)^0.5 </
RMS(1..10) = 6.204836823
=={{header|Icon}} and {{header|Unicon}}==
<
every put(x := [], 1 to 10)
writes("x := [ "); every writes(!x," "); write("]")
write("Quadratic mean:",q := qmean!x)
end</
<
local m
if *L = 0 then fail
every (m := 0.0) +:= !L^2
return sqrt(m / *L)
end</
=={{header|Io}}==
<
rms( Range 1 to(10) asList ) println</
=={{header|J}}==
'''Solution:'''
<
'''Example Usage:'''
<
6.20484</
<code>*:</code> means [http://jsoftware.com/help/dictionary/d112.htm square]
Line 989 ⟶ 1,050:
=={{header|Java}}==
<
public static double rootMeanSquare(double... nums) {
Line 1,002 ⟶ 1,063:
System.out.println("The RMS of the numbers from 1 to 10 is " + rootMeanSquare(nums));
}
}</
{{out}}
<pre>The RMS of the numbers from 1 to 10 is 6.2048368229954285</pre>
Line 1,010 ⟶ 1,071:
{{works with|JavaScript|1.8}}
{{works with|Firefox|3.0}}
<
var sum_of_squares = ary.reduce(function(s,x) {return (s + x*x)}, 0);
return Math.sqrt(sum_of_squares / ary.length);
}
print( root_mean_square([1,2,3,4,5,6,7,8,9,10]) ); // ==> 6.2048368229954285</
===ES6===
<
'use strict';
Line 1,037 ⟶ 1,098:
// -> 6.2048368229954285
})();</
{{Out}}
Line 1,044 ⟶ 1,105:
=={{header|jq}}==
The following filter returns ''null'' if given an empty array:
<
| if $length == 0 then null
else map(. * .) | add | sqrt / $length
end ;</
=={{header|Julia}}==
There are a variety of ways to do this via built-in functions in Julia, given an array <code>A = [1:10]</code> of values. The formula can be implemented directly as:
<
or shorter with using Statistics (and as spoken: root-mean-square)
<
or the implicit allocation of a new array by <code>A.^2.</code> can be avoided by using <code>sum</code> as a higher-order function: <
One can also use an explicit loop for near-C performance
<
function rms(A)
s = 0.0
Line 1,064 ⟶ 1,125:
return sqrt(s / length(A))
end
</syntaxhighlight>
Potentially even better is to use the built-in <code>norm</code> function, which computes the square root of the sum of the squares of the entries of <code>A</code> in a way that avoids the possibility of spurious floating-point overflow (if the entries of <code>A</code> are so large that they may overflow if squared): <
=={{header|K}}==
<syntaxhighlight lang="k">
rms:{_sqrt (+/x^2)%#x}
rms 1+!10
6.204837
</syntaxhighlight>
=={{header|Kotlin}}==
<
fun quadraticMean(vector: Array<Double>) : Double {
Line 1,085 ⟶ 1,146:
val vector = Array(10, { (it + 1).toDouble() })
print("Quadratic mean of numbers 1 to 10 is ${quadraticMean(vector)}")
}</
{{out}}
Line 1,091 ⟶ 1,152:
Quadratic mean of numbers 1 to 10 is 6.2048368229954285
</pre>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def rms
{lambda {:n}
{sqrt
{/ {+ {S.map {lambda {:i} {* :i :i}}
{S.serie 1 :n}}}
:n}}}}
-> rms
{rms 10}
-> 6.2048368229954285
</syntaxhighlight>
=={{header|Lasso}}==
<
return math_sqrt((with n in #a sum #n*#n) / decimal(#a->size))
}
rms(generateSeries(1,10)->asStaticArray)</
{{out}}
Line 1,102 ⟶ 1,177:
=={{header|Liberty BASIC}}==
<
SourceList$ ="1 2 3 4 5 6 7 8 9 10"
Line 1,127 ⟶ 1,202:
print "R.M.S. value is "; ( SumOfSquares /n)^0.5
end</
=={{header|Logo}}==
<
output sqrt quotient (apply "sum map [? * ?] :v) count :v
end
show rms iseq 1 10</
=={{header|Lua}}==
<
function rms(t) return (sumsq(unpack(t)) / #t)^.5 end
print(rms{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})</
=={{header|Maple}}==
<
RMS := proc( x )
return sqrt( Statistics:-Mean( x ^~ 2 ) );
end proc:
RMS( y );
</syntaxhighlight>
{{out}}
<pre>6.20483682299543
Line 1,154 ⟶ 1,229:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang
The above will give the precise solution <math>\sqrt{\frac{77}{2}}</math>, to downgrade to 6.20484, use '<code>10.</code>' to imply asking for numeric solution, or append '<code>//N</code>' after the whole expression.
=={{header|MATLAB}}==
<
rms = sqrt(mean(list.^2));
end</
Solution:
<
ans =
6.204836822995429</
=={{header|Maxima}}==
<
rms(L) := sqrt(lsum(x^2, x, L)/length(L))$
rms(L), numer; /* 6.204836822995429 */</
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
fn RMS arr =
(
Line 1,183 ⟶ 1,258:
return (sqrt (sumSquared/arr.count as float))
)
</syntaxhighlight>
Output:
<syntaxhighlight lang="maxscript">
rms #{1..10}
6.20484
</syntaxhighlight>
=={{header|min}}==
{{works with|min|0.
<
(1 2 3 4 5 6 7 8 9 10) rms puts!</
{{out}}
<pre>6.204836822995428</pre>
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">0 П0 П1 С/П x^2 ИП0 x^2 ИП1 *
+ ИП1 1 + П1 / КвКор П0 БП
03</
''Instruction:'' В/О С/П Number С/П Number ...
Line 1,213 ⟶ 1,284:
=={{header|Morfa}}==
{{trans|D}}
<
import morfa.base;
import morfa.functional.base;
Line 1,228 ⟶ 1,299:
println(rms(1 .. 11));
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,235 ⟶ 1,306:
=={{header|Nemerle}}==
<
using System.Console;
using System.Math;
Line 1,251 ⟶ 1,322:
WriteLine("RMS of [1 .. 10]: {0:g6}", RMS($[1 .. 10]));
}
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref symbols nobinary
Line 1,269 ⟶ 1,340:
return
</syntaxhighlight>
{{out}}
<pre>
Line 1,276 ⟶ 1,347:
=={{header|Nim}}==
<
from sequtils import mapIt
Line 1,283 ⟶ 1,354:
result = sqrt(result / float(num.len))
echo qmean(@[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0])</
{{out}}
<pre>6.204836822995428</pre>
Line 1,289 ⟶ 1,360:
=={{header|Oberon-2}}==
Oxford Oberon-2
<
MODULE QM;
IMPORT ML := MathL, Out;
Line 1,314 ⟶ 1,385:
Out.String("Quadratic Mean: ");Out.LongReal(Rms(nums));Out.Ln
END QM.
</syntaxhighlight>
{{out}}
<pre>
Line 1,321 ⟶ 1,392:
=={{header|Objeck}}==
<
class Hello {
function : Main(args : String[]) ~ Nil {
Line 1,338 ⟶ 1,409:
}
}
}</
=={{header|OCaml}}==
<
sqrt (Array.fold_left (fun s x -> s +. x*.x) 0.0 a /.
float_of_int (Array.length a))
Line 1,347 ⟶ 1,418:
rms (Array.init 10 (fun i -> float_of_int (i+1))) ;;
(* 6.2048368229954285 *)</
=={{header|Oforth}}==
<
{{out}}
Line 1,359 ⟶ 1,430:
=={{header|ooRexx}}==
<
call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11)
call testAverage .array~of(30, 10, 20, 30, 40, 50, -100, 4.7, -11e2)
Line 1,380 ⟶ 1,451:
return rxcalcsqrt(sum/numbers~items)
::requires rxmath LIBRARY</
{{out}}
<pre>list = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Line 1,392 ⟶ 1,463:
=={{header|Oz}}==
<
fun {Square X} X*X end
Line 1,402 ⟶ 1,473:
end
in
{Show {RMS {List.number 1 10 1}}}</
{{out}}
<pre>
Line 1,410 ⟶ 1,481:
=={{header|PARI/GP}}==
General RMS calculation:
<
sqrt(sum(i=1,#v,v[i]^2)/#v)
};
RMS(vector(10,i,i))</
Specific functions for the first ''n'' positive integers:
<
sqrt((n+1)*(2*n+1)/6)
};
RMS_first(10)</
Asymptotically this is n/sqrt(3).
=={{header|Perl}}==
<
sub rms
{
Line 1,433 ⟶ 1,504:
}
say rms(1..10);</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">rms</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">sqsum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 1,446 ⟶ 1,517:
<span style="color: #0000FF;">?</span><span style="color: #000000;">rms</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">})</span>
<!--</
{{out}}
<pre>
Line 1,453 ⟶ 1,524:
Alternative, same output<br>
You could make this a one-liner, for no gain and making it harder to debug - an explicitly named intermediate such as sqsum adds no additional overhead compared to the un-named hidden temporary variable the compiler would otherwise use anyway, and of course sqsum can be examined/verified to pinpoint any error more effectively. The last (commented-out) line also removes the function call, but of course it has also removed every last descriptive hint of what it is supposed to be doing as well.
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">rms</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">sqsum</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}))</span>
Line 1,460 ⟶ 1,531:
<span style="color: #0000FF;">?</span><span style="color: #000000;">rms</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<span style="color: #000080;font-style:italic;">-- ?sqrt(sum(apply(true,power,{tagset(10),2}))/10) -- (ugh)</span>
<!--</
=={{header|Phixmonti}}==
<
0 swap
len for
Line 1,476 ⟶ 1,547:
endfor
rms print</
=={{header|PHP}}==
<
// Created with PHP 7.0
Line 1,494 ⟶ 1,565:
echo rms(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
</syntaxhighlight>
{{out}}
<pre>
Line 1,503 ⟶ 1,574:
{{trans|Prolog}}
{{works with|Picat}}
<syntaxhighlight lang="picat">
rms(Xs) = Y =>
Sum = sum_of_squares(Xs),
Line 1,518 ⟶ 1,589:
Y = rms(1..10),
printf("The root-mean-square of 1..10 is %f\n", Y).
</syntaxhighlight>
{{out}}
<pre>
Line 1,525 ⟶ 1,596:
=={{header|PicoLisp}}==
<
(let Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0)
Line 1,536 ⟶ 1,607:
(length Lst) )
T )
*Scl ) ) )</
{{out}}
<pre>6.20484</pre>
=={{header|PL/I}}==
<
declare A(10) Dec Float(15) static initial (1,2,3,4,5,6,7,8,9,10);
declare (n,RMS) Dec Float(15);
Line 1,547 ⟶ 1,618:
RMS = sqrt(sum(A**2)/n);
put Skip Data(rms);
End;</
{{out}}
<pre>RMS= 6.20483682299543E+0000;</pre>
=={{header|PostScript}}==
<
/x exch def
/sum 0 def
Line 1,567 ⟶ 1,638:
}def
[1 2 3 4 5 6 7 8 9 10] findrms</
{{out}}
<pre>
Line 1,573 ⟶ 1,644:
</pre>
{{libheader|initlib}}
<
=={{header|Potion}}==
<
total = 0.0
series each (x): total += x * x.
Line 1,583 ⟶ 1,654:
.
rms((1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) print</
=={{header|Powerbuilder}}==
<
decimal ld_rms
Line 1,598 ⟶ 1,669:
ld_rms = Sqrt(ll_product / ll_y)
//ld_rms value is 6.20483682299542849</
=={{header|PowerShell}}==
<
$sqsum=$nums | foreach-object { $_*$_} | measure-object -sum | select-object -expand Sum
return [math]::sqrt($sqsum/$nums.count)
}
get-rms @(1..10) </
=={{header|Processing}}==
<
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print(rms(numbers));
Line 1,621 ⟶ 1,692:
mean = sqrt(mean / nums.length);
return mean;
}</
{{out}}
<pre>6.204837</pre>
Line 1,627 ⟶ 1,698:
=={{header|Prolog}}==
{{works with|GNU Prolog}}
<syntaxhighlight lang="prolog">
:- initialization(main).
Line 1,645 ⟶ 1,716:
rms(Xs, Y),
format('The root-mean-square of 1..10 is ~f\n', [Y]).
</syntaxhighlight>
{{out}}
<pre>
Line 1,652 ⟶ 1,723:
=={{header|PureBasic}}==
<
If OpenConsole()
Line 1,678 ⟶ 1,749:
PrintN("Press ENTER to exit"): Input()
CloseConsole()
EndIf</
=={{header|Python}}==
{{works with|Python|3}}
<
>>> def qmean(num):
return sqrt(sum(n*n for n in num)/len(num))
>>> qmean(range(1,11))
6.2048368229954285</
<small>Note that function [http://docs.python.org/release/3.2/library/functions.html#range range] in Python includes the first limit of 1, excludes the second limit of 11, and has a default increment of 1.</small>
Line 1,694 ⟶ 1,765:
Alternatively in terms of '''reduce''':
<
from math import (sqrt)
Line 1,705 ⟶ 1,776:
print(
rootMeanSquare([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)</
{{Out}}
<pre>6.2048368229954285</pre>
=={{header|Qi}}==
<
R -> (sqrt (/ (APPLY + (MAPCAR * R R)) (length R))))</
Line 1,718 ⟶ 1,789:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
<
[ [] swap
Line 1,744 ⟶ 1,815:
80 rms
80 point$ echo$</
{{out}}
Line 1,756 ⟶ 1,827:
The following function works for any vector x:
<
RMS(1:10)
Line 1,765 ⟶ 1,836:
RMS(c(NA, 1:10), na.rm = T)
# [1] 6.204837</
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(define (rms nums)
(sqrt (/ (for/sum ([n nums]) (* n n)) (length nums))))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub rms(*@nums) { sqrt ([+] @nums X** 2) / @nums }
say rms 1..10;</syntaxhighlight>
Here's a slightly more concise version, albeit arguably less readable:
<syntaxhighlight lang="raku"
=={{header|REXX}}==
Line 1,792 ⟶ 1,863:
<br>calculation as well as a reasonable attempt at providing a first-guess square root by essentially halving
<br>the number using logarithmic (base ten) arithmetic.
<
parse arg nums digs show . /*obtain the optional arguments from CL*/
if nums=='' | nums=="," then nums=10 /*Not specified? Then use the default.*/
Line 1,810 ⟶ 1,881:
h=d+6; do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
return g</
'''output''' when using the default inputs:
<pre>
Line 1,817 ⟶ 1,888:
=={{header|Ring}}==
<
nums = [1,2,3,4,5,6,7,8,9,10]
sum = 0
Line 1,829 ⟶ 1,900:
x = sqrt(sum / len(number))
return x
</syntaxhighlight>
=={{header|RPL}}==
≪ LIST→ → n
≪ 0 1 n '''START''' SWAP SQ + '''NEXT'''
n / √
≫ ≫ '<span style="color:blue">RMS</span>' STO
{ 1 2 3 4 5 6 7 8 9 10 } <span style="color:blue">RMS</span>
{{out}}
<pre>
1: 6.204836823
</pre>
{{works with|HP|48G}}
≪ DUP ≪ SQ + ≫ STREAM SWAP SIZE / √
≫ '<span style="color:blue">RMS</span>' STO
=={{header|Ruby}}==
<
def quadratic_mean
Math.sqrt( self.inject(0.0) {|s, y| s + y*y} / self.length )
Line 1,844 ⟶ 1,930:
end
(1..10).quadratic_mean # => 6.2048368229954285</
and a non object-oriented solution:
<
Math.sqrt(seq.sum{|x| x*x}.fdiv(seq.size) )
end
puts rms (1..10) # => 6.2048368229954285</
=={{header|Run BASIC}}==
<
while word$(valueList$,i +1) <> "" ' grab values from list
thisValue = val(word$(valueList$,i +1)) ' turn values into numbers
Line 1,860 ⟶ 1,946:
wend
print "List of Values:";valueList$;" containing ";i;" values"
print "Root Mean Square =";(sumSquares/i)^0.5</
{{out}}
Line 1,867 ⟶ 1,953:
=={{header|Rust}}==
<
let sum_squares = vec.iter().fold(0, |acc, &x| acc + x.pow(2));
return ((sum_squares as f32)/(vec.len() as f32)).sqrt();
Line 1,875 ⟶ 1,961:
let vec = (1..11).collect();
println!("The root mean square is: {}", root_mean_square(vec));
}</
{{out}}
Line 1,888 ⟶ 1,974:
built-in syntax.
<
{
return sqrt(sum(sqr(arr)) / length(arr));
}
print(rms([1:10]));</
=={{header|Sather}}==
<
-- irrms stands for Integer Ranged RMS
irrms(i, f:INT):FLT
Line 1,911 ⟶ 1,997:
#OUT + irrms(1, 10) + "\n";
end;
end;</
=={{header|S-BASIC}}==
<
var n, sqsum, sqmean, rms = real
sqsum = 0
Line 1,925 ⟶ 2,011:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,932 ⟶ 2,018:
=={{header|Scala}}==
<
println(rms(1 to 10))</
{{out}}
<pre>6.2048368229954285</pre>
=={{header|Scheme}}==
<
(sqrt (/ (apply + (map * nums nums))
(length nums))))
(rms '(1 2 3 4 5 6 7 8 9 10))</
{{out}}
<pre>6.20483682299543</pre>
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 1,969 ⟶ 2,055:
begin
writeln(rms(numbers) digits 7);
end func;</
=={{header|Shen}}==
{{works with|shen-scheme|0.17}}
<
(tc +)
Line 1,998 ⟶ 2,084:
Lim -> (iota-h 1 Lim))
(output "~A~%" (rms (iota 10)))</
=={{header|Sidef}}==
<
sqrt(a.map{.**2}.sum / a.len)
}
say rms(1..10)</
Using hyper operators, we can write it as:
<
{{out}}
Line 2,014 ⟶ 2,100:
=={{header|Smalltalk}}==
<
=={{header|SNOBOL4}}==
Line 2,020 ⟶ 2,106:
{{works with|CSnobol}}
There is no built-in sqrt( ) function in Snobol4+.
<
rms i = i + 1; ssq = ssq + (a<i> * a<i>) :s(rms)
rms = sqrt(1.0 * ssq / prototype(a)) :(return)
Line 2,029 ⟶ 2,115:
loop i = i + 1; str len(p) span('0123456789') . a<i> @p :s(loop)
output = str ' -> ' rms(a)
end</
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 -> 6.20483682</pre>
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "calcrms" )
@( description, "Compute the Root mean square of the numbers 1..10." )
@( description, "The root mean square is also known by its initial RMS (or rms), and as the" )
@( description, "quadratic mean. The RMS is calculated as the mean of the squares of the" )
@( description, "numbers, square-rooted" )
@( see_also, "http://rosettacode.org/wiki/Averages/Root_mean_square" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
pragma restriction( no_external_commands );
procedure calcrms is
type float_arr is array(1..10) of float;
list : constant float_arr := (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0);
total: float := 0.0;
rms : float;
begin
for p in arrays.first(list)..arrays.last(list) loop
total := @ + list(p)**2;
end loop;
rms := numerics.sqrt( total / float(arrays.length(list)));
? rms;
end calcrms;</syntaxhighlight>
=={{header|Standard ML}}==
<
let
val v' = Vector.map (fn x => x*x) v
Line 2,042 ⟶ 2,155:
end;
rms(Vector.tabulate(10, fn n => real(n+1)));</
{{out}}
<pre>val it = 6.204836823 : real</pre>
Line 2,049 ⟶ 2,162:
Compute the RMS of a variable and return the result in r(rms).
<
syntax varname(numeric) [if] [in]
tempvar x
Line 2,055 ⟶ 2,168:
qui sum `x' `if' `in'
return scalar rms=sqrt(r(mean))
end</
'''Example'''
<
set obs 20
gen x=rnormal()
Line 2,069 ⟶ 2,182:
rms x if x>0
di r(rms)
.7423647</
=={{header|Swift}}==
<
@inlinable
public func rms() -> Element {
Line 2,080 ⟶ 2,193:
}
print("RMS of 1...10: \((1...10).map(Double.init).rms())")</
{{out}}
Line 2,088 ⟶ 2,201:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<
set sum 0.0
foreach value $list { set sum [expr {$sum + $value**2}] }
Line 2,094 ⟶ 2,207:
}
puts "RMS(1..10) = [qmean {1 2 3 4 5 6 7 8 9 10}]"</
{{out}}
<pre>
Line 2,102 ⟶ 2,215:
=={{header|Ursala}}==
using the <code>mean</code> function among others from the <code>flo</code> library
<
#import flo
#cast %e
rms = sqrt mean sqr* float* nrange(1,10)</
{{out}}
<pre>
Line 2,115 ⟶ 2,228:
=={{header|Vala}}==
Valac probably needs to have the flag "-X -lm" added to include the C Math library.
<
double sum_squares = 0;
double mean;
Line 2,133 ⟶ 2,246:
stdout.printf("%s\n", mean.to_string());
}</
{{out}}
<pre>
Line 2,141 ⟶ 2,254:
=={{header|VBA}}==
Using Excel VBA
<
For i = 1 To UBound(s)
s(i) = s(i) ^ 2
Line 2,151 ⟶ 2,264:
s = [{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]
Debug.Print root_mean_square(s)
End Sub</
Without using Excel worksheetfunction:
<
Dim i As Integer
If iLow > iHigh Then
Line 2,169 ⟶ 2,282:
Debug.Print rms(1, 10)
End Sub
</syntaxhighlight>
Output:
Line 2,176 ⟶ 2,289:
</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import math
fn main() {
Line 2,186 ⟶ 2,299:
}
println(math.sqrt(sum / n))
}</
{{out}}
Line 2,194 ⟶ 2,307:
=={{header|Wortel}}==
<
; using a composition and a fork (like you would do in J)
rms1 ^(@sqrt @(@sum / #) *^@sq)
Line 2,205 ⟶ 2,318:
!rms2 @to 10
]]
}</
{{out}}
<pre>[6.2048368229954285 6.2048368229954285]</pre>
=={{header|Wren}}==
<
System.print(rms)</
{{out}}
Line 2,219 ⟶ 2,332:
=={{header|XLISP}}==
<
(sqrt
(/
Line 2,234 ⟶ 2,347:
; test QUADRATIC-MEAN
(print (quadratic-mean (range 1 11)))</
{{out}}
<pre>6.20483682299543</pre>
=={{header|XPL0}}==
<
code real RlOut=48;
int N;
Line 2,247 ⟶ 2,360:
RlOut(0, sqrt(S/10.0));
CrLf(0);
]</
{{out}}
<pre>
Line 2,254 ⟶ 2,367:
=={{header|Yacas}}==
<
The above will give the precise solution <math>\sqrt{\frac{77}{2}}</math>, to downgrade to 6.20483682299, surround the expression with '<code>N()</code>'.
=={{header|zkl}}==
<
The order in the reduce function is important as it coerces n*n to float.
<pre>
|