Created
September 2, 2011 04:21
-
-
Save mythz/1187913 to your computer and use it in GitHub Desktop.
Generate a weighted sequence of numbers with an optional multiplier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate a sequence of numbers: | |
# - ordered with the highest values | |
# - has a sum of 1 | |
# - has an optional multiplier to alter the decreasing rate | |
sequence = (n,m=0) -> | |
f = (p,multiplier) -> p * p + multiplier | |
s = "" | |
max = n * n | |
sum = 0 | |
sum += f(p,m) for p in [1..n] | |
total = 0 | |
for p in [1..n] | |
r = n - p + 1 | |
a = f(r,m) / sum | |
total += a | |
s += ", " if s | |
s += a.toFixed 4 | |
"#{s}\nfor n: #{n}, m: #{m}, total: #{total}\n" | |
print = (rest...) -> console.log rest... | |
print sequence 3 # 0.6429, 0.2857, 0.0714 | |
print sequence 5, -0.5 # 0.4667, 0.2952, 0.1619, 0.0667, 0.0095 | |
print sequence 5, 0 # 0.4545, 0.2909, 0.1636, 0.0727, 0.0182 | |
print sequence 5, 1.5 # 0.4240, 0.2800, 0.1680, 0.0880, 0.0400 | |
print sequence 5, 2 # 0.4154, 0.2769, 0.1692, 0.0923, 0.0462 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
let sequence n normalizer = | |
let f p normalizer = p * p + normalizer | |
let sum = | |
[1.0..n] | |
|> Seq.map (fun p -> f p normalizer) | |
|> Seq.sum | |
let seq = | |
[1.0..n] | |
|> Seq.map (fun p -> f (n - p + 1.0) normalizer / sum) | |
let sb = new System.Text.StringBuilder() | |
seq | |
|> Seq.map (fun i -> Math.Round(i,2).ToString()) | |
|> Seq.iter (fun i -> sb.Append((if sb.Length > 0 then ", " else "") + i) |> ignore) | |
let total = seq |> Seq.sum | |
sprintf "%A\nfor n: %f, normalizer: %f, total %f" sb n normalizer total | |
printfn "%s" (sequence 3.0 0.0) | |
printfn "%s" (sequence 5.0 -0.5) | |
printfn "%s" (sequence 5.0 0.0) | |
printfn "%s" (sequence 5.0 1.5) | |
printfn "%s" (sequence 5.0 2.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#JavaScript output | |
var print, sequence; | |
var __slice = Array.prototype.slice; | |
sequence = function(n, m) { | |
var a, f, max, p, r, s, sum, total; | |
if (m == null) { | |
m = 0; | |
} | |
f = function(p, multiplier) { | |
return p * p + multiplier; | |
}; | |
s = ""; | |
max = n * n; | |
sum = 0; | |
for (p = 1; 1 <= n ? p <= n : p >= n; 1 <= n ? p++ : p--) { | |
sum += f(p, m); | |
} | |
total = 0; | |
for (p = 1; 1 <= n ? p <= n : p >= n; 1 <= n ? p++ : p--) { | |
r = n - p + 1; | |
a = f(r, m) / sum; | |
total += a; | |
if (s) { | |
s += ", "; | |
} | |
s += a.toFixed(4); | |
} | |
return "" + s + "\nfor n: " + n + ", m: " + m + ", total: " + total + "\n"; | |
}; | |
print = function() { | |
var rest; | |
rest = 1 <= arguments.length ? __slice.call(arguments, 0) : []; | |
return console.log.apply(console, rest); | |
}; | |
print(sequence(3)); | |
print(sequence(5, -0.5)); | |
print(sequence(5, 0)); | |
print(sequence(5, 1.5)); | |
print(sequence(5, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment