This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case q"-$x" => Negate(getComponent(x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Negate(value: Component) extends Component { | |
override def toTree(implicit c: blackbox.Context): c.Tree = { | |
import c.universe._ | |
q"-${value.toTree}" | |
} | |
override def derive: Component = Negate(value.derive) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> (0.0 to 1000.0 by 0.1).forall(x => df(x) == 2) | |
res0: Boolean = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f'(x) = 1 + 0 + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f(x) = x + 5 + x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> val df = Scalac.derivative(x => x + 5 + x) | |
df: Double => Double = <function1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in class Variable | |
override def derive: Component = DoubleConstant(1) | |
//in class DoubleConstant | |
override def derive: Component = DoubleConstant(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private def getComponent(tree: Trees#Tree)(implicit c: blackbox.Context): Component = { | |
import c.universe._ | |
tree match { | |
case Ident(TermName(x)) => Variable(x) | |
case Literal(Constant(a)) => DoubleConstant(a.toString.toDouble) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in class Variable | |
override def toTree(implicit c: blackbox.Context): c.Tree = { | |
import c.universe._ | |
Ident(TermName(name)) | |
} | |
//in class DoubleConstant | |
override def toTree(implicit c: blackbox.Context): c.Tree = { | |
import c.universe._ | |
Literal(Constant(value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Variable(name: String) extends Component { | |
override def toTree(implicit c: blackbox.Context): c.Tree = ??? | |
override def derive: Component = ??? | |
} | |
case class DoubleConstant(value: Double) extends Component { | |
override def toTree(implicit c: blackbox.Context): c.Tree = ??? | |
override def derive: Component = ??? | |
} |