text:
int_rules_1_2_2_4 = @theory begin

    #= ::Subsection::Closed:: =#
    #= 1.2.2.4*(f*x)^m*(d+e*x^2)^q*(a+b*x^2+c*x^4)^p =#
    #= Antiderivative(((~f')*(~x))^(~m')*((~e')*(~x)^2)^(~q')*((~a)+(~b')*(~x)^2+(~c')*(~x)^4)^(~p'),(~x)*Symbol) := e^q/f^(2*q)*Antiderivative((f*x)^(m+2*q)*(a+b*x^2+c*x^4)^p,x) <--  FreeQ([a,b,c,e,f,m,p],x) && IntegerQ(q) =#
    #= Antiderivative(((~f')*(~x))^(~m')*((~e')*(~x)^2)^(~q')*((~a)+(~c')*(~x)^4)^(~p'),~x) :=  e^q/f^(2*q)*Antiderivative((f*x)^(m+2*q)*(a+c*x^4)^p,x) <--  FreeQ([a,c,e,f,m,p],x) && IntegerQ(q) =#
    @apply_utils Antiderivative((~x) ^ ~(m') * (~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (1 / (2 * (~e) ^ ((~m - 1) / 2))) * Subst(Antiderivative((~e * ~x) ^ (~q + (~m - 1) / 2) * (~a + ~b * ~x + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~b, ~c, ~e, ~p, ~q], ~x) && (Not(IntegerQ(~q)) && IntegerQ((~m - 1) / 2))
    @apply_utils Antiderivative((~x) ^ ~(m') * (~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (1 / (2 * (~e) ^ ((~m - 1) / 2))) * Subst(Antiderivative((~e * ~x) ^ (~q + (~m - 1) / 2) * (~a + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~c, ~e, ~p, ~q], ~x) && (Not(IntegerQ(~q)) && IntegerQ((~m - 1) / 2))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (((~e) ^ IntPart(~q) * (~e * (~x) ^ 2) ^ FracPart(~q)) / ((~f) ^ (2 * IntPart(~q)) * (~f * ~x) ^ (2 * FracPart(~q)))) * Antiderivative((~f * ~x) ^ (~m + 2 * ~q) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c, ~e, ~f, ~m, ~p, ~q], ~x) && Not(IntegerQ(~q))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (((~e) ^ IntPart(~q) * (~e * (~x) ^ 2) ^ FracPart(~q)) / ((~f) ^ (2 * IntPart(~q)) * (~f * ~x) ^ (2 * FracPart(~q)))) * Antiderivative((~f * ~x) ^ (~m + 2 * ~q) * (~a + ~c * (~x) ^ 4) ^ ~p, ~x) <-- FreeQ([~a, ~c, ~e, ~f, ~m, ~p, ~q], ~x) && Not(IntegerQ(~q))
    @apply_utils Antiderivative(~x * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (1 / 2) * Subst(Antiderivative((~d + ~e * ~x) ^ ~q * (~a + ~b * ~x + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~p, ~q], ~x)
    @apply_utils Antiderivative(~x * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (1 / 2) * Subst(Antiderivative((~d + ~e * ~x) ^ ~q * (~a + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~c, ~d, ~e, ~p, ~q], ~x)
    #= Antiderivative(((~f')*(~x))^(~m')*((~d)+(~e')*(~x)^2)^(~q')*((~a)+(~b')*(~x)^2+(~c')*(~x)^4)^(~p'),(~x)*Symbol) := 1/c^p*Antiderivative((f*x)^m*(d+e*x^2)^q*(b/2+c*x^2)^(2*p),x) <--  FreeQ([a,b,c,d,e,f,m,p,q],x) && EqQ(b^2-4*a*c,0) && IntegerQ(p) =#
    @apply_utils Antiderivative((~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (1 / 2) * Subst(Antiderivative((~x) ^ ((~m - 1) / 2) * (~d + ~e * ~x) ^ ~q * (~a + ~b * ~x + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~p, ~q], ~x) && (EqQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~p)) && IGtQ((~m + 1) / 2, 0)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => ((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ FracPart(~p) / ((~c) ^ IntPart(~p) * (~b / 2 + ~c * (~x) ^ 2) ^ (2 * FracPart(~p)))) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~b / 2 + ~c * (~x) ^ 2) ^ (2 * ~p), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x) && (EqQ((~b) ^ 2 - 4 * ~a * ~c, 0) && Not(IntegerQ(~p)))
    @apply_utils Antiderivative((~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (1 / 2) * Subst(Antiderivative((~x) ^ ((~m - 1) / 2) * (~d + ~e * ~x) ^ ~q * (~a + ~b * ~x + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~p, ~q], ~x) && IntegerQ((~m - 1) / 2)
    @apply_utils Antiderivative((~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (1 / 2) * Subst(Antiderivative((~x) ^ ((~m - 1) / 2) * (~d + ~e * ~x) ^ ~q * (~a + ~c * (~x) ^ 2) ^ ~p, ~x), ~x, (~x) ^ 2) <-- FreeQ([~a, ~c, ~d, ~e, ~p, ~q], ~x) && IntegerQ((~m + 1) / 2)
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + ~p) * (~a / ~d + (~c / ~e) * (~x) ^ 2) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (EqQ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2, 0) && IntegerQ(~p)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + ~p) * (~a / ~d + (~c / ~e) * (~x) ^ 2) ^ ~p, ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~q, ~m, ~q], ~x) && (EqQ(~c * (~d) ^ 2 + ~a * (~e) ^ 2, 0) && IntegerQ(~p))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => ((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ FracPart(~p) / ((~d + ~e * (~x) ^ 2) ^ FracPart(~p) * (~a / ~d + (~c * (~x) ^ 2) / ~e) ^ FracPart(~p))) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + ~p) * (~a / ~d + (~c / ~e) * (~x) ^ 2) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (EqQ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2, 0) && Not(IntegerQ(~p))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~p, ~x) => ((~a + ~c * (~x) ^ 4) ^ FracPart(~p) / ((~d + ~e * (~x) ^ 2) ^ FracPart(~p) * (~a / ~d + (~c * (~x) ^ 2) / ~e) ^ FracPart(~p))) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + ~p) * (~a / ~d + (~c / ~e) * (~x) ^ 2) ^ ~p, ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x) && (EqQ(~c * (~d) ^ 2 + ~a * (~e) ^ 2, 0) && Not(IntegerQ(~p)))
    @apply_utils Antiderivative((~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((-(~d)) ^ (~m / 2 - 1) * ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2) ^ ~p * ~x * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1)) + (1 / (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1))) * Antiderivative((~d + ~e * (~x) ^ 2) ^ (~q + 1) * ExpandToSum(Together((1 / (~d + ~e * (~x) ^ 2)) * (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1) * (~x) ^ ~m * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p - (-(~d)) ^ (~m / 2 - 1) * ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2) ^ ~p * (~d + ~e * (2 * ~q + 3) * (~x) ^ 2))), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) && (ILtQ(~q, -1) && IGtQ(~m / 2, 0))))
    @apply_utils Antiderivative((~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((-(~d)) ^ (~m / 2 - 1) * (~c * (~d) ^ 2 + ~a * (~e) ^ 2) ^ ~p * ~x * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1)) + (1 / (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1))) * Antiderivative((~d + ~e * (~x) ^ 2) ^ (~q + 1) * ExpandToSum(Together((1 / (~d + ~e * (~x) ^ 2)) * (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1) * (~x) ^ ~m * (~a + ~c * (~x) ^ 4) ^ ~p - (-(~d)) ^ (~m / 2 - 1) * (~c * (~d) ^ 2 + ~a * (~e) ^ 2) ^ ~p * (~d + ~e * (2 * ~q + 3) * (~x) ^ 2))), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (IGtQ(~p, 0) && (ILtQ(~q, -1) && IGtQ(~m / 2, 0)))
    @apply_utils Antiderivative((~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((-(~d)) ^ (~m / 2 - 1) * ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2) ^ ~p * ~x * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1)) + ((-(~d)) ^ (~m / 2 - 1) / (2 * (~e) ^ (2 * ~p) * (~q + 1))) * Antiderivative((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * ExpandToSum(Together((1 / (~d + ~e * (~x) ^ 2)) * (2 * (-(~d)) ^ (-(~m) / 2 + 1) * (~e) ^ (2 * ~p) * (~q + 1) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p - ((~e) ^ (-(~m) / 2) * ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2) ^ ~p * (~x) ^ -(~m)) * (~d + ~e * (2 * ~q + 3) * (~x) ^ 2))), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) && (ILtQ(~q, -1) && ILtQ(~m / 2, 0))))
    @apply_utils Antiderivative((~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((-(~d)) ^ (~m / 2 - 1) * (~c * (~d) ^ 2 + ~a * (~e) ^ 2) ^ ~p * ~x * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (2 * (~e) ^ (2 * ~p + ~m / 2) * (~q + 1)) + ((-(~d)) ^ (~m / 2 - 1) / (2 * (~e) ^ (2 * ~p) * (~q + 1))) * Antiderivative((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * ExpandToSum(Together((1 / (~d + ~e * (~x) ^ 2)) * (2 * (-(~d)) ^ (-(~m) / 2 + 1) * (~e) ^ (2 * ~p) * (~q + 1) * (~a + ~c * (~x) ^ 4) ^ ~p - ((~e) ^ (-(~m) / 2) * (~c * (~d) ^ 2 + ~a * (~e) ^ 2) ^ ~p * (~x) ^ -(~m)) * (~d + ~e * (2 * ~q + 3) * (~x) ^ 2))), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (IGtQ(~p, 0) && (ILtQ(~q, -1) && ILtQ(~m / 2, 0)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) && IGtQ(~q, -2)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~a + ~c * (~x) ^ 4) ^ ~p, ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (IGtQ(~p, 0) && IGtQ(~q, -2))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => With([Qx = PolynomialQuotient((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~d + ~e * (~x) ^ 2, ~x), R = Coeff(PolynomialRemainder((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~d + ~e * (~x) ^ 2, ~x), ~x, 0)], (-R * (~f * ~x) ^ (~m + 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (2 * ~d * ~f * (~q + 1)) + (~f / (2 * ~d * (~q + 1))) * Antiderivative((~f * ~x) ^ (~m - 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * ExpandToSum(2 * ~d * (~q + 1) * ~x * Qx + R * (~m + 2 * ~q + 3) * ~x, ~x), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) && (LtQ(~q, -1) && GtQ(~m, 0))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => With([Qx = PolynomialQuotient((~a + ~c * (~x) ^ 4) ^ ~p, ~d + ~e * (~x) ^ 2, ~x), R = Coeff(PolynomialRemainder((~a + ~c * (~x) ^ 4) ^ ~p, ~d + ~e * (~x) ^ 2, ~x), ~x, 0)], (-R * (~f * ~x) ^ (~m + 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (2 * ~d * ~f * (~q + 1)) + (~f / (2 * ~d * (~q + 1))) * Antiderivative((~f * ~x) ^ (~m - 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * ExpandToSum(2 * ~d * (~q + 1) * ~x * Qx + R * (~m + 2 * ~q + 3) * ~x, ~x), ~x)) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (IGtQ(~p, 0) && (LtQ(~q, -1) && GtQ(~m, 0)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => With([Qx = PolynomialQuotient((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~f * ~x, ~x), R = PolynomialRemainder((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~f * ~x, ~x)], (R * (~f * ~x) ^ (~m + 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (~d * ~f * (~m + 1)) + (1 / (~d * (~f) ^ 2 * (~m + 1))) * Antiderivative((~f * ~x) ^ (~m + 2) * (~d + ~e * (~x) ^ 2) ^ ~q * ExpandToSum((~d * ~f * (~m + 1) * Qx) / ~x - ~e * R * (~m + 2 * ~q + 3), ~x), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) && LtQ(~m, -1)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => With([Qx = PolynomialQuotient((~a + ~c * (~x) ^ 4) ^ ~p, ~f * ~x, ~x), R = PolynomialRemainder((~a + ~c * (~x) ^ 4) ^ ~p, ~f * ~x, ~x)], (R * (~f * ~x) ^ (~m + 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (~d * ~f * (~m + 1)) + (1 / (~d * (~f) ^ 2 * (~m + 1))) * Antiderivative((~f * ~x) ^ (~m + 2) * (~d + ~e * (~x) ^ 2) ^ ~q * ExpandToSum((~d * ~f * (~m + 1) * Qx) / ~x - ~e * R * (~m + 2 * ~q + 3), ~x), ~x)) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~q], ~x) && (IGtQ(~p, 0) && LtQ(~m, -1))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((~c) ^ ~p * (~f * ~x) ^ ((~m + 4 * ~p) - 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (~e * (~f) ^ (4 * ~p - 1) * (~m + 4 * ~p + 2 * ~q + 1)) + (1 / (~e * (~m + 4 * ~p + 2 * ~q + 1))) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * ExpandToSum(~e * (~m + 4 * ~p + 2 * ~q + 1) * ((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p - (~c) ^ ~p * (~x) ^ (4 * ~p)) - ~d * (~c) ^ ~p * ((~m + 4 * ~p) - 1) * (~x) ^ (4 * ~p - 2), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) && (Not(IntegerQ(~q)) && NeQ(~m + 4 * ~p + 2 * ~q + 1, 0))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((~c) ^ ~p * (~f * ~x) ^ ((~m + 4 * ~p) - 1) * (~d + ~e * (~x) ^ 2) ^ (~q + 1)) / (~e * (~f) ^ (4 * ~p - 1) * (~m + 4 * ~p + 2 * ~q + 1)) + (1 / (~e * (~m + 4 * ~p + 2 * ~q + 1))) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * ExpandToSum(~e * (~m + 4 * ~p + 2 * ~q + 1) * ((~a + ~c * (~x) ^ 4) ^ ~p - (~c) ^ ~p * (~x) ^ (4 * ~p)) - ~d * (~c) ^ ~p * ((~m + 4 * ~p) - 1) * (~x) ^ (4 * ~p - 2), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (IGtQ(~p, 0) && (Not(IntegerQ(~q)) && NeQ(~m + 4 * ~p + 2 * ~q + 1, 0)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => With([k = Denominator(~m)], (k / ~f) * Subst(Antiderivative((~x) ^ (k * (~m + 1) - 1) * (~d + (~e * (~x) ^ (2k)) / (~f) ^ 2) ^ ~q * (~a + (~b * (~x) ^ (2k)) / (~f) ^ k + (~c * (~x) ^ (4k)) / (~f) ^ 4) ^ ~p, ~x), ~x, (~f * ~x) ^ (1 / k))) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~p, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (FractionQ(~m) && IntegerQ(~p)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~p, ~x) => With([k = Denominator(~m)], (k / ~f) * Subst(Antiderivative((~x) ^ (k * (~m + 1) - 1) * (~d + (~e * (~x) ^ (2k)) / ~f) ^ ~q * (~a + (~c * (~x) ^ (4k)) / ~f) ^ ~p, ~x), ~x, (~f * ~x) ^ (1 / k))) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~p, ~q], ~x) && (FractionQ(~m) && IntegerQ(~p))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((~f * ~x) ^ (~m + 1) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p * (~d * (~m + 4 * ~p + 3) + ~e * (~m + 1) * (~x) ^ 2)) / (~f * (~m + 1) * (~m + 4 * ~p + 3)) + ((2 * ~p) / ((~f) ^ 2 * (~m + 1) * (~m + 4 * ~p + 3))) * Antiderivative((~f * ~x) ^ (~m + 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p - 1) * Simp((2 * ~a * ~e * (~m + 1) - ~b * ~d * (~m + 4 * ~p + 3)) + (~b * ~e * (~m + 1) - 2 * ~c * ~d * (~m + 4 * ~p + 3)) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (GtQ(~p, 0) && (LtQ(~m, -1) && (~m + 4 * ~p + 3 != 0 && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m)))))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((~f * ~x) ^ (~m + 1) * (~a + ~c * (~x) ^ 4) ^ ~p * (~d * (~m + 4 * ~p + 3) + ~e * (~m + 1) * (~x) ^ 2)) / (~f * (~m + 1) * (~m + 4 * ~p + 3)) + ((4 * ~p) / ((~f) ^ 2 * (~m + 1) * (~m + 4 * ~p + 3))) * Antiderivative((~f * ~x) ^ (~m + 2) * (~a + ~c * (~x) ^ 4) ^ (~p - 1) * (~a * ~e * (~m + 1) - ~c * ~d * (~m + 4 * ~p + 3) * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (GtQ(~p, 0) && (LtQ(~m, -1) && (~m + 4 * ~p + 3 != 0 && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m))))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((~f * ~x) ^ (~m + 1) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p * (~b * ~e * 2 * ~p + ~c * ~d * (~m + 4 * ~p + 3) + ~c * ~e * (4 * ~p + ~m + 1) * (~x) ^ 2)) / (~c * ~f * (4 * ~p + ~m + 1) * (~m + 4 * ~p + 3)) + ((2 * ~p) / (~c * (4 * ~p + ~m + 1) * (~m + 4 * ~p + 3))) * Antiderivative((~f * ~x) ^ ~m * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p - 1) * Simp((2 * ~a * ~c * ~d * (~m + 4 * ~p + 3) - ~a * ~b * ~e * (~m + 1)) + ((2 * ~a * ~c * ~e * (4 * ~p + ~m + 1) + ~b * ~c * ~d * (~m + 4 * ~p + 3)) - (~b) ^ 2 * ~e * (~m + 2 * ~p + 1)) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (GtQ(~p, 0) && (NeQ(4 * ~p + ~m + 1, 0) && (NeQ(~m + 4 * ~p + 3, 0) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m)))))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => ((~f * ~x) ^ (~m + 1) * (~a + ~c * (~x) ^ 4) ^ ~p * (~c * ~d * (~m + 4 * ~p + 3) + ~c * ~e * (4 * ~p + ~m + 1) * (~x) ^ 2)) / (~c * ~f * (4 * ~p + ~m + 1) * (~m + 4 * ~p + 3)) + ((4 * ~a * ~p) / ((4 * ~p + ~m + 1) * (~m + 4 * ~p + 3))) * Antiderivative((~f * ~x) ^ ~m * (~a + ~c * (~x) ^ 4) ^ (~p - 1) * Simp(~d * (~m + 4 * ~p + 3) + ~e * (4 * ~p + ~m + 1) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x) && (GtQ(~p, 0) && (NeQ(4 * ~p + ~m + 1, 0) && (NeQ(~m + 4 * ~p + 3, 0) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m))))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (~f * (~f * ~x) ^ (~m - 1) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * ((~b * ~d - 2 * ~a * ~e) - (~b * ~e - 2 * ~c * ~d) * (~x) ^ 2)) / (2 * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c)) - ((~f) ^ 2 / (2 * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c))) * Antiderivative((~f * ~x) ^ (~m - 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * Simp((~m - 1) * (~b * ~d - 2 * ~a * ~e) - (4 * ~p + 4 + ~m + 1) * (~b * ~e - 2 * ~c * ~d) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~p, -1) && (GtQ(~m, 1) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m))))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => (~f * (~f * ~x) ^ (~m - 1) * (~a + ~c * (~x) ^ 4) ^ (~p + 1) * (~a * ~e - ~c * ~d * (~x) ^ 2)) / (4 * ~a * ~c * (~p + 1)) - ((~f) ^ 2 / (4 * ~a * ~c * (~p + 1))) * Antiderivative((~f * ~x) ^ (~m - 2) * (~a + ~c * (~x) ^ 4) ^ (~p + 1) * (~a * ~e * (~m - 1) - ~c * ~d * (4 * ~p + 4 + ~m + 1) * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (LtQ(~p, -1) && (GtQ(~m, 1) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m)))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (-((~f * ~x) ^ (~m + 1)) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * ((~d * ((~b) ^ 2 - 2 * ~a * ~c) - ~a * ~b * ~e) + (~b * ~d - 2 * ~a * ~e) * ~c * (~x) ^ 2)) / (2 * ~a * ~f * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c)) + (1 / (2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c))) * Antiderivative((~f * ~x) ^ ~m * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * Simp((~d * ((~b) ^ 2 * (~m + 2 * (~p + 1) + 1) - 2 * ~a * ~c * (~m + 4 * (~p + 1) + 1)) - ~a * ~b * ~e * (~m + 1)) + ~c * (~m + 2 * (2 * ~p + 3) + 1) * (~b * ~d - 2 * ~a * ~e) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~p, -1) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m)))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (-((~f * ~x) ^ (~m + 1)) * (~a + ~c * (~x) ^ 4) ^ (~p + 1) * (~d + ~e * (~x) ^ 2)) / (4 * ~a * ~f * (~p + 1)) + (1 / (4 * ~a * (~p + 1))) * Antiderivative((~f * ~x) ^ ~m * (~a + ~c * (~x) ^ 4) ^ (~p + 1) * Simp(~d * (~m + 4 * (~p + 1) + 1) + ~e * (~m + 2 * (2 * ~p + 3) + 1) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x) && (LtQ(~p, -1) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (~e * ~f * (~f * ~x) ^ (~m - 1) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1)) / (~c * (~m + 4 * ~p + 3)) - ((~f) ^ 2 / (~c * (~m + 4 * ~p + 3))) * Antiderivative((~f * ~x) ^ (~m - 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p * Simp(~a * ~e * (~m - 1) + (~b * ~e * (~m + 2 * ~p + 1) - ~c * ~d * (~m + 4 * ~p + 3)) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~p], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (GtQ(~m, 1) && (NeQ(~m + 4 * ~p + 3, 0) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m))))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (~e * ~f * (~f * ~x) ^ (~m - 1) * (~a + ~c * (~x) ^ 4) ^ (~p + 1)) / (~c * (~m + 4 * ~p + 3)) - ((~f) ^ 2 / (~c * (~m + 4 * ~p + 3))) * Antiderivative((~f * ~x) ^ (~m - 2) * (~a + ~c * (~x) ^ 4) ^ ~p * (~a * ~e * (~m - 1) - ~c * ~d * (~m + 4 * ~p + 3) * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~p], ~x) && (GtQ(~m, 1) && (NeQ(~m + 4 * ~p + 3, 0) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m)))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (~d * (~f * ~x) ^ (~m + 1) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1)) / (~a * ~f * (~m + 1)) + (1 / (~a * (~f) ^ 2 * (~m + 1))) * Antiderivative((~f * ~x) ^ (~m + 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p * Simp((~a * ~e * (~m + 1) - ~b * ~d * (~m + 2 * ~p + 3)) - ~c * ~d * (~m + 4 * ~p + 5) * (~x) ^ 2, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~p], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~m, -1) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m)))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) * (~a + ~(c') * (~x) ^ 4) ^ ~p, ~x) => (~d * (~f * ~x) ^ (~m + 1) * (~a + ~c * (~x) ^ 4) ^ (~p + 1)) / (~a * ~f * (~m + 1)) + (1 / (~a * (~f) ^ 2 * (~m + 1))) * Antiderivative((~f * ~x) ^ (~m + 2) * (~a + ~c * (~x) ^ 4) ^ ~p * (~a * ~e * (~m + 1) - ~c * ~d * (~m + 4 * ~p + 5) * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~p], ~x) && (LtQ(~m, -1) && (IntegerQ(2 * ~p) && (IntegerQ(~p) || IntegerQ(~m))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2)) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => With([r = Rt((~c / ~e) * (2 * ~c * ~d - ~b * ~e), 2)], (~e / 2) * Antiderivative((~f * ~x) ^ ~m / (((~c * ~d) / ~e - r * ~x) + ~c * (~x) ^ 2), ~x) + (~e / 2) * Antiderivative((~f * ~x) ^ ~m / ((~c * ~d) / ~e + r * ~x + ~c * (~x) ^ 2), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (EqQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0) && (GtQ(~d / ~e, 0) && PosQ((~c / ~e) * (2 * ~c * ~d - ~b * ~e)))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2)) / (~a + ~(c') * (~x) ^ 4), ~x) => With([r = Rt((2 * (~c) ^ 2 * ~d) / ~e, 2)], (~e / 2) * Antiderivative((~f * ~x) ^ ~m / (((~c * ~d) / ~e - r * ~x) + ~c * (~x) ^ 2), ~x) + (~e / 2) * Antiderivative((~f * ~x) ^ ~m / ((~c * ~d) / ~e + r * ~x + ~c * (~x) ^ 2), ~x)) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x) && (EqQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0) && GtQ(~d / ~e, 0))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2)) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => With([q = Rt((~b) ^ 2 - 4 * ~a * ~c, 2)], (~e / 2 + (2 * ~c * ~d - ~b * ~e) / (2q)) * Antiderivative((~f * ~x) ^ ~m / ((~b / 2 - q / 2) + ~c * (~x) ^ 2), ~x) + (~e / 2 - (2 * ~c * ~d - ~b * ~e) / (2q)) * Antiderivative((~f * ~x) ^ ~m / (~b / 2 + q / 2 + ~c * (~x) ^ 2), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && NeQ((~b) ^ 2 - 4 * ~a * ~c, 0)
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2)) / (~a + ~(c') * (~x) ^ 4), ~x) => With([q = Rt(-(~a) * ~c, 2)], -((~e / 2 + (~c * ~d) / (2q))) * Antiderivative((~f * ~x) ^ ~m / (q - ~c * (~x) ^ 2), ~x) + (~e / 2 - (~c * ~d) / (2q)) * Antiderivative((~f * ~x) ^ ~m / (q + ~c * (~x) ^ 2), ~x)) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x)
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q')) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IntegerQ(~q) && IntegerQ(~m)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q')) / (~a + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q) / (~a + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x) && (IntegerQ(~q) && IntegerQ(~m))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q')) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m, (~d + ~e * (~x) ^ 2) ^ ~q / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IntegerQ(~q) && Not(IntegerQ(~m))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q')) / (~a + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m, (~d + ~e * (~x) ^ 2) ^ ~q / (~a + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x) && (IntegerQ(~q) && Not(IntegerQ(~m)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => ((~f) ^ 4 / (~c) ^ 2) * Antiderivative((~f * ~x) ^ (~m - 4) * ((~c * ~d - ~b * ~e) + ~c * ~e * (~x) ^ 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1), ~x) - ((~f) ^ 4 / (~c) ^ 2) * Antiderivative(((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ (~q - 1) * Simp(~a * (~c * ~d - ~b * ~e) + ((~b * ~c * ~d - (~b) ^ 2 * ~e) + ~a * ~c * ~e) * (~x) ^ 2, ~x)) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && (GtQ(~q, 0) && GtQ(~m, 3))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => ((~f) ^ 4 / ~c) * Antiderivative((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) - ((~a * (~f) ^ 4) / ~c) * Antiderivative(((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ ~q) / (~a + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~q], ~x) && (Not(IntegerQ(~q)) && GtQ(~m, 3))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => ((~e * (~f) ^ 2) / ~c) * Antiderivative((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1), ~x) - ((~f) ^ 2 / ~c) * Antiderivative(((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1) * Simp(~a * ~e - (~c * ~d - ~b * ~e) * (~x) ^ 2, ~x)) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && (GtQ(~q, 0) && (GtQ(~m, 1) && LeQ(~m, 3)))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => ((~e * (~f) ^ 2) / ~c) * Antiderivative((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1), ~x) - ((~f) ^ 2 / ~c) * Antiderivative(((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1) * Simp(~a * ~e - ~c * ~d * (~x) ^ 2, ~x)) / (~a + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (Not(IntegerQ(~q)) && (GtQ(~q, 0) && (GtQ(~m, 1) && LeQ(~m, 3))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~m * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => (~d / ~a) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q - 1), ~x) - (1 / (~a * (~f) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m + 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1) * Simp((~b * ~d - ~a * ~e) + ~c * ~d * (~x) ^ 2, ~x)) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && (GtQ(~q, 0) && LtQ(~m, 0))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~m * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => (~d / ~a) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q - 1), ~x) + (1 / (~a * (~f) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m + 2) * (~d + ~e * (~x) ^ 2) ^ (~q - 1) * Simp(~a * ~e - ~c * ~d * (~x) ^ 2, ~x)) / (~a + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (Not(IntegerQ(~q)) && (GtQ(~q, 0) && LtQ(~m, 0)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => (((~d) ^ 2 * (~f) ^ 4) / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) - ((~f) ^ 4 / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * Simp(~a * ~d + (~b * ~d - ~a * ~e) * (~x) ^ 2, ~x)) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && (LtQ(~q, -1) && GtQ(~m, 3))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => (((~d) ^ 2 * (~f) ^ 4) / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) - ((~a * (~f) ^ 4) / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 4) * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * (~d - ~e * (~x) ^ 2)) / (~a + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (Not(IntegerQ(~q)) && (LtQ(~q, -1) && GtQ(~m, 3)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => ((-(~d) * ~e * (~f) ^ 2) / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) + ((~f) ^ 2 / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * Simp(~a * ~e + ~c * ~d * (~x) ^ 2, ~x)) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && (LtQ(~q, -1) && (GtQ(~m, 1) && LeQ(~m, 3)))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(d') + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => ((-(~d) * ~e * (~f) ^ 2) / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) + ((~f) ^ 2 / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 2) * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * Simp(~a * ~e + ~c * ~d * (~x) ^ 2, ~x)) / (~a + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (Not(IntegerQ(~q)) && (LtQ(~q, -1) && (GtQ(~m, 1) && LeQ(~m, 3))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => ((~e) ^ 2 / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) + (1 / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * Simp((~c * ~d - ~b * ~e) - ~c * ~e * (~x) ^ 2, ~x)) / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && LtQ(~q, -1)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => ((~e) ^ 2 / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~x) + (~c / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ (~q + 1) * (~d - ~e * (~x) ^ 2)) / (~a + ~c * (~x) ^ 4), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m], ~x) && (Not(IntegerQ(~q)) && LtQ(~q, -1))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand((~d + ~e * (~x) ^ 2) ^ ~q, (~f * ~x) ^ ~m / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && IntegerQ(~m)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand((~d + ~e * (~x) ^ 2) ^ ~q, (~f * ~x) ^ ~m / (~a + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~q], ~x) && (Not(IntegerQ(~q)) && IntegerQ(~m))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, 1 / (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (Not(IntegerQ(~q)) && Not(IntegerQ(~m))))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, 1 / (~a + ~c * (~x) ^ 4), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && (Not(IntegerQ(~q)) && Not(IntegerQ(~m)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4), ~x) => With([r = Rt((~b) ^ 2 - 4 * ~a * ~c, 2)], ((2 * ~c) / r) * Antiderivative(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q) / ((~b - r) + 2 * ~c * (~x) ^ 2), ~x) - ((2 * ~c) / r) * Antiderivative(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q) / (~b + r + 2 * ~c * (~x) ^ 2), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~q], ~x) && NeQ((~b) ^ 2 - 4 * ~a * ~c, 0)
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q) / (~a + ~(c') * (~x) ^ 4), ~x) => With([r = Rt(-(~a) * ~c, 2)], (-(~c) / (2r)) * Antiderivative(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q) / (r - ~c * (~x) ^ 2), ~x) - (~c / (2r)) * Antiderivative(((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q) / (r + ~c * (~x) ^ 2), ~x)) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~q], ~x)
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~m * (~(a') + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p')) / (~(d') + ~(e') * (~x) ^ 2), ~x) => (1 / (~d) ^ 2) * Antiderivative((~f * ~x) ^ ~m * (~a * ~d + (~b * ~d - ~a * ~e) * (~x) ^ 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p - 1), ~x) + (((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2) / ((~d) ^ 2 * (~f) ^ 4)) * Antiderivative(((~f * ~x) ^ (~m + 4) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p - 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (GtQ(~p, 0) && LtQ(~m, -2)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~m * (~a + ~(c') * (~x) ^ 4) ^ ~(p')) / (~(d') + ~(e') * (~x) ^ 2), ~x) => (~a / (~d) ^ 2) * Antiderivative((~f * ~x) ^ ~m * (~d - ~e * (~x) ^ 2) * (~a + ~c * (~x) ^ 4) ^ (~p - 1), ~x) + ((~c * (~d) ^ 2 + ~a * (~e) ^ 2) / ((~d) ^ 2 * (~f) ^ 4)) * Antiderivative(((~f * ~x) ^ (~m + 4) * (~a + ~c * (~x) ^ 4) ^ (~p - 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (GtQ(~p, 0) && LtQ(~m, -2))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~m * (~(a') + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p')) / (~(d') + ~(e') * (~x) ^ 2), ~x) => (1 / (~d * ~e)) * Antiderivative((~f * ~x) ^ ~m * (~a * ~e + ~c * ~d * (~x) ^ 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p - 1), ~x) - (((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2) / (~d * ~e * (~f) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m + 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p - 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (GtQ(~p, 0) && LtQ(~m, 0)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~m * (~a + ~(c') * (~x) ^ 4) ^ ~(p')) / (~(d') + ~(e') * (~x) ^ 2), ~x) => (1 / (~d * ~e)) * Antiderivative((~f * ~x) ^ ~m * (~a * ~e + ~c * ~d * (~x) ^ 2) * (~a + ~c * (~x) ^ 4) ^ (~p - 1), ~x) - ((~c * (~d) ^ 2 + ~a * (~e) ^ 2) / (~d * ~e * (~f) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m + 2) * (~a + ~c * (~x) ^ 4) ^ (~p - 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (GtQ(~p, 0) && LtQ(~m, 0))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(a') + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p) / (~(d') + ~(e') * (~x) ^ 2), ~x) => (-((~f) ^ 4) / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 4) * (~a * ~d + (~b * ~d - ~a * ~e) * (~x) ^ 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~x) + (((~d) ^ 2 * (~f) ^ 4) / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 4) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~p, -1) && GtQ(~m, 2)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~a + ~(c') * (~x) ^ 4) ^ ~p) / (~(d') + ~(e') * (~x) ^ 2), ~x) => ((-(~a) * (~f) ^ 4) / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 4) * (~d - ~e * (~x) ^ 2) * (~a + ~c * (~x) ^ 4) ^ ~p, ~x) + (((~d) ^ 2 * (~f) ^ 4) / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 4) * (~a + ~c * (~x) ^ 4) ^ (~p + 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (LtQ(~p, -1) && GtQ(~m, 2))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~(a') + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p) / (~(d') + ~(e') * (~x) ^ 2), ~x) => ((~f) ^ 2 / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 2) * (~a * ~e + ~c * ~d * (~x) ^ 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~x) - ((~d * ~e * (~f) ^ 2) / ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 2) * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~p, -1) && GtQ(~m, 0)))
    @apply_utils Antiderivative(((~(f') * ~x) ^ ~(m') * (~a + ~(c') * (~x) ^ 4) ^ ~p) / (~(d') + ~(e') * (~x) ^ 2), ~x) => ((~f) ^ 2 / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative((~f * ~x) ^ (~m - 2) * (~a * ~e + ~c * ~d * (~x) ^ 2) * (~a + ~c * (~x) ^ 4) ^ ~p, ~x) - ((~d * ~e * (~f) ^ 2) / (~c * (~d) ^ 2 + ~a * (~e) ^ 2)) * Antiderivative(((~f * ~x) ^ (~m - 2) * (~a + ~c * (~x) ^ 4) ^ (~p + 1)) / (~d + ~e * (~x) ^ 2), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f], ~x) && (LtQ(~p, -1) && GtQ(~m, 0))
    @apply_utils Antiderivative((~x) ^ 2 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => (~d / (2 * ~d * ~e)) * Antiderivative(1 / sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) - (~d / (2 * ~d * ~e)) * Antiderivative((~d - ~e * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (NeQ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2, 0) && (PosQ(~c / ~a) && EqQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0))))
    @apply_utils Antiderivative((~x) ^ 2 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => (~d / (2 * ~d * ~e)) * Antiderivative(1 / sqrt(~a + ~c * (~x) ^ 4), ~x) - (~d / (2 * ~d * ~e)) * Antiderivative((~d - ~e * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4)), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (NeQ(~c * (~d) ^ 2 + ~a * (~e) ^ 2, 0) && (PosQ(~c / ~a) && EqQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0)))
    @apply_utils Antiderivative((~x) ^ 2 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => With([q = Rt(~c / ~a, 2)], ((-(~a) * (~e + ~d * q)) / (~c * (~d) ^ 2 - ~a * (~e) ^ 2)) * Antiderivative(1 / sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) + ((~a * ~d * (~e + ~d * q)) / (~c * (~d) ^ 2 - ~a * (~e) ^ 2)) * Antiderivative((1 + q * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (NeQ((~c * (~d) ^ 2 - ~b * ~d * ~e) + ~a * (~e) ^ 2, 0) && (PosQ(~c / ~a) && NeQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0))))
    @apply_utils Antiderivative((~x) ^ 2 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => With([q = Rt(~c / ~a, 2)], ((-(~a) * (~e + ~d * q)) / (~c * (~d) ^ 2 - ~a * (~e) ^ 2)) * Antiderivative(1 / sqrt(~a + ~c * (~x) ^ 4), ~x) + ((~a * ~d * (~e + ~d * q)) / (~c * (~d) ^ 2 - ~a * (~e) ^ 2)) * Antiderivative((1 + q * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4)), ~x)) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (NeQ(~c * (~d) ^ 2 + ~a * (~e) ^ 2, 0) && (PosQ(~c / ~a) && NeQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0)))
    @apply_utils Antiderivative((~x) ^ 4 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => (-1 / (~e) ^ 2) * Antiderivative((~d - ~e * (~x) ^ 2) / sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) + ((~d) ^ 2 / (~e) ^ 2) * Antiderivative(1 / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (PosQ(~c / ~a) && EqQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0)))
    @apply_utils Antiderivative((~x) ^ 4 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => (-1 / (~e) ^ 2) * Antiderivative((~d - ~e * (~x) ^ 2) / sqrt(~a + ~c * (~x) ^ 4), ~x) + ((~d) ^ 2 / (~e) ^ 2) * Antiderivative(1 / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4)), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (PosQ(~c / ~a) && EqQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0))
    @apply_utils Antiderivative((~x) ^ 4 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => With([q = Rt(~c / ~a, 2)], (-1 / (~e * q)) * Antiderivative((1 - q * (~x) ^ 2) / sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) + ((~d) ^ 2 / (~e * (~e - ~d * q))) * Antiderivative((1 + q * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)), ~x) <-- EqQ(2 * ~c * ~d - ~a * ~e * q, 0)) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (PosQ(~c / ~a) && NeQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0)))
    @apply_utils Antiderivative((~x) ^ 4 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => With([q = Rt(~c / ~a, 2)], (-1 / (~e * q)) * Antiderivative((1 - q * (~x) ^ 2) / sqrt(~a + ~c * (~x) ^ 4), ~x) + ((~d) ^ 2 / (~e * (~e - ~d * q))) * Antiderivative((1 + q * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4)), ~x) <-- EqQ(2 * ~c * ~d - ~a * ~e * q, 0)) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (PosQ(~c / ~a) && NeQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0))
    @apply_utils Antiderivative((~x) ^ 4 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => With([q = Rt(~c / ~a, 2)], ((-((2 * ~c * ~d - ~a * ~e * q)) / (~c * ~e * (~e - ~d * q))) * Antiderivative(1 / sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x) - (1 / (~e * q)) * Antiderivative((1 - q * (~x) ^ 2) / sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4), ~x)) + ((~d) ^ 2 / (~e * (~e - ~d * q))) * Antiderivative((1 + q * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (PosQ(~c / ~a) && NeQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0)))
    @apply_utils Antiderivative((~x) ^ 4 / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => With([q = Rt(~c / ~a, 2)], ((-((2 * ~c * ~d - ~a * ~e * q)) / (~c * ~e * (~e - ~d * q))) * Antiderivative(1 / sqrt(~a + ~c * (~x) ^ 4), ~x) - (1 / (~e * q)) * Antiderivative((1 - q * (~x) ^ 2) / sqrt(~a + ~c * (~x) ^ 4), ~x)) + ((~d) ^ 2 / (~e * (~e - ~d * q))) * Antiderivative((1 + q * (~x) ^ 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4)), ~x)) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && (PosQ(~c / ~a) && NeQ(~c * (~d) ^ 2 - ~a * (~e) ^ 2, 0))
    @apply_utils Antiderivative((~x) ^ ~m / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => ((~x) ^ (~m - 5) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)) / (~c * ~e * (~m - 3)) - (1 / (~c * ~e * (~m - 3))) * Antiderivative(((~x) ^ (~m - 6) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4))) * Simp(~a * ~d * (~m - 5) + (~a * ~e * (~m - 5) + ~b * ~d * (~m - 4)) * (~x) ^ 2 + (~b * ~e * (~m - 4) + ~c * ~d * (~m - 3)) * (~x) ^ 4, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && IGtQ(~m / 2, 2))
    @apply_utils Antiderivative((~x) ^ ~m / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => ((~x) ^ (~m - 5) * sqrt(~a + ~c * (~x) ^ 4)) / (~c * ~e * (~m - 3)) - (1 / (~c * ~e * (~m - 3))) * Antiderivative(((~x) ^ (~m - 6) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4))) * Simp(~a * ~d * (~m - 5) + ~a * ~e * (~m - 5) * (~x) ^ 2 + ~c * ~d * (~m - 3) * (~x) ^ 4, ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && IGtQ(~m / 2, 2)
    @apply_utils Antiderivative((~x) ^ ~m / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => ((~x) ^ (~m + 1) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4)) / (~a * ~d * (~m + 1)) - (1 / (~a * ~d * (~m + 1))) * Antiderivative(((~x) ^ (~m + 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4))) * Simp(~a * ~e * (~m + 1) + ~b * ~d * (~m + 2) + (~b * ~e * (~m + 2) + ~c * ~d * (~m + 3)) * (~x) ^ 2 + ~c * ~e * (~m + 3) * (~x) ^ 4, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && ILtQ(~m / 2, 0))
    @apply_utils Antiderivative((~x) ^ ~m / ((~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => ((~x) ^ (~m + 1) * sqrt(~a + ~c * (~x) ^ 4)) / (~a * ~d * (~m + 1)) - (1 / (~a * ~d * (~m + 1))) * Antiderivative(((~x) ^ (~m + 2) / ((~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4))) * Simp(~a * ~e * (~m + 1) + ~c * ~d * (~m + 3) * (~x) ^ 2 + ~c * ~e * (~m + 3) * (~x) ^ 4, ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && ILtQ(~m / 2, 0)
    @apply_utils Antiderivative((~x) ^ ~m / (sqrt(~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4)), ~x) => (((~x) ^ 3 * sqrt(~e + ~d / (~x) ^ 2) * sqrt(~c + ~b / (~x) ^ 2 + ~a / (~x) ^ 4)) / (sqrt(~d + ~e * (~x) ^ 2) * sqrt(~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4))) * Antiderivative((~x) ^ (~m - 3) / (sqrt(~e + ~d / (~x) ^ 2) * sqrt(~c + ~b / (~x) ^ 2 + ~a / (~x) ^ 4)), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && IntegerQ(~m / 2))
    @apply_utils Antiderivative((~x) ^ ~m / (sqrt(~d + ~(e') * (~x) ^ 2) * sqrt(~a + ~(c') * (~x) ^ 4)), ~x) => (((~x) ^ 3 * sqrt(~e + ~d / (~x) ^ 2) * sqrt(~c + ~a / (~x) ^ 4)) / (sqrt(~d + ~e * (~x) ^ 2) * sqrt(~a + ~c * (~x) ^ 4))) * Antiderivative((~x) ^ (~m - 3) / (sqrt(~e + ~d / (~x) ^ 2) * sqrt(~c + ~a / (~x) ^ 4)), ~x) <-- FreeQ([~a, ~c, ~d, ~e], ~x) && IntegerQ(~m / 2)
    @apply_utils Antiderivative((~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => With([f = Coeff(PolynomialRemainder((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4, ~x), ~x, 0), g = Coeff(PolynomialRemainder((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4, ~x), ~x, 2)], (~x * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * ((~a * ~b * g - f * ((~b) ^ 2 - 2 * ~a * ~c)) - ~c * (~b * f - 2 * ~a * g) * (~x) ^ 2)) / (2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c)) + (1 / (2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c))) * Antiderivative((~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * Simp(ExpandToSum((((2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c) * PolynomialQuotient((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4, ~x) + (~b) ^ 2 * f * (2 * ~p + 3)) - 2 * ~a * ~c * f * (4 * ~p + 5)) - ~a * ~b * g) + ~c * (4 * ~p + 7) * (~b * f - 2 * ~a * g) * (~x) ^ 2, ~x), ~x), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~p, -1) && (IGtQ(~q, 1) && IGtQ(~m / 2, 0))))
    @apply_utils Antiderivative((~x) ^ ~m * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~p, ~x) => With([f = Coeff(PolynomialRemainder((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4, ~x), ~x, 0), g = Coeff(PolynomialRemainder((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4, ~x), ~x, 2)], (~x * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * ((~a * ~b * g - f * ((~b) ^ 2 - 2 * ~a * ~c)) - ~c * (~b * f - 2 * ~a * g) * (~x) ^ 2)) / (2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c)) + (1 / (2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c))) * Antiderivative((~x) ^ ~m * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ (~p + 1) * Simp(ExpandToSum(2 * ~a * (~p + 1) * ((~b) ^ 2 - 4 * ~a * ~c) * (~x) ^ -(~m) * PolynomialQuotient((~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q, ~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4, ~x) + (((~b) ^ 2 * f * (2 * ~p + 3) - 2 * ~a * ~c * f * (4 * ~p + 5)) - ~a * ~b * g) * (~x) ^ -(~m) + ~c * (4 * ~p + 7) * (~b * f - 2 * ~a * g) * (~x) ^ (2 - ~m), ~x), ~x), ~x)) <-- FreeQ([~a, ~b, ~c, ~d, ~e], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (LtQ(~p, -1) && (IGtQ(~q, 1) && ILtQ(~m / 2, 0))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~x), ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x) && (NeQ((~b) ^ 2 - 4 * ~a * ~c, 0) && (IGtQ(~p, 0) || (IGtQ(~q, 0) || IntegersQ(~m, ~q))))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Antiderivative(ExpandIntegrand((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~a + ~c * (~x) ^ 4) ^ ~p, ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x) && (IGtQ(~p, 0) || (IGtQ(~q, 0) || IntegersQ(~m, ~q)))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~q * (~a + ~(c') * (~x) ^ 4) ^ ~p, ~x) => ((~f * ~x) ^ ~m / (~x) ^ ~m) * Antiderivative(ExpandIntegrand((~x) ^ ~m * (~a + ~c * (~x) ^ 4) ^ ~p, (~d / ((~d) ^ 2 - (~e) ^ 2 * (~x) ^ 4) - (~e * (~x) ^ 2) / ((~d) ^ 2 - (~e) ^ 2 * (~x) ^ 4)) ^ -(~q), ~x), ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~p], ~x) && (Not(IntegerQ(~p)) && ILtQ(~q, 0))
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(b') * (~x) ^ 2 + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Unintegrable((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~a + ~b * (~x) ^ 2 + ~c * (~x) ^ 4) ^ ~p, ~x) <-- FreeQ([~a, ~b, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x)
    @apply_utils Antiderivative((~(f') * ~x) ^ ~(m') * (~d + ~(e') * (~x) ^ 2) ^ ~(q') * (~a + ~(c') * (~x) ^ 4) ^ ~(p'), ~x) => Unintegrable((~f * ~x) ^ ~m * (~d + ~e * (~x) ^ 2) ^ ~q * (~a + ~c * (~x) ^ 4) ^ ~p, ~x) <-- FreeQ([~a, ~c, ~d, ~e, ~f, ~m, ~p, ~q], ~x)
end

max_stars_repo_path: src/rules/1 Algebraic functions/1.2 Trinomial products/1.2.2 Quartic/1.2.2.4 (f x)^m (d+e x^2)^q (a+b x^2+c x^4)^p.jl


text:
# File with plotting routines

# constants
const w0 = 3.0e14

abstract type FileType end
struct SIFlux       <: FileType end
struct TotalFlux{T} <: FileType end

"""
    plot_scuff(filetype :: SIFlux,...)

Plot and save (optional) the spectral flux and transfer function.

# Arguments
- `trans` : the number of the transformation, trans = "DEFAULT" if no transformation is given
- `filename ::  Union{String,Vector{String}}` : can be single filename or an array of filenames for parallel computations
- `columnname` : column name. Example : [:c12]. Only use one name at the time.
"""
function plot_scuff(filetype :: SIFlux, filename :: Union{String,Vector{String}}, columnname ,T1,T2, trans = "DEFAULT"; savefile = (false," "))

    wv, Pabs, Prad, qtrans = simulation_data(filetype, filename , columnname ,T1,T2, trans = trans; savefile = savefile)

    p1 = plot(wv, abs.(Prad),
              xscale = :log10,
              xlim = (wv[1],wv[end]),
              yscale = :log10,
              title = "Prad",
              xlabel = "Frequency (rad/s)",
              ylabel= "Transfer function")
    p2 = plot(wv, abs.(Pabs),
              xscale = :log10,
              xlim = (wv[1],wv[end]),
              yscale = :log10,
              title = "Pabs",
              xlabel = "Frequency (rad/s)",
              ylabel= "Transfer function")
    p3 = plot(wv, abs.(qtrans) ,
              xscale = :log10,
              yscale = :log10,
              title = "Ptransf",
              xlabel ="Frequency (rad/s)" ,
              ylabel= "heat transfer")
    l = @layout [p1 p2; p3]
    plot(p1,p2,p3,layout = l)

end

"""
    plot_scuff(filetype :: TotalFlux{:vsT},...)

Compute and plot total heat_transfer as a function temparature of one of the bodies.
# Arguments
- `Tmin` : minimum value of temperature of body 2
- `Tmax` : maximum value of temperature of body 2
- `T1 = 0.0` : temperature of body 1. Set by default at 0.0K
"""
function plot_scuff(filetype :: TotalFlux{:vsT}, filename:: Union{String,Vector{String}}, columnname,Tmin,Tmax, trans= "DEFAULT";T1=0.0, savefile = (false," "))

    wv, Pabs, Prad, Tempv, q_tot = simulation_data(filetype, filename, columnname ,Tmin,Tmax, trans= trans;T1=T1, savefile = savefile)

    p1 = plot(wv, abs.(Prad),
              xscale = :log10,
              xlim = (wv[1],wv[end]),
              yscale = :log10, #ylim = (1e-30,1e-10),
              title = "Prad",
              xlabel = "Frequency (rad/s)",
              ylabel= "Transfer function")
    p2 = plot(Tempv, abs.(q_tot),
              yscale = :log10,
              title = "Qtransf",
              xlabel ="Temperature (K)" ,
              ylabel= "Total flux (W)")
    l = @layout [p1 p2]
    plot(p1,p2,layout = l)

end

"""
    plot_scuff(filetype :: TotalFlux{:vsd},...)

Computes total heat_transfer as a function separation distance.
# Arguments
- `T1` : Temperature of body 1
- `T2` : Temperature of body 2
- `trans` : vector containing the transformation label
"""
function plot_scuff(filetype :: TotalFlux{:vsd}, filename :: Union{String,Vector{String}}, columnname ,T1,T2, trans; savefile = (false," "))

trans, q_tot = simulation_data(filetype, filename, columnname ,T1,T2, trans; savefile = savefile)

    plot(trans, abs.(q_tot),
         yscale = :log10, #ylim = (1e-,1e-5),
         title = "Prad vs separation distance",
         xlabel = "separation distance",
         ylabel= "Total flux (W)")

end

"""
    plot_scuff(filetype :: TotalFlux{:vsth},...)

Computes total heat_transfer as a function cylinder thickness.
# Arguments
- `T1` : Temperature of body 1
- `T2` : Temperature of body 2
- `trans` : vector containing the transformation label
"""
function plot_scuff(filetype :: TotalFlux{:vsth}, filename :: Union{String,Vector{String}}, columnname ,T1,T2, trans; savefile = (false," "))

trans, q_tot = simulation_data(filetype, filename, columnname ,T1,T2, trans; savefile = savefile)

    plot(trans, abs.(q_tot),
         yscale = :log10, #ylim = (1e-,1e-5),
         title = "Prad vs cylinder thickness",
         xlabel = "Cylinder thickness",
         ylabel= "Total flux (W)")

end

"Checks the convergence for the number of frequencies and type by coputing the total flux"

function benchmark_freq(filetype :: SIFlux, dirname :: Union{String,Vector{String}} , filename:: String, columnname ;T1=1.0,T2=0.0, savefile = (false," "))
    discrtype = ["logspace" "linspace"]
    discrnum  = ["N=50" ;"N=100"; "N=150" ;"N=200";"N=500" ; "N=1000" ]
    cnt2 = 0
    q_tot = zeros(Float64,length(discrnum),length(discrtype))
    for dt in discrtype
        cnt2 = cnt2 + 1
        cnt1 = 0
        for dn in discrnum
            cnt1 = cnt1 + 1
            flename = dirname*"/"*dt*"/"*dn*"/"*filename
            dfPabs,dfPrad = import_data(filetype, flename)
            u = dfPabs[:Freq]
            τ = dfPrad[columnname...]
            q_tot[cnt1,cnt2] = total_transfer(T1,T2,u.*w0,τ)
         end
    end

    if savefile[1] == true
        dfqtot = convert(DataFrame,q_tot)
        writetable("Qtot_vs_freqNum_"*savefile[2]*".dat", dfqtot)
    end

    rel_error = abs(q_tot[:,1].-q_tot[:,2])./(-q_tot[:,1]).*100
    p1 = plot([50;100;150;200;500;1000],-q_tot,yaxis = :log10)
    p2 = plot([50;100;150;200;500;1000],rel_error)
    l = @layout [p1 p2]
    plot(p1,p2, layout = l)

end

ext: jl


text:
# DS1000Z oscilloscope function generator driver

mutable struct DS1000Z_FGEN
    handle
	initialized
    instr_dict
	wfm_dict
	rev_wfm_dict
	togg_wfm_dict
	unit_dict
	fct
end
DS1000Z_FGEN(handle) = DS1000Z_FGEN(
                handle,
				false,
                Dict(
                    "C1"=>"1",
					"C2"=>"2"
                    ),
                Dict(
					"sinusoid"=>"SINusoid",
					"triangle"=>"RAMP",
					"square"=>"PULSe", # use pulse because it accepts duty cycle
                    ),
                Dict(
                    "SIN"=>"sinusoid",
					"RAMP"=>"triangle",
					"PULS"=>"square",
                    ),
                Dict(
                    "1"=>"sinusoid",
					"2"=>"triangle",
					"3"=>"square",
                    ),
                Dict(
					"Vpp"=>"1",
					"Vrms"=>"2",
					"dBm"=>"3",
					"1"=>"Vpp",
					"2"=>"Vrms",
					"3"=>"dBm",
                    ),
					"sinusoid",
                    )

# MISC
function connect!(obj::DS1000Z_FGEN, handle) # DONE
	obj.handle = handle
    obj.connected = 1
	# set output default on
	write(obj.handle, ":SOURce1:OUTPut1:STATe 1")
	write(obj.handle, ":SOURce2:OUTPut2:STATe 1")
	# set default 50Ohm
	write(obj.handle, ":SOURce1:OUTPut1:IMPedance FIFTy")
	write(obj.handle, ":SOURce2:OUTPut2:IMPedance FIFTy")
	return nothing
end
function disconnect!(obj::DS1000Z_FGEN)
	obj.handle = 0
    obj.connected = 0
	return nothing
end
reset_instr(obj::DS1000Z_FGEN) = write(obj.handle, "*RST")
clear_instr(obj::DS1000Z_FGEN) = write(obj.handle, "*CLS")
get_idn(obj::DS1000Z_FGEN) = query(obj.handle, "*IDN?")

## WFM
function set_wfm(obj::DS1000Z_FGEN, ch, fct="sinusoid") # DONE
	# set_wfm(fgen, "C1", "sinusoid")
	ch = obj.instr_dict[ch]
	if fct == "triangle"
		cmdfct = ":SOURce$ch:FUNCtion:RAMP:SYMMetry 50"
		write(obj.handle, cmdfct)
		sleep(0.3)
	end
	fct = obj.wfm_dict[fct]
	cmd = ":SOURce$ch:FUNCtion:SHAPe $fct"
	write(obj.handle, cmd)

end
function get_wfm(obj::DS1000Z_FGEN, ch) # DONE
	# get_wfm(fgen, "C1")
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:FUNCtion:SHAPe?"
	value = strip(query(obj.handle, cmd))
	value = obj.rev_wfm_dict[value]
end

# UNIT
function set_amplit_unit(obj::DS1000Z_FGEN, ch, unit="Vpp")  # KIND OF DONE
	# set_amplit_unit(fgen, "C1", "Vpp")
	ch = obj.instr_dict[ch]
	#unit = obj.unit_dict[unit]
	#cmd = ":AMPLitude:UNIT $unit"
	#write(obj.handle, cmd)
	@info "not implemented yet, default to Vpp"
end
function get_amplit_unit(obj::DS1000Z_FGEN, ch)  # KIND OF DONE
	# get_amplit_unit(fgen, "C1")
	ch = obj.instr_dict[ch]
	#cmd = ":AMPLitude:UNIT?"
	#value = strip(query(obj.handle, cmd))
	#value = obj.unit_dict[value]
	#@info "not implemented yet, default to Vpp"
	return "Vpp"
end

# AMPLITUDE
function set_amplit(obj::DS1000Z_FGEN, ch, value) # DONE
	# set_amplit(fgen, "C1", 2.2)
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:VOLTage:LEVel:IMMediate:AMPLitude $value"
	write(obj.handle, cmd)
end
function get_amplit(obj::DS1000Z_FGEN, ch) # DONE
	# get_amplit(fgen, "C1")
	ch = obj.instr_dict[ch]
	cmd =  ":SOURce$ch:VOLTage:LEVel:IMMediate:AMPLitude?"
	value = parse(Float64, strip(query(obj.handle, cmd)))
end

# OFFSET
function set_offs(obj::DS1000Z_FGEN, ch, value) # DONE
	# set_offs(fgen, "C1", 1.1)
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:VOLTage:LEVel:IMMediate:OFFSet $value"
	write(obj.handle, cmd)
end
function get_offs(obj::DS1000Z_FGEN, ch) # DONE
	# get_offs(fgen, "C1")
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:VOLTage:LEVel:IMMediate:OFFSet?"
	value = parse(Float64, strip(query(obj.handle, cmd)))
end

# FREQUENCY
function set_freq(obj::DS1000Z_FGEN, ch, value) # DONE
	# set_freq(fgen, "C1", 1333)
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:FREQuency:FIXed $value"
	write(obj.handle, cmd)
end
function get_freq(obj::DS1000Z_FGEN, ch) # DONE
	# get_freq(fgen, "C1")
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:FREQuency:FIXed?"
	value = parse(Float64, strip(query(obj.handle, cmd)))
end

# DUTY CYCLE
function set_duty(obj::DS1000Z_FGEN, ch, value) # DONE
	# set_duty(fgen, "C1", 40)
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:PULSe:DCYCle $value"
	write(obj.handle, cmd)
end
function get_duty(obj::DS1000Z_FGEN, ch) # DONE
	# get_duty(fgen, "C1")
	ch = obj.instr_dict[ch]
	cmd = ":SOURce$ch:PULSe:DCYCle?"
	value = parse(Float64, strip(query(obj.handle, cmd)))
end

max_stars_repo_name: iuliancioarca/TIVM

max_issues_repo_name: iuliancioarca/TIVM

max_forks_repo_name: iuliancioarca/TIVM


text:
# "Allocate ODBC handles for interacting with the ODBC Driver Manager"
function ODBCAllocHandle(handletype, parenthandle)
    handle = Ref{Ptr{Void}}()
    ODBC_v06.API.SQLAllocHandle(handletype, parenthandle, handle)
    handle = handle[]
    if handletype == ODBC_v06.API.SQL_HANDLE_ENV
        ODBC_v06.API.SQLSetEnvAttr(handle, ODBC_v06.API.SQL_ATTR_ODBC_VERSION, ODBC_v06.API.SQL_OV_ODBC3)
    end
    return handle
end

# "Alternative connect function that allows user to create datasources on the fly through opening the ODBC admin"
function ODBCDriverConnect!(dbc::Ptr{Void}, conn_string, prompt::Bool)
    @static if is_windows()
        driver_prompt = prompt ? ODBC_v06.API.SQL_DRIVER_PROMPT : ODBC_v06.API.SQL_DRIVER_NOPROMPT
        window_handle = prompt ? ccall((:GetForegroundWindow, :user32), Ptr{Void}, () ) : C_NULL
    else
        driver_prompt = ODBC_v06.API.SQL_DRIVER_NOPROMPT
        window_handle = C_NULL
    end
    out_conn = Block(ODBC_v06.API.SQLWCHAR, BUFLEN)
    out_buff = Ref{Int16}()
    @CHECK dbc ODBC_v06.API.SQL_HANDLE_DBC ODBC_v06.API.SQLDriverConnect(dbc, window_handle, conn_string, out_conn.ptr, BUFLEN, out_buff, driver_prompt)
    connection_string = string(out_conn, out_buff[])
    return connection_string
end

"`ODBC_v06.prepare` prepares an SQL statement to be executed"
function prepare(dsn::DSN, query::AbstractString)
    stmt = ODBCAllocHandle(ODBC_v06.API.SQL_HANDLE_STMT, dsn.dbc_ptr)
    ODBC_v06.@CHECK stmt ODBC_v06.API.SQL_HANDLE_STMT ODBC_v06.API.SQLPrepare(stmt, query)
    return Statement(dsn, stmt, query, Task(1))
end

cast(x) = x
cast(x::Dates.Date) = ODBC_v06.API.SQLDate(x)
cast(x::Dates.DateTime) = ODBC_v06.API.SQLTimestamp(x)
cast(x::String) = WeakRefString(pointer(x), sizeof(x))

getpointer(::Type{T}, A, i) where {T} = unsafe_load(Ptr{Ptr{Void}}(pointer(A, i)))
getpointer(::Type{WeakRefString{T}}, A, i) where {T} = convert(Ptr{Void}, A[i].ptr)
getpointer(::Type{String}, A, i) = convert(Ptr{Void}, pointer(Vector{UInt8}(A[i])))

sqllength(x) = 1
sqllength(x::AbstractString) = length(x)
sqllength(x::Vector{UInt8}) = length(x)
sqllength(x::WeakRefString) = x.len
sqllength(x::ODBC_v06.API.SQLDate) = 10
sqllength(x::Union{ODBC_v06.API.SQLTime,ODBC_v06.API.SQLTimestamp}) = length(string(x))

clength(x) = 1
clength(x::AbstractString) = length(x)
clength(x::Vector{UInt8}) = length(x)
clength(x::WeakRefString{T}) where {T} = codeunits2bytes(T, x.len)
clength(x::CategoricalArrays.CategoricalValue) = length(String(x))
clength(x::Missing) = ODBC_v06.API.SQL_NULL_DATA

digits(x) = 0
digits(x::ODBC_v06.API.SQLTimestamp) = length(string(x.fraction * 1000000))

function execute!(statement::Statement, values)
    stmt = statement.stmt
    values2 = Any[cast(x) for x in values]
    pointers = Ptr[]
    types = map(typeof, values2)
    for (i, v) in enumerate(values2)
        if ismissing(v)
            ODBC_v06.@CHECK stmt ODBC_v06.API.SQL_HANDLE_STMT ODBC_v06.API.SQLBindParameter(stmt, i, ODBC_v06.API.SQL_PARAM_INPUT,
                ODBC_v06.API.SQL_C_CHAR, ODBC_v06.API.SQL_CHAR, 0, 0, C_NULL, 0, Ref(ODBC_v06.API.SQL_NULL_DATA))
        else
            ctype, sqltype = ODBC_v06.API.julia2C[types[i]], ODBC_v06.API.julia2SQL[types[i]]
            csize, len, dgts = sqllength(v), clength(v), digits(v)
            ptr = getpointer(types[i], values2, i)
            # println("ctype: $ctype, sqltype: $sqltype, digits: $dgts, len: $len, csize: $csize")
            push!(pointers, ptr)
            ODBC_v06.@CHECK stmt ODBC_v06.API.SQL_HANDLE_STMT ODBC_v06.API.SQLBindParameter(stmt, i, ODBC_v06.API.SQL_PARAM_INPUT,
                ctype, sqltype, csize, dgts, ptr, len, Ref(len))
        end
    end
    execute!(statement)
    return
end

function execute!(statement::Statement)
    stmt = statement.stmt
    ODBC_v06.@CHECK stmt ODBC_v06.API.SQL_HANDLE_STMT ODBC_v06.API.SQLExecute(stmt)
    return
end

"`ODBC_v06.execute!` is a minimal method for just executing an SQL `query` string. No results are checked for or returned."
function execute!(dsn::DSN, query::AbstractString, stmt=dsn.stmt_ptr)
    ODBC_v06.ODBCFreeStmt!(stmt)
    ODBC_v06.@CHECK stmt ODBC_v06.API.SQL_HANDLE_STMT ODBC_v06.API.SQLExecDirect(stmt, query)
    return
end

"""
`ODBC_v06.Source` constructs a valid `Data.Source` type that executes an SQL `query` string for the `dsn` ODBC DSN.
Results are checked for and an `ODBC_v06.ResultBlock` is allocated to prepare for fetching the resultset.
"""
function Source(dsn::DSN, query::AbstractString; weakrefstrings::Bool=true, noquery::Bool=false)
    stmt = dsn.stmt_ptr
    noquery || ODBC_v06.ODBCFreeStmt!(stmt)
    supportsreset = ODBC_v06.API.SQLSetStmtAttr(stmt, ODBC_v06.API.SQL_ATTR_CURSOR_SCROLLABLE, ODBC_v06.API.SQL_SCROLLABLE, ODBC_v06.API.SQL_IS_INTEGER)
    supportsreset &= ODBC_v06.API.SQLSetStmtAttr(stmt, ODBC_v06.API.SQL_ATTR_CURSOR_TYPE, ODBC_v06.API.SQL_CURSOR_STATIC, ODBC_v06.API.SQL_IS_INTEGER)
    noquery || (ODBC_v06.@CHECK stmt ODBC_v06.API.SQL_HANDLE_STMT ODBC_v06.API.SQLExecDirect(stmt, query))
    rows, cols = Ref{Int}(), Ref{Int16}()
    ODBC_v06.API.SQLNumResultCols(stmt, cols)
    ODBC_v06.API.SQLRowCount(stmt, rows)
    rows, cols = rows[], cols[]
    #Allocate arrays to hold each column's metadata
    cnames = Array{String}(cols)
    ctypes, csizes = Array{ODBC_v06.API.SQLSMALLINT}(cols), Array{ODBC_v06.API.SQLULEN}(cols)
    cdigits, cnulls = Array{ODBC_v06.API.SQLSMALLINT}(cols), Array{ODBC_v06.API.SQLSMALLINT}(cols)
    juliatypes = Array{Type}(cols)
    alloctypes = Array{DataType}(cols)
    longtexts = Array{Bool}(cols)
    longtext = false
    #Allocate space for and fetch the name, type, size, etc. for each column
    len, dt, csize = Ref{ODBC_v06.API.SQLSMALLINT}(), Ref{ODBC_v06.API.SQLSMALLINT}(), Ref{ODBC_v06.API.SQLULEN}()
    digits, missing = Ref{ODBC_v06.API.SQLSMALLINT}(), Ref{ODBC_v06.API.SQLSMALLINT}()
    cname = ODBC_v06.Block(ODBC_v06.API.SQLWCHAR, ODBC_v06.BUFLEN)
    for x = 1:cols
        ODBC_v06.API.SQLDescribeCol(stmt, x, cname.ptr, ODBC_v06.BUFLEN, len, dt, csize, digits, missing)
        cnames[x] = string(cname, len[])
        t = dt[]
        ctypes[x], csizes[x], cdigits[x], cnulls[x] = t, csize[], digits[], missing[]
        alloctypes[x], juliatypes[x], longtexts[x] = ODBC_v06.API.SQL2Julia[t]
        longtext |= longtexts[x]
    end
    if !weakrefstrings
        foreach(i->juliatypes[i] <: Union{WeakRefString, Missing} && setindex!(juliatypes, Union{String, Missing}, i), 1:length(juliatypes))
    end
    # Determine fetch strategy
    # rows might be -1 (dbms doesn't return total rows in resultset), 0 (empty resultset), or 1+
    if longtext
        rowset = allocsize = 1
    elseif rows > -1
        # rowset = min(rows, ODBC_v06.API.MAXFETCHSIZE)
        allocsize = rowset = rows
    else
        rowset = allocsize = 1
    end
    ODBC_v06.API.SQLSetStmtAttr(stmt, ODBC_v06.API.SQL_ATTR_ROW_ARRAY_SIZE, rowset, ODBC_v06.API.SQL_IS_UINTEGER)
    boundcols = Array{Any}(cols)
    indcols = Array{Vector{ODBC_v06.API.SQLLEN}}(cols)
    for x = 1:cols
        if longtexts[x]
            boundcols[x], indcols[x] = alloctypes[x][], ODBC_v06.API.SQLLEN[]
        else
            boundcols[x], elsize = internal_allocate(alloctypes[x], rowset, csizes[x])
            indcols[x] = Array{ODBC_v06.API.SQLLEN}(rowset)
            ODBC_v06.API.SQLBindCols(stmt, x, ODBC_v06.API.SQL2C[ctypes[x]], pointer(boundcols[x]), elsize, indcols[x])
        end
    end
    columns = ((allocate(T) for T in juliatypes)...,)
    schema = Data.Schema(juliatypes, cnames, rows >= 0 ? rows : Missings.missing,
        Dict("types"=>[ODBC_v06.API.SQL_TYPES[c] for c in ctypes], "sizes"=>csizes, "digits"=>cdigits, "nulls"=>cnulls))
    rowsfetched = Ref{ODBC_v06.API.SQLLEN}() # will be populated by call to SQLFetchScroll
    ODBC_v06.API.SQLSetStmtAttr(stmt, ODBC_v06.API.SQL_ATTR_ROWS_FETCHED_PTR, rowsfetched, ODBC_v06.API.SQL_NTS)
    types = [ODBC_v06.API.SQL2C[ctypes[x]] for x = 1:cols]
    source = ODBC_v06.Source(schema, dsn, query, columns, 100, rowsfetched, 0, boundcols, indcols, csizes, types, Type[longtexts[x] ? ODBC_v06.API.Long{T} : T for (x, T) in enumerate(juliatypes)], supportsreset == 1)
    rows != 0 && fetch!(source)
    return source
end

# primitive types
allocate(::Type{T}) where {T} = Vector{T}(0)
allocate(::Type{Union{Missing, WeakRefString{T}}}) where {T} = WeakRefStringArray(UInt8[], Union{Missing, WeakRefString{T}}, 0)

internal_allocate(::Type{T}, rowset, size) where {T} = Vector{T}(rowset), sizeof(T)
# string/binary types
internal_allocate(::Type{T}, rowset, size) where {T <: Union{UInt8, UInt16, UInt32}} = zeros(T, rowset * (size + 1)), sizeof(T) * (size + 1)

function fetch!(source)
    stmt = source.dsn.stmt_ptr
    source.status = ODBC_v06.API.SQLFetchScroll(stmt, ODBC_v06.API.SQL_FETCH_NEXT, 0)
    # source.rowsfetched[] == 0 && return
    # types = source.jltypes
    # for col = 1:length(types)
    #     ODBC_v06.cast!(types[col], source, col)
    # end
    return
end

# primitive types
function cast!(::Type{T}, source, col) where {T}
    len = source.rowsfetched[]
    c = source.columns[col]
    resize!(c, len)
    ind = source.indcols[col]
    data = source.boundcols[col]
    @simd for i = 1:len
        @inbounds c[i] = ifelse(ind[i] == ODBC_v06.API.SQL_NULL_DATA, missing, data[i])
    end
    return c
end

cast(::Type{Float64}, arr, cur, ind) = ind <= 0 ? DECZERO : parse(Float64, String(unsafe_wrap(Array, pointer(arr, cur), ind)))

function cast!(::Type{Union{Float64, Missing}}, source, col)
    len = source.rowsfetched[]
    c = source.columns[col]
    resize!(c, len)
    cur = 1
    elsize = source.sizes[col] + 1
    inds = source.indcols[col]
    @inbounds for i = 1:len
        ind = inds[i]
        c[i] = ind == ODBC_v06.API.SQL_NULL_DATA ? missing : cast(Float64, source.boundcols[col], cur, ind)
        cur += elsize
    end
    return c
end

cast(::Type{Vector{UInt8}}, arr, cur, ind) = arr[cur:(cur + max(ind, 0) - 1)]

function cast!(::Type{Union{Vector{UInt8}, Missing}}, source, col)
    len = source.rowsfetched[]
    c = source.columns[col]
    resize!(c, len)
    cur = 1
    elsize = source.sizes[col] + 1
    inds = source.indcols[col]
    @inbounds for i = 1:len
        ind = inds[i]
        c[i] = ind == ODBC_v06.API.SQL_NULL_DATA ? missing : cast(Vector{UInt8}, source.boundcols[col], cur, ind)
        cur += elsize
    end
    return c
end

# string types
bytes2codeunits(::Type{UInt8},  bytes) = ifelse(bytes == ODBC_v06.API.SQL_NULL_DATA, 0, Int(bytes))
bytes2codeunits(::Type{UInt16}, bytes) = ifelse(bytes == ODBC_v06.API.SQL_NULL_DATA, 0, Int(bytes >> 1))
bytes2codeunits(::Type{UInt32}, bytes) = ifelse(bytes == ODBC_v06.API.SQL_NULL_DATA, 0, Int(bytes >> 2))
codeunits2bytes(::Type{UInt8},  bytes) = ifelse(bytes == ODBC_v06.API.SQL_NULL_DATA, 0, Int(bytes))
codeunits2bytes(::Type{UInt16}, bytes) = ifelse(bytes == ODBC_v06.API.SQL_NULL_DATA, 0, Int(bytes * 2))
codeunits2bytes(::Type{UInt32}, bytes) = ifelse(bytes == ODBC_v06.API.SQL_NULL_DATA, 0, Int(bytes * 4))

function cast!(::Type{Union{String, Missing}}, source, col)
    len = source.rowsfetched[]
    c = source.columns[col]
    resize!(c, len)
    data = source.boundcols[col]
    T = eltype(data)
    cur = 1
    elsize = source.sizes[col] + 1
    inds = source.indcols[col]
    @inbounds for i in 1:len
        ind = inds[i]
        length = ODBC_v06.bytes2codeunits(T, max(ind, 0))
        c[i] = ind == ODBC_v06.API.SQL_NULL_DATA ? missing : (length == 0 ? "" : String(transcode(UInt8, data[cur:(cur + length - 1)])))
        cur += elsize
    end
    return c
end

function cast!(::Type{Union{WeakRefString{T}, Missing}}, source, col) where {T}
    len = source.rowsfetched[]
    c = source.columns[col]
    resize!(c, len)
    empty!(c.data)
    data = copy(source.boundcols[col])
    push!(c.data, data)
    cur = 1
    elsize = source.sizes[col] + 1
    inds = source.indcols[col]
    EMPTY = WeakRefString{T}(Ptr{T}(0), 0)
    @inbounds for i = 1:len
        ind = inds[i]
        length = ODBC_v06.bytes2codeunits(T, max(ind, 0))
        c[i] = ind == ODBC_v06.API.SQL_NULL_DATA ? missing : (length == 0 ? EMPTY : WeakRefString{T}(pointer(data, cur), length))
        cur += elsize
    end
    return c
end

# long types
const LONG_DATA_BUFFER_SIZE = 1024

function cast!(::Type{ODBC_v06.API.Long{Union{T, Missing}}}, source, col) where {T}
    stmt = source.dsn.stmt_ptr
    eT = eltype(source.boundcols[col])
    data = eT[]
    buf = zeros(eT, ODBC_v06.LONG_DATA_BUFFER_SIZE)
    ind = Ref{ODBC_v06.API.SQLLEN}()
    res = ODBC_v06.API.SQLGetData(stmt, col, source.ctypes[col], pointer(buf), sizeof(buf), ind)
    isnull = ind[] == ODBC_v06.API.SQL_NULL_DATA
    while !isnull
        len = ind[]
        oldlen = length(data)
        resize!(data, oldlen + bytes2codeunits(eT, len))
        ccall(:memcpy, Void, (Ptr{Void}, Ptr{Void}, Csize_t), pointer(data, oldlen + 1), pointer(buf), len)
        res = ODBC_v06.API.SQLGetData(stmt, col, source.ctypes[col], pointer(buf), length(buf), ind)
        res != ODBC_v06.API.SQL_SUCCESS && res != ODBC_v06.API.SQL_SUCCESS_WITH_INFO && break
    end
    c = source.columns[col]
    resize!(c, 1)
    c[1] = isnull ? missing : T(transcode(UInt8, data))
    return c
end

# DataStreams interface
Data.schema(source::ODBC_v06.Source) = source.schema
"Checks if an `ODBC_v06.Source` has finished fetching results from an executed query string"
Data.isdone(source::ODBC_v06.Source, x=1, y=1) = source.status != ODBC_v06.API.SQL_SUCCESS && source.status != ODBC_v06.API.SQL_SUCCESS_WITH_INFO
function Data.reset!(source::ODBC_v06.Source)
    source.supportsreset || throw(ArgumentError("Data.reset! not supported, probably due to the database vendor ODBC driver implementation"))
    stmt = source.dsn.stmt_ptr
    source.status = ODBC_v06.API.SQLFetchScroll(stmt, ODBC_v06.API.SQL_FETCH_FIRST, 0)
    source.rowoffset = 0
    return
end

Data.streamtype(::Type{ODBC_v06.Source}, ::Type{Data.Column}) = true
Data.streamtype(::Type{ODBC_v06.Source}, ::Type{Data.Field}) = true

function Data.streamfrom(source::ODBC_v06.Source, ::Type{Data.Field}, ::Type{Union{T, Missing}}, row, col) where {T}
    val = if isempty(source.columns[col])
        cast!(source.jltypes[col], source, col)[row - source.rowoffset]
    else
        source.columns[col][row - source.rowoffset]
    end

    if col == length(source.columns) && (row - source.rowoffset) == source.rowsfetched[] && !Data.isdone(source)
        ODBC_v06.fetch!(source)
        if source.rowsfetched[] > 0
            for i = 1:col
                cast!(source.jltypes[i], source, i)
            end
            source.rowoffset += source.rowsfetched[]
        end
    end
    return val
end

function Data.streamfrom(source::ODBC_v06.Source, ::Type{Data.Column}, ::Type{Union{T, Missing}}, row, col) where {T}
    dest = cast!(source.jltypes[col], source, col)
    if col == length(source.columns) && !Data.isdone(source)
        ODBC_v06.fetch!(source)
    end
    return dest
end

function query(dsn::DSN, sql::AbstractString, sink=DataFrame, args...; weakrefstrings::Bool=true, append::Bool=false, transforms::Dict=Dict{Int,Function}())
    sink = Data.stream!(Source(dsn, sql; weakrefstrings=weakrefstrings), sink, args...; append=append, transforms=transforms)
    return Data.close!(sink)
end

function query(dsn::DSN, sql::AbstractString, sink::T; weakrefstrings::Bool=true, append::Bool=false, transforms::Dict=Dict{Int,Function}()) where {T}
    sink = Data.stream!(Source(dsn, sql; weakrefstrings=weakrefstrings), sink; append=append, transforms=transforms)
    return Data.close!(sink)
end

query(source::ODBC_v06.Source, sink=DataFrame, args...; append::Bool=false, transforms::Dict=Dict{Int,Function}()) = (sink = Data.stream!(source, sink, args...; append=append, transforms=transforms); return Data.close!(sink))
query(source::ODBC_v06.Source, sink::T; append::Bool=false, transforms::Dict=Dict{Int,Function}()) where {T} = (sink = Data.stream!(source, sink; append=append, transforms=transforms); return Data.close!(sink))

"Convenience string macro for executing an SQL statement against a DSN."
macro sql_str(s,dsn)
    query(dsn,s)
end


text:
#=##############################################################################
# DESCRIPTION
    Lifting paneled body types definition. Implementations of
    AbstractLiftingBody.
# AUTHORSHIP
  * Author    : Eduardo J. Alvarez
  * Email     : Edo.AlvarezR@gmail.com
  * Created   : Sep 2018
  * License   : MIT License
=###############################################################################

################################################################################
# RIGID-WAKE BODY TYPE
################################################################################
"""
  `RigidWakeBody(grid::gt.GridTriangleSurface)`

Lifting paneled body that is solved using a vortex-ring panels, and a steady,
rigid wake. `grid` is the grid surface (paneled geometry).

  **Properties**
  * `nnodes::Int64`                     : Number of nodes
  * `ncells::Int64`                     : Number of cells
  * `fields::Array{String, 1}`          : Available fields (solutions)
  * `Oaxis::Array{T,2} where {T<:Real}` : Coordinate system of original grid
  * `O::Array{T,1} where {T<:Real}`     : Position of CS of original grid
  * `ncellsTE::Int64`                   : Number of cells along trailing edge
  * `nnodesTE::Int64`                   : Number of nodes along trailing edge

"""
struct RigidWakeBody <: AbstractLiftingBody

  # User inputs
  grid::gt.GridTriangleSurface              # Paneled geometry
  U::Array{Int64,1}                         # Indices of all panels along the
                                            # upper side of the trailing edge
  L::Array{Int64,1}                         # Indices of all panels along the
                                            # lower side of the trailing edge

  # Properties
  nnodes::Int64                             # Number of nodes
  ncells::Int64                             # Number of cells
  fields::Array{String, 1}                  # Available fields (solutions)
  Oaxis::Array{T1,2} where {T1<:RType}      # Coordinate system of original grid
  O::Array{T2,1} where {T2<:RType}          # Position of CS of original grid
  ncellsTE::Int64                           # Number of cells along TE
  nnodesTE::Int64                           # Number of nodes along TE

  # Internal variables
  _G::Array{T3,2} where {T3<:RType}         # Body Geometry solution matrix
                                            # (it doesn't include the wake)

  RigidWakeBody(  grid,
                    U, L,
                  nnodes=grid.nnodes, ncells=grid.ncells,
                    fields=Array{String,1}(),
                    Oaxis=Array(1.0I, 3, 3), O=zeros(3),
                    ncellsTE=size(U,1), nnodesTE=size(U,1)+1,
                  _G=_calc_G_lifting_vortexring(grid, U, L)
         ) = _checkTE(U,L) ? new( grid,
                    sort(U), sort(L),
                  nnodes, ncells,
                    fields,
                    Oaxis, O,
                    ncellsTE, nnodesTE,
                  _G
         ) : error("Got invalid trailing edge.")
end

"""
    `solve(self::RigidWakeBody, Vinfs::Array{Array{T1,1},1}, D::Array{Array{T2,1},1})`

Solve the lifting panel body canceling the normal component of Vinfs, where
`Vinfs[i]` is the velocity at the i-th panel, and `D[j]` is the direction of
the semi-infinite vortex at the j-th TE edge filament.
"""
function solve(self::RigidWakeBody, Vinfs::Array{Array{T1,1},1},
                          D::Array{Array{T2,1},1}) where {T1<:RType, T2<:RType}
  # ERROR CASES
  if size(Vinfs,1) != self.ncells
    error("Invalid Vinfs; expected size $(self.ncells), got $(size(Vinfs,1))")
  elseif size(D,1) != self.nnodesTE && self.nnodesTE-1 != 0
    error("Invalid D; expected size $(self.nnodesTE), got $(size(D,1))")
  end

  CPs = [get_controlpoint(self, i) for i in 1:self.ncells] # Control points
  normals = [get_normal(self, i) for i in 1:self.ncells]

  # Geometric matrix --- Body contribution
  G = deepcopy(self._G)

  # Geometric matrix --- Rigid wake contribution
  for (j, u_j) in enumerate(self.U) # Iterates over upper side of TE

    # Upper in-going vortex
    PanelSolver.Vsemiinfinitevortex(
                          get_TE(self, j+1; upper=true),    # Starting point
                          D[j+1],                           # Direction
                          -1.0,                             # Unitary strength
                          CPs,                              # Targets
                                        # Velocity of j-th horseshoe on every CP
                          view(G, :, u_j);
                          dot_with=normals                  # Normal of every CP
                        )

    # Upper out-going vortex
    PanelSolver.Vsemiinfinitevortex(
                          get_TE(self, j; upper=true),      # Starting point
                          D[j],                             # Direction
                          1.0,                              # Unitary strength
                          CPs,                              # Targets
                                        # Velocity of j-th horseshoe on every CP
                          view(G, :, u_j);
                          dot_with=normals                  # Normal of every CP
                        )
  end

  for (j, l_j) in enumerate(self.L) # Iterates over lower side of TE

    # Incoming vortex
    PanelSolver.Vsemiinfinitevortex(
                          get_TE(self, j; upper=false), # Starting point
                          D[j],                         # Direction
                          -1.0,                         # Unitary strength
                          CPs,                          # Targets
                                      # Velocity of j-th horseshoe on every CP
                          view(G, :, l_j);
                          dot_with=normals              # Normal of every CP
                        )

    # Outgoing vortex
    PanelSolver.Vsemiinfinitevortex(
                          get_TE(self, j+1; upper=false), # Starting point
                          D[j+1],                         # Direction
                          1.0,                          # Unitary strength
                          CPs,                          # Targets
                                        # Velocity of j-th horseshoe on every CP
                          view(G, :, l_j);
                          dot_with=normals              # Normal of every CP
                        )
  end

  lambda = [-dot(Vinfs[i], get_normal(self, i)) for i in 1:self.ncells]
  Gamma = G\lambda

  # println(lambda)
  # println(G)
  # println(Gamma)

  # Gammas of upper and lower TE cells
  Gup = [Gamma[i] for i in self.U]
  Glo = [Gamma[i] for i in self.L]

  # Gamma at every TE bound vortex
  GTE = Glo - Gup

  # Gammas of the semi-infinite vortices
  # NOTE: Here I assume that all TE cells are contiguous
  Gammawake = self.nnodesTE-1 != 0 ?
                vcat(GTE[1], [GTE[i]-GTE[i-1] for i in 2:self.nnodesTE-1], -GTE[end]) :
                []

  add_field(self, "D", D)
  add_field(self, "Vinf", Vinfs)
  add_field(self, "Gamma", Gamma)
  add_field(self, "Gammawake", Gammawake)
  _solvedflag(self, true)
end

##### INTERNAL FUNCTIONS  ######################################################
"""
Returns the velocity induced by the body on the targets `targets`. It adds the
velocity at the i-th target to out[i].
"""
function _Vind(self::RigidWakeBody, targets::Array{Array{T1,1},1},
                          out::Array{Array{T2,1},1}) where{T1<:RType, T2<:RType}

    # Sorts trailing edge indices
    _U = sort(self.U)
    _L = sort(self.L)

    cur_u = 1                 # Index of current upper trailing edge cell
    cur_l = 1                 # Index of current lower trailing edge cell

    allnodes = self.grid.orggrid.nodes

    # Iterate over each panel calculating body-induced velocity
    for j in 1:self.ncells

        panel = gt.get_cell(self.grid, j)
        nodes = [view(allnodes, :, ind) for ind in vcat(panel[end], panel[1:end-1])]
        Gamma = get_fieldval(self, "Gamma", j; _check=false)

        PanelSolver.Vvortexring(nodes, Gamma, targets, out;
                                # Checks for TE
                                closed_ring= !( (cur_u<=size(_U,1) && j==_U[cur_u]) ||
                                                (cur_l<=size(_L,1) && j==_L[cur_l]))
                                )

          if cur_u<=size(_U,1) && j==_U[cur_u]; cur_u+=1; end;
          if cur_l<=size(_L,1) && j==_L[cur_l]; cur_l+=1; end;
    end

    # Iterates over upper side of TE calculating semi-infinite wake induced velocity
    for (j, u_j) in enumerate(self.U)

      Gamma = get_fieldval(self, "Gamma", u_j; _check=false)
      D = get_fieldval(self, "D", j+1; _check=false)

      # Upper in-going vortex
      PanelSolver.Vsemiinfinitevortex(
                            get_TE(self, j+1; upper=true),    # Starting point
                            D,                                # Direction
                            -Gamma,                           # strength
                            targets,                          # Targets
                            out
                          )

      D = get_fieldval(self, "D", j; _check=false)

      # Upper out-going vortex
      PanelSolver.Vsemiinfinitevortex(
                            get_TE(self, j; upper=true),      # Starting point
                            D,                                # Direction
                            Gamma,                            # strength
                            targets,                          # Targets
                            out
                          )
    end

    # Iterates over lower side of TE calculating semi-infinite wake induced velocity
    for (j, l_j) in enumerate(self.L) # Iterates over lower side of TE

      Gamma = get_fieldval(self, "Gamma", l_j; _check=false)
      D = get_fieldval(self, "D", j; _check=false)

      # Incoming vortex
      PanelSolver.Vsemiinfinitevortex(
                            get_TE(self, j; upper=false),     # Starting point
                            D,                                # Direction
                            -Gamma,                           # strength
                            targets,                          # Targets
                            out
                          )

      D = get_fieldval(self, "D", j+1; _check=false)

      # Outgoing vortex
      PanelSolver.Vsemiinfinitevortex(
                            get_TE(self, j+1; upper=false),   # Starting point
                            D,                                # Direction
                            Gamma,                            # strength
                            targets,                          # Targets
                            out
                          )
    end
end

"""
Returns the velocity induced by the body's `i`-th panel on the targets
`targets` assuming unitary strength. It adds the velocity at the j-th target to
out[j].
"""
function _GVind(self::RigidWakeBody, i::Int, targets::Array{Array{T1,1},1},
                          out::Array{Array{T2,1},1}) where {T1, T2}

    panel = gt.get_cell(self.grid, i)
    nodes = [view(self.grid.orggrid.nodes, :, ind) for ind in vcat(panel[end], panel[1:end-1])]
    Gamma = 1.0

    # Check whether this is a trailing edge panel
    TE = Bool(length(searchsorted(self.U, i))) || Bool(length(searchsorted(self.L, i)))

    PanelSolver.Vvortexring(nodes, Gamma, targets, out; closed_ring = !TE)
    # TODO: Add velocity of semi-infinite wake
end

"""
Outputs a vtk file with the wake.
"""
function _savewake(self::RigidWakeBody, filename::String;
                    len::RType=1.0, upper::Bool=true, suffix="_wake",optargs...)

  if check_field(self, "D")==false
    error("Requested to save wake, but D field wasn't found.")
  end

  # Points along trailing edge
  TE = [get_TE(self, i; upper=upper) for i in 1:self.nnodesTE]

  # End point of wake
  wake = TE + len*get_field(self, "D")["field_data"]

  # Points and lines
  points = vcat(TE, wake)
  lines = [[i, i+self.nnodesTE] for i in 0:self.nnodesTE-1]

  # Point data
  point_data = []
  if check_solved(self)
    push!(point_data, Dict( "field_name"=> "Gamma",
                            "field_type"=> "scalar",
                            "field_data"=> vcat(
                                  get_field(self, "Gammawake")["field_data"],
                                  get_field(self, "Gammawake")["field_data"])))
  end

  # Generate VTK
  return gt.generateVTK(filename*suffix, points; lines=lines,
                                              point_data=point_data, optargs...)
end
##### END OF RIGID-WAKE BODY ###################################################


text:
using AMDB:
    collated_dataset, data_path,
    count_total_length, Proportions, aggregate_tail,
    dump_latex, individual_history
using IndirectArrays: IndirectArray
using StatsBase: fit, Histogram
using Plots; pgfplots()

data = collated_dataset("collated_subset")
results_dir = data_path("results")
mkpath(results_dir)
cd(results_dir)

# count all the different spell types by total time spent in each

c = count_total_length(data[:STARTEND], data[:AM])
p = Proportions(c)
pa = aggregate_tail(p, 10, "rest")
dump_latex("spell_categories.tex", aggregate_tail(p, 12, "rest"))

# histogram of spell counts
y = length.(data[:ix])
spell_counts = fit(Histogram, y, closed = :right, nbins = 100)
plot(spell_counts, xlim = (0,30), xlab = "spells by individual (unmerged)", legend = false)
savefig("spell_counts.tex")

# spell coverage
individual_coverage(startend) = sum(length.(startend))
y = collect(sum(length.(data[:STARTEND][r])) for r in data[:ix])
coverage_hist = fit(Histogram, y / (16*365), closed = :left, nbins = 100)
plot(coverage_hist, xlab = "spell coverage (unmerged)", xlim = (0,20), legend = false)
savefig("spell_coverage.tex")

# inspect individuals
cols = [:STARTEND, :AM, :BENR, :SUM_MA, :NACE, :RGS, :AVG_BMG]

H = individual_history(data, 2624, cols...)
dump_latex("ind1.tex", H)

H = individual_history(data, 9168197, cols...)
dump_latex("ind2.tex", H)

H = individual_history(data, 6831135, cols...)
dump_latex("ind3.tex", H)

H = individual_history(data, 1466249, cols...) # multiple wages
dump_latex("ind4.tex", H)

H = individual_history(data, 7827568, cols...) # top-coded emp num
dump_latex("ind5.tex", H)

# draw from this and save interesting ones
H = individual_history(data, rand(data[:PENR]), cols...)


text:
nlp = woods(n = 10)

global nbsolver = 2
for solver in ALL_solvers
    global nbsolver += 1
    println(nbsolver, "  ", solver)
    (x, f, gNorm, iter, optimal, tired, status) = solver(nlp, verbose = true)
    @printf(
        "%-15s  %8d  %9.2e  %7.1e  %5d  %5d  %6d  %s\n",
        nlp.meta.name,
        nlp.meta.nvar,
        f,
        gNorm,
        nlp.counters.neval_obj,
        nlp.counters.neval_grad,
        nlp.counters.neval_hprod,
        status
    )    #stats = run_solver(solver, model, verbose=false)
    #@test (all([stats...] .>= 0))
    @test optimal
    reset!(nlp)
end


text:
using CmdStan
using Test

ProjDir = dirname(@__FILE__)
cd(ProjDir) do

bernoulli = "
data {
  int<lower=0> N;
  int<lower=0,upper=1> y[N];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ beta(1,1);
    y ~ bernoulli(theta);
}
"

data = [
  Dict("N" => 10, "y" => [0, 1, 0, 1, 0, 0, 0, 0, 0, 1]),
  Dict("N" => 10, "y" => [0, 1, 0, 0, 0, 0, 1, 0, 0, 1]),
  Dict("N" => 10, "y" => [0, 0, 0, 0, 0, 0, 1, 0, 1, 1]),
  Dict("N" => 10, "y" => [0, 0, 0, 1, 0, 0, 0, 1, 0, 1])
]

m = Stanmodel(Optimize(), name="bernoulli", model=bernoulli);
m.command[1] = CmdStan.cmdline(m)

println()
show(IOContext(stdout, :compact => true), m)
println()
show(m)

@assert m.output.refresh == 100

s1 = Sample()

println()
show(IOContext(stdout, :compact => true), s1)
println()
show(s1)

n1 = CmdStan.Lbfgs()
n2 = CmdStan.Lbfgs(history_size=15)
@assert 3*n1.history_size == n2.history_size

b1 = CmdStan.Bfgs()
b2 = CmdStan.Bfgs(tol_obj=1//100000000)
@assert b1.tol_obj == b2.tol_obj
o1 = Optimize()
o2 = Optimize(CmdStan.Newton())
o3 = Optimize(CmdStan.Lbfgs(history_size=10))
o4 = Optimize(CmdStan.Bfgs(tol_obj=1e-9))
o5 = Optimize(method=CmdStan.Bfgs(tol_obj=1e-9), save_iterations=true)
@assert o5.method.tol_obj == o1.method.tol_obj/10

println()
show(IOContext(stdout, :compact => true), o5)
println()
show(o5)

d1 = Diagnose()
@assert d1.diagnostic.error == 1e-6
d2 = Diagnose(CmdStan.Gradient(error=1e-7))
@assert d2.diagnostic.error == 1e-7

println()
show(IOContext(stdout, :compact => true), d2)
println()
show(d2)

println()
m.command

cd(ProjDir)
isdir("tmp") &&
  rm("tmp", recursive=true);

end # cd


text:
global cellId = 0

function resetCellId!(id=0)
    global cellId
    cellId = id
end

"""CW cell type"""
struct Cell{R} <: AbstractCell
    d::Int
    boundary::Vector{Cell{R}}
    boundaryMap::Dict{UInt64,R}
    hash::UInt64
    function Cell{R}(d::Int, cells::Vector{Cell{R}}, boundaryMap::Dict{UInt64,R}) where {R}
        global cellId
        @assert length(cells) == length(boundaryMap) "Collections of cells and boundary coefficients must be of the same size"
        cellId += 1
        new(d, cells, boundaryMap, hash(cellId))
    end
end
function Cell(d::Int, cells::Vector{Cell{R}}, boundary::Vector{R}) where {R}
    boundaryMap = Dict{UInt64,R}( hash(c)=>b for (c,b) in zip(cells, boundary) )
    Cell{R}(d, cells, boundaryMap)
end
function Cell(d::Int, cells::Vector{Cell{R}}) where {R}
    o = one(R)
    boundaryMap = Dict{UInt64,R}()
    for (i,c) in enumerate(cells)
        @assert dim(c) == d-1 "Incorrect dimension of a boundary cell $c"
        boundaryMap[hash(c)] = isodd(i) ? o : -o
    end
    Cell{R}(d, cells, boundaryMap)
end
Cell(d::Int, cells::Cell{R}...) where {R} = Cell(d, [cells...])
Cell(::Type{R}, d::Int) where {R} = Cell(d, Cell{R}[])
Cell() = Cell(Int, 0)

# Private methods

function show(io::IO, c::Cell)
    vstr = ""
    print(io, "C$(c.d)")
    if length(c.boundary) > 0
        vstr = foldl((v,u)->v*"$(u){$(c.boundaryMap[hash(u)])}, ", c.boundary, init="")[1:end-2]
        print(io, "[$vstr]")
    end
end
eltype(c::Cell{R}) where {R} = R
eltype(::Type{Cell{R}}) where {R} = R

function push!(c1::Cell{R}, c2::Cell{R}, coeff::R) where {R}
    @assert dim(c1)-1 == dim(c2) "Incorrect dimension of a boundary cell $c2"
    h = hash(c2)
    c1.boundaryMap[h] = coeff
    push!(c1.boundary, c2)
end

function push!(c1::Cell{R}, c2::Cell{R}) where {R}
    v = isodd(length(c1.boundaryMap)) ? -one(R) : one(R)
    push!(c1, c2, v)
    c1
end

# Public methods

dim(c::Cell) = c.d
hash(c::Cell) = c.hash
==(a::Cell, b::Cell) = a.hash == b.hash

function vertices(c::Cell)
    vs = Set{Cell}()
    process = Set{Cell}(c.boundary)
    dim(c) == 0 && return collect(vs)
    while length(process) > 0
        pc = pop!(process)
        if dim(pc) == 0
            push!(vs, pc)
        else
            for v in pc.boundary
                push!(process, v)
            end
        end
    end
    return collect(vs)
end

function union(u::Cell, v::Cell)
    dim(u) != dim(v) && throw(DimensionMismatch("Cell dimensions must match for union"))
    return Cell(dim(v)+1, u, v)
end

faces(c::Cell) = c.boundary

boundary(c::Cell) = Chain(dim(c), copy(c.boundaryMap))


text:
include("./incremental.jl")
include("./kernel.jl")
include("./kernel_system.jl")

using Plots

function get_out_of_bounds(y, ℓ, u)
    for i in length(y)
        if y[i] > u[i] + eps() * 10^4
            return i, u[i]
        elseif y[i] < ℓ[i] - eps() * 10^4
            return i, ℓ[i]
        end
    end
    return 0
end

function eval!(y, kd, y1, y2, x)
    for i in size(x, 2)
        y[i] = predict_point(kd, y1, y2, x[:, i], ones(1, 1))
    end
    return nothing
end

function active_set(k, x, y, ℓ, u)
    n = size(x, 2)
    kd = kernel_data(n, k, x[:, 1:1], ones(1, 1), x[:, n:n], ones(1, 1))
    active = zeros(Bool, n)
    active[1] = active[n] = true
    yhat = Array{Float64}(undef, n)
    eval!(yhat[.!active], kd, y[1], y[2:end][active[2:end]], x[:, .!active])
    out_of_bounds = get_out_of_bounds(yhat[.!active], ℓ[.!active], u[.!active])

    while out_of_bounds[1] >= 0
        i = out_of_bounds[1]
        add_point!(kd, x[:, .!active][:, i], ones(1))
        y[.!active][i] = out_of_bounds[2]
        active[.!active][i] = true
        eval!(yhat[.!active], kd, y[1], y[2:end][active[2:end]], x[:, .!active])
        out_of_bounds = get_out_of_bounds(yhat, ℓ, u)
    end
    return active, yhat
end

k = FixedParam(RBF(), 0.05)
x = collect(-1:0.1:1)
x = reshape(x, 1, length(x))

f(t) = exp(-t ^ 2) * cospi(2t)
y = f.(x)
y = reshape(y, length(y))
plt = plot([x' y])
savefig(plt, "base.png")

ℓ = y .- 10
u = y .+ 10
active, yhat = active_set(k, x, y, ℓ, u)
plot!(plt, [x yhat])
scatter!(plt, [x[active] yhat[active]])
savefig(plt, "plot1.png")

ℓ = y .- 0.2
u = y .+ 0.2
active, yhat = active_set(k, x, y, ℓ, u)
plot!(plt, [x yhat])
scatter!(plt, [x[active] yhat[active]])
savefig(plt, "plot2.png")

ℓ = y .- 0.1
u = y .+ 0.1
active, yhat = active_set(k, x, y, ℓ, u)
plot!(plt, [x yhat])
scatter!(plt, [x[active] yhat[active]])
savefig(plt, "plot3.png")

max_stars_repo_name: dbindel/btg

max_issues_repo_name: dbindel/btg

max_forks_repo_name: dbindel/btg


text:
using DSGE, JLD, Base.Test

path = dirname(@__FILE__)

# Set up
m = AnSchorfheide(testing = true)

kal1, kal2 = jldopen("$path/../reference/kalman_cat_args.jld", "r") do file
    read(file, "kal1"), read(file, "kal2")
end

# Concatenate Kalmans
kal12 = cat(m, kal1, kal2)

# Test equality
exp_kal12 = jldopen("$path/../reference/kalman_cat_out.jld", "r") do file
    read(file, "kal12")
end

@testset "Testing Kalman output concatenation" begin
    for arg in fieldnames(kal1)
        @test exp_kal12[arg] ≈ kal12[arg]
    end
end

nothing