floats.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. // Copyright ©2013 The Gonum Authors. All rights reserved.
  2. // Use of this code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package floats
  5. import (
  6. "errors"
  7. "math"
  8. "sort"
  9. "gonum.org/v1/gonum/floats/scalar"
  10. "gonum.org/v1/gonum/internal/asm/f64"
  11. )
  12. const (
  13. zeroLength = "floats: zero length slice"
  14. shortSpan = "floats: slice length less than 2"
  15. badLength = "floats: slice lengths do not match"
  16. badDstLength = "floats: destination slice length does not match input"
  17. )
  18. // Add adds, element-wise, the elements of s and dst, and stores the result in dst.
  19. // It panics if the argument lengths do not match.
  20. func Add(dst, s []float64) {
  21. if len(dst) != len(s) {
  22. panic(badDstLength)
  23. }
  24. f64.AxpyUnitaryTo(dst, 1, s, dst)
  25. }
  26. // AddTo adds, element-wise, the elements of s and t and
  27. // stores the result in dst.
  28. // It panics if the argument lengths do not match.
  29. func AddTo(dst, s, t []float64) []float64 {
  30. if len(s) != len(t) {
  31. panic(badLength)
  32. }
  33. if len(dst) != len(s) {
  34. panic(badDstLength)
  35. }
  36. f64.AxpyUnitaryTo(dst, 1, s, t)
  37. return dst
  38. }
  39. // AddConst adds the scalar c to all of the values in dst.
  40. func AddConst(c float64, dst []float64) {
  41. f64.AddConst(c, dst)
  42. }
  43. // AddScaled performs dst = dst + alpha * s.
  44. // It panics if the slice argument lengths do not match.
  45. func AddScaled(dst []float64, alpha float64, s []float64) {
  46. if len(dst) != len(s) {
  47. panic(badLength)
  48. }
  49. f64.AxpyUnitaryTo(dst, alpha, s, dst)
  50. }
  51. // AddScaledTo performs dst = y + alpha * s, where alpha is a scalar,
  52. // and dst, y and s are all slices.
  53. // It panics if the slice argument lengths do not match.
  54. //
  55. // At the return of the function, dst[i] = y[i] + alpha * s[i]
  56. func AddScaledTo(dst, y []float64, alpha float64, s []float64) []float64 {
  57. if len(s) != len(y) {
  58. panic(badLength)
  59. }
  60. if len(dst) != len(y) {
  61. panic(badDstLength)
  62. }
  63. f64.AxpyUnitaryTo(dst, alpha, s, y)
  64. return dst
  65. }
  66. // argsort is a helper that implements sort.Interface, as used by
  67. // Argsort and ArgsortStable.
  68. type argsort struct {
  69. s []float64
  70. inds []int
  71. }
  72. func (a argsort) Len() int {
  73. return len(a.s)
  74. }
  75. func (a argsort) Less(i, j int) bool {
  76. return a.s[i] < a.s[j]
  77. }
  78. func (a argsort) Swap(i, j int) {
  79. a.s[i], a.s[j] = a.s[j], a.s[i]
  80. a.inds[i], a.inds[j] = a.inds[j], a.inds[i]
  81. }
  82. // Argsort sorts the elements of dst while tracking their original order.
  83. // At the conclusion of Argsort, dst will contain the original elements of dst
  84. // but sorted in increasing order, and inds will contain the original position
  85. // of the elements in the slice such that dst[i] = origDst[inds[i]].
  86. // It panics if the argument lengths do not match.
  87. func Argsort(dst []float64, inds []int) {
  88. if len(dst) != len(inds) {
  89. panic(badDstLength)
  90. }
  91. for i := range dst {
  92. inds[i] = i
  93. }
  94. a := argsort{s: dst, inds: inds}
  95. sort.Sort(a)
  96. }
  97. // ArgsortStable sorts the elements of dst while tracking their original order and
  98. // keeping the original order of equal elements. At the conclusion of ArgsortStable,
  99. // dst will contain the original elements of dst but sorted in increasing order,
  100. // and inds will contain the original position of the elements in the slice such
  101. // that dst[i] = origDst[inds[i]].
  102. // It panics if the argument lengths do not match.
  103. func ArgsortStable(dst []float64, inds []int) {
  104. if len(dst) != len(inds) {
  105. panic(badDstLength)
  106. }
  107. for i := range dst {
  108. inds[i] = i
  109. }
  110. a := argsort{s: dst, inds: inds}
  111. sort.Stable(a)
  112. }
  113. // Count applies the function f to every element of s and returns the number
  114. // of times the function returned true.
  115. func Count(f func(float64) bool, s []float64) int {
  116. var n int
  117. for _, val := range s {
  118. if f(val) {
  119. n++
  120. }
  121. }
  122. return n
  123. }
  124. // CumProd finds the cumulative product of the first i elements in
  125. // s and puts them in place into the ith element of the
  126. // destination dst.
  127. // It panics if the argument lengths do not match.
  128. //
  129. // At the return of the function, dst[i] = s[i] * s[i-1] * s[i-2] * ...
  130. func CumProd(dst, s []float64) []float64 {
  131. if len(dst) != len(s) {
  132. panic(badDstLength)
  133. }
  134. if len(dst) == 0 {
  135. return dst
  136. }
  137. return f64.CumProd(dst, s)
  138. }
  139. // CumSum finds the cumulative sum of the first i elements in
  140. // s and puts them in place into the ith element of the
  141. // destination dst.
  142. // It panics if the argument lengths do not match.
  143. //
  144. // At the return of the function, dst[i] = s[i] + s[i-1] + s[i-2] + ...
  145. func CumSum(dst, s []float64) []float64 {
  146. if len(dst) != len(s) {
  147. panic(badDstLength)
  148. }
  149. if len(dst) == 0 {
  150. return dst
  151. }
  152. return f64.CumSum(dst, s)
  153. }
  154. // Distance computes the L-norm of s - t. See Norm for special cases.
  155. // It panics if the slice argument lengths do not match.
  156. func Distance(s, t []float64, L float64) float64 {
  157. if len(s) != len(t) {
  158. panic(badLength)
  159. }
  160. if len(s) == 0 {
  161. return 0
  162. }
  163. if L == 2 {
  164. return f64.L2DistanceUnitary(s, t)
  165. }
  166. var norm float64
  167. if L == 1 {
  168. for i, v := range s {
  169. norm += math.Abs(t[i] - v)
  170. }
  171. return norm
  172. }
  173. if math.IsInf(L, 1) {
  174. for i, v := range s {
  175. absDiff := math.Abs(t[i] - v)
  176. if absDiff > norm {
  177. norm = absDiff
  178. }
  179. }
  180. return norm
  181. }
  182. for i, v := range s {
  183. norm += math.Pow(math.Abs(t[i]-v), L)
  184. }
  185. return math.Pow(norm, 1/L)
  186. }
  187. // Div performs element-wise division dst / s
  188. // and stores the value in dst.
  189. // It panics if the argument lengths do not match.
  190. func Div(dst, s []float64) {
  191. if len(dst) != len(s) {
  192. panic(badLength)
  193. }
  194. f64.Div(dst, s)
  195. }
  196. // DivTo performs element-wise division s / t
  197. // and stores the value in dst.
  198. // It panics if the argument lengths do not match.
  199. func DivTo(dst, s, t []float64) []float64 {
  200. if len(s) != len(t) {
  201. panic(badLength)
  202. }
  203. if len(dst) != len(s) {
  204. panic(badDstLength)
  205. }
  206. return f64.DivTo(dst, s, t)
  207. }
  208. // Dot computes the dot product of s1 and s2, i.e.
  209. // sum_{i = 1}^N s1[i]*s2[i].
  210. // It panics if the argument lengths do not match.
  211. func Dot(s1, s2 []float64) float64 {
  212. if len(s1) != len(s2) {
  213. panic(badLength)
  214. }
  215. return f64.DotUnitary(s1, s2)
  216. }
  217. // Equal returns true when the slices have equal lengths and
  218. // all elements are numerically identical.
  219. func Equal(s1, s2 []float64) bool {
  220. if len(s1) != len(s2) {
  221. return false
  222. }
  223. for i, val := range s1 {
  224. if s2[i] != val {
  225. return false
  226. }
  227. }
  228. return true
  229. }
  230. // EqualApprox returns true when the slices have equal lengths and
  231. // all element pairs have an absolute tolerance less than tol or a
  232. // relative tolerance less than tol.
  233. func EqualApprox(s1, s2 []float64, tol float64) bool {
  234. if len(s1) != len(s2) {
  235. return false
  236. }
  237. for i, a := range s1 {
  238. if !scalar.EqualWithinAbsOrRel(a, s2[i], tol, tol) {
  239. return false
  240. }
  241. }
  242. return true
  243. }
  244. // EqualFunc returns true when the slices have the same lengths
  245. // and the function returns true for all element pairs.
  246. func EqualFunc(s1, s2 []float64, f func(float64, float64) bool) bool {
  247. if len(s1) != len(s2) {
  248. return false
  249. }
  250. for i, val := range s1 {
  251. if !f(val, s2[i]) {
  252. return false
  253. }
  254. }
  255. return true
  256. }
  257. // EqualLengths returns true when all of the slices have equal length,
  258. // and false otherwise. It also returns true when there are no input slices.
  259. func EqualLengths(slices ...[]float64) bool {
  260. // This length check is needed: http://play.golang.org/p/sdty6YiLhM
  261. if len(slices) == 0 {
  262. return true
  263. }
  264. l := len(slices[0])
  265. for i := 1; i < len(slices); i++ {
  266. if len(slices[i]) != l {
  267. return false
  268. }
  269. }
  270. return true
  271. }
  272. // Find applies f to every element of s and returns the indices of the first
  273. // k elements for which the f returns true, or all such elements
  274. // if k < 0.
  275. // Find will reslice inds to have 0 length, and will append
  276. // found indices to inds.
  277. // If k > 0 and there are fewer than k elements in s satisfying f,
  278. // all of the found elements will be returned along with an error.
  279. // At the return of the function, the input inds will be in an undetermined state.
  280. func Find(inds []int, f func(float64) bool, s []float64, k int) ([]int, error) {
  281. // inds is also returned to allow for calling with nil.
  282. // Reslice inds to have zero length.
  283. inds = inds[:0]
  284. // If zero elements requested, can just return.
  285. if k == 0 {
  286. return inds, nil
  287. }
  288. // If k < 0, return all of the found indices.
  289. if k < 0 {
  290. for i, val := range s {
  291. if f(val) {
  292. inds = append(inds, i)
  293. }
  294. }
  295. return inds, nil
  296. }
  297. // Otherwise, find the first k elements.
  298. nFound := 0
  299. for i, val := range s {
  300. if f(val) {
  301. inds = append(inds, i)
  302. nFound++
  303. if nFound == k {
  304. return inds, nil
  305. }
  306. }
  307. }
  308. // Finished iterating over the loop, which means k elements were not found.
  309. return inds, errors.New("floats: insufficient elements found")
  310. }
  311. // HasNaN returns true when the slice s has any values that are NaN and false
  312. // otherwise.
  313. func HasNaN(s []float64) bool {
  314. for _, v := range s {
  315. if math.IsNaN(v) {
  316. return true
  317. }
  318. }
  319. return false
  320. }
  321. // LogSpan returns a set of n equally spaced points in log space between,
  322. // l and u where N is equal to len(dst). The first element of the
  323. // resulting dst will be l and the final element of dst will be u.
  324. // It panics if the length of dst is less than 2.
  325. // Note that this call will return NaNs if either l or u are negative, and
  326. // will return all zeros if l or u is zero.
  327. // Also returns the mutated slice dst, so that it can be used in range, like:
  328. //
  329. // for i, x := range LogSpan(dst, l, u) { ... }
  330. func LogSpan(dst []float64, l, u float64) []float64 {
  331. Span(dst, math.Log(l), math.Log(u))
  332. for i := range dst {
  333. dst[i] = math.Exp(dst[i])
  334. }
  335. return dst
  336. }
  337. // LogSumExp returns the log of the sum of the exponentials of the values in s.
  338. // Panics if s is an empty slice.
  339. func LogSumExp(s []float64) float64 {
  340. // Want to do this in a numerically stable way which avoids
  341. // overflow and underflow
  342. // First, find the maximum value in the slice.
  343. maxval := Max(s)
  344. if math.IsInf(maxval, 0) {
  345. // If it's infinity either way, the logsumexp will be infinity as well
  346. // returning now avoids NaNs
  347. return maxval
  348. }
  349. var lse float64
  350. // Compute the sumexp part
  351. for _, val := range s {
  352. lse += math.Exp(val - maxval)
  353. }
  354. // Take the log and add back on the constant taken out
  355. return math.Log(lse) + maxval
  356. }
  357. // Max returns the maximum value in the input slice. If the slice is empty, Max will panic.
  358. func Max(s []float64) float64 {
  359. return s[MaxIdx(s)]
  360. }
  361. // MaxIdx returns the index of the maximum value in the input slice. If several
  362. // entries have the maximum value, the first such index is returned.
  363. // It panics if s is zero length.
  364. func MaxIdx(s []float64) int {
  365. if len(s) == 0 {
  366. panic(zeroLength)
  367. }
  368. max := math.NaN()
  369. var ind int
  370. for i, v := range s {
  371. if math.IsNaN(v) {
  372. continue
  373. }
  374. if v > max || math.IsNaN(max) {
  375. max = v
  376. ind = i
  377. }
  378. }
  379. return ind
  380. }
  381. // Min returns the minimum value in the input slice.
  382. // It panics if s is zero length.
  383. func Min(s []float64) float64 {
  384. return s[MinIdx(s)]
  385. }
  386. // MinIdx returns the index of the minimum value in the input slice. If several
  387. // entries have the minimum value, the first such index is returned.
  388. // It panics if s is zero length.
  389. func MinIdx(s []float64) int {
  390. if len(s) == 0 {
  391. panic(zeroLength)
  392. }
  393. min := math.NaN()
  394. var ind int
  395. for i, v := range s {
  396. if math.IsNaN(v) {
  397. continue
  398. }
  399. if v < min || math.IsNaN(min) {
  400. min = v
  401. ind = i
  402. }
  403. }
  404. return ind
  405. }
  406. // Mul performs element-wise multiplication between dst
  407. // and s and stores the value in dst.
  408. // It panics if the argument lengths do not match.
  409. func Mul(dst, s []float64) {
  410. if len(dst) != len(s) {
  411. panic(badLength)
  412. }
  413. for i, val := range s {
  414. dst[i] *= val
  415. }
  416. }
  417. // MulTo performs element-wise multiplication between s
  418. // and t and stores the value in dst.
  419. // It panics if the argument lengths do not match.
  420. func MulTo(dst, s, t []float64) []float64 {
  421. if len(s) != len(t) {
  422. panic(badLength)
  423. }
  424. if len(dst) != len(s) {
  425. panic(badDstLength)
  426. }
  427. for i, val := range t {
  428. dst[i] = val * s[i]
  429. }
  430. return dst
  431. }
  432. // NearestIdx returns the index of the element in s
  433. // whose value is nearest to v. If several such
  434. // elements exist, the lowest index is returned.
  435. // It panics if s is zero length.
  436. func NearestIdx(s []float64, v float64) int {
  437. if len(s) == 0 {
  438. panic(zeroLength)
  439. }
  440. switch {
  441. case math.IsNaN(v):
  442. return 0
  443. case math.IsInf(v, 1):
  444. return MaxIdx(s)
  445. case math.IsInf(v, -1):
  446. return MinIdx(s)
  447. }
  448. var ind int
  449. dist := math.NaN()
  450. for i, val := range s {
  451. newDist := math.Abs(v - val)
  452. // A NaN distance will not be closer.
  453. if math.IsNaN(newDist) {
  454. continue
  455. }
  456. if newDist < dist || math.IsNaN(dist) {
  457. dist = newDist
  458. ind = i
  459. }
  460. }
  461. return ind
  462. }
  463. // NearestIdxForSpan return the index of a hypothetical vector created
  464. // by Span with length n and bounds l and u whose value is closest
  465. // to v. That is, NearestIdxForSpan(n, l, u, v) is equivalent to
  466. // Nearest(Span(make([]float64, n),l,u),v) without an allocation.
  467. // It panics if n is less than two.
  468. func NearestIdxForSpan(n int, l, u float64, v float64) int {
  469. if n < 2 {
  470. panic(shortSpan)
  471. }
  472. if math.IsNaN(v) {
  473. return 0
  474. }
  475. // Special cases for Inf and NaN.
  476. switch {
  477. case math.IsNaN(l) && !math.IsNaN(u):
  478. return n - 1
  479. case math.IsNaN(u):
  480. return 0
  481. case math.IsInf(l, 0) && math.IsInf(u, 0):
  482. if l == u {
  483. return 0
  484. }
  485. if n%2 == 1 {
  486. if !math.IsInf(v, 0) {
  487. return n / 2
  488. }
  489. if math.Copysign(1, v) == math.Copysign(1, l) {
  490. return 0
  491. }
  492. return n/2 + 1
  493. }
  494. if math.Copysign(1, v) == math.Copysign(1, l) {
  495. return 0
  496. }
  497. return n / 2
  498. case math.IsInf(l, 0):
  499. if v == l {
  500. return 0
  501. }
  502. return n - 1
  503. case math.IsInf(u, 0):
  504. if v == u {
  505. return n - 1
  506. }
  507. return 0
  508. case math.IsInf(v, -1):
  509. if l <= u {
  510. return 0
  511. }
  512. return n - 1
  513. case math.IsInf(v, 1):
  514. if u <= l {
  515. return 0
  516. }
  517. return n - 1
  518. }
  519. // Special cases for v outside (l, u) and (u, l).
  520. switch {
  521. case l < u:
  522. if v <= l {
  523. return 0
  524. }
  525. if v >= u {
  526. return n - 1
  527. }
  528. case l > u:
  529. if v >= l {
  530. return 0
  531. }
  532. if v <= u {
  533. return n - 1
  534. }
  535. default:
  536. return 0
  537. }
  538. // Can't guarantee anything about exactly halfway between
  539. // because of floating point weirdness.
  540. return int((float64(n)-1)/(u-l)*(v-l) + 0.5)
  541. }
  542. // Norm returns the L norm of the slice S, defined as
  543. // (sum_{i=1}^N s[i]^L)^{1/L}
  544. // Special cases:
  545. // L = math.Inf(1) gives the maximum absolute value.
  546. // Does not correctly compute the zero norm (use Count).
  547. func Norm(s []float64, L float64) float64 {
  548. // Should this complain if L is not positive?
  549. // Should this be done in log space for better numerical stability?
  550. // would be more cost
  551. // maybe only if L is high?
  552. if len(s) == 0 {
  553. return 0
  554. }
  555. if L == 2 {
  556. return f64.L2NormUnitary(s)
  557. }
  558. var norm float64
  559. if L == 1 {
  560. for _, val := range s {
  561. norm += math.Abs(val)
  562. }
  563. return norm
  564. }
  565. if math.IsInf(L, 1) {
  566. for _, val := range s {
  567. norm = math.Max(norm, math.Abs(val))
  568. }
  569. return norm
  570. }
  571. for _, val := range s {
  572. norm += math.Pow(math.Abs(val), L)
  573. }
  574. return math.Pow(norm, 1/L)
  575. }
  576. // Prod returns the product of the elements of the slice.
  577. // Returns 1 if len(s) = 0.
  578. func Prod(s []float64) float64 {
  579. prod := 1.0
  580. for _, val := range s {
  581. prod *= val
  582. }
  583. return prod
  584. }
  585. // Reverse reverses the order of elements in the slice.
  586. func Reverse(s []float64) {
  587. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  588. s[i], s[j] = s[j], s[i]
  589. }
  590. }
  591. // Same returns true when the input slices have the same length and all
  592. // elements have the same value with NaN treated as the same.
  593. func Same(s, t []float64) bool {
  594. if len(s) != len(t) {
  595. return false
  596. }
  597. for i, v := range s {
  598. w := t[i]
  599. if v != w && !(math.IsNaN(v) && math.IsNaN(w)) {
  600. return false
  601. }
  602. }
  603. return true
  604. }
  605. // Scale multiplies every element in dst by the scalar c.
  606. func Scale(c float64, dst []float64) {
  607. if len(dst) > 0 {
  608. f64.ScalUnitary(c, dst)
  609. }
  610. }
  611. // ScaleTo multiplies the elements in s by c and stores the result in dst.
  612. // It panics if the slice argument lengths do not match.
  613. func ScaleTo(dst []float64, c float64, s []float64) []float64 {
  614. if len(dst) != len(s) {
  615. panic(badDstLength)
  616. }
  617. if len(dst) > 0 {
  618. f64.ScalUnitaryTo(dst, c, s)
  619. }
  620. return dst
  621. }
  622. // Span returns a set of N equally spaced points between l and u, where N
  623. // is equal to the length of the destination. The first element of the destination
  624. // is l, the final element of the destination is u.
  625. // It panics if the length of dst is less than 2.
  626. //
  627. // Span also returns the mutated slice dst, so that it can be used in range expressions,
  628. // like:
  629. //
  630. // for i, x := range Span(dst, l, u) { ... }
  631. func Span(dst []float64, l, u float64) []float64 {
  632. n := len(dst)
  633. if n < 2 {
  634. panic(shortSpan)
  635. }
  636. // Special cases for Inf and NaN.
  637. switch {
  638. case math.IsNaN(l):
  639. for i := range dst[:len(dst)-1] {
  640. dst[i] = math.NaN()
  641. }
  642. dst[len(dst)-1] = u
  643. return dst
  644. case math.IsNaN(u):
  645. for i := range dst[1:] {
  646. dst[i+1] = math.NaN()
  647. }
  648. dst[0] = l
  649. return dst
  650. case math.IsInf(l, 0) && math.IsInf(u, 0):
  651. for i := range dst[:len(dst)/2] {
  652. dst[i] = l
  653. dst[len(dst)-i-1] = u
  654. }
  655. if len(dst)%2 == 1 {
  656. if l != u {
  657. dst[len(dst)/2] = 0
  658. } else {
  659. dst[len(dst)/2] = l
  660. }
  661. }
  662. return dst
  663. case math.IsInf(l, 0):
  664. for i := range dst[:len(dst)-1] {
  665. dst[i] = l
  666. }
  667. dst[len(dst)-1] = u
  668. return dst
  669. case math.IsInf(u, 0):
  670. for i := range dst[1:] {
  671. dst[i+1] = u
  672. }
  673. dst[0] = l
  674. return dst
  675. }
  676. step := (u - l) / float64(n-1)
  677. for i := range dst {
  678. dst[i] = l + step*float64(i)
  679. }
  680. return dst
  681. }
  682. // Sub subtracts, element-wise, the elements of s from dst.
  683. // It panics if the argument lengths do not match.
  684. func Sub(dst, s []float64) {
  685. if len(dst) != len(s) {
  686. panic(badLength)
  687. }
  688. f64.AxpyUnitaryTo(dst, -1, s, dst)
  689. }
  690. // SubTo subtracts, element-wise, the elements of t from s and
  691. // stores the result in dst.
  692. // It panics if the argument lengths do not match.
  693. func SubTo(dst, s, t []float64) []float64 {
  694. if len(s) != len(t) {
  695. panic(badLength)
  696. }
  697. if len(dst) != len(s) {
  698. panic(badDstLength)
  699. }
  700. f64.AxpyUnitaryTo(dst, -1, t, s)
  701. return dst
  702. }
  703. // Sum returns the sum of the elements of the slice.
  704. func Sum(s []float64) float64 {
  705. return f64.Sum(s)
  706. }
  707. // Within returns the first index i where s[i] <= v < s[i+1]. Within panics if:
  708. // - len(s) < 2
  709. // - s is not sorted
  710. func Within(s []float64, v float64) int {
  711. if len(s) < 2 {
  712. panic(shortSpan)
  713. }
  714. if !sort.Float64sAreSorted(s) {
  715. panic("floats: input slice not sorted")
  716. }
  717. if v < s[0] || v >= s[len(s)-1] || math.IsNaN(v) {
  718. return -1
  719. }
  720. for i, f := range s[1:] {
  721. if v < f {
  722. return i
  723. }
  724. }
  725. return -1
  726. }
  727. // SumCompensated returns the sum of the elements of the slice calculated with greater
  728. // accuracy than Sum at the expense of additional computation.
  729. func SumCompensated(s []float64) float64 {
  730. // SumCompensated uses an improved version of Kahan's compensated
  731. // summation algorithm proposed by Neumaier.
  732. // See https://en.wikipedia.org/wiki/Kahan_summation_algorithm for details.
  733. var sum, c float64
  734. for _, x := range s {
  735. // This type conversion is here to prevent a sufficiently smart compiler
  736. // from optimising away these operations.
  737. t := float64(sum + x)
  738. if math.Abs(sum) >= math.Abs(x) {
  739. c += (sum - t) + x
  740. } else {
  741. c += (x - t) + sum
  742. }
  743. sum = t
  744. }
  745. return sum + c
  746. }