Skip to content

Commit

Permalink
core: add support for basic trig functions
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 committed Nov 10, 2023
1 parent 33bd114 commit 5ee54d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
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,)"
)
}

}

0 comments on commit 5ee54d0

Please sign in to comment.