Averages/Root mean square: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: move paren to a better location) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
{{task}} |
{{task}} |
||
;Task |
|||
{{task heading}} |
|||
Compute the [[wp:Root mean square|Root mean square]] of the numbers 1..10. |
Compute the [[wp:Root mean square|Root mean square]] of the numbers 1..10. |
||
Line 14: | Line 14: | ||
;See also |
|||
{{Related tasks/Statistical measures}} |
{{Related tasks/Statistical measures}} |
||
Line 22: | Line 22: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=11l>F qmean(num) |
<syntaxhighlight lang="11l">F qmean(num) |
||
R sqrt(sum(num.map(n -> n * n)) / Float(num.len)) |
R sqrt(sum(num.map(n -> n * n)) / Float(num.len)) |
||
Line 33: | Line 33: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit |
||
BYTE FUNC Equal(REAL POINTER a,b) |
BYTE FUNC Equal(REAL POINTER a,b) |
||
Line 84: | Line 84: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight 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 113: | 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|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)}} |
||
<syntaxhighlight lang=algol68># Define the rms PROCedure & ABS OPerators for LONG... REAL # |
<syntaxhighlight lang="algol68"># Define the rms PROCedure & ABS OPerators for LONG... REAL # |
||
MODE RMSFIELD = #LONG...# REAL; |
MODE RMSFIELD = #LONG...# REAL; |
||
PROC (RMSFIELD)RMSFIELD rms field sqrt = #long...# sqrt; |
PROC (RMSFIELD)RMSFIELD rms field sqrt = #long...# sqrt; |
||
Line 148: | Line 148: | ||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
Because ALGOL-M lacks a built-in square root function, we have to supply our own. |
Because ALGOL-M lacks a built-in square root function, we have to supply our own. |
||
<syntaxhighlight lang=algol> |
<syntaxhighlight lang="algol"> |
||
BEGIN |
BEGIN |
||
Line 192: | Line 192: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
<syntaxhighlight lang=algolw>begin |
<syntaxhighlight lang="algolw">begin |
||
% computes the root-mean-square of an array of numbers with % |
% computes the root-mean-square of an array of numbers with % |
||
% the specified lower bound (lb) and upper bound (ub) % |
% the specified lower bound (lb) and upper bound (ub) % |
||
Line 219: | Line 219: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="apl"> rms←{((+/⍵*2)÷⍴⍵)*0.5} |
||
x←⍳10 |
x←⍳10 |
||
Line 227: | Line 227: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{Trans|JavaScript}}( ES6 version ) |
{{Trans|JavaScript}}( ES6 version ) |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="applescript">-- rootMeanSquare :: [Num] -> Real |
||
on rootMeanSquare(xs) |
on rootMeanSquare(xs) |
||
script |
script |
||
Line 278: | Line 278: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight lang=rebol>rootMeanSquare: function [arr]-> |
<syntaxhighlight lang="rebol">rootMeanSquare: function [arr]-> |
||
sqrt (sum map arr 'i -> i^2) // size arr |
sqrt (sum map arr 'i -> i^2) // size arr |
||
Line 288: | Line 288: | ||
=={{header|Astro}}== |
=={{header|Astro}}== |
||
<syntaxhighlight lang=python>sqrt(mean(x²))</syntaxhighlight> |
<syntaxhighlight lang="python">sqrt(mean(x²))</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
===Using a loop=== |
===Using a loop=== |
||
<syntaxhighlight lang=autohotkey>MsgBox, % RMS(1, 10) |
<syntaxhighlight lang="autohotkey">MsgBox, % RMS(1, 10) |
||
Line 313: | Line 313: | ||
We can show that:<br> |
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> |
<math>\sum_{i=a}^b i^2 = \frac{b(b+1)(2b+1)-a(a-1)(2a-1)}{6}</math> |
||
<syntaxhighlight lang=autohotkey>MsgBox, % RMS(1, 10) |
<syntaxhighlight lang="autohotkey">MsgBox, % RMS(1, 10) |
||
Line 327: | Line 327: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang=awk>#!/usr/bin/awk -f |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
# computes RMS of the 1st column of a data file |
# computes RMS of the 1st column of a data file |
||
{ |
{ |
||
Line 344: | Line 344: | ||
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>. |
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>. |
||
<syntaxhighlight lang=qbasic>DIM i(1 TO 10) AS DOUBLE, L0 AS LONG |
<syntaxhighlight lang="qbasic">DIM i(1 TO 10) AS DOUBLE, L0 AS LONG |
||
FOR L0 = 1 TO 10 |
FOR L0 = 1 TO 10 |
||
i(L0) = L0 |
i(L0) = L0 |
||
Line 363: | Line 363: | ||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="applesoftbasic"> 10 N = 10 |
||
20 FOR I = 1 TO N |
20 FOR I = 1 TO N |
||
30 S = S + I * I |
30 S = S + I * I |
||
Line 374: | Line 374: | ||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="is-basic">100 PRINT RMS(10) |
||
110 DEF RMS(N) |
110 DEF RMS(N) |
||
120 LET R=0 |
120 LET R=0 |
||
Line 384: | Line 384: | ||
==={{header|Sinclair ZX81 BASIC}}=== |
==={{header|Sinclair ZX81 BASIC}}=== |
||
<syntaxhighlight lang=basic>10 FAST |
<syntaxhighlight lang="basic">10 FAST |
||
20 LET RMS=0 |
20 LET RMS=0 |
||
30 FOR X=1 TO 10 |
30 FOR X=1 TO 10 |
||
Line 396: | Line 396: | ||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
<syntaxhighlight lang=bbcbasic> DIM array(9) |
<syntaxhighlight lang="bbcbasic"> DIM array(9) |
||
array() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
array() = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 |
||
Line 406: | Line 406: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
RMS is a tacit function which computes root mean square. |
RMS is a tacit function which computes root mean square. |
||
<syntaxhighlight lang=bqn>RMS ← √+´∘ט÷≠ |
<syntaxhighlight lang="bqn">RMS ← √+´∘ט÷≠ |
||
RMS 1+↕10</syntaxhighlight> |
RMS 1+↕10</syntaxhighlight> |
||
<lang>6.2048368229954285</syntaxhighlight> |
<syntaxhighlight lang="text">6.2048368229954285</syntaxhighlight> |
||
[https://mlochbaum.github.io/BQN/try.html#code=Uk1TIOKGkCDiiJorwrTiiJjDl8ucw7fiiaAKClJNUyAxK+KGlTEw Try It!] |
[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. |
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. |
||
<syntaxhighlight lang=bqn>RMS ← (+´÷≠)⌾(ט)</syntaxhighlight> |
<syntaxhighlight lang="bqn">RMS ← (+´÷≠)⌾(ט)</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 437: | Line 437: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
namespace rms |
namespace rms |
||
Line 462: | Line 462: | ||
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}} |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 483: | Line 483: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
#include <cmath> |
#include <cmath> |
||
Line 502: | Line 502: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
<syntaxhighlight lang=clojure> |
<syntaxhighlight lang="clojure"> |
||
(defn rms [xs] |
(defn rms [xs] |
||
(Math/sqrt (/ (reduce + (map #(* % %) xs)) |
(Math/sqrt (/ (reduce + (map #(* % %) xs)) |
||
Line 515: | Line 515: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
Could be written more succinctly, with an inline loop and more <tt>COMPUTE</tt> statements; but that wouldn't be very COBOLic. |
Could be written more succinctly, with an inline loop and more <tt>COMPUTE</tt> statements; but that wouldn't be very COBOLic. |
||
<syntaxhighlight lang=cobol>IDENTIFICATION DIVISION. |
<syntaxhighlight lang="cobol">IDENTIFICATION DIVISION. |
||
PROGRAM-ID. QUADRATIC-MEAN-PROGRAM. |
PROGRAM-ID. QUADRATIC-MEAN-PROGRAM. |
||
DATA DIVISION. |
DATA DIVISION. |
||
Line 541: | Line 541: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
{{trans|JavaScript}} |
{{trans|JavaScript}} |
||
<syntaxhighlight lang=coffeescript> root_mean_square = (ary) -> |
<syntaxhighlight lang="coffeescript"> root_mean_square = (ary) -> |
||
sum_of_squares = ary.reduce ((s,x) -> s + x*x), 0 |
sum_of_squares = ary.reduce ((s,x) -> s + x*x), 0 |
||
return Math.sqrt(sum_of_squares / ary.length) |
return Math.sqrt(sum_of_squares / ary.length) |
||
Line 548: | Line 548: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang=lisp>(loop for x from 1 to 10 |
<syntaxhighlight lang="lisp">(loop for x from 1 to 10 |
||
for xx = (* x x) |
for xx = (* x x) |
||
for n from 1 |
for n from 1 |
||
Line 556: | Line 556: | ||
Here's a non-iterative solution. |
Here's a non-iterative solution. |
||
<syntaxhighlight lang=lisp> |
<syntaxhighlight lang="lisp"> |
||
(defun root-mean-square (numbers) |
(defun root-mean-square (numbers) |
||
"Takes a list of numbers, returns their quadratic mean." |
"Takes a list of numbers, returns their quadratic mean." |
||
Line 568: | Line 568: | ||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
<syntaxhighlight lang=ruby>def rms(seq) |
<syntaxhighlight lang="ruby">def rms(seq) |
||
Math.sqrt(seq.sum { |x| x*x } / seq.size) |
Math.sqrt(seq.sum { |x| x*x } / seq.size) |
||
end |
end |
||
Line 578: | Line 578: | ||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang=d>import std.stdio, std.math, std.algorithm, std.range; |
<syntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.range; |
||
real rms(R)(R d) pure { |
real rms(R)(R d) pure { |
||
Line 593: | Line 593: | ||
=={{header|Delphi}}/{{header|Pascal}}== |
=={{header|Delphi}}/{{header|Pascal}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="delphi">program AveragesMeanSquare; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 618: | Line 618: | ||
=={{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#E|pythagorean means]]: |
||
<syntaxhighlight lang=e>def makeMean(base, include, finish) { |
<syntaxhighlight lang="e">def makeMean(base, include, finish) { |
||
return def mean(numbers) { |
return def mean(numbers) { |
||
var count := 0 |
var count := 0 |
||
Line 632: | Line 632: | ||
def RMS := makeMean(0, fn b,x { b+x**2 }, fn acc,n { (acc/n).sqrt() })</syntaxhighlight> |
def RMS := makeMean(0, fn b,x { b+x**2 }, fn acc,n { (acc/n).sqrt() })</syntaxhighlight> |
||
<syntaxhighlight lang=e>? RMS(1..10) |
<syntaxhighlight lang="e">? RMS(1..10) |
||
# value: 6.2048368229954285</syntaxhighlight> |
# value: 6.2048368229954285</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
<syntaxhighlight lang=scheme> |
<syntaxhighlight lang="scheme"> |
||
(define (rms xs) |
(define (rms xs) |
||
(sqrt (// (for/sum ((x xs)) (* x x)) (length xs)))) |
(sqrt (// (for/sum ((x xs)) (* x x)) (length xs)))) |
||
Line 647: | Line 647: | ||
{{trans|C#}} |
{{trans|C#}} |
||
ELENA 5.0 : |
ELENA 5.0 : |
||
<syntaxhighlight lang=elena>import extensions; |
<syntaxhighlight lang="elena">import extensions; |
||
import system'routines; |
import system'routines; |
||
import system'math; |
import system'math; |
||
Line 667: | Line 667: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
<syntaxhighlight lang=elixir> |
<syntaxhighlight lang="elixir"> |
||
defmodule RC do |
defmodule RC do |
||
def root_mean_square(enum) do |
def root_mean_square(enum) do |
||
Line 699: | Line 699: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
<syntaxhighlight lang=erlang>rms(Nums) -> |
<syntaxhighlight lang="erlang">rms(Nums) -> |
||
math:sqrt(lists:foldl(fun(E,S) -> S+E*E end, 0, Nums) / length(Nums)). |
math:sqrt(lists:foldl(fun(E,S) -> S+E*E end, 0, Nums) / length(Nums)). |
||
Line 707: | Line 707: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
PROGRAM ROOT_MEAN_SQUARE |
PROGRAM ROOT_MEAN_SQUARE |
||
BEGIN |
BEGIN |
||
Line 721: | Line 721: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
<syntaxhighlight lang=euphoria>function rms(sequence s) |
<syntaxhighlight lang="euphoria">function rms(sequence s) |
||
atom sum |
atom sum |
||
if length(s) = 0 then |
if length(s) = 0 then |
||
Line 741: | Line 741: | ||
===Cell reference expression=== |
===Cell reference expression=== |
||
If values are entered in the cells A1 to A10, the below expression will give the RMS value |
If values are entered in the cells A1 to A10, the below expression will give the RMS value |
||
<syntaxhighlight lang=excel> |
<syntaxhighlight lang="excel"> |
||
=SQRT(SUMSQ($A1:$A10)/COUNT($A1:$A10)) |
=SQRT(SUMSQ($A1:$A10)/COUNT($A1:$A10)) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 755: | Line 755: | ||
{{Works with|Office 365 betas 2021}} |
{{Works with|Office 365 betas 2021}} |
||
<syntaxhighlight lang=lisp>ROOTMEANSQR |
<syntaxhighlight lang="lisp">ROOTMEANSQR |
||
=LAMBDA(xs, |
=LAMBDA(xs, |
||
SQRT(SUMSQ(xs)/COUNT(xs)) |
SQRT(SUMSQ(xs)/COUNT(xs)) |
||
Line 762: | Line 762: | ||
For this test, we assume that the following generic lambda is also bound to the name ENUMFROMTO in the Name Manager: |
For this test, we assume that the following generic lambda is also bound to the name ENUMFROMTO in the Name Manager: |
||
<syntaxhighlight lang=lisp>ENUMFROMTO |
<syntaxhighlight lang="lisp">ENUMFROMTO |
||
=LAMBDA(a, |
=LAMBDA(a, |
||
LAMBDA(z, |
LAMBDA(z, |
||
Line 790: | Line 790: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Uses a lambda expression and function piping. |
Uses a lambda expression and function piping. |
||
<syntaxhighlight lang= |
<syntaxhighlight 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]</syntaxhighlight> |
let res = RMS [1.0..10.0]</syntaxhighlight> |
||
Line 797: | Line 797: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<syntaxhighlight lang=factor>: root-mean-square ( seq -- mean ) |
<syntaxhighlight lang="factor">: root-mean-square ( seq -- mean ) |
||
[ [ sq ] map-sum ] [ length ] bi / sqrt ;</syntaxhighlight> |
[ [ sq ] map-sum ] [ length ] bi / sqrt ;</syntaxhighlight> |
||
Line 804: | Line 804: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
<syntaxhighlight lang=fantom>class Main |
<syntaxhighlight lang="fantom">class Main |
||
{ |
{ |
||
static Float averageRms (Float[] nums) |
static Float averageRms (Float[] nums) |
||
Line 822: | Line 822: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang=forth>: rms ( faddr len -- frms ) |
<syntaxhighlight lang="forth">: rms ( faddr len -- frms ) |
||
dup >r 0e |
dup >r 0e |
||
floats bounds do |
floats bounds do |
||
Line 834: | Line 834: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Assume <math> x </math> stored in array x. |
Assume <math> x </math> stored in array x. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fortran">print *,sqrt( sum(x**2)/size(x) )</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang=freebasic> |
<syntaxhighlight lang="freebasic"> |
||
' FB 1.05.0 Win64 |
' FB 1.05.0 Win64 |
||
Line 867: | Line 867: | ||
=={{header|Futhark}}== |
=={{header|Futhark}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="futhark"> |
||
import "futlib/math" |
import "futlib/math" |
||
Line 875: | Line 875: | ||
=={{header|GEORGE}}== |
=={{header|GEORGE}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="george"> |
||
1, 10 rep (i) |
1, 10 rep (i) |
||
i i | (v) ; |
i i | (v) ; |
||
Line 891: | Line 891: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 913: | Line 913: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
<syntaxhighlight lang=groovy>def quadMean = { list -> |
<syntaxhighlight lang="groovy">def quadMean = { list -> |
||
list == null \ |
list == null \ |
||
? null \ |
? null \ |
||
Line 921: | Line 921: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Test: |
Test: |
||
<syntaxhighlight lang=groovy>def list = 1..10 |
<syntaxhighlight lang="groovy">def list = 1..10 |
||
def Q = quadMean(list) |
def Q = quadMean(list) |
||
println """ |
println """ |
||
Line 933: | Line 933: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Given the <code>mean</code> function defined in [[Averages/Pythagorean means]]: |
Given the <code>mean</code> function defined in [[Averages/Pythagorean means]]: |
||
<syntaxhighlight lang=haskell>main = print $ mean 2 [1 .. 10]</syntaxhighlight> |
<syntaxhighlight lang="haskell">main = print $ mean 2 [1 .. 10]</syntaxhighlight> |
||
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/): |
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/): |
||
<syntaxhighlight lang=haskell>import Data.List (genericLength) |
<syntaxhighlight lang="haskell">import Data.List (genericLength) |
||
rootMeanSquare :: [Double] -> Double |
rootMeanSquare :: [Double] -> Double |
||
Line 948: | Line 948: | ||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="hicest">sum = 0 |
||
DO i = 1, 10 |
DO i = 1, 10 |
||
sum = sum + i^2 |
sum = sum + i^2 |
||
Line 956: | Line 956: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="icon">procedure main() |
||
every put(x := [], 1 to 10) |
every put(x := [], 1 to 10) |
||
writes("x := [ "); every writes(!x," "); write("]") |
writes("x := [ "); every writes(!x," "); write("]") |
||
Line 963: | Line 963: | ||
<syntaxhighlight lang= |
<syntaxhighlight lang="icon">procedure qmean(L[]) #: quadratic mean |
||
local m |
local m |
||
if *L = 0 then fail |
if *L = 0 then fail |
||
Line 971: | Line 971: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="io">rms := method (figs, (figs map(** 2) reduce(+) / figs size) sqrt) |
||
rms( Range 1 to(10) asList ) println</syntaxhighlight> |
rms( Range 1 to(10) asList ) println</syntaxhighlight> |
||
Line 977: | Line 977: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
<syntaxhighlight lang=j>rms=: (+/ % #)&.:*:</syntaxhighlight> |
<syntaxhighlight lang="j">rms=: (+/ % #)&.:*:</syntaxhighlight> |
||
'''Example Usage:''' |
'''Example Usage:''' |
||
<syntaxhighlight lang=j> rms 1 + i. 10 |
<syntaxhighlight lang="j"> rms 1 + i. 10 |
||
6.20484</syntaxhighlight> |
6.20484</syntaxhighlight> |
||
<code>*:</code> means [http://jsoftware.com/help/dictionary/d112.htm square] |
<code>*:</code> means [http://jsoftware.com/help/dictionary/d112.htm square] |
||
Line 989: | Line 989: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang=java>public class RootMeanSquare { |
<syntaxhighlight lang="java">public class RootMeanSquare { |
||
public static double rootMeanSquare(double... nums) { |
public static double rootMeanSquare(double... nums) { |
||
Line 1,010: | Line 1,010: | ||
{{works with|JavaScript|1.8}} |
{{works with|JavaScript|1.8}} |
||
{{works with|Firefox|3.0}} |
{{works with|Firefox|3.0}} |
||
<syntaxhighlight lang=javascript>function root_mean_square(ary) { |
<syntaxhighlight lang="javascript">function root_mean_square(ary) { |
||
var sum_of_squares = ary.reduce(function(s,x) {return (s + x*x)}, 0); |
var sum_of_squares = ary.reduce(function(s,x) {return (s + x*x)}, 0); |
||
return Math.sqrt(sum_of_squares / ary.length); |
return Math.sqrt(sum_of_squares / ary.length); |
||
Line 1,020: | Line 1,020: | ||
===ES6=== |
===ES6=== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,044: | Line 1,044: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
The following filter returns ''null'' if given an empty array: |
The following filter returns ''null'' if given an empty array: |
||
<syntaxhighlight lang=jq>def rms: length as $length |
<syntaxhighlight lang="jq">def rms: length as $length |
||
| if $length == 0 then null |
| if $length == 0 then null |
||
else map(. * .) | add | sqrt / $length |
else map(. * .) | add | sqrt / $length |
||
end ;</syntaxhighlight>With this definition, the following program would compute the rms of each array in a file or stream of numeric arrays:<syntaxhighlight lang=jq>rms</syntaxhighlight> |
end ;</syntaxhighlight>With this definition, the following program would compute the rms of each array in a file or stream of numeric arrays:<syntaxhighlight lang="jq">rms</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{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: |
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: |
||
<syntaxhighlight lang=julia>sqrt(sum(A.^2.) / length(A))</syntaxhighlight> |
<syntaxhighlight lang="julia">sqrt(sum(A.^2.) / length(A))</syntaxhighlight> |
||
or shorter with using Statistics (and as spoken: root-mean-square) |
or shorter with using Statistics (and as spoken: root-mean-square) |
||
<syntaxhighlight lang=julia>sqrt(mean(A.^2.))</syntaxhighlight> |
<syntaxhighlight lang="julia">sqrt(mean(A.^2.))</syntaxhighlight> |
||
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: <syntaxhighlight lang=julia>sqrt(sum(x -> x*x, A) / length(A))</syntaxhighlight> |
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: <syntaxhighlight lang="julia">sqrt(sum(x -> x*x, A) / length(A))</syntaxhighlight> |
||
One can also use an explicit loop for near-C performance |
One can also use an explicit loop for near-C performance |
||
<syntaxhighlight lang=julia> |
<syntaxhighlight lang="julia"> |
||
function rms(A) |
function rms(A) |
||
s = 0.0 |
s = 0.0 |
||
Line 1,065: | Line 1,065: | ||
end |
end |
||
</syntaxhighlight> |
</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): <syntaxhighlight lang=julia>norm(A) / sqrt(length(A))</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): <syntaxhighlight lang="julia">norm(A) / sqrt(length(A))</syntaxhighlight> |
||
=={{header|K}}== |
=={{header|K}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="k"> |
||
rms:{_sqrt (+/x^2)%#x} |
rms:{_sqrt (+/x^2)%#x} |
||
rms 1+!10 |
rms 1+!10 |
||
Line 1,075: | Line 1,075: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
<syntaxhighlight lang=scala>// version 1.0.5-2 |
<syntaxhighlight lang="scala">// version 1.0.5-2 |
||
fun quadraticMean(vector: Array<Double>) : Double { |
fun quadraticMean(vector: Array<Double>) : Double { |
||
Line 1,093: | Line 1,093: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lasso">define rms(a::staticarray)::decimal => { |
||
return math_sqrt((with n in #a sum #n*#n) / decimal(#a->size)) |
return math_sqrt((with n in #a sum #n*#n) / decimal(#a->size)) |
||
} |
} |
||
Line 1,102: | Line 1,102: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang=lb>' [RC] Averages/Root mean square |
<syntaxhighlight lang="lb">' [RC] Averages/Root mean square |
||
SourceList$ ="1 2 3 4 5 6 7 8 9 10" |
SourceList$ ="1 2 3 4 5 6 7 8 9 10" |
||
Line 1,130: | Line 1,130: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<syntaxhighlight lang=logo>to rms :v |
<syntaxhighlight lang="logo">to rms :v |
||
output sqrt quotient (apply "sum map [? * ?] :v) count :v |
output sqrt quotient (apply "sum map [? * ?] :v) count :v |
||
end |
end |
||
Line 1,137: | Line 1,137: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<syntaxhighlight lang=lua>function sumsq(a, ...) return a and a^2 + sumsq(...) or 0 end |
<syntaxhighlight lang="lua">function sumsq(a, ...) return a and a^2 + sumsq(...) or 0 end |
||
function rms(t) return (sumsq(unpack(t)) / #t)^.5 end |
function rms(t) return (sumsq(unpack(t)) / #t)^.5 end |
||
Line 1,143: | Line 1,143: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maple">y := [ seq(1..10) ]: |
||
RMS := proc( x ) |
RMS := proc( x ) |
||
return sqrt( Statistics:-Mean( x ^~ 2 ) ); |
return sqrt( Statistics:-Mean( x ^~ 2 ) ); |
||
Line 1,154: | Line 1,154: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">RootMeanSquare@Range[10]</syntaxhighlight> |
||
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. |
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}}== |
=={{header|MATLAB}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="matlab">function rms = quadraticMean(list) |
||
rms = sqrt(mean(list.^2)); |
rms = sqrt(mean(list.^2)); |
||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
Solution: |
Solution: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="matlab">>> quadraticMean((1:10)) |
||
ans = |
ans = |
||
Line 1,169: | Line 1,169: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
<syntaxhighlight lang=maxima>L: makelist(i, i, 10)$ |
<syntaxhighlight lang="maxima">L: makelist(i, i, 10)$ |
||
rms(L) := sqrt(lsum(x^2, x, L)/length(L))$ |
rms(L) := sqrt(lsum(x^2, x, L)/length(L))$ |
||
Line 1,176: | Line 1,176: | ||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maxscript"> |
||
fn RMS arr = |
fn RMS arr = |
||
( |
( |
||
Line 1,185: | Line 1,185: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
Output: |
Output: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maxscript"> |
||
rms #{1..10} |
rms #{1..10} |
||
6.20484 |
6.20484 |
||
Line 1,192: | Line 1,192: | ||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.6}} |
{{works with|min|0.19.6}} |
||
<syntaxhighlight lang=min>(dup *) :sq |
<syntaxhighlight lang="min">(dup *) :sq |
||
('sq map sum) :sum-sq |
('sq map sum) :sum-sq |
||
(('sum-sq 'size) => cleave / sqrt) :rms |
(('sum-sq 'size) => cleave / sqrt) :rms |
||
Line 1,203: | Line 1,203: | ||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>0 П0 П1 С/П x^2 ИП0 x^2 ИП1 * |
<syntaxhighlight lang="text">0 П0 П1 С/П x^2 ИП0 x^2 ИП1 * |
||
+ ИП1 1 + П1 / КвКор П0 БП |
+ ИП1 1 + П1 / КвКор П0 БП |
||
03</syntaxhighlight> |
03</syntaxhighlight> |
||
Line 1,213: | Line 1,213: | ||
=={{header|Morfa}}== |
=={{header|Morfa}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=morfa> |
<syntaxhighlight lang="morfa"> |
||
import morfa.base; |
import morfa.base; |
||
import morfa.functional.base; |
import morfa.functional.base; |
||
Line 1,235: | Line 1,235: | ||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
using System.Math; |
using System.Math; |
||
Line 1,254: | Line 1,254: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,276: | Line 1,276: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<syntaxhighlight lang=nim>from math import sqrt, sum |
<syntaxhighlight lang="nim">from math import sqrt, sum |
||
from sequtils import mapIt |
from sequtils import mapIt |
||
Line 1,289: | Line 1,289: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Oxford Oberon-2 |
Oxford Oberon-2 |
||
<syntaxhighlight lang=oberon2> |
<syntaxhighlight lang="oberon2"> |
||
MODULE QM; |
MODULE QM; |
||
IMPORT ML := MathL, Out; |
IMPORT ML := MathL, Out; |
||
Line 1,321: | Line 1,321: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
<syntaxhighlight lang=objeck>bundle Default { |
<syntaxhighlight lang="objeck">bundle Default { |
||
class Hello { |
class Hello { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Line 1,341: | Line 1,341: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<syntaxhighlight lang=ocaml>let rms a = |
<syntaxhighlight 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 /. |
||
float_of_int (Array.length a)) |
float_of_int (Array.length a)) |
||
Line 1,351: | Line 1,351: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="oforth">10 seq map(#sq) sum 10.0 / sqrt .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,359: | Line 1,359: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="oorexx">call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1) |
||
call testAverage .array~of(10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, .11) |
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) |
call testAverage .array~of(30, 10, 20, 30, 40, 50, -100, 4.7, -11e2) |
||
Line 1,392: | Line 1,392: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
<syntaxhighlight lang=oz>declare |
<syntaxhighlight lang="oz">declare |
||
fun {Square X} X*X end |
fun {Square X} X*X end |
||
Line 1,410: | Line 1,410: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
General RMS calculation: |
General RMS calculation: |
||
<syntaxhighlight lang=parigp>RMS(v)={ |
<syntaxhighlight lang="parigp">RMS(v)={ |
||
sqrt(sum(i=1,#v,v[i]^2)/#v) |
sqrt(sum(i=1,#v,v[i]^2)/#v) |
||
}; |
}; |
||
Line 1,417: | Line 1,417: | ||
Specific functions for the first ''n'' positive integers: |
Specific functions for the first ''n'' positive integers: |
||
<syntaxhighlight lang=parigp>RMS_first(n)={ |
<syntaxhighlight lang="parigp">RMS_first(n)={ |
||
sqrt((n+1)*(2*n+1)/6) |
sqrt((n+1)*(2*n+1)/6) |
||
}; |
}; |
||
Line 1,425: | Line 1,425: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl>use v5.10.0; |
<syntaxhighlight lang="perl">use v5.10.0; |
||
sub rms |
sub rms |
||
{ |
{ |
||
Line 1,436: | Line 1,436: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<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: #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> |
<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,453: | Line 1,453: | ||
Alternative, same output<br> |
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. |
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. |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<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: #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> |
<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,463: | Line 1,463: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="phixmonti">def rms |
||
0 swap |
0 swap |
||
len for |
len for |
||
Line 1,479: | Line 1,479: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="php"><?php |
||
// Created with PHP 7.0 |
// Created with PHP 7.0 |
||
Line 1,503: | Line 1,503: | ||
{{trans|Prolog}} |
{{trans|Prolog}} |
||
{{works with|Picat}} |
{{works with|Picat}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picat"> |
||
rms(Xs) = Y => |
rms(Xs) = Y => |
||
Sum = sum_of_squares(Xs), |
Sum = sum_of_squares(Xs), |
||
Line 1,525: | Line 1,525: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(scl 5) |
||
(let Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) |
(let Lst (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0) |
||
Line 1,541: | Line 1,541: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="pl/i"> atest: Proc Options(main); |
||
declare A(10) Dec Float(15) static initial (1,2,3,4,5,6,7,8,9,10); |
declare A(10) Dec Float(15) static initial (1,2,3,4,5,6,7,8,9,10); |
||
declare (n,RMS) Dec Float(15); |
declare (n,RMS) Dec Float(15); |
||
Line 1,552: | Line 1,552: | ||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
<syntaxhighlight lang=postscript>/findrms{ |
<syntaxhighlight lang="postscript">/findrms{ |
||
/x exch def |
/x exch def |
||
/sum 0 def |
/sum 0 def |
||
Line 1,573: | Line 1,573: | ||
</pre> |
</pre> |
||
{{libheader|initlib}} |
{{libheader|initlib}} |
||
<syntaxhighlight lang=postscript>[1 10] 1 range dup 0 {dup * +} fold exch length div sqrt</syntaxhighlight> |
<syntaxhighlight lang="postscript">[1 10] 1 range dup 0 {dup * +} fold exch length div sqrt</syntaxhighlight> |
||
=={{header|Potion}}== |
=={{header|Potion}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="potion">rms = (series) : |
||
total = 0.0 |
total = 0.0 |
||
series each (x): total += x * x. |
series each (x): total += x * x. |
||
Line 1,587: | Line 1,587: | ||
=={{header|Powerbuilder}}== |
=={{header|Powerbuilder}}== |
||
<syntaxhighlight lang=powerbuilder>long ll_x, ll_y, ll_product |
<syntaxhighlight lang="powerbuilder">long ll_x, ll_y, ll_product |
||
decimal ld_rms |
decimal ld_rms |
||
Line 1,601: | Line 1,601: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight 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 |
||
return [math]::sqrt($sqsum/$nums.count) |
return [math]::sqrt($sqsum/$nums.count) |
||
Line 1,609: | Line 1,609: | ||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
<syntaxhighlight lang=processing>void setup() { |
<syntaxhighlight lang="processing">void setup() { |
||
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
float[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
||
print(rms(numbers)); |
print(rms(numbers)); |
||
Line 1,627: | Line 1,627: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|GNU Prolog}} |
{{works with|GNU Prolog}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="prolog"> |
||
:- initialization(main). |
:- initialization(main). |
||
Line 1,652: | Line 1,652: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="purebasic">NewList MyList() ; To hold a unknown amount of numbers to calculate |
||
If OpenConsole() |
If OpenConsole() |
||
Line 1,682: | Line 1,682: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|3}} |
{{works with|Python|3}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="python">>>> from math import sqrt |
||
>>> def qmean(num): |
>>> def qmean(num): |
||
return sqrt(sum(n*n for n in num)/len(num)) |
return sqrt(sum(n*n for n in num)/len(num)) |
||
Line 1,694: | Line 1,694: | ||
Alternatively in terms of '''reduce''': |
Alternatively in terms of '''reduce''': |
||
<syntaxhighlight lang=python>from functools import (reduce) |
<syntaxhighlight lang="python">from functools import (reduce) |
||
from math import (sqrt) |
from math import (sqrt) |
||
Line 1,710: | Line 1,710: | ||
=={{header|Qi}}== |
=={{header|Qi}}== |
||
<syntaxhighlight lang=qi>(define rms |
<syntaxhighlight lang="qi">(define rms |
||
R -> (sqrt (/ (APPLY + (MAPCAR * R R)) (length R))))</syntaxhighlight> |
R -> (sqrt (/ (APPLY + (MAPCAR * R R)) (length R))))</syntaxhighlight> |
||
Line 1,718: | Line 1,718: | ||
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>. |
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery">[ $ "bigrat.qky" loadfile ] now! |
||
[ [] swap |
[ [] swap |
||
Line 1,756: | Line 1,756: | ||
The following function works for any vector x: |
The following function works for any vector x: |
||
<syntaxhighlight lang=rsplus>RMS <- function(x, na.rm = F) sqrt(mean(x^2, na.rm = na.rm)) |
<syntaxhighlight lang="rsplus">RMS <- function(x, na.rm = F) sqrt(mean(x^2, na.rm = na.rm)) |
||
RMS(1:10) |
RMS(1:10) |
||
Line 1,768: | Line 1,768: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(define (rms nums) |
(define (rms nums) |
||
Line 1,777: | Line 1,777: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="raku" line>sub rms(*@nums) { sqrt ([+] @nums X** 2) / @nums } |
||
say rms 1..10;</syntaxhighlight> |
say rms 1..10;</syntaxhighlight> |
||
Here's a slightly more concise version, albeit arguably less readable: |
Here's a slightly more concise version, albeit arguably less readable: |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="raku" line>sub rms { sqrt @_ R/ [+] @_ X** 2 }</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,792: | Line 1,792: | ||
<br>calculation as well as a reasonable attempt at providing a first-guess square root by essentially halving |
<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. |
<br>the number using logarithmic (base ten) arithmetic. |
||
<syntaxhighlight lang=rexx>/*REXX program computes and displays the root mean square (RMS) of a number sequence. */ |
<syntaxhighlight lang="rexx">/*REXX program computes and displays the root mean square (RMS) of a number sequence. */ |
||
parse arg nums digs show . /*obtain the optional arguments from CL*/ |
parse arg nums digs show . /*obtain the optional arguments from CL*/ |
||
if nums=='' | nums=="," then nums=10 /*Not specified? Then use the default.*/ |
if nums=='' | nums=="," then nums=10 /*Not specified? Then use the default.*/ |
||
Line 1,817: | Line 1,817: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
nums = [1,2,3,4,5,6,7,8,9,10] |
nums = [1,2,3,4,5,6,7,8,9,10] |
||
sum = 0 |
sum = 0 |
||
Line 1,832: | Line 1,832: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>class Array |
<syntaxhighlight lang="ruby">class Array |
||
def quadratic_mean |
def quadratic_mean |
||
Math.sqrt( self.inject(0.0) {|s, y| s + y*y} / self.length ) |
Math.sqrt( self.inject(0.0) {|s, y| s + y*y} / self.length ) |
||
Line 1,847: | Line 1,847: | ||
and a non object-oriented solution: |
and a non object-oriented solution: |
||
<syntaxhighlight lang=ruby>def rms(seq) |
<syntaxhighlight lang="ruby">def rms(seq) |
||
Math.sqrt(seq.sum{|x| x*x}.fdiv(seq.size) ) |
Math.sqrt(seq.sum{|x| x*x}.fdiv(seq.size) ) |
||
end |
end |
||
Line 1,853: | Line 1,853: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
<syntaxhighlight lang=runbasic>valueList$ = "1 2 3 4 5 6 7 8 9 10" |
<syntaxhighlight lang="runbasic">valueList$ = "1 2 3 4 5 6 7 8 9 10" |
||
while word$(valueList$,i +1) <> "" ' grab values from list |
while word$(valueList$,i +1) <> "" ' grab values from list |
||
thisValue = val(word$(valueList$,i +1)) ' turn values into numbers |
thisValue = val(word$(valueList$,i +1)) ' turn values into numbers |
||
Line 1,867: | Line 1,867: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang=rust>fn root_mean_square(vec: Vec<i32>) -> f32 { |
<syntaxhighlight lang="rust">fn root_mean_square(vec: Vec<i32>) -> f32 { |
||
let sum_squares = vec.iter().fold(0, |acc, &x| acc + x.pow(2)); |
let sum_squares = vec.iter().fold(0, |acc, &x| acc + x.pow(2)); |
||
return ((sum_squares as f32)/(vec.len() as f32)).sqrt(); |
return ((sum_squares as f32)/(vec.len() as f32)).sqrt(); |
||
Line 1,888: | Line 1,888: | ||
built-in syntax. |
built-in syntax. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="s-lang">define rms(arr) |
||
{ |
{ |
||
return sqrt(sum(sqr(arr)) / length(arr)); |
return sqrt(sum(sqr(arr)) / length(arr)); |
||
Line 1,896: | Line 1,896: | ||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
<syntaxhighlight lang=sather>class MAIN is |
<syntaxhighlight lang="sather">class MAIN is |
||
-- irrms stands for Integer Ranged RMS |
-- irrms stands for Integer Ranged RMS |
||
irrms(i, f:INT):FLT |
irrms(i, f:INT):FLT |
||
Line 1,914: | Line 1,914: | ||
=={{header|S-BASIC}}== |
=={{header|S-BASIC}}== |
||
<syntaxhighlight lang=basic> |
<syntaxhighlight lang="basic"> |
||
var n, sqsum, sqmean, rms = real |
var n, sqsum, sqmean, rms = real |
||
sqsum = 0 |
sqsum = 0 |
||
Line 1,932: | Line 1,932: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
<syntaxhighlight lang=scala>def rms(nums: Seq[Int]) = math.sqrt(nums.map(math.pow(_, 2)).sum / nums.size) |
<syntaxhighlight lang="scala">def rms(nums: Seq[Int]) = math.sqrt(nums.map(math.pow(_, 2)).sum / nums.size) |
||
println(rms(1 to 10))</syntaxhighlight> |
println(rms(1 to 10))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,938: | Line 1,938: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<syntaxhighlight lang=scheme>(define (rms nums) |
<syntaxhighlight lang="scheme">(define (rms nums) |
||
(sqrt (/ (apply + (map * nums nums)) |
(sqrt (/ (apply + (map * nums nums)) |
||
(length nums)))) |
(length nums)))) |
||
Line 1,947: | Line 1,947: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
include "math.s7i"; |
include "math.s7i"; |
||
Line 1,973: | Line 1,973: | ||
=={{header|Shen}}== |
=={{header|Shen}}== |
||
{{works with|shen-scheme|0.17}} |
{{works with|shen-scheme|0.17}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="shen">(declare scm.sqrt [number --> number]) |
||
(tc +) |
(tc +) |
||
Line 2,001: | Line 2,001: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang=ruby>func rms(a) { |
<syntaxhighlight lang="ruby">func rms(a) { |
||
sqrt(a.map{.**2}.sum / a.len) |
sqrt(a.map{.**2}.sum / a.len) |
||
} |
} |
||
Line 2,008: | Line 2,008: | ||
Using hyper operators, we can write it as: |
Using hyper operators, we can write it as: |
||
<syntaxhighlight lang=ruby>func rms(a) { a »**» 2 «+» / a.len -> sqrt }</syntaxhighlight> |
<syntaxhighlight lang="ruby">func rms(a) { a »**» 2 «+» / a.len -> sqrt }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,014: | Line 2,014: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
<syntaxhighlight lang=smalltalk>(((1 to: 10) inject: 0 into: [ :s :n | n*n + s ]) / 10) sqrt.</syntaxhighlight> |
<syntaxhighlight lang="smalltalk">(((1 to: 10) inject: 0 into: [ :s :n | n*n + s ]) / 10) sqrt.</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Line 2,020: | Line 2,020: | ||
{{works with|CSnobol}} |
{{works with|CSnobol}} |
||
There is no built-in sqrt( ) function in Snobol4+. |
There is no built-in sqrt( ) function in Snobol4+. |
||
<syntaxhighlight lang= |
<syntaxhighlight 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) |
||
rms = sqrt(1.0 * ssq / prototype(a)) :(return) |
rms = sqrt(1.0 * ssq / prototype(a)) :(return) |
||
Line 2,034: | Line 2,034: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<syntaxhighlight lang=sml>fun rms(v: real vector) = |
<syntaxhighlight lang="sml">fun rms(v: real vector) = |
||
let |
let |
||
val v' = Vector.map (fn x => x*x) v |
val v' = Vector.map (fn x => x*x) v |
||
Line 2,049: | Line 2,049: | ||
Compute the RMS of a variable and return the result in r(rms). |
Compute the RMS of a variable and return the result in r(rms). |
||
<syntaxhighlight lang=stata>program rms, rclass |
<syntaxhighlight lang="stata">program rms, rclass |
||
syntax varname(numeric) [if] [in] |
syntax varname(numeric) [if] [in] |
||
tempvar x |
tempvar x |
||
Line 2,059: | Line 2,059: | ||
'''Example''' |
'''Example''' |
||
<syntaxhighlight lang=stata>clear |
<syntaxhighlight lang="stata">clear |
||
set obs 20 |
set obs 20 |
||
gen x=rnormal() |
gen x=rnormal() |
||
Line 2,073: | Line 2,073: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<syntaxhighlight lang=swift>extension Collection where Element: FloatingPoint { |
<syntaxhighlight lang="swift">extension Collection where Element: FloatingPoint { |
||
@inlinable |
@inlinable |
||
public func rms() -> Element { |
public func rms() -> Element { |
||
Line 2,088: | Line 2,088: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.5}} |
{{works with|Tcl|8.5}} |
||
<syntaxhighlight lang=tcl>proc qmean list { |
<syntaxhighlight lang="tcl">proc qmean list { |
||
set sum 0.0 |
set sum 0.0 |
||
foreach value $list { set sum [expr {$sum + $value**2}] } |
foreach value $list { set sum [expr {$sum + $value**2}] } |
||
Line 2,102: | Line 2,102: | ||
=={{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 |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ursala">#import nat |
||
#import flo |
#import flo |
||
Line 2,115: | Line 2,115: | ||
=={{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. |
||
<syntaxhighlight lang=vala>double rms(double[] list){ |
<syntaxhighlight lang="vala">double rms(double[] list){ |
||
double sum_squares = 0; |
double sum_squares = 0; |
||
double mean; |
double mean; |
||
Line 2,141: | Line 2,141: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
Using Excel VBA |
Using Excel VBA |
||
<syntaxhighlight lang=vb>Private Function root_mean_square(s() As Variant) As Double |
<syntaxhighlight lang="vb">Private Function root_mean_square(s() As Variant) As Double |
||
For i = 1 To UBound(s) |
For i = 1 To UBound(s) |
||
s(i) = s(i) ^ 2 |
s(i) = s(i) ^ 2 |
||
Line 2,153: | Line 2,153: | ||
End Sub</syntaxhighlight> |
End Sub</syntaxhighlight> |
||
Without using Excel worksheetfunction: |
Without using Excel worksheetfunction: |
||
<syntaxhighlight lang=vb>Function rms(iLow As Integer, iHigh As Integer) |
<syntaxhighlight lang="vb">Function rms(iLow As Integer, iHigh As Integer) |
||
Dim i As Integer |
Dim i As Integer |
||
If iLow > iHigh Then |
If iLow > iHigh Then |
||
Line 2,177: | Line 2,177: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
<syntaxhighlight lang=vlang>import math |
<syntaxhighlight lang="vlang">import math |
||
fn main() { |
fn main() { |
||
Line 2,194: | Line 2,194: | ||
=={{header|Wortel}}== |
=={{header|Wortel}}== |
||
<syntaxhighlight lang=wortel>@let { |
<syntaxhighlight lang="wortel">@let { |
||
; using a composition and a fork (like you would do in J) |
; using a composition and a fork (like you would do in J) |
||
rms1 ^(@sqrt @(@sum / #) *^@sq) |
rms1 ^(@sqrt @(@sum / #) *^@sq) |
||
Line 2,210: | Line 2,210: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
<syntaxhighlight lang=ecmascript>var rms = ((1..10).reduce(0) { |acc, i| acc + i*i }/10).sqrt |
<syntaxhighlight lang="ecmascript">var rms = ((1..10).reduce(0) { |acc, i| acc + i*i }/10).sqrt |
||
System.print(rms)</syntaxhighlight> |
System.print(rms)</syntaxhighlight> |
||
Line 2,219: | Line 2,219: | ||
=={{header|XLISP}}== |
=={{header|XLISP}}== |
||
<syntaxhighlight lang=lisp>(defun quadratic-mean (xs) |
<syntaxhighlight lang="lisp">(defun quadratic-mean (xs) |
||
(sqrt |
(sqrt |
||
(/ |
(/ |
||
Line 2,239: | Line 2,239: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="xpl0">code CrLf=9; |
||
code real RlOut=48; |
code real RlOut=48; |
||
int N; |
int N; |
||
Line 2,254: | Line 2,254: | ||
=={{header|Yacas}}== |
=={{header|Yacas}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="yacas">Sqrt(Add((1 .. 10)^2)/10)</syntaxhighlight> |
||
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>'. |
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}}== |
=={{header|zkl}}== |
||
<syntaxhighlight lang=zkl>fcn rms(z){ ( z.reduce(fcn(p,n){ p + n*n },0.0) /z.len() ).sqrt() }</syntaxhighlight> |
<syntaxhighlight lang="zkl">fcn rms(z){ ( z.reduce(fcn(p,n){ p + n*n },0.0) /z.len() ).sqrt() }</syntaxhighlight> |
||
The order in the reduce function is important as it coerces n*n to float. |
The order in the reduce function is important as it coerces n*n to float. |
||
<pre> |
<pre> |