-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Additive.scala
272 lines (232 loc) · 9.44 KB
/
Additive.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package algebra
package ring
import scala.{specialized => sp}
import scala.annotation.{nowarn, tailrec}
trait AdditiveSemigroup[@sp(Int, Long, Float, Double) A] extends Any with Serializable {
def additive: Semigroup[A] = new Semigroup[A] {
def combine(x: A, y: A): A = plus(x, y)
@nowarn("msg=deprecated")
override def combineAllOption(as: TraversableOnce[A]): Option[A] = trySum(as)
}
def plus(x: A, y: A): A
def sumN(a: A, n: Int): A =
if (n > 0) positiveSumN(a, n)
else throw new IllegalArgumentException("Illegal non-positive exponent to sumN: %s".format(n))
protected[this] def positiveSumN(a: A, n: Int): A = {
@tailrec def loop(b: A, k: Int, extra: A): A =
if (k == 1) plus(b, extra)
else {
val x = if ((k & 1) == 1) plus(b, extra) else extra
loop(plus(b, b), k >>> 1, x)
}
if (n == 1) a else loop(a, n - 1, a)
}
/**
* Given a sequence of `as`, combine them and return the total.
*
* If the sequence is empty, returns None. Otherwise, returns Some(total).
*/
@nowarn("msg=deprecated")
def trySum(as: TraversableOnce[A]): Option[A] =
as.toIterator.reduceOption(plus)
}
trait AdditiveCommutativeSemigroup[@sp(Int, Long, Float, Double) A] extends Any with AdditiveSemigroup[A] {
override def additive: CommutativeSemigroup[A] = new CommutativeSemigroup[A] {
def combine(x: A, y: A): A = plus(x, y)
@nowarn("msg=deprecated")
override def combineAllOption(as: TraversableOnce[A]): Option[A] = trySum(as)
}
}
trait AdditiveMonoid[@sp(Int, Long, Float, Double) A] extends Any with AdditiveSemigroup[A] {
override def additive: Monoid[A] = new Monoid[A] {
def empty = zero
def combine(x: A, y: A): A = plus(x, y)
@nowarn("msg=deprecated")
override def combineAllOption(as: TraversableOnce[A]): Option[A] = trySum(as)
@nowarn("msg=deprecated")
override def combineAll(as: TraversableOnce[A]): A = sum(as)
}
def zero: A
/**
* Tests if `a` is zero.
*/
def isZero(a: A)(implicit ev: Eq[A]): Boolean = ev.eqv(a, zero)
override def sumN(a: A, n: Int): A =
if (n > 0) positiveSumN(a, n)
else if (n == 0) zero
else throw new IllegalArgumentException("Illegal negative exponent to sumN: %s".format(n))
/**
* Given a sequence of `as`, compute the sum.
*/
@nowarn("msg=deprecated")
def sum(as: TraversableOnce[A]): A =
as.foldLeft(zero)(plus)
@nowarn("msg=deprecated")
override def trySum(as: TraversableOnce[A]): Option[A] =
if (as.isEmpty) None else Some(sum(as))
}
trait AdditiveCommutativeMonoid[@sp(Int, Long, Float, Double) A]
extends Any
with AdditiveMonoid[A]
with AdditiveCommutativeSemigroup[A] {
override def additive: CommutativeMonoid[A] = new CommutativeMonoid[A] {
def empty = zero
def combine(x: A, y: A): A = plus(x, y)
@nowarn("msg=deprecated")
override def combineAllOption(as: TraversableOnce[A]): Option[A] = trySum(as)
@nowarn("msg=deprecated")
override def combineAll(as: TraversableOnce[A]): A = sum(as)
}
}
trait AdditiveGroup[@sp(Int, Long, Float, Double) A] extends Any with AdditiveMonoid[A] {
override def additive: Group[A] = new Group[A] {
def empty = zero
def combine(x: A, y: A): A = plus(x, y)
override def remove(x: A, y: A): A = minus(x, y)
def inverse(x: A): A = negate(x)
@nowarn("msg=deprecated")
override def combineAllOption(as: TraversableOnce[A]): Option[A] = trySum(as)
@nowarn("msg=deprecated")
override def combineAll(as: TraversableOnce[A]): A = sum(as)
}
def negate(x: A): A
def minus(x: A, y: A): A = plus(x, negate(y))
override def sumN(a: A, n: Int): A =
if (n > 0) positiveSumN(a, n)
else if (n == 0) zero
else if (n == Int.MinValue) positiveSumN(negate(plus(a, a)), 1073741824)
else positiveSumN(negate(a), -n)
}
trait AdditiveCommutativeGroup[@sp(Int, Long, Float, Double) A]
extends Any
with AdditiveGroup[A]
with AdditiveCommutativeMonoid[A] {
override def additive: CommutativeGroup[A] = new CommutativeGroup[A] {
def empty = zero
def combine(x: A, y: A): A = plus(x, y)
override def remove(x: A, y: A): A = minus(x, y)
def inverse(x: A): A = negate(x)
@nowarn("msg=deprecated")
override def combineAllOption(as: TraversableOnce[A]): Option[A] = trySum(as)
@nowarn("msg=deprecated")
override def combineAll(as: TraversableOnce[A]): A = sum(as)
}
}
trait AdditiveSemigroupFunctions[S[T] <: AdditiveSemigroup[T]] {
def isAdditiveCommutative[A](implicit ev: S[A]): Boolean =
ev.isInstanceOf[AdditiveCommutativeSemigroup[_]]
def plus[@sp(Int, Long, Float, Double) A](x: A, y: A)(implicit ev: S[A]): A =
ev.plus(x, y)
def sumN[@sp(Int, Long, Float, Double) A](a: A, n: Int)(implicit ev: S[A]): A =
ev.sumN(a, n)
@nowarn("msg=deprecated")
def trySum[A](as: TraversableOnce[A])(implicit ev: S[A]): Option[A] =
ev.trySum(as)
}
trait AdditiveMonoidFunctions[M[T] <: AdditiveMonoid[T]] extends AdditiveSemigroupFunctions[M] {
def zero[@sp(Int, Long, Float, Double) A](implicit ev: M[A]): A =
ev.zero
def isZero[@sp(Int, Long, Float, Double) A](a: A)(implicit ev0: M[A], ev1: Eq[A]): Boolean =
ev0.isZero(a)
@nowarn("msg=deprecated")
def sum[@sp(Int, Long, Float, Double) A](as: TraversableOnce[A])(implicit ev: M[A]): A =
ev.sum(as)
}
trait AdditiveGroupFunctions[G[T] <: AdditiveGroup[T]] extends AdditiveMonoidFunctions[G] {
def negate[@sp(Int, Long, Float, Double) A](x: A)(implicit ev: G[A]): A =
ev.negate(x)
def minus[@sp(Int, Long, Float, Double) A](x: A, y: A)(implicit ev: G[A]): A =
ev.minus(x, y)
}
object AdditiveSemigroup extends AdditiveSemigroupFunctions[AdditiveSemigroup] {
@inline final def apply[A](implicit ev: AdditiveSemigroup[A]): AdditiveSemigroup[A] = ev
/**
* This method converts an additive instance into a generic
* instance.
*
* Given an implicit `AdditiveSemigroup[A]`, this method returns a
* `Semigroup[A]`.
*/
@inline final def additive[A](implicit ev: AdditiveSemigroup[A]): Semigroup[A] =
ev.additive
}
object AdditiveCommutativeSemigroup extends AdditiveSemigroupFunctions[AdditiveCommutativeSemigroup] {
@inline final def apply[A](implicit ev: AdditiveCommutativeSemigroup[A]): AdditiveCommutativeSemigroup[A] = ev
/**
* This method converts an additive instance into a generic
* instance.
*
* Given an implicit `AdditiveCommutativeSemigroup[A]`, this method returns a
* `CommutativeSemigroup[A]`.
*/
@inline final def additive[A](implicit ev: AdditiveCommutativeSemigroup[A]): CommutativeSemigroup[A] =
ev.additive
}
object AdditiveMonoid extends AdditiveMonoidFunctions[AdditiveMonoid] {
@inline final def apply[A](implicit ev: AdditiveMonoid[A]): AdditiveMonoid[A] = ev
/**
* This method converts an additive instance into a generic
* instance.
*
* Given an implicit `AdditiveMonoid[A]`, this method returns a
* `Monoid[A]`.
*/
@inline final def additive[A](implicit ev: AdditiveMonoid[A]): Monoid[A] =
ev.additive
}
object AdditiveCommutativeMonoid extends AdditiveMonoidFunctions[AdditiveCommutativeMonoid] {
@inline final def apply[A](implicit ev: AdditiveCommutativeMonoid[A]): AdditiveCommutativeMonoid[A] = ev
/**
* This method converts an additive instance into a generic
* instance.
*
* Given an implicit `AdditiveCommutativeMonoid[A]`, this method returns a
* `CommutativeMonoid[A]`.
*/
@inline final def additive[A](implicit ev: AdditiveCommutativeMonoid[A]): CommutativeMonoid[A] =
ev.additive
}
object AdditiveGroup extends AdditiveGroupFunctions[AdditiveGroup] {
@inline final def apply[A](implicit ev: AdditiveGroup[A]): AdditiveGroup[A] = ev
/**
* This method converts an additive instance into a generic
* instance.
*
* Given an implicit `AdditiveGroup[A]`, this method returns a
* `Group[A]`.
*/
@inline final def additive[A](implicit ev: AdditiveGroup[A]): Group[A] =
ev.additive
}
object AdditiveCommutativeGroup extends AdditiveGroupFunctions[AdditiveCommutativeGroup] {
@inline final def apply[A](implicit ev: AdditiveCommutativeGroup[A]): AdditiveCommutativeGroup[A] = ev
/**
* This method converts an additive instance into a generic
* instance.
*
* Given an implicit `AdditiveCommutativeGroup[A]`, this method returns a
* `CommutativeGroup[A]`.
*/
@inline final def additive[A](implicit ev: AdditiveCommutativeGroup[A]): CommutativeGroup[A] =
ev.additive
}