text:
@testset "4.1.4.2 (a+b sin)^m (c+d sin)^n (A+B sin+C sin^2)" begin
    (A, B, C, a, b, c, d, e, f, m, n, x, ) = @variables A B C a b c d e f m n x

    #= ::Package:: =#

    #= ::Title::Closed:: =#
    #=Integrands*of*the*form*(a+b*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)=#

    #= ::Section::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)=#

    #= ::Subsection:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^(n/2)*(A+C*sin(e+f*x)^2)=#

    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2)*(A + C*sin(e + f*x)^2), x, 5, If(13>=8, (64*c^3*(C*(39 - 16*m + 4*m^2) + A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(5 + 2*m)*(7 + 2*m)*(9 + 2*m)*(3 + 8*m + 4*m^2)*sqrt(c - c*sin(e + f*x))) + (16*c^2*(C*(39 - 16*m + 4*m^2) + A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(7 + 2*m)*(9 + 2*m)*(15 + 16*m + 4*m^2)) + (2*c*(C*(39 - 16*m + 4*m^2) + A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(5 + 2*m)*(7 + 2*m)*(9 + 2*m)) - (4*C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(f*(7 + 2*m)*(9 + 2*m)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(7/2))/(c*f*(9 + 2*m)), (64*c^3*(C*(39 - 16*m + 4*m^2) + A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(9 + 2*m)*(105 + 352*m + 344*m^2 + 128*m^3 + 16*m^4)*sqrt(c - c*sin(e + f*x))) + (16*c^2*(C*(39 - 16*m + 4*m^2) + A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(9 + 2*m)*(105 + 142*m + 60*m^2 + 8*m^3)) + (2*c*(C*(39 - 16*m + 4*m^2) + A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(9 + 2*m)*(35 + 24*m + 4*m^2)) - (4*C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(f*(63 + 32*m + 4*m^2)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(7/2))/(c*f*(9 + 2*m)))]
    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2)*(A + C*sin(e + f*x)^2), x, 4, If(13>=8, (8*c^2*(C*(19 - 8*m + 4*m^2) + A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(5 + 2*m)*(7 + 2*m)*(3 + 8*m + 4*m^2)*sqrt(c - c*sin(e + f*x))) + (2*c*(C*(19 - 8*m + 4*m^2) + A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(3 + 2*m)*(5 + 2*m)*(7 + 2*m)) - (4*C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(5 + 2*m)*(7 + 2*m)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(c*f*(7 + 2*m)), (8*c^2*(C*(19 - 8*m + 4*m^2) + A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(7 + 2*m)*(15 + 46*m + 36*m^2 + 8*m^3)*sqrt(c - c*sin(e + f*x))) + (2*c*(C*(19 - 8*m + 4*m^2) + A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(7 + 2*m)*(15 + 16*m + 4*m^2)) - (4*C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(35 + 24*m + 4*m^2)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(c*f*(7 + 2*m)))]
    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(1/2)*(A + C*sin(e + f*x)^2), x, 4, If(13>=8, (2*c*(C - 6*C*m + A*(5 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(1 + 2*m)*(5 + 2*m)*sqrt(c - c*sin(e + f*x))) + (4*c*C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(a*f*(3 + 2*m)*(5 + 2*m)*sqrt(c - c*sin(e + f*x))) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(c*f*(5 + 2*m)), (2*c*(C - 6*C*m + A*(5 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(5 + 12*m + 4*m^2)*sqrt(c - c*sin(e + f*x))) + (4*c*C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(a*f*(15 + 16*m + 4*m^2)*sqrt(c - c*sin(e + f*x))) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(c*f*(5 + 2*m)))]
    @test_int [(a + a*sin(e + f*x))^m/(c - c*sin(e + f*x))^(1/2)*(A + C*sin(e + f*x)^2), x, 4, ((A + C)*cos(e + f*x)*HypergeometricFunctions._₂F₁(1, 1/2 + m, 3/2 + m, (1/2)*(1 + sin(e + f*x)))*(a + a*sin(e + f*x))^m)/(f*(1 + 2*m)*sqrt(c - c*sin(e + f*x))) - (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(a*f*(3 + 2*m)*sqrt(c - c*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c - c*sin(e + f*x))^(3/2)*(A + C*sin(e + f*x)^2), x, 5, ((A + C)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(4*a*f*(c - c*sin(e + f*x))^(3/2)) + ((A + 2*A*m + C*(9 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(4*c*f*(1 + 2*m)*sqrt(c - c*sin(e + f*x))) + ((A*(1 - 2*m) - C*(7 + 2*m))*cos(e + f*x)*HypergeometricFunctions._₂F₁(1, 1/2 + m, 3/2 + m, (1/2)*(1 + sin(e + f*x)))*(a + a*sin(e + f*x))^m)/(4*c*f*(1 + 2*m)*sqrt(c - c*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c - c*sin(e + f*x))^(5/2)*(A + C*sin(e + f*x)^2), x, 5, ((A + C)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(8*a*f*(c - c*sin(e + f*x))^(5/2)) + ((A*(5 - 2*m) - C*(11 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(16*c*f*(c - c*sin(e + f*x))^(3/2)) + ((A*(3 - 8*m + 4*m^2) + C*(19 + 24*m + 4*m^2))*cos(e + f*x)*HypergeometricFunctions._₂F₁(1, 1/2 + m, 3/2 + m, (1/2)*(1 + sin(e + f*x)))*(a + a*sin(e + f*x))^m)/(32*c^2*f*(1 + 2*m)*sqrt(c - c*sin(e + f*x)))]

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^(m/2)*(c-c*sin(e+f*x))^(n/2)*(A+C*sin(e+f*x)^2)=#

    @test_int [(A + C*sin(e + f*x)^2)/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 8, ((A + C)*cos(e + f*x)*sqrt(a + a*sin(e + f*x)))/(4*a*f*(c - c*sin(e + f*x))^(3/2)) - ((A - 3*C)*cos(e + f*x)*log(1 - sin(e + f*x)))/(4*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + ((A + C)*cos(e + f*x)*log(1 + sin(e + f*x)))/(4*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)*when*m*symbolic=#

    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^n*(A + C*sin(e + f*x)^2), x, 6, (1/(f*(1 + 2*m)*(1 + m + n)*(2 + m + n)))*(2^(1/2 + n)*c*(C*(1 + 2*m)*(m - n) + (1 + m + n)*(C*(1 - m + n) + A*(2 + m + n)))*cos(e + f*x)*HypergeometricFunctions._₂F₁((1/2)*(1 + 2*m), (1/2)*(1 - 2*n), (1/2)*(3 + 2*m), (1/2)*(1 + sin(e + f*x)))*(1 - sin(e + f*x))^(1/2 - n)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(-1 + n)) - (C*(1 + 2*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^n)/(f*(1 + m + n)*(2 + m + n)) + (C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(1 + n))/(c*f*(2 + m + n))]

    #= ::Section::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)=#

    @test_int [(a + a*sin(e + f*x))^m*(A + C*sin(e + f*x)^2)*(c + d*sin(e + f*x))^n, x, 10, -((C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(1 + n))/(d*f*(2 + m + n))) + (sqrt(2)*(c*(C + 2*C*m) + d*(C*(1 - m + n) + A*(2 + m + n)))*AppellF1(1/2 + m, 1/2, -n, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^n)/(((c + d*sin(e + f*x))/(c - d))^n*(d*f*(1 + 2*m)*(2 + m + n)*sqrt(1 - sin(e + f*x)))) + (sqrt(2)*C*(d*m - c*(1 + m))*AppellF1(3/2 + m, 1/2, -n, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*(c + d*sin(e + f*x))^n)/(((c + d*sin(e + f*x))/(c - d))^n*(a*d*f*(3 + 2*m)*(2 + m + n)*sqrt(1 - sin(e + f*x))))]
    @test_int [(a + a*sin(e + f*x))^m*(A + C*sin(e + f*x)^2)/(c + d*sin(e + f*x))^(m + 2), x, 8, ((c^2*C + A*d^2)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(-1 - m))/(d*(c^2 - d^2)*f*(1 + m)) - (2^(1/2 + m)*a*(c*(A + C)*d*(1 + m) + d^2*(C - A*m + C*m) - c^2*(C + 2*C*m))*cos(e + f*x)*HypergeometricFunctions._₂F₁(1/2, 1/2 - m, 3/2, ((c - d)*(1 - sin(e + f*x)))/(2*(c + d*sin(e + f*x))))*(a + a*sin(e + f*x))^(-1 + m)*(((c + d)*(1 + sin(e + f*x)))/(c + d*sin(e + f*x)))^(1/2 - m))/((c + d*sin(e + f*x))^m*((c - d)*d*(c + d)^2*f*(1 + m))) + (sqrt(2)*C*AppellF1(3/2 + m, 1/2, 1 + m, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*((c + d*sin(e + f*x))/(c - d))^m)/((c + d*sin(e + f*x))^m*(a*(c - d)*d*f*(3 + 2*m)*sqrt(1 - sin(e + f*x))))]

    @test_int [(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(3/2)*(A + C*sin(e + f*x)^2), x, 10, -((2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(5/2))/(d*f*(7 + 2*m))) + (sqrt(2)*(c - d)*(2*c*(C + 2*C*m) + d*(C*(5 - 2*m) + A*(7 + 2*m)))*AppellF1(1/2 + m, 1/2, -(3/2), 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c + d*sin(e + f*x)))/(d*f*(1 + 2*m)*(7 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d))) + (2*sqrt(2)*C*(c - d)*(d*m - c*(1 + m))*AppellF1(3/2 + m, 1/2, -(3/2), 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt(c + d*sin(e + f*x)))/(a*d*f*(3 + 2*m)*(7 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d)))]
    @test_int [(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(1/2)*(A + C*sin(e + f*x)^2), x, 10, -((2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(3/2))/(d*f*(5 + 2*m))) + (sqrt(2)*(2*c*(C + 2*C*m) + d*(C*(3 - 2*m) + A*(5 + 2*m)))*AppellF1(1/2 + m, 1/2, -(1/2), 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c + d*sin(e + f*x)))/(d*f*(1 + 2*m)*(5 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d))) + (2*sqrt(2)*C*(d*m - c*(1 + m))*AppellF1(3/2 + m, 1/2, -(1/2), 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt(c + d*sin(e + f*x)))/(a*d*f*(3 + 2*m)*(5 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d)))]
    @test_int [(a + a*sin(e + f*x))^m/(c + d*sin(e + f*x))^(1/2)*(A + C*sin(e + f*x)^2), x, 10, -((2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c + d*sin(e + f*x)))/(d*f*(3 + 2*m))) + (sqrt(2)*(2*c*(C + 2*C*m) + d*(C - 2*C*m + A*(3 + 2*m)))*AppellF1(1/2 + m, 1/2, 1/2, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt((c + d*sin(e + f*x))/(c - d)))/(d*f*(1 + 2*m)*(3 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x))) - (2*sqrt(2)*C*(c + c*m - d*m)*AppellF1(3/2 + m, 1/2, 1/2, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt((c + d*sin(e + f*x))/(c - d)))/(a*d*f*(3 + 2*m)^2*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c + d*sin(e + f*x))^(3/2)*(A + C*sin(e + f*x)^2), x, 10, (2*(c^2*C + A*d^2)*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(d*(c^2 - d^2)*f*sqrt(c + d*sin(e + f*x))) + (sqrt(2)*(c*(A + C)*d - d^2*(A - C + 4*A*m) - 2*c^2*(C + 2*C*m))*AppellF1(1/2 + m, 1/2, 1/2, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt((c + d*sin(e + f*x))/(c - d)))/(d*(c^2 - d^2)*f*(1 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x))) + (sqrt(2)*(2*c^2*C*(1 + m) + d^2*(A - C + 2*A*m))*AppellF1(3/2 + m, 1/2, 1/2, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt((c + d*sin(e + f*x))/(c - d)))/(a*d*(c^2 - d^2)*f*(3 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c + d*sin(e + f*x))^(5/2)*(A + C*sin(e + f*x)^2), x, 10, (2*(c^2*C + A*d^2)*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(3*d*(c^2 - d^2)*f*(c + d*sin(e + f*x))^(3/2)) + (sqrt(2)*(3*c*(A + C)*d + d^2*(A + 3*C - 4*A*m) - 2*c^2*(C + 2*C*m))*AppellF1(1/2 + m, 1/2, 3/2, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt((c + d*sin(e + f*x))/(c - d)))/(3*(c - d)^2*d*(c + d)*f*(1 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x))) + (sqrt(2)*(2*c^2*C*(1 + m) - d^2*(A + 3*C - 2*A*m))*AppellF1(3/2 + m, 1/2, 3/2, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt((c + d*sin(e + f*x))/(c - d)))/(3*a*(c - d)^2*d*(c + d)*f*(3 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x)))]

    #= ::Section:: =#
    #=Integrands*of*the*form*(a+b*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+C*sin(e+f*x)^2)=#

    #= ::Title::Closed:: =#
    #=Integrands*of*the*form*(a+b*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Section::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Subsection:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Subsection:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^(n/2)*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^(m/2)*(c-c*sin(e+f*x))^(n/2)*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    @test_int [(A + B*sin(e + f*x) + C*sin(e + f*x)^2)/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 8, ((A + B + C)*cos(e + f*x)*sqrt(a + a*sin(e + f*x)))/(4*a*f*(c - c*sin(e + f*x))^(3/2)) - ((A - B - 3*C)*cos(e + f*x)*log(1 - sin(e + f*x)))/(4*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + ((A - B + C)*cos(e + f*x)*log(1 + sin(e + f*x)))/(4*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c-c*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)*with*m*and/or*n*symbolic=#

    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^n*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 6, (1/(f*(1 + 2*m)*(1 + m + n)*(2 + m + n)))*(2^(1/2 + n)*c*((1 + m + n)*(C*(1 - m + n) + A*(2 + m + n)) + (m - n)*(C + 2*C*m + B*(2 + m + n)))*cos(e + f*x)*HypergeometricFunctions._₂F₁((1/2)*(1 + 2*m), (1/2)*(1 - 2*n), (1/2)*(3 + 2*m), (1/2)*(1 + sin(e + f*x)))*(1 - sin(e + f*x))^(1/2 - n)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(-1 + n)) - ((C + 2*C*m + B*(2 + m + n))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^n)/(f*(1 + m + n)*(2 + m + n)) + (C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(1 + n))/(c*f*(2 + m + n))]

    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 5, If(13>=8, -((64*c^3*(B*(45 - 8*m - 4*m^2) - C*(39 - 16*m + 4*m^2) - A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(5 + 2*m)*(7 + 2*m)*(9 + 2*m)*(3 + 8*m + 4*m^2)*sqrt(c - c*sin(e + f*x)))) - (16*c^2*(B*(45 - 8*m - 4*m^2) - C*(39 - 16*m + 4*m^2) - A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(7 + 2*m)*(9 + 2*m)*(15 + 16*m + 4*m^2)) - (2*c*(B*(45 - 8*m - 4*m^2) - C*(39 - 16*m + 4*m^2) - A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(5 + 2*m)*(7 + 2*m)*(9 + 2*m)) - (2*(9*B + 2*C + 2*B*m + 4*C*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(f*(7 + 2*m)*(9 + 2*m)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(7/2))/(c*f*(9 + 2*m)), -((64*c^3*(B*(45 - 8*m - 4*m^2) - C*(39 - 16*m + 4*m^2) - A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(945 + 3378*m + 3800*m^2 + 1840*m^3 + 400*m^4 + 32*m^5)*sqrt(c - c*sin(e + f*x)))) - (16*c^2*(B*(45 - 8*m - 4*m^2) - C*(39 - 16*m + 4*m^2) - A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(945 + 1488*m + 824*m^2 + 192*m^3 + 16*m^4)) - (2*c*(B*(45 - 8*m - 4*m^2) - C*(39 - 16*m + 4*m^2) - A*(63 + 32*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(315 + 286*m + 84*m^2 + 8*m^3)) - (2*(9*B + 2*C + 2*B*m + 4*C*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(f*(63 + 32*m + 4*m^2)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(7/2))/(c*f*(9 + 2*m)))]
    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 4, If(13>=8, -((8*c^2*(B*(21 - 8*m - 4*m^2) - C*(19 - 8*m + 4*m^2) - A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(5 + 2*m)*(7 + 2*m)*(3 + 8*m + 4*m^2)*sqrt(c - c*sin(e + f*x)))) - (2*c*(B*(21 - 8*m - 4*m^2) - C*(19 - 8*m + 4*m^2) - A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(3 + 2*m)*(5 + 2*m)*(7 + 2*m)) - (2*(7*B + 2*C + 2*B*m + 4*C*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(5 + 2*m)*(7 + 2*m)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(c*f*(7 + 2*m)), -((8*c^2*(B*(21 - 8*m - 4*m^2) - C*(19 - 8*m + 4*m^2) - A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(7 + 2*m)*(15 + 46*m + 36*m^2 + 8*m^3)*sqrt(c - c*sin(e + f*x)))) - (2*c*(B*(21 - 8*m - 4*m^2) - C*(19 - 8*m + 4*m^2) - A*(35 + 24*m + 4*m^2))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c - c*sin(e + f*x)))/(f*(105 + 142*m + 60*m^2 + 8*m^3)) - (2*(7*B + 2*C + 2*B*m + 4*C*m)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(f*(35 + 24*m + 4*m^2)) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(5/2))/(c*f*(7 + 2*m)))]
    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(1/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 4, If(13>=8, (2*c*(C - 6*C*m + A*(5 + 2*m) - B*(5 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(1 + 2*m)*(5 + 2*m)*sqrt(c - c*sin(e + f*x))) + (2*c*(5*B + 2*C + 2*B*m + 4*C*m)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(a*f*(3 + 2*m)*(5 + 2*m)*sqrt(c - c*sin(e + f*x))) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(c*f*(5 + 2*m)), (2*c*(C - 6*C*m + A*(5 + 2*m) - B*(5 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(5 + 12*m + 4*m^2)*sqrt(c - c*sin(e + f*x))) + (2*c*(5*B + 2*C + 2*B*m + 4*C*m)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(a*f*(15 + 16*m + 4*m^2)*sqrt(c - c*sin(e + f*x))) + (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(3/2))/(c*f*(5 + 2*m)))]
    @test_int [(a + a*sin(e + f*x))^m/(c - c*sin(e + f*x))^(1/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 5, -((2*B*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(f*(1 + 2*m)*sqrt(c - c*sin(e + f*x)))) + ((A + B + C)*cos(e + f*x)*HypergeometricFunctions._₂F₁(1, 1/2 + m, 3/2 + m, (1/2)*(1 + sin(e + f*x)))*(a + a*sin(e + f*x))^m)/(f*(1 + 2*m)*sqrt(c - c*sin(e + f*x))) - (2*C*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(a*f*(3 + 2*m)*sqrt(c - c*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c - c*sin(e + f*x))^(3/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 5, ((A + B + C)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(4*a*f*(c - c*sin(e + f*x))^(3/2)) + ((A + B + 2*A*m + 2*B*m + C*(9 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(4*c*f*(1 + 2*m)*sqrt(c - c*sin(e + f*x))) + ((A*(1 - 2*m) - B*(3 + 2*m) - C*(7 + 2*m))*cos(e + f*x)*HypergeometricFunctions._₂F₁(1, 1/2 + m, 3/2 + m, (1/2)*(1 + sin(e + f*x)))*(a + a*sin(e + f*x))^m)/(4*c*f*(1 + 2*m)*sqrt(c - c*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c - c*sin(e + f*x))^(5/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 5, ((A + B + C)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m))/(8*a*f*(c - c*sin(e + f*x))^(5/2)) + ((A*(5 - 2*m) - B*(3 + 2*m) - C*(11 + 2*m))*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(16*c*f*(c - c*sin(e + f*x))^(3/2)) - ((B*(5 - 8*m - 4*m^2) - A*(3 - 8*m + 4*m^2) - C*(19 + 24*m + 4*m^2))*cos(e + f*x)*HypergeometricFunctions._₂F₁(1, 1/2 + m, 3/2 + m, (1/2)*(1 + sin(e + f*x)))*(a + a*sin(e + f*x))^m)/(32*c^2*f*(1 + 2*m)*sqrt(c - c*sin(e + f*x)))]

    @test_int [(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(-m - 2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 6, -((2^(-(1/2) - m)*C*cos(e + f*x)^3*HypergeometricFunctions._₂F₁((1/2)*(3 + 2*m), (1/2)*(3 + 2*m), (1/2)*(5 + 2*m), (1/2)*(1 + sin(e + f*x)))*(1 - sin(e + f*x))^(1/2 + m)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(-2 - m))/(f*(3 + 2*m))) + ((A + B + C)*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*(c - c*sin(e + f*x))^(-2 - m))/(2*a*f*(3 + 2*m)) + ((A - B + C)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c - c*sin(e + f*x))^(-1 - m))/(2*c*f*(1 + 2*m))]

    #= ::Section::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+a*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    @test_int [(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^n*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 10, -((C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(1 + n))/(d*f*(2 + m + n))) + (sqrt(2)*(c*(C + 2*C*m) + d*(C*(1 - m + n) + A*(2 + m + n) - B*(2 + m + n)))*AppellF1(1/2 + m, 1/2, -n, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^n)/(((c + d*sin(e + f*x))/(c - d))^n*(d*f*(1 + 2*m)*(2 + m + n)*sqrt(1 - sin(e + f*x)))) - (sqrt(2)*(c*C*(1 + m) - d*(C*m + B*(2 + m + n)))*AppellF1(3/2 + m, 1/2, -n, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*(c + d*sin(e + f*x))^n)/(((c + d*sin(e + f*x))/(c - d))^n*(a*d*f*(3 + 2*m)*(2 + m + n)*sqrt(1 - sin(e + f*x))))]

    @test_int [(a + a*sin(e + f*x))^m*(A + B*sin(e + f*x) + C*sin(e + f*x)^2)/(c + d*sin(e + f*x))^(m + 2), x, 8, ((c^2*C - B*c*d + A*d^2)*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(-1 - m))/(d*(c^2 - d^2)*f*(1 + m)) - (1/((c - d)*d*(c + d)^2*f*(1 + m)))*((2^(1/2 + m)*a*(c*d*(A + C + A*m + B*m + C*m) - c^2*(C + 2*C*m) - d^2*(A*m + B*(1 + m) - C*(1 + m)))*cos(e + f*x)*HypergeometricFunctions._₂F₁(1/2, 1/2 - m, 3/2, ((c - d)*(1 - sin(e + f*x)))/(2*(c + d*sin(e + f*x))))*(a + a*sin(e + f*x))^(-1 + m)*(((c + d)*(1 + sin(e + f*x)))/(c + d*sin(e + f*x)))^(1/2 - m))/(c + d*sin(e + f*x))^m) + (sqrt(2)*C*AppellF1(3/2 + m, 1/2, 1 + m, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*((c + d*sin(e + f*x))/(c - d))^m)/((c + d*sin(e + f*x))^m*(a*(c - d)*d*f*(3 + 2*m)*sqrt(1 - sin(e + f*x))))]

    @test_int [(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(3/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 10, -((2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(5/2))/(d*f*(7 + 2*m))) + (sqrt(2)*(c - d)*(2*c*(C + 2*C*m) - d*(7*B - 5*C + 2*B*m + 2*C*m - A*(7 + 2*m)))*AppellF1(1/2 + m, 1/2, -(3/2), 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c + d*sin(e + f*x)))/(d*f*(1 + 2*m)*(7 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d))) - (sqrt(2)*(c - d)*(2*c*C*(1 + m) - d*(2*C*m + B*(7 + 2*m)))*AppellF1(3/2 + m, 1/2, -(3/2), 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt(c + d*sin(e + f*x)))/(a*d*f*(3 + 2*m)*(7 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d)))]
    @test_int [(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(1/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 10, -((2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*(c + d*sin(e + f*x))^(3/2))/(d*f*(5 + 2*m))) + (sqrt(2)*(2*c*(C + 2*C*m) - d*(5*B - 3*C + 2*B*m + 2*C*m - A*(5 + 2*m)))*AppellF1(1/2 + m, 1/2, -(1/2), 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c + d*sin(e + f*x)))/(d*f*(1 + 2*m)*(5 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d))) - (sqrt(2)*(2*c*C*(1 + m) - d*(2*C*m + B*(5 + 2*m)))*AppellF1(3/2 + m, 1/2, -(1/2), 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt(c + d*sin(e + f*x)))/(a*d*f*(3 + 2*m)*(5 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt((c + d*sin(e + f*x))/(c - d)))]
    @test_int [(a + a*sin(e + f*x))^m/(c + d*sin(e + f*x))^(1/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 10, -((2*C*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt(c + d*sin(e + f*x)))/(d*f*(3 + 2*m))) + (sqrt(2)*(2*c*(C + 2*C*m) - d*(3*B - C + 2*B*m + 2*C*m - A*(3 + 2*m)))*AppellF1(1/2 + m, 1/2, 1/2, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt((c + d*sin(e + f*x))/(c - d)))/(d*f*(1 + 2*m)*(3 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x))) - (sqrt(2)*(2*c*C*(1 + m) - d*(2*C*m + B*(3 + 2*m)))*AppellF1(3/2 + m, 1/2, 1/2, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt((c + d*sin(e + f*x))/(c - d)))/(a*d*f*(3 + 2*m)^2*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c + d*sin(e + f*x))^(3/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 10, (2*(c^2*C - B*c*d + A*d^2)*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(d*(c^2 - d^2)*f*sqrt(c + d*sin(e + f*x))) - (sqrt(2)*(d^2*(A + B - C + 4*A*m) - c*d*(A + B + C + 4*B*m) + 2*c^2*(C + 2*C*m))*AppellF1(1/2 + m, 1/2, 1/2, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt((c + d*sin(e + f*x))/(c - d)))/(d*(c^2 - d^2)*f*(1 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x))) - (sqrt(2)*(d*(B*c - A*d)*(1 + 2*m) + C*(d^2 - 2*c^2*(1 + m)))*AppellF1(3/2 + m, 1/2, 1/2, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt((c + d*sin(e + f*x))/(c - d)))/(a*d*(c^2 - d^2)*f*(3 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x)))]
    @test_int [(a + a*sin(e + f*x))^m/(c + d*sin(e + f*x))^(5/2)*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 10, (2*(c^2*C - B*c*d + A*d^2)*cos(e + f*x)*(a + a*sin(e + f*x))^m)/(3*d*(c^2 - d^2)*f*(c + d*sin(e + f*x))^(3/2)) + (sqrt(2)*(d^2*(A - 3*B + 3*C - 4*A*m) + c*d*(3*A - B + 3*C + 4*B*m) - 2*c^2*(C + 2*C*m))*AppellF1(1/2 + m, 1/2, 3/2, 3/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^m*sqrt((c + d*sin(e + f*x))/(c - d)))/(3*(c - d)^2*d*(c + d)*f*(1 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x))) + (sqrt(2)*(B*c*d*(1 - 2*m) + 2*c^2*C*(1 + m) - d^2*(A + 3*C - 2*A*m))*AppellF1(3/2 + m, 1/2, 3/2, 5/2 + m, (1/2)*(1 + sin(e + f*x)), -((d*(1 + sin(e + f*x)))/(c - d)))*cos(e + f*x)*(a + a*sin(e + f*x))^(1 + m)*sqrt((c + d*sin(e + f*x))/(c - d)))/(3*a*(c - d)^2*d*(c + d)*f*(3 + 2*m)*sqrt(1 - sin(e + f*x))*sqrt(c + d*sin(e + f*x)))]

    #= ::Section::Closed:: =#
    #=Integrands*of*the*form*(a+b*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*sin(e+f*x)^m*(a+b*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    @test_int [(A + B*sin(c + d*x) + C*sin(c + d*x)^2)*(a + b*sin(c + d*x)), x, 2, (1/2)*(b*B + a*(2*A + C))*x - ((A*b + a*B + b*C)*cos(c + d*x))/d + (b*C*cos(c + d*x)^3)/(3*d) - ((b*B + a*C)*cos(c + d*x)*sin(c + d*x))/(2*d), (1/2)*(b*B + a*(2*A + C))*x - ((b^2*(3*A + 2*C) + a*(3*b*B - a*C))*cos(c + d*x))/(3*b*d) - ((3*b*B - a*C)*cos(c + d*x)*sin(c + d*x))/(6*d) - (C*cos(c + d*x)*(a + b*sin(c + d*x))^2)/(3*b*d)]

    #= ::Subsection:: =#
    #=Integrands*of*the*form*sin(e+f*x)^m*(a+b*sin(e+f*x))^(n/2)*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*sin(e+f*x)^(m/2)*(a+b*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)=#

    @test_int [((a + b*sin(e + f*x))*(A + B*sin(e + f*x) + C*sin(e + f*x)^2))/sin(e + f*x)^(3/2), x, 5, (2*(b*B - a*(A - C))*Elliptic.E((1/2)*(e - pi/2 + f*x), 2))/f + (2*(3*A*b + 3*a*B + b*C)*Elliptic.F((1/2)*(e - pi/2 + f*x), 2))/(3*f) - (2*a*A*cos(e + f*x))/(f*sqrt(sin(e + f*x))) - (2*b*C*cos(e + f*x)*sqrt(sin(e + f*x)))/(3*f)]

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*(a+b*sin(e+f*x))^m*(c+d*sin(e+f*x))^n*(A+B*sin(e+f*x)+C*sin(e+f*x)^2)*with*m*and/or*n*symbolic=#

    @test_int [(a + b*sin(e + f*x))^m*(c + d*sin(e + f*x))^n*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x, 0, Unintegrable((a + b*sin(e + f*x))^m*(c + d*sin(e + f*x))^n*(A + B*sin(e + f*x) + C*sin(e + f*x)^2), x)]

    #= ::Title::Closed:: =#
    #=Integrands*of*the*form*(g*sin(e+f*x))^p*(a+b*sin(e+f*x))^m*(c+d*sin(e+f*x))^n=#

    #= ::Subsection::Closed:: =#
    #=Integrands*of*the*form*sin(e+f*x)^m*(a+a*sin(e+f*x))^(n/2)*(c-c*sin(e+f*x))^(p/2)=#

    #= [sin(e + f*x)^4/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 4, (atanh(sin(e + f*x))*cos(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cos(e + f*x)*sin(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cos(e + f*x)*sin(e + f*x)^3)/(3*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [sin(e + f*x)^3/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 4, cos(e + f*x)^3/(2*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cos(e + f*x)*log(cos(e + f*x)))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [sin(e + f*x)^2/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 3, (atanh(sin(e + f*x))*cos(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cos(e + f*x)*sqrt(a + a*sin(e + f*x)))/(a*f*sqrt(c - c*sin(e + f*x))), (atanh(sin(e + f*x))*cos(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cos(e + f*x)*sin(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [sin(e + f*x)^1/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 2, -((cos(e + f*x)*log(cos(e + f*x)))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))))]
    @test_int [csc(e + f*x)^1/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 3, (cos(e + f*x)*log(tan(e + f*x)))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [csc(e + f*x)^2/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 3, (atanh(sin(e + f*x))*cos(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - cot(e + f*x)/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [csc(e + f*x)^3/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 4, -((cos(e + f*x)*cot(e + f*x)^2)/(2*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))) + (cos(e + f*x)*log(tan(e + f*x)))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [csc(e + f*x)^4/(sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), x, 4, (atanh(sin(e + f*x))*cos(e + f*x))/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - cot(e + f*x)/(f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cot(e + f*x)*csc(e + f*x)^2)/(3*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))] =#

    #= [sin(e + f*x)^4/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 8, -((3*atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))) - cos(e + f*x)^3/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (2*cos(e + f*x)*log(cos(e + f*x)))/(c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + sec(e + f*x)/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (3*cos(e + f*x)*sin(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)^2*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [sin(e + f*x)^3/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 7, -((3*atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))) + (cos(e + f*x)*log(cos(e + f*x)))/(c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (3*cos(e + f*x)*sin(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)^2*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [sin(e + f*x)^2/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 6, (cos(e + f*x)*sqrt(a + a*sin(e + f*x)))/(4*a*f*(c - c*sin(e + f*x))^(3/2)) + (3*cos(e + f*x)*log(1 - sin(e + f*x)))/(4*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (cos(e + f*x)*log(1 + sin(e + f*x)))/(4*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), -((atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))) + (cos(e + f*x)*log(cos(e + f*x)))/(c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + tan(e + f*x)/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [sin(e + f*x)^1/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 5, cos(e + f*x)/(2*f*sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)) - (atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))), -((atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))) + sec(e + f*x)/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + tan(e + f*x)/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [csc(e + f*x)^1/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 7, (atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (cos(e + f*x)*log(tan(e + f*x)))/(c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + tan(e + f*x)/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [csc(e + f*x)^2/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 8, (3*atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (3*cot(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (cos(e + f*x)*log(tan(e + f*x)))/(c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (csc(e + f*x)*sec(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))]
    @test_int [csc(e + f*x)^3/(sqrt(a + a*sin(e + f*x))*(c - c*sin(e + f*x))^(3/2)), x, 8, (3*atanh(sin(e + f*x))*cos(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (3*cot(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) - (cos(e + f*x)*cot(e + f*x)^2)/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (2*cos(e + f*x)*log(tan(e + f*x)))/(c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (csc(e + f*x)*sec(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x))) + (sin(e + f*x)*tan(e + f*x))/(2*c*f*sqrt(a + a*sin(e + f*x))*sqrt(c - c*sin(e + f*x)))] =#
    end

max_stars_repo_path: test/4 Trig functions/4.1 Sine/4.1.4.2 (a+b sin)^m (c+d sin)^n (A+B sin+C sin^2).jl


text:
module seisimu

    include("initiation/initiation.jl")
    include("extrap/run_base.jl")
    include("extrap/run_simulation.jl")
    include("IO/IO.jl")

    # GPU part is currently disabled
    # include("initiation/ForCUDA/gpuchunk.jl")

    # functions and types
    export init2DEL, init2DAC, init3DEL, init3DAC, reset!,
    Ricker,initMTsource,initSFsource,
    runsimu,
    acmod2d,elmod3d,acmod3d,elmod2d,
    addmt!,addsf!,run!, write_mor

end

ext: jl


text:
using Test
using Elefridge

@testset "CRPS tests" begin

    # Gaussian around true 0
    c = 0.2336949772551091
    @test isapprox(CRPS(randn(100_000),0.0),c,rtol=1e-1)

    # for true = 1,-1 the score needs to increase
    @test CRPS(randn(100_000),1) > c
    @test CRPS(randn(100_000),-1) > c
end


text:
#include("../setup.jl")          # need for testf
using Test, Random, CUDA, NNlib
using CUDA.CUDNN
using NNlib
using NNlib: ∇conv_data, ∇conv_filter,
    maxpool, meanpool, ∇maxpool, ∇meanpool,
    softmax, ∇softmax, logsoftmax, ∇logsoftmax

@test has_cudnn()
@test CUDNN.version() isa VersionNumber

@testset "NNlib" begin
    a, b, c = rand(Float64, 10, 10, 3, 1), rand(Float64, 2, 2, 3, 4), rand(Float64, 9, 9, 4, 1)
    da, db, dc = CuArray(a), CuArray(b), CuArray(c)
    cdims = DenseConvDims(a, b)
    @test NNlib.conv(a, b, cdims) ≈ collect(NNlib.conv(da, db, cdims))
    @test ∇conv_data(c, b, cdims) ≈ collect(∇conv_data(dc, db, cdims))
    @test ∇conv_filter(a, c, cdims) ≈ collect(∇conv_filter(da, dc, cdims))

    # Test for agreement between CPU NNlib and CuDNN versions, across a variety of kwargs
    for num_spatial_dims in (1, 2, 3)
        # Initialize data we'll run our tests over
        C_in = 3
        C_out = 4
        batch_size = 1
        x = rand(Float64, fill(8, num_spatial_dims)..., C_in, batch_size)
        w = rand(Float64, fill(2, num_spatial_dims)..., C_in, C_out)
        b = rand(Float64, fill(1, num_spatial_dims)..., C_in, C_out)
        options = (Dict(), Dict(:dilation => 2), Dict(:flipkernel => true), Dict(:stride => 2), Dict(:padding => 1))

        # @denizyuret: algo option deprecated for nnlib, handling in cudnn
        # algos = (1, 0, 1, 1,)
        # for (opts, algo) in zip(options, algos)

        for opts in options
            cdims = DenseConvDims(x, w; opts...)
            y = NNlib.conv(x, w, cdims)

            # Test that basic convolution is equivalent across GPU/CPU
            @test testf((x, w) -> NNlib.conv(x, w, cdims), x, w)
            @test testf((y, w) -> NNlib.∇conv_data(y, w, cdims), y, w)
            @test testf((x, y) -> NNlib.∇conv_filter(x, y, cdims), x, y)

            # Scaling factors
            @test testf((x, w) -> NNlib.conv(x, w, cdims; alpha=2.0), x, w)
            @test testf((y, w) -> NNlib.∇conv_data(y, w, cdims; alpha=2.0), y, w)
            @test testf((x, y) -> NNlib.∇conv_filter(x, y, cdims; alpha=2.0), x, y)

            @test testf((y, x, w) -> NNlib.conv!(copy(y), x, w, cdims; beta=2.0), y, x, w)
            # @test testf((x, y, w) -> NNlib.∇conv_data!(copy(x), y, w, cdims; beta=2.0), x, y, w)
            @test testf((w, x, y) -> NNlib.∇conv_filter!(copy(w), x, y, cdims; beta=2.0), w, x, y)

            # Test the compatibility shims
            cy,cx,cw = CuArray{Float32}.((y,x,w))
            opts2 = Dict((k==:padding ? :pad : k)=>v for (k,v) in opts)
            @test NNlib.conv!(similar(cy),cx,cw; opts2...) ≈ NNlib.conv!(similar(cy),cx,cw,cdims)
            @test NNlib.∇conv_filter!(similar(cw),cy,cx; opts2...) ≈ NNlib.∇conv_filter!(similar(cw),cx,cy,cdims)
        end

        # Test that pooling is equivalent across GPU/CPU
        pdims = PoolDims(x, 2)
        y = maxpool(x, pdims)
        dy = ones(size(y))
        @test testf(x -> maxpool(x, pdims), x)
        @test testf((dy, y, x) -> ∇maxpool(dy, y, x, pdims), dy, y, x)
        @test testf(x -> maxpool(x, pdims), x)
        @test testf((dy, y, x) -> ∇maxpool(dy, y, x, pdims), dy, y, x)

        # Test the compatibility shims for pooling
        cx,cy,cdy = CuArray{Float32}.((x,y,dy))
        win,pad=2,1
        @test maxpool!(similar(cy), cx, win; pad=pad, stride=win) ≈ maxpool!(similar(cy), cx, PoolDims(cx, win; padding=pad, stride=win))
        @test meanpool!(similar(cy), cx, win; pad=pad, stride=win) ≈ meanpool!(similar(cy), cx, PoolDims(cx, win; padding=pad, stride=win))

        # CPU implementation of ∇conv_bias!
        db = zeros(Float64, 1, 1, 3, 1)
        dy = randn(Float64, 8, 8, 3, 1)
        function CUDNN.∇conv_bias!(db, dy)
            db .= sum(dy, dims=(1:(ndims(dy)-2)))
            return db
        end
        @test testf(CUDNN.∇conv_bias!, db, dy)
    end

    for dims in [(5,5), (5,)]
        x = randn(Float64,dims)
        y = softmax(x)
        dy = randn(Float64,dims)
        @test testf(softmax, x)
        @test testf(∇softmax, dy, x) # add y when NNlib implements it
        y = logsoftmax(x)
        @test testf(logsoftmax, x)
        @test testf(∇logsoftmax, dy, x) # add y when NNlib implements it
    end
end

@testset "Activations and Other Ops" begin
    @test testf(CUDNN.cudnnAddTensor, CUDA.rand(Float32, 10, 10, 3, 1), CUDA.rand(Float32, 10, 10, 3, 1))
    @test testf(CUDNN.cudnnActivationForward!, CUDA.rand(Float32, 10, 10, 3, 1), CUDA.rand(Float32, 10, 10, 3, 1))
    # @denizyuret: no high level api for backward functions, see CUDA/lib/cudnn/README.md
    # @test testf(CUDNN.cudnnActivationBackward, CUDA.rand(Float32, 10, 10, 3, 1), CUDA.rand(Float32, 10, 10, 3, 1), CUDA.rand(Float32, 10, 10, 3, 1), CUDA.rand(Float32, 10, 10, 3, 1))

    # activations defined in src/nnlib.jl
    ACTIVATION_FUNCTIONS = [σ, logσ, hardσ, hardtanh, relu, leakyrelu, relu6, rrelu,
                            elu, gelu, celu, swish, lisht, selu, trelu, softplus,
                            softsign, logcosh, mish, tanhshrink, softshrink];
    for dims in ((5,5), (5,))
        for f in filter(x -> x != rrelu, ACTIVATION_FUNCTIONS)
            @test testf(x -> f.(x), rand(Float64, dims))
        end
    end

    # softplus does not give `Inf` for large arguments
    x = CuArray([1000.])
    @test all(softplus.(x) .== x)

    # optimized activation overwrote inputs
    let
        x = CUDA.ones(1)
        @test Array(x) == [1f0]
        tanh.(x)
        @test Array(x) == [1f0]
        y = tanh.(x)
        @test Array(x) == [1f0]
        @test Array(y) == [tanh(1f0)]
        x .= tanh.(y)
        @test Array(y) == [tanh(1f0)]
        @test Array(x) == [tanh(tanh(1f0))]
    end
end

@testset "Batchnorm" begin
    v = CUDA.rand(Float32, 2)
    m = CUDA.rand(Float32, 2, 5)
    for training in (false, true)
        CUDNN.batchnorm(v, v, m, v, v, 1.0; training=training)
    end
end


text:
@testset "Test Linear Basis" begin

    bas1 = BasisMatrices.Basis(BasisMatrices.Lin(), [0.0,4.0], 5)

    @testset "test type" begin
        bas2 = BasisMatrices.Basis(BasisMatrices.Lin(), [0.0,2.0,4.0], 5)
        @test  bas1.params[1].breaks  ==  [0.0,1.0,2.0,3.0,4.0]
        @test  bas2.params[1].evennum  ==  3
        @test_throws ErrorException BasisMatrices.Basis(BasisMatrices.Lin(), [0.0], 5)
        @test_throws ErrorException BasisMatrices.Basis(BasisMatrices.Lin(), [0.0,1.5,4.0], 5)
    end

    @testset "test constructor" begin

       valbas_lin = @inferred BasisMatrices.evalbase(bas1.params[1])
       valbas_spl = @inferred BasisMatrices.evalbase(BasisMatrices.SplineParams([0.0,1.0,2.0,3.0,4.0],0,1))

       @test  valbas_lin  ==  valbas_spl

    end

    @testset "test derivative" begin

    	#test derivative of basis indirectly, by fitting exponential function
		bas2 = Basis(LinParams([0,1.0],1000000))
		coeffs = funfitf(bas2,exp)
		points = rand(10000)

		d1 = funeval(coeffs,bas2,points,1)

        @test_approx_eq_eps d1 exp(points) 1e-7

    end

end


text:
# BAM Record
# ==========

type BAMRecord
    # fixed-length fields (see BMA specs for the details)
    block_size::Int32
    refid::Int32
    pos::Int32
    bin_mq_nl::UInt32
    flag_nc::UInt32
    l_seq::Int32
    next_refid::Int32
    next_pos::Int32
    tlen::Int32

    # variable length data
    data::Vector{UInt8}

    # pointer to reference sequence names (shared)
    refseqnames::Vector{String}
end

# the data size of fixed-length fields (.block_size-.tlen)
const BAM_FIXED_FIELDS_BYTES = 36

function BAMRecord()
    flag_nc = UInt32(SAM_FLAG_UNMAP) << 16
    return BAMRecord(0, -1, -1, 0, flag_nc, 0, -1, -1, 0, UInt8[], String[])
end

# NOTE: this does not copy `refseqnames`.
function Base.copy(rec::BAMRecord)
    return BAMRecord(
        rec.block_size,
        rec.refid,
        rec.pos,
        rec.bin_mq_nl,
        rec.flag_nc,
        rec.l_seq,
        rec.next_refid,
        rec.next_pos,
        rec.tlen,
        copy(rec.data),
        rec.refseqnames)
end

function Base.isless(rec1::BAMRecord, rec2::BAMRecord)
    # compared by left-most position of an alignment
    if rec1.refid == rec2.refid
        return isless(rec1.pos, rec2.pos)
    else
        return isless(rec1.refid, rec2.refid)
    end
end

function Base.:(==)(rec1::BAMRecord, rec2::BAMRecord)
    return (
        rec1.refid      == rec2.refid      &&
        rec1.pos        == rec2.pos        &&
        rec1.bin_mq_nl  == rec2.bin_mq_nl  &&
        rec1.flag_nc    == rec2.flag_nc    &&
        rec1.l_seq      == rec2.l_seq      &&
        rec1.next_refid == rec2.next_refid &&
        rec1.next_pos   == rec2.next_pos   &&
        rec1.tlen       == rec2.tlen)  # TODO: check data
end

"""
    ismapped(rec::BAMRecord)

Return `true` if and only if `rec` is mapped to a reference sequence.
"""
function ismapped(rec::BAMRecord)
    return flag(rec) & SAM_FLAG_UNMAP == 0
end

"""
    refindex(rec::BAMRecord)

Return the index of a reference sequence that `rec` is mapped onto.

The index is 1-based and will be 0 for an alignment without mapping position.
"""
function refindex(rec::BAMRecord)
    return rec.refid + 1
end

function nextrefindex(rec::BAMRecord)
    return rec.next_refid + 1
end

"""
    refname(rec::BAMRecord)

Return the name of a reference sequence that `rec` is mapped onto.

If `rec` is unmapped, it returns `"*"` like SAM records.
"""
function refname(rec::BAMRecord)
    i = refindex(rec)
    if i == 0
        return "*"
    else
        return rec.refseqnames[i]
    end
end

function nextrefname(rec::BAMRecord)
    i = nextrefindex(rec)
    if i == 0
        return "*"
    else
        return rec.refseqnames[i]
    end
end

"""
    leftposition(rec::BAMRecord)

Return the leftmost mapping position of `rec`.

The index is 1-based and will be 0 for an alignment without mapping position.
"""
function Bio.Intervals.leftposition(rec::BAMRecord)
    return rec.pos + 1
end

"""
    rightposition(rec::BAMRecord)

Return the rightmost mapping position of `rec`.
"""
function Bio.Intervals.rightposition(rec::BAMRecord)
    return Int32(leftposition(rec) + alignment_length(rec) - 1)
end

function nextleftposition(rec::BAMRecord)
    return rec.next_pos + 1
end

"""
    mappingquality(rec::BAMRecord)

Return the mapping quality of the alignment `rec`.
"""
function mappingquality(rec::BAMRecord)
    return UInt8((rec.bin_mq_nl >> 8) & 0xff)
end

"""
    flag(rec::BAMRecord)

Return the flag of the alignment `rec`.
"""
function flag(rec::BAMRecord)
    return UInt16(rec.flag_nc >> 16)
end

"""
    templatelength(rec::BAMRecord)

Return the template length of the alignment `rec`.
"""
function templatelength(rec::BAMRecord)
    return rec.tlen
end

function Bio.Seq.seqname(rec::BAMRecord)
    # drop the last NUL character
    return unsafe_string(pointer(rec.data), max(seqname_length(rec) - 1, 0))
end

"""
    cigar_rle(rec::BAMRecord)

Return a run-length encoded tuple `(ops, lens)` of the CIGAR string.
See also `cigar`.
"""
function cigar_rle(rec::BAMRecord)
    offset = seqname_length(rec)
    ops = Bio.Align.Operation[]
    lens = Int[]
    for i in offset+1:4:offset+n_cigar_op(rec)*4
        x = unsafe_load(Ptr{UInt32}(pointer(rec.data, i)))
        op = Bio.Align.Operation(x & 0x0f)
        push!(ops, op)
        push!(lens, x >> 4)
    end
    return ops, lens
end

"""
    cigar(rec::BAMRecord)

Return a CIGAR string of the alignment `rec`. See also `cigar_rle`.
"""
function cigar(rec::BAMRecord)
    buf = IOBuffer()
    for (op, len) in zip(cigar_rle(rec)...)
        print(buf, len, Char(op))
    end
    return takebuf_string(buf)
end

"""
    alignment(rec::BAMRecord)

Make an alignment object from `rec`.
"""
function alignment(rec::BAMRecord)
    if !ismapped(rec)
        return Alignment(AlignmentAnchor[])
    end
    seqpos = 0
    refpos = leftposition(rec) - 1
    anchors = [AlignmentAnchor(seqpos, refpos, OP_START)]
    for (op, len) in zip(cigar_rle(rec)...)
        if ismatchop(op)
            seqpos += len
            refpos += len
        elseif isinsertop(op)
            seqpos += len
        elseif isdeleteop(op)
            refpos += len
        else
            error("operation $(op) is not supported")
        end
        push!(anchors, AlignmentAnchor(seqpos, refpos, op))
    end
    return Alignment(anchors)
end

function Bio.Seq.sequence(rec::BAMRecord)
    seqlen = seqlength(rec)
    data = Vector{UInt64}(cld(seqlen, 16))
    src::Ptr{UInt64} = pointer(rec.data, seqname_length(rec) + n_cigar_op(rec) * 4 + 1)
    for i in 1:endof(data)
        # copy data flipping high and low nybble
        x = unsafe_load(src, i)
        data[i] = (x & 0x0f0f0f0f0f0f0f0f) << 4 | (x & 0xf0f0f0f0f0f0f0f0) >> 4
    end
    return Bio.Seq.DNASequence(data, 1:seqlen, false)
end

"""
    seqlength(rec::BAMRecord)

Return the length of the DNA sequence.
"""
function seqlength(rec)
    return rec.l_seq
end

"""
    qualities(rec::BAMRecord)

Return base qualities of the alignment `rec`.
"""
function qualities(rec::BAMRecord)
    seqlen = seqlength(rec)
    offset = seqname_length(rec) + n_cigar_op(rec) * 4 + cld(seqlen, 2)
    return [reinterpret(Int8, rec.data[i+offset]) for i in 1:seqlen]
end

function Base.getindex(rec::BAMRecord, tag::AbstractString)
    checkkeytag(tag)
    return getvalue(rec.data, auxdata_position(rec), UInt8(tag[1]), UInt8(tag[2]))
end

function Base.setindex!(rec::BAMRecord, val, tag::AbstractString)
    checkkeytag(tag)
    setvalue!(rec.data, auxdata_position(rec), val, UInt8(tag[1]), UInt8(tag[2]))
    return rec
end

function Base.delete!(rec::BAMRecord, tag::AbstractString)
    checkkeytag(tag)
    deletevalue!(rec.data, auxdata_position(rec), UInt8(tag[1]), UInt8(tag[2]))
    return rec
end

function Base.haskey(rec::BAMRecord, tag::AbstractString)
    checkkeytag(tag)
    return findtag(rec.data, auxdata_position(rec), UInt8(tag[1]), UInt8(tag[2])) > 0
end

function optional_fields(rec::BAMRecord)
    return AuxDataDict(rec.data[auxdata_position(rec):data_size(rec)])
end

function auxdata_position(rec)
    seqlen = seqlength(rec)
    return seqname_length(rec) + n_cigar_op(rec) * 4 + cld(seqlen, 2) + seqlen + 1
end

# Return the size of the `.data` field.
function data_size(rec::BAMRecord)
    return rec.block_size - BAM_FIXED_FIELDS_BYTES + sizeof(rec.block_size)
end

# Return the length of alignment.
function alignment_length(rec::BAMRecord)
    offset = seqname_length(rec)
    length::Int = 0
    for i in offset+1:4:offset+n_cigar_op(rec)*4
        x = unsafe_load(Ptr{UInt32}(pointer(rec.data, i)))
        op = Operation(x & 0x0f)
        if ismatchop(op) || isdeleteop(op)
            length += x >> 4
        end
    end
    return length
end

# Return the length of the read name.
function seqname_length(rec)
    return rec.bin_mq_nl & 0xff
end

# Return the number of CIGAR operations.
function n_cigar_op(rec)
    return rec.flag_nc & 0xffff
end


text:
# using PyPlot
#=
module MLVisualization

export
    visualize,
    display_sim,
    write_tmp_gif
=#
using Reel
using AutomotiveDrivingModels
using AutoViz
using Multilane
using Interact
using POMDPToolbox
using Cairo

BEHAVIOR_COLORS = Dict{Float64,AbstractString}(0.5=>"#0101DF",0.25=>"#D7DF01",0.0=>"#FF0000")

function display_sim(mdp, S::Array{MLState,1}, A::Array{MLAction,1})
    warn("This should be run in a Jupyter Notebook")
    assert(length(S) == length(A)+1)
    @manipulate for i = 1:length(A)
        visualize(mdp,S[i],A[i],S[i+1])
    end
end

show_state(mdp::Union{MLMDP, MLPOMDP}, s::MLState) = show_state(mdp.dmodel.phys_param, s)

function show_state(pp::PhysicalParam, s::MLState)
    c = visualize(pp, s)
    tn = string(tempname(), ".png")
    write_to_png(c, tn)
    run(`xdg-open $tn`)
end

function show_sim(mdp, sim::HistoryRecorder)
    f = write_tmp_gif(mdp, sim)
    run(`gifview $f`)
end

function display_sim(mdp, sim::HistoryRecorder)
    display_sim(mdp, sim.state_hist, sim.action_hist)
end

function save_frame(mdp, sim, k=1, filename=string(tempname(),".png"))
    c = visualize(mdp, sim.state_hist[k], sim.action_hist[k], sim.state_hist[k+1])
    write_to_png(c, filename)
    println("Saved to $filename")
end

function write_tmp_gif(mdp, sim::HistoryRecorder)
    dt = mdp.dmodel.phys_param.dt
    S = sim.state_hist
    A = sim.action_hist
    length(sim.action_hist)
    film = roll(fps = 1/dt, duration = dt*(length(sim.state_hist))) do t, dt
        i = round(Int, t/dt)+1
        if i == length(sim.state_hist)
            visualize(mdp, S[i])
        else
            visualize(mdp, S[i], A[i], S[i+1], idx=Nullable(i))
        end
    end
    filename = string(tempname(), ".gif")
    write(filename, film)
    return filename
end

function visualize(mdp::Union{MLMDP,MLPOMDP}, s::MLState, a::MLAction, sp::MLState;
                   idx::Nullable{Int}=Nullable{Int}())
    pp = mdp.dmodel.phys_param
    roadway = gen_straight_roadway(pp.nb_lanes,
                                   pp.lane_length,
                                   lane_width=pp.w_lane)

    hbol = HardBrakeOverlay(pp, braking_ids(mdp, s, sp))
    iol = InfoOverlay(pp, idx,
                      s.cars[1].vel,
                      max_braking(mdp, s, sp),
                      is_crash(mdp, s, sp))
    cidol = CarIDOverlay()
    cvol = CarVelOverlay()

    scene = Scene()
    for cs in s.cars
        push!(scene, Vehicle(VehicleState(VecSE2(cs.x, (cs.y-1.0)*pp.w_lane, 0.0), roadway, cs.vel),
                                #VehicleDef(cs.id, AgentClass.CAR, pp.l_car, pp.w_car)))
                                VehicleDef(AgentClass.CAR, pp.l_car, pp.w_car),cs.id))
    end

    #render(scene, roadway, [hbol, iol, cidol, cvol], cam=FitToContentCamera())

    render(scene, roadway, cam=FitToContentCamera())
    print("Visualize Check")

#    return scene
end

function visualize_cz(mdp::Union{MLMDP,MLPOMDP}, s::MLState, a::MLAction, sp::MLState;
                   idx::Nullable{Int}=Nullable{Int}())
    pp = mdp.dmodel.phys_param
    roadway = gen_straight_roadway(pp.nb_lanes,
                                   pp.lane_length,
                                   lane_width=pp.w_lane)

    hbol = HardBrakeOverlay(pp, braking_ids(mdp, s, sp))
    iol = InfoOverlay(pp, idx,
                      s.cars[1].vel,
                      max_braking(mdp, s, sp),
                      is_crash(mdp, s, sp))
    cidol = CarIDOverlay()
    cvol = CarVelOverlay()

    scene = Scene()
    #for cs in s.cars
    #    push!(scene, Vehicle(VehicleState(VecSE2(cs.x, (cs.y-1.0)*pp.w_lane, 0.0), roadway, cs.vel),
                                #VehicleDef(cs.id, AgentClass.CAR, pp.l_car, pp.w_car)))
    #                            VehicleDef(AgentClass.CAR, pp.l_car, pp.w_car),cs.id))
    #end

    #render(scene, roadway, [hbol, iol, cidol, cvol], cam=FitToContentCamera())

    #render(scene, roadway, cam=FitToContentCamera())
    #return scene
    print("CZ Visualize Check")
    return scene
end

visualize(mdp::Union{MLMDP,MLPOMDP}, s::MLState) = visualize(mdp.dmodel.phys_param, s)

function visualize(pp::PhysicalParam, s::MLState)
    roadway = gen_straight_roadway(pp.nb_lanes,
                                   pp.lane_length,
                                   lane_width=pp.w_lane)
    scene = Scene()
    for cs in s.cars
        push!(scene, Vehicle(VehicleState(VecSE2(cs.x, (cs.y-1.0)*pp.w_lane, 0.0), roadway, cs.vel),
                                VehicleDef(cs.id, AgentClass.CAR, pp.l_car, pp.w_car)))
    end
    render(scene, roadway, [CarIDOverlay(), CarVelOverlay()], cam=FitToContentCamera())
end

type HardBrakeOverlay <: SceneOverlay
    pp::PhysicalParam
    ids::Vector{Int}
end

function AutoViz.render!(rm::RenderModel, o::HardBrakeOverlay, scene::Scene, roadway::Roadway)
    for veh in scene
        if veh.def.id in o.ids
            for (size, offset) in [(1.2,0.4), (1.0,0.8), (0.8,1.2)]
                top = VecE2(veh.state.posG) + VecE2(-((1.+offset)*o.pp.l_car/2), size*o.pp.w_car/2)
                bottom = VecE2(veh.state.posG) + VecE2(-((1.+offset)*o.pp.l_car/2), -size*o.pp.w_car/2)
                add_instruction!(rm, render_line_segment, (top.x, top.y, bottom.x, bottom.y, colorant"red", 0.3))
            end
        end
    end
    # if !isempty(o.ids)
    #     add_instruction!(rendermodel, render_text, ("Hard Brake(s)!", 0, 15, 12, colorant"red"))
    # end
end

type InfoOverlay <: SceneOverlay
    pp::PhysicalParam
    state_index::Nullable{Int}
    vel::Float64
    max_braking::Float64
    iscrash::Bool
end

function AutoViz.render!(rm::RenderModel, o::InfoOverlay, scene::Scene, roadway::Roadway)
    line_delta = 1.6
    top_line_y = -o.pp.w_lane/2.0 - line_delta
    y = top_line_y
    if !isnull(o.state_index)
        add_instruction!(rm, render_text,
                         ("state index: $(get(o.state_index))", 0, y, 12, colorant"white"))
        y -= line_delta
    end
    add_instruction!(rm, render_text,
                     (@sprintf("velocity: %5.2f m/s", o.vel), 0, y, 12, colorant"white"))
    y -= line_delta
    add_instruction!(rm, render_text,
                     (@sprintf("max braking: %5.2f m/s", o.max_braking), 0, y, 12,
                      o.max_braking==o.pp.brake_limit ? colorant"red" : colorant"white"))
    y -= line_delta
    if o.iscrash
        add_instruction!(rm, render_text,
                         ("crash!", 0, y, 12, colorant"red"))
        y -= line_delta
    end
end

type CarIDOverlay <: SceneOverlay end

function AutoViz.render!(rm::RenderModel, o::CarIDOverlay, scene::Scene, roadway::Roadway)
    for (i,v) in enumerate(scene)
        cx = v.state.posG.x
        cy = v.state.posG.y
        nx = cx - v.def.length/2
        ny = cy + v.def.width/2 - 0.6
        add_instruction!(rm, render_text,
                         (@sprintf("%02.d",i), nx, ny, 7, colorant"white"))
        idx = cx - v.def.length/2
        idy = cy - v.def.width/2 + 0.1
        add_instruction!(rm, render_text,
                         (@sprintf("id %02.d",v.def.id), idx, idy, 7, colorant"white"))
    end
end

type CarVelOverlay <: SceneOverlay end

function AutoViz.render!(rm::RenderModel, o::CarVelOverlay, scene::Scene, roadway::Roadway)
    for (i,v) in enumerate(scene)
        cx = v.state.posG.x
        cy = v.state.posG.y
        vx = cx + v.def.length/2 - 1.4
        vy = cy + v.def.width/2 - 0.6
        add_instruction!(rm, render_text,
                         (@sprintf("%04.1f",v.state.v), vx, vy, 7, colorant"white"))
    end
end

#=
type HelloWorldOverlay <: SceneOverlay end

function AutoViz.render!(rendermodel::RenderModel, overlay::HelloWorldOverlay, scene::Scene, roadway::Roadway)
    add_instruction!(rendermodel, render_text, ("Hello World!", 0, 0, 12, colorant"white"))
end
=#

#=
function draw_bang(x::Union{Float64,Int},
										y::Union{Float64,Int},
										color::AbstractString="#FFBF00";
    								a::Union{Float64,Int}=2.,
										b::Union{Float64,Int}=a,
    								nb_spikes::Int=8,
										spike_depth::Union{Float64,Int}=1.5,
										th_offset::Union{Float64,Int}=0.,
    								th_irregularity::Float64=0.5,
										r_irregularity::Float64=0.5,
										rng::AbstractRNG=MersenneTwister(1),
    								edge_color::AbstractString="k")
    #A somewhat overly complicated function for drawing the crash graphic
    X = zeros(nb_spikes*2)
    Y = zeros(nb_spikes*2)

    th_interval = 2.*pi/length(X)

    for i = 1:length(X)
        if i % 2 == 1
            a_ = a+spike_depth*(0.5 + r_irregularity*(rand(rng)-0.5))
            b_ = b+spike_depth*(0.5 + r_irregularity*(rand(rng)-0.5))
        else
            a_ = a-spike_depth*(0.5 + r_irregularity*(rand(rng)-0.5))
            b_ = b-spike_depth*(0.5 + r_irregularity*(rand(rng)-0.5))
        end
        th = (i+th_irregularity*(rand(rng)-0.5))*th_interval+th_offset
        X[i] = x + a_*cos(th)
        Y[i] = y + b_*sin(th)
    end

    fill(X,Y,color=color,ec=edge_color)
end
=#

#=
function draw_direction(phys_param::PhysicalParam, x::Float64, y::Float64, v_nom::Float64, s::CarState)
	#plot desired car speed relative to self car as an opaque arrow
	#arrow should be at most one car in length, and ~1.2 of a car width at headway
	#NOTE: the arrow head is extra length
	max_arrow_length = 1.125*phys_param.l_car
	phantom_arrow_length = max_arrow_length*(s.vel-v_nom)/(phys_param.v_fast-phys_param.v_slow)
	phantom_arrow_head_width = 1.2*phys_param.w_car
	phantom_arrow_body_width  = 0.8*phys_param.w_car
	phantom_arrow_head_length = phantom_arrow_length/3.
	arrow(x,y,phantom_arrow_length,0.,head_width=phantom_arrow_head_width,
				head_length=phantom_arrow_head_width,width=phantom_arrow_body_width,
				alpha=0.5,color="#1C1C1C")

	#draw actual speed/direction; this is where the center of the car will be in
	#the next time step, approximately

	dx = phys_param.dt*(s.vel-v_nom)
    dy = s.lane_change*phys_param.w_lane
	hw = 0.5*phys_param.w_car
	hl = 1.5*hw
	w = 0.75*hw
	th = atan2(dy,dx)
	dx -= hl*cos(th)
	dy -= hl*sin(th)
	arrow(x,y,dx,dy,width=w,head_width=hw,head_length=hl,fc="#DF7401", ec="#0404B4",alpha=0.75)
	#terrible looking high contrast blue/orange arrows

end

function draw_sedan(pp::PhysicalParam, s::CarState, v_nom::Float64, frame::Float64=0., INTERVAL::Float64=0.)
	if s.x < 0.
		#oob
		return
	end
	lane_length = pp.lane_length
	x_ctr = s.x+frame
	y_ctr = pp.w_lane*s.y - INTERVAL*floor(Integer,x_ctr/lane_length)*(frame == 0. ? 0.: 1.)
	x_ctr = mod(x_ctr,lane_length+0.0001)
  #Fix nullable TODO
  if isnull(s.behavior)
    p = -1.
  else
    p = s.behavior.p_mobil.p
   end
   color = get(BEHAVIOR_COLORS,p,"#B404AE") #PLACEHOLDER
	draw_direction(pp,x_ctr,y_ctr,v_nom,s)
	draw_sedan(pp,x_ctr,y_ctr,color)
	annotate("$(round(s.vel,2))",xy=(x_ctr,y_ctr))
    annotate(string(s.id), xy=(x_ctr, y_ctr-2))
end
=#

# end # module Visualization

max_stars_repo_name: zhcao/HighwayExit

max_issues_repo_name: zhcao/HighwayExit

max_forks_repo_name: zhcao/HighwayExit


text:
module QXGraphDecompositions

include("LabeledGraph.jl")
include("treewidth.jl")

end