Averages/Root mean square: Difference between revisions
Content deleted Content added
→{{header|AWK}}: compute RMS |
m <lang> needs a language |
||
Line 9: | Line 9: | ||
Cf. [[Averages/Pythagorean means]] |
Cf. [[Averages/Pythagorean means]] |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang Ada>with Ada.Float_Text_IO; use Ada.Float_Text_IO; |
|||
<lang Ada> |
|||
with Ada.Float_Text_IO; use Ada.Float_Text_IO; |
|||
with Ada.Numerics.Elementary_Functions; |
with Ada.Numerics.Elementary_Functions; |
||
use Ada.Numerics.Elementary_Functions; |
use Ada.Numerics.Elementary_Functions; |
||
Line 29: | Line 28: | ||
list := (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0); |
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); |
put( rms(list) , Exp=>0); |
||
end calcrms; |
end calcrms;</lang> |
||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
6.20484 |
6.20484 |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68|Standard - no extensions to language used}} |
{{works with|ALGOL 68|Standard - no extensions to language used}} |
||
{{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|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)}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}} |
||
<lang algol68># Define the rms PROCedure & ABS OPerators for LONG... REAL # |
<lang algol68># Define the rms PROCedure & ABS OPerators for LONG... REAL # |
||
Line 76: | Line 73: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
<lang APL> |
<lang APL> rms←{((+/⍵*2)÷⍴⍵)*0.5} |
||
rms←{((+/⍵*2)÷⍴⍵)*0.5} |
|||
x←⍳10 |
x←⍳10 |
||
rms x |
rms x |
||
6.204836823 |
6.204836823</lang> |
||
</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 186: | Line 181: | ||
} |
} |
||
}</lang> |
}</lang> |
||
An alternative method demonstrating the more functional style introduced by LINQ and lambda expressions in C# 3. |
An alternative method demonstrating the more functional style introduced by LINQ and lambda expressions in C# 3. |
||
{{works with|C sharp|C#|3}} |
{{works with|C sharp|C#|3}} |
||
<lang csharp>using System; |
<lang csharp>using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 238: | Line 230: | ||
(println (rms (range 1 11)))</lang> |
(println (rms (range 1 11)))</lang> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 274: | Line 265: | ||
</pre> |
</pre> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}/{{header|Pascal}}== |
||
<lang Delphi>program AveragesMeanSquare; |
<lang Delphi>program AveragesMeanSquare; |
||
Line 299: | Line 290: | ||
=={{header|E}}== |
=={{header|E}}== |
||
Using the same generic mean function as used in [[../Pythagorean means#E|pythagorean means]]: |
|||
Using the same generic mean function as used in [[../Pythagorean means]]: |
|||
<lang e>def makeMean(base, include, finish) { |
<lang e>def makeMean(base, include, finish) { |
||
return def mean(numbers) { |
return def mean(numbers) { |
||
Line 324: | Line 313: | ||
rms([1,2,3,4,5,6,7,8,9,10]).</lang> |
rms([1,2,3,4,5,6,7,8,9,10]).</lang> |
||
Output: |
Output: |
||
< |
<pre>6.2048368229954285</pre> |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Line 343: | Line 331: | ||
constant s = {1,2,3,4,5,6,7,8,9,10} |
constant s = {1,2,3,4,5,6,7,8,9,10} |
||
? rms(s)</lang> |
? rms(s)</lang> |
||
Output: |
Output: |
||
<pre>6.204836823</pre> |
<pre>6.204836823</pre> |
||
Line 349: | Line 336: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Uses a lambda expression and function piping. |
Uses a lambda expression and function piping. |
||
<lang Fsharp>let RMS (x:float list) : float = List.map (fun y -> y**2.0) x |> List.average |> System.Math.Sqrt |
|||
let res = RMS [1.0..10.0]</lang> |
|||
<lang Fsharp> |
|||
Answer (in F# Interactive window): |
|||
let RMS (x:float list) : float = List.map (fun y -> y**2.0) x |> List.average |> System.Math.Sqrt |
|||
<pre>val res : float = 6.204836823</pre> |
|||
let res = RMS [1.0..10.0] |
|||
</lang> |
|||
Answer (in F# Interactive window):<pre>val res : float = 6.204836823</pre> |
|||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
<lang fantom>class Main |
|||
<lang fantom> |
|||
class Main |
|||
{ |
{ |
||
static Float averageRms (Float[] nums) |
static Float averageRms (Float[] nums) |
||
Line 376: | Line 358: | ||
echo ("RMS Average of $a is: " + averageRms(a)) |
echo ("RMS Average of $a is: " + averageRms(a)) |
||
} |
} |
||
}</lang> |
|||
} |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 399: | Line 380: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Assume <math> x </math> stored in array x. |
Assume <math> x </math> stored in array x. |
||
<lang Fortran>print *,sqrt( sum(x**2)/size(x) )</lang> |
<lang Fortran>print *,sqrt( sum(x**2)/size(x) )</lang> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
<lang go>package main |
<lang go>package main |
||
Line 426: | Line 407: | ||
: ((list.collect { it*it }.sum()) / list.size()) ** 0.5 |
: ((list.collect { it*it }.sum()) / list.size()) ** 0.5 |
||
}</lang> |
}</lang> |
||
Test: |
Test: |
||
<lang groovy>def list = 1..10 |
<lang groovy>def list = 1..10 |
||
Line 434: | Line 414: | ||
Q: ${Q} |
Q: ${Q} |
||
"""</lang> |
"""</lang> |
||
Output: |
Output: |
||
<pre>list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
<pre>list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
||
Line 441: | Line 420: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Given the <code>mean</code> function defiend in [[Averages/Pythagorean means]]: |
Given the <code>mean</code> function defiend in [[Averages/Pythagorean means]]: |
||
<lang haskell>main = print $ mean 2 [1 .. 10]</lang> |
<lang haskell>main = print $ mean 2 [1 .. 10]</lang> |
||
Line 479: | Line 457: | ||
<lang j> rms 1 + i. 10 |
<lang j> rms 1 + i. 10 |
||
6.20484</lang> |
6.20484</lang> |
||
<code>*:</code> means [http://jsoftware.com/help/dictionary/d112.htm square] |
<code>*:</code> means [http://jsoftware.com/help/dictionary/d112.htm square] |
||
Line 488: | Line 464: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<lang java>public class RMS{ |
<lang java>public class RMS { |
||
public static double rms(double[] nums){ |
|||
double ms = 0; |
double ms = 0; |
||
for (int i = 0; i < nums.length; i++) |
for (int i = 0; i < nums.length; i++) |
||
ms += nums[i] * nums[i]; |
ms += nums[i] * nums[i]; |
||
ms /= nums.length; |
ms /= nums.length; |
||
return Math.sqrt(ms); |
return Math.sqrt(ms); |
||
} |
|||
} |
|||
public static void main(String[] args){ |
|||
double[] nums = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; |
double[] nums = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}; |
||
System.out.println("The RMS of the numbers from 1 to 10 is " + rms(nums)); |
System.out.println("The RMS of the numbers from 1 to 10 is " + rms(nums)); |
||
} |
|||
} |
|||
}</lang> |
}</lang> |
||
Output: |
Output: |
||
<pre>The RMS of the numbers from 1 to 10 is 6.2048368229954285</pre> |
<pre>The RMS of the numbers from 1 to 10 is 6.2048368229954285</pre> |
||
Line 591: | Line 566: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
<lang objeck> |
<lang objeck>bundle Default { |
||
bundle Default { |
|||
class Hello { |
class Hello { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 609: | Line 583: | ||
} |
} |
||
} |
} |
||
}</lang> |
|||
} |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<lang ocaml>let rms a = |
<lang ocaml>let rms a = |
||
sqrt (Array.fold_left (fun s x -> s +. x*.x) 0.0 a /. |
sqrt (Array.fold_left (fun s x -> s +. x*.x) 0.0 a /. |
||
Line 634: | Line 606: | ||
in |
in |
||
{Show {RMS {List.number 1 10 1}}}</lang> |
{Show {RMS {List.number 1 10 1}}}</lang> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 655: | Line 626: | ||
RMS_first(10)</lang> |
RMS_first(10)</lang> |
||
Asymptotically this is n/sqrt(3). |
Asymptotically this is n/sqrt(3). |
||
=={{header|Pascal}}== |
|||
See [[Averages/Root_mean_square#Delphi | Delphi]] |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 678: | Line 646: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<lang PL/I>declare A(10) fixed decimal static initial (1,2,3,4,5,6,7,8,9,10); |
|||
<lang PL/I> |
|||
declare A(10) fixed decimal static initial (1,2,3,4,5,6,7,8,9,10); |
|||
n = hbound(A,1); |
n = hbound(A,1); |
||
RMS = sqrt(sum(A**2)/n); |
RMS = sqrt(sum(A**2)/n);</lang> |
||
</lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 701: | Line 667: | ||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
<lang> |
<lang postscript>/findrms{ |
||
/findrms{ |
|||
/x exch def |
/x exch def |
||
/sum 0 def |
/sum 0 def |
||
Line 717: | Line 682: | ||
}def |
}def |
||
[1 2 3 4 5 6 7 8 9 10] findrms |
[1 2 3 4 5 6 7 8 9 10] findrms</lang> |
||
</lang> |
|||
Output: |
Output: |
||
< |
<pre> |
||
6.20483685 |
6.20483685 |
||
</ |
</pre> |
||
{{libheader|initlib}} |
{{libheader|initlib}} |
||
<lang postscript> |
<lang postscript>[1 10] 1 range dup 0 {dup * +} fold exch length div sqrt</lang> |
||
[1 10] 1 range dup 0 {dup * +} fold exch length div sqrt |
|||
</lang> |
|||
=={{header|Powerbuilder}}== |
=={{header|Powerbuilder}}== |
||
<lang>long ll_x, ll_y, ll_product |
<lang powerbuilder>long ll_x, ll_y, ll_product |
||
decimal ld_rms |
decimal ld_rms |
||
Line 744: | Line 705: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<lang PowerShell>function get-rms([float[]]$nums){ |
<lang PowerShell>function get-rms([float[]]$nums){ |
||
$sqsum=$nums | foreach-object { $_*$_} | measure-object -sum | select-object -expand Sum |
$sqsum=$nums | foreach-object { $_*$_} | measure-object -sum | select-object -expand Sum |
||
Line 783: | Line 743: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|3}} |
{{works with|Python|3}} |
||
<lang Python>>>> from math import sqrt |
<lang Python>>>> from math import sqrt |
||
>>> def qmean(num): |
>>> def qmean(num): |
||
Line 795: | Line 754: | ||
=={{header|Qi}}== |
=={{header|Qi}}== |
||
<lang qi>(define rms |
|||
R -> (sqrt (/ (APPLY + (MAPCAR * R R)) (length R))))</lang> |
|||
<lang qi> |
|||
(define rms |
|||
R -> (sqrt (/ (APPLY + (MAPCAR * R R)) (length R)))) |
|||
</lang> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
<lang R> |
<lang R>sqrt(sum((1:10)^2/10))</lang> |
||
sqrt(sum((1:10)^2/10)) |
|||
</lang> |
|||
or generally, for x |
or generally, for x |
||
<lang R> |
<lang R>x<-1:10 |
||
sqrt(sum((x)^2/length(x)))</lang> |
|||
x<-1:10 |
|||
sqrt(sum((x)^2/length(x))) |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
REXX has no built-in SQRT, so a simple version is included here. |
REXX has no built-in SQRT, so a simple version is included here. |
||
<lang rexx>/*REXX program to compute root mean square. */ |
|||
<lang rexx> |
|||
/*REXX program to compute root mean square. */ |
|||
parse arg n . /*get the argument (maybe). */ |
parse arg n . /*get the argument (maybe). */ |
||
Line 849: | Line 798: | ||
numeric digits d /*restore the original precision.*/ |
numeric digits d /*restore the original precision.*/ |
||
return g/1 /*normalize to old precision. */ |
return g/1 /*normalize to old precision. */</lang> |
||
</lang> |
|||
Output: |
Output: |
||
<pre> |
|||
<pre style="height:10ex;overflow:scroll"> |
|||
root mean square for 1-->10 is 6.2048368229954282980666209777247378499279652953641 |
root mean square for 1-->10 is 6.2048368229954282980666209777247378499279652953641 |
||
</pre> |
</pre> |
||
Line 896: | Line 844: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<lang scala>def rms(nums: Seq[Int]) = math.sqrt(nums.map(math.pow(_, 2)).sum / nums.size) |
|||
println(rms(1 to 10))</lang> |
|||
<lang scala> |
|||
def rms(nums: Seq[Int]) = math.sqrt(nums.map(math.pow(_, 2)).sum / nums.size) |
|||
println(rms(1 to 10)) |
|||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 908: | Line 853: | ||
(rms '(1 2 3 4 5 6 7 8 9 10))</lang> |
(rms '(1 2 3 4 5 6 7 8 9 10))</lang> |
||
Output: |
Output: |
||
< |
<pre>6.20483682299543</pre> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 938: | Line 882: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
<lang smalltalk>(((1 to: 10) inject: 0 into: [ :s :n | n*n + s ]) / 10) sqrt.</lang> |
|||
<lang smalltalk> |
|||
(((1 to: 10) inject: 0 into: [ :s :n | n*n + s ]) / 10) sqrt. |
|||
</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
{{works with|Macro Spitbol}} |
{{works with|Macro Spitbol}} |
||
{{works with|CSnobol}} |
{{works with|CSnobol}} |
||
There is no built-in sqrt( ) function in Snobol4+. |
There is no built-in sqrt( ) function in Snobol4+. |
||
<lang SNOBOL4> define('rms(a)i,ssq') :(rms_end) |
<lang SNOBOL4> define('rms(a)i,ssq') :(rms_end) |
||
rms i = i + 1; ssq = ssq + (a<i> * a<i>) :s(rms) |
rms i = i + 1; ssq = ssq + (a<i> * a<i>) :s(rms) |
||
Line 959: | Line 898: | ||
output = str ' -> ' rms(a) |
output = str ' -> ' rms(a) |
||
end</lang> |
end</lang> |
||
Output: |
Output: |
||
<pre>1 2 3 4 5 6 7 8 9 10 -> 6.20483682</pre> |
<pre>1 2 3 4 5 6 7 8 9 10 -> 6.20483682</pre> |
||
Line 979: | Line 917: | ||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
using the <code>mean</code> function among others from the <code>flo</code> library |
using the <code>mean</code> function among others from the <code>flo</code> library |
||
<lang Ursala> |
<lang Ursala>#import nat |
||
#import nat |
|||
#import flo |
#import flo |
||
#cast %e |
#cast %e |
||
rms = sqrt mean sqr* float* nrange(1,10) |
rms = sqrt mean sqr* float* nrange(1,10)</lang> |
||
</lang> |
|||
output: |
output: |
||
<pre> |
<pre> |
||
Line 994: | Line 930: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
Valac probably needs to have the flag "-X -lm" added to include the C Math library. |
Valac probably needs to have the flag "-X -lm" added to include the C Math library. |
||
<lang vala> |
<lang vala>double rms(double[] list){ |
||
double rms(double[] list){ |
|||
double sum_squares = 0; |
double sum_squares = 0; |
||
double mean; |
double mean; |
||
Line 1,013: | Line 948: | ||
stdout.printf("%s\n", mean.to_string()); |
stdout.printf("%s\n", mean.to_string()); |
||
}</lang> |
|||
} |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |