Skip to content

Commit

Permalink
core: add support for basic trig functions (#1589)
Browse files Browse the repository at this point in the history
These can be useful for defining synthetic data when
demoing operations.
  • Loading branch information
brharrington authored Nov 10, 2023
1 parent 33bd114 commit bad944e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ object MathExpr {
def apply(v: Double): Double = -v
}

case class Sine(expr: TimeSeriesExpr) extends UnaryMathExpr {

def name: String = "sin"

def apply(v: Double): Double = math.sin(v)
}

case class Sqrt(expr: TimeSeriesExpr) extends UnaryMathExpr {

def name: String = "sqrt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ object MathVocabulary extends Vocabulary {
As,
GroupBy,
Const,
Pi,
Random,
SeededRandom,
Time,
Expand All @@ -51,6 +52,7 @@ object MathVocabulary extends Vocabulary {
ClampMax,
Abs,
Negate,
Sine,
Sqrt,
PerStep,
Add,
Expand Down Expand Up @@ -231,7 +233,12 @@ object MathVocabulary extends Vocabulary {
),
List("name,playback.startLatency,:eq")
),
Macro("median", List("(", "50", ")", ":percentiles"), List("name,requestLatency,:eq"))
Macro("median", List("(", "50", ")", ":percentiles"), List("name,requestLatency,:eq")),
Macro("cos", List(":pi", "2", ":div", ":swap", ":sub", ":sin")),
Macro("tan", List(":dup", ":sin", ":swap", ":cos", ":div")),
Macro("cot", List(":dup", ":cos", ":swap", ":sin", ":div")),
Macro("sec", List("1", ":swap", ":cos", ":div")),
Macro("csc", List("1", ":swap", ":sin", ":div"))
)

case object As extends SimpleWord {
Expand Down Expand Up @@ -324,6 +331,28 @@ object MathVocabulary extends Vocabulary {
override def examples: List[String] = List("42")
}

case object Pi extends SimpleWord {

override def name: String = "pi"

protected def matcher: PartialFunction[List[Any], Boolean] = {
case _ => true
}

protected def executor: PartialFunction[List[Any], List[Any]] = {
case stack => MathExpr.Constant(Math.PI) :: stack
}

override def summary: String =
"""
|Generates a line where each datapoint has the value of the mathematical constant π.
""".stripMargin.trim

override def signature: String = " -- TimeSeriesExpr"

override def examples: List[String] = Nil
}

case object Random extends SimpleWord {

override def name: String = "random"
Expand Down Expand Up @@ -727,6 +756,19 @@ object MathVocabulary extends Vocabulary {
""".stripMargin.trim
}

case object Sine extends UnaryWord {

override def name: String = "sin"

def newInstance(t: TimeSeriesExpr): TimeSeriesExpr = MathExpr.Sine(t)

override def summary: String =
"""
|Compute a new time series where each interval has the sine of the value from the
|input time series.
""".stripMargin.trim
}

case object Sqrt extends UnaryWord {

override def name: String = "sqrt"
Expand Down Expand Up @@ -1197,4 +1239,5 @@ object MathVocabulary extends Vocabulary {
"name,requestLatency,:eq,(,25,50,90,)"
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ModelExtractorsSuite extends FunSuite {

completionTest("name", 8)
completionTest("name,sps", 22)
completionTest("name,sps,:eq", 20)
completionTest("name,sps,:eq,app,foo,:eq", 41)
completionTest("name,sps,:eq", 21)
completionTest("name,sps,:eq,app,foo,:eq", 42)
completionTest("name,sps,:eq,app,foo,:eq,:and,(,asg,)", 12)
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ExprApiSuite extends MUnitRouteSuite {
testGet("/api/v1/expr/complete?q=name,sps,:eq,(,nf.cluster,)") {
assertEquals(response.status, StatusCodes.OK)
val data = Json.decode[List[ExprApiSuite.Candidate]](responseAs[String]).map(_.name)
assertEquals(data, List("by", "by", "cg", "offset", "palette"))
assertEquals(data, List("by", "by", "pi", "cg", "offset", "palette"))
}

// TODO: Right now these fail. As a future improvement suggestions should be possible within
Expand Down

0 comments on commit bad944e

Please sign in to comment.