+/** Default interface of nth derivative ex::diff(s, n). It should be called
+ * instead of ::derivative(s) for first derivatives and for nth derivatives it
+ * just recurses down.
+ *
+ * @param s symbol to differentiate in
+ * @param nth order of differentiation
+ * @see ex::diff */
+ex basic::diff(const symbol & s, unsigned nth) const
+{
+ // trivial: zeroth derivative
+ if (!nth)
+ return ex(*this);
+
+ // evaluate unevalueted *this before differentiating
+ if (!(flags & status_flags::evaluated))
+ return ex(*this).diff(s, nth);
+
+ ex ndiff = derivative(s);
+ while (!ndiff.is_zero() && // stop differentiating zeros
+ nth>1) {
+ ndiff = ndiff.diff(s);
+ --nth;
+ }
+ return ndiff;
+}
+