text:
module NB5I

using NBodyIPs.FastPolys
using StaticArrays
# using BenchmarkTools: @btime

# include("fastpolys.jl")
# using FastPolys

tdegrees(::Val{5}) =
          (@SVector [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, ]),
    (@SVector [0, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, ])

# Primary invariants for NBody=5and deg=10
 # : definitions at the beginning of the file
const P1_1 = (1,2,3,4,5,6,7,8,9,10,)

const P2_1 = (1,1,1,2,2,3,1,1,1,2,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,)
const P2_2 = (2,3,4,3,4,4,5,6,7,5,6,7,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)

const P3_1 = (1,2,3,4,5,6,7,8,9,10,)

const P4_1 = (1,1,1,2,1,1,1,2,2,3,4,3,4,2,3,4,5,5,6,7,)
const P4_2 = (2,2,3,3,5,5,6,5,5,6,7,6,7,8,8,9,6,8,8,9,)
const P4_3 = (3,4,4,4,6,7,7,8,9,8,9,10,10,9,10,10,7,9,10,10,)

const P5_1 = (1,2,3,4,5,6,7,8,9,10,)

const P6_1 = (1,1,2,3,4,)
const P6_2 = (2,5,5,6,7,)
const P6_3 = (3,6,8,8,9,)
const P6_4 = (4,7,9,10,10,)

const P7_1 = (1,2,3,4,5,6,7,8,9,10,)

const P8_1 = (1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,3,3,1,1,1,2,2,3,4,3,4,2,3,4,)
const P8_2 = (2,2,2,2,2,2,2,3,4,2,3,4,3,4,3,4,4,4,5,5,5,5,5,5,5,6,6,5,6,7,)
const P8_3 = (3,3,3,3,3,3,5,5,5,5,6,7,5,5,6,7,6,7,6,6,6,6,7,6,7,7,7,8,8,8,)
const P8_4 = (4,4,4,4,4,4,6,6,6,8,8,9,8,8,8,9,8,9,7,7,7,8,8,8,9,8,9,9,9,9,)
const P8_5 = (5,6,7,8,9,10,7,7,7,9,10,10,9,9,10,10,10,10,8,9,10,9,9,10,10,10,10,10,10,10,)

const P9_1 = (1,2,3,4,5,6,7,8,9,10,)

const P10_1 = (1,2,3,4,5,6,7,8,9,10,)

# Irreducible secondaries for NBody=5and deg=10
 # : definitions at the beginning of the file
const IS1_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS1_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS2_1 = (1,1,1,2,2,3,5,5,6,8,)
const IS2_2 = (2,3,4,3,4,4,6,7,7,9,)
const IS2_3 = (5,6,7,8,9,10,8,9,10,10,)

const IS3_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS3_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS4_1 = (1,1,1,2,2,3,1,1,1,2,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,)
const IS4_2 = (2,3,4,3,4,4,5,6,7,5,6,7,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)

const IS5_1 = (1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,8,9,10,5,5,6,7,6,7,8,9,10,8,9,10,)
const IS5_2 = (2,3,4,1,1,1,3,4,2,2,4,3,1,1,1,2,2,3,6,7,5,5,7,6,5,5,6,9,8,8,)
const IS5_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,3,4,4,8,9,8,9,10,10,6,7,7,10,10,9,)

const IS6_1 = (1,1,1,2,2,3,4,3,4,2,3,4,1,1,1,2,2,3,4,3,4,2,3,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,6,7,5,6,7,8,9,8,9,10,10,)
const IS6_2 = (2,2,3,1,1,1,1,1,1,3,2,2,5,5,6,5,5,6,7,6,7,8,8,9,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,6,5,5,8,8,9,5,5,6,7,6,7,)
const IS6_3 = (3,4,4,3,4,2,2,4,3,4,4,3,6,7,7,8,9,8,9,10,10,9,10,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,7,7,6,9,10,10,9,8,10,10,8,9,)

const IS7_1 = (1,1,1,1,1,1,2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,5,6,9,8,8,)
const IS10_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,3,4,4,8,9,8,9,10,10,6,7,7,10,10,9,)

const IS11_1 = (1,1,1,2,2,3,1,1,1,2,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,)
const IS11_2 = (2,3,4,3,4,4,5,6,7,5,6,7,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)
const IS11_3 = (5,6,7,8,9,10,2,3,4,1,1,1,3,4,2,2,4,3,8,9,10,6,7,5,5,7,6,10,9,8,)

const IS12_1 = (1,1,1,2,2,3,4,3,4,2,3,4,1,1,1,2,2,3,4,3,4,2,3,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,6,7,5,6,7,8,9,8,9,10,10,)
const IS12_2 = (2,2,3,1,1,1,1,1,1,3,2,2,5,5,6,5,5,6,7,6,7,8,8,9,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,6,5,5,8,8,9,5,5,6,7,6,7,)
const IS12_3 = (3,4,4,3,4,2,2,4,3,4,4,3,6,7,7,8,9,8,9,10,10,9,10,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,7,7,6,9,10,10,9,8,10,10,8,9,)

const IS13_1 = (1,1,1,1,1,1,2,2,3,2,2,3,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,5,5,6,5,5,6,7,6,7,8,8,9,)
const IS13_2 = (2,2,3,4,3,4,3,4,4,3,4,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)
const IS13_3 = (3,4,2,2,4,3,1,1,1,4,3,2,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8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,5,6,7,6,7,5,5,6,7,6,7,8,8,8,9,8,9,2,2,3,4,3,4,2,2,3,4,3,4,3,4,3,4,4,4,3,4,3,4,4,4,6,7,5,5,7,6,6,7,5,5,7,6,5,5,5,5,6,6,9,8,9,8,8,8,7,7,7,6,7,6,8,8,8,9,8,9,6,7,6,7,7,7,9,8,9,8,8,8,)
const IS15_4 = (5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,6,7,6,7,7,7,8,9,8,9,10,10,8,9,8,9,10,10,9,9,10,10,10,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,8,9,8,9,10,10,8,9,8,9,10,10,6,7,6,7,7,7,10,10,10,10,9,9,8,9,8,9,10,10,9,9,10,10,10,10,9,8,10,10,8,9,10,10,10,10,9,9,)

const IS16_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS16_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS17_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS17_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS18_1 = (1,1,1,2,2,3,1,1,1,2,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,)
const IS18_2 = (2,3,4,3,4,4,5,6,7,5,6,7,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)

const IS19_1 = (1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,8,9,10,5,5,6,7,6,7,8,9,10,8,9,10,)
const IS19_2 = (2,3,4,1,1,1,3,4,2,2,4,3,1,1,1,2,2,3,6,7,5,5,7,6,5,5,6,9,8,8,)
const IS19_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,3,4,4,8,9,8,9,10,10,6,7,7,10,10,9,)

const IS20_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS20_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)
const IS20_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,1,1,1,3,4,2,2,4,3,2,3,4,1,1,1,3,4,2,2,4,3,8,9,8,9,10,10,6,7,5,5,7,6,6,7,5,5,7,6,10,10,9,8,9,8,)

const IS21_1 = (1,1,1,2,2,3,5,5,6,8,)
const IS21_2 = (2,3,4,3,4,4,6,7,7,9,)
const IS21_3 = (5,6,7,8,9,10,8,9,10,10,)

const IS22_1 = (1,1,1,2,2,3,4,3,4,2,3,4,1,1,1,2,2,3,4,3,4,2,3,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,6,7,5,6,7,8,9,8,9,10,10,)
const IS22_2 = (2,2,3,1,1,1,1,1,1,3,2,2,5,5,6,5,5,6,7,6,7,8,8,9,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,6,5,5,8,8,9,5,5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= (5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,6,7,6,7,7,7,8,9,8,9,10,10,8,9,8,9,10,10,9,9,10,10,10,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,8,9,8,9,10,10,8,9,8,9,10,10,6,7,6,7,7,7,10,10,10,10,9,9,8,9,8,9,10,10,9,9,10,10,10,10,9,8,10,10,8,9,10,10,10,10,9,9,)

const IS26_1 = (1,1,1,1,1,1,2,2,3,2,2,3,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,5,5,6,5,5,6,7,6,7,8,8,9,)
const IS26_2 = (2,2,3,4,3,4,3,4,4,3,4,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)
const IS26_3 = (3,4,2,2,4,3,1,1,1,4,3,2,2,2,3,4,3,4,1,1,1,1,1,1,3,4,2,2,4,3,3,4,2,2,4,3,1,1,1,2,2,3,4,3,4,2,3,4,7,6,5,6,7,5,5,7,6,5,6,7,)
const IS26_4 = (5,5,6,7,6,7,8,9,10,8,9,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,8,9,10,6,7,5,5,7,6,10,9,8,8,9,10,9,8,10,10,8,9,10,9,8,)

const IS27_1 = (1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS27_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10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS31_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS31_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS32_1 = (1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,8,9,10,5,5,6,7,6,7,8,9,10,8,9,10,)
const IS32_2 = (2,3,4,1,1,1,3,4,2,2,4,3,1,1,1,2,2,3,6,7,5,5,7,6,5,5,6,9,8,8,)
const IS32_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,3,4,4,8,9,8,9,10,10,6,7,7,10,10,9,)

const IS33_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS33_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)
const IS33_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,1,1,1,3,4,2,2,4,3,2,3,4,1,1,1,3,4,2,2,4,3,8,9,8,9,10,10,6,7,5,5,7,6,6,7,5,5,7,6,10,10,9,8,9,8,)

const IS34_1 = (1,1,1,2,2,3,1,1,1,2,3,4,2,2,3,4,3,4,5,5,6,5,5,6,7,6,7,8,8,9,)
const IS34_2 = (2,3,4,3,4,4,5,6,7,5,6,7,8,9,8,9,10,10,6,7,7,8,9,8,9,10,10,9,10,10,)
const IS34_3 = (5,6,7,8,9,10,2,3,4,1,1,1,3,4,2,2,4,3,8,9,10,6,7,5,5,7,6,10,9,8,)

const IS35_1 = (1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,8,9,10,5,5,6,7,6,7,8,9,10,8,9,10,)
const IS35_2 = (2,3,4,1,1,1,3,4,2,2,4,3,1,1,1,2,2,3,6,7,5,5,7,6,5,5,6,9,8,8,)
const IS35_3 = (5,6,7,5,6,7,8,9,8,9,10,10,2,3,4,3,4,4,8,9,8,9,10,10,6,7,7,10,10,9,)

const IS36_1 = (1,1,1,2,2,3,4,3,4,2,3,4,1,1,1,2,2,3,4,3,4,2,3,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,6,7,5,6,7,8,9,8,9,10,10,)
const IS36_2 = (2,2,3,1,1,1,1,1,1,3,2,2,5,5,6,5,5,6,7,6,7,8,8,9,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,6,5,5,8,8,9,5,5,6,7,6,7,)
const IS36_3 = (3,4,4,3,4,2,2,4,3,4,4,3,6,7,7,8,9,8,9,10,10,9,10,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,7,7,6,9,10,10,9,8,10,10,8,9,)

const IS37_1 = (1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,1,1,1,1,1,1,2,2,3,4,3,4,2,2,3,4,3,4,2,2,3,4,3,4,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS37_2 = (2,2,3,4,3,4,1,1,1,1,1,1,3,4,2,2,4,3,3,4,2,2,4,3,5,5,6,7,6,7,5,5,6,7,6,7,8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10,10,9,9,10,10,10,10,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,8,9,8,9,10,10,8,9,8,9,10,10,6,7,6,7,7,7,10,10,10,10,9,9,8,9,8,9,10,10,9,9,10,10,10,10,9,8,10,10,8,9,10,10,10,10,9,9,)

const IS41_1 = (1,2,3,4,5,6,7,8,9,10,)

const IS42_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS42_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS43_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS43_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS44_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS44_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

const IS45_1 = (1,2,3,4,5,6,7,8,9,10,)

const IS46_1 = (1,1,1,2,3,4,2,2,3,4,3,4,1,1,1,2,3,4,2,2,3,4,3,4,5,6,7,5,6,7,8,9,8,9,10,10,5,5,6,7,6,7,5,5,6,7,6,7,8,9,8,9,10,10,8,9,8,9,10,10,)
const IS46_2 = (2,3,4,1,1,1,3,4,2,2,4,3,5,6,7,5,6,7,8,9,8,9,10,10,1,1,1,2,3,4,2,2,3,4,3,4,6,7,5,5,7,6,8,9,8,9,10,10,5,5,6,7,6,7,9,8,10,10,8,9,)

# Primary invariants for NBody=5and deg=10
 # : definitions of the types at the beginning of the file
const P1 = Val((P1_1,))
const P2 = Val((P2_1,P2_2,))
const P3 = Val((P3_1,))
const P4 = Val((P4_1,P4_2,P4_3,))
const P5 = Val((P5_1,))
const P6 = Val((P6_1,P6_2,P6_3,P6_4,))
const P7 = Val((P7_1,))
const P8 = Val((P8_1,P8_2,P8_3,P8_4,P8_5,))
const P9 = Val((P9_1,))
const P10 = Val((P10_1,))
# Irreducible secondaries for NBody=5and deg=10
 # : definitions of the types at the beginning of the file
const IS1 = Val((IS1_1,IS1_2,))
const IS2 = Val((IS2_1,IS2_2,IS2_3,))
const IS3 = Val((IS3_1,IS3_2,))
const IS4 = Val((IS4_1,IS4_2,))
const IS5 = Val((IS5_1,IS5_2,IS5_3,))
const IS6 = Val((IS6_1,IS6_2,IS6_3,))
const IS7 = Val((IS7_1,IS7_2,IS7_3,))
const IS8 = Val((IS8_1,IS8_2,))
const IS9 = Val((IS9_1,IS9_2,))
const IS10 = Val((IS10_1,IS10_2,IS10_3,))
const IS11 = Val((IS11_1,IS11_2,IS11_3,))
const IS12 = Val((IS12_1,IS12_2,IS12_3,))
const IS13 = Val((IS13_1,IS13_2,IS13_3,))
const IS14 = Val((IS14_1,IS14_2,IS14_3,))
const IS15 = Val((IS15_1,IS15_2,IS15_3,IS15_4,))
const IS16 = Val((IS16_1,IS16_2,))
const IS17 = Val((IS17_1,IS17_2,))
const IS18 = Val((IS18_1,IS18_2,))
const IS19 = Val((IS19_1,IS19_2,IS19_3,))
const IS20 = Val((IS20_1,IS20_2,IS20_3,))
const IS21 = Val((IS21_1,IS21_2,IS21_3,))
const IS22 = Val((IS22_1,IS22_2,IS22_3,))
const IS23 = Val((IS23_1,IS23_2,IS23_3,))
const IS24 = Val((IS24_1,IS24_2,IS24_3,))
const IS25 = Val((IS25_1,IS25_2,IS25_3,IS25_4,))
const IS26 = Val((IS26_1,IS26_2,IS26_3,IS26_4,))
const IS27 = Val((IS27_1,IS27_2,IS27_3,))
const IS28 = Val((IS28_1,))
const IS29 = Val((IS29_1,IS29_2,))
const IS30 = Val((IS30_1,IS30_2,))
const IS31 = Val((IS31_1,IS31_2,))
const IS32 = Val((IS32_1,IS32_2,IS32_3,))
const IS33 = Val((IS33_1,IS33_2,IS33_3,))
const IS34 = Val((IS34_1,IS34_2,IS34_3,))
const IS35 = Val((IS35_1,IS35_2,IS35_3,))
const IS36 = Val((IS36_1,IS36_2,IS36_3,))
const IS37 = Val((IS37_1,IS37_2,IS37_3,))
const IS38 = Val((IS38_1,IS38_2,IS38_3,))
const IS39 = Val((IS39_1,IS39_2,IS39_3,))
const IS40 = Val((IS40_1,IS40_2,IS40_3,IS40_4,))
const IS41 = Val((IS41_1,))
const IS42 = Val((IS42_1,IS42_2,))
const IS43 = Val((IS43_1,IS43_2,))
const IS44 = Val((IS44_1,IS44_2,))
const IS45 = Val((IS45_1,))
const IS46 = Val((IS46_1,IS46_2,))

function invariants_gen(x1::SVector{10, T}) where {T}
   x2 = x1.*x1
   x3 = x2.*x1
   x4 = x3.*x1
   x5 = x4.*x1
   x6 = x5.*x1
   x7 = x6.*x1
   x8 = x7.*x1
   x9 = x8.*x1
   #------------------------------------------------
   # Primaries
   #------------------------------------------------

# Primary invariants for NBody=5and deg=10
 # : what goes in the function for the evaluation
P1 = fpoly((x1,) , Main.P1)
P2 = fpoly((x1,x1,) , Main.P2)
P3 = fpoly((x2,) , Main.P3)
P4 = fpoly((x1,x1,x1,) , Main.P4)
P5 = fpoly((x3,) , Main.P5)
P6 = fpoly((x1,x1,x1,x1,) , Main.P6)
P7 = fpoly((x4,) , Main.P7)
P8 = fpoly((x1,x1,x1,x1,x1,) , Main.P8)
P9 = fpoly((x5,) , Main.P9)
P10 = fpoly((x6,) , Main.P10)

   #------------------------------------------------
   # Irreducible secondaries
   #------------------------------------------------

# Irreducible secondaries for NBody=5and deg=10
 # : what goes in the function for the evaluation
IS1 = fpoly((x2,x1,) , Main.IS1)
IS2 = fpoly((x1,x1,x1,) , Main.IS2)
IS3 = fpoly((x3,x1,) , Main.IS3)
IS4 = fpoly((x2,x2,) , Main.IS4)
IS5 = fpoly((x2,x1,x1,) , Main.IS5)
IS6 = fpoly((x2,x1,x1,) , Main.IS6)
IS7 = fpoly((x2,x1,x1,) , Main.IS7)
IS8 = fpoly((x4,x1,) , Main.IS8)
IS9 = fpoly((x3,x2,) , Main.IS9)
IS10 = fpoly((x3,x1,x1,) , Main.IS10)
IS11 = fpoly((x2,x2,x1,) , Main.IS11)
IS12 = fpoly((x3,x1,x1,) , Main.IS12)
IS13 = fpoly((x2,x2,x1,) , Main.IS13)
IS14 = fpoly((x3,x1,x1,) , Main.IS14)
IS15 = fpoly((x2,x1,x1,x1,) , Main.IS15)
IS16 = fpoly((x5,x1,) , Main.IS16)
IS17 = fpoly((x4,x2,) , Main.IS17)
IS18 = fpoly((x3,x3,) , Main.IS18)
IS19 = fpoly((x4,x1,x1,) , Main.IS19)
IS20 = fpoly((x3,x2,x1,) , Main.IS20)
IS21 = fpoly((x2,x2,x2,) , Main.IS21)
IS22 = fpoly((x4,x1,x1,) , Main.IS22)
IS23 = fpoly((x3,x2,x1,) , Main.IS23)
IS24 = fpoly((x4,x1,x1,) , Main.IS24)
IS25 = fpoly((x3,x1,x1,x1,) , Main.IS25)
IS26 = fpoly((x2,x2,x1,x1,) , Main.IS26)
IS27 = fpoly((x3,x2,x1,) , Main.IS27)
IS28 = fpoly((x7,) , Main.IS28)
IS29 = fpoly((x6,x1,) , Main.IS29)
IS30 = fpoly((x5,x2,) , Main.IS30)
IS31 = fpoly((x4,x3,) , Main.IS31)
IS32 = fpoly((x5,x1,x1,) , Main.IS32)
IS33 = fpoly((x4,x2,x1,) , Main.IS33)
IS34 = fpoly((x3,x3,x1,) , Main.IS34)
IS35 = fpoly((x3,x2,x2,) , Main.IS35)
IS36 = fpoly((x5,x1,x1,) , Main.IS36)
IS37 = fpoly((x4,x2,x1,) , Main.IS37)
IS38 = fpoly((x3,x3,x1,) , Main.IS38)
IS39 = fpoly((x5,x1,x1,) , Main.IS39)
IS40 = fpoly((x4,x1,x1,x1,) , Main.IS40)
IS41 = fpoly((x8,) , Main.IS41)
IS42 = fpoly((x7,x1,) , Main.IS42)
IS43 = fpoly((x6,x2,) , Main.IS43)
IS44 = fpoly((x5,x3,) , Main.IS44)
IS45 = fpoly((x9,) , Main.IS45)
IS46 = fpoly((x8,x1,) , Main.IS46)

   #------------------------------------------------
   # All secondaries
   #------------------------------------------------

 SEC1  = 1
 SEC2  = IS1
 SEC3  = IS2
 SEC4  = IS3
 SEC5  = IS4
 SEC6  = IS5
 SEC7  = IS6
 SEC8  = IS7
 SEC9  = IS8
 SEC10  = IS9
 SEC11  = IS10
 SEC12  = IS11
 SEC13  = IS12
 SEC14  = IS13
 SEC15  = IS14
 SEC16  = IS15
 SEC17  = IS1 *IS1
 SEC18  = IS1 *IS2
 SEC19  = IS2 *IS2
 SEC20  = IS16
 SEC21  = IS17
 SEC22  = IS18
 SEC23  = IS19
 SEC24  = IS20
 SEC25  = IS21
 SEC26  = IS22
 SEC27  = IS23
 SEC28  = IS24
 SEC29  = IS25
 SEC30  = IS26
 SEC31  = IS27
 SEC32  = IS1 *IS3
 SEC33  = IS2 *IS3
 SEC34  = IS1 *IS4
 SEC35  = IS2 *IS4
 SEC36  = IS1 *IS5
 SEC37  = IS2 *IS5
 SEC38  = IS1 *IS6
 SEC39  = IS2 *IS6
 SEC40  = IS1 *IS7
 SEC41  = IS2 *IS7
 SEC42  = IS28
 SEC43  = IS29
 SEC44  = IS30
 SEC45  = IS31
 SEC46  = IS32
 SEC47  = IS33
 SEC48  = IS34
 SEC49  = IS35
 SEC50  = IS36
 SEC51  = IS37
 SEC52  = IS38
 SEC53  = IS39
 SEC54  = IS40
 SEC55  = IS4 *IS4
 SEC56  = IS3 *IS5
 SEC57  = IS4 *IS5
 SEC58  = IS5 *IS5
 SEC59  = IS3 *IS6
 SEC60  = IS4 *IS6
 SEC61  = IS5 *IS6
 SEC62  = IS6 *IS6
 SEC63  = IS3 *IS7
 SEC64  = IS4 *IS7
 SEC65  = IS5 *IS7
 SEC66  = IS6 *IS7
 SEC67  = IS7 *IS7
 SEC68  = IS1 *IS8
 SEC69  = IS2 *IS8
 SEC70  = IS1 *IS9
 SEC71  = IS2 *IS9
 SEC72  = IS1 *IS10
 SEC73  = IS2 *IS10
 SEC74  = IS1 *IS11
 SEC75  = IS2 *IS11
 SEC76  = IS1 *IS12
 SEC77  = IS2 *IS12
 SEC78  = IS1 *IS13
 SEC79  = IS2 *IS13
 SEC80  = IS1 *IS14
 SEC81  = IS2 *IS14
 SEC82  = IS1 *IS15
 SEC83  = IS2 *IS15
 SEC84  = IS41
 SEC85  = IS42
 SEC86  = IS43
 SEC87  = IS44
 SEC88  = IS3 *IS12
 SEC89  = IS4 *IS12
 SEC90  = IS5 *IS12
 SEC91  = IS6 *IS12
 SEC92  = IS7 *IS12
 SEC93  = IS3 *IS13
 SEC94  = IS4 *IS13
 SEC95  = IS5 *IS13
 SEC96  = IS6 *IS13
 SEC97  = IS7 *IS13
 SEC98  = IS3 *IS14
 SEC99  = IS4 *IS14
 SEC100  = IS5 *IS14
 SEC101  = IS6 *IS14
 SEC102  = IS7 *IS14
 SEC103  = IS3 *IS15
 SEC104  = IS4 *IS15
 SEC105  = IS5 *IS15
 SEC106  = IS6 *IS15
 SEC107  = IS7 *IS15
 SEC108  = IS1 *IS16
 SEC109  = IS2 *IS16
 SEC110  = IS1 *IS17
 SEC111  = IS2 *IS17
 SEC112  = IS1 *IS18
 SEC113  = IS2 *IS18
 SEC114  = IS1 *IS19
 SEC115  = IS2 *IS19
 SEC116  = IS1 *IS20
 SEC117  = IS2 *IS20
 SEC118  = IS1 *IS21
 SEC119  = IS2 *IS21
 SEC120  = IS1 *IS22
 SEC121  = IS2 *IS22
 SEC122  = IS1 *IS23
 SEC123  = IS2 *IS23
 SEC124  = IS1 *IS24
 SEC125  = IS2 *IS24
 SEC126  = IS1 *IS25
 SEC127  = IS2 *IS25
 SEC128  = IS1 *IS26
 SEC129  = IS2 *IS26
 SEC130  = IS1 *IS27
 SEC131  = IS2 *IS27
 SEC132  = IS45
 SEC133  = IS46
 SEC134  = IS3 *IS21
 SEC135  = IS4 *IS21
 SEC136  = IS5 *IS21
 SEC137  = IS6 *IS21
 SEC138  = IS7 *IS21
 SEC139  = IS3 *IS22
 SEC140  = IS4 *IS22
 SEC141  = IS5 *IS22
 SEC142  = IS6 *IS22
 SEC143  = IS7 *IS22
 SEC144  = IS3 *IS23
 SEC145  = IS4 *IS23
 SEC146  = IS5 *IS23
 SEC147  = IS6 *IS23
 SEC148  = IS7 *IS23
 SEC149  = IS3 *IS24
 SEC150  = IS4 *IS24
 SEC151  = IS5 *IS24
 SEC152  = IS6 *IS24
 SEC153  = IS7 *IS24
 SEC154  = IS3 *IS25
 SEC155  = IS4 *IS25
 SEC156  = IS5 *IS25
 SEC157  = IS6 *IS25
 SEC158  = IS7 *IS25
 SEC159  = IS3 *IS26
 SEC160  = IS4 *IS26
 SEC161  = IS5 *IS26
 SEC162  = IS6 *IS26
 SEC163  = IS7 *IS26
 SEC164  = IS3 *IS27
 SEC165  = IS4 *IS27
 SEC166  = IS5 *IS27
 SEC167  = IS6 *IS27
 SEC168  = IS7 *IS27
 SEC169  = IS1 *IS28
 SEC170  = IS2 *IS28
 SEC171  = IS1 *IS29
 SEC172  = IS2 *IS29
 SEC173  = IS1 *IS30
 SEC174  = IS2 *IS30
 SEC175  = IS1 *IS31
 SEC176  = IS2 *IS31
 SEC177  = IS1 *IS32
 SEC178  = IS2 *IS32
 SEC179  = IS1 *IS33
 SEC180  = IS2 *IS33
 SEC181  = IS1 *IS34
 SEC182  = IS2 *IS34
 SEC183  = IS1 *IS35
 SEC184  = IS2 *IS35
 SEC185  = IS1 *IS36
 SEC186  = IS2 *IS36
 SEC187  = IS1 *IS37
 SEC188  = IS2 *IS37
 SEC189  = IS1 *IS38
 SEC190  = IS2 *IS38
 SEC191  = IS1 *IS39
 SEC192  = IS2 *IS39
 SEC193  = IS1 *IS40
 SEC194  = IS2 *IS40

return (@SVector [P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,]), (@SVector [SEC1,SEC2,SEC3,SEC4,SEC5,SEC6,SEC7,SEC8,SEC9,SEC10,SEC11,SEC12,SEC13,SEC14,SEC15,SEC16,SEC17,SEC18,SEC19,SEC20,SEC21,SEC22,SEC23,SEC24,SEC25,SEC26,SEC27,SEC28,SEC29,SEC30,SEC31,SEC32,SEC33,SEC34,SEC35,SEC36,SEC37,SEC38,SEC39,SEC40,SEC41,SEC42,SEC43,SEC44,SEC45,SEC46,SEC47,SEC48,SEC49,SEC50,SEC51,SEC52,SEC53,SEC54,SEC55,SEC56,SEC57,SEC58,SEC59,SEC60,SEC61,SEC62,SEC63,SEC64,SEC65,SEC66,SEC67,SEC68,SEC69,SEC70,SEC71,SEC72,SEC73,SEC74,SEC75,SEC76,SEC77,SEC78,SEC79,SEC80,SEC81,SEC82,SEC83,SEC84,SEC85,SEC86,SEC87,SEC88,SEC89,SEC90,SEC91,SEC92,SEC93,SEC94,SEC95,SEC96,SEC97,SEC98,SEC99,SEC100,SEC101,SEC102,SEC103,SEC104,SEC105,SEC106,SEC107,SEC108,SEC109,SEC110,SEC111,SEC112,SEC113,SEC114,SEC115,SEC116,SEC117,SEC118,SEC119,SEC120,SEC121,SEC122,SEC123,SEC124,SEC125,SEC126,SEC127,SEC128,SEC129,SEC130,SEC131,SEC132,SEC133,SEC134,SEC135,SEC136,SEC137,SEC138,SEC139,SEC140,SEC141,SEC142,SEC143,SEC144,SEC145,SEC146,SEC147,SEC148,SEC149,SEC150,SEC151,SEC152,SEC153,SEC154,SEC155,SEC156,SEC157,SEC158,SEC159,SEC160,SEC161,SEC162,SEC163,SEC164,SEC165,SEC166,SEC167,SEC168,SEC169,SEC170,SEC171,SEC172,SEC173,SEC174,SEC175,SEC176,SEC177,SEC178,SEC179,SEC180,SEC181,SEC182,SEC183,SEC184,SEC185,SEC186,SEC187,SEC188,SEC189,SEC190,SEC191,SEC192,SEC193,SEC194,])
 end

function invariants_d_gen(x1::SVector{10, T}) where {T}
   x2 = x1.*x1
   x3 = x2.*x1
   x4 = x3.*x1
   x5 = x4.*x1
   x6 = x5.*x1
   x7 = x6.*x1
   x8 = x7.*x1
   x9 = x8.*x1

   dx1 = @SVector ones(10)
   dx2 = 2 * x1
   dx3 = 3 * x2
   dx4 = 4 * x3
   dx5 = 5 * x4
   dx6 = 6 * x5
   dx7 = 7 * x6
   dx8 = 8 * x7
   dx9 = 9 * x8
   #------------------------------------------------
   # Primaries
   #------------------------------------------------

# Primary invariants for NBody=5and deg=10
 # : what goes in the function for the derivatives
dP1 = fpoly_d((x1,),(dx1,) , Main.P1)
dP2 = fpoly_d((x1,x1,),(dx1,dx1,) , Main.P2)
dP3 = fpoly_d((x2,),(dx2,) , Main.P3)
dP4 = fpoly_d((x1,x1,x1,),(dx1,dx1,dx1,) , Main.P4)
dP5 = fpoly_d((x3,),(dx3,) , Main.P5)
dP6 = fpoly_d((x1,x1,x1,x1,),(dx1,dx1,dx1,dx1,) , Main.P6)
dP7 = fpoly_d((x4,),(dx4,) , Main.P7)
dP8 = fpoly_d((x1,x1,x1,x1,x1,),(dx1,dx1,dx1,dx1,dx1,) , Main.P8)
dP9 = fpoly_d((x5,),(dx5,) , Main.P9)
dP10 = fpoly_d((x6,),(dx6,) , Main.P10)

   #------------------------------------------------
   # Irreducible secondaries
   #------------------------------------------------

# Irreducible secondaries for NBody=5and deg=10
 # : what goes in the function for the evaluation
IS1 = fpoly((x2,x1,) , Main.IS1)
IS2 = fpoly((x1,x1,x1,) , Main.IS2)
IS3 = fpoly((x3,x1,) , Main.IS3)
IS4 = fpoly((x2,x2,) , Main.IS4)
IS5 = fpoly((x2,x1,x1,) , Main.IS5)
IS6 = fpoly((x2,x1,x1,) , Main.IS6)
IS7 = fpoly((x2,x1,x1,) , Main.IS7)
IS8 = fpoly((x4,x1,) , Main.IS8)
IS9 = fpoly((x3,x2,) , Main.IS9)
IS10 = fpoly((x3,x1,x1,) , Main.IS10)
IS11 = fpoly((x2,x2,x1,) , Main.IS11)
IS12 = fpoly((x3,x1,x1,) , Main.IS12)
IS13 = fpoly((x2,x2,x1,) , Main.IS13)
IS14 = fpoly((x3,x1,x1,) , Main.IS14)
IS15 = fpoly((x2,x1,x1,x1,) , Main.IS15)
IS16 = fpoly((x5,x1,) , Main.IS16)
IS17 = fpoly((x4,x2,) , Main.IS17)
IS18 = fpoly((x3,x3,) , Main.IS18)
IS19 = fpoly((x4,x1,x1,) , Main.IS19)
IS20 = fpoly((x3,x2,x1,) , Main.IS20)
IS21 = fpoly((x2,x2,x2,) , Main.IS21)
IS22 = fpoly((x4,x1,x1,) , Main.IS22)
IS23 = fpoly((x3,x2,x1,) , Main.IS23)
IS24 = fpoly((x4,x1,x1,) , Main.IS24)
IS25 = fpoly((x3,x1,x1,x1,) , Main.IS25)
IS26 = fpoly((x2,x2,x1,x1,) , Main.IS26)
IS27 = fpoly((x3,x2,x1,) , Main.IS27)
IS28 = fpoly((x7,) , Main.IS28)
IS29 = fpoly((x6,x1,) , Main.IS29)
IS30 = fpoly((x5,x2,) , Main.IS30)
IS31 = fpoly((x4,x3,) , Main.IS31)
IS32 = fpoly((x5,x1,x1,) , Main.IS32)
IS33 = fpoly((x4,x2,x1,) , Main.IS33)
IS34 = fpoly((x3,x3,x1,) , Main.IS34)
IS35 = fpoly((x3,x2,x2,) , Main.IS35)
IS36 = fpoly((x5,x1,x1,) , Main.IS36)
IS37 = fpoly((x4,x2,x1,) , Main.IS37)
IS38 = fpoly((x3,x3,x1,) , Main.IS38)
IS39 = fpoly((x5,x1,x1,) , Main.IS39)
IS40 = fpoly((x4,x1,x1,x1,) , Main.IS40)
IS41 = fpoly((x8,) , Main.IS41)
IS42 = fpoly((x7,x1,) , Main.IS42)
IS43 = fpoly((x6,x2,) , Main.IS43)
IS44 = fpoly((x5,x3,) , Main.IS44)
IS45 = fpoly((x9,) , Main.IS45)
IS46 = fpoly((x8,x1,) , Main.IS46)

# Irreducible secondaries for NBody=5and deg=10
 # : what goes in the function for the derivatives
dIS1 = fpoly_d((x2,x1,),(dx2,dx1,) , Main.IS1)
dIS2 = fpoly_d((x1,x1,x1,),(dx1,dx1,dx1,) , Main.IS2)
dIS3 = fpoly_d((x3,x1,),(dx3,dx1,) , Main.IS3)
dIS4 = fpoly_d((x2,x2,),(dx2,dx2,) , Main.IS4)
dIS5 = fpoly_d((x2,x1,x1,),(dx2,dx1,dx1,) , Main.IS5)
dIS6 = fpoly_d((x2,x1,x1,),(dx2,dx1,dx1,) , Main.IS6)
dIS7 = fpoly_d((x2,x1,x1,),(dx2,dx1,dx1,) , Main.IS7)
dIS8 = fpoly_d((x4,x1,),(dx4,dx1,) , Main.IS8)
dIS9 = fpoly_d((x3,x2,),(dx3,dx2,) , Main.IS9)
dIS10 = fpoly_d((x3,x1,x1,),(dx3,dx1,dx1,) , Main.IS10)
dIS11 = fpoly_d((x2,x2,x1,),(dx2,dx2,dx1,) , Main.IS11)
dIS12 = fpoly_d((x3,x1,x1,),(dx3,dx1,dx1,) , Main.IS12)
dIS13 = fpoly_d((x2,x2,x1,),(dx2,dx2,dx1,) , Main.IS13)
dIS14 = fpoly_d((x3,x1,x1,),(dx3,dx1,dx1,) , Main.IS14)
dIS15 = fpoly_d((x2,x1,x1,x1,),(dx2,dx1,dx1,dx1,) , Main.IS15)
dIS16 = fpoly_d((x5,x1,),(dx5,dx1,) , Main.IS16)
dIS17 = fpoly_d((x4,x2,),(dx4,dx2,) , Main.IS17)
dIS18 = fpoly_d((x3,x3,),(dx3,dx3,) , Main.IS18)
dIS19 = fpoly_d((x4,x1,x1,),(dx4,dx1,dx1,) , Main.IS19)
dIS20 = fpoly_d((x3,x2,x1,),(dx3,dx2,dx1,) , Main.IS20)
dIS21 = fpoly_d((x2,x2,x2,),(dx2,dx2,dx2,) , Main.IS21)
dIS22 = fpoly_d((x4,x1,x1,),(dx4,dx1,dx1,) , Main.IS22)
dIS23 = fpoly_d((x3,x2,x1,),(dx3,dx2,dx1,) , Main.IS23)
dIS24 = fpoly_d((x4,x1,x1,),(dx4,dx1,dx1,) , Main.IS24)
dIS25 = fpoly_d((x3,x1,x1,x1,),(dx3,dx1,dx1,dx1,) , Main.IS25)
dIS26 = fpoly_d((x2,x2,x1,x1,),(dx2,dx2,dx1,dx1,) , Main.IS26)
dIS27 = fpoly_d((x3,x2,x1,),(dx3,dx2,dx1,) , Main.IS27)
dIS28 = fpoly_d((x7,),(dx7,) , Main.IS28)
dIS29 = fpoly_d((x6,x1,),(dx6,dx1,) , Main.IS29)
dIS30 = fpoly_d((x5,x2,),(dx5,dx2,) , Main.IS30)
dIS31 = fpoly_d((x4,x3,),(dx4,dx3,) , Main.IS31)
dIS32 = fpoly_d((x5,x1,x1,),(dx5,dx1,dx1,) , Main.IS32)
dIS33 = fpoly_d((x4,x2,x1,),(dx4,dx2,dx1,) , Main.IS33)
dIS34 = fpoly_d((x3,x3,x1,),(dx3,dx3,dx1,) , Main.IS34)
dIS35 = fpoly_d((x3,x2,x2,),(dx3,dx2,dx2,) , Main.IS35)
dIS36 = fpoly_d((x5,x1,x1,),(dx5,dx1,dx1,) , Main.IS36)
dIS37 = fpoly_d((x4,x2,x1,),(dx4,dx2,dx1,) , Main.IS37)
dIS38 = fpoly_d((x3,x3,x1,),(dx3,dx3,dx1,) , Main.IS38)
dIS39 = fpoly_d((x5,x1,x1,),(dx5,dx1,dx1,) , Main.IS39)
dIS40 = fpoly_d((x4,x1,x1,x1,),(dx4,dx1,dx1,dx1,) , Main.IS40)
dIS41 = fpoly_d((x8,),(dx8,) , Main.IS41)
dIS42 = fpoly_d((x7,x1,),(dx7,dx1,) , Main.IS42)
dIS43 = fpoly_d((x6,x2,),(dx6,dx2,) , Main.IS43)
dIS44 = fpoly_d((x5,x3,),(dx5,dx3,) , Main.IS44)
dIS45 = fpoly_d((x9,),(dx9,) , Main.IS45)
dIS46 = fpoly_d((x8,x1,),(dx8,dx1,) , Main.IS46)

   #------------------------------------------------
   # All secondaries
   #------------------------------------------------

 dSEC1   = @SVector zeros(10)
 dSEC2  = dIS1
 dSEC3  = dIS2
 dSEC4  = dIS3
 dSEC5  = dIS4
 dSEC6  = dIS5
 dSEC7  = dIS6
 dSEC8  = dIS7
 dSEC9  = dIS8
 dSEC10  = dIS9
 dSEC11  = dIS10
 dSEC12  = dIS11
 dSEC13  = dIS12
 dSEC14  = dIS13
 dSEC15  = dIS14
 dSEC16  = dIS15
 dSEC17   =  dIS1 *IS1 + IS1 *dIS1
 dSEC18   =  dIS1 *IS2 + IS1 *dIS2
 dSEC19   =  dIS2 *IS2 + IS2 *dIS2
 dSEC20  = dIS16
 dSEC21  = dIS17
 dSEC22  = dIS18
 dSEC23  = dIS19
 dSEC24  = dIS20
 dSEC25  = dIS21
 dSEC26  = dIS22
 dSEC27  = dIS23
 dSEC28  = dIS24
 dSEC29  = dIS25
 dSEC30  = dIS26
 dSEC31  = dIS27
 dSEC32   =  dIS1 *IS3 + IS1 *dIS3
 dSEC33   =  dIS2 *IS3 + IS2 *dIS3
 dSEC34   =  dIS1 *IS4 + IS1 *dIS4
 dSEC35   =  dIS2 *IS4 + IS2 *dIS4
 dSEC36   =  dIS1 *IS5 + IS1 *dIS5
 dSEC37   =  dIS2 *IS5 + IS2 *dIS5
 dSEC38   =  dIS1 *IS6 + IS1 *dIS6
 dSEC39   =  dIS2 *IS6 + IS2 *dIS6
 dSEC40   =  dIS1 *IS7 + IS1 *dIS7
 dSEC41   =  dIS2 *IS7 + IS2 *dIS7
 dSEC42  = dIS28
 dSEC43  = dIS29
 dSEC44  = dIS30
 dSEC45  = dIS31
 dSEC46  = dIS32
 dSEC47  = dIS33
 dSEC48  = dIS34
 dSEC49  = dIS35
 dSEC50  = dIS36
 dSEC51  = dIS37
 dSEC52  = dIS38
 dSEC53  = dIS39
 dSEC54  = dIS40
 dSEC55   =  dIS4 *IS4 + IS4 *dIS4
 dSEC56   =  dIS3 *IS5 + IS3 *dIS5
 dSEC57   =  dIS4 *IS5 + IS4 *dIS5
 dSEC58   =  dIS5 *IS5 + IS5 *dIS5
 dSEC59   =  dIS3 *IS6 + IS3 *dIS6
 dSEC60   =  dIS4 *IS6 + IS4 *dIS6
 dSEC61   =  dIS5 *IS6 + IS5 *dIS6
 dSEC62   =  dIS6 *IS6 + IS6 *dIS6
 dSEC63   =  dIS3 *IS7 + IS3 *dIS7
 dSEC64   =  dIS4 *IS7 + IS4 *dIS7
 dSEC65   =  dIS5 *IS7 + IS5 *dIS7
 dSEC66   =  dIS6 *IS7 + IS6 *dIS7
 dSEC67   =  dIS7 *IS7 + IS7 *dIS7
 dSEC68   =  dIS1 *IS8 + IS1 *dIS8
 dSEC69   =  dIS2 *IS8 + IS2 *dIS8
 dSEC70   =  dIS1 *IS9 + IS1 *dIS9
 dSEC71   =  dIS2 *IS9 + IS2 *dIS9
 dSEC72   =  dIS1 *IS10 + IS1 *dIS10
 dSEC73   =  dIS2 *IS10 + IS2 *dIS10
 dSEC74   =  dIS1 *IS11 + IS1 *dIS11
 dSEC75   =  dIS2 *IS11 + IS2 *dIS11
 dSEC76   =  dIS1 *IS12 + IS1 *dIS12
 dSEC77   =  dIS2 *IS12 + IS2 *dIS12
 dSEC78   =  dIS1 *IS13 + IS1 *dIS13
 dSEC79   =  dIS2 *IS13 + IS2 *dIS13
 dSEC80   =  dIS1 *IS14 + IS1 *dIS14
 dSEC81   =  dIS2 *IS14 + IS2 *dIS14
 dSEC82   =  dIS1 *IS15 + IS1 *dIS15
 dSEC83   =  dIS2 *IS15 + IS2 *dIS15
 dSEC84  = dIS41
 dSEC85  = dIS42
 dSEC86  = dIS43
 dSEC87  = dIS44
 dSEC88   =  dIS3 *IS12 + IS3 *dIS12
 dSEC89   =  dIS4 *IS12 + IS4 *dIS12
 dSEC90   =  dIS5 *IS12 + IS5 *dIS12
 dSEC91   =  dIS6 *IS12 + IS6 *dIS12
 dSEC92   =  dIS7 *IS12 + IS7 *dIS12
 dSEC93   =  dIS3 *IS13 + IS3 *dIS13
 dSEC94   =  dIS4 *IS13 + IS4 *dIS13
 dSEC95   =  dIS5 *IS13 + IS5 *dIS13
 dSEC96   =  dIS6 *IS13 + IS6 *dIS13
 dSEC97   =  dIS7 *IS13 + IS7 *dIS13
 dSEC98   =  dIS3 *IS14 + IS3 *dIS14
 dSEC99   =  dIS4 *IS14 + IS4 *dIS14
 dSEC100   =  dIS5 *IS14 + IS5 *dIS14
 dSEC101   =  dIS6 *IS14 + IS6 *dIS14
 dSEC102   =  dIS7 *IS14 + IS7 *dIS14
 dSEC103   =  dIS3 *IS15 + IS3 *dIS15
 dSEC104   =  dIS4 *IS15 + IS4 *dIS15
 dSEC105   =  dIS5 *IS15 + IS5 *dIS15
 dSEC106   =  dIS6 *IS15 + IS6 *dIS15
 dSEC107   =  dIS7 *IS15 + IS7 *dIS15
 dSEC108   =  dIS1 *IS16 + IS1 *dIS16
 dSEC109   =  dIS2 *IS16 + IS2 *dIS16
 dSEC110   =  dIS1 *IS17 + IS1 *dIS17
 dSEC111   =  dIS2 *IS17 + IS2 *dIS17
 dSEC112   =  dIS1 *IS18 + IS1 *dIS18
 dSEC113   =  dIS2 *IS18 + IS2 *dIS18
 dSEC114   =  dIS1 *IS19 + IS1 *dIS19
 dSEC115   =  dIS2 *IS19 + IS2 *dIS19
 dSEC116   =  dIS1 *IS20 + IS1 *dIS20
 dSEC117   =  dIS2 *IS20 + IS2 *dIS20
 dSEC118   =  dIS1 *IS21 + IS1 *dIS21
 dSEC119   =  dIS2 *IS21 + IS2 *dIS21
 dSEC120   =  dIS1 *IS22 + IS1 *dIS22
 dSEC121   =  dIS2 *IS22 + IS2 *dIS22
 dSEC122   =  dIS1 *IS23 + IS1 *dIS23
 dSEC123   =  dIS2 *IS23 + IS2 *dIS23
 dSEC124   =  dIS1 *IS24 + IS1 *dIS24
 dSEC125   =  dIS2 *IS24 + IS2 *dIS24
 dSEC126   =  dIS1 *IS25 + IS1 *dIS25
 dSEC127   =  dIS2 *IS25 + IS2 *dIS25
 dSEC128   =  dIS1 *IS26 + IS1 *dIS26
 dSEC129   =  dIS2 *IS26 + IS2 *dIS26
 dSEC130   =  dIS1 *IS27 + IS1 *dIS27
 dSEC131   =  dIS2 *IS27 + IS2 *dIS27
 dSEC132  = dIS45
 dSEC133  = dIS46
 dSEC134   =  dIS3 *IS21 + IS3 *dIS21
 dSEC135   =  dIS4 *IS21 + IS4 *dIS21
 dSEC136   =  dIS5 *IS21 + IS5 *dIS21
 dSEC137   =  dIS6 *IS21 + IS6 *dIS21
 dSEC138   =  dIS7 *IS21 + IS7 *dIS21
 dSEC139   =  dIS3 *IS22 + IS3 *dIS22
 dSEC140   =  dIS4 *IS22 + IS4 *dIS22
 dSEC141   =  dIS5 *IS22 + IS5 *dIS22
 dSEC142   =  dIS6 *IS22 + IS6 *dIS22
 dSEC143   =  dIS7 *IS22 + IS7 *dIS22
 dSEC144   =  dIS3 *IS23 + IS3 *dIS23
 dSEC145   =  dIS4 *IS23 + IS4 *dIS23
 dSEC146   =  dIS5 *IS23 + IS5 *dIS23
 dSEC147   =  dIS6 *IS23 + IS6 *dIS23
 dSEC148   =  dIS7 *IS23 + IS7 *dIS23
 dSEC149   =  dIS3 *IS24 + IS3 *dIS24
 dSEC150   =  dIS4 *IS24 + IS4 *dIS24
 dSEC151   =  dIS5 *IS24 + IS5 *dIS24
 dSEC152   =  dIS6 *IS24 + IS6 *dIS24
 dSEC153   =  dIS7 *IS24 + IS7 *dIS24
 dSEC154   =  dIS3 *IS25 + IS3 *dIS25
 dSEC155   =  dIS4 *IS25 + IS4 *dIS25
 dSEC156   =  dIS5 *IS25 + IS5 *dIS25
 dSEC157   =  dIS6 *IS25 + IS6 *dIS25
 dSEC158   =  dIS7 *IS25 + IS7 *dIS25
 dSEC159   =  dIS3 *IS26 + IS3 *dIS26
 dSEC160   =  dIS4 *IS26 + IS4 *dIS26
 dSEC161   =  dIS5 *IS26 + IS5 *dIS26
 dSEC162   =  dIS6 *IS26 + IS6 *dIS26
 dSEC163   =  dIS7 *IS26 + IS7 *dIS26
 dSEC164   =  dIS3 *IS27 + IS3 *dIS27
 dSEC165   =  dIS4 *IS27 + IS4 *dIS27
 dSEC166   =  dIS5 *IS27 + IS5 *dIS27
 dSEC167   =  dIS6 *IS27 + IS6 *dIS27
 dSEC168   =  dIS7 *IS27 + IS7 *dIS27
 dSEC169   =  dIS1 *IS28 + IS1 *dIS28
 dSEC170   =  dIS2 *IS28 + IS2 *dIS28
 dSEC171   =  dIS1 *IS29 + IS1 *dIS29
 dSEC172   =  dIS2 *IS29 + IS2 *dIS29
 dSEC173   =  dIS1 *IS30 + IS1 *dIS30
 dSEC174   =  dIS2 *IS30 + IS2 *dIS30
 dSEC175   =  dIS1 *IS31 + IS1 *dIS31
 dSEC176   =  dIS2 *IS31 + IS2 *dIS31
 dSEC177   =  dIS1 *IS32 + IS1 *dIS32
 dSEC178   =  dIS2 *IS32 + IS2 *dIS32
 dSEC179   =  dIS1 *IS33 + IS1 *dIS33
 dSEC180   =  dIS2 *IS33 + IS2 *dIS33
 dSEC181   =  dIS1 *IS34 + IS1 *dIS34
 dSEC182   =  dIS2 *IS34 + IS2 *dIS34
 dSEC183   =  dIS1 *IS35 + IS1 *dIS35
 dSEC184   =  dIS2 *IS35 + IS2 *dIS35
 dSEC185   =  dIS1 *IS36 + IS1 *dIS36
 dSEC186   =  dIS2 *IS36 + IS2 *dIS36
 dSEC187   =  dIS1 *IS37 + IS1 *dIS37
 dSEC188   =  dIS2 *IS37 + IS2 *dIS37
 dSEC189   =  dIS1 *IS38 + IS1 *dIS38
 dSEC190   =  dIS2 *IS38 + IS2 *dIS38
 dSEC191   =  dIS1 *IS39 + IS1 *dIS39
 dSEC192   =  dIS2 *IS39 + IS2 *dIS39
 dSEC193   =  dIS1 *IS40 + IS1 *dIS40
 dSEC194   =  dIS2 *IS40 + IS2 *dIS40

return (dP1,dP2,dP3,dP4,dP5,dP6,dP7,dP8,dP9,dP10,), (dSEC1,dSEC2,dSEC3,dSEC4,dSEC5,dSEC6,dSEC7,dSEC8,dSEC9,dSEC10,dSEC11,dSEC12,dSEC13,dSEC14,dSEC15,dSEC16,dSEC17,dSEC18,dSEC19,dSEC20,dSEC21,dSEC22,dSEC23,dSEC24,dSEC25,dSEC26,dSEC27,dSEC28,dSEC29,dSEC30,dSEC31,dSEC32,dSEC33,dSEC34,dSEC35,dSEC36,dSEC37,dSEC38,dSEC39,dSEC40,dSEC41,dSEC42,dSEC43,dSEC44,dSEC45,dSEC46,dSEC47,dSEC48,dSEC49,dSEC50,dSEC51,dSEC52,dSEC53,dSEC54,dSEC55,dSEC56,dSEC57,dSEC58,dSEC59,dSEC60,dSEC61,dSEC62,dSEC63,dSEC64,dSEC65,dSEC66,dSEC67,dSEC68,dSEC69,dSEC70,dSEC71,dSEC72,dSEC73,dSEC74,dSEC75,dSEC76,dSEC77,dSEC78,dSEC79,dSEC80,dSEC81,dSEC82,dSEC83,dSEC84,dSEC85,dSEC86,dSEC87,dSEC88,dSEC89,dSEC90,dSEC91,dSEC92,dSEC93,dSEC94,dSEC95,dSEC96,dSEC97,dSEC98,dSEC99,dSEC100,dSEC101,dSEC102,dSEC103,dSEC104,dSEC105,dSEC106,dSEC107,dSEC108,dSEC109,dSEC110,dSEC111,dSEC112,dSEC113,dSEC114,dSEC115,dSEC116,dSEC117,dSEC118,dSEC119,dSEC120,dSEC121,dSEC122,dSEC123,dSEC124,dSEC125,dSEC126,dSEC127,dSEC128,dSEC129,dSEC130,dSEC131,dSEC132,dSEC133,dSEC134,dSEC135,dSEC136,dSEC137,dSEC138,dSEC139,dSEC140,dSEC141,dSEC142,dSEC143,dSEC144,dSEC145,dSEC146,dSEC147,dSEC148,dSEC149,dSEC150,dSEC151,dSEC152,dSEC153,dSEC154,dSEC155,dSEC156,dSEC157,dSEC158,dSEC159,dSEC160,dSEC161,dSEC162,dSEC163,dSEC164,dSEC165,dSEC166,dSEC167,dSEC168,dSEC169,dSEC170,dSEC171,dSEC172,dSEC173,dSEC174,dSEC175,dSEC176,dSEC177,dSEC178,dSEC179,dSEC180,dSEC181,dSEC182,dSEC183,dSEC184,dSEC185,dSEC186,dSEC187,dSEC188,dSEC189,dSEC190,dSEC191,dSEC192,dSEC193,dSEC194,)
 end

function invariants_ed_gen(x1::SVector{10, T}) where {T}
   x2 = x1.*x1
   x3 = x2.*x1
   x4 = x3.*x1
   x5 = x4.*x1
   x6 = x5.*x1
   x7 = x6.*x1
   x8 = x7.*x1
   x9 = x8.*x1

   dx1 = @SVector ones(10)
   dx2 = 2 * x1
   dx3 = 3 * x2
   dx4 = 4 * x3
   dx5 = 5 * x4
   dx6 = 6 * x5
   dx7 = 7 * x6
   dx8 = 8 * x7
   dx9 = 9 * x8
   #------------------------------------------------
   # Primaries
   #------------------------------------------------

# Primary invariants for NBody=5and deg=10
 # : what goes in the function for the evaluation and derivatives
P1, dP1 = fpoly_ed((x1,),(dx1,) , Main.P1)
P2, dP2 = fpoly_ed((x1,x1,),(dx1,dx1,) , Main.P2)
P3, dP3 = fpoly_ed((x2,),(dx2,) , Main.P3)
P4, dP4 = fpoly_ed((x1,x1,x1,),(dx1,dx1,dx1,) , Main.P4)
P5, dP5 = fpoly_ed((x3,),(dx3,) , Main.P5)
P6, dP6 = fpoly_ed((x1,x1,x1,x1,),(dx1,dx1,dx1,dx1,) , Main.P6)
P7, dP7 = fpoly_ed((x4,),(dx4,) , Main.P7)
P8, dP8 = fpoly_ed((x1,x1,x1,x1,x1,),(dx1,dx1,dx1,dx1,dx1,) , Main.P8)
P9, dP9 = fpoly_ed((x5,),(dx5,) , Main.P9)
P10, dP10 = fpoly_ed((x6,),(dx6,) , Main.P10)

   #------------------------------------------------
   # Irreducible secondaries
   #------------------------------------------------

# Irreducible secondaries for NBody=5and deg=10
 # : what goes in the function for the evaluation and derivatives
IS1, dIS1 = fpoly_ed((x2,x1,),(dx2,dx1,) , Main.IS1)
IS2, dIS2 = fpoly_ed((x1,x1,x1,),(dx1,dx1,dx1,) , Main.IS2)
IS3, dIS3 = fpoly_ed((x3,x1,),(dx3,dx1,) , Main.IS3)
IS4, dIS4 = fpoly_ed((x2,x2,),(dx2,dx2,) , Main.IS4)
IS5, dIS5 = fpoly_ed((x2,x1,x1,),(dx2,dx1,dx1,) , Main.IS5)
IS6, dIS6 = fpoly_ed((x2,x1,x1,),(dx2,dx1,dx1,) , Main.IS6)
IS7, dIS7 = fpoly_ed((x2,x1,x1,),(dx2,dx1,dx1,) , Main.IS7)
IS8, dIS8 = fpoly_ed((x4,x1,),(dx4,dx1,) , Main.IS8)
IS9, dIS9 = fpoly_ed((x3,x2,),(dx3,dx2,) , Main.IS9)
IS10, dIS10 = fpoly_ed((x3,x1,x1,),(dx3,dx1,dx1,) , Main.IS10)
IS11, dIS11 = fpoly_ed((x2,x2,x1,),(dx2,dx2,dx1,) , Main.IS11)
IS12, dIS12 = fpoly_ed((x3,x1,x1,),(dx3,dx1,dx1,) , Main.IS12)
IS13, dIS13 = fpoly_ed((x2,x2,x1,),(dx2,dx2,dx1,) , Main.IS13)
IS14, dIS14 = fpoly_ed((x3,x1,x1,),(dx3,dx1,dx1,) , Main.IS14)
IS15, dIS15 = fpoly_ed((x2,x1,x1,x1,),(dx2,dx1,dx1,dx1,) , Main.IS15)
IS16, dIS16 = fpoly_ed((x5,x1,),(dx5,dx1,) , Main.IS16)
IS17, dIS17 = fpoly_ed((x4,x2,),(dx4,dx2,) , Main.IS17)
IS18, dIS18 = fpoly_ed((x3,x3,),(dx3,dx3,) , Main.IS18)
IS19, dIS19 = fpoly_ed((x4,x1,x1,),(dx4,dx1,dx1,) , Main.IS19)
IS20, dIS20 = fpoly_ed((x3,x2,x1,),(dx3,dx2,dx1,) , Main.IS20)
IS21, dIS21 = fpoly_ed((x2,x2,x2,),(dx2,dx2,dx2,) , Main.IS21)
IS22, dIS22 = fpoly_ed((x4,x1,x1,),(dx4,dx1,dx1,) , Main.IS22)
IS23, dIS23 = fpoly_ed((x3,x2,x1,),(dx3,dx2,dx1,) , Main.IS23)
IS24, dIS24 = fpoly_ed((x4,x1,x1,),(dx4,dx1,dx1,) , Main.IS24)
IS25, dIS25 = fpoly_ed((x3,x1,x1,x1,),(dx3,dx1,dx1,dx1,) , Main.IS25)
IS26, dIS26 = fpoly_ed((x2,x2,x1,x1,),(dx2,dx2,dx1,dx1,) , Main.IS26)
IS27, dIS27 = fpoly_ed((x3,x2,x1,),(dx3,dx2,dx1,) , Main.IS27)
IS28, dIS28 = fpoly_ed((x7,),(dx7,) , Main.IS28)
IS29, dIS29 = fpoly_ed((x6,x1,),(dx6,dx1,) , Main.IS29)
IS30, dIS30 = fpoly_ed((x5,x2,),(dx5,dx2,) , Main.IS30)
IS31, dIS31 = fpoly_ed((x4,x3,),(dx4,dx3,) , Main.IS31)
IS32, dIS32 = fpoly_ed((x5,x1,x1,),(dx5,dx1,dx1,) , Main.IS32)
IS33, dIS33 = fpoly_ed((x4,x2,x1,),(dx4,dx2,dx1,) , Main.IS33)
IS34, dIS34 = fpoly_ed((x3,x3,x1,),(dx3,dx3,dx1,) , Main.IS34)
IS35, dIS35 = fpoly_ed((x3,x2,x2,),(dx3,dx2,dx2,) , Main.IS35)
IS36, dIS36 = fpoly_ed((x5,x1,x1,),(dx5,dx1,dx1,) , Main.IS36)
IS37, dIS37 = fpoly_ed((x4,x2,x1,),(dx4,dx2,dx1,) , Main.IS37)
IS38, dIS38 = fpoly_ed((x3,x3,x1,),(dx3,dx3,dx1,) , Main.IS38)
IS39, dIS39 = fpoly_ed((x5,x1,x1,),(dx5,dx1,dx1,) , Main.IS39)
IS40, dIS40 = fpoly_ed((x4,x1,x1,x1,),(dx4,dx1,dx1,dx1,) , Main.IS40)
IS41, dIS41 = fpoly_ed((x8,),(dx8,) , Main.IS41)
IS42, dIS42 = fpoly_ed((x7,x1,),(dx7,dx1,) , Main.IS42)
IS43, dIS43 = fpoly_ed((x6,x2,),(dx6,dx2,) , Main.IS43)
IS44, dIS44 = fpoly_ed((x5,x3,),(dx5,dx3,) , Main.IS44)
IS45, dIS45 = fpoly_ed((x9,),(dx9,) , Main.IS45)
IS46, dIS46 = fpoly_ed((x8,x1,),(dx8,dx1,) , Main.IS46)

   #------------------------------------------------
   # All secondaries
   #------------------------------------------------

 SEC1  = 1
 SEC2  = IS1
 SEC3  = IS2
 SEC4  = IS3
 SEC5  = IS4
 SEC6  = IS5
 SEC7  = IS6
 SEC8  = IS7
 SEC9  = IS8
 SEC10  = IS9
 SEC11  = IS10
 SEC12  = IS11
 SEC13  = IS12
 SEC14  = IS13
 SEC15  = IS14
 SEC16  = IS15
 SEC17  = IS1 *IS1
 SEC18  = IS1 *IS2
 SEC19  = IS2 *IS2
 SEC20  = IS16
 SEC21  = IS17
 SEC22  = IS18
 SEC23  = IS19
 SEC24  = IS20
 SEC25  = IS21
 SEC26  = IS22
 SEC27  = IS23
 SEC28  = IS24
 SEC29  = IS25
 SEC30  = IS26
 SEC31  = IS27
 SEC32  = IS1 *IS3
 SEC33  = IS2 *IS3
 SEC34  = IS1 *IS4
 SEC35  = IS2 *IS4
 SEC36  = IS1 *IS5
 SEC37  = IS2 *IS5
 SEC38  = IS1 *IS6
 SEC39  = IS2 *IS6
 SEC40  = IS1 *IS7
 SEC41  = IS2 *IS7
 SEC42  = IS28
 SEC43  = IS29
 SEC44  = IS30
 SEC45  = IS31
 SEC46  = IS32
 SEC47  = IS33
 SEC48  = IS34
 SEC49  = IS35
 SEC50  = IS36
 SEC51  = IS37
 SEC52  = IS38
 SEC53  = IS39
 SEC54  = IS40
 SEC55  = IS4 *IS4
 SEC56  = IS3 *IS5
 SEC57  = IS4 *IS5
 SEC58  = IS5 *IS5
 SEC59  = IS3 *IS6
 SEC60  = IS4 *IS6
 SEC61  = IS5 *IS6
 SEC62  = IS6 *IS6
 SEC63  = IS3 *IS7
 SEC64  = IS4 *IS7
 SEC65  = IS5 *IS7
 SEC66  = IS6 *IS7
 SEC67  = IS7 *IS7
 SEC68  = IS1 *IS8
 SEC69  = IS2 *IS8
 SEC70  = IS1 *IS9
 SEC71  = IS2 *IS9
 SEC72  = IS1 *IS10
 SEC73  = IS2 *IS10
 SEC74  = IS1 *IS11
 SEC75  = IS2 *IS11
 SEC76  = IS1 *IS12
 SEC77  = IS2 *IS12
 SEC78  = IS1 *IS13
 SEC79  = IS2 *IS13
 SEC80  = IS1 *IS14
 SEC81  = IS2 *IS14
 SEC82  = IS1 *IS15
 SEC83  = IS2 *IS15
 SEC84  = IS41
 SEC85  = IS42
 SEC86  = IS43
 SEC87  = IS44
 SEC88  = IS3 *IS12
 SEC89  = IS4 *IS12
 SEC90  = IS5 *IS12
 SEC91  = IS6 *IS12
 SEC92  = IS7 *IS12
 SEC93  = IS3 *IS13
 SEC94  = IS4 *IS13
 SEC95  = IS5 *IS13
 SEC96  = IS6 *IS13
 SEC97  = IS7 *IS13
 SEC98  = IS3 *IS14
 SEC99  = IS4 *IS14
 SEC100  = IS5 *IS14
 SEC101  = IS6 *IS14
 SEC102  = IS7 *IS14
 SEC103  = IS3 *IS15
 SEC104  = IS4 *IS15
 SEC105  = IS5 *IS15
 SEC106  = IS6 *IS15
 SEC107  = IS7 *IS15
 SEC108  = IS1 *IS16
 SEC109  = IS2 *IS16
 SEC110  = IS1 *IS17
 SEC111  = IS2 *IS17
 SEC112  = IS1 *IS18
 SEC113  = IS2 *IS18
 SEC114  = IS1 *IS19
 SEC115  = IS2 *IS19
 SEC116  = IS1 *IS20
 SEC117  = IS2 *IS20
 SEC118  = IS1 *IS21
 SEC119  = IS2 *IS21
 SEC120  = IS1 *IS22
 SEC121  = IS2 *IS22
 SEC122  = IS1 *IS23
 SEC123  = IS2 *IS23
 SEC124  = IS1 *IS24
 SEC125  = IS2 *IS24
 SEC126  = IS1 *IS25
 SEC127  = IS2 *IS25
 SEC128  = IS1 *IS26
 SEC129  = IS2 *IS26
 SEC130  = IS1 *IS27
 SEC131  = IS2 *IS27
 SEC132  = IS45
 SEC133  = IS46
 SEC134  = IS3 *IS21
 SEC135  = IS4 *IS21
 SEC136  = IS5 *IS21
 SEC137  = IS6 *IS21
 SEC138  = IS7 *IS21
 SEC139  = IS3 *IS22
 SEC140  = IS4 *IS22
 SEC141  = IS5 *IS22
 SEC142  = IS6 *IS22
 SEC143  = IS7 *IS22
 SEC144  = IS3 *IS23
 SEC145  = IS4 *IS23
 SEC146  = IS5 *IS23
 SEC147  = IS6 *IS23
 SEC148  = IS7 *IS23
 SEC149  = IS3 *IS24
 SEC150  = IS4 *IS24
 SEC151  = IS5 *IS24
 SEC152  = IS6 *IS24
 SEC153  = IS7 *IS24
 SEC154  = IS3 *IS25
 SEC155  = IS4 *IS25
 SEC156  = IS5 *IS25
 SEC157  = IS6 *IS25
 SEC158  = IS7 *IS25
 SEC159  = IS3 *IS26
 SEC160  = IS4 *IS26
 SEC161  = IS5 *IS26
 SEC162  = IS6 *IS26
 SEC163  = IS7 *IS26
 SEC164  = IS3 *IS27
 SEC165  = IS4 *IS27
 SEC166  = IS5 *IS27
 SEC167  = IS6 *IS27
 SEC168  = IS7 *IS27
 SEC169  = IS1 *IS28
 SEC170  = IS2 *IS28
 SEC171  = IS1 *IS29
 SEC172  = IS2 *IS29
 SEC173  = IS1 *IS30
 SEC174  = IS2 *IS30
 SEC175  = IS1 *IS31
 SEC176  = IS2 *IS31
 SEC177  = IS1 *IS32
 SEC178  = IS2 *IS32
 SEC179  = IS1 *IS33
 SEC180  = IS2 *IS33
 SEC181  = IS1 *IS34
 SEC182  = IS2 *IS34
 SEC183  = IS1 *IS35
 SEC184  = IS2 *IS35
 SEC185  = IS1 *IS36
 SEC186  = IS2 *IS36
 SEC187  = IS1 *IS37
 SEC188  = IS2 *IS37
 SEC189  = IS1 *IS38
 SEC190  = IS2 *IS38
 SEC191  = IS1 *IS39
 SEC192  = IS2 *IS39
 SEC193  = IS1 *IS40
 SEC194  = IS2 *IS40

 dSEC1   = @SVector zeros(10)
 dSEC2  = dIS1
 dSEC3  = dIS2
 dSEC4  = dIS3
 dSEC5  = dIS4
 dSEC6  = dIS5
 dSEC7  = dIS6
 dSEC8  = dIS7
 dSEC9  = dIS8
 dSEC10  = dIS9
 dSEC11  = dIS10
 dSEC12  = dIS11
 dSEC13  = dIS12
 dSEC14  = dIS13
 dSEC15  = dIS14
 dSEC16  = dIS15
 dSEC17   =  dIS1 *IS1 + IS1 *dIS1
 dSEC18   =  dIS1 *IS2 + IS1 *dIS2
 dSEC19   =  dIS2 *IS2 + IS2 *dIS2
 dSEC20  = dIS16
 dSEC21  = dIS17
 dSEC22  = dIS18
 dSEC23  = dIS19
 dSEC24  = dIS20
 dSEC25  = dIS21
 dSEC26  = dIS22
 dSEC27  = dIS23
 dSEC28  = dIS24
 dSEC29  = dIS25
 dSEC30  = dIS26
 dSEC31  = dIS27
 dSEC32   =  dIS1 *IS3 + IS1 *dIS3
 dSEC33   =  dIS2 *IS3 + IS2 *dIS3
 dSEC34   =  dIS1 *IS4 + IS1 *dIS4
 dSEC35   =  dIS2 *IS4 + IS2 *dIS4
 dSEC36   =  dIS1 *IS5 + IS1 *dIS5
 dSEC37   =  dIS2 *IS5 + IS2 *dIS5
 dSEC38   =  dIS1 *IS6 + IS1 *dIS6
 dSEC39   =  dIS2 *IS6 + IS2 *dIS6
 dSEC40   =  dIS1 *IS7 + IS1 *dIS7
 dSEC41   =  dIS2 *IS7 + IS2 *dIS7
 dSEC42  = dIS28
 dSEC43  = dIS29
 dSEC44  = dIS30
 dSEC45  = dIS31
 dSEC46  = dIS32
 dSEC47  = dIS33
 dSEC48  = dIS34
 dSEC49  = dIS35
 dSEC50  = dIS36
 dSEC51  = dIS37
 dSEC52  = dIS38
 dSEC53  = dIS39
 dSEC54  = dIS40
 dSEC55   =  dIS4 *IS4 + IS4 *dIS4
 dSEC56   =  dIS3 *IS5 + IS3 *dIS5
 dSEC57   =  dIS4 *IS5 + IS4 *dIS5
 dSEC58   =  dIS5 *IS5 + IS5 *dIS5
 dSEC59   =  dIS3 *IS6 + IS3 *dIS6
 dSEC60   =  dIS4 *IS6 + IS4 *dIS6
 dSEC61   =  dIS5 *IS6 + IS5 *dIS6
 dSEC62   =  dIS6 *IS6 + IS6 *dIS6
 dSEC63   =  dIS3 *IS7 + IS3 *dIS7
 dSEC64   =  dIS4 *IS7 + IS4 *dIS7
 dSEC65   =  dIS5 *IS7 + IS5 *dIS7
 dSEC66   =  dIS6 *IS7 + IS6 *dIS7
 dSEC67   =  dIS7 *IS7 + IS7 *dIS7
 dSEC68   =  dIS1 *IS8 + IS1 *dIS8
 dSEC69   =  dIS2 *IS8 + IS2 *dIS8
 dSEC70   =  dIS1 *IS9 + IS1 *dIS9
 dSEC71   =  dIS2 *IS9 + IS2 *dIS9
 dSEC72   =  dIS1 *IS10 + IS1 *dIS10
 dSEC73   =  dIS2 *IS10 + IS2 *dIS10
 dSEC74   =  dIS1 *IS11 + IS1 *dIS11
 dSEC75   =  dIS2 *IS11 + IS2 *dIS11
 dSEC76   =  dIS1 *IS12 + IS1 *dIS12
 dSEC77   =  dIS2 *IS12 + IS2 *dIS12
 dSEC78   =  dIS1 *IS13 + IS1 *dIS13
 dSEC79   =  dIS2 *IS13 + IS2 *dIS13
 dSEC80   =  dIS1 *IS14 + IS1 *dIS14
 dSEC81   =  dIS2 *IS14 + IS2 *dIS14
 dSEC82   =  dIS1 *IS15 + IS1 *dIS15
 dSEC83   =  dIS2 *IS15 + IS2 *dIS15
 dSEC84  = dIS41
 dSEC85  = dIS42
 dSEC86  = dIS43
 dSEC87  = dIS44
 dSEC88   =  dIS3 *IS12 + IS3 *dIS12
 dSEC89   =  dIS4 *IS12 + IS4 *dIS12
 dSEC90   =  dIS5 *IS12 + IS5 *dIS12
 dSEC91   =  dIS6 *IS12 + IS6 *dIS12
 dSEC92   =  dIS7 *IS12 + IS7 *dIS12
 dSEC93   =  dIS3 *IS13 + IS3 *dIS13
 dSEC94   =  dIS4 *IS13 + IS4 *dIS13
 dSEC95   =  dIS5 *IS13 + IS5 *dIS13
 dSEC96   =  dIS6 *IS13 + IS6 *dIS13
 dSEC97   =  dIS7 *IS13 + IS7 *dIS13
 dSEC98   =  dIS3 *IS14 + IS3 *dIS14
 dSEC99   =  dIS4 *IS14 + IS4 *dIS14
 dSEC100   =  dIS5 *IS14 + IS5 *dIS14
 dSEC101   =  dIS6 *IS14 + IS6 *dIS14
 dSEC102   =  dIS7 *IS14 + IS7 *dIS14
 dSEC103   =  dIS3 *IS15 + IS3 *dIS15
 dSEC104   =  dIS4 *IS15 + IS4 *dIS15
 dSEC105   =  dIS5 *IS15 + IS5 *dIS15
 dSEC106   =  dIS6 *IS15 + IS6 *dIS15
 dSEC107   =  dIS7 *IS15 + IS7 *dIS15
 dSEC108   =  dIS1 *IS16 + IS1 *dIS16
 dSEC109   =  dIS2 *IS16 + IS2 *dIS16
 dSEC110   =  dIS1 *IS17 + IS1 *dIS17
 dSEC111   =  dIS2 *IS17 + IS2 *dIS17
 dSEC112   =  dIS1 *IS18 + IS1 *dIS18
 dSEC113   =  dIS2 *IS18 + IS2 *dIS18
 dSEC114   =  dIS1 *IS19 + IS1 *dIS19
 dSEC115   =  dIS2 *IS19 + IS2 *dIS19
 dSEC116   =  dIS1 *IS20 + IS1 *dIS20
 dSEC117   =  dIS2 *IS20 + IS2 *dIS20
 dSEC118   =  dIS1 *IS21 + IS1 *dIS21
 dSEC119   =  dIS2 *IS21 + IS2 *dIS21
 dSEC120   =  dIS1 *IS22 + IS1 *dIS22
 dSEC121   =  dIS2 *IS22 + IS2 *dIS22
 dSEC122   =  dIS1 *IS23 + IS1 *dIS23
 dSEC123   =  dIS2 *IS23 + IS2 *dIS23
 dSEC124   =  dIS1 *IS24 + IS1 *dIS24
 dSEC125   =  dIS2 *IS24 + IS2 *dIS24
 dSEC126   =  dIS1 *IS25 + IS1 *dIS25
 dSEC127   =  dIS2 *IS25 + IS2 *dIS25
 dSEC128   =  dIS1 *IS26 + IS1 *dIS26
 dSEC129   =  dIS2 *IS26 + IS2 *dIS26
 dSEC130   =  dIS1 *IS27 + IS1 *dIS27
 dSEC131   =  dIS2 *IS27 + IS2 *dIS27
 dSEC132  = dIS45
 dSEC133  = dIS46
 dSEC134   =  dIS3 *IS21 + IS3 *dIS21
 dSEC135   =  dIS4 *IS21 + IS4 *dIS21
 dSEC136   =  dIS5 *IS21 + IS5 *dIS21
 dSEC137   =  dIS6 *IS21 + IS6 *dIS21
 dSEC138   =  dIS7 *IS21 + IS7 *dIS21
 dSEC139   =  dIS3 *IS22 + IS3 *dIS22
 dSEC140   =  dIS4 *IS22 + IS4 *dIS22
 dSEC141   =  dIS5 *IS22 + IS5 *dIS22
 dSEC142   =  dIS6 *IS22 + IS6 *dIS22
 dSEC143   =  dIS7 *IS22 + IS7 *dIS22
 dSEC144   =  dIS3 *IS23 + IS3 *dIS23
 dSEC145   =  dIS4 *IS23 + IS4 *dIS23
 dSEC146   =  dIS5 *IS23 + IS5 *dIS23
 dSEC147   =  dIS6 *IS23 + IS6 *dIS23
 dSEC148   =  dIS7 *IS23 + IS7 *dIS23
 dSEC149   =  dIS3 *IS24 + IS3 *dIS24
 dSEC150   =  dIS4 *IS24 + IS4 *dIS24
 dSEC151   =  dIS5 *IS24 + IS5 *dIS24
 dSEC152   =  dIS6 *IS24 + IS6 *dIS24
 dSEC153   =  dIS7 *IS24 + IS7 *dIS24
 dSEC154   =  dIS3 *IS25 + IS3 *dIS25
 dSEC155   =  dIS4 *IS25 + IS4 *dIS25
 dSEC156   =  dIS5 *IS25 + IS5 *dIS25
 dSEC157   =  dIS6 *IS25 + IS6 *dIS25
 dSEC158   =  dIS7 *IS25 + IS7 *dIS25
 dSEC159   =  dIS3 *IS26 + IS3 *dIS26
 dSEC160   =  dIS4 *IS26 + IS4 *dIS26
 dSEC161   =  dIS5 *IS26 + IS5 *dIS26
 dSEC162   =  dIS6 *IS26 + IS6 *dIS26
 dSEC163   =  dIS7 *IS26 + IS7 *dIS26
 dSEC164   =  dIS3 *IS27 + IS3 *dIS27
 dSEC165   =  dIS4 *IS27 + IS4 *dIS27
 dSEC166   =  dIS5 *IS27 + IS5 *dIS27
 dSEC167   =  dIS6 *IS27 + IS6 *dIS27
 dSEC168   =  dIS7 *IS27 + IS7 *dIS27
 dSEC169   =  dIS1 *IS28 + IS1 *dIS28
 dSEC170   =  dIS2 *IS28 + IS2 *dIS28
 dSEC171   =  dIS1 *IS29 + IS1 *dIS29
 dSEC172   =  dIS2 *IS29 + IS2 *dIS29
 dSEC173   =  dIS1 *IS30 + IS1 *dIS30
 dSEC174   =  dIS2 *IS30 + IS2 *dIS30
 dSEC175   =  dIS1 *IS31 + IS1 *dIS31
 dSEC176   =  dIS2 *IS31 + IS2 *dIS31
 dSEC177   =  dIS1 *IS32 + IS1 *dIS32
 dSEC178   =  dIS2 *IS32 + IS2 *dIS32
 dSEC179   =  dIS1 *IS33 + IS1 *dIS33
 dSEC180   =  dIS2 *IS33 + IS2 *dIS33
 dSEC181   =  dIS1 *IS34 + IS1 *dIS34
 dSEC182   =  dIS2 *IS34 + IS2 *dIS34
 dSEC183   =  dIS1 *IS35 + IS1 *dIS35
 dSEC184   =  dIS2 *IS35 + IS2 *dIS35
 dSEC185   =  dIS1 *IS36 + IS1 *dIS36
 dSEC186   =  dIS2 *IS36 + IS2 *dIS36
 dSEC187   =  dIS1 *IS37 + IS1 *dIS37
 dSEC188   =  dIS2 *IS37 + IS2 *dIS37
 dSEC189   =  dIS1 *IS38 + IS1 *dIS38
 dSEC190   =  dIS2 *IS38 + IS2 *dIS38
 dSEC191   =  dIS1 *IS39 + IS1 *dIS39
 dSEC192   =  dIS2 *IS39 + IS2 *dIS39
 dSEC193   =  dIS1 *IS40 + IS1 *dIS40
 dSEC194   =  dIS2 *IS40 + IS2 *dIS40

return (@SVector [P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,]), (@SVector [SEC1,SEC2,SEC3,SEC4,SEC5,SEC6,SEC7,SEC8,SEC9,SEC10,SEC11,SEC12,SEC13,SEC14,SEC15,SEC16,SEC17,SEC18,SEC19,SEC20,SEC21,SEC22,SEC23,SEC24,SEC25,SEC26,SEC27,SEC28,SEC29,SEC30,SEC31,SEC32,SEC33,SEC34,SEC35,SEC36,SEC37,SEC38,SEC39,SEC40,SEC41,SEC42,SEC43,SEC44,SEC45,SEC46,SEC47,SEC48,SEC49,SEC50,SEC51,SEC52,SEC53,SEC54,SEC55,SEC56,SEC57,SEC58,SEC59,SEC60,SEC61,SEC62,SEC63,SEC64,SEC65,SEC66,SEC67,SEC68,SEC69,SEC70,SEC71,SEC72,SEC73,SEC74,SEC75,SEC76,SEC77,SEC78,SEC79,SEC80,SEC81,SEC82,SEC83,SEC84,SEC85,SEC86,SEC87,SEC88,SEC89,SEC90,SEC91,SEC92,SEC93,SEC94,SEC95,SEC96,SEC97,SEC98,SEC99,SEC100,SEC101,SEC102,SEC103,SEC104,SEC105,SEC106,SEC107,SEC108,SEC109,SEC110,SEC111,SEC112,SEC113,SEC114,SEC115,SEC116,SEC117,SEC118,SEC119,SEC120,SEC121,SEC122,SEC123,SEC124,SEC125,SEC126,SEC127,SEC128,SEC129,SEC130,SEC131,SEC132,SEC133,SEC134,SEC135,SEC136,SEC137,SEC138,SEC139,SEC140,SEC141,SEC142,SEC143,SEC144,SEC145,SEC146,SEC147,SEC148,SEC149,SEC150,SEC151,SEC152,SEC153,SEC154,SEC155,SEC156,SEC157,SEC158,SEC159,SEC160,SEC161,SEC162,SEC163,SEC164,SEC165,SEC166,SEC167,SEC168,SEC169,SEC170,SEC171,SEC172,SEC173,SEC174,SEC175,SEC176,SEC177,SEC178,SEC179,SEC180,SEC181,SEC182,SEC183,SEC184,SEC185,SEC186,SEC187,SEC188,SEC189,SEC190,SEC191,SEC192,SEC193,SEC194,]), (@SVector [dP1,dP2,dP3,dP4,dP5,dP6,dP7,dP8,dP9,dP10,]), (@SVector [dSEC1,dSEC2,dSEC3,dSEC4,dSEC5,dSEC6,dSEC7,dSEC8,dSEC9,dSEC10,dSEC11,dSEC12,dSEC13,dSEC14,dSEC15,dSEC16,dSEC17,dSEC18,dSEC19,dSEC20,dSEC21,dSEC22,dSEC23,dSEC24,dSEC25,dSEC26,dSEC27,dSEC28,dSEC29,dSEC30,dSEC31,dSEC32,dSEC33,dSEC34,dSEC35,dSEC36,dSEC37,dSEC38,dSEC39,dSEC40,dSEC41,dSEC42,dSEC43,dSEC44,dSEC45,dSEC46,dSEC47,dSEC48,dSEC49,dSEC50,dSEC51,dSEC52,dSEC53,dSEC54,dSEC55,dSEC56,dSEC57,dSEC58,dSEC59,dSEC60,dSEC61,dSEC62,dSEC63,dSEC64,dSEC65,dSEC66,dSEC67,dSEC68,dSEC69,dSEC70,dSEC71,dSEC72,dSEC73,dSEC74,dSEC75,dSEC76,dSEC77,dSEC78,dSEC79,dSEC80,dSEC81,dSEC82,dSEC83,dSEC84,dSEC85,dSEC86,dSEC87,dSEC88,dSEC89,dSEC90,dSEC91,dSEC92,dSEC93,dSEC94,dSEC95,dSEC96,dSEC97,dSEC98,dSEC99,dSEC100,dSEC101,dSEC102,dSEC103,dSEC104,dSEC105,dSEC106,dSEC107,dSEC108,dSEC109,dSEC110,dSEC111,dSEC112,dSEC113,dSEC114,dSEC115,dSEC116,dSEC117,dSEC118,dSEC119,dSEC120,dSEC121,dSEC122,dSEC123,dSEC124,dSEC125,dSEC126,dSEC127,dSEC128,dSEC129,dSEC130,dSEC131,dSEC132,dSEC133,dSEC134,dSEC135,dSEC136,dSEC137,dSEC138,dSEC139,dSEC140,dSEC141,dSEC142,dSEC143,dSEC144,dSEC145,dSEC146,dSEC147,dSEC148,dSEC149,dSEC150,dSEC151,dSEC152,dSEC153,dSEC154,dSEC155,dSEC156,dSEC157,dSEC158,dSEC159,dSEC160,dSEC161,dSEC162,dSEC163,dSEC164,dSEC165,dSEC166,dSEC167,dSEC168,dSEC169,dSEC170,dSEC171,dSEC172,dSEC173,dSEC174,dSEC175,dSEC176,dSEC177,dSEC178,dSEC179,dSEC180,dSEC181,dSEC182,dSEC183,dSEC184,dSEC185,dSEC186,dSEC187,dSEC188,dSEC189,dSEC190,dSEC191,dSEC192,dSEC193,dSEC194,])
 end

end

max_stars_repo_name: cortner/ManyBodyIPs

max_issues_repo_name: cortner/ManyBodyIPs

max_forks_repo_name: cortner/ManyBodyIPs


text:
# Residuals utilities

##################################################
# Residuals definition
"Add residuals for least squares optimization."
function KN_add_rsds(m::Model, ncons::Integer)
    ptr_cons = zeros(Cint, ncons)
    ret = @kn_ccall(add_rsds, Cint, (Ptr{Cvoid}, Cint, Ptr{Cint}), m.env, ncons, ptr_cons)
    _checkraise(ret)
    return ptr_cons
end

function KN_add_rsd(m::Model)
    ptr_cons = Cint[0]
    ret = @kn_ccall(add_rsd, Cint, (Ptr{Cvoid}, Ptr{Cint}), m.env, ptr_cons)
    _checkraise(ret)
    return ptr_cons[1]
end

##################################################
# Residuals structure
##################################################

# add structure of linear constraint
"""
Add linear structure to the residual functions.
Each component i of arrays indexRsds, indexVars and coefs adds a linear
term:
   coefs[i]*x[indexVars[i]]
to residual r[indexRsds[i]].

"""
function KN_add_rsd_linear_struct(m::Model,
                                  indexRsds::Vector{Cint},
                                  indexVars::Vector{Cint},
                                  coefs::Vector{Float64})
    # get number of constraints
    nnz = length(indexRsds)
    @assert nnz == length(indexVars) == length(coefs)
    ret = @kn_ccall(add_rsd_linear_struct,
                    Cint,
                    (Ptr{Cvoid}, KNLONG, Ptr{Cint}, Ptr{Cint}, Ptr{Cdouble}),
                    m.env,
                    nnz,
                    indexRsds,
                    indexVars,
                    coefs)
    _checkraise(ret)
end

function KN_add_rsd_linear_struct(m::Model,
                                  indexRsd::Integer,
                                  indexVar::Vector{Cint},
                                  coefs::Vector{Float64})
    # get number of constraints
    nnz = length(indexVar)
    @assert nnz == length(coefs)
    ret = @kn_ccall(add_rsd_linear_struct_one,
                    Cint,
                    (Ptr{Cvoid}, KNLONG, Cint, Ptr{Cint}, Ptr{Cdouble}),
                    m.env,
                    nnz,
                    indexRsd,
                    indexVar,
                    coefs)
    _checkraise(ret)
end
KN_add_rsd_linear_struct(m::Model, indexRsd::Integer, indexVar::Integer, coef::Float64) =
    KN_add_rsd_linear_struct(m, indexRsd, Int32[indexVar], [coef])

##################################################
# Residuals getters
##################################################
function KN_get_rsd_values(m::Model)
    nc = KN_get_number_cons(m)
    rsdvals = zeros(Cdouble, nc)
    ret = @kn_ccall(get_rsd_values_all, Cint, (Ptr{Cvoid}, Ptr{Cdouble}),
                    m.env, rsdvals)
    _checkraise(ret)
    return rsdvals
end

function KN_get_rsd_values(m::Model, indexRsds::Vector{Cint})
    nc = length(indexRsds)
    rsdvals = zeros(Cdouble, nc)
    ret = @kn_ccall(get_rsd_values, Cint,
                    (Ptr{Cvoid}, Cint, Ptr{Cint}, Ptr{Cdouble}),
                    m.env, nc, indexRsds, rsdvals)
    _checkraise(ret)
    return rsdvals
end

function KN_get_rsd_values(m::Model, indexRsd::Integer)
    nc = 1
    rsdvals = zeros(Cdouble, nc)
    ret = @kn_ccall(get_rsd_value, Cint, (Ptr{Cvoid}, Cint, Ptr{Cdouble}),
                    m.env, indexRsd, rsdvals)
    _checkraise(ret)
    return rsdvals
end

##################################################
# Residuals constants
##################################################
function KN_add_rsd_constants(m::Model, indexRsds::Vector{Cint}, constants::Vector{Cdouble})
    nnc = length(constants)
    @assert length(indexRsds) == length(constant)
    ret = @kn_ccall(add_rsd_constants, Cint,
                    (Ptr{Cvoid}, Cint, Ptr{Cint}, Ptr{Cdouble}),
                    m.env,
                    nnc,
                    indexRsds,
                    constants)
    _checkraise(ret)
end

function KN_add_rsd_constants(m::Model, constants::Vector{Cdouble})
    nnc = length(constants)
    ret = @kn_ccall(add_rsd_constants_all, Cint,
                    (Ptr{Cvoid}, Ptr{Cdouble}),
                    m.env, constants)
    _checkraise(ret)
end

function KN_add_rsd_constant(m::Model, indexRsd::Integer, constant::Cdouble)
    ret = @kn_ccall(add_rsd_constant, Cint,
                    (Ptr{Cvoid}, Cint, Cdouble),
                    m.env, indexRsd, constant)
    _checkraise(ret)
end

ext: jl


text:
using Base.Threads

"""
    launch_inference_server(f, num_workers)

Launch an inference server.

# Arguments

  - `f` is the function to be evaluated (e.g. a neural network).
    It takes a batch of inputs and returns a batch of results
    of similar size.
  - `num_workers` is the number of workers that are expected to
    query the server. It also corresponds to the batch size that is used to
    evaluate `f`.

# How to use

This function returns a channel along which workers can send queries.
A query can be either:

    - `:none` when a worker is done and about to terminate
    - a named tuple with fields `query` (the input to be given to `f`) and
      `answer_channel` (the channel the sever must use to return its answer).

The server stops automatically after all workers send `:none`.
"""
function launch_inference_server(f, num_workers)
  channel = Channel(num_workers)
  Threads.@spawn begin
    num_active = num_workers
    pending = []
    while num_active > 0
      req = take!(channel)
      if req == :done
        num_active -= 1
      else
        push!(pending, req)
      end
      @assert length(pending) <= num_active
      if length(pending) == num_active
        batch = [p.query for p in pending]
        results = f(batch)
        for i in eachindex(pending)
          put!(pending[i].answer_channel, results[i])
        end
        empty!(pending)
      end
    end
  end
  return channel
end

# Imagine I am evaluating a big neural network on a batch of inputs instead...
function compute_squares(batch)
  return [x^2 for x in batch]
end

# Compute the square of every integer in 1:n using num_workers parallel workers.
# Return the answer in a dictionary.
function generate_squares(; n, num_workers)
  d = n ÷ num_workers
  reqc = launch_inference_server(compute_squares, num_workers)
  results = []
  for i in 1:num_workers
    res = Threads.@spawn begin
      answc = Channel(1)
      squares = Dict()
      xs = [(i-1)*d+j for j in 1:d]
      for x in xs
        put!(reqc, (query=x, answer_channel=answc))
        y = take!(answc)
        squares[x] = y
      end
      put!(reqc, :done)
      return squares
    end
    push!(results, res)
  end
  results = fetch.(results)
  return reduce(merge, results)
end

squares = generate_squares(n=1_000_000, num_workers=10)
for (x, y) in squares
  @assert y == x^2
end


text:
module ShowShadow

using Printf
import SearchBall, GeometryTypes
const sb = SearchBall
const gt = GeometryTypes

function init_strategies(config; kwargs...)
    # myPlayers
    ws = config.ws

    if length(ws.myPlayers) > 0
        sb.add_check_button!(config, "draw function", "draw background")do widget
            if sb.get_value(widget, Bool)
                run_strategies(config, 0.0, :reset)
            else
                sb.clear_background!(config)
            end
        end

    radiobuttons = []
        for myIdx in eachindex(ws.myPlayers)
            myPlayer = ws.myPlayers[myIdx]
            rb = sb.add_radio_button!(config, "rb$myIdx", string(myIdx))do widget
                # if sb.get_manip_value(config, "draw function")
                    run_strategies(config, 0.0, :reset)
                # end
            end
            push!(radiobuttons, rb)
        end
        sb.add_radio_button_group!(config, "show function for", radiobuttons)
    end
    sb.add_check_button!(config, "with hfov", "with hfov") do widget
        run_strategies(config, 0.0, :reset)
    end

    add_shadowcheckbuttons(ws.myPlayers, "myPlayer", config)
    add_shadowcheckbuttons(ws.opponents, "opponent", config)

    return
end

function add_shadowcheckbuttons(player::AbstractVector{<:sb.Robot}, name::AbstractString, config::sb.Config)
    for myplayer_idx in eachindex(player)
        sb.add_check_button!(config, string(name, "Shadow", myplayer_idx), string(name, " ", myplayer_idx)) do widget
            if !sb.get_value(widget, Bool)
                sb.show_text!(config, string(name, " ", myplayer_idx, " area:"), "")
            end
            run_strategies(config)
        end
        sb.init_text!(config, string(name, " ", myplayer_idx, " area:"), "")
    end
end

function handle_shadow(player::AbstractVector{<:sb.Robot}, name::AbstractString, config::sb.Config)
    for myplayer_idx in eachindex(player)
        sb.remove_graphics_filter!(config) do s
            startswith(s, string(name, " ", myplayer_idx, " area:"))
        end

        if !sb.get_manip_value(config, string(name, "Shadow", myplayer_idx))
            continue
        end
        shadows = sb.get_shadow_polygons(player[myplayer_idx], config, sb.get_manip_value(config, "with hfov"))

        shadow_area = sum([sb.polygon_area(s) for s in shadows])

        sb.show_text!(config, string(name, " ", myplayer_idx, " area:"), @sprintf("%.2fm²",shadow_area))

        for (idx, shadow) in enumerate(shadows)
            if isempty(shadow)
                continue
            end
            sb.show_graphic!(config, string(name, " ", myplayer_idx, " area:", idx), shadow[vcat(1:end, 1)], sb.GraphicLine(fill=true, fillcolor=[0.0, 0.0, 0.0, 0.2]))
        end
    end
end

function calc_myPlayers(config::sb.Config, dt::Number, set_type::Symbol, moved_object::Union{sb.Object{<:Number}, Nothing}=nothing)
    ws = config.ws

    handle_shadow(ws.myPlayers, "myPlayer", config)
    handle_shadow(ws.opponents, "opponent", config)

    if length(ws.myPlayers) > 0
        myforfunction = parse(Int, sb.get_manip_value(config, "show function for"))

        if set_type ∈ [:reset, :release] && sb.get_manip_value(config, "draw function") && moved_object!=ws.myPlayers[myforfunction] && (sb.get_manip_value(config, "with hfov") || length(ws.myPlayers) > 1 || length(ws.opponents) > 0)
            vals, ins = sb.sample(config, myforfunction, sb.get_manip_value(config, "with hfov"))
            sb.send_background(vals, ins, config)
        end
    end

    return
end

function calc_opponend(config::sb.Config, dt::Number, set_type::Symbol, moved_object::Union{sb.Object{<:Number}, Nothing}=nothing)
    ws = config.ws

    # do stuff

    return
end

function run_strategies(config::sb.Config, dt::Number=0, set_type::Symbol=:default, moved_object::Union{sb.Object{<:Number}, Nothing}=nothing)
    calc_myPlayers(config, dt, set_type, moved_object)
    calc_opponend(config, dt, set_type, moved_object)
    return
end

end


text:
using DebuggingUtilities
using Test

include("funcdefs.jl")
funcdefs_path = joinpath(@__DIR__, "funcdefs.jl")

@testset "DebuggingUtilities" begin
    io = IOBuffer()
    DebuggingUtilities.showlnio[] = io

    @test foo() == 7

    str = chomp(String(take!(io)))
    target = ("x = 5", "(in $funcdefs_path:4)", "x = 7", "(in $funcdefs_path:6)")
    for (i,ln) in enumerate(split(str, '\n'))
        ln = lstrip(ln)
        @test ln == target[i]
    end

    @test recurses(1) == 10
    str = chomp(String(take!(io)))
    lines = split(str, '\n')
    offset = lstrip(lines[1]).offset
    for i = 1:5
        j = 2*i-1
        k = 4*i-3
        ln = String(lines[k])
        lns = lstrip(ln)
        @test lns.offset == offset + i - 1
        @test lns == "n = $j"
        ln = String(lines[k+1])
        lns = lstrip(ln)
        @test lns.offset == offset + i - 1
        @test lns == "(in recurses at $funcdefs_path:10)"
        j += 1
        ln = String(lines[k+2])
        lns = lstrip(ln)
        @test lns.offset == offset + i - 1
        @test lns == "n = $j"
        ln = String(lines[k+3])
        lns = lstrip(ln)
        @test lns.offset == offset + i - 1
        @test lns == "(in recurses at $funcdefs_path:12)"
    end

    # Just make sure these run
    io = IOBuffer()
    DebuggingUtilities.showlnio[] = io
    test_showline("noerror.jl")
    @test_throws DomainError test_showline("error.jl")
    time_showline("noerror.jl")

    DebuggingUtilities.showlnio[] = stdout
end


text:
const realOptions = [ :IntegratorRelTol, :IntegratorAbsTol, :IntegratorMinStepSize, :LineSearchMax, :LineSearchMin,
                      :LineSearchInit, :LineSearchAdaptAbsTol, :LineSearchAdaptFactor, :LineSearchIntervalTol,
                      :LineSearchIntervalFactor, :OptimParamLineSearchFactor, :OptimTimeLineSearchFactor,
                      :TScale, :TOffset, :JScale, :MultiplierMax, :MultiplierDampingFactor, :PenaltyMax,
                      :PenaltyMin, :PenaltyIncreaseFactor, :PenaltyDecreaseFactor, :PenaltyIncreaseThreshold,
                      :AugLagUpdateGradientRelTol, :ConvergenceGradientRelTol ]

const intOptions = [ :MaxGradIter, :MaxMultIter, :Nhor, :IntegratorMaxSteps ]

const stringOptions = [ :TimeDiscretization, :IntegratorCost, :Integrator, :LineSearchType, :ConstraintsHandling ]

const boolOptions = [ :ShiftControl, :ScaleProblem, :LineSearchExpAutoFallback, :OptimControl,
                      :OptimParam, :OptimTime, :IntegralCost, :TerminalCost, :EqualityConstraints,
                      :InequalityConstraints, :TerminalEqualityConstraints, :TerminalInequalityConstraints,
                      :ConvergenceCheck ]

const realVectorOptions = [ :xScale, :xOffset, :uScale, :uOffset, :pScale, :pOffset, :cScale, :ConstraintsAbsTol ]

const intVectorOptions = [ :FlagsRodas ]

const vectorParameters = [ :x0, :xdes, :u0, :udes, :umax, :umin, :p0, :pmax, :pmin ]

const scalarParameters = [ :Thor, :Tmax, :Tmin, :dt, :t0 ]

"""
    _set_settings!( model::GRAMPC.Model; settings... )

Internal helper function to pass all settings (options and parameters) to the underlying C solver.
"""
function _set_settings!( model::GRAMPC.Model; settings... )
    isempty( settings ) && return

    # We need an initialized model to set the options
    _is_initialized( model ) || error( "Model must be initialized before options can be set" )

    for (key, value) in settings
        keyString = String( key )

        if( key in intOptions )
            ccall( (:grampc_setopt_int, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, typeInt), model.problem, keyString, value )

        elseif( key in realOptions )
            ccall( (:grampc_setopt_real, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, typeRNum), model.problem, keyString, value )

        elseif( key in stringOptions )
            # Look-up the string to use to set the setting
            optDict = optionSymbolStringDict[key]
            valString = optDict[value]
            ccall( (:grampc_setopt_string, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, Cstring), model.problem, keyString, valString )

        elseif( key in boolOptions )
            # Boolean options are handled by passing in a string of either "on" or "off"
            boolString = value ? "on" : "off"
            ccall( (:grampc_setopt_string, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, Cstring), model.problem, keyString, boolString )

        elseif( key in realVectorOptions )
            ccall( (:grampc_setopt_real_vector, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, Ptr{typeRNum}), model.problem, keyString, value )

        elseif( key in intVectorOptions )
            ccall( (:grampc_setopt_int_vector, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, Ptr{typeInt}), model.problem, keyString, value )

        elseif( key in vectorParameters )
            ccall( (:grampc_setparam_real_vector, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, Ptr{typeRNum}), model.problem, keyString, value )

        elseif( key in scalarParameters )
            ccall( (:grampc_setparam_real, libgrampc_julia), Cvoid, (Ptr{CGRAMPC}, Cstring, typeRNum), model.problem, keyString, value )

        else
            error( "Unknown setting $keyString" )

        end
    end
end

function _get_setting( model::GRAMPC.Model, setting::Symbol )
    _is_initialized( model ) || error( "Model not initialized" )

    problem = unsafe_load( model.problem )

    options = unsafe_load( problem.opt )
    params  = unsafe_load( problem.param )

    if( setting in intOptions )
        return getproperty( options, setting )

    elseif( setting in realOptions )
        return getproperty( options, setting )

    elseif( setting in stringOptions )
        # Look-up the string to use to set the setting
        optDict = optionSymbolSymbolDict[setting]
        return optDict[getproperty( options, setting )]

    elseif( setting in boolOptions )
        return boolMap[getproperty( options, setting )]

    elseif( setting in realVectorOptions )
        a = unsafe_wrap( Array, getproperty( options, setting ), _get_setting_vector_length( model, setting ) )
        return a

    elseif( setting in intVectorOptions )
        a = unsafe_wrap( Array, getproperty( options, setting ), _get_setting_vector_length( model, setting ) )
        return a

    elseif( setting in vectorParameters )
        a = unsafe_wrap( Array, getproperty( params, setting ), _get_setting_vector_length( model, setting ) )
        return a

    elseif( setting in scalarParameters )
        return getproperty( params, setting )

    else
        error( "Unknown setting $(String(setting))" )

    end

end

function _get_setting_vector_length( model::GRAMPC.Model, setting::Symbol )
    if setting == :FlagsRodas
        return 8

    elseif setting == :x0 || setting == :xdes || setting == :xScale || setting == :xOffset
        return model.Nx

    elseif setting == :u0 || setting == :udes || setting == :umax || setting == :umin || setting == :uScale || setting == :uOffset
        return model.Nu

    elseif setting == :p0 || setting == :pmax || setting == :pmin || setting == :pScale || setting == :pOffset

        return model.Np
    elseif setting == :cScale || setting == :ConstraintsAbsTol
        problem = unsafe_load( model.problem )
        params  = unsafe_load( problem.param )

        return params.Nc
    end
end


text:
# This file is a part of JuliaPackageTemplate.jl, licensed under the MIT License (MIT).

"""
    JuliaPackageTemplate.hello_world()

Prints "Hello, World!" and returns 42.

```jldoctest
using JuliaPackageTemplate

JuliaPackageTemplate.hello_world()

# output

Hello, World!
42
```
"""
function hello_world()
    println("Hello, World!")
    return 42
end


text:
brng_type = VSL_BRNG_ARS5
info("Testing discrete distributions with $brng_type...")
brng = BasicRandomNumberGenerator(brng_type, 200701281)

stdout_ = STDOUT
pwd_ = pwd()
cd(joinpath(Pkg.dir("VSL"), "test/"))
mkpath("./actual")
outfile = open("./actual/discrete.out", "w")
redirect_stdout(outfile)

t_uniform = UniformDiscrete(brng, Cint(1), Cint(10))
@test_rand(rand(t_uniform))
@test_rand(rand(t_uniform, 2, 3))

t_uniform_bits = UniformBits(brng)
@test_rand(rand(t_uniform_bits))
@test_rand(rand(t_uniform_bits, 2, 3))

t_uniform_bits32 = UniformBits32(brng)
@test_rand(rand(t_uniform_bits32))
@test_rand(rand(t_uniform_bits32, 2, 3))

t_uniform_bits64 = UniformBits64(brng)
@test_rand(rand(t_uniform_bits64))
@test_rand(rand(t_uniform_bits64, 2, 3))

t_bernoulli = Bernoulli(brng, 0.3)
@test_rand(rand(t_bernoulli))
@test_rand(rand(t_bernoulli, 4, 5))

t_geometric = Geometric(brng, 0.6)
@test_rand(rand(t_geometric))
@test_rand(rand(t_geometric, 6, 3))

t_binomial = Binomial(brng, Cint(10), 0.6)
@test_rand(rand(t_binomial))
@test_rand(rand(t_binomial, 6, 3))

t_hypergeometric = Hypergeometric(brng, Cint(10), Cint(5), Cint(6))
@test_rand(rand(t_hypergeometric))
@test_rand(rand(t_hypergeometric, 3, 6))

t_poisson = Poisson(brng, 3.0)
@test_rand(rand(t_poisson))
@test_rand(rand(t_poisson, 3, 6))

t_poisson_v = PoissonV(brng, [1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8])
@test_rand(rand(t_poisson_v))
@test_rand(rand(t_poisson_v, 3, 3))

t_neg_binomial = NegBinomial(brng, 3.0, 0.7)
@test_rand(rand(t_neg_binomial))
@test_rand(rand(t_neg_binomial, 3, 4))

close(outfile)
redirect_stdout(stdout_)

@test success(`diff ./expected/discrete.out ./actual/discrete.out`)
cd(pwd_)


text:
let
    assignment = [1, 2, 1, 2, 1, 3]
    foldset1 = foldset_match(assignment, 1)
    foldset2 = foldset_withhold(assignment, 2)

    @test collect(foldset1) == find(assignment .== 1)
    @test collect(foldset2) == find(v-> v != 2, assignment)
end


text:
using RDatasets, Plots, Measures; pyplot()

data = dataset("datasets", "iris")
println("Number of rows: ", nrow(data))

insertSpace(name) = begin
    i = findlast(isuppercase,name)
    name[1:i-1]*" "*name[i:end]
end

featureNames = insertSpace.(string.(names(data)))[1:4]
println("Names of features:\n\t", featureNames)

speciesNames = unique(data.Species)
speciesFreqs = [sn => sum(data.Species .== sn) for sn in speciesNames]
println("Frequency per species:\n\t", speciesFreqs)

default(msw = 0, ms = 3)

scatters = [
    scatter(data[:,i], data[:,j], c=[:blue :red :green], group=data.Species,
        xlabel=featureNames[i], ylabel=featureNames[j], legend = i==1 && j==1)
    for i in 1:4, j in 1:4 ]

plot(scatters..., size=(1200,800), margin = 4mm)

max_stars_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook

max_issues_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook

max_forks_repo_name: Yoshinobu-Ishizaki/StatsWithJuliaBook


text:
function plot_lines!(vis::Visualizer, model::Hopper2D, q::AbstractVector;
		r_foot=0.04, offset=0.04, size=10, name::Symbol=:hopper_2D, col::Bool=true)
	p_shift = [0.0, 0.0, r_foot]

	# Point Traj
	top_point = Vector{Point{3,Float64}}()
	bot_point = Vector{Point{3,Float64}}()

	for qi in q
		push!(top_point, Point(qi[1], -offset, qi[2])+p_shift)
		push!(bot_point, Point(kinematics(model, qi)[1], -offset, kinematics(model, qi)[2])+p_shift)
	end

	# Set lines
	orange_mat, blue_mat, black_mat = get_line_material(size)
	if col
		setobject!(vis[name]["/lines/top"], MeshCat.Line(top_point, orange_mat))
		setobject!(vis[name]["/lines/bot"], MeshCat.Line(bot_point, blue_mat))
	else
		setobject!(vis[name]["/lines/top"], MeshCat.Line(top_point, black_mat))
		setobject!(vis[name]["/lines/bot"], MeshCat.Line(bot_point, black_mat))
	end
	return nothing
end

function build_robot!(vis::Visualizer, model::Hopper2D; name::Symbol=:Hopper2D, r=0.04, α=1.0)
	r = convert(Float32, r)
	r_leg = convert(Float32, 0.5 * r)
	body_mat = MeshPhongMaterial(color = RGBA(0.0, 0.0, 0.0, α))
	contact_mat = MeshPhongMaterial(color = RGBA(1.0, 165.0 / 255.0, 0.0, α))

	default_background!(vis)

    setobject!(vis[name][:robot]["body"], Sphere(Point3f0(0),
        convert(Float32, 0.1)), body_mat)
    setobject!(vis[name][:robot]["foot"], Sphere(Point3f0(0), r), contact_mat)
    setobject!(vis[name][:robot]["leg"], Cylinder(Point3f0(0),
		Point3f0(0, 0, 1.0), r_leg), body_mat)
	return nothing
end

function set_robot!(vis::Visualizer, model::Hopper2D, q::AbstractVector;
		name::Symbol=:Hopper2D, r=0.04)
	r = convert(Float32, r)
	r_leg = convert(Float32, 0.5 * r)
	p_shift = [0.0; 0.0; r]

    p_body = [q[1], 0.0, q[2]]
    p_foot = [kinematics(model, q)[1], 0.0, kinematics(model, q)[2]]
	leg_length = norm(p_body - p_foot)

    settransform!(vis[name][:robot]["body"], Translation(p_body + p_shift))
    settransform!(vis[name][:robot]["foot"], Translation(p_foot + p_shift))
	settransform!(vis[name][:robot]["leg"],
		compose(
			cable_transform(p_foot + p_shift, p_body + p_shift),
			LinearMap(Diagonal([1,1,leg_length])))
		)
	return nothing
end

function contact_point(model::Hopper2D, q::AbstractVector)
	p_foot = [kinematics(model, q)[1], 0.0, kinematics(model, q)[2]]
	pc = [p_foot]
	return pc
end

function stairs!(vis)
	setobject!(vis["box1"], GeometryBasics.HyperRectangle(Vec(0.0, 0.0, 0.0),
		Vec(0.25, 0.5, 0.25)), MeshPhongMaterial(color = RGBA(0.5, 0.5, 0.5, 1.0)))
	settransform!(vis["box1"], Translation(0.125, -0.25, 0))

	setobject!(vis["box2"], GeometryBasics.HyperRectangle(Vec(0.0, 0.0, 0.0),
		Vec(0.25, 0.5, 2 * 0.25)), MeshPhongMaterial(color = RGBA(0.5, 0.5, 0.5, 1.0)))
	settransform!(vis["box2"], Translation(0.125 + 0.25, -0.25, 0))

	setobject!(vis["box3"], GeometryBasics.HyperRectangle(Vec(0.0, 0.0, 0.0),
		Vec(0.25, 0.5, 3 * 0.25)), MeshPhongMaterial(color = RGBA(0.5, 0.5, 0.5, 1.0)))
	settransform!(vis["box3"], Translation(0.125 + 2 * 0.25, -0.25, 0))
end

function hopper_parkour_ghost!(vis, sim, traj, ref_traj;
    idx = [1, 35, 50, 110, 130, 190, 210, 265, 270, 284, 295, 300, 305, 320],
    α = [convert(Float64, i / length(idx)) for i = 1:length(idx)],
    _name = "_stairs")

    plot_lines!(vis, sim.s.model, ref_traj.q[1:1:end], size = 5, offset = -0.5)
    stairs!(vis)
    # settransform!(vis["/Cameras/default"],
    #         compose(Translation(0.0, -95.0, -1.0), LinearMap(RotY(0.0 * π) * RotZ(-π / 2.0))))
    # setprop!(vis["/Cameras/default/rotated/<object>"], "zoom", 20)

    for (i, t) in enumerate(idx)
        name = Symbol("Hopper" * "$i" * _name)
        build_robot!(vis, sim.s.model, name=name, α = α[i])
        set_robot!(vis, sim.s.model, traj.q[t], name = name)
    end
end


text:
using AccuracyAtTop
using EvalMetrics
using Flux
using MLDatasets
using Plots
using ProgressMeter

using Flux: gpu, onehotbatch, onecold

# ------------------------------------------------------------------------------------------
# Auxiliary functions
# ------------------------------------------------------------------------------------------
function reshape_data(X::AbstractArray{T, 3}, y::AbstractVector, pos_class) where T
    s = size(X)
    return reshape(X, s[1], s[2], 1, s[3]), onehotbatch(y, 0:9)
end

function eval_scores(model, batches)
    targets = []
    scores = []

    @showprogress for (x,y) in batches
        push!(targets, cpu(y))
        push!(scores, cpu(model(x)))
    end
    return hcat(targets...), hcat(scores...)
end

# ------------------------------------------------------------------------------------------
# Data preparation
# ------------------------------------------------------------------------------------------
using Flux.Data: DataLoader

T = Float32
pos_class = 0
batchsize = 128
device = gpu

X_train, y_train = reshape_data(MLDatasets.FashionMNIST.traindata(T)..., pos_class);
X_test, y_test = reshape_data(MLDatasets.FashionMNIST.testdata(T)..., pos_class);

batches_train = (device(batch) for batch in DataLoader((X_train, y_train); batchsize))
batches_test = (device(batch) for batch in DataLoader((X_test, y_test); batchsize))

# ------------------------------------------------------------------------------------------
# Model preparation
# ------------------------------------------------------------------------------------------
model = Chain(
    # First convolution
    Conv((5, 5), 1=>20, stride=(1,1), relu),
    MaxPool((2,2)),

    # Second convolution
    Conv((5, 5), 20=>50, stride=(1,1), relu),
    MaxPool((2,2)),

    flatten,
    Dense(4*4*50, 500),
    Dense(500, 10)
) |> device

# objective
surrogate = quadratic;
pars = params(model);
aatp = DeepTopPush()

loss(x, y) = objective(aatp, y, model(x); surrogate)

# ------------------------------------------------------------------------------------------
# Model training
# ------------------------------------------------------------------------------------------
opt = ADAM(0.001);
n_epochs = 10

@showprogress for i in 1:n_epochs
    Flux.train!(loss, pars, batches_train, opt)
end

# ------------------------------------------------------------------------------------------
# Evaluation
# ------------------------------------------------------------------------------------------
tar_train, s_train = eval_scores(model, batches_train)
tar_test, s_test = eval_scores(model, batches_test)

kwargs = (xscale = :log10, xlims = (1e-5, 1))

for ind in 1:10
    plt = plot(
        rocplot(tar_train[ind, :], s_train[ind, :]; title = "Train = $(ind)", kwargs...),
        rocplot(tar_test[ind, :], s_test[ind, :]; title = "Test", kwargs...),
        size = (800, 400)
    )
    display(plt)
end


text:
using Griddly

image_path = joinpath(@__DIR__,"..","..","..","resources","images")
shader_path = joinpath(@__DIR__,"..","..","..","resources","shaders")
gdy_path = joinpath(@__DIR__,"..","..","..","resources","games")

gdy_reader = Griddly.GDYReader(image_path,shader_path)
grid = Griddly.load!(gdy_reader,joinpath(gdy_path,"Single-Player/Mini-Grid/minigrid-doggo.yaml"))

println("Grid is loaded, go to create the game now")

game = Griddly.create_game(grid,Griddly.SPRITE_2D)

println("Game is created, we now register a player")

# Create a player
player1 = Griddly.register_player!(game,"Bob", Griddly.BLOCK_2D)

println("Player is created, now we initialize the  game")

Griddly.init!(game)

println("Game is initialized now we want to play on the 5 gvgai levels each for 1000 timesteps")

frames = 0
start = time_ns()

render_window = RenderWindow(700,700)

for l in 0:4
    Griddly.load_level!(grid,l)
    Griddly.reset!(game)
    observation = Griddly.observe(game)
    println(convert(Array{Int,3},Griddly.get_data(observation)))
    for j in 1:200
        dir = rand(0:5)

        reward, done = Griddly.step_player!(player1,"move", [dir])

        # player1_tiles = Griddly.observe(player1)

        observation = Griddly.observe(game)
        observation = Griddly.get_data(observation)
        render(render_window,observation)

        frames += 1

        if (frames % 100 == 0)
            over = time_ns()
            println("fps: $(frames / (over - start))")
            frames = 0
            start = time_ns()
        end
    end
end


text:
# https://juliadocs.github.io/Documenter.jl/stable/man/guide/#Package-Guide
push!(LOAD_PATH,"../src/")

using Documenter, TimeSeriesEcon

makedocs(sitename = "TimeSeriesEcon.jl",
         format = Documenter.HTML(prettyurls = get(ENV, "CI", nothing) == "true"),
         modules = [TimeSeriesEcon],
         pages = [
        "Home" => "index.md",
        "Quickstart" => "quickstart.md",
        "MIT" => "mit.md",
        "TSeries" => "tseries.md",
        "Frequency" => "frequency.md"
    ]
)

deploydocs(
    repo = "github.com/bankofcanada/TimeSeriesEcon.jl.git",
)