nthRoot function

dynamic nthRoot(
  1. dynamic x,
  2. dynamic nth
)

Returns the nth root of a number.

Example:

// The cube root of 8 will be:
print(nthRoot(8, 3));  // Output: 2.0

Implementation

dynamic nthRoot(dynamic x, dynamic nth) {
  if (x is Expression || nth is Expression) {
    final base = x is Expression ? x : Literal(x);
    final exp = nth is Expression ? nth : Literal(nth);
    return Pow(base, Divide(Literal(1), exp));
  }
  if (x is num || x is Complex) return Complex(x).nthRoot(nth.toInt());
  if (x is Matrix) return MatrixFunctions(x).nthRoot(nth.toInt());
  throw ArgumentError('Input should be num, Complex, Matrix, or Expression');
}