Skip to content

Commit

Permalink
named div
Browse files Browse the repository at this point in the history
  • Loading branch information
erikerlandson committed Apr 2, 2022
1 parent 9dd881d commit eb68883
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 33 deletions.
1 change: 1 addition & 0 deletions core/src/main/scala/coulomb/ops/standard/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ package coulomb.ops.standard
export add.given
export sub.given
export mul.given
export div.given
export pow.given
export simplify.given
78 changes: 45 additions & 33 deletions core/src/main/scala/coulomb/ops/standard/div.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,48 @@

package coulomb.ops.standard

import scala.util.NotGiven
import scala.Conversion

import algebra.ring.MultiplicativeGroup

import coulomb.{`/`, Quantity, withUnit}
import coulomb.ops.{Div, SimplifiedUnit, ValueResolution}

transparent inline given ctx_div_1V2U[VL, UL, VR, UR](using
// https://github.com/lampepfl/dotty/issues/14585
eqv: VR =:= VL,
alg: MultiplicativeGroup[VL],
su: SimplifiedUnit[UL / UR]
): Div[VL, UL, VR, UR] =
new Div[VL, UL, VR, UR]:
type VO = VL
type UO = su.UO
def apply(ql: Quantity[VL, UL], qr: Quantity[VR, UR]): Quantity[VO, UO] =
alg.div(ql.value, eqv(qr.value)).withUnit[UO]

transparent inline given ctx_div_2V2U[VL, UL, VR, UR](using
nev: NotGiven[VR =:= VL],
vres: ValueResolution[VL, VR],
icl: Conversion[Quantity[VL, UL], Quantity[vres.VO, UL]],
icr: Conversion[Quantity[VR, UR], Quantity[vres.VO, UR]],
alg: MultiplicativeGroup[vres.VO],
su: SimplifiedUnit[UL / UR]
): Div[VL, UL, VR, UR] =
new Div[VL, UL, VR, UR]:
type VO = vres.VO
type UO = su.UO
def apply(ql: Quantity[VL, UL], qr: Quantity[VR, UR]): Quantity[VO, UO] =
alg.div(icl(ql).value, icr(qr).value).withUnit[UO]
object div:
import scala.util.NotGiven
import scala.Conversion

import algebra.ring.MultiplicativeGroup

import coulomb.{`/`, Quantity, withUnit}
import coulomb.ops.{Div, SimplifiedUnit, ValueResolution}

transparent inline given ctx_div_1V2U[VL, UL, VR, UR](using
// https://github.com/lampepfl/dotty/issues/14585
eqv: VR =:= VL,
alg: MultiplicativeGroup[VL],
su: SimplifiedUnit[UL / UR]
): Div[VL, UL, VR, UR] =
new infra.Div1V2U[VL, UL, VR, UR, su.UO](alg, eqv)

transparent inline given ctx_div_2V2U[VL, UL, VR, UR](using
nev: NotGiven[VR =:= VL],
vres: ValueResolution[VL, VR],
icl: Conversion[Quantity[VL, UL], Quantity[vres.VO, UL]],
icr: Conversion[Quantity[VR, UR], Quantity[vres.VO, UR]],
alg: MultiplicativeGroup[vres.VO],
su: SimplifiedUnit[UL / UR]
): Div[VL, UL, VR, UR] =
new infra.Div2V2U[VL, UL, VR, UR, vres.VO, su.UO](alg, icl, icr)

object infra:
class Div1V2U[VL, UL, VR, UR, UOp](
alg: MultiplicativeGroup[VL],
eqv: VR =:= VL) extends Div[VL, UL, VR, UR]:
type VO = VL
type UO = UOp
def apply(ql: Quantity[VL, UL], qr: Quantity[VR, UR]): Quantity[VO, UO] =
alg.div(ql.value, eqv(qr.value)).withUnit[UO]

class Div2V2U[VL, UL, VR, UR, VOp, UOp](
alg: MultiplicativeGroup[VOp],
icl: Conversion[Quantity[VL, UL], Quantity[VOp, UL]],
icr: Conversion[Quantity[VR, UR], Quantity[VOp, UR]]) extends Div[VL, UL, VR, UR]:
type VO = VOp
type UO = UOp
def apply(ql: Quantity[VL, UL], qr: Quantity[VR, UR]): Quantity[VO, UO] =
alg.div(icl(ql).value, icr(qr).value).withUnit[UO]

0 comments on commit eb68883

Please sign in to comment.