Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add angular unit radian #49

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/main/scala/libra/nonsi/package.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package libra

import ops.base.{Show, ConversionFactor}
import spire._, spire.algebra._, spire.math._, spire.implicits._
import libra.nonsi.Angle
import libra.ops.quantity.ConvertTo
import ops.base.{Conversion, ConversionFactor, Show}
import spire._
import spire.algebra._
import spire.math._
import spire.implicits._
import singleton.ops._
import libra.si.{Time, Second}
import libra.si.{Second, Time}
import shapeless._

/* Non-SI units */
Expand All @@ -16,10 +21,12 @@ package object nonsi {
trait Degree extends UnitOfMeasure[Angle]
trait Arcminute extends UnitOfMeasure[Angle]
trait Arcsecond extends UnitOfMeasure[Angle]
trait Radian extends UnitOfMeasure[Angle]

implicit def degreeShow: Show[Degree] = Show[Degree]("degree")
implicit def arcminuteShow: Show[Arcminute] = Show[Arcminute]("arcminute")
implicit def arcsecondShow: Show[Arcsecond] = Show[Arcsecond]("arcsecond")
implicit def radian: Show[Radian] = Show[Radian]("rad")

implicit def degreeArcminuteConversion[A](
implicit c: ConvertableTo[A]
Expand All @@ -36,6 +43,25 @@ package object nonsi {
): ConversionFactor[A, Angle, Arcminute, Arcsecond] =
new ConversionFactor(c.fromInt(60))

implicit def radianDegreeConversion[A](
implicit c: ConvertableTo[A]
): ConversionFactor[A, Angle, Radian, Degree] =
new ConversionFactor(c.fromDouble(180 / pi))

implicit def radianArcminuteConversion[A](
implicit c: ConvertableTo[A],
multiplicative: MultiplicativeSemigroup[A]
): ConversionFactor[A, Angle, Radian, Arcminute] =
degreeArcminuteConversion.compose(radianDegreeConversion)

implicit def radianArcsecondConversion[A](
implicit c: ConvertableTo[A],
multiplicative: MultiplicativeSemigroup[A]
): ConversionFactor[A, Angle, Radian, Arcsecond] =
degreeArcsecondConversion.compose(radianDegreeConversion)



type AngularVelocityQuantity[A, L <: UnitOfMeasure[Angle], T <: UnitOfMeasure[Time]] =
Quantity[A, Term[Angle, L, Fraction[1, 1]] :: Term[Time, T, Fraction[-1, 1]] :: HNil]

Expand All @@ -46,5 +72,6 @@ package object nonsi {
def degreessPerSecond: AngularVelocityQuantity[A, Degree, Second] = Quantity(a)
def arcminutesPerSecond: AngularVelocityQuantity[A, Arcminute, Second] = Quantity(a)
def arcsecondsPerSecond: AngularVelocityQuantity[A, Arcsecond, Second] = Quantity(a)
def radian: QuantityOf[A, Angle, Radian] = Quantity(a)
}
}
11 changes: 9 additions & 2 deletions src/main/scala/libra/ops/base.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package libra
package ops

import spire.algebra._, spire.implicits._
import libra.UnitOfMeasure
import spire.algebra._
import spire.implicits._
import spire.math.ConvertableFrom

object base {

Expand All @@ -23,7 +26,11 @@ object base {
* @tparam F the unit to convert from
* @tparam T the unit to convert to
*/
case class ConversionFactor[A, D, F <: UnitOfMeasure[D], T <: UnitOfMeasure[D]](val value: A)
case class ConversionFactor[A, D, F <: UnitOfMeasure[D], T <: UnitOfMeasure[D]](val value: A) {
def compose[OtherF <: UnitOfMeasure[D]](otherConversionFactor: ConversionFactor[A, D, OtherF, F])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really nice helper function 👍

There was a previous attempt at trying to compose conversion factors implicitly, i.e given an implicit ConversionFactor[P, Q, A, B] and an implicit ConversionFactor[P, Q, B, C], produce a ConversionFactor[P, Q, A, C]. However, this couldn't be picked up by scala's implicit resolution. I'll keep investigating, but this is a good stand in in the meantime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tried to make it implicitly but i failed (it was above my abilities), i'll be waiting impatiently for results of your investigation :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created an issue to track it properly #50

(implicit multiplicativeSemigroup: MultiplicativeSemigroup[A]): ConversionFactor[A, D, OtherF, T] =
new ConversionFactor(otherConversionFactor.value * value)
}

/** Derived typeclass for the conversion factor from F to T
*
Expand Down
34 changes: 33 additions & 1 deletion src/test/scala/libra/NonSISpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package libra

import spire.implicits._
import libra.nonsi._, libra.si._
import spire.math._
import libra.nonsi._
import libra.si._

import org.scalatest._

Expand All @@ -20,6 +22,10 @@ class NonSISpec extends WordSpec {
"arcsecond value" in {
assert(2.0.degree.to[Arcsecond].value === 7200.0)
}

"radian value" in {
assert(180.0.degree.to[Radian].value === pi)
}
}

"arcminute" should {
Expand Down Expand Up @@ -76,6 +82,10 @@ class NonSISpec extends WordSpec {
"arcsecond value" in {
assert(2.0.arcminutesPerSecond.to[Arcsecond].value === 120.0)
}

"radian value" in {
assert(10800.0.arcminutesPerSecond.to[Radian].value === pi)
}
}

"arcsecondsPerSecond" should {
Expand All @@ -90,5 +100,27 @@ class NonSISpec extends WordSpec {
"arcminutesPerSecond value" in {
assert(120.0.arcsecondsPerSecond.to[Arcminute].value === 2.0)
}

"radian value" in {
assert(648000.0.arcsecondsPerSecond.to[Radian].value === pi)
}
}

"radians" should {
"show" in {
assert(2.radian.show === "2 rad [∠]")
}

"degree value" in {
assert(pi.radian.to[Degree].value === 180.0)
}

"arcminute value" in {
assert(pi.radian.to[Arcminute].value === 10800.0)
}

"arcsecond value" in {
assert(pi.radian.to[Arcsecond].value === 648000.0)
}
}
}