-
-
Save reusee/5756793 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package main | |
| import ( | |
| "math" | |
| "math/rand" | |
| "fmt" | |
| "sync" | |
| ) | |
| func main() { | |
| n := 1000000 | |
| numbers := make([]float32, n) | |
| max := float32(0) | |
| var x float32 | |
| for i := 0; i < n; i++ { | |
| x = rand.Float32() | |
| numbers[i] = x | |
| if x > max { | |
| max = x | |
| } | |
| } | |
| fmt.Printf("Inf-norm = %v\n", max) | |
| wg := new(sync.WaitGroup) | |
| for i := 1; i <= 100; i++ { | |
| wg.Add(1) | |
| go func(i int) { | |
| fmt.Printf("%v norm = %v\n", i, pnorm(numbers, i)) | |
| wg.Done() | |
| }(i) | |
| } | |
| wg.Wait() | |
| } | |
| func pnorm(nums []float32, p int) float64 { | |
| acc := float64(0) | |
| for _, n := range nums { | |
| acc = acc + math.Pow(float64(n), float64(p)) | |
| } | |
| return math.Pow(acc, float64(1) / float64(p)) | |
| } |
This file contains hidden or 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
| extern mod std; | |
| extern mod extra; | |
| use extra::arc::ARC; | |
| use std::*; | |
| fn main() { | |
| let numbers = vec::from_fn(1000000, |_| rand::random::<float>()); | |
| println(fmt!("Inf-norm = %?", numbers.max())); | |
| let numbers_arc = ARC(numbers); | |
| for uint::range(1, 100) |num| { | |
| let (port, chan) = stream(); | |
| chan.send(numbers_arc.clone()); | |
| do spawn { | |
| let local_arc: ARC<~[float]> = port.recv(); | |
| let task_numbers = local_arc.get(); | |
| println(fmt!("%u-norm = %?", num, pnorm(task_numbers, num))); | |
| } | |
| } | |
| } | |
| fn pnorm(nums: &~[float], p: uint) -> float { | |
| (vec::foldl(0.0, *nums, |a,b| a+(*b).pow(p as float) )).pow(1f / (p as float)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment