import Base: empty!, getindex, show
import Printf: @printf

if !isdefined(Main, :Partition)
include("core.jl")
end

"""
    entropy(partition)

Return average entropy in a partition.
The entropy of a bucket of size n is defined as lg(n), i.e. the number of additional bits
required to distinguish items in the bucket. The average is taken over all answers
in the partition.
```
julia> p=partition(["aback", "abase", "abate", "abbey"], "abbot")
ggybb aback abase
ggyby abate
gggbb abbey

julia> entropy(p)
0.5
```
"""
function entropy(p::Partition)
    sum = 0.0
    count = 0
    for i in p.nonEmptyBuckets
        n = length(p.buckets[i])
        sum += n * log(2, n)
        count += n
    end
    return sum / count
end

"""
    entropy(answers, guess)

Return remaining entropy, after partitioning possible answers with a guess.

Example:
```
julia> entropy(["aback", "abase", "abate", "abbey"], "abbot")
0.5
```
"""
entropy(answers::Vector{String}, guess::String) = entropy(partition(answers, guess))

"""
    minEntropy(partition, answers, guesses, maxCount)

Low-level variant of minEntropy. Caller passes in a Partition to use as scratch space.
Returns (entropy, guessIndex) pairs that minimize remaining entropy, in order from least to highest entropy.
"""
function minEntropy(scratch::Partition, answers::Vector{AnswerIndex}, guesses::Vector{GuessIndex}, maxCount::Integer)
    e = zeros(Float64, length(guesses))
    for i in 1:length(guesses)
        partition!(scratch, answers, guesses[i])
        e[i] = entropy(scratch)
    end
    perm = sortperm(e)
    return [(guesses[j], e[j]) for j in perm[1:min(maxCount,end)]]
end

"""
    minEntropy(answers, guesses, maxCount)

Return (entropy, guess) pairs that minimize remaining entropy, in order from least to highest entropy.
Returns min(maxCount, length(guesses)) elements.

Example:
```
julia> minEntropy(answerWords, guessWords, 4)
4-element Vector{Tuple{String, Float64}}:
 ("soare", 5.290836367768747)
 ("roate", 5.2940171538556315)
 ("raise", 5.29888678732612)
 ("raile", 5.311086768195722)
```
"""
function minEntropy(answers::Vector{String}, guesses::Vector{String}, maxCount::Integer)
    p = Partition()
    best = minEntropy(p, indicesOfWords(answers, answerWords), indicesOfWords(guesses, guessWords), maxCount)
    return [(guessWords[pair[1]], pair[2]) for pair in best]
end

"""
    search(answers, guesses, width)

Low-level overload of search that takes indices instead of strings.
"""
function search(answers::Vector{AnswerIndex}, guesses::Vector{GuessIndex}, width::Integer)
    minAvg = typemax(Float64)
    minGuess = nothing
    p = Partition()
    for (guess, entropy) in minEntropy(p, answers, guesses, width)
        partition!(p, answers, guess)
        if length(p) == 1
            # Useless guess
            continue
        end
        # sum accumulates number of guesses to guess each possible answer.
        sum = 0.0
        for i in p.nonEmptyBuckets
            # Get possible answers in the bucket
            a = p.buckets[i]
            if length(a) > 2
                sum += search(a, guesses, width)[2] * length(a)
            elseif length(a) == 2
                # Choose one of the answers as the next guess.
                # Takes one guess if lucky, two if not, for total of 3 over the two answers.
                sum += 3.0
            elseif answerWords[a[1]] == guessWords[guess]
                # Guess matched the answer
                sum += 0.0
            else
                # Only one possible answer left
                sum += 1.0
            end
        end
        avg = sum / length(answers) + 1
        if avg < minAvg
            minAvg = avg
            minGuess = guess
        end
    end
    return (minGuess, minAvg)
end

"""
    search(answers, guesses, width)

Do heuristic recursive search that minimizes average number of guesses to pick out an answer.
At each tree level, width guesses that minimize entropy are tried.

Returns pair with best first guess and average number of guesses (including that guess).
```
julia> search(["aback", "abase", "abate", "abbey"], ["aback", "abase", "abate", "abbey"], 4)
("abase", 1.75)
```
"""
function search(answers::Vector{String}, guesses::Vector{String}, width::Integer)
    (guessIndex, avg) = search(indicesOfWords(answers, answerWords), indicesOfWords(guesses, guessWords), width)
    return (guessWords[guessIndex], avg)
end

"""
    benchmark()

Time method `search` for successively wider windows.
```
julia> benchmark()
  3.843582 seconds (239.37 k allocations: 77.807 MiB, 0.23% gc time)
1 soare 3.532613
 10.000957 seconds (831.42 k allocations: 279.804 MiB, 0.33% gc time)
2 soare 3.524406
 19.578414 seconds (1.78 M allocations: 608.635 MiB, 0.31% gc time)
3 soare 3.520086
 33.192401 seconds (3.19 M allocations: 1.075 GiB, 0.32% gc time)
4 raile 3.514903
 52.062859 seconds (5.03 M allocations: 1.703 GiB, 0.33% gc time)
5 reast 3.480346
 76.379512 seconds (7.41 M allocations: 2.523 GiB, 0.33% gc time)
6 slate 3.476026
 99.450780 seconds (10.45 M allocations: 3.576 GiB, 0.33% gc time)
7 slate 3.474730
130.135821 seconds (14.07 M allocations: 4.829 GiB, 0.33% gc time)
8 slate 3.474730
```
"""
function benchmark()
    for width in 1:20
        p = @time search(answerWords, guessWords, width)
        @printf("%d %s %.6f\n", width, p[1], p[2])
    end
end

nothing


include("math.jl")
using KitBase

quadratureorder =2
points, triangulation = KitBase.octa_quadrature(quadratureorder)

println("-------------------")
#println(points)
println(size(points))

#nq = size(points, 1)

momentBasis =  ComputeSphericalBasisKarth(points,2, 3)
#momentBasis2 = ComputeSphericalBasisAnalytical(points)
println(size(momentBasis))
println(momentBasis)
#println(size(momentBasis2))
#println(momentBasis2-momentBasis)

#create points
points1D = linspace(-1,1,5)
points1DM = zeros(5,1)
points1DM[:,1] = points1D[:]
#println(size(points1DM))

momentBasis =  ComputeSphericalBasisKarth(points1DM,2,1)
momentBasis2 =  ComputeSphericalBasisAnalytical(points1DM)
println(size(momentBasis))
println(size(momentBasis2))
println(momentBasis-momentBasis2)


using JuMP
using Ipopt
using LinearAlgebra

X = randn(10,1000)
Xn = sqrt.(sum(X.^2, dims=2))
X ./= Xn
ϵ = 1e-1
λ = 1.0
α = 1.0
n_eigs = 5
v, V = eigen(X'*X + ϵ*I)
Γ = V[:, (end-n_eigs+1):end]

μ = rand(10)
U = copy(Γ)

function Ml(μ, X, ϵ)
    M = ϵ*I + X'*Diagonal(μ)*X
    Mc = cholesky(Hermitian(M))
    return (M, Mc)
end

Ml(μ) = Ml(μ, X, ϵ)

_, Mc = Ml(μ)

f(U) = α*sum((U.-Γ).^2) + λ*tr(U'*(Mc\U))

function g!(gvec, U, m, n)
    for i = 1:n
        gvec[((i-1)*m+1):i*m] .= 2*α*(U[1:m, i].-Γ[1:m, i]) .+ 2*λ*(Mc\U[1:m, i])
    end
end

m, n = size(U)

L = LinearIndices((m, n))
jump_wrapper_f(x...) = f([x[L[i,j]] for i in 1:m, j in 1:n])
jump_wrapper_g!(gvec, x...) = g!(gvec, [x[L[i,j]] for i in 1:m, j in 1:n], m, n)

sumprod(x...) = sum(x[i]*x[i+m] for i in 1:m)
function sumprod_grad!(gvec, x...)
    for i in 1:m
        gvec[i] = x[i+m]
        gvec[i+m] = x[i]
    end
end

model = Model(optimizer_with_attributes(Ipopt.Optimizer))
JuMP.register(model, :f, m*n, jump_wrapper_f, jump_wrapper_g!)
JuMP.register(model, :sumprod, 2*m, sumprod, sumprod_grad!)

@variable(model, x[1:m, 1:n])

set_start_value.(x, U)

# This is the problem section because of the indexing expressions being splatted

for i = 1:n
    for j = i:n
        x_i = x[1:m, i]
        x_j = x[1:m, j]
        @NLconstraint(model, sumprod(x_i..., x_j...) == ((i==j) ? 1 : 0))
    end
end

@NLobjective(model, Min, f(x...))

optimize!(model)


using Rubin
using Tests
using Elliptic
using HypergeometricFunctions
using Polylogarithms
using SpecialFunctions

@test integrate((a^2+-1*b^2*x^2)^-1, x) == :(a^-1*b^-1*arctanh(b*x*a^-1))
@test integrate((a^2+b^2*x^2)^-1, x) == :(a^-1*b^-1*arctan(b*x*a^-1))
@test integrate(sec(2*a*x), x) == :((1/2)*a^-1*arctanh(sin(2*a*x)))
@test integrate(1//4*sin(1//3*x)^-1, x) == :(-3//4*arctanh(cos(1//3*x)))
@test integrate(-1*cos(2x+1//4*pi)^-1, x) == :(-1//2*arctanh(sin(2x+1//4*pi)))
@test integrate(sec(x)*tan(x), x) == :(sec(x))
@test integrate(cot(x)*csc(x), x) == :(-1*csc(x))
@test integrate(sin(2x)^-1*tan(x), x) == :((1/2)*tan(x))
@test integrate((1+cos(x))^-1, x) == :((1+cos(x))^-1*sin(x))
@test integrate((1+-1*cos(x))^-1, x) == :(-1*(1+-1*cos(x))^-1*sin(x))
@test integrate((a+-1*b*cos(x))^-1*sin(x), x) == :(b^-1*log(a+-1*b*cos(x)))
@test integrate((a^2+b^2*sin(x)^2)^-1*cos(x), x) == :(a^-1*b^-1*arctan(b*a^-1*sin(x)))
@test integrate((a^2+-1*b^2*sin(x)^2)^-1*cos(x), x) == :(a^-1*b^-1*arctanh(b*a^-1*sin(x)))
@test integrate((a^2+b^2*sin(x)^2)^-1*sin(2x), x) == :(b^-2*log(a^2+b^2*sin(x)^2))
@test integrate((a^2+-1*b^2*sin(x)^2)^-1*sin(2x), x) == :(-1*b^-2*log(a^2+-1*b^2*sin(x)^2))
@test integrate((4+-1*cos(x)^2)^-1, x) == :(1//6*x*3^(1/2)+1//6*3^(1/2)*arctan((3+sin(x)^2+2*3^(1/2))^-1*cos(x)*sin(x)))
@test integrate((-1+exp(2x))^-1*exp(x), x) == :(-1*arctanh(exp(x)))
@test integrate(x^-1*log(x)^-1, x) == :(log(log(x)))
@test integrate(x^-1*(1+log(x)^2)^-1, x) == :(arctan(log(x)))
@test integrate(x^-1*(1+-1*log(x))^-1, x) == :(-1*log(1+-1*log(x)))
@test integrate(x^-1*(1+log(x*a^-1))^-1, x) == :(log(1+log(x*a^-1)))
@test integrate(x^-2*(1+x+-1*x^(1/2))^2, x) == :(x+-1*x^-1+-4*x^(1/2)+3*log(x)+4*x^-1//2)
@test integrate(x^-3//2*(2+-1*x^2//3)*(x+x^(1/2)), x) == :(2*log(x)+4*x^(1/2)+-6//7*x^7//6+-3//2*x^2//3)
@test integrate((3+2x)^-1*(-1+2x), x) == :(x+-2*log(3+2x))
@test integrate((-2+3*x^2)^-1*(-5+2x), x) == :((1//3+-5//12*6^(1/2))*log(6^(1/2)+-3x)+(1//3+5//12*6^(1/2))*log(6^(1/2)+3x))
@test integrate((2+3*x^2)^-1*(-5+2x), x) == :(1//3*log(2+3*x^2)+-5//6*6^(1/2)*arctan((1/2)*x*6^(1/2)))
@test integrate(sin(x)*sin(1//4*x), x) == :(-2//5*sin(5//4*x)+2//3*sin(3//4*x))
@test integrate(cos(3x)*cos(4x), x) == :((1/2)*sin(x)+1//14*sin(7x))
@test integrate(-1*tan(x)*tan(a+-1x), x) == :(-1x+cot(a)*log(cos(a+-1x))+-1*cot(a)*log(cos(x)))
@test integrate(sin(x)^2, x) == :((1/2)*x+-1//2*cos(x)*sin(x))
@test integrate(cos(x)^2, x) == :((1/2)*x+(1/2)*cos(x)*sin(x))
@test integrate(cos(x)^3*sin(x), x) == :(-1//4*cos(x)^4)
@test integrate(cos(x)^3*sin(x)^-4, x) == :(sin(x)^-1+-1//3*sin(x)^-3)
@test integrate(cos(x)^-2*sin(x)^-2, x) == :(-1*cot(x)+tan(x))
@test integrate(cot(3//4*x)^2, x) == :(-1x+-4//3*cot(3//4*x))
@test integrate((1+tan(2x))^2, x) == :((1/2)*tan(2x)+-1*log(cos(2x)))
@test integrate((-1*cot(x)+tan(x))^2, x) == :(-1*cot(x)+-4x+tan(x))
@test integrate((-1*sec(x)+tan(x))^2, x) == :(-1x+-2*(1+sin(x))^-1*cos(x))
@test integrate((1+sin(x))^-1*sin(x), x) == :(x+(1+sin(x))^-1*cos(x))
@test integrate((1+-1*cos(x))^-1*cos(x), x) == :(-1x+-1*(1+-1*cos(x))^-1*sin(x))
@test integrate((-1+exp((1/2)*x))^3*exp(-1//2*x), x) == :(-6*exp((1/2)*x)+2*exp(-1//2*x)+3x+exp(x))
@test integrate((5+x^2+-6x)^-1, x) == :(-1//4*log(1+-1x)+1//4*log(5+-1x))
@test integrate(x^2*(13+x^6+-6*x^3)^-1, x) == :(1//6*arctan(-3//2+(1/2)*x^3))
@test integrate((-1+x^2+-4x)^-1*(2+x), x) == :(1//10*(5+-4*5^(1/2))*log(2+-1x+-1*5^(1/2))+1//10*(5+4*5^(1/2))*log(2+5^(1/2)+-1x))
@test integrate((1+(1+x)^1//3)^-1, x) == :(-3*(1+x)^1//3+3*log(1+(1+x)^1//3)+3//2*(1+x)^2//3)
@test integrate(x^-1//2*(b+a*x)^-1, x) == :(2*a^-1//2*b^-1//2*arctan(a^(1/2)*b^-1//2*x^(1/2)))
@test integrate(x^3*(1+x^2)^(1/2), x) == :(-1//3*(1+x^2)^3//2+1//5*(1+x^2)^5//2)
@test integrate(x*(a^4+-1*x^4)^-1//2, x) == :((1/2)*arctan(x^2*(a^4+-1*x^4)^-1//2))
@test integrate(x^-1*(x^2+-1*a^2)^-1//2, x) == :(a^-1*arctan(a^-1*(x^2+-1*a^2)^(1/2)))
@test integrate(x^-1*(a^2+-1*x^2)^-1//2, x) == :(-1*a^-1*arctanh(a^-1*(a^2+-1*x^2)^(1/2)))
@test integrate(x^-1*(a^2+x^2)^-1//2, x) == :(-1*a^-1*arctanh(a^-1*(a^2+x^2)^(1/2)))
@test integrate((2+x+-1*x^2)^-1//2, x) == :(-1*arcsin(1//3+-2//3*x))
@test integrate((5+-4x+3*x^2)^-1//2, x) == :(-1//3*3^(1/2)*arcsinh(1//11*11^(1/2)*(2+-3x)))
@test integrate((x+-1*x^2)^-1//2, x) == :(-1*arcsin(1+-2x))
@test integrate((2+x+-1*x^2)^-1//2*(1+2x), x) == :(-2*(2+x+-1*x^2)^(1/2)+-2*arcsin(1//3+-2//3*x))
@test integrate(x^-1*(2+x+-1*x^2)^-1//2, x) == :(-1//2*2^(1/2)*arctanh(1//4*2^(1/2)*(2+x+-1*x^2)^-1//2*(4+x)))
@test integrate((1+-1*cos(x))^-1*sin(x)^-1*(2+3*sin(x)), x) == :(-1*(1+-1*cos(x))^-1+-1*arctanh(cos(x))+-3*(1+-1*cos(x))^-1*sin(x))
@test integrate(sin(2x)^-1*(1+-1*tan(x)), x) == :((1/2)*log(tan(x))+-1//2*tan(x))
@test integrate((1+-1*tan(x)^2)^-1*(1+tan(x)^2), x) == :((1/2)*arctanh(2*cos(x)*sin(x)))
@test integrate((a^2+-4*sin(x)^2)^-1//3*sin(2x), x) == :(-3//8*(a^2+-4*sin(x)^2)^2//3)
@test integrate((-1+a^(2x))^-1//2, x) == :(log(a)^-1*arctan((-1+a^(2x))^(1/2)))
@test integrate((-1+exp(x))^-1//2*exp((1/2)*x), x) == :(2*arctanh((-1+exp(x))^-1//2*exp((1/2)*x)))
@test integrate((1+x^2)^-1*arctan(x)^n, x) == :((1+n)^-1*arctan(x)^(1+n))
@test integrate((a^2+-1*x^2)^-1//2*arcsin(x*a^-1)^3//2, x) == :(2//5*a*(1+-1*a^-2*x^2)^(1/2)*(a^2+-1*x^2)^-1//2*arcsin(x*a^-1)^5//2)
@test integrate((1+-1*x^2)^-1//2*arccos(x)^-3, x) == :((1/2)*arccos(x)^-2)
@test integrate(x*log(x)^2, x) == :(1//4*x^2+(1/2)*x^2*log(x)^2+-1//2*x^2*log(x))
@test integrate(x^-5*log(x), x) == :(-1//16*x^-4+-1//4*x^-4*log(x))
@test integrate(cos(x)^5, x) == :(-2//3*sin(x)^3+1//5*sin(x)^5+sin(x))
@test integrate(cos(x)^4*sin(x)^2, x) == :(1//16*x+-1//6*cos(x)^5*sin(x)+1//16*cos(x)*sin(x)+1//24*cos(x)^3*sin(x))
@test integrate(sin(x)^-5, x) == :(-3//8*arctanh(cos(x))+-3//8*cot(x)*csc(x)+-1//4*csc(x)^3*cot(x))
@test integrate(exp(-1x)*sin(x), x) == :(-1//2*cos(x)*exp(-1x)+-1//2*exp(-1x)*sin(x))
@test integrate(exp(2x)*sin(3x), x) == :(-3//13*cos(3x)*exp(2x)+2//13*exp(2x)*sin(3x))
@test integrate(a^x*cos(x), x) == :(a^x*(1+log(a)^2)^-1*sin(x)+a^x*(1+log(a)^2)^-1*cos(x)*log(a))
@test integrate(cos(log(x)), x) == :((1/2)*x*cos(log(x))+(1/2)*x*sin(log(x)))
@test integrate(sec(x)^2*log(cos(x)), x) == :(-1x+log(cos(x))*tan(x)+tan(x))
@test integrate(x*tan(x)^2, x) == :(-1//2*x^2+x*tan(x)+log(cos(x)))
@test integrate(x^-2*arcsin(x), x) == :(-1*arctanh((1+-1*x^2)^(1/2))+-1*x^-1*arcsin(x))
@test integrate(arcsin(x)^2, x) == :(-2x+x*arcsin(x)^2+2*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate(x^2*(1+x^2)^-1*arctan(x), x) == :(-1//2*arctan(x)^2+-1//2*log(1+x^2)+x*arctan(x))
@test integrate((2x+3*x^2)^3, x) == :(2*x^4+9*x^6+27//7*x^7+36//5*x^5)
@test integrate((-1+2x+3*x^2)^2*(-1+x), x) == :(-1x+-7//2*x^4+-2//3*x^3+3//2*x^6+3//5*x^5+5//2*x^2)
@test integrate(x^(-1+k)*(a+b*x^k)^n, x) == :(b^-1*k^-1*(1+n)^-1*(a+b*x^k)^(1+n))
@test integrate(x^3*(1+2x)^-1, x) == :(-1//8*x^2+-1//16*log(1+2x)+1//6*x^3+1//8*x)
@test integrate(x^6*(2+3*x^2)^-1, x) == :(-2//27*x^3+1//15*x^5+4//27*x+-4//81*6^(1/2)*arctan((1/2)*x*6^(1/2)))
@test integrate((2+-7x+3*x^2)^-1, x) == :(-1//5*log(1+-3x)+1//5*log(2+-1x))
@test integrate((1+x^2+-1x)^-1*(-1+3x), x) == :(3//2*log(1+x^2+-1x)+-1//3*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2x)))
@test integrate(x^2*(5+x^2+2x)^-1, x) == :(x+-1*log(5+x^2+2x)+-3//2*arctan(1/2+(1/2)*x))
@test integrate((1+-1x+2*x^2)^-1*(-5*x^3+4*x^2+6*x^4), x) == :(x^3+-1//2*x^2+1//4*log(1+-1x+2*x^2)+-1//14*7^(1/2)*arctan(1//7*7^(1/2)*(1+-4x)))
@test integrate((x^2+x^3+-6x)^-1*(-1+x+x^2), x) == :((1/2)*log(2+-1x)+1//3*log(3+x)+1//6*log(x))
@test integrate((x^3+-6*a^3+-6*a*x^2+11*x*a^2)^-1*(5*x^2+11*a^2+-7*a*x), x) == :(-17*log(-1x+2a)+9//2*log(a+-1x)+35//2*log(-1x+3a))
@test integrate((6+x^4+-5*x^2)^-1*(-5+2*x^2), x) == :(-1//2*2^(1/2)*arctanh((1/2)*x*2^(1/2))+-1//3*3^(1/2)*arctanh(1//3*x*3^(1/2)))
@test integrate((-1+x)^-1*(-4+x)^-1*(-3+x)^-1*(-2+x)^-1, x) == :((1/2)*log(2+-1x)+-1//2*log(3+-1x)+-1//6*log(1+-1x)+1//6*log(4+-1x))
@test integrate((-1+x)^-3*(1+x^2), x) == :(-1*(1+-1x)^-2+2*(1+-1x)^-1+log(1+-1x))
@test integrate(x^5*(3+x)^-2, x) == :(-108x+-2*x^3+243*(3+x)^-1+405*log(3+x)+1//4*x^4+27//2*x^2)
@test integrate((-27+x^4+-8*x^3+18*x^2)^-1*(-2+5*x^3), x) == :(407*(48+-16x)^-1+-133//8*(3+-1x)^-2+7//64*log(1+x)+313//64*log(3+-1x))
@test integrate((3+x)^-2*(4+x)^-2*(-9+x^3+-6*x^2+3x), x) == :(-263*log(4+x)+99*(3+x)^-1+181*(4+x)^-1+264*log(3+x))
@test integrate(x^-1*(-1+x^2)^-2*(2+x^2+x^3), x) == :(2*log(x)+-5//4*log(1+x)+-3//4*log(1+-1x)+(2+-2*x^2)^-1*(3+x))
@test integrate((x^3+x^6+-1*x^4+-1*x^5)^-1, x) == :((2+-2x)^-1+-1*x^-1+2*log(x)+-7//4*log(1+-1x)+-1//2*x^-2+-1//4*log(1+x))
@test integrate((-1+x+x^3+-1*x^2)^-1*(1+x^4), x) == :(x+(1/2)*x^2+-1*arctan(x)+-1//2*log(1+x^2)+log(1+-1x))
@test integrate(x^-1*(1+x)^-1*(1+x^2)^-1, x) == :(-1//2*arctan(x)+-1//2*log(1+x)+-1//4*log(1+x^2)+log(x))
@test integrate(x^2*(-2+x^2+x^4)^-1, x) == :(-1//3*arctanh(x)+1//3*2^(1/2)*arctan((1/2)*x*2^(1/2)))
@test integrate((2+x^4+2*x^3+3*x^2+4x)^-1*(x^3+4*x^2+6x), x) == :((1+x)^-1+-1//3*log(1+x)+2//3*log(2+x^2)+4//3*2^(1/2)*arctan((1/2)*x*2^(1/2)))
@test integrate(x*(1+x)^-1*(1+x^2)^-1*(1+2x)^-2, x) == :(2*(5+10x)^-1+-7//100*log(1+x^2)+-1//2*log(1+x)+1//50*arctan(x)+16//25*log(1+2x))
@test integrate((1+x^2)^-1*(-1+x)^-3*(-2+x+3*x^2), x) == :(-1*arctan(x)+5*(2+-2x)^-1+-3//2*log(1+-1x)+-1//2*(1+-1x)^-2+3//4*log(1+x^2))
@test integrate((1+x^2+x^4)^-1, x) == :(-1//4*log(1+x^2+-1x)+1//4*log(1+x+x^2)+-1//6*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2x))+1//6*3^(1/2)*arctan(1//3*3^(1/2)*(1+2x)))
@test integrate((x^5+-9x)^-1*(3+2*x^3), x) == :(-1//3*log(x)+1//12*log(9+-1*x^4)+-1//3*3^(1/2)*arctanh(1//3*x*3^(1/2))+1//3*3^(1/2)*arctan(1//3*x*3^(1/2)))
@test integrate((-4+x)^-3*(8+x^2+-4x)^-1*(-20+5*x^3+8x), x) == :(41*(16+-4x)^-1+-83//4*(4+-1x)^-2+-45//16*log(4+-1x)+-3//16*arctan(1+-1//2*x)+45//32*log(8+x^2+-4x))
@test integrate((1+x^2)^-1*(2+x^2)^-1*(3+x^2)^-1*(4+x^2)^-1, x) == :(-1//12*arctan((1/2)*x)+1//6*arctan(x)+-1//4*2^(1/2)*arctan((1/2)*x*2^(1/2))+1//6*3^(1/2)*arctan(1//3*x*3^(1/2)))
@test integrate(x*(1+x^2)^-1*(2+x^2)^-1*(3+x^2)^-1*(4+x^2)^-1, x) == :(-1//4*log(2+x^2)+-1//12*log(4+x^2)+1//4*log(3+x^2)+1//12*log(1+x^2))
@test integrate((a^3+x^3)^-1, x) == :(-1//6*a^-2*log(a^2+x^2+-1*a*x)+1//3*a^-2*log(a+x)+-1//3*3^(1/2)*a^-2*arctan(1//3*3^(1/2)*a^-1*(a+-2x)))
@test integrate(x*(a^3+x^3)^-1, x) == :(-1//3*a^-1*log(a+x)+1//6*a^-1*log(a^2+x^2+-1*a*x)+-1//3*3^(1/2)*a^-1*arctan(1//3*3^(1/2)*a^-1*(a+-2x)))
@test integrate(x^2*(a^3+x^3)^-1, x) == :(1//3*log(a^3+x^3))
@test integrate(x^-1*(a^3+x^3)^-1, x) == :(a^-3*log(x)+-1//3*a^-3*log(a^3+x^3))
@test integrate(x^-2*(a^3+x^3)^-1, x) == :(-1*a^-3*x^-1+-1//6*a^-4*log(a^2+x^2+-1*a*x)+1//3*a^-4*log(a+x)+1//3*3^(1/2)*a^-4*arctan(1//3*3^(1/2)*a^-1*(a+-2x)))
@test integrate(x^-3*(a^3+x^3)^-1, x) == :(-1//2*a^-3*x^-2+-1//3*a^-5*log(a+x)+1//6*a^-5*log(a^2+x^2+-1*a*x)+1//3*3^(1/2)*a^-5*arctan(1//3*3^(1/2)*a^-1*(a+-2x)))
@test integrate(x^-4*(a^3+x^3)^-1, x) == :(-1*a^-6*log(x)+-1//3*a^-3*x^-3+1//3*a^-6*log(a^3+x^3))
@test integrate(x^-5*(a^3+x^3)^-1, x) == :(a^-6*x^-1+-1//3*a^-7*log(a+x)+-1//4*a^-3*x^-4+1//6*a^-7*log(a^2+x^2+-1*a*x)+-1//3*3^(1/2)*a^-7*arctan(1//3*3^(1/2)*a^-1*(a+-2x)))
@test integrate((a^4+-1*x^4)^-1, x) == :((1/2)*a^-3*arctan(x*a^-1)+(1/2)*a^-3*arctanh(x*a^-1))
@test integrate(x*(a^4+-1*x^4)^-1, x) == :((1/2)*a^-2*arctanh(a^-2*x^2))
@test integrate(x^-1*(a^4+-1*x^4)^-1, x) == :(a^-4*log(x)+-1//4*a^-4*log(a^4+-1*x^4))
@test integrate(x^-2*(a^4+-1*x^4)^-1, x) == :((1/2)*a^-5*arctanh(x*a^-1)+-1*a^-4*x^-1+-1//2*a^-5*arctan(x*a^-1))
@test integrate(x^-3*(a^4+-1*x^4)^-1, x) == :((1/2)*a^-6*arctanh(a^-2*x^2)+-1//2*a^-4*x^-2)
@test integrate(x^-4*(a^4+-1*x^4)^-1, x) == :((1/2)*a^-7*arctan(x*a^-1)+(1/2)*a^-7*arctanh(x*a^-1)+-1//3*a^-4*x^-3)
@test integrate(x*(a^4+x^4)^-1, x) == :((1/2)*a^-2*arctan(a^-2*x^2))
@test integrate(x^2*(a^4+x^4)^-1, x) == :(-1//4*2^(1/2)*a^-1*arctan(1+-1*x*2^(1/2)*a^-1)+-1//8*2^(1/2)*a^-1*log(a^2+x^2+a*x*2^(1/2))+1//4*2^(1/2)*a^-1*arctan(1+x*2^(1/2)*a^-1)+1//8*2^(1/2)*a^-1*log(a^2+x^2+-1*a*x*2^(1/2)))
@test integrate((a^5+x^5)^-1, x) == :(1//5*a^-4*log(a+x)+-1//5*a^-4*(5//2+(1/2)*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2))))+-1//5*a^-4*(5//2+-1//2*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2))))+-1//20*a^-4*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2)))+-1//20*a^-4*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2))))
@test integrate(x*(a^5+x^5)^-1, x) == :(-1//5*a^-3*log(a+x)+-1//5*a^-3*(5//2+(1/2)*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2))))+1//5*a^-3*(5//2+-1//2*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2))))+1//20*a^-3*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2)))+1//20*a^-3*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2))))
@test integrate(x^2*(a^5+x^5)^-1, x) == :(1//5*a^-2*log(a+x)+-1//5*a^-2*(5//2+(1/2)*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2))))+-1//20*a^-2*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2)))+-1//20*a^-2*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2)))+1//5*a^-2*(5//2+-1//2*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2)))))
@test integrate(x^3*(a^5+x^5)^-1, x) == :(-1//5*a^-1*log(a+x)+-1//5*a^-1*(5//2+(1/2)*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2))))+-1//5*a^-1*(5//2+-1//2*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2))))+1//20*a^-1*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2)))+1//20*a^-1*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2))))
@test integrate(x^4*(a^5+x^5)^-1, x) == :(1//5*log(a^5+x^5))
@test integrate(x^-1*(a^5+x^5)^-1, x) == :(a^-5*log(x)+-1//5*a^-5*log(a^5+x^5))
@test integrate(x^-2*(a^5+x^5)^-1, x) == :(-1*a^-5*x^-1+1//5*a^-6*log(a+x)+-1//20*a^-6*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2)))+-1//20*a^-6*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2)))+1//5*a^-6*(5//2+(1/2)*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2))))+1//5*a^-6*(5//2+-1//2*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2)))))
@test integrate(x^-3*(a^5+x^5)^-1, x) == :(-1//2*a^-5*x^-2+-1//5*a^-7*log(a+x)+-1//5*a^-7*(5//2+-1//2*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2))))+1//5*a^-7*(5//2+(1/2)*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2))))+1//20*a^-7*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2)))+1//20*a^-7*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2))))
@test integrate(x^-4*(a^5+x^5)^-1, x) == :(-1//3*a^-5*x^-3+1//5*a^-8*log(a+x)+-1//5*a^-8*(5//2+-1//2*5^(1/2))^(1/2)*arctan(a^-1*(10+2*5^(1/2))^-1//2*(-4x+a*(1+-1*5^(1/2))))+-1//20*a^-8*(1+5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+-1//2*5^(1/2)))+-1//20*a^-8*(1+-1*5^(1/2))*log(a^2+x^2+-1*a*x*(1/2+(1/2)*5^(1/2)))+1//5*a^-8*(5//2+(1/2)*5^(1/2))^(1/2)*arctan((1/2)*a^-1*(1/2+1//10*5^(1/2))^(1/2)*(-4x+a*(1+5^(1/2)))))
@test integrate((1+x^6)^-1*(1+x^4), x) == :(-1//3*arctan(3^(1/2)+-2x)+1//3*arctan(3^(1/2)+2x)+2//3*arctan(x))
@test integrate((5+x^2+3x)^-3, x) == :((605+121*x^2+363x)^-1*(9+6x)+1//22*(5+x^2+3x)^-2*(3+2x)+12//1331*11^(1/2)*arctan(1//11*11^(1/2)*(3+2x)))
@test integrate((1+x^2)^-4*(1+x^2+x^4), x) == :(7//16*arctan(x)+7*x*(16+16*x^2)^-1+-1//24*x*(1+x^2)^-2+1//6*x*(1+x^2)^-3)
@test integrate((c+a*x^2+2*b*x)^-2*(B+A*x), x) == :(-1*(2*b^2+-2*a*c)^-1*(c+a*x^2+2*b*x)^-1*(B*b+-1*A*c+-1*x*(A*b+-1*B*a))+-1//2*(b^2+-1*a*c)^-3//2*(A*b+-1*B*a)*arctanh((b^2+-1*a*c)^-1//2*(b+a*x)))
@test integrate((5+x^2+-4x)^-2*(-41+-27*x^2+5*x^3+55x), x) == :(-2*arctan(2+-1x)+5//2*log(5+x^2+-4x)+(5+x^2+-4x)^-1*(1+-1x))
@test integrate((-1+x^3)^-2, x) == :(-2//9*log(1+-1x)+1//9*log(1+x+x^2)+x*(3+-3*x^3)^-1+2//9*3^(1/2)*arctan(1//3*3^(1/2)*(1+2x)))
@test integrate(x^-2*(1+x^2)^-3*(4+3*x^4), x) == :(-4*x^-1+-57//8*arctan(x)+-25*x*(8+8*x^2)^-1+-7//4*x*(1+x^2)^-2)
@test integrate(x*(1+x^6)^-1, x) == :(-1//12*log(1+x^4+-1*x^2)+1//6*log(1+x^2)+-1//6*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2*x^2)))
@test integrate(x^3*(1+-2*x^2+3*x^4)^-1, x) == :(1//12*log(1+-2*x^2+3*x^4)+-1//12*2^(1/2)*arctan((1/2)*2^(1/2)*(1+-3*x^2)))
@test integrate(x^5*(-4+x^2+3*x^4)^-1, x) == :(-8//63*log(4+3*x^2)+1//6*x^2+1//14*log(1+-1*x^2))
@test integrate(x^2*(9+x^6+-10*x^3)^-1, x) == :(-1//24*log(1+-1*x^3)+1//24*log(9+-1*x^3))
@test integrate((-2+x)^-4*(1+x^3+-4*x^2), x) == :(2*(2+-1x)^-1+2*(2+-1x)^-2+-7//3*(2+-1x)^-3+log(2+-1x))
@test integrate(x^3*(-1+x)^-12, x) == :(-3//10*(1+-1x)^-10+-1//8*(1+-1x)^-8+1//3*(1+-1x)^-9+1//11*(1+-1x)^-11)
@test integrate((1+2x)^-5*(x^4+-3x), x) == :((8+16x)^-1+-25//128*(1+2x)^-4+-3//32*(1+2x)^-2+1//32*log(1+2x)+7//24*(1+2x)^-3)
@test integrate((1+x)^-3*(-1+x)^-2, x) == :((8+-8x)^-1+-1*(4+4x)^-1+-1//8*(1+x)^-2+3//8*arctanh(x))
@test integrate(x^-2*(5+-6x)^-2, x) == :(6*(125+-150x)^-1+-12//125*log(5+-6x)+-1//25*x^-1+12//125*log(x))
@test integrate((-3+x^2+-2x)^-3, x) == :(-3//512*log(1+x)+3//512*log(3+-1x)+(384+-128*x^2+256x)^-1*(3+-3x)+1//16*(3+-1*x^2+2x)^-2*(1+-1x))
@test integrate((13+x^2+-4x)^-3, x) == :(1//648*arctan(-2//3+1//3*x)+-1*(2808+-864x+216*x^2)^-1*(2+-1x)+-1//36*(13+x^2+-4x)^-2*(2+-1x))
@test integrate((2+x)^-3*(3+x)^-4, x) == :(-10*log(3+x)+4*(2+x)^-1+6*(3+x)^-1+10*log(2+x)+-1//2*(2+x)^-2+1//3*(3+x)^-3+3//2*(3+x)^-2)
@test integrate(x^8*(4+x^2)^-4, x) == :(-35//8*arctan((1/2)*x)+35//16*x+-35*x^3*(192+48*x^2)^-1+-7//24*x^5*(4+x^2)^-2+-1//6*x^7*(4+x^2)^-3)
@test integrate((5+2x+3*x^2)^-2*(-4+7x), x) == :(-1*(140+56x+84*x^2)^-1*(39+19x)+-19//392*14^(1/2)*arctan(1//14*14^(1/2)*(1+3x)))
@test integrate((-2+-4x+3*x^2)^-2*(5+-4x), x) == :(-1*(40+-60*x^2+80x)^-1*(18+-7x)+-7//200*10^(1/2)*arctanh(1//10*10^(1/2)*(2+-3x)))
@test integrate(x^5*(1+x^4)^-3, x) == :(1//16*arctan(x^2)+x^2*(16+16*x^4)^-1+-1//8*x^2*(1+x^4)^-2)
@test integrate(x^3*(a^4+x^4)^-3, x) == :(-1//8*(a^4+x^4)^-2)
@test integrate(x^-1*(a^4+x^4)^-3, x) == :(a^-12*log(x)+-1//4*a^-12*log(a^4+x^4)+1//4*a^-8*(a^4+x^4)^-1+1//8*a^-4*(a^4+x^4)^-2)
@test integrate(x^-2*(a^4+x^4)^-3, x) == :(-45//32*a^-12*x^-1+-45//128*2^(1/2)*a^-13*arctan(1+x*2^(1/2)*a^-1)+-45//256*2^(1/2)*a^-13*log(a^2+x^2+-1*a*x*2^(1/2))+1//8*a^-4*x^-1*(a^4+x^4)^-2+9//32*a^-8*x^-1*(a^4+x^4)^-1+45//128*2^(1/2)*a^-13*arctan(1+-1*x*2^(1/2)*a^-1)+45//256*2^(1/2)*a^-13*log(a^2+x^2+a*x*2^(1/2)))
@test integrate(x^-3*(a^4+x^4)^-3, x) == :(-15//16*a^-14*arctan(a^-2*x^2)+-15//16*a^-12*x^-2+1//8*a^-4*x^-2*(a^4+x^4)^-2+5//16*a^-8*x^-2*(a^4+x^4)^-1)
@test integrate(x^14*(3+2*x^5)^-3, x) == :(3*(60+40*x^5)^-1+-9//80*(3+2*x^5)^-2+1//40*log(3+2*x^5))
@test integrate(9//5*x^-2*(3+-2*x^2)^-3, x) == :(-1//8*x^-1+1//8*x^-1*(3+-2*x^2)^-1+1//24*6^(1/2)*arctanh(1//3*x*6^(1/2))+3//20*x^-1*(3+-2*x^2)^-2)
@test integrate(x^-2*(1+x^2)^-3*(4+3*x^4), x) == :(-4*x^-1+-57//8*arctan(x)+-25*x*(8+8*x^2)^-1+-7//4*x*(1+x^2)^-2)
@test integrate((-1+x+x^5+-1*x^4+-2*x^3+2*x^2)^-1*(5+-1*x^4+-3x+5*x^3+6*x^2), x) == :((1+x)^-1+-2*log(1+x)+2*(1+-1x)^-1+-3//2*(1+-1x)^-2+log(1+-1x))
@test integrate(x^-1*(1+x^3)^-2*(1+x^2), x) == :(-5//18*log(1+x^2+-1x)+-4//9*log(1+x)+-1//9*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2x))+x*(3+3*x^3)^-1*(x+-1*x^2)+log(x))
@test integrate((1+x)^-2*(1+x+x^2)^-2*(-2+x^2+-3x), x) == :((1/2)*log(1+x+x^2)+-1*log(1+x)+-2*(1+x)^-1+-1*(3+3x+3*x^2)^-1*(7+5x)+-25//9*3^(1/2)*arctan(1//3*3^(1/2)*(1+2x)))
@test integrate((1+-4x)^-3*(2+-3x)^-1, x) == :(-3*(25+-100x)^-1+-9//125*log(1+-4x)+1//10*(1+-4x)^-2+9//125*log(2+-3x))
@test integrate(x^3*(2+-5*x^2)^-7, x) == :(-1//250*(2+-5*x^2)^-5+1//150*(2+-5*x^2)^-6)
@test integrate(x^7*(2+-5*x^2)^-3, x) == :(-6*(1250+-3125*x^2)^-1+-3//625*log(2+-5*x^2)+-1//250*x^2+2//625*(2+-5*x^2)^-2)
@test integrate((2+x)^-3*(3+x)^-4, x) == :(-10*log(3+x)+4*(2+x)^-1+6*(3+x)^-1+10*log(2+x)+-1//2*(2+x)^-2+1//3*(3+x)^-3+3//2*(3+x)^-2)
@test integrate(x^5*(3+x)^-2, x) == :(-108x+-2*x^3+243*(3+x)^-1+405*log(3+x)+1//4*x^4+27//2*x^2)
@test integrate((b1+c1*x)*(a+c*x^2+2*b*x), x) == :(x^2*(b*b1+(1/2)*a*c1)+x^3*(1//3*b1*c+2//3*b*c1)+a*b1*x+1//4*c*c1*x^4)
@test integrate((a+c*x^2+2*b*x)^2*(b1+c1*x), x) == :(x^3*(4//3*b1*b^2+2//3*a*b1*c+4//3*a*b*c1)+x^4*(c1*b^2+b*b1*c+(1/2)*a*c*c1)+b1*x*a^2+(1/2)*a*x^2*(a*c1+4*b*b1)+1//5*c*x^5*(b1*c+4*b*c1)+1//6*c1*c^2*x^6)
@test integrate((a+c*x^2+2*b*x)^3*(b1+c1*x), x) == :(x^4*(2*b1*b^3+3*a*c1*b^2+3//4*c*c1*a^2+3*a*b*b1*c)+x^5*(8//5*c1*b^3+3//5*a*b1*c^2+12//5*b1*c*b^2+12//5*a*b*c*c1)+a*x^3*(4*b1*b^2+a*b1*c+2*a*b*c1)+b1*x*a^3+(1/2)*c*x^6*(4*c1*b^2+a*c*c1+2*b*b1*c)+(1/2)*a^2*x^2*(a*c1+6*b*b1)+1//7*c^2*x^7*(b1*c+6*b*c1)+1//8*c1*c^3*x^8)
@test integrate((a+c*x^2+2*b*x)^4*(b1+c1*x), x) == :(x^5*(16//5*b1*b^4+6//5*b1*a^2*c^2+32//5*a*c1*b^3+24//5*b*c*c1*a^2+48//5*a*b1*c*b^2)+x^6*(8//3*c1*b^4+c1*a^2*c^2+16//3*b1*c*b^3+4*a*b*b1*c^2+8*a*c*c1*b^2)+a*x^4*(8*b1*b^3+c*c1*a^2+6*a*c1*b^2+6*a*b*b1*c)+b1*x*a^4+(1/2)*a^3*x^2*(a*c1+8*b*b1)+(1/2)*c^2*x^8*(6*c1*b^2+a*c*c1+2*b*b1*c)+1//9*c^3*x^9*(b1*c+8*b*c1)+1//10*c1*c^4*x^10+4//3*a^2*x^3*(6*b1*b^2+a*b1*c+2*a*b*c1)+4//7*c*x^7*(8*c1*b^3+a*b1*c^2+6*b1*c*b^2+6*a*b*c*c1))
@test integrate((a+c*x^2+2*b*x)^-1*(b1+c1*x), x) == :((1/2)*c1*c^-1*log(a+c*x^2+2*b*x)+-1*c^-1*(b^2+-1*a*c)^-1//2*(b1*c+-1*b*c1)*arctanh((b^2+-1*a*c)^-1//2*(b+c*x)))
@test integrate((a+c*x^2+2*b*x)^-2*(b1+c1*x), x) == :((1/2)*(b^2+-1*a*c)^-3//2*(b1*c+-1*b*c1)*arctanh((b^2+-1*a*c)^-1//2*(b+c*x))+-1*(2*b^2+-2*a*c)^-1*(a+c*x^2+2*b*x)^-1*(b*b1+x*(b1*c+-1*b*c1)+-1*a*c1))
@test integrate((a+c*x^2+2*b*x)^-3*(b1+c1*x), x) == :(-1*(4*b^2+-4*a*c)^-1*(a+c*x^2+2*b*x)^-2*(b*b1+x*(b1*c+-1*b*c1)+-1*a*c1)+-3//8*c*(b^2+-1*a*c)^-5//2*(b1*c+-1*b*c1)*arctanh((b^2+-1*a*c)^-1//2*(b+c*x))+1//8*(b^2+-1*a*c)^-2*(a+c*x^2+2*b*x)^-1*(b+c*x)*(-3*b*c1+3*b1*c))
@test integrate((a+c*x^2+2*b*x)^-4*(b1+c1*x), x) == :(-1*(6*b^2+-6*a*c)^-1*(a+c*x^2+2*b*x)^-3*(b*b1+x*(b1*c+-1*b*c1)+-1*a*c1)+1//24*(b^2+-1*a*c)^-2*(a+c*x^2+2*b*x)^-2*(b+c*x)*(-5*b*c1+5*b1*c)+5//16*c^2*(b^2+-1*a*c)^-7//2*(b1*c+-1*b*c1)*arctanh((b^2+-1*a*c)^-1//2*(b+c*x))+-5//16*c*(b^2+-1*a*c)^-3*(a+c*x^2+2*b*x)^-1*(b+c*x)*(b1*c+-1*b*c1))
@test integrate(x*(3+2*x^2+6x)^-1, x) == :((1//4+-1//4*3^(1/2))*log(3+-1*3^(1/2)+2x)+(1//4+1//4*3^(1/2))*log(3+3^(1/2)+2x))
@test integrate((3+2*x^2+6x)^-3*(-3+2x), x) == :(-1*(6+4*x^2+12x)^-1*(3+2x)+1//3*3^(1/2)*arctanh(1//3*3^(1/2)*(3+2x))+1//4*(3+2*x^2+6x)^-2*(5+4x))
@test integrate((4+x^2+5x)^-2*(-1+x), x) == :(-7//27*log(4+x)+7//27*log(1+x)+(36+9*x^2+45x)^-1*(13+7x))
@test integrate((2+x^2+3x)^-5, x) == :(-70*log(2+x)+70*log(1+x)+(2+x^2+3x)^-1*(105+70x)+-1//6*(2+x^2+3x)^-2*(105+70x)+1//4*(2+x^2+3x)^-4*(-3+-2x)+1//6*(2+x^2+3x)^-3*(21+14x))
@test integrate(x^-3*(7+-6x+2*x^2)^-2, x) == :(-69//1715*x^-1+-40//2401*log(7+-6x+2*x^2)+-1//490*x^-2+80//2401*log(x)+-234//60025*5^(1/2)*arctan(1//5*5^(1/2)*(3+-2x))+-1//35*x^-2*(7+-6x+2*x^2)^-1*(2+-3x))
@test integrate(x^9*(2+x^2+3x)^-5, x) == :(-1471*log(1+x)+735x+1472*log(2+x)+(1/2)*x^4*(2+x^2+3x)^-2*(184+135x)+-1*x^2*(4+2*x^2+6x)^-1*(2206+1593x)+-1//12*x^6*(2+x^2+3x)^-3*(110+81x)+1//4*x^8*(2+x^2+3x)^-4*(4+3x))
@test integrate((1+2x)^2*(3+2*x^2+5x)^-5, x) == :(-2480*log(3+2x)+2480*log(1+x)+(3+2*x^2+5x)^-1*(3100+2480x)+-1//3*(3+2*x^2+5x)^-2*(775+620x)+1//3*(3+2*x^2+5x)^-3*(73+62x)+1//4*(3+2*x^2+5x)^-4*(1+2x)*(7+6x))
@test integrate(x^-7*(a+-1*b*x^2)^3, x) == :(-1*b^3*log(x)+-1//6*a^3*x^-6+-3//2*a*b^2*x^-2+3//4*b*a^2*x^-4)
@test integrate(x^13*(a^4+x^4)^-5, x) == :(-5//96*x^6*(a^4+x^4)^-3+-5//128*x^2*(a^4+x^4)^-2+-1//16*x^10*(a^4+x^4)^-4+5//256*a^-6*arctan(a^-2*x^2)+5//256*a^-4*x^2*(a^4+x^4)^-1)
@test integrate(x^3//2*(-1x+2*x^(1/2))^2*(1+x^2), x) == :(-1*x^4+-2//3*x^6+2//9*x^9//2+2//13*x^13//2+8//7*x^7//2+8//11*x^11//2)
@test integrate((x^3//2+-3*x^3//5)^2*(4*x^3//2+-1//3*x^2//3), x) == :(-120//23*x^23//5+-45//43*x^43//15+-1//14*x^14//3+8//11*x^11//2+60//113*x^113//30+360//37*x^37//10)
@test integrate((1+(1+x)^(1/2))^-1, x) == :(-2*log(1+(1+x)^(1/2))+2*(1+x)^(1/2))
@test integrate(x*(1+(1+x)^(1/2))^-1, x) == :(-1x+2//3*(1+x)^3//2)
@test integrate((-1+(1+x)^(1/2))^-1*(1+(1+x)^(1/2)), x) == :(x+4*(1+x)^(1/2)+4*log(1+-1*(1+x)^(1/2)))
@test integrate(((1+x)^2//3+-1*(1+x)^(1/2))^-1, x) == :(3*(1+x)^1//3+6*(1+x)^1//6+6*log(1+-1*(1+x)^1//6))
@test integrate(x^-1//2*(1+x^1//4)^1//3, x) == :(-3*(1+x^1//4)^4//3+12//7*(1+x^1//4)^7//3)
@test integrate(x^-3*(1+x)^-3//2, x) == :(-15//4*arctanh((1+x)^(1/2))+15//4*(1+x)^-1//2+-1//2*x^-2*(1+x)^-1//2+5//4*x^-1*(1+x)^-1//2)
@test integrate(x^-5*(1+-1x)^-7//2, x) == :(-3003//64*arctanh((1+-1x)^(1/2))+1001//64*(1+-1x)^-3//2+3003//64*(1+-1x)^-1//2+3003//320*(1+-1x)^-5//2+-429//64*x^-1*(1+-1x)^-5//2+-143//96*x^-2*(1+-1x)^-5//2+-13//24*x^-3*(1+-1x)^-5//2+-1//4*x^-4*(1+-1x)^-5//2)
@test integrate(x^-5*(-1+x)^-2//3, x) == :(-55//243*log(x)+55//81*log(1+(-1+x)^1//3)+-110//243*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2*(-1+x)^1//3))+1//4*x^-4*(-1+x)^1//3+11//27*x^-2*(-1+x)^1//3+11//36*x^-3*(-1+x)^1//3+55//81*x^-1*(-1+x)^1//3)
@test integrate(((1+x)^-1*(1+-1x))^(1/2), x) == :(-2*arctan(((1+x)^-1*(1+-1x))^(1/2))+((1+x)^-1*(1+-1x))^(1/2)*(1+x))
@test integrate((-1+x)^-1*(-25+x^2)^-1*(-5+x)^(1/2)*(3+x)^(1/2), x) == :(1//6*arctan(1//4*(-5+x)^(1/2)*(3+x)^(1/2))+1//15*5^(1/2)*arctanh(5^(1/2)*(-5+x)^-1//2*(3+x)^(1/2)))
@test integrate(x*(1+x)^2//3*(1+-1x)^(1/2)*((1+x)^(1/2)*(1+-1x)^2//3+-1*(1+x)^1//3*(1+-1x)^5//6)^-1, x) == :(-5//6*arctan((1+x)^-1//6*(1+-1x)^-1//6*((1+-1x)^1//3+-1*(1+x)^1//3))+1//6*arctan((1+x)^1//6*(1+-1x)^-1//6)+-1*(3+x)*(1//4+-1//4*x)+-4//9*3^(1/2)*arctan(1//3*3^(1/2)*(1+-1x)^-1//3*((1+-1x)^1//3+-2*(1+x)^1//3))+1//18*3^(1/2)*arctanh(3^(1/2)*(1+x)^1//6*(1+-1x)^1//6*((1+x)^1//3+(1+-1x)^1//3)^-1)+(1+x)^1//3*(1+-1x)^2//3*(-1//12+1//4*x)+-1//12*(1+x)^1//6*(1+-1x)^5//6*(10+3x)+1//4*x*(1+x)^(1/2)*(1+-1x)^(1/2)+1//12*(1+x)^2//3*(1+-1x)^1//3*(1+3x)+1//12*(1+x)^5//6*(1+-1x)^1//6*(2+3x))
@test integrate(((-1+x)^2*(1+x))^-1//3, x) == :(-3//2*log(1+-1*((-1+x)^2*(1+x))^-1//3*(-1+x))+-1//2*log(1+x)+3^(1/2)*arctan(1//3*3^(1/2)*(1+((-1+x)^2*(1+x))^-1//3*(-2+2x))))
@test integrate(x^-2*((-1+x)^2*(1+x))^1//3, x) == :(-3//2*log(1+-1*((-1+x)^2*(1+x))^-1//3*(-1+x))+-2//3*log(1+x)+-1//2*log(1+((-1+x)^2*(1+x))^-1//3*(-1+x))+1//6*log(x)+-1*3^(1/2)*arctan(1//3*3^(1/2)*(1+((-1+x)^2*(1+x))^-1//3*(-2+2x)))+-1*x^-1*((-1+x)^2*(1+x))^1//3+-1//3*3^(1/2)*arctan(1//3*3^(1/2)*(1+-1*((-1+x)^2*(1+x))^-1//3*(-2+2x))))
@test integrate((-3+x^2+-2x)^-5//2, x) == :(-1//24*(-3+x^2+-2x)^-1//2*(1+-1x)+1//12*(-3+x^2+-2x)^-3//2*(1+-1x))
@test integrate((9+x^3+-5*x^2+3x)^-1//2, x) == :((1+x)^(1/2)*(9+x^3+-5*x^2+3x)^-1//2*(3+-1x)*arctanh((1/2)*(1+x)^(1/2)))
@test integrate((9+x^3+-5*x^2+3x)^-3//2, x) == :(-15//256*(3+-1x)^3*(9+x^3+-5*x^2+3x)^-3//2*(1+x)+1//8*(9+x^3+-5*x^2+3x)^-3//2*(1+x)*(3+-1x)+5//64*(3+-1x)^2*(9+x^3+-5*x^2+3x)^-3//2*(1+x)+15//512*(1+x)^3//2*(3+-1x)^3*(9+x^3+-5*x^2+3x)^-3//2*arctanh((1/2)*(1+x)^(1/2)))
@test integrate((9+x^3+-5*x^2+3x)^-1//3, x) == :(-3//2*log(1+-1*(9+x^3+-5*x^2+3x)^-1//3*(-3+x))+-1//2*log(1+x)+3^(1/2)*arctan(1//3*3^(1/2)*(1+(9+x^3+-5*x^2+3x)^-1//3*(-6+2x))))
@test integrate((9+x^3+-5*x^2+3x)^-2//3, x) == :(1//4*(9+x^3+-5*x^2+3x)^-2//3*(1+x)*(9+-3x))
@test integrate((9+x^3+-5*x^2+3x)^-4//3, x) == :(-27//320*(3+-1x)^3*(9+x^3+-5*x^2+3x)^-4//3*(1+x)+1//20*(9+x^3+-5*x^2+3x)^-4//3*(1+x)*(9+-3x)+9//80*(3+-1x)^2*(9+x^3+-5*x^2+3x)^-4//3*(1+x))
@test integrate((4+-2*x^2+3x)^-1//2, x) == :(-1//2*2^(1/2)*arcsin(1//41*41^(1/2)*(3+-4x)))
@test integrate((-3+-1*x^2+4x)^-1//2, x) == :(-1*arcsin(2+-1x))
@test integrate((-2+-5x+-3*x^2)^-1//2, x) == :(1//3*3^(1/2)*arcsin(5+6x))
@test integrate((1+-1*x^2)^-1//2*(4+x^2)^-1, x) == :(1//10*5^(1/2)*arctan((1/2)*x*5^(1/2)*(1+-1*x^2)^-1//2))
@test integrate((1+4*x^2)^-1//2*(4+x^2)^-1, x) == :(1//30*15^(1/2)*arctanh((1/2)*x*15^(1/2)*(1+4*x^2)^-1//2))
@test integrate(x*(3+-1*x^2)^-1*(5+-1*x^2)^-1//2, x) == :((1/2)*2^(1/2)*arctanh((1/2)*2^(1/2)*(5+-1*x^2)^(1/2)))
@test integrate(x*(3+-1*x^2)^-1//2*(5+-1*x^2)^-1, x) == :(-1//2*2^(1/2)*arctan((1/2)*2^(1/2)*(3+-1*x^2)^(1/2)))
@test integrate((-1+x^4)^-1*(2+x^2)^-1//2, x) == :(-1//2*arctan(x*(2+x^2)^-1//2)+-1//6*3^(1/2)*arctanh(x*3^(1/2)*(2+x^2)^-1//2))
@test integrate(x*(-1+x^2)^-1*(4+x^2+2x)^-1//2, x) == :(-1//6*3^(1/2)*arctanh(1//3*3^(1/2)*(4+x^2+2x)^(1/2))+-1//14*7^(1/2)*arctanh(1//7*7^(1/2)*(4+x^2+2x)^-1//2*(5+2x)))
@test integrate((-8+x^3)^-1*(5+x^2+2x)^-1//2, x) == :(1//12*arctanh((5+x^2+2x)^(1/2))+-1//12*3^(1/2)*arctan(1//3*3^(1/2)*(5+x^2+2x)^-1//2*(1+x))+-1//156*13^(1/2)*arctanh(1//13*13^(1/2)*(5+x^2+2x)^-1//2*(7+3x)))
@test integrate(x*(4+x+x^2)^-1*(5+4x+4*x^2)^-1//2, x) == :(-1//165*165^(1/2)*arctanh(1//15*165^(1/2)*(5+4x+4*x^2)^-1//2*(1+2x))+1//11*11^(1/2)*arctan(1//11*11^(1/2)*(5+4x+4*x^2)^(1/2)))
@test integrate((1+x^2)^-1*(1+x+x^2)^-1//2*(3+x), x) == :(2^(1/2)*arctanh((1/2)*2^(1/2)*(1+x+x^2)^-1//2*(1+x))+-2*2^(1/2)*arctan((1/2)*2^(1/2)*(1+x+x^2)^-1//2*(1+-1x)))
@test integrate((-1+x^2+6x)^-1//2*(4+3*x^2+4x)^-1*(1+2x), x) == :(-5//84*14^(1/2)*arctan(1//4*14^(1/2)*(-1+x^2+6x)^-1//2*(2+-1x))+-1//21*7^(1/2)*arctanh(7^(1/2)*(-1+x^2+6x)^-1//2*(1+x)))
@test integrate((13+-22x+10*x^2)^-1//2*(17+-18x+5*x^2)^-1*(-2+x), x) == :(1//70*35^(1/2)*arctanh((1/2)*35^(1/2)*(13+-22x+10*x^2)^-1//2*(1+-1x)))
@test integrate(x^4*(5+-1*x^2)^(1/2), x) == :(125//16*arcsin(1//5*x*5^(1/2))+-25//16*x*(5+-1*x^2)^(1/2)+-5//24*x^3*(5+-1*x^2)^(1/2)+1//6*x^5*(5+-1*x^2)^(1/2))
@test integrate(x^-6*(2+x^2)^-1//2, x) == :(-1//10*x^-5*(2+x^2)^(1/2)+-1//15*x^-1*(2+x^2)^(1/2)+1//15*x^-3*(2+x^2)^(1/2))
@test integrate((3+2*x^2)^-7//2, x) == :(1//15*x*(3+2*x^2)^-5//2+4//135*x*(3+2*x^2)^-3//2+8//405*x*(3+2*x^2)^-1//2)
@test integrate(x*(1+x^2+a*(1+x^2)^(1/2))^-1, x) == :(log(a+(1+x^2)^(1/2)))
@test integrate((1+x^2)^-3//2*(1+x^2+-1x), x) == :((1+x^2)^-1//2+arcsinh(x))
@test integrate((1+x^2)^(1/2)*(2+x^2)^-1, x) == :(-1//2*2^(1/2)*arctanh((1/2)*x*2^(1/2)*(1+x^2)^-1//2)+arcsinh(x))
@test integrate((1+x^2)^-1//2*(2+x^2)^-2, x) == :(3//8*2^(1/2)*arctanh((1/2)*x*2^(1/2)*(1+x^2)^-1//2)+-1*x*(1+x^2)^(1/2)*(8+4*x^2)^-1)
@test integrate(x^2*(-6+x^2)^-1*(-2+x^2)^-1//2, x) == :(-1//2*6^(1/2)*arctanh(1//3*x*6^(1/2)*(-2+x^2)^-1//2)+arctanh(x*(-2+x^2)^-1//2))
@test integrate((1+x^2)^-2*(1+-1*x^2)^-1//2*(5+x^2), x) == :(2*2^(1/2)*arctan(x*2^(1/2)*(1+-1*x^2)^-1//2)+x*(1+x^2)^-1*(1+-1*x^2)^(1/2))
@test integrate((5+(1+-1*x^2)^(1/2))^-1*(-1*(1+-1*x^2)^(1/2)+4x), x) == :(-1x+-4*(1+-1*x^2)^(1/2)+5*arcsin(x)+20*log(5+(1+-1*x^2)^(1/2))+-25//12*6^(1/2)*arctan(5//12*x*6^(1/2)*(1+-1*x^2)^-1//2)+25//12*6^(1/2)*arctan(1//12*x*6^(1/2)))
@test integrate(x^2*(1+x^2)^-1//2*(1+(1+x^2)^3//2+-1*x^3)^-1*(2+-1*(1+x^2)^(1/2)), x) == :(-41//54*arcsinh(x)+-7//54*log(3+2x+3*x^2)+-1//6*x^2+7//27*arctanh((1/2)*(1+x^2)^-1//2*(1+-1x))+8//9*x+8//9*(1+x^2)^(1/2)+-1//6*x*(1+x^2)^(1/2)+4//27*2^(1/2)*arctan(1//4*2^(1/2)*(1+3x))+4//27*2^(1/2)*arctan((1/2)*2^(1/2)*(1+x^2)^-1//2*(1+x)))
@test integrate(x*(-1*x^2+2*r*x)^(1/2), x) == :(-1//3*(-1*x^2+2*r*x)^3//2+r^3*arctan(x*(-1*x^2+2*r*x)^-1//2)+-1//2*r*(-1*x^2+2*r*x)^(1/2)*(r+-1x))
@test integrate(x^2*(-1*x^2+2*r*x)^(1/2), x) == :(-5//12*r*(-1*x^2+2*r*x)^3//2+-1//4*x*(-1*x^2+2*r*x)^3//2+5//4*r^4*arctan(x*(-1*x^2+2*r*x)^-1//2)+-5//8*r^2*(-1*x^2+2*r*x)^(1/2)*(r+-1x))
@test integrate(x^3*(-1*x^2+2*r*x)^(1/2), x) == :(-7//12*r^2*(-1*x^2+2*r*x)^3//2+-1//5*x^2*(-1*x^2+2*r*x)^3//2+7//4*r^5*arctan(x*(-1*x^2+2*r*x)^-1//2)+-7//8*r^3*(-1*x^2+2*r*x)^(1/2)*(r+-1x)+-7//20*r*x*(-1*x^2+2*r*x)^3//2)
@test integrate((-1+x^2)^-1*(x^2+2x)^-1//2, x) == :(-1//2*arctan((x^2+2x)^(1/2))+-1//6*3^(1/2)*arctanh(1//3*3^(1/2)*(x^2+2x)^-1//2*(1+2x)))
@test integrate((1+x)^-3*(-1*x^2+2x)^-1//2*(-2+3x), x) == :(-2*(3+3x)^-1*(-1*x^2+2x)^(1/2)+-5//6*(1+x)^-2*(-1*x^2+2x)^(1/2)+1//6*3^(1/2)*arctan(1//3*3^(1/2)*(-1*x^2+2x)^-1//2*(1+-2x)))
@test integrate((1+x+x^2)^-1//2, x) == :(arcsinh(1//3*3^(1/2)*(1+2x)))
@test integrate(x^3*(1+x+x^2)^-1//2, x) == :(7//16*arcsinh(1//3*3^(1/2)*(1+2x))+-1*(1+x+x^2)^(1/2)*(1//24+5//12*x)+1//3*x^2*(1+x+x^2)^(1/2))
@test integrate((1+x+x^2)^-3//2, x) == :(1//3*(1+x+x^2)^-1//2*(2+4x))
@test integrate(x*(1+x+x^2)^-3//2, x) == :(-1//3*(1+x+x^2)^-1//2*(4+2x))
@test integrate(x^3*(1+x+x^2)^-3//2, x) == :(-3//2*arcsinh(1//3*3^(1/2)*(1+2x))+(1+x+x^2)^(1/2)*(5//3+2//3*x)+-2//3*x^2*(1+x+x^2)^-1//2*(2+x))
@test integrate(x^2*(1+x+x^2)^(1/2), x) == :(-5//24*(1+x+x^2)^3//2+3//128*arcsinh(1//3*3^(1/2)*(1+2x))+(1+x+x^2)^(1/2)*(1//64+1//32*x)+1//4*x*(1+x+x^2)^3//2)
@test integrate((1+x+x^2)^3//2, x) == :(27//128*arcsinh(1//3*3^(1/2)*(1+2x))+(1+x+x^2)^(1/2)*(9//64+9//32*x)+(1+x+x^2)^3//2*(1//8+1//4*x))
@test integrate((1+x+x^2)^5//2, x) == :(135//1024*arcsinh(1//3*3^(1/2)*(1+2x))+(1+x+x^2)^(1/2)*(45//512+45//256*x)+(1+x+x^2)^3//2*(5//64+5//32*x)+(1+x+x^2)^5//2*(1//12+1//6*x))
@test integrate(x^-2*(1+x+x^2)^-1//2, x) == :((1/2)*arctanh((1/2)*(1+x+x^2)^-1//2*(2+x))+-1*x^-1*(1+x+x^2)^(1/2))
@test integrate(x^-3*(1+x+x^2)^-1//2, x) == :(1//8*arctanh((1/2)*(1+x+x^2)^-1//2*(2+x))+-1//2*x^-2*(1+x+x^2)^(1/2)+3//4*x^-1*(1+x+x^2)^(1/2))
@test integrate(x^-2*(1+x+x^2)^-3//2, x) == :(3//2*arctanh((1/2)*(1+x+x^2)^-1//2*(2+x))+-5//3*x^-1*(1+x+x^2)^(1/2)+1//3*x^-1*(1+x+x^2)^-1//2*(2+-2x))
@test integrate(x^-3*(1+x+x^2)^-3//2, x) == :(-3//8*arctanh((1/2)*(1+x+x^2)^-1//2*(2+x))+-7//6*x^-2*(1+x+x^2)^(1/2)+37//12*x^-1*(1+x+x^2)^(1/2)+1//3*x^-2*(1+x+x^2)^-1//2*(2+-2x))
@test integrate((1+x)^-1*(1+x+x^2)^-1//2, x) == :(-1*arctanh((1/2)*(1+x+x^2)^-1//2*(1+-1x)))
@test integrate((x^3+-1x)^-1*(4+x^2+2x)^-1//2, x) == :((1/2)*arctanh((1/2)*(4+x^2+2x)^-1//2*(4+x))+-1//6*3^(1/2)*arctanh(1//3*3^(1/2)*(4+x^2+2x)^(1/2))+-1//14*7^(1/2)*arctanh(1//7*7^(1/2)*(4+x^2+2x)^-1//2*(5+2x)))
@test integrate((-1+x)^-2*(4+x^2+2x)^(1/2), x) == :((1+-1x)^-1*(4+x^2+2x)^(1/2)+-2//7*7^(1/2)*arctanh(1//7*7^(1/2)*(4+x^2+2x)^-1//2*(5+2x))+arcsinh(1//3*3^(1/2)*(1+x)))
@test integrate((3+x^2+2x)^-2*(4+x^2+2x)^-1//2*(3+2x), x) == :(-1//8*2^(1/2)*arctan((1/2)*2^(1/2)*(4+x^2+2x)^-1//2*(1+x))+-1*(4+x^2+2x)^(1/2)*(12+4*x^2+8x)^-1*(3+-1x)+arctanh((4+x^2+2x)^(1/2)))
@test integrate((-3+x+2*x^2)^-1*(-3+x^2+2x)^-1//2*(2*x^3+3*x^2), x) == :((-3+x^2+2x)^(1/2)+(2+-2x)^-1*(-3+x^2+2x)^(1/2))
@test integrate((1+x+x^2)^-1*(2+x+x^2)^-1//2*(1+x^4), x) == :(-1*arctanh((2+x+x^2)^(1/2))+-7//4*(2+x+x^2)^(1/2)+-1//8*arcsinh(1//7*7^(1/2)*(1+2x))+(1/2)*x*(2+x+x^2)^(1/2)+1//3*3^(1/2)*arctan(1//3*3^(1/2)*(2+x+x^2)^-1//2*(1+2x)))
@test integrate((4+x^2+2x)^-7//2, x) == :(1//15*(4+x^2+2x)^-5//2*(1+x)+1//135*(4+x^2+2x)^-3//2*(4+4x)+1//405*(4+x^2+2x)^-1//2*(8+8x))
@test integrate((1+3*x^2+8x)^-5//2, x) == :(-1//39*(1+3*x^2+8x)^-3//2*(4+3x)+1//169*(1+3*x^2+8x)^-1//2*(8+6x))
@test integrate((5+-3*x^2+4x)^-5//2, x) == :(-1//57*(5+-3*x^2+4x)^-3//2*(2+-3x)+-1//361*(5+-3*x^2+4x)^-1//2*(4+-6x))
@test integrate((1+(2+x^2+2x)^(1/2))^-1, x) == :((1+x)^-1+-1*(1+x)^-1*(2+x^2+2x)^(1/2)+arcsinh(1+x))
@test integrate((x+(1+x+x^2)^(1/2))^-1, x) == :((1+x+x^2)^(1/2)+-1x+2*log(x+(1+x+x^2)^(1/2))+-3//2*arcsinh(1//3*3^(1/2)*(1+2x)))
@test integrate(x^2*(1+2x+2*(1+x+x^2)^(1/2))^-1, x) == :(-5//36*(1+x+x^2)^3//2+-1//6*x^4+-1//9*x^3+1//64*arcsinh(1//3*3^(1/2)*(1+2x))+(1+x+x^2)^(1/2)*(1//96+1//48*x)+1//6*x*(1+x+x^2)^3//2)
@test integrate((-1+(1+x+x^2)^(1/2))^-1*((1+x+x^2)^(1/2)+-3x), x) == :(x+-1*arctanh((1/2)*(1+x+x^2)^-1//2*(2+x))+-4*log(1+x)+-3*(1+x+x^2)^(1/2)+4*arctanh((1/2)*(1+x+x^2)^-1//2*(1+-1x))+5//2*arcsinh(1//3*3^(1/2)*(1+2x))+log(x))
@test integrate(((4+x^2+2x)^(1/2)+-1*(1+x+x^2)^(1/2))^-1*(1+x), x) == :(-2*(1+x+x^2)^(1/2)+-2*(4+x^2+2x)^(1/2)+11//2*arcsinh(1//3*3^(1/2)*(1+x))+43//8*arcsinh(1//3*3^(1/2)*(1+2x))+(1+x+x^2)^(1/2)*(1//4+(1/2)*x)+(4+x^2+2x)^(1/2)*(1/2+(1/2)*x)+-2*7^(1/2)*arctanh(1//14*7^(1/2)*(1+x+x^2)^-1//2*(1+5x))+2*7^(1/2)*arctanh(1//7*7^(1/2)*(4+x^2+2x)^-1//2*(1+-2x)))
@test integrate(x^-3*(-1+x)^-1//2, x) == :(3//4*arctan((-1+x)^(1/2))+(1/2)*x^-2*(-1+x)^(1/2)+3//4*x^-1*(-1+x)^(1/2))
@test integrate(x^-2*(1+-3*x^-1)^-4//3, x) == :(-1*(1+-3*x^-1)^-1//3)
@test integrate(x^-2*(-1+3x)^4//3, x) == :(-6*log(1+(-1+3x)^1//3)+2*log(x)+12*(-1+3x)^1//3+-1*x^-1*(-1+3x)^4//3+4*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2*(-1+3x)^1//3)))
@test integrate(x^2*(4+-3x)^4//3, x) == :(-16//63*(4+-3x)^7//3+-1//117*(4+-3x)^13//3+4//45*(4+-3x)^10//3)
@test integrate(x^-1*(1+-2*x^1//3)^3//4, x) == :(-6*arctanh((1+-2*x^1//3)^1//4)+4*(1+-2*x^1//3)^3//4+6*arctan((1+-2*x^1//3)^1//4))
@test integrate(x*(3+-2*x^(1/2))^-3//4, x) == :(-27//2*(3+-2*x^(1/2))^1//4+-1//2*(3+-2*x^(1/2))^9//4+1//26*(3+-2*x^(1/2))^13//4+27//10*(3+-2*x^(1/2))^5//4)
@test integrate(x^-2*(-1+2*x^(1/2))^5//4, x) == :(-1*x^-1*(-1+2*x^(1/2))^5//4+-5//2*x^-1//2*(-1+2*x^(1/2))^1//4+-5//4*2^(1/2)*arctan(1+-1*2^(1/2)*(-1+2*x^(1/2))^1//4)+-5//8*2^(1/2)*log(1+(-1+2*x^(1/2))^(1/2)+-1*2^(1/2)*(-1+2*x^(1/2))^1//4)+5//4*2^(1/2)*arctan(1+2^(1/2)*(-1+2*x^(1/2))^1//4)+5//8*2^(1/2)*log(1+(-1+2*x^(1/2))^(1/2)+2^(1/2)*(-1+2*x^(1/2))^1//4))
@test integrate(x^6*(1+x^7)^1//3, x) == :(3//28*(1+x^7)^4//3)
@test integrate(x^6*(1+x^7)^-5//3, x) == :(-3//14*(1+x^7)^-2//3)
@test integrate(x^-1*(-27+2*x^7)^-2//3, x) == :(-1//18*log(x)+1//42*log(3+(-27+2*x^7)^1//3)+-1//63*3^(1/2)*arctan(1//9*3^(1/2)*(3+-2*(-27+2*x^7)^1//3)))
@test integrate(x^-8*(1+x^7)^2//3, x) == :(-1//3*log(x)+1//7*log(1+-1*(1+x^7)^1//3)+-1//7*x^-7*(1+x^7)^2//3+2//21*3^(1/2)*arctan(1//3*3^(1/2)*(1+2*(1+x^7)^1//3)))
@test integrate(x^-2*(3+4*x^4)^1//4, x) == :((1/2)*2^(1/2)*arctanh(x*2^(1/2)*(3+4*x^4)^-1//4)+-1*x^-1*(3+4*x^4)^1//4+-1//2*2^(1/2)*arctan(x*2^(1/2)*(3+4*x^4)^-1//4))
@test integrate(x^2*(3+4*x^4)^5//4, x) == :(-45//256*2^(1/2)*arctan(x*2^(1/2)*(3+4*x^4)^-1//4)+1//8*x^3*(3+4*x^4)^5//4+15//32*x^3*(3+4*x^4)^1//4+45//256*2^(1/2)*arctanh(x*2^(1/2)*(3+4*x^4)^-1//4))
@test integrate(x^6*(3+4*x^4)^1//4, x) == :(-27//1024*2^(1/2)*arctanh(x*2^(1/2)*(3+4*x^4)^-1//4)+1//8*x^7*(3+4*x^4)^1//4+3//128*x^3*(3+4*x^4)^1//4+27//1024*2^(1/2)*arctan(x*2^(1/2)*(3+4*x^4)^-1//4))
@test integrate((x*(1+-1*x^2))^1//3, x) == :(-1//4*log(x+(x*(1+-1*x^2))^1//3)+1//12*log(x)+(1/2)*x*(x*(1+-1*x^2))^1//3+1//6*3^(1/2)*arctan(1//3*3^(1/2)*(x*(1+-1*x^2))^-1//3*(-1*(x*(1+-1*x^2))^1//3+2x)))
@test integrate(x^3*(1+2*x^8)^-1//2*(-1+x^4)^-1, x) == :(-1//12*3^(1/2)*arctanh(1//3*3^(1/2)*(1+2*x^8)^-1//2*(1+2*x^4)))
@test integrate(x^9*(1+x^5+x^10)^(1/2), x) == :(-3//80*arcsinh(1//3*3^(1/2)*(1+2*x^5))+1//15*(1+x^5+x^10)^3//2+(1+x^5+x^10)^(1/2)*(-1//40+-1//20*x^5))
@test integrate(x^-5*(4+x^4+2*x^2)^-1//2, x) == :(1//128*arctanh((1/2)*(4+x^4+2*x^2)^-1//2*(4+x^2))+-1//16*x^-4*(4+x^4+2*x^2)^(1/2)+3//64*x^-2*(4+x^4+2*x^2)^(1/2))
@test integrate(x^-1*(1+x^4+3*x^2)^-1//2*(-1+x^2), x) == :(arctanh((1+x^4+3*x^2)^-1//2*(1+x^2)))
@test integrate((x^4+-3*x^2)^3//5*(-3x+2*x^3), x) == :(5//16*(x^4+-3*x^2)^8//5)
@test integrate((-1+3*x^3)^-3//4*(-2*x^5+3*x^8+-1*x^2*(-1+3*x^3)^2//3), x) == :(-4//27*(-1+3*x^3)^1//4+-4//33*(-1+3*x^3)^11//12+4//243*(-1+3*x^3)^9//4)
@test integrate((-1+x^3)^-1*(2+x^3)^-1//3, x) == :(-1//3*3^1//6*arctan(1//3*3^(1/2)+2//3*x*3^5//6*(2+x^3)^-1//3)+-1//18*3^2//3*log(1+x*3^1//3*(2+x^3)^-1//3+3^2//3*x^2*(2+x^3)^-2//3)+1//9*3^2//3*log(1+-1*x*3^1//3*(2+x^3)^-1//3))
@test integrate((1+x^4)^-1*(2+x^4)^-1//4, x) == :(-1//4*2^(1/2)*arctan(1+-1*x*2^(1/2)*(2+x^4)^-1//4)+-1//8*2^(1/2)*log(1+x^2*(2+x^4)^-1//2+-1*x*2^(1/2)*(2+x^4)^-1//4)+1//4*2^(1/2)*arctan(1+x*2^(1/2)*(2+x^4)^-1//4)+1//8*2^(1/2)*log(1+x^2*(2+x^4)^-1//2+x*2^(1/2)*(2+x^4)^-1//4))
@test integrate((2+x^3)^-1//3*(-1+x^3), x) == :(5//6*log((2+x^3)^1//3+-1x)+-5//9*3^(1/2)*arctan(1//3*3^(1/2)*(1+2*x*(2+x^3)^-1//3))+1//3*x*(2+x^3)^2//3)
@test integrate((1+x^4)^3//4*(2+x^4)^-2, x) == :(3//32*2^1//4*arctan((1/2)*x*2^3//4*(1+x^4)^-1//4)+3//32*2^1//4*arctanh((1/2)*x*2^3//4*(1+x^4)^-1//4)+x*(1+x^4)^3//4*(16+8*x^4)^-1)
@test integrate((x^3+3x+3*x^2)^-1*(3+x^3+3x+3*x^2)^-1//3, x) == :(-1//3*3^1//6*arctan(1//3*3^(1/2)+1//3*3^5//6*(2+(1+x)^3)^-1//3*(2+2x))+-1//18*3^2//3*log(1+3^1//3*(2+(1+x)^3)^-1//3*(1+x)+3^2//3*(1+x)^2*(2+(1+x)^3)^-2//3)+1//9*3^2//3*log(1+-1*3^1//3*(2+(1+x)^3)^-1//3*(1+x)))
@test integrate((1+x^2)^-1*(1+x^4)^-1//2*(1+-1*x^2), x) == :((1/2)*2^(1/2)*arctan(x*2^(1/2)*(1+x^4)^-1//2))
@test integrate((1+x^4)^-1//2*(1+-1*x^2)^-1*(1+x^2), x) == :((1/2)*2^(1/2)*arctanh(x*2^(1/2)*(1+x^4)^-1//2))
@test integrate((1+-1*x^2)^-1*(1+x^2+x^4)^-1//2*(1+x^2), x) == :(1//3*3^(1/2)*arctanh(x*3^(1/2)*(1+x^2+x^4)^-1//2))
@test integrate((1+x^2)^-1*(1+x^2+x^4)^-1//2*(1+-1*x^2), x) == :(arctan(x*(1+x^2+x^4)^-1//2))
@test integrate(x^-2*(1+x^2+x^4)^-1//2*(-1+x^4), x) == :(x^-1*(1+x^2+x^4)^(1/2))
@test integrate((1+x^2+2*a*x)^-1*(1+x^4+2*a*x+2*a*x^3+2*b*x^2)^-1//2*(1+-1*x^2), x) == :((1/2)*2^(1/2)*(1+-1b)^-1//2*arctan((1/2)*2^(1/2)*(1+-1b)^-1//2*(1+x^4+2*a*x+2*a*x^3+2*b*x^2)^-1//2*(a+a*x^2+x*(2+-2b+2*a^2))))
@test integrate((1+x^4)^-1*((1+x^4)^(1/2)+-1*x^2)^-1//2, x) == :(arctan(x*((1+x^4)^(1/2)+-1*x^2)^-1//2))
@test integrate((1+x^(2n))^-1*((1+x^(2n))^(n^-1)+-1*x^2)^-1//2, x) == :(arctan(x*((1+x^(2n))^(n^-1)+-1*x^2)^-1//2))
@test integrate(cos(x)^2, x) == :((1/2)*x+(1/2)*cos(x)*sin(x))
@test integrate(cos(x)^3, x) == :(-1//3*sin(x)^3+sin(x))
@test integrate(sin(x)^4, x) == :(3//8*x+-3//8*cos(x)*sin(x)+-1//4*sin(x)^3*cos(x))
@test integrate(cos(x)^6, x) == :(5//16*x+1//6*cos(x)^5*sin(x)+5//16*cos(x)*sin(x)+5//24*cos(x)^3*sin(x))
@test integrate(sin(x)^8, x) == :(35//128*x+-35//128*cos(x)*sin(x)+-35//192*sin(x)^3*cos(x)+-7//48*sin(x)^5*cos(x)+-1//8*sin(x)^7*cos(x))
@test integrate(-1*cos(3x+5//12*pi)^3, x) == :(-1//3*sin(3x+5//12*pi)+1//9*sin(3x+5//12*pi)^3)
@test integrate(sin(x)^-6, x) == :(-1*cot(x)+-2//3*cot(x)^3+-1//5*cot(x)^5)
@test integrate(csc(x)^7, x) == :(-5//16*arctanh(cos(x))+-5//16*cot(x)*csc(x)+-5//24*csc(x)^3*cot(x)+-1//6*csc(x)^5*cot(x))
@test integrate(cos(x)^-12, x) == :(2*tan(x)^5+1//11*tan(x)^11+5//3*tan(x)^3+5//9*tan(x)^9+10//7*tan(x)^7+tan(x))
@test integrate(cos(3x+1//4*pi)^-3, x) == :(1//6*arctanh(sin(3x+1//4*pi))+1//6*sec(3x+1//4*pi)*tan(3x+1//4*pi))
@test integrate(tan(x)^6, x) == :(-1x+-1//3*tan(x)^3+1//5*tan(x)^5+tan(x))
@test integrate(tan(x)^-5, x) == :((1/2)*cot(x)^2+-1//4*cot(x)^4+log(sin(x)))
@test integrate(cot(1//3*x+1//4*pi)^4, x) == :(x+-1*cot(1//3*x+1//4*pi)^3+3*cot(1//3*x+1//4*pi))
@test integrate(cos(x)^6*sin(x)^4, x) == :(3//256*x+-3//80*cos(x)^7*sin(x)+-1//10*cos(x)^7*sin(x)^3+1//128*cos(x)^3*sin(x)+1//160*cos(x)^5*sin(x)+3//256*cos(x)*sin(x))
@test integrate(cos(x)^6*sin(x)^7, x) == :(-3//11*cos(x)^11+-1//7*cos(x)^7+1//3*cos(x)^9+1//13*cos(x)^13)
@test integrate(cos(x)^-1*sin(x)^11, x) == :(-1*log(cos(x))+-5//2*cos(x)^4+-5//8*cos(x)^8+1//10*cos(x)^10+5//2*cos(x)^2+5//3*cos(x)^6)
@test integrate(cos(x)^-6*sin(x)^-6, x) == :(-10*cot(x)+10*tan(x)+-5//3*cot(x)^3+-1//5*cot(x)^5+1//5*tan(x)^5+5//3*tan(x)^3)
@test integrate(cos(x)^2*sin(x)^2, x) == :(1//8*x+-1//4*cos(x)^3*sin(x)+1//8*cos(x)*sin(x))
@test integrate(cos(x)^4*sin(x)^4, x) == :(3//128*x+-1//8*cos(x)^5*sin(x)^3+-1//16*cos(x)^5*sin(x)+1//64*cos(x)^3*sin(x)+3//128*cos(x)*sin(x))
@test integrate(cos(x)^6*sin(x)^6, x) == :(5//1024*x+-1//12*cos(x)^7*sin(x)^5+-1//24*cos(x)^7*sin(x)^3+-1//64*cos(x)^7*sin(x)+1//384*cos(x)^5*sin(x)+5//1024*cos(x)*sin(x)+5//1536*cos(x)^3*sin(x))
@test integrate(cos(x)^8*sin(x)^8, x) == :(35//32768*x+-5//384*cos(x)^9*sin(x)^3+-1//16*cos(x)^9*sin(x)^7+-1//32*cos(x)^9*sin(x)^5+-1//256*cos(x)^9*sin(x)+1//2048*cos(x)^7*sin(x)+7//12288*cos(x)^5*sin(x)+35//32768*cos(x)*sin(x)+35//49152*cos(x)^3*sin(x))
@test integrate(cos(2x+1//4*pi)^-1*sin(2x+1//4*pi)^-3, x) == :((1/2)*log(tan(2x+1//4*pi))+-1//4*cot(2x+1//4*pi)^2)
@test integrate(sec(x)^2*tan(x)^2, x) == :(1//3*tan(x)^3)
@test integrate(cot(x)^3*csc(x), x) == :(-1//3*csc(x)^3+csc(x))
@test integrate(sec(x)^3*tan(x), x) == :(1//3*sec(x)^3)
@test integrate(cot(x)^2*csc(x)^3, x) == :(1//8*arctanh(cos(x))+-1//4*csc(x)^3*cot(x)+1//8*cot(x)*csc(x))
@test integrate(cos(x)^3*sin(x)^-7, x) == :(-1//6*csc(x)^6+1//4*csc(x)^4)
@test integrate(sec(x)^3//2*tan(x)^5, x) == :(-4//7*sec(x)^7//2+2//3*sec(x)^3//2+2//11*sec(x)^11//2)
@test integrate(sec(x)^4*tan(x)^3//2, x) == :(2//5*tan(x)^5//2+2//9*tan(x)^9//2)
@test integrate(cot(x)^4*csc(x)^3, x) == :(-1//16*arctanh(cos(x))+-1//6*cot(x)^3*csc(x)^3+-1//16*cot(x)*csc(x)+1//8*csc(x)^3*cot(x))
@test integrate(sec((1/2)*x+1//4*pi)^3*tan((1/2)*x+1//4*pi)^2, x) == :(-1//4*arctanh(sin((1/2)*x+1//4*pi))+(1/2)*sec((1/2)*x+1//4*pi)^3*tan((1/2)*x+1//4*pi)+-1//4*sec((1/2)*x+1//4*pi)*tan((1/2)*x+1//4*pi))
@test integrate((1+-1//2*sin(x))^4*(4+-3*cos(x)), x) == :(-3*cos(x)^2+-3*sin(x)+10*cos(x)+-3//2*sin(x)^3+-3//80*sin(x)^5+-2//3*cos(x)^3+3//8*sin(x)^4+227//32*x+-99//32*cos(x)*sin(x)+-1//16*sin(x)^3*cos(x))
@test integrate((3+-2*cot(x))^3*(1/2+-3*cot(x)), x) == :((3+-2*cot(x))^3+-42*cot(x)+4*log(sin(x))+5*(3+-2*cot(x))^2+-285//2*x)
@test integrate(cos(x)^-5*cos(5x), x) == :(-15*tan(x)+16x+5//3*tan(x)^3)
@test integrate(cos(x)^-1*cos(4x), x) == :(-8//3*sin(x)^3+arctanh(sin(x)))
@test integrate(cos(x)*cos(4x), x) == :(1//6*sin(3x)+1//10*sin(5x))
@test integrate(cos(x)^-5*cos(4x), x) == :(35//8*arctanh(sin(x))+-29//8*sec(x)*tan(x)+1//4*sec(x)^3*tan(x))
@test integrate(cos(x)^4*cos(4x), x) == :(1//8*sin(2x)+1//16*x+1//24*sin(6x)+1//128*sin(8x)+3//32*sin(4x))
@test integrate(sin(x)^-5*cos(5x), x) == :(6*csc(x)^2+16*log(sin(x))+-1//4*csc(x)^4)
@test integrate(sin(x)^-4*sin(4x), x) == :(-8*log(sin(x))+-2*csc(x)^2)
@test integrate((cos(x)^4+sin(x)^4)^-1*sin(2x), x) == :(-1*arctan(cos(2x)))
@test integrate((4+-3*cos(x)^2+5*sin(x)^2)^-1, x) == :(1//3*x+1//3*arctan(2*(1+2*sin(x)^2)^-1*cos(x)*sin(x)))
@test integrate((4+4*cot(x)+tan(x))^-1, x) == :(2*(10+5*tan(x))^-1+-3//25*log(2*cos(x)+sin(x))+4//25*x)
@test integrate((2*sec(x)+sin(x))^-2, x) == :((30+15*tan(x)+30*tan(x)^2)^-1*(1+4*tan(x))+-8//225*15^(1/2)*arctan((4+15^(1/2)+2*cos(x)*sin(x))^-1*(1+-2*cos(x)^2))+8//225*x*15^(1/2))
@test integrate((2*sec(x)+cos(x))^-2, x) == :((18+12*tan(x)^2)^-1*tan(x)+-1//36*6^(1/2)*arctan((2+6^(1/2)+cos(x)^2)^-1*cos(x)*sin(x))+1//36*x*6^(1/2))
@test integrate((1+3*tan(x))^-3*(5+-1*tan(x)+-6*tan(x)^2), x) == :(-29*(50+150*tan(x))^-1+-67//250*x+-28//125*log(3*sin(x)+cos(x))+-7//10*(1+3*tan(x))^-2)
@test integrate(cos(x)^2*cos(3x)^-1, x) == :((1/2)*arctanh(2*sin(x)))
@test integrate(cos(2x)^-1*sin(x), x) == :((1/2)*2^(1/2)*arctanh(2^(1/2)*cos(x)))
@test integrate(cos(2x)^-1*sin(x)^2, x) == :(-1//2*x+1//4*arctanh(2*cos(x)*sin(x)))
@test integrate(cos(3x)^-1*sin(x)^3, x) == :(-1//24*log(3+-4*cos(x)^2)+1//3*log(cos(x)))
@test integrate(sin(3x)^-1*cos(x), x) == :(-1//6*log(3+-4*sin(x)^2)+1//3*log(sin(x)))
@test integrate(sin(4x)^-1*sin(x), x) == :(-1//4*arctanh(sin(x))+1//4*2^(1/2)*arctanh(2^(1/2)*sin(x)))
@test integrate(sin(x)^3*sin(4x)^-1, x) == :(-1//4*arctanh(sin(x))+1//8*2^(1/2)*arctanh(2^(1/2)*sin(x)))
@test integrate((1+sin(2x))^(1/2), x) == :(-1*(1+sin(2x))^-1//2*cos(2x))
@test integrate((1+-1*sin(2x))^(1/2), x) == :((1+-1*sin(2x))^-1//2*cos(2x))
@test integrate((1+cos(2x))^-1//2, x) == :((1/2)*2^(1/2)*arctanh((1/2)*2^(1/2)*(1+cos(2x))^-1//2*sin(2x)))
@test integrate((1+-1*cos(2x))^-1//2, x) == :(-1//2*2^(1/2)*arctanh((1/2)*2^(1/2)*(1+-1*cos(2x))^-1//2*sin(2x)))
@test integrate((1+-1*cos(3x))^-3//2, x) == :(-1//6*(1+-1*cos(3x))^-3//2*sin(3x)+-1//12*2^(1/2)*arctanh((1/2)*2^(1/2)*(1+-1*cos(3x))^-1//2*sin(3x)))
@test integrate((1+-1*sin(2//3*x))^5//2, x) == :(3//5*(1+-1*sin(2//3*x))^3//2*cos(2//3*x)+8//5*(1+-1*sin(2//3*x))^(1/2)*cos(2//3*x)+32//5*(1+-1*sin(2//3*x))^-1//2*cos(2//3*x))
@test integrate((1+2*sin(x))^-3//2*(-1*cos(x)^2+2*(1+2*sin(x))^1//4)*cos(x), x) == :(-4*(1+2*sin(x))^-1//4+-1//2*(1+2*sin(x))^(1/2)+1//12*(1+2*sin(x))^3//2+3//4*(1+2*sin(x))^-1//2)
@test integrate(tan(x)^(1/2), x) == :((1/2)*2^(1/2)*arctan(1+2^(1/2)*tan(x)^(1/2))+-1//2*2^(1/2)*arctan(1+-1*2^(1/2)*tan(x)^(1/2))+-1//4*2^(1/2)*log(1+2^(1/2)*tan(x)^(1/2)+tan(x))+1//4*2^(1/2)*log(1+-1*2^(1/2)*tan(x)^(1/2)+tan(x)))
@test integrate((4+3*tan(2x))^-3//2, x) == :(-3//25*(4+3*tan(2x))^-1//2+-9//500*2^(1/2)*arctan((1/2)*2^(1/2)*(4+3*tan(2x))^-1//2*(1+-3*tan(2x)))+13//500*2^(1/2)*arctanh((1/2)*2^(1/2)*(4+3*tan(2x))^-1//2*(3+tan(2x))))
@test integrate((4+-3*tan(x))^-3//2*cos(x)^-2*(-1*(4+-3*tan(x))^(1/2)+3*tan(x)), x) == :(1//3*log(4+-3*tan(x))+2//3*(4+-3*tan(x))^(1/2)+8//3*(4+-3*tan(x))^-1//2)
@test integrate((-1+tan(x)^(1/2))^-2*tan(x), x) == :((1+-1*tan(x)^(1/2))^-1+(1/2)*log(cos(x))+-1//2*x+(1/2)*2^(1/2)*arctan((1/2)*2^(1/2)*tan(x)^-1//2*(1+-1*tan(x)))+(1/2)*2^(1/2)*arctanh((1/2)*2^(1/2)*tan(x)^-1//2*(1+tan(x)))+log(1+-1*tan(x)^(1/2)))
@test integrate(sin(2x)^-1//2*sin(x), x) == :(-1//2*arcsin(-1*sin(x)+cos(x))+-1//2*log(sin(2x)^(1/2)+cos(x)+sin(x)))
@test integrate(sin(2x)^-1//2*cos(x), x) == :((1/2)*log(sin(2x)^(1/2)+cos(x)+sin(x))+-1//2*arcsin(-1*sin(x)+cos(x)))
@test integrate(sin(2x)^(1/2)*sin(x), x) == :(-1//4*arcsin(-1*sin(x)+cos(x))+1//4*log(sin(2x)^(1/2)+cos(x)+sin(x))+-1//2*sin(2x)^(1/2)*cos(x))
@test integrate(sin(2x)^(1/2)*(-1*sin(x)+cos(x)), x) == :(-1//2*log(sin(2x)^(1/2)+cos(x)+sin(x))+(1/2)*sin(2x)^(1/2)*cos(x)+(1/2)*sin(2x)^(1/2)*sin(x))
@test integrate(sin(x)^7*sin(2x)^-7//2, x) == :(-1//16*arcsin(-1*sin(x)+cos(x))+1//16*log(sin(2x)^(1/2)+cos(x)+sin(x))+-1//4*sin(2x)^-1//2*sin(x)+1//5*sin(x)^5*sin(2x)^-5//2)
@test integrate(cos(x)^7*sin(2x)^-7//2, x) == :(-1//16*arcsin(-1*sin(x)+cos(x))+-1//16*log(sin(2x)^(1/2)+cos(x)+sin(x))+-1//5*cos(x)^5*sin(2x)^-5//2+1//4*sin(2x)^-1//2*cos(x))
@test integrate(sin(x)^-5*sin(2x)^3//2, x) == :(-1//5*csc(x)^5*sin(2x)^5//2)
@test integrate(cos(x)^-3*sin(2x)^-1//2, x) == :(1//5*sec(x)^3*sin(2x)^(1/2)+4//5*sin(2x)^(1/2)*sec(x))
@test integrate(sin(x)^-1*sin(2x)^-3//2, x) == :(-2//3*sin(2x)^-3//2*cos(x)+4//3*sin(2x)^-1//2*sin(x))
@test integrate((cos(x)^-14*sin(x)^2)^1//3, x) == :(3//5*(sec(x)^12*tan(x)^2)^1//3*cos(x)^3*sin(x)+3//11*(sec(x)^12*tan(x)^2)^1//3*sin(x)^3*cos(x))
@test integrate((cos(x)^11*sin(x)^13)^-1//4, x) == :(-8*(cos(x)^11*sin(x)^13)^-1//4*cos(x)^3*sin(x)^3+-4//9*(cos(x)^11*sin(x)^13)^-1//4*cos(x)^5*sin(x)+4//7*(cos(x)^11*sin(x)^13)^-1//4*sin(x)^5*cos(x))
@test integrate((cos(x)^3*sin(x))^-1//2*(-1*sin(2x)^(1/2)+cos(2x)), x) == :(-1*2^(1/2)*log(-1*2^(1/2)*(cos(x)^3*sin(x))^(1/2)*sec(x)+cos(x)+sin(x))+-1*(cos(x)^3*sin(x))^-1//2*sin(2x)+-1*(cos(x)^3*sin(x))^-1//2*sin(2x)^(1/2)*arcsin(-1*sin(x)+cos(x))*cos(x)+-1*(cos(x)^3*sin(x))^-1//2*sin(2x)^(1/2)*arctanh(sin(x))*cos(x))
@test integrate((sin(x)^2+5*cos(x)^2)^5//2*cos(x), x) == :(625//32*arcsin(2//5*5^(1/2)*sin(x))+1//6*(5+-4*sin(x)^2)^5//2*sin(x)+25//24*(5+-4*sin(x)^2)^3//2*sin(x)+125//16*(5+-4*sin(x)^2)^(1/2)*sin(x))
@test integrate((-1*cos(x)^2+-5*sin(x)^2)^3//2*cos(x), x) == :(3//16*arctan(2*(-1+-4*sin(x)^2)^-1//2*sin(x))+-3//8*(-1+-4*sin(x)^2)^(1/2)*sin(x)+1//4*(-1+-4*sin(x)^2)^3//2*sin(x))
@test integrate((-2*sin(x)^2+5*cos(x)^2)^-7//2*sin(x), x) == :(-1//15*(-2+7*cos(x)^2)^-3//2*cos(x)+1//10*(-2+7*cos(x)^2)^-5//2*cos(x)+1//15*(-2+7*cos(x)^2)^-1//2*cos(x))
@test integrate((2+-5*sin(x)^2)^-3//2*cos(x)*cos(2x), x) == :(1//10*(2+-5*sin(x)^2)^-1//2*sin(x)+2//25*5^(1/2)*arcsin((1/2)*10^(1/2)*sin(x)))
@test integrate((5*cos(x)^2+9*sin(x)^2)^-5//2*sin(5x), x) == :(-1//2*arcsin(2//3*cos(x))+-55//27*(9+-4*cos(x)^2)^-3//2*cos(x)+295//243*(9+-4*cos(x)^2)^-1//2*cos(x))
@test integrate((-5+4*sin(x)^2)^-5//2*cos(x)*cos(2x)*sin(3x), x) == :(-5//8*(-5+4*sin(x)^2)^-1//2+-1//4*(-5+4*sin(x)^2)^-3//2+1//8*(-5+4*sin(x)^2)^(1/2))
@test integrate((2+-3*sin(x)^2)^3//5*sin(4x), x) == :(-20//117*(2+-3*sin(x)^2)^13//5+5//36*(2+-3*sin(x)^2)^8//5)
@test integrate(cos(2x)^(1/2)*cos(x), x) == :((1/2)*cos(2x)^(1/2)*sin(x)+1//4*2^(1/2)*arcsin(2^(1/2)*sin(x)))
@test integrate(cos(2x)^3//2*sin(x), x) == :(-3//16*2^(1/2)*arctanh(2^(1/2)*cos(2x)^-1//2*cos(x))+-1//4*cos(2x)^3//2*cos(x)+3//8*cos(2x)^(1/2)*cos(x))
@test integrate(cos(2x)^-5//2*sin(x), x) == :(-1//3*cos(2x)^-3//2*cos(3x))
@test integrate(cos(x)^-3*cos(2x)^3//2, x) == :(-5//2*arctan(cos(2x)^-1//2*sin(x))+2*2^(1/2)*arcsin(2^(1/2)*sin(x))+-1//2*cos(2x)^(1/2)*sec(x)*tan(x))
@test integrate((4+-5*sec(x)^2)^3//2, x) == :(8*arctan(2*(-1+-5*tan(x)^2)^-1//2*tan(x))+-7//2*5^(1/2)*arctan(5^(1/2)*(-1+-5*tan(x)^2)^-1//2*tan(x))+-5//2*(-1+-5*tan(x)^2)^(1/2)*tan(x))
@test integrate((4+-5*sec(x)^2)^-3//2, x) == :(1//8*arctan(2*(-1+-5*tan(x)^2)^-1//2*tan(x))+-5//4*(-1+-5*tan(x)^2)^-1//2*tan(x))
@test integrate((4+-1*cot(x)^2)^-1//2*cos(x)^-4*(-3+cos(2x)), x) == :(-2//3*(4+-1*cot(x)^2)^(1/2)*tan(x)+-1//3*(4+-1*cot(x)^2)^(1/2)*tan(x)^3)
@test integrate((-2+cos(x)^2)^-1*(5+-4*sec(x)^2)^-3//2*tan(x)^3*(3+sin(x)^2), x) == :(-2//15*(5+-4*sec(x)^2)^-1//2+-1//18*3^(1/2)*arctanh(1//3*3^(1/2)*(5+-4*sec(x)^2)^(1/2))+-1//25*5^(1/2)*arctanh(1//5*5^(1/2)*(5+-4*sec(x)^2)^(1/2)))
@test integrate((4*sec(x)^2+5*tan(x)^2)^-3//2*sin(x)^-2*(sec(x)^2+-3*(4*sec(x)^2+5*tan(x)^2)^(1/2)*tan(x)), x) == :(-3//4*log(tan(x))+3//8*log(4+9*tan(x)^2)+-7//8*(4+9*tan(x)^2)^-1//2*tan(x)+-1//4*(4+9*tan(x)^2)^-1//2*cot(x))
@test integrate((1+5*tan(x)^2)^5//2*tan(x), x) == :(-32*arctan((1/2)*(1+5*tan(x)^2)^(1/2))+16*(1+5*tan(x)^2)^(1/2)+-4//3*(1+5*tan(x)^2)^3//2+1//5*(1+5*tan(x)^2)^5//2)
@test integrate((1+5*tan(x)^2)^-5//2*tan(x), x) == :(-1//12*(1+5*tan(x)^2)^-3//2+1//16*(1+5*tan(x)^2)^-1//2+1//32*arctan((1/2)*(1+5*tan(x)^2)^(1/2)))
@test integrate((a^3+b^3*tan(x)^2)^-1//3*tan(x), x) == :((1/2)*(a^3+-1*b^3)^-1//3*log(cos(x))+3//4*(a^3+-1*b^3)^-1//3*log((a^3+-1*b^3)^1//3+-1*(a^3+b^3*tan(x)^2)^1//3)+(1/2)*3^(1/2)*(a^3+-1*b^3)^-1//3*arctan(1//3*3^(1/2)*(1+2*(a^3+-1*b^3)^-1//3*(a^3+b^3*tan(x)^2)^1//3)))
@test integrate((1+-7*tan(x)^2)^2//3*tan(x), x) == :(2*log(cos(x))+3*log(2+-1*(1+-7*tan(x)^2)^1//3)+3//4*(1+-7*tan(x)^2)^2//3+2*3^(1/2)*arctan(1//3*3^(1/2)*(1+(1+-7*tan(x)^2)^1//3)))
@test integrate((a^4+b^4*csc(x)^2)^-1//4*cot(x), x) == :(a^-1*arctanh(a^-1*(a^4+b^4*csc(x)^2)^1//4)+-1*a^-1*arctan(a^-1*(a^4+b^4*csc(x)^2)^1//4))
@test integrate((a^4+-1*b^4*csc(x)^2)^-1//4*cot(x), x) == :(a^-1*arctanh(a^-1*(a^4+-1*b^4*csc(x)^2)^1//4)+-1*a^-1*arctan(a^-1*(a^4+-1*b^4*csc(x)^2)^1//4))
@test integrate((tan(x)*tan(2x))^-3//2*cos(x)^-2*(-1*cos(2x)+2*tan(x)^2), x) == :(2*arctanh((tan(x)*tan(2x))^-1//2*tan(x))+(1/2)*(tan(x)*tan(2x))^-3//2*tan(x)+-11//8*2^(1/2)*arctanh(2^(1/2)*(tan(x)*tan(2x))^-1//2*tan(x))+2//3*(tan(x)*tan(2x))^-3//2*tan(x)^3+3//4*(tan(x)*tan(2x))^-1//2*tan(x))
@test integrate((a^3+-1*b^3*cos(x)^n)^-4//3*tan(x), x) == :((1/2)*a^-4*log(cos(x))+-3*a^-3*n^-1*(a^3+-1*b^3*cos(x)^n)^-1//3+-3//2*a^-4*n^-1*log(a+-1*(a^3+-1*b^3*cos(x)^n)^1//3)+-1*3^(1/2)*a^-4*n^-1*arctan(1//3*3^(1/2)*a^-1*(a+2*(a^3+-1*b^3*cos(x)^n)^1//3)))
@test integrate((2+-5*sin(x)^3)^-4//3*sin(x)^9*cot(x), x) == :(-1//625*(2+-5*sin(x)^3)^5//3+2//125*(2+-5*sin(x)^3)^2//3+4//125*(2+-5*sin(x)^3)^-1//3)
@test integrate((1+-8*tan(x)^2)^-2//3*cos(x)^-2*(1+(1+-8*tan(x)^2)^1//3)*tan(x), x) == :(-3//32*(1+(1+-8*tan(x)^2)^1//3)^2)
@test integrate(cos(x)^4*cos(2x)^2//3*tan(x), x) == :(-3//40*cos(2x)^5//3+-3//64*cos(2x)^8//3)
@test integrate(cos(2x)^-3//4*sin(x)^6*tan(x), x) == :(-1//5*cos(2x)^5//4+1//36*cos(2x)^9//4+7//4*cos(2x)^1//4+(1/2)*2^(1/2)*arctan((1/2)*2^(1/2)*cos(2x)^-1//4*(1+-1*cos(2x)^(1/2)))+-1//2*2^(1/2)*arctanh((1/2)*2^(1/2)*cos(2x)^-1//4*(1+cos(2x)^(1/2))))
@test integrate((cot(x)^-1*cot(2x))^(1/2), x) == :(-1//2*2^(1/2)*arcsin(tan(x))+arctan(2^(1/2)*(1+-1*tan(x)^2)^-1//2*tan(x)))
@test integrate(x^-5*(5+x^2)^-1, x) == :(-1//20*x^-4+-1//250*log(5+x^2)+1//50*x^-2+1//125*log(x))
@test integrate(x^-6*(5+x^2)^-1, x) == :(-1//25*x^-5+-1//125*x^-1+1//75*x^-3+-1//625*5^(1/2)*arctan(1//5*x*5^(1/2)))
@test integrate(x^-1*(-4+x^2)^-4, x) == :((512+-128*x^2)^-1+-1//512*log(4+-1*x^2)+1//24*(4+-1*x^2)^-3+1//64*(4+-1*x^2)^-2+1//256*log(x))
@test integrate(x^-1*(-2+x^2)^-5//2, x) == :(-1//6*(-2+x^2)^-3//2+1//4*(-2+x^2)^-1//2+1//8*2^(1/2)*arctan((1/2)*2^(1/2)*(-2+x^2)^(1/2)))
@test integrate(x^-1*(-10+x^2)^5//2, x) == :(100*(-10+x^2)^(1/2)+-10//3*(-10+x^2)^3//2+1//5*(-10+x^2)^5//2+-100*10^(1/2)*arctan(1//10*10^(1/2)*(-10+x^2)^(1/2)))
@test integrate(x^(1+2n), x) == :(x^(2+2n)*(2+2n)^-1)
@test integrate(x^7*(-5+x^2)^-3, x) == :((1/2)*x^2+75*(10+-2*x^2)^-1+-125//4*(5+-1*x^2)^-2+15//2*log(5+-1*x^2))
@test integrate((-1+x^2)^-5*(-4*x^3+3*x^5), x) == :(-3//4*(1+-1*x^2)^-2+1//3*(1+-1*x^2)^-3+1//8*(1+-1*x^2)^-4)
@test integrate(x^3*(1+x^2)^9//14, x) == :(-7//23*(1+x^2)^23//14+7//37*(1+x^2)^37//14)
@test integrate(x^5*(-4+x^2)^-13//6, x) == :(-24*(-4+x^2)^-1//6+-48//7*(-4+x^2)^-7//6+3//5*(-4+x^2)^5//6)
@test integrate((1+2*x^2)^-5//2, x) == :(1//3*x*(1+2*x^2)^-3//2+2//3*x*(1+2*x^2)^-1//2)
@test integrate((-1+x^2+-2x)^-5//2, x) == :(-1//6*(-1+x^2+-2x)^-1//2*(1+-1x)+1//6*(-1+x^2+-2x)^-3//2*(1+-1x))
@test integrate(x^-4*(-8+x^2)^-3//2, x) == :(-1//192*x*(-8+x^2)^-1//2+1//24*x^-3*(-8+x^2)^-1//2+1//48*x^-1*(-8+x^2)^-1//2)
@test integrate(x^-13//3*(5+x^2)^2, x) == :(-15//2*x^-10//3+-15//2*x^-4//3+3//2*x^2//3)
@test integrate(x^-7*(1+x^2)^-3, x) == :(-10*log(x)+-3*x^-2+-2*(1+x^2)^-1+5*log(1+x^2)+-1//4*(1+x^2)^-2+-1//6*x^-6+3//4*x^-4)
@test integrate((x^-2*(2+x^2))^7//9*(2+x^2)^-3//2, x) == :(-9//10*x*(1+2*x^-2)^7//9*(2+x^2)^-1//2)
@test integrate(x^2*(3+-1*x^2)^-3//2, x) == :(-1*arcsin(1//3*x*3^(1/2))+x*(3+-1*x^2)^-1//2)
@test integrate(x^-4*(25+-1*x^2)^3//2, x) == :(x^-1*(25+-1*x^2)^(1/2)+-1//3*x^-3*(25+-1*x^2)^3//2+arcsin(1//5*x))
@test integrate((1+-2*x^2)^-7//2, x) == :(1//5*x*(1+-2*x^2)^-5//2+4//15*x*(1+-2*x^2)^-3//2+8//15*x*(1+-2*x^2)^-1//2)
@test integrate((-7+-1*x^2+6x)^-5//2, x) == :(-1//6*(-7+-1*x^2+6x)^-3//2*(3+-1x)+-1//6*(-7+-1*x^2+6x)^-1//2*(3+-1x))
@test integrate((1+-2x+-2*x^2)^3, x) == :(x+-4*x^6+-3*x^2+2*x^3+4*x^4+-12//5*x^5+-8//7*x^7)
@test integrate((-1+x^2+-1x)^2*(-1+5x), x) == :(-1x+-11//5*x^5+-3//4*x^4+3//2*x^2+5//6*x^6+11//3*x^3)
@test integrate((1+-8x+2*x^2)^-5//2*(1+3x), x) == :(-1//21*(1+-8x+2*x^2)^-1//2*(4+-2x)+1//6*(1+-8x+2*x^2)^-3//2*(1+-2x))
@test integrate((1+-4*x^2+2x)^-5//2*(-1+-8x+8*x^3), x) == :(-1//15*(1+-4*x^2+2x)^-3//2*(4+4x)+-1//75*(1+-4*x^2+2x)^-1//2*(7+122x))
@test integrate(x^2*cos(x)^5, x) == :(-298//225*sin(x)+-2//125*sin(x)^5+76//675*sin(x)^3+2//25*x*cos(x)^5+8//15*x^2*sin(x)+8//45*x*cos(x)^3+16//15*x*cos(x)+1//5*x^2*cos(x)^4*sin(x)+4//15*x^2*cos(x)^2*sin(x))
@test integrate(x^3*sin(x)^3, x) == :(-40//9*sin(x)+-2//27*sin(x)^3+2*x^2*sin(x)+-2//3*x^3*cos(x)+1//3*x^2*sin(x)^3+40//9*x*cos(x)+-1//3*x^3*sin(x)^2*cos(x)+2//9*x*sin(x)^2*cos(x))
@test integrate(x^2*sin(x)^6, x) == :(-245//1152*x+5//48*x^3+1//18*x*sin(x)^6+1//108*sin(x)^5*cos(x)+5//16*x*sin(x)^2+5//48*x*sin(x)^4+65//1728*sin(x)^3*cos(x)+245//1152*cos(x)*sin(x)+-5//16*x^2*cos(x)*sin(x)+-5//24*x^2*sin(x)^3*cos(x)+-1//6*x^2*sin(x)^5*cos(x))
@test integrate(x^2*sin(x)^2*cos(x), x) == :(-4//9*sin(x)+-2//27*sin(x)^3+1//3*x^2*sin(x)^3+4//9*x*cos(x)+2//9*x*sin(x)^2*cos(x))
@test integrate(x*cos(x)^4*sin(x)^-2, x) == :(-3//4*x^2+-1//4*cos(x)^2+-1*x*cot(x)+-1//2*x*cos(x)*sin(x)+log(sin(x)))
@test integrate(x*cos(x)^-4*sin(x)^3, x) == :(5//6*arctanh(sin(x))+-1*x*sec(x)+-1//6*sec(x)*tan(x)+1//3*x*sec(x)^3)
@test integrate(x*cos(x)^-3*sin(x), x) == :(-1//2*tan(x)+(1/2)*x*sec(x)^2)
@test integrate(x*cos(x)^-1*sin(x)^3, x) == :(1//4*x+(1/2)*im*x^2+(1/2)*im*Polylogarithms.polylog(2,-1*exp(2*im*x))+-1*x*log(1+exp(2*im*x))+-1//2*x*sin(x)^2+-1//4*cos(x)*sin(x))
@test integrate(x*cos(x)^-3*sin(x)^3, x) == :((1/2)*x+-1//2*tan(x)+x*log(1+exp(2*im*x))+(1/2)*x*tan(x)^2+-1//2*im*x^2+-1//2*im*Polylogarithms.polylog(2,-1*exp(2*im*x)))
@test integrate((x*sin(x)+cos(x))^-2*(2x+sin(2x)), x) == :(2*(1+x^-1*cot(x))^-1)
@test integrate(x^2*(-1*sin(x)+x*cos(x))^-2, x) == :(-1*cot(x)+x*(-1*sin(x)+x*cos(x))^-1*csc(x))
@test integrate(a^(m*x)*b^(n*x), x) == :(a^(m*x)*b^(n*x)*(m*log(a)+n*log(b))^-1)
@test integrate(-1*exp(-1x)+exp(x), x) == :(exp(x)+exp(-1x))
@test integrate((-1*exp(-1x)+exp(x))^2, x) == :((1/2)*exp(2x)+-2x+-1//2*exp(-2x))
@test integrate((-1*exp(-1x)+exp(x))^3, x) == :(-3*exp(x)+-3*exp(-1x)+1//3*exp(-3x)+1//3*exp(3x))
@test integrate((-1*exp(-1x)+exp(x))^4, x) == :(-2*exp(2x)+2*exp(-2x)+6x+-1//4*exp(-4x)+1//4*exp(4x))
@test integrate((a^(-4x)+-1*a^(2x))^3, x) == :(3x+(1/2)*a^(-6x)*log(a)^-1+-1//6*a^(6x)*log(a)^-1+-1//12*a^(-12x)*log(a)^-1)
@test integrate(a^(k*x)+a^(l*x), x) == :(a^(k*x)*k^-1*log(a)^-1+a^(l*x)*l^-1*log(a)^-1)
@test integrate((a^(k*x)+a^(l*x))^2, x) == :((1/2)*a^(2*k*x)*k^-1*log(a)^-1+(1/2)*a^(2*l*x)*l^-1*log(a)^-1+2*a^(x*(k+l))*(k+l)^-1*log(a)^-1)
@test integrate((a^(k*x)+a^(l*x))^3, x) == :(3*a^(x*(k+2l))*(k+2l)^-1*log(a)^-1+3*a^(x*(l+2k))*(l+2k)^-1*log(a)^-1+1//3*a^(3*k*x)*k^-1*log(a)^-1+1//3*a^(3*l*x)*l^-1*log(a)^-1)
@test integrate((a^(k*x)+a^(l*x))^4, x) == :(3*a^(x*(2k+2l))*(k+l)^-1*log(a)^-1+4*a^(x*(k+3l))*(k+3l)^-1*log(a)^-1+4*a^(x*(l+3k))*(l+3k)^-1*log(a)^-1+1//4*a^(4*k*x)*k^-1*log(a)^-1+1//4*a^(4*l*x)*l^-1*log(a)^-1)
@test integrate(a^(k*x)+-1*a^(l*x), x) == :(a^(k*x)*k^-1*log(a)^-1+-1*a^(l*x)*l^-1*log(a)^-1)
@test integrate((a^(k*x)+-1*a^(l*x))^2, x) == :((1/2)*a^(2*k*x)*k^-1*log(a)^-1+(1/2)*a^(2*l*x)*l^-1*log(a)^-1+-2*a^(x*(k+l))*(k+l)^-1*log(a)^-1)
@test integrate((a^(k*x)+-1*a^(l*x))^3, x) == :(-3*a^(x*(l+2k))*(l+2k)^-1*log(a)^-1+3*a^(x*(k+2l))*(k+2l)^-1*log(a)^-1+-1//3*a^(3*l*x)*l^-1*log(a)^-1+1//3*a^(3*k*x)*k^-1*log(a)^-1)
@test integrate((a^(k*x)+-1*a^(l*x))^4, x) == :(-4*a^(x*(k+3l))*(k+3l)^-1*log(a)^-1+-4*a^(x*(l+3k))*(l+3k)^-1*log(a)^-1+3*a^(x*(2k+2l))*(k+l)^-1*log(a)^-1+1//4*a^(4*k*x)*k^-1*log(a)^-1+1//4*a^(4*l*x)*l^-1*log(a)^-1)
@test integrate(1+a^(m*x), x) == :(x+a^(m*x)*m^-1*log(a)^-1)
@test integrate((1+a^(m*x))^2, x) == :(x+(1/2)*a^(2*m*x)*m^-1*log(a)^-1+2*a^(m*x)*m^-1*log(a)^-1)
@test integrate((1+a^(m*x))^3, x) == :(x+3*a^(m*x)*m^-1*log(a)^-1+1//3*a^(3*m*x)*m^-1*log(a)^-1+3//2*a^(2*m*x)*m^-1*log(a)^-1)
@test integrate((1+a^(m*x))^4, x) == :(x+3*a^(2*m*x)*m^-1*log(a)^-1+4*a^(m*x)*m^-1*log(a)^-1+1//4*a^(4*m*x)*m^-1*log(a)^-1+4//3*a^(3*m*x)*m^-1*log(a)^-1)
@test integrate(1+-1*a^(m*x), x) == :(x+-1*a^(m*x)*m^-1*log(a)^-1)
@test integrate((1+-1*a^(m*x))^2, x) == :(x+(1/2)*a^(2*m*x)*m^-1*log(a)^-1+-2*a^(m*x)*m^-1*log(a)^-1)
@test integrate((1+-1*a^(m*x))^3, x) == :(x+-3*a^(m*x)*m^-1*log(a)^-1+-1//3*a^(3*m*x)*m^-1*log(a)^-1+3//2*a^(2*m*x)*m^-1*log(a)^-1)
@test integrate((1+-1*a^(m*x))^4, x) == :(x+-4*a^(m*x)*m^-1*log(a)^-1+3*a^(2*m*x)*m^-1*log(a)^-1+-4//3*a^(3*m*x)*m^-1*log(a)^-1+1//4*a^(4*m*x)*m^-1*log(a)^-1)
@test integrate((b+a*exp(n*x))^-1, x) == :(x*b^-1+-1*b^-1*n^-1*log(b+a*exp(n*x)))
@test integrate((1+exp(x))^-1*(-1+exp(x)), x) == :(-1x+2*log(1+exp(x)))
@test integrate((1+-2*exp(2x)+3*exp(4x))^-1*exp(4x), x) == :(1//12*log(1+-2*exp(2x)+3*exp(4x))+-1//12*2^(1/2)*arctan((1/2)*2^(1/2)*(1+-3*exp(2x))))
@test integrate((-1+-1*exp(2x)+exp(x)+exp(3x))^-1*(exp(x)+exp(5x)), x) == :((1/2)*exp(2x)+-1*arctan(exp(x))+-1//2*log(1+exp(2x))+exp(x)+log(1+-1*exp(x)))
@test integrate((a+b*exp(n*x))^(r*s^-1)*exp(n*x), x) == :(s*b^-1*n^-1*(a+b*exp(n*x))^(s^-1*(r+s))*(r+s)^-1)
@test integrate((1+-2*exp(1//3*x))^1//4, x) == :(-6*arctan((1+-2*exp(1//3*x))^1//4)+-6*arctanh((1+-2*exp(1//3*x))^1//4)+12*(1+-2*exp(1//3*x))^1//4)
@test integrate((a^2+exp(2x))^-1//2*exp(x), x) == :(arctanh((a^2+exp(2x))^-1//2*exp(x)))
@test integrate((-1*a^2+exp(2x))^-1//2*exp(x), x) == :(arctanh((-1*a^2+exp(2x))^-1//2*exp(x)))
@test integrate((-2+exp(3//4*x))^-1*(-2+exp(3//2*x)+exp(3//4*x))^-1//2*exp(3//4*x), x) == :(2//3*arctanh(1//4*(-2+exp(3//2*x)+exp(3//4*x))^-1//2*(2+-5*exp(3//4*x))))
@test integrate((3+-1*exp((1/2)*x))^-3//4*exp(2x), x) == :(-216*(3+-1*exp((1/2)*x))^1//4+-8*(3+-1*exp((1/2)*x))^9//4+8//13*(3+-1*exp((1/2)*x))^13//4+216//5*(3+-1*exp((1/2)*x))^5//4)
@test integrate(x^3*exp(-1//2*x), x) == :(-96*exp(-1//2*x)+-48*x*exp(-1//2*x)+-12*x^2*exp(-1//2*x)+-2*x^3*exp(-1//2*x))
@test integrate(x^-3*exp(-1//2*x), x) == :(1//8*expintegralei(-1//2*x)+-1//2*x^-2*exp(-1//2*x)+1//4*x^-1*exp(-1//2*x))
@test integrate(a^(3x)*x^2, x) == :(2//27*a^(3x)*log(a)^-3+-2//9*x*a^(3x)*log(a)^-2+1//3*a^(3x)*x^2*log(a)^-1)
@test integrate(x*(1+x^2)*exp(x^2), x) == :((1/2)*x^2*exp(x^2))
@test integrate(x*(exp(x)+exp(-1x))^-2, x) == :((1/2)*x+-1//4*log(1+exp(2x))+-1*x*(2+2*exp(2x))^-1)
@test integrate((1+-1*x^2)^-1//2*(1+-1x+-1*x^2)*exp(x), x) == :((1+-1*x^2)^(1/2)*exp(x))
@test integrate(cos(2x)*exp(-3x), x) == :(-3//13*cos(2x)*exp(-3x)+2//13*exp(-3x)*sin(2x))
@test integrate(exp(x)^-1//3*(cos((1/2)*x)+sin((1/2)*x)), x) == :(-30//13*exp(x)^-1//3*cos((1/2)*x)+6//13*exp(x)^-1//3*sin((1/2)*x))
@test integrate((3^(3x))^-1//4*cos(3//2*x), x) == :(8//3*(3^(3x))^-1//4*(4+log(3)^2)^-1*sin(3//2*x)+-4//3*(3^(3x))^-1//4*(4+log(3)^2)^-1*cos(3//2*x)*log(3))
@test integrate(cos(x)^2*exp(m*x), x) == :(2*m^-1*(4+m^2)^-1*exp(m*x)+m*(4+m^2)^-1*cos(x)^2*exp(m*x)+2*(4+m^2)^-1*cos(x)*exp(m*x)*sin(x))
@test integrate(sin(x)^3*exp(m*x), x) == :(-6*(9+m^4+10*m^2)^-1*cos(x)*exp(m*x)+m*(9+m^2)^-1*sin(x)^3*exp(m*x)+-3*(9+m^2)^-1*sin(x)^2*cos(x)*exp(m*x)+6*m*(9+m^4+10*m^2)^-1*exp(m*x)*sin(x))
@test integrate(cos(1//3*x)^3*exp(x)^-1//2, x) == :(-48//65*exp(x)^-1//2*cos(1//3*x)+-2//5*cos(1//3*x)^3*exp(x)^-1//2+32//65*exp(x)^-1//2*sin(1//3*x)+4//5*cos(1//3*x)^2*exp(x)^-1//2*sin(1//3*x))
@test integrate(cos(x)^2*sin(x)^2*exp(2x), x) == :(1//16*exp(2x)+-1//40*exp(2x)*sin(4x)+-1//80*cos(4x)*exp(2x))
@test integrate(cos(3//2*x)^2*sin(3//2*x)^2*exp(3x), x) == :(1//24*exp(3x)+-1//60*exp(3x)*sin(6x)+-1//120*cos(6x)*exp(3x))
@test integrate((1+-1*cos(x))^-1*(1+-1*sin(x))*exp(x), x) == :(-1*(1+-1*cos(x))^-1*exp(x)*sin(x))
@test integrate((1+cos(x))^-1*(1+sin(x))*exp(x), x) == :((1+cos(x))^-1*exp(x)*sin(x))
@test integrate((1+-1*sin(x))^-1*(1+cos(x))*exp(x), x) == :((1+-1*sin(x))^-1*cos(x)*exp(x))
@test integrate((1+sin(x))^-1*(1+-1*cos(x))*exp(x), x) == :(-1*(1+sin(x))^-1*cos(x)*exp(x))
@test integrate(x*cos(x)*exp(x), x) == :(-1//2*exp(x)*sin(x)+(1/2)*x*cos(x)*exp(x)+(1/2)*x*exp(x)*sin(x))
@test integrate(x^2*exp(x)*sin(x), x) == :(-1//2*cos(x)*exp(x)+-1//2*exp(x)*sin(x)+x*cos(x)*exp(x)+(1/2)*x^2*exp(x)*sin(x)+-1//2*x^2*cos(x)*exp(x))
@test integrate(x^2*exp(-3x)*sin(x), x) == :(-13//250*cos(x)*exp(-3x)+-9//250*exp(-3x)*sin(x)+-4//25*x*exp(-3x)*sin(x)+-3//10*x^2*exp(-3x)*sin(x)+-3//25*x*cos(x)*exp(-3x)+-1//10*x^2*cos(x)*exp(-3x))
@test integrate(x^2*cos(x)^3*exp((1/2)*x), x) == :(-792//50653*exp((1/2)*x)*sin(3x)+-428//50653*cos(3x)*exp((1/2)*x)+-132//125*cos(x)*exp((1/2)*x)+-24//125*exp((1/2)*x)*sin(x)+-24//25*x*exp((1/2)*x)*sin(x)+-24//1369*x*exp((1/2)*x)*sin(3x)+2//37*x^2*cos(x)^3*exp((1/2)*x)+18//25*x*cos(x)*exp((1/2)*x)+48//185*x^2*cos(x)*exp((1/2)*x)+70//1369*x*cos(3x)*exp((1/2)*x)+96//185*x^2*exp((1/2)*x)*sin(x)+12//37*x^2*cos(x)^2*exp((1/2)*x)*sin(x))
@test integrate(x^2*exp(2x)*sin(4x), x) == :(-11//500*exp(2x)*sin(4x)+1//250*cos(4x)*exp(2x)+-1//5*x^2*cos(4x)*exp(2x)+1//10*x^2*exp(2x)*sin(4x)+2//25*x*cos(4x)*exp(2x)+3//50*x*exp(2x)*sin(4x))
@test integrate(x^2*sin(x)^2*cos(x)*exp((1/2)*x), x) == :(-44//125*cos(x)*exp((1/2)*x)+-8//125*exp((1/2)*x)*sin(x)+428//50653*cos(3x)*exp((1/2)*x)+792//50653*exp((1/2)*x)*sin(3x)+-70//1369*x*cos(3x)*exp((1/2)*x)+-8//25*x*exp((1/2)*x)*sin(x)+-3//37*x^2*exp((1/2)*x)*sin(3x)+-1//74*x^2*cos(3x)*exp((1/2)*x)+1//5*x^2*exp((1/2)*x)*sin(x)+1//10*x^2*cos(x)*exp((1/2)*x)+6//25*x*cos(x)*exp((1/2)*x)+24//1369*x*exp((1/2)*x)*sin(3x))
@test integrate(cosh(x), x) == :(sinh(x))
@test integrate(sinh(x), x) == :(cosh(x))
@test integrate(tanh(x), x) == :(log(cosh(x)))
@test integrate(coth(x), x) == :(log(sinh(x)))
@test integrate(sech(x), x) == :(arctan(sinh(x)))
@test integrate(csch(x), x) == :(-1*arctanh(cosh(x)))
@test integrate(cosh(x)^2, x) == :((1/2)*x+(1/2)*cosh(x)*sinh(x))
@test integrate(sinh(x)^5, x) == :(-2//3*cosh(x)^3+1//5*cosh(x)^5+cosh(x))
@test integrate(tanh(x)^4, x) == :(x+-1*tanh(x)+-1//3*tanh(x)^3)
@test integrate(csch(x)^3, x) == :((1/2)*arctanh(cosh(x))+-1//2*coth(x)*csch(x))
@test integrate(cosh(x)^-5, x) == :(3//8*arctan(sinh(x))+1//4*sech(x)^3*tanh(x)+3//8*sech(x)*tanh(x))
@test integrate(sech(x)^-4*tanh(x)^5, x) == :(-1*cosh(x)^2+1//4*cosh(x)^4+log(cosh(x)))
@test integrate(sech(x)^3//4*tanh(x)^5, x) == :(-4//3*sech(x)^3//4+-4//19*sech(x)^19//4+8//11*sech(x)^11//4)
@test integrate((1+cosh(x))^-2, x) == :((3+3*cosh(x))^-1*sinh(x)+1//3*(1+cosh(x))^-2*sinh(x))
@test integrate((a+b*tanh(x))^-1, x) == :(a*x*(a^2+-1*b^2)^-1+-1*b*(a^2+-1*b^2)^-1*log(a*cosh(x)+b*sinh(x)))
@test integrate((a^2+b^2*cosh(x)^2)^-1, x) == :(a^-1*(a^2+b^2)^-1//2*arctanh(a*(a^2+b^2)^-1//2*tanh(x)))
@test integrate((a^2+-1*b^2*cosh(x)^2)^-1, x) == :(a^-1*(a^2+-1*b^2)^-1//2*arctanh(a*(a^2+-1*b^2)^-1//2*tanh(x)))
@test integrate((1+-1*sinh(x)^4)^-1, x) == :((1/2)*tanh(x)+1//4*2^(1/2)*arctanh(2^(1/2)*tanh(x)))
@test integrate((cosh(x)^3+sinh(x)^3)^-1*(cosh(x)^3+-1*sinh(x)^3), x) == :(-1*(3+3*tanh(x))^-1+-4//9*3^(1/2)*arctan(1//3*3^(1/2)*(1+-2*tanh(x))))
@test integrate(cosh(x)*cosh(2x)*cosh(3x), x) == :(1//4*x+1//8*sinh(2x)+1//16*sinh(4x)+1//24*sinh(6x))
@test integrate(cosh(3//2*x)*sinh(x)*sinh(5//2*x), x) == :(-1//4*x+-1//12*sinh(3x)+1//8*sinh(2x)+1//20*sinh(5x))
@test integrate((-9+4*cosh(x)^2)^-5//2*sinh(x), x) == :(-1//27*(-9+4*cosh(x)^2)^-3//2*cosh(x)+2//243*(-9+4*cosh(x)^2)^-1//2*cosh(x))
@test integrate((1+-1*sinh(x)^2)^-3//2*sinh(x)^2*sinh(2x), x) == :(2*(1+-1*sinh(x)^2)^(1/2)+2*(1+-1*sinh(x)^2)^-1//2)
@test integrate(cosh(2x)^-1//2*cosh(x), x) == :((1/2)*2^(1/2)*arcsinh(2^(1/2)*sinh(x)))
@test integrate(x*tanh(x)^2, x) == :((1/2)*x^2+-1*x*tanh(x)+log(cosh(x)))
@test integrate(x*coth(x)^2, x) == :((1/2)*x^2+-1*x*coth(x)+log(sinh(x)))
@test integrate((-1*sinh(x)+cosh(x))^-1*(x+cosh(x)+sinh(x)), x) == :((1/2)*exp(2x)+-1*exp(x)+x*exp(x))
@test integrate((1+cosh(x))^-1*(x+cosh(x)+sinh(x)), x) == :(x+-1*(1+-1x)*tanh((1/2)*x))
@test integrate(sinh(x)^-4*exp(2x), x) == :(8//3*(1+-1*exp(2x))^-3*exp(6x))
@test integrate(cosh(x)^-4*exp(-2x), x) == :(-8//3*(1+exp(2x))^-3)
@test integrate((-1*sinh(x)+cosh(x))^-1*exp(x), x) == :((1/2)*exp(2x))
@test integrate((cosh(x)+sinh(x))^-1*exp(x), x) == :(x)
@test integrate((1+-1*cosh(x))^-1*exp(x), x) == :(-2*(1+-1*exp(x))^-1+-2*log(1+-1*exp(x)))
@test integrate((1+cosh(x))^-1*(1+sinh(x))*exp(x), x) == :(2*(1+exp(x))^-1+exp(x))
@test integrate((1+-1*cosh(x))^-1*(1+-1*sinh(x))*exp(x), x) == :(-2*(1+-1*exp(x))^-1+exp(x))
@test integrate(x^m*log(x), x) == :(-1*x^(1+m)*(1+m)^-2+x^(1+m)*(1+m)^-1*log(x))
@test integrate(x^m*log(x)^2, x) == :(2*x^(1+m)*(1+m)^-3+x^(1+m)*(1+m)^-1*log(x)^2+-2*x^(1+m)*(1+m)^-2*log(x))
@test integrate(x^-5//2*log(x)^2, x) == :(-16//27*x^-3//2+-8//9*x^-3//2*log(x)+-2//3*x^-3//2*log(x)^2)
@test integrate((a+b*x)*log(x), x) == :(-1*a*x+-1//4*b*x^2+a*x*log(x)+(1/2)*b*x^2*log(x))
@test integrate((a+b*x)^3*log(x), x) == :(-1*x*a^3+-1//16*b^3*x^4+-3//4*b*a^2*x^2+-1//3*a*b^2*x^3+-1//4*a^4*b^-1*log(x)+1//4*b^-1*(a+b*x)^4*log(x))
@test integrate(-1+-8*log(x)^2+3*log(x)^3, x) == :(-35x+-17*x*log(x)^2+3*x*log(x)^3+34*x*log(x))
@test integrate((1+x^4)*(1+log(x)^3+-2*log(x)), x) == :(-3x+169//625*x^5+x*log(x)^3+-3*x*log(x)^2+4*x*log(x)+-44//125*x^5*log(x)+-3//25*x^5*log(x)^2+1//5*x^5*log(x)^3)
@test integrate(x^-3*log(x)^-4, x) == :(-4//3*expintegralei(-2*log(x))+-2//3*x^-2*log(x)^-1+-1//3*x^-2*log(x)^-3+1//3*x^-2*log(x)^-2)
@test integrate((a+b*x)^-1*log(x), x) == :(b^-1*Polylogarithms.polylog(2,-1*b*x*a^-1)+b^-1*log(x)*log(1+b*x*a^-1))
@test integrate((a+b*x)^-2*log(x), x) == :(-1*a^-1*b^-1*log(a+b*x)+x*a^-1*(a+b*x)^-1*log(x))
@test integrate(x^-1*log(x)^n, x) == :((1+n)^-1*log(x)^(1+n))
@test integrate(x^-1*(a+b*log(x))^n, x) == :(b^-1*(1+n)^-1*(a+b*log(x))^(1+n))
@test integrate(x^-1*(a+b*log(x))^-1, x) == :(b^-1*log(a+b*log(x)))
@test integrate(x^-1*(a+b*log(x))^(-1n), x) == :(b^-1*(1+-1n)^-1*(a+b*log(x))^(1+-1n))
@test integrate(x^-1*(a^2+log(x)^2)^-1//2, x) == :(arctanh((a^2+log(x)^2)^-1//2*log(x)))
@test integrate(x^-1*(log(x)^2+-1*a^2)^-1//2, x) == :(arctanh((log(x)^2+-1*a^2)^-1//2*log(x)))
@test integrate(x^-1*(a^2+-1*log(x)^2)^-1//2, x) == :(arctan((a^2+-1*log(x)^2)^-1//2*log(x)))
@test integrate(x^-1*(a^2+log(x)^2)^-1//2*log(x)^-1, x) == :(-1*a^-1*arctanh(a^-1*(a^2+log(x)^2)^(1/2)))
@test integrate(x^-1*(a^2+-1*log(x)^2)^-1//2*log(x)^-1, x) == :(-1*a^-1*arctanh(a^-1*(a^2+-1*log(x)^2)^(1/2)))
@test integrate(x^-1*(log(x)^2+-1*a^2)^-1//2*log(x)^-1, x) == :(a^-1*arctan(a^-1*(log(x)^2+-1*a^2)^(1/2)))
@test integrate(x^-1*log(log(x)), x) == :(-1*log(x)+log(x)*log(log(x)))
@test integrate(x^-1*log(log(x))^2, x) == :(2*log(x)+log(log(x))^2*log(x)+-2*log(x)*log(log(x)))
@test integrate(x^-1*log(log(x))^3, x) == :(-6*log(x)+log(log(x))^3*log(x)+-3*log(log(x))^2*log(x)+6*log(x)*log(log(x)))
@test integrate(x^-1*log(log(x))^4, x) == :(24*log(x)+log(log(x))^4*log(x)+-24*log(x)*log(log(x))+-4*log(log(x))^3*log(x)+12*log(log(x))^2*log(x))
@test integrate(x^-1*log(log(x))^n, x) == :((-1*log(log(x)))^(-1n)*log(log(x))^n*SpecialFunctions.gamma(1+n,-1*log(log(x))))
@test integrate(log(sin(x))^-1*cot(x), x) == :(log(log(sin(x))))
@test integrate((cos(x)^-1+cos(x))*tan(x), x) == :(-1*cos(x)+sec(x))
@test integrate(log(cosh(x))*sinh(x), x) == :(-1*cosh(x)+cosh(x)*log(cosh(x)))
@test integrate(log(cosh(x))*tanh(x), x) == :((1/2)*log(cosh(x))^2)
@test integrate(log(x+-1*(1+x^2)^(1/2)), x) == :((1+x^2)^(1/2)+x*log(x+-1*(1+x^2)^(1/2)))
@test integrate(x^-3*log(-1+x), x) == :((1/2)*x^-1+(1/2)*log(1+-1x)+-1//2*log(x)+-1//2*x^-2*log(-1+x))
@test integrate((-1*exp(-1x)+exp(x))*log(1+exp(2x)), x) == :(-2*exp(x)+exp(x)*log(1+exp(2x))+exp(-1x)*log(1+exp(2x)))
@test integrate(exp(3//2*x)*log(-1+exp(x)), x) == :(-4//3*exp((1/2)*x)+-4//9*exp(3//2*x)+4//3*arctanh(exp((1/2)*x))+2//3*exp(3//2*x)*log(-1+exp(x)))
@test integrate(cos(x)^3*log(sin(x)), x) == :(-1*sin(x)+1//9*sin(x)^3+log(sin(x))*sin(x)+-1//3*sin(x)^3*log(sin(x)))
@test integrate(cos(x)^-4*log(tan(x)), x) == :(-1*tan(x)+-1//9*tan(x)^3+log(tan(x))*tan(x)+1//3*tan(x)^3*log(tan(x)))
@test integrate((1+cos(x))^-1*log(cos((1/2)*x)), x) == :(-1//2*x+(1+cos(x))^-1*log(cos((1/2)*x))*sin(x)+tan((1/2)*x))
@test integrate((1+cos(x))^-2*cos(x)*log(sin(x)), x) == :(-2//3*x+8*(9+9*cos(x))^-1*sin(x)+-1//9*(1+cos(x))^-2*sin(x)+2*(3+3*cos(x))^-1*log(sin(x))*sin(x)+-1//3*(1+cos(x))^-2*log(sin(x))*sin(x))
@test integrate(x^-5*arccos(x)^2, x) == :(-1//12*x^-2+1//3*log(x)+-1//4*x^-4*arccos(x)^2+1//3*x^-1*(1+-1*x^2)^(1/2)*arccos(x)+1//6*x^-3*(1+-1*x^2)^(1/2)*arccos(x))
@test integrate(x^2*arcsin(x)^2, x) == :(-4//9*x+-2//27*x^3+1//3*x^3*arcsin(x)^2+4//9*(1+-1*x^2)^(1/2)*arcsin(x)+2//9*x^2*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate(x^3*arctan(x)^2, x) == :(-1//3*log(1+x^2)+-1//4*arctan(x)^2+1//12*x^2+(1/2)*x*arctan(x)+-1//6*x^3*arctan(x)+1//4*x^4*arctan(x)^2)
@test integrate(x^-5*arctan(x)^2, x) == :(-2//3*log(x)+-1//12*x^-2+1//3*log(1+x^2)+1//4*arctan(x)^2+(1/2)*x^-1*arctan(x)+-1//4*x^-4*arctan(x)^2+-1//6*x^-3*arctan(x))
@test integrate(x^3*arccsc(x)^2, x) == :(1//3*log(x)+1//12*x^2+1//4*x^4*arccsc(x)^2+1//3*x*(1+-1*x^-2)^(1/2)*arccsc(x)+1//6*x^3*(1+-1*x^-2)^(1/2)*arccsc(x))
@test integrate(x^-5*arcsec(x)^4, x) == :(-45//128*x^-2+-45//128*arcsec(x)^2+-3//128*x^-4+3//32*arcsec(x)^4+-1//4*x^-4*arcsec(x)^4+3//16*x^-4*arcsec(x)^2+9//16*x^-2*arcsec(x)^2+-45//64*x^-1*(1+-1*x^-2)^(1/2)*arcsec(x)+-3//32*x^-3*(1+-1*x^-2)^(1/2)*arcsec(x)+1//4*x^-3*(1+-1*x^-2)^(1/2)*arcsec(x)^3+3//8*x^-1*(1+-1*x^-2)^(1/2)*arcsec(x)^3)
@test integrate((1+-1*x^2)^(1/2)*arcsin(x), x) == :(-1//4*x^2+1//4*arcsin(x)^2+(1/2)*x*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate((1+-1*x^2)^(1/2)*arccos(x), x) == :(-1//4*arccos(x)^2+1//4*x^2+(1/2)*x*(1+-1*x^2)^(1/2)*arccos(x))
@test integrate(x*(1+-1*x^2)^(1/2)*arccos(x), x) == :(-1//3*x+1//9*x^3+-1//3*(1+-1*x^2)^3//2*arccos(x))
@test integrate((1+-1*x^2)^3//2*arcsin(x), x) == :(-5//16*x^2+1//16*x^4+3//16*arcsin(x)^2+1//4*x*(1+-1*x^2)^3//2*arcsin(x)+3//8*x*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate(x*(1+-1*x^2)^3//2*arcsin(x), x) == :(-2//15*x^3+1//5*x+1//25*x^5+-1//5*(1+-1*x^2)^5//2*arcsin(x))
@test integrate(x^3*(1+-1*x^2)^3//2*arccos(x), x) == :(-2//35*x+-1//49*x^7+-1//105*x^3+8//175*x^5+-1//5*(1+-1*x^2)^5//2*arccos(x)+1//7*(1+-1*x^2)^7//2*arccos(x))
@test integrate(x^-1*(1+-1*x^2)^3//2*arccos(x), x) == :(-1//9*x^3+4//3*x+im*Polylogarithms.polylog(2,im*exp(im*arccos(x)))+(1+-1*x^2)^(1/2)*arccos(x)+-1*im*Polylogarithms.polylog(2,-1*im*exp(im*arccos(x)))+1//3*(1+-1*x^2)^3//2*arccos(x)+2*im*arccos(x)*arctan(exp(im*arccos(x))))
@test integrate(x^-6*(1+-1*x^2)^3//2*arcsin(x), x) == :(-1//20*x^-4+1//5*x^-2+1//5*log(x)+-1//5*x^-5*(1+-1*x^2)^5//2*arcsin(x))
@test integrate(x^2*(1+-1*x^2)^-1//2*arcsin(x), x) == :(1//4*x^2+1//4*arcsin(x)^2+-1//2*x*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate(x^4*(1+-1*x^2)^-1//2*arcsin(x), x) == :(1//16*x^4+3//16*x^2+3//16*arcsin(x)^2+-3//8*x*(1+-1*x^2)^(1/2)*arcsin(x)+-1//4*x^3*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate(x*(1+-1*x^2)^-3//2*arcsin(x), x) == :(-1*arctanh(x)+(1+-1*x^2)^-1//2*arcsin(x))
@test integrate(x*(1+-1*x^2)^-3//2*arccos(x), x) == :((1+-1*x^2)^-1//2*arccos(x)+arctanh(x))
@test integrate((1+-1*x^2)^-5//2*arcsin(x), x) == :(-1*(6+-6*x^2)^-1+1//3*log(1+-1*x^2)+1//3*x*(1+-1*x^2)^-3//2*arcsin(x)+2//3*x*(1+-1*x^2)^-1//2*arcsin(x))
@test integrate(x^3*(1+-1*x^2)^-3//2*arcsin(x), x) == :(-1x+-1*arctanh(x)+(1+-1*x^2)^(1/2)*arcsin(x)+(1+-1*x^2)^-1//2*arcsin(x))
@test integrate(x^-1*(1+-1*x^2)^-3//2*arcsin(x), x) == :(-1*arctanh(x)+im*Polylogarithms.polylog(2,-1*exp(im*arcsin(x)))+(1+-1*x^2)^-1//2*arcsin(x)+-1*im*Polylogarithms.polylog(2,exp(im*arcsin(x)))+-2*arcsin(x)*arctanh(exp(im*arcsin(x))))
@test integrate(x^-4*(1+-1*x^2)^-1//2*arccos(x), x) == :(-2//3*log(x)+1//6*x^-2+-2//3*x^-1*(1+-1*x^2)^(1/2)*arccos(x)+-1//3*x^-3*(1+-1*x^2)^(1/2)*arccos(x))
@test integrate(x*(1+-1*x^2)^(1/2)*arccos(x)^2, x) == :(2//27*(1+-1*x^2)^3//2+4//9*(1+-1*x^2)^(1/2)+-2//3*x*arccos(x)+-1//3*(1+-1*x^2)^3//2*arccos(x)^2+2//9*x^3*arccos(x))
@test integrate(x^2*(1+-1*x^2)^-1//2*arcsin(x)^3, x) == :(-3//8*x^2+-3//8*arcsin(x)^2+1//8*arcsin(x)^4+3//4*x^2*arcsin(x)^2+-1//2*x*(1+-1*x^2)^(1/2)*arcsin(x)^3+3//4*x*(1+-1*x^2)^(1/2)*arcsin(x))
@test integrate(x*(1+x^2)^-2*arctan(x), x) == :(1//4*arctan(x)+x*(4+4*x^2)^-1+-1*(2+2*x^2)^-1*arctan(x))
@test integrate(x*(1+x^2)^-3*arctan(x), x) == :(3//32*arctan(x)+3*x*(32+32*x^2)^-1+-1//4*(1+x^2)^-2*arctan(x)+1//16*x*(1+x^2)^-2)
@test integrate(x^2*(1+x^2)^-1*arctan(x), x) == :(-1//2*arctan(x)^2+-1//2*log(1+x^2)+x*arctan(x))
@test integrate(x^3*(1+x^2)^-1*arctan(x), x) == :((1/2)*arctan(x)+-1//2*x+arctan(x)*log(2*(1+im*x)^-1)+(1/2)*im*arctan(x)^2+(1/2)*im*Polylogarithms.polylog(2,1+-2*(1+im*x)^-1)+(1/2)*x^2*arctan(x))
@test integrate(x^2*(1+x^2)^-2*arctan(x), x) == :(-1*(4+4*x^2)^-1+1//4*arctan(x)^2+-1*x*(2+2*x^2)^-1*arctan(x))
@test integrate(x^3*(1+x^2)^-2*arctan(x), x) == :(-1//4*arctan(x)+(2+2*x^2)^-1*arctan(x)+-1*x*(4+4*x^2)^-1+-1*arctan(x)*log(2*(1+im*x)^-1)+-1//2*im*arctan(x)^2+-1//2*im*Polylogarithms.polylog(2,1+-2*(1+im*x)^-1))
@test integrate(x^5*(1+x^2)^-2*arctan(x), x) == :(-1//2*x+3//4*arctan(x)+im*arctan(x)^2+im*Polylogarithms.polylog(2,1+-2*(1+im*x)^-1)+x*(4+4*x^2)^-1+(1/2)*x^2*arctan(x)+-1*(2+2*x^2)^-1*arctan(x)+2*arctan(x)*log(2*(1+im*x)^-1))
@test integrate(x^-2*(1+x^2)*arctan(x), x) == :(-1*log(1+x^2)+x*arctan(x)+-1*x^-1*arctan(x)+log(x))
@test integrate(x^-5*(1+x^2)*arctan(x), x) == :(-1//4*x^-1+-1//12*x^-3+-1//4*x^-4*(1+x^2)^2*arctan(x))
@test integrate(x^-5*(1+x^2)^2*arctan(x), x) == :(-3//4*x^-1+-3//4*arctan(x)+-1//12*x^-3+(1/2)*im*Polylogarithms.polylog(2,-1*im*x)+-1*x^-2*arctan(x)+-1//2*im*Polylogarithms.polylog(2,im*x)+-1//4*x^-4*arctan(x))
@test integrate(x^-2*(1+x^2)^-1*arctan(x), x) == :(-1//2*arctan(x)^2+-1//2*log(1+x^2)+-1*x^-1*arctan(x)+log(x))
@test integrate(x^-3*arctan(x)^2, x) == :(-1//2*arctan(x)^2+-1//2*log(1+x^2)+-1*x^-1*arctan(x)+-1//2*x^-2*arctan(x)^2+log(x))
@test integrate(x^-5*arctan(x)^2*(1+x^2), x) == :(-1//6*log(1+x^2)+-1//12*x^-2+1//3*log(x)+-1//2*x^-1*arctan(x)+-1//6*x^-3*arctan(x)+-1//4*x^-4*(1+x^2)^2*arctan(x)^2)
@test integrate(x^-4*(-1+x^2)^(1/2)*arcsec(x), x) == :(1//3*(x^2)^-1//2+-1//9*x^-2*(x^2)^-1//2+1//3*x^-3*(-1+x^2)^3//2*arcsec(x))
@test integrate(x^-2*(-1+x^2)^-1//2*arcsec(x), x) == :((x^2)^-1//2+x^-1*(-1+x^2)^(1/2)*arcsec(x))
@test integrate(arcsin(((a+x)^-1*(x+-1a))^(1/2)), x) == :((a+x)*arcsin(((a+x)^-1*(x+-1a))^(1/2))+-1*a*2^(1/2)*(a*(a+x)^-1)^-1//2*((a+x)^-1*(x+-1a))^(1/2))
@test integrate(arctan(((a+x)^-1*(x+-1a))^(1/2)), x) == :(x*arctan((-1*(a+x)^-1*(a+-1x))^(1/2))+-1*a*arctanh((-1*(a+x)^-1*(a+-1x))^(1/2)))
@test integrate((1+x)^-3*arctan(x), x) == :(-1*(4+4x)^-1+-1//8*log(1+x^2)+1//4*log(1+x)+-1//2*(1+x)^-2*arctan(x))
@test integrate((a+x)^-1*arctan(x+-1a), x) == :(arctan(a+-1x)*log(2*(1+-1*im*(a+-1x))^-1)+(1/2)*im*Polylogarithms.polylog(2,1+(1+-1*im*(a+-1x))^-1*(im+-2a)^-1*(2a+2x))+-1*arctan(a+-1x)*log(-1*(1+-1*im*(a+-1x))^-1*(im+-2a)^-1*(2a+2x))+-1//2*im*Polylogarithms.polylog(2,1+-2*(1+-1*im*(a+-1x))^-1))
@test integrate((1+-1*x^2)^-1//2*arcsin((1+-1*x^2)^(1/2)), x) == :(-1//2*x^-1*(x^2)^(1/2)*arcsin((1+-1*x^2)^(1/2))^2)
@test integrate(x*(1+x^2)^-1//2*arctan((1+x^2)^(1/2)), x) == :(-1//2*log(2+x^2)+(1+x^2)^(1/2)*arctan((1+x^2)^(1/2)))
@test integrate((1+-1x)^-5//2*arcsin(x), x) == :(-1*(1+x)^(1/2)*(3+-3x)^-1+-1//6*2^(1/2)*arctanh((1/2)*2^(1/2)*(1+x)^(1/2))+2//3*(1+-1x)^-3//2*arcsin(x))
@test integrate(sinh(x)^-4*arccot(cosh(x))*cosh(x), x) == :(1//6*coth(x)+-1//3*csch(x)^3*arccot(cosh(x))+1//12*2^(1/2)*arctanh((1/2)*2^(1/2)*tanh(x)))
@test integrate(arcsin(tanh(x))*exp(x), x) == :(arcsin(tanh(x))*exp(x)+-1*(sech(x)^2)^(1/2)*cosh(x)*log(1+exp(2x)))

test/0 Independent test suites/Timofeev Problems.jl


function rotation_matrix(q0::Float64, q1::Float64, q2::Float64, q3::Float64)

	a11::Float64 = q0^2 + q1^2 - q2^2 - q3^2
	a12::Float64 = 2 * q1 * q2 + 2 * q0 * q3
	a13::Float64 = 2 * q1 * q3 - 2 * q0 * q2
	a21::Float64 = 2 * q1 * q2 - 2 * q0 * q3
	a22::Float64 = q0^2 - q1^2 + q2^2 - q3^2
	a23::Float64 = 2 * q2 * q3 + 2 * q0 * q1
	a31::Float64 = 2 * q1 * q3 + 2 * q0 * q2
	a32::Float64 = 2 * q2 * q3 - 2 * q0 * q1
	a33::Float64 = q0^2 - q1^2 - q2^2 + q3^2

	return (a11, a12, a13, a21, a22, a23, a31, a32, a33)

end


using LinearAlgebra
using Random

Random.seed!(0)

function gradient_fd(f,x) #compute gradient using finite differences
	gradfd = zero(x)
	xeps1 = zero(x)
	xeps2 = zero(x)
	delta = sqrt(eps())
	for i in eachindex(gradfd)
		xeps1 .= x
		xeps2 .= x
		xeps1[i] -= delta
		xeps2[i] += delta
		gradfd[i] = (f(xeps2)-f(xeps1))/(2*delta)
	end
	return gradfd
end

stuff = [
  Dict( "f"      => ElasticNet(2.0, 3.0),
        "x"      => [-2., -1., 0., 1., 2., 3.],
        "∇f(x)"  => 2*[-1., -1., 0., 1., 1., 1.] + 3*[-2., -1., 0., 1., 2., 3.],
      ),
  Dict( "f"      => NormL1(2.0),
        "x"      => [-2., -1., 0., 1., 2., 3.],
        "∇f(x)"  => 2*[-1., -1., 0., 1., 1., 1.],
      ),
  Dict( "f"      => NormL2(2.0),
        "x"      => [-1., 0., 2.],
        "∇f(x)"  => 2/sqrt(5)*[-1., 0., 2.],
      ),
  Dict( "f"      => NormL2(2.0),
        "x"      => [-1., 0., 2.],
        "∇f(x)"  => 2/sqrt(5)*[-1., 0., 2.],
      ),
  Dict( "f"      => NormL2(2.0),
        "x"      => [0., 0., 0.],
        "∇f(x)"  => [0., 0., 0.],
      ),
  # Dict( "f"      => NormL21(),
  #       "x"      => ,
  #       "∇f(x)"  => ,
  #     ),
  Dict( "f"      => NormLinf(2.),
        "x"      => [-2., -1., 0., 1., 2., 3.],
        "∇f(x)"  => 2*[0., 0., 0., 0., 0., 1.],
      ),
  Dict( "f"      => NormLinf(2.),
        "x"      => [-4., -1., 0., 1., 2., 3.],
        "∇f(x)"  => 2*[-1., 0., 0., 0., 0., 0.],
      ),
  # Dict( "f"      => NuclearNorm(),
  #       "x"      => ,
  #       "∇f(x)"  => ,
  #     ),
  Dict( "f"      => SqrNormL2(2.0),
        "x"      => [-2., -1., 0., 1., 2., 3.],
        "∇f(x)"  => 2*[-2., -1., 0., 1., 2., 3.],
      ),
  Dict( "f"      => HingeLoss([1., 2., 1., 2., 1.], 2.0),
        "x"      => [-2., -1., 0., 2., 3.],
        "∇f(x)"  => -2*[1., 2., 1.,  0., 0.],
      ),
  Dict( "f"      => HuberLoss(2., 3.),
        "x"      => [-1., 0.5],
        "∇f(x)"  => 3*[-1., 0.5],
      ),
  Dict( "f"      => HuberLoss(2., 3.),
        "x"      => [-2., 1.5],
        "∇f(x)"  => [-4.8, 3.6], # 3*2*[-2., 1.5]/norm([-2., 1.5]),
      ),
  Dict( "f"      => Linear([-1.,2.,3.]),
        "x"      => [1., 5., 3.14],
        "∇f(x)"  => [-1.,2.,3.],
      ),
  Dict( "f"      => LogBarrier(2.0, 1.0, 1.5),
        "x"      => [1.0, 2.0, 3.0],
        "∇f(x)"  => -1.5*2.0./(2.0*[1.0, 2.0, 3.0].+1.0),# -μ*a*/(a*x+b)
      ),
  Dict( "f"      => LeastSquares([1.0 2.0; 3.0 4.0], [0.1, 0.2], 2.0, iterative=false),
        "x"      => [-1., 2.],
        "∇f(x)"  => [34.6, 50.], #λ*(A'A*x-A'b),
      ),
  Dict( "f"      => LeastSquares([1.0 2.0; 3.0 4.0], [0.1, 0.2], 2.0, iterative=true),
        "x"      => [-1., 2.],
        "∇f(x)"  => [34.6, 50.], #λ*(A'A*x-A'b),
      ),
  Dict( "f"      => Quadratic([2. -1.; -1. 2.], [0.1, 0.2], iterative=false),
        "x"      => [3., 4.],
        "∇f(x)"  => [2.1, 5.2], #[2. -1.; -1. 2.]*[3., 4.]+[0.1, 0.2],
      ),
  Dict( "f"      => Quadratic([2. -1.; -1. 2.], [0.1, 0.2], iterative=true),
        "x"      => [3., 4.],
        "∇f(x)"  => [2.1, 5.2], #[2. -1.; -1. 2.]*[3., 4.]+[0.1, 0.2],
      ),
  Dict( "f"      => SumPositive(),
        "x"      => [-1., 1., 2.],
        "∇f(x)"  => [0., 1., 1.],
      ),
  # Dict( "f"      => Maximum(2.0),
  #       "x"      => [-4., 2., 3.],
  #       "∇f(x)"  => 2*[0., 0., 1.],
  #     ),
  Dict( "f"      => DistL2(IndZero(),2.0),
        "x"      => [1., -2],
        "∇f(x)"  => 2*[1., -2]./norm([1., -2]),
      ),
  Dict( "f"      => DistL2(IndBallL2(1.0),2.0),
        "x"      => [2., -2],
        "∇f(x)"  => [sqrt(2), -sqrt(2)],
      ),
  Dict( "f"      => SqrDistL2(IndZero(),2.0),
        "x"      => [1., -2],
        "∇f(x)"  => 2*[1., -2],
      ),
  Dict( "f"      => SqrDistL2(IndBallL2(1.0),2.0),
        "x"      => [2., -2],
        "∇f(x)"  => 2.0*([2., -2]-[1/sqrt(2), -1/(sqrt(2))]),
      ),
  Dict( "f"      => LogisticLoss([1.0, -1.0, 1.0, -1.0, 1.0], 1.5),
        "x"      => [-1.0, -2.0, 3.0, 2.0, 1.0],
        "∇f(x)"  => [-1.0965878679450072, 0.17880438303317633, -0.07113880976635019, 1.3211956169668235, -0.4034121320549927]
      ),
  Dict( "f"      => SqrHingeLoss([1.0, -1.0, 1.0, -1.0, 1.0], 1.5),
       "x"      => randn(MersenneTwister(0),Float64,5),
       "∇f(x)"  => gradient_fd(SqrHingeLoss([1.0, -1.0, 1.0, -1.0, 1.0], 1.5),randn(MersenneTwister(0),Float64,5))
      ),
  Dict( "f"      => CrossEntropy([1.0, 0., 1.0,  0., 1.0]),
       "x"      => rand(MersenneTwister(0),Float64,5),
       "∇f(x)"  => gradient_fd(CrossEntropy([1.0, 0., 1.0,  0., 1.0]),rand(MersenneTwister(0),Float64,5))
      ),
  Dict( "f"      => CrossEntropy([true, false, true,  false, true]),
       "x"      => rand(MersenneTwister(0),Float64,5),
       "∇f(x)"  => gradient_fd(CrossEntropy([true, false, true,  false, true]),rand(MersenneTwister(0),Float64,5))
      ),
  Dict( "f"      => CrossEntropy([true, false, true,  false, true]),
       "x"      => rand(MersenneTwister(0),Float64,1,5),
       "∇f(x)"  => gradient_fd(CrossEntropy([true, false, true,  false, true]),rand(MersenneTwister(0),Float64,1,5))
      ),
]

for i = 1:length(stuff)

  f = stuff[i]["f"]
  x = stuff[i]["x"]

  ref_∇f = stuff[i]["∇f(x)"]

  ref_fx = f(x)
  ∇f = similar(x)
  fx = gradient!(∇f, f, x)
  @test fx ≈ ref_fx
  @test ∇f ≈ ref_∇f

  for j = 1:11
    #For initial point x and 10 other random points
    fx = gradient!(∇f, f, x)
    for k = 1:10
      # Test conditions in different directions
      if ProximalOperators.is_convex(f)
        # Test ∇f is subgradient
	if typeof(f) <: CrossEntropy
		d = x.*(rand(Float64, size(x)).-1)./2 # assures 0 <= x+d <= 1
	else
		d = randn(Float64, size(x))
	end
        @test f(x+d) ≥ fx + dot(d, ∇f) - TOL_ASSERT
      else
        # Assume smooth function
        d = randn(Float64, size(x))
        d ./= norm(d).*sqrt(TOL_ASSERT)
        @test f(x+d) ≈ fx + dot(d, ∇f) atol=TOL_ASSERT
      end
    end
    x = rand(Float64, size(x))
  end
end


using Distributions
using TensorFlow

# Generate some synthetic data
x = randn(100, 50)
w = randn(50, 10)
y_prob = exp(x * w)
y_prob ./= sum(y_prob, 2)

function draw(probs)
    y = zeros(size(probs))
    for i in 1:size(probs, 1)
        idx = rand(Categorical(probs[i, :]))
        y[i, idx] = 1
    end
    return y
end

y = draw(y_prob)

# Build the model
sess = Session(Graph())
X = placeholder(Float64)
Y_obs = placeholder(Float64)

variable_scope("logistic_model", initializer = Normal(0, 0.001)) do
    global W = get_variable("weights", [50, 10], Float64)
    global B = get_variable("bias", [10], Float64)
end

Y=nn.softmax(X * W + B)
Loss = -reduce_sum(log(Y) .* Y_obs)
optimizer = train.AdamOptimizer()
minimize_op = train.minimize(optimizer, Loss)
saver = train.Saver()
# Run training
run(sess, global_variables_initializer())
checkpoint_path = mktempdir()
info("Checkpoint files saved in $checkpoint_path")
for epoch in 1:10
    cur_loss, _ = run(sess, [Loss, minimize_op], Dict(X=>x, Y_obs=>y))
    println(@sprintf("Current loss is %.2f.", cur_loss))
    train.save(saver, sess, joinpath(checkpoint_path, "logistic"), global_step=epoch)
end


using Printf

println("Some easy ones to start with\n")

for i in [-1:21..., 100, 101, 10000, 10001, 1000000, 1010101]
    @printf("%8d is %s\n", i, num2text(i))
end

println("\nSome larger numbers\n")

println("The largest signed literal integer (short-scale)")
i = typemax(1)
println("    ", i, " is")
println(num2text(i))
println()

println("The largest signed literal integer (long-scale)")
println("    ", i, " is")
println(num2text(i, false))
println()

println("The largest unsigned integer (short-scale)")
i = typemax(UInt128)
println("    ", i, " is")
println(num2text(i))
println()

println("50! (short-scale)")
i = factorial(big(50))
println("    ", i, " is")
println(num2text(i))
println()

println("51! (short-scale)")
i = factorial(big(51))
println("    ", i, " is")
println(num2text(i))
println()

println("51! (long-scale)")
println("    ", i, " is")
println(num2text(i, false))


todisplay = VERSION <= VersionNumber("1.2") ? true : false

using LinearAlgebra
using LinearAlgebraicRepresentation
Lar = LinearAlgebraicRepresentation
using CAGD
if todisplay
    using ViewerGL
    GL = ViewerGL
	include("../views.jl")
end

function twocubegrids(n=1, m=1, p=1, atol = 1e-6)
    V,(VV,EV,FV,CV) = Lar.cuboidGrid([n,m,p],true)
    mybox = (V,CV,FV,EV)

    twocubs = Lar.Struct([mybox, Lar.t(.3,.4,.5), Lar.r(pi/5,0,0), Lar.r(0,0,pi/12), mybox])

    V,CV,FV,EV = Lar.struct2lar(twocubs)
    if todisplay  GL.VIEW([ GL.GLGrid(V,FV, GL.COLORS[1]) ])  end

    cop_EV = convert(Lar.ChainOp, Lar.coboundary_0(EV::Lar.Cells))
    cop_FE = Lar.coboundary_1(V, FV::Lar.Cells, EV::Lar.Cells);
    model = CAGD.Model(V)
    CAGD.addModelCells!(model, 1, cop_EV)
    CAGD.addModelCells!(model, 2, cop_FE)

    split_model = CAGD.facesplitting(model)
    congr_model = CAGD.mergeModelVertices(split_model, signed_merge=true)
    if todisplay  displayModel(congr_model)  end
    gift_model = deepcopy(congr_model)
    FC, bicon_comps = CAGD.tgw(congr_model, 3, atol = atol)
    CAGD.addModelCells!(gift_model, 3, convert(Lar.ChainOp, FC'))
    if todisplay  viewExplode(gift_model)  end
end


# ToFeT_io.jl
# Code to read in ToFeT file format .edge and .xyz specifications

import DelimitedFiles
import SparseArrays

function readToFeTedges(f)
    raw=DelimitedFiles.readdlm(f)

    # - .+1 renumber indices from 1 rather than zero
    # - double up (vcat) indices for making explicit A->B && implicit B->A
    As=Int.(vcat(raw[:,1],raw[:,2])) .+ 1
    Bs=Int.(vcat(raw[:,2],raw[:,1])) .+ 1

    Js=vcat(raw[:,3],raw[:,3])

    SparseArrays.sparse(As,Bs,Js)
end

function generateLinearChain(n; J=0.1)
    # bit ugly, surely a nicer way to write this?
    As=[ i for i=1:n-1 ]
    Bs=[ i+1 for i=1:n-1 ]
    Js=[J for i=1:n-1]

    SparseArrays.sparse(vcat(As,Bs),vcat(Bs,As),vcat(Js,Js))
end

function generateNearestNeighbourCube(n; J=0.1)
    N=n^3 # number of sites in cube
    As=Int[]
    Bs=Int[]
    Js=Float64[]

    nn=[+1 -1 +n -n +n^2 -n^2]

    for i in 1:N # iterate over all elements
        for neighbour in nn
            append!(As,i)
            append!(Bs,(i+neighbour-1+N)%N + 1) # modulo arithmatic to enforce PBCs
            append!(Js,J)
        end
    end

    SparseArrays.sparse(As,Bs,Js)
end

function readToFeTxyz(f)
     raw=DelimitedFiles.readdlm(f)
     XYZ=raw[:,1:3]
 end


using StocasExamples
using Base.Test
using Stocas

const se = StocasExamples
solve!(se.am2010()...)
solve!(se.am_tauchen()...)
solve!(se.dixit()...)
solve!(se.hitsch()...)
solve!(se.rust()...)
solve!(se.seven()...)