Skip to content

Commit

Permalink
consolidate ops/algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
erikerlandson committed Dec 10, 2023
1 parent 09559b4 commit 429493d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 260 deletions.
88 changes: 88 additions & 0 deletions core/src/main/scala/coulomb/ops/algebra.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2022 Erik Erlandson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package coulomb.ops.algebra

import scala.annotation.implicitNotFound

import _root_.algebra.ring.*

import coulomb.*
import coulomb.syntax.*

// there is no typelevel community typeclass that expresses the concept
// "supports raising to fractional powers, without truncation"
// The closest thing is spire NRoot, but it is also defined on truncating integer types,
// so it is not helpful for distinguishing "pow" from "tpow", and in any case requires spire
// https://github.com/typelevel/spire/issues/741

@implicitNotFound("Fractional power not defined for value type ${V}")
abstract class FractionalPower[V]:
/** returns v^e */
def pow(v: V, e: Double): V

@implicitNotFound("Truncating power not defined for value type ${V}")
abstract class TruncatingPower[V]:
/** returns v^e, truncated to integer value (toward zero) */
def tpow(v: V, e: Double): V

object all:
export coulomb.ops.algebra.int.given
export coulomb.ops.algebra.long.given
export coulomb.ops.algebra.float.given
export coulomb.ops.algebra.double.given

object int:
given ctx_Int_is_TruncatingPower: TruncatingPower[Int] with
def tpow(v: Int, e: Double): Int = math.pow(v.toDouble, e).toInt

given ctx_Int_is_TruncatedDivision: TruncatedDivision[Int] with
def tquot(x: Int, y: Int): Int = x / y
// I don't care about these
def tmod(x: Int, y: Int): Int = ???
def fquot(x: Int, y: Int): Int = ???
def fmod(x: Int, y: Int): Int = ???
def abs(a: Int): Int = ???
def additiveCommutativeMonoid
: _root_.algebra.ring.AdditiveCommutativeMonoid[Int] = ???
def order: _root_.cats.kernel.Order[Int] = ???
def signum(a: Int): Int = ???

object long:
given ctx_Long_is_TruncatingPower: TruncatingPower[Long] with
def tpow(v: Long, e: Double): Long = math.pow(v.toDouble, e).toLong

given ctx_Long_is_TruncatedDivision: TruncatedDivision[Long] with
def tquot(x: Long, y: Long): Long = x / y
// I don't care about these
def tmod(x: Long, y: Long): Long = ???
def fquot(x: Long, y: Long): Long = ???
def fmod(x: Long, y: Long): Long = ???
def abs(a: Long): Long = ???
def additiveCommutativeMonoid
: _root_.algebra.ring.AdditiveCommutativeMonoid[Long] = ???
def order: _root_.cats.kernel.Order[Long] = ???
def signum(a: Long): Int = ???

object float:
given ctx_Float_is_FractionalPower: FractionalPower[Float] =
(v: Float, e: Double) => math.pow(v.toDouble, e).toFloat

object double:
given ctx_Double_is_FractionalPower: FractionalPower[Double] =
(v: Double, e: Double) => math.pow(v, e)


35 changes: 0 additions & 35 deletions core/src/main/scala/coulomb/ops/algebra/algebra.scala

This file was deleted.

23 changes: 0 additions & 23 deletions core/src/main/scala/coulomb/ops/algebra/all.scala

This file was deleted.

27 changes: 0 additions & 27 deletions core/src/main/scala/coulomb/ops/algebra/double.scala

This file was deleted.

27 changes: 0 additions & 27 deletions core/src/main/scala/coulomb/ops/algebra/float.scala

This file was deleted.

39 changes: 0 additions & 39 deletions core/src/main/scala/coulomb/ops/algebra/int.scala

This file was deleted.

39 changes: 0 additions & 39 deletions core/src/main/scala/coulomb/ops/algebra/long.scala

This file was deleted.

70 changes: 0 additions & 70 deletions core/src/main/scala/coulomb/ops/syntax/coef.scala

This file was deleted.

0 comments on commit 429493d

Please sign in to comment.