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

Make AngularVelocity a derivative of Angle #173

Merged
merged 2 commits into from
Jan 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
10 changes: 7 additions & 3 deletions shared/src/main/scala/squants/motion/AngularVelocity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
package squants.motion

import squants._
import squants.space.{ Degrees, Gradians, Turns }
import squants.space.{Degrees, Gradians, Turns}
import squants.time.TimeDerivative

/**
* @author garyKeorkunian
Expand All @@ -19,14 +20,17 @@ import squants.space.{ Degrees, Gradians, Turns }
*
*/
final class AngularVelocity private (val value: Double, val unit: AngularVelocityUnit)
extends Quantity[AngularVelocity] {
// TODO - Make this a TimeDerivative of Angle
extends Quantity[AngularVelocity] with TimeDerivative[Angle] {
def dimension = AngularVelocity

def toRadiansPerSecond = to(RadiansPerSecond)
def toDegreesPerSecond = to(DegreesPerSecond)
def toGradsPerSecond = to(GradsPerSecond)
def toTurnsPerSecond = to(TurnsPerSecond)

protected[squants] def timeIntegrated: Angle = Radians(toRadiansPerSecond)

protected[squants] def time: Time = Seconds(1)
}

object AngularVelocity extends Dimension[AngularVelocity] {
Expand Down
9 changes: 7 additions & 2 deletions shared/src/main/scala/squants/space/Angle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
package squants.space

import squants._
import squants.time.Time
import squants.motion.{AngularVelocity, RadiansPerSecond}
import squants.time.{Time, TimeIntegral}

/**
* @author garyKeorkunian
Expand All @@ -18,7 +19,7 @@ import squants.time.Time
* @param value value in [[squants.space.Radians]]
*/
final class Angle private (val value: Double, val unit: AngleUnit)
extends Quantity[Angle] {
extends Quantity[Angle] with TimeIntegral[AngularVelocity] {

def dimension = Angle

Expand All @@ -34,6 +35,10 @@ final class Angle private (val value: Double, val unit: AngleUnit)
def tan = math.tan(toRadians)
def asin = math.asin(toRadians)
def acos = math.acos(toRadians)

protected def timeDerived: AngularVelocity = RadiansPerSecond(toRadians)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder why on AngularVelocity the protected methods are [squants] protected but not in Angle

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a timeDerived value. We're not consistent on those.

The other values in this PR are timeIntegrated values, which we're pretty good at making protected[squants].

timeDerived is all over the place, though.

Good question. We don't seem to be consistent in that.

In Velocity, the timeDerived member is public (line 31).
In Information, the timeDerived member is protected (line 27).
In LuminousEnergy, the timeDerived member is protected (line 25).
etc.


override protected def time: Time = Seconds(1)
}

object Angle extends Dimension[Angle] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.scalatest.{ Matchers, FlatSpec }
import scala.language.postfixOps
import squants.space.Radians
import squants.QuantityParseException
import squants.time.Seconds

/**
* @author garyKeorkunian
Expand Down Expand Up @@ -53,6 +54,10 @@ class AngularVelocitySpec extends FlatSpec with Matchers {
TurnsPerSecond(1).toString(TurnsPerSecond) should be("1.0 turns/s")
}

it should "return Angle when multiplied by Time" in {
RadiansPerSecond(1) * Seconds(1) should be(Radians(1))
}

behavior of "AngularVelocityConversions"

it should "provide aliases for single unit values" in {
Expand Down
10 changes: 10 additions & 0 deletions shared/src/test/scala/squants/space/AngleSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package squants.space

import org.scalatest.{ FlatSpec, Matchers }
import squants.QuantityParseException
import squants.motion.RadiansPerSecond
import squants.time.Seconds

/**
* @author garyKeorkunian
Expand Down Expand Up @@ -75,6 +77,14 @@ class AngleSpec extends FlatSpec with Matchers {
Radians(1).asin should be(math.asin(1))
}

it should "return AngularVelocity when divided by Time" in {
Radians(1) / Seconds(1) should be(RadiansPerSecond(1))
}

it should "return Time when divided by AngularVelocity" in {
Radians(1) / RadiansPerSecond(1) should be(Seconds(1))
}

behavior of "AngleConversion"

it should "provide aliases for single unit values" in {
Expand Down
10 changes: 9 additions & 1 deletion shared/src/test/scala/squants/space/SpaceChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package squants.space
import org.scalacheck.Prop._
import org.scalacheck.Properties
import squants.QuantityChecks
import squants.motion.CubicMetersPerSecond
import squants.motion.{CubicMetersPerSecond, RadiansPerSecond}
import squants.time.Seconds

/**
Expand All @@ -22,6 +22,7 @@ import squants.time.Seconds
object SpaceChecks extends Properties("Space") with QuantityChecks {

implicit val tolVfr = CubicMetersPerSecond(tol)
implicit val tolAngularVelocity = RadiansPerSecond(tol)

property("Area = Length * Length") = forAll(posNum, posNum) { (length1: TestData, length2: TestData) ⇒
SquareMeters(length1 * length2) == Meters(length1) * Meters(length2) &&
Expand All @@ -41,4 +42,11 @@ object SpaceChecks extends Properties("Space") with QuantityChecks {
Seconds(time) =~ CubicMeters(vfr * time) / CubicMetersPerSecond(vfr) &&
CubicMetersPerSecond(vfr) =~ CubicMeters(vfr * time) / Seconds(time)
}

property("Angle = AngularVelocity * Time") = forAll(posNum, posNum) { (angularVel: TestData, time: TestData) ⇒
Radians(angularVel * time) == RadiansPerSecond(angularVel) * Seconds(time) &&
Radians(angularVel * time) == Seconds(time) * RadiansPerSecond(angularVel) &&
Seconds(time) =~ Radians(angularVel * time) / RadiansPerSecond(angularVel) &&
RadiansPerSecond(angularVel) =~ Radians(angularVel * time) / Seconds(time)
}
}