Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. func cal(X: [Int], Y: [Int64]) -> Double {
  2. let total = Double(Y.reduce(0, +))
  3. var z = zip(X, Y).map { pow(Double($0), 2) * Double($1) }.reduce(0, +) / Double(total)
  4. return z
  5. }
  6.  
  7. func cal(xs: [Int], ys: [Int64]) -> Double {
  8. let xs = xs.lazy.map(Double.init)
  9. let ys = ys.lazy.map(Double.init)
  10.  
  11. let total = ys.reduce(0, +)
  12. return zip(xs, ys).map { $0*$0 * $1 }.reduce(0, +) / total
  13. }
  14.  
  15. extension Sequence where Element: Numeric {
  16. func sum() -> Element {
  17. return reduce(0, +)
  18. }
  19. }
  20.  
  21. func cal(xs: [Int], ys: [Int64]) -> Double {
  22. let xs = xs.lazy.map(Double.init)
  23. let ys = ys.lazy.map(Double.init)
  24.  
  25. let total = ys.sum()
  26. return zip(xs, ys).map { $0*$0 * $1 }.sum() / total
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement