Averages/Root mean square: Difference between revisions
rms in ocaml |
|||
Line 75: | Line 75: | ||
<lang ocaml>let rms a = let n = Array.length a in |
<lang ocaml>let rms a = let n = Array.length a in |
||
let |
let f s x = s +. x*.x in |
||
sqrt ( |
sqrt (Array.fold_left f 0.0 a /. float_of_int n);; |
||
rms (Array.init 10 ( |
rms (Array.init 10 (fun i -> float_of_int (i+1)));; |
||
(* 6.2048368229954285 *)</lang> |
(* 6.2048368229954285 *)</lang> |
||
Revision as of 19:21, 10 March 2010
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Compute the Root mean square of the numbers 1..10.
The root mean square is also known by its initial RMS (or rms), and as the quadratic mean.
The RMS is calculated as the mean of the squares of the numbers, square-rooted:
C.f. Averages/Pythagorean means
Clojure
<lang clojure>(use '[clojure.contrib.math :only (sqrt)])
(defn rms [xs]
(sqrt (/ (reduce + (map #(* % %) xs))
(count xs))))
(println (rms (range 1 11)))</lang>
Output:
6.2048368229954285
Factor
<lang factor>: root-mean-square ( seq -- mean )
[ [ sq ] map-sum ] [ length ] bi / sqrt ;</lang>
( scratchpad ) 10 [1,b] root-mean-square . 6.204836822995428
Forth
<lang forth>: rms ( faddr len -- frms )
dup >r 0e floats bounds do i f@ fdup f* f+ float +loop r> s>f f/ fsqrt ;
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</lang>
Fortran
Assume stored in array x.
<lang Fortran>print *,sqrt( sum(x**2)/size(x) )</lang>
Haskell
Given the mean
function defiend in Averages/Pythagorean means:
<lang haskell>main = print $ mean 2 [1 .. 10]</lang>
J
Solution: <lang j>rms=: (+/ % #)&.:*:</lang>
Example Usage: <lang j> rms >: i. 10 6.20484</lang>
Logo
<lang logo>to rms :v
output sqrt quotient (apply "sum map [? * ?] :v) count :v
end
show rms iseq 1 10</lang>
Lua
<lang lua>function sumsq(a, ...) return a and a^2 + sumsq(...) or 0 end function rms(t) return (sumsq(unpack(t)) / #t)^.5 end
print(rms{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})</lang>
OCaml
<lang ocaml>let rms a = let n = Array.length a in let f s x = s +. x*.x in sqrt (Array.fold_left f 0.0 a /. float_of_int n);;
rms (Array.init 10 (fun i -> float_of_int (i+1)));; (* 6.2048368229954285 *)</lang>
Oz
<lang oz>declare
fun {Square X} X*X end
fun {RMS Xs} {Sqrt {Int.toFloat {FoldL {Map Xs Square} Number.'+' 0}} / {Int.toFloat {Length Xs}}} end
in
{Show {RMS {List.number 1 10 1}}}</lang>
Output:
6.2048
Perl
<lang perl>sub rms {
my $r = 0; $r += $_**2 for @_; return sqrt( $r/@_ );
}
my @ints = (1..10); print rms(@ints), "\n";</lang>
PL/I
<lang PL/I> declare A(10) fixed decimal static initial (1,2,3,4,5,6,7,8,9,10); n = hbound(A,1); RMS = sqrt(sum(A**2)/n); </lang>
PureBasic
When compiled for Windows x86 using PureBasic 4.41 , this program is only 6.50 kB.
<lang PureBasic> NewList MyList() ; To hold a unknown amount of numbers to calculate
If OpenConsole()
Define.d result Define i, sum_of_squares ;Populate a random amounts of numbers to calculate For i=0 To (Random(45)+5) ; max elements is unknown to the program AddElement(MyList()) MyList()=Random(15) ; Put in a random number Next
Print("Averages/Root mean square"+#CRLF$+"of : ")
; Calculate square of each element, print each & add them together ForEach MyList() Print(Str(MyList())+" ") ; Present to our user sum_of_squares+MyList()*MyList() ; Sum the squares, e.g Next
;Present the result result=Sqr(sum_of_squares/ListSize(MyList())) PrintN(#CRLF$+"= "+StrD(result)) PrintN("Press ENTER to exit"): Input() CloseConsole()
EndIf </lang>
Python
<lang Python>>>> from __future__ import division >>> from math import sqrt >>> def qmean(num): return sqrt(sum(n*n for n in num)/len(num))
>>> numbers = range(1,11) # 1..10 >>> qmean(numbers) 6.2048368229954285</lang>
R
<lang R> sqrt(sum((1:10)^2/10)) </lang> or generally, for x <lang R> x<-1:10 sqrt(sum((x)^2/length(x))) </lang>
Ruby
<lang ruby>class Array
def quadratic_mean Math.sqrt( self.inject(0) {|s, y| s += y*y}.to_f / self.length ) end
end
class Range
def quadratic_mean self.to_a.quadratic_mean end
end
(1..10).quadratic_mean # => 6.20483682299543</lang>
Tcl
<lang tcl>proc qmean list {
set sum 0.0 foreach value $list { set sum [expr {$sum + $value**2}] } return [expr { sqrt($sum / [llength $list]) }]
}
puts "RMS(1..10) = [qmean {1 2 3 4 5 6 7 8 9 10}]"</lang> Output:
RMS(1..10) = 6.2048368229954285
Ursala
using the mean
function among others from the flo
library
<lang Ursala>
- import nat
- import flo
- cast %e
rms = sqrt mean sqr* float* nrange(1,10) </lang> output:
6.204837e+00