RosettaCodeData/Task/Arithmetic-derivative/Sidef/arithmetic-derivative-2.sidef

27 lines
652 B
Plaintext

subset Integer < Number { .is_int }
subset Positive < Integer { .is_pos }
subset Negative < Integer { .is_neg }
subset Prime < Positive { .is_prime }
func arithmetic_derivative((0)) { 0 }
func arithmetic_derivative((1)) { 0 }
func arithmetic_derivative(Prime _) { 1 }
func arithmetic_derivative(Negative n) {
-arithmetic_derivative(-n)
}
func arithmetic_derivative(Positive n) is cached {
var a = n.factor.rand
var b = n/a
arithmetic_derivative(a)*b + a*arithmetic_derivative(b)
}
func arithmetic_derivative(Number n) {
var (a, b) = n.nude
(arithmetic_derivative(a)*b - arithmetic_derivative(b)*a) / b**2
}