com.phasmidsoftware.number.misc.DiFunc.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of number_2.13 Show documentation
Show all versions of number_2.13 Show documentation
Fuzzy, Lazy Scala library for numerical computation
The newest version!
package com.phasmidsoftware.number.misc
/**
* @author scalaprof
*
* This trait defines a differentiable function and is used by Fuzzy
*/
trait DiFunc[X] {
/**
* @return the number of independent variables in the function
*/
def arity: Int
/**
* @return the function which transforms a value into a different value (THE function)
*/
def f: X => X
/**
* @param i which variable we want the partial derivative for (0<=i Double
}
/**
* Base class for trait DiFunc
*
* @param g function which transforms value (THE function)
* @param ds a variable number of one-functions
*/
abstract class DiFuncBase[X](g: X => X, ds: (X => Double)*) extends DiFunc[X] {
def arity: Int = ds.size
def f: X => X = g
def df_dx(i: Int): X => Double =
if (i < arity) ds(i)
else throw new UnsupportedOperationException(s"no partial derivative defined for $i")
}
case class Power(y: Double) extends DiFuncBase[Double](x => math.pow(x, y), x => y * math.pow(x, y - 1), x => math.log(x) * math.pow(x, y))
case class Times(y: Double) extends DiFuncBase[Double](x => x * y, _ => y, x => x)
case object Negative extends DiFuncBase[Double](x => -x, _ => -1.0)
case object Inverse extends DiFuncBase[Double](x => 1.0 / x, x => -1.0 / x / x)