Skip to content

Instantly share code, notes, and snippets.

class Line {
constructor(m, c) {
this.m = m;
this.c = c
}
apply(x) {
return this.m * x + this.c;
}
object Integration {
def integrate(fn: Double => Double,
interval: (Double, Double),
precision: Double = 0.0001): Double = {
def areaBetween(start: Double, end: Double): Double = (fn(end) + fn(start)) / 2 * (end - start)
def doIntegrate(start: Double, end: Double, area: Double): Double = {
val mid = (start + end)/2
val left = areaBetween(start, mid)
val right = areaBetween(mid, end)
@yellowflash
yellowflash / AutoDiff.scala
Created March 13, 2023 05:54
Automatic differentiation, by differed cached evaluation.
object AutoDiff {
// When we are computing derivative for say f(x) we would like to keep track of dx/dt (t being independent variable)
// in-case t == x then diff = 1.
// Suppose x is not a scalar ie., R^k then we would like to keep track of partial derivatives on each co-ordinate.
// Hence V is a vector space (which is just enough for our case), this would be final Jacobian
case class Dual[K, V](value: K, diff: V)
// We define a Vectorspace V over the field K, we can reduce the generalization by keeping K == Double and it should mostly work too.