From 3f9b930c6078def0d39687a32b9d75e666cbde79 Mon Sep 17 00:00:00 2001 From: Susana Quartin Date: Tue, 11 Aug 2020 23:32:50 +0100 Subject: [PATCH] Adds division of TimeDerivative[A] by quantity of A Fixes #299 --- .../main/scala/squants/time/TimeDerivative.scala | 8 ++++++++ .../scala/squants/time/TimeDerivativeSpec.scala | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/shared/src/main/scala/squants/time/TimeDerivative.scala b/shared/src/main/scala/squants/time/TimeDerivative.scala index 6e22aeed..79ede26f 100644 --- a/shared/src/main/scala/squants/time/TimeDerivative.scala +++ b/shared/src/main/scala/squants/time/TimeDerivative.scala @@ -29,6 +29,14 @@ trait TimeDerivative[A <: Quantity[A] with TimeIntegral[_]] { * @return */ def *(that: Time): A = timeIntegrated * (that / this.time) + + /** + * Returns the portion of quantity change per unit of Time + * + * @param that The amount of Quantity + * @return + */ + def /(that: A): Frequency = (timeIntegrated / that) / time } trait SecondTimeDerivative[A <: SecondTimeIntegral[_]] { self: TimeDerivative[_] ⇒ diff --git a/shared/src/test/scala/squants/time/TimeDerivativeSpec.scala b/shared/src/test/scala/squants/time/TimeDerivativeSpec.scala index faec6860..4127d989 100644 --- a/shared/src/test/scala/squants/time/TimeDerivativeSpec.scala +++ b/shared/src/test/scala/squants/time/TimeDerivativeSpec.scala @@ -9,8 +9,8 @@ package squants.time import squants.CustomMatchers -import squants.motion.UsMilesPerHour -import squants.space.UsMiles +import squants.motion.{MetersPerSecond, UsMilesPerHour} +import squants.space.{Meters, UsMiles} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers @@ -41,6 +41,16 @@ class TimeDerivativeSpec extends AnyFlatSpec with Matchers with CustomMatchers { it should "satisfy Derivative = Integral * Frequency" in { implicit val tolerance = UsMilesPerHour(0.0000000000001) - UsMilesPerHour(55) should beApproximately(UsMiles(55) * 1/Hours(1)) + UsMilesPerHour(55) should beApproximately(UsMiles(55) * 1 / Hours(1)) + } + + it should "satisfy Frequency = Derivative / Integral (Time value in hours)" in { + implicit val tolerance = Hertz(0.0000000000001) + Hertz(0.01) should beApproximately(UsMilesPerHour(72) / UsMiles(2)) + } + + it should "satisfy Frequency = Derivative / Integral (Time value in Seconds)" in { + implicit val tolerance = Hertz(0.0000000000001) + Hertz(55) should beApproximately(MetersPerSecond(110) / Meters(2)) } }