-
Notifications
You must be signed in to change notification settings - Fork 87
/
math.scala
59 lines (47 loc) · 2.18 KB
/
math.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
package com.thoughtworks.deeplearning
import com.thoughtworks.deeplearning.Lift.{LiftCase1, LiftCase2}
import com.thoughtworks.raii.asynchronous.Do
import shapeless.{DepFn1, DepFn2, Poly, Poly1, Poly2}
/**
* A namespace of definitions of polymophic functions.
*
* Those functions are implemented in other objects.
*
* @author 杨博 (Yang Bo) <pop.atry@gmail.com>
*/
object math {
implicit final class PolyOps[Operand0](operand0: Operand0) {
def -[Operand1](operand1: Operand1)(
implicit methodCase: LiftCase2[polyFunctions.-.type, Operand0, Operand1]): methodCase.Out =
methodCase(operand0, operand1)
def +[Operand1](operand1: Operand1)(
implicit methodCase: LiftCase2[polyFunctions.+.type, Operand0, Operand1]): methodCase.Out =
methodCase(operand0, operand1)
def *[Operand1](operand1: Operand1)(
implicit methodCase: LiftCase2[polyFunctions.*.type, Operand0, Operand1]): methodCase.Out =
methodCase(operand0, operand1)
def /[Operand1](operand1: Operand1)(
implicit methodCase: LiftCase2[polyFunctions./.type, Operand0, Operand1]): methodCase.Out =
methodCase(operand0, operand1)
}
object polyFunctions {
object - extends Poly2
object + extends Poly2
object * extends Poly2
object / extends Poly2
object abs extends Poly1
object exp extends Poly1
object log extends Poly1
object max extends Poly2
object min extends Poly2
}
def abs[From](a: From)(implicit liftCase1: LiftCase1[polyFunctions.abs.type, From]): liftCase1.Out = liftCase1(a)
def exp[From](a: From)(implicit liftCase1: LiftCase1[polyFunctions.exp.type, From]): liftCase1.Out = liftCase1(a)
def log[From](a: From)(implicit liftCase1: LiftCase1[polyFunctions.log.type, From]): liftCase1.Out = liftCase1(a)
def max[Operand0, Operand1](operand0: Operand0, operand1: Operand1)(
implicit methodCase: LiftCase2[polyFunctions.max.type, Operand0, Operand1]): methodCase.Out =
methodCase(operand0, operand1)
def min[Operand0, Operand1](operand0: Operand0, operand1: Operand1)(
implicit methodCase: LiftCase2[polyFunctions.min.type, Operand0, Operand1]): methodCase.Out =
methodCase(operand0, operand1)
}