text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

                     @variable(m, -1 <= x[i=1:4] <= 1)

                     @variable(m, -41.21023060076703 <= q <= 39.01192102445897)

                     add_NL_constraint(m, :(softplus(-0.296746779395487 + 0.5509866377924739*softplus(-0.36537726371933 + -0.13236748618316563*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.8938439926067105*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.2026514795524368*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.4969096390589627*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + -0.850208463612319*softplus(-0.13805861779585626 + 0.8324190447258686*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.45691763611374503*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.0018061937798208305*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.06838129719944464*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.14361846152386626*softplus(0.892089992500452 + -0.9262441239021939*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.1220711338530176*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.013243228467184842*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.22830737094249542*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.12455164844812394*softplus(-0.09971606472325156 + 0.22050596868305217*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.9537528744420904*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.13795746696872868*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.0643190431003462*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))))) + softplus(-0.7906046812278151 + -0.2168623555923963*softplus(-0.36537726371933 + -0.13236748618316563*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.8938439926067105*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.2026514795524368*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.4969096390589627*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.3937942437507238*softplus(-0.13805861779585626 + 0.8324190447258686*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.45691763611374503*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.0018061937798208305*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.06838129719944464*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.013414887338259796*softplus(0.892089992500452 + -0.9262441239021939*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.1220711338530176*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.013243228467184842*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.22830737094249542*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + -0.20160941359718088*softplus(-0.09971606472325156 + 0.22050596868305217*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.9537528744420904*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.13795746696872868*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.0643190431003462*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))))) + softplus(0.21825887860627402 + 0.23342217391611308*softplus(-0.36537726371933 + -0.13236748618316563*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.8938439926067105*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.2026514795524368*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.4969096390589627*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.7638006108327029*softplus(-0.13805861779585626 + 0.8324190447258686*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.45691763611374503*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.0018061937798208305*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.06838129719944464*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.4051674738413942*softplus(0.892089992500452 + -0.9262441239021939*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.1220711338530176*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.013243228467184842*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.22830737094249542*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.17219310126869347*softplus(-0.09971606472325156 + 0.22050596868305217*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.9537528744420904*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.13795746696872868*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.0643190431003462*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))))) + softplus(0.8210746038366681 + -0.06951431198870361*softplus(-0.36537726371933 + -0.13236748618316563*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.8938439926067105*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.2026514795524368*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.4969096390589627*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.9856384884704164*softplus(-0.13805861779585626 + 0.8324190447258686*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.45691763611374503*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.0018061937798208305*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.06838129719944464*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + -0.2856111885860577*softplus(0.892089992500452 + -0.9262441239021939*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.1220711338530176*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.013243228467184842*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.22830737094249542*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4])))) + 0.04937870878523665*softplus(-0.09971606472325156 + 0.22050596868305217*softplus(0.24726246482452874 + -0.2513871312104543*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.4677853214537917*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + -0.10088601735084568*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.6507022966434817*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.9537528744420904*softplus(0.30866893537934104 + -0.11067617174498023*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.7749548972944802*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.44318801341623093*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + 0.23547099627350976*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + -0.13795746696872868*softplus(-0.39572563026399044 + -0.2902839675147266*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + 0.2975675315855191*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.808554335286296*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.2217074058396178*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))) + 0.0643190431003462*softplus(0.8092602903578396 + -0.8861475410157751*softplus(-0.7954573007956682 + 0.11924049292543248*$(x[1]) + 0.1731334424011517*$(x[2]) + -0.6758597754938092*$(x[3]) + 0.13301420625435734*$(x[4])) + -0.38211269748902854*softplus(-0.7274702539948783 + 0.17282628886946716*$(x[1]) + 0.018302754525115095*$(x[2]) + 0.800298561254523*$(x[3]) + -0.4821070372998202*$(x[4])) + 0.47211467596801615*softplus(-0.4299065646425082 + -0.1572016654178685*$(x[1]) + 0.08953552697871814*$(x[2]) + 0.5995804212147959*$(x[3]) + -0.3420773339437706*$(x[4])) + -0.8428121402295612*softplus(0.440020919910439 + 0.27581141739825465*$(x[1]) + 0.9904718804451909*$(x[2]) + -0.9775405312317109*$(x[3]) + 0.5010243235294203*$(x[4]))))) - $q <= 0.0))

                     @objective(m, Min, q)

                     return m

max_stars_repo_name: PSORLab/RSActivationFunctions

max_issues_repo_name: PSORLab/RSActivationFunctions

max_forks_repo_name: PSORLab/RSActivationFunctions


text:
@safetestset "correct phase diff" begin
    using UltraDark:phase_diff

    n = 4

    @testset "correct size" begin
        a = reshape(Vector{Complex}(1:n^3), (n, n, n))

        @test size(phase_diff(a, [1, 0, 0])) == (n, n, n)
        @test size(phase_diff(a, [0, 1, 0])) == (n, n, n)
        @test size(phase_diff(a, [0, 0, 1])) == (n, n, n)
    end

    @testset "purely real" begin
        a = reshape(Vector{Complex}(1:n^3), (n, n, n))
        @test all(phase_diff(a, [1, 0, 0]) .== 0)
        @test all(phase_diff(a, [0, 1, 0]) .== 0)
        @test all(phase_diff(a, [0, 0, 1]) .== 0)
    end

    @testset "purely imaginary" begin
        a = reshape(Vector{Complex}(1:n^3), (n, n, n)) * im
        @test all(phase_diff(a, [1, 0, 0]) .== 0)
        @test all(phase_diff(a, [0, 1, 0]) .== 0)
        @test all(phase_diff(a, [0, 0, 1]) .== 0)
    end

    @testset "real with 1 row negated" begin
        a = reshape(Vector{Complex}(1:n^3), (n, n, n))
        a[1, :, :] *= -1

        diff = zeros(Real, size(a))
        diff .= 0
        diff[1, :, :] .= -π
        diff[2, :, :] .= +π

        @test all(phase_diff(a, [1, 0, 0]) .≈ diff)
        @test all(phase_diff(a, [0, 1, 0]) .≈ 0)
        @test all(phase_diff(a, [0, 0, 1]) .≈ 0)
    end

    @testset "real with 1 row negated" begin
        a = reshape(Vector{Complex}(1:n^3), (n, n, n))
        a[:, 3, :] *= -1

        diff = zeros(Real, size(a))
        diff .= 0
        diff[:, 3, :] .= -π
        diff[:, 4, :] .= +π

        @test all(phase_diff(a, [1, 0, 0]) .≈ 0)
        @test all(phase_diff(a, [0, 1, 0]) .≈ diff)
        @test all(phase_diff(a, [0, 0, 1]) .≈ 0)
    end

    @testset "real with 1 row imaginary" begin
        a = reshape(Vector{Complex}(1:n^3), (n, n, n))
        a[:, :, 2] *= im

        diff = zeros(Real, size(a))
        diff .= 0
        diff[:, :, 2] .= -π/2
        diff[:, :, 3] .= +π/2

        @test all(phase_diff(a, [1, 0, 0]) .≈ 0)
        @test all(phase_diff(a, [0, 1, 0]) .≈ 0)
        @test all(phase_diff(a, [0, 0, 1]) .≈ diff)
    end

end

@safetestset "terminate if grad too large" begin
    using UltraDark
    using UltraDark: evolve_to!

    n = 16

    # Set up grid with discontinuous phase
    grids = Grids(1.0, n)
    grids.ψx .= 1
    grids.ψx[1, 1, 1] = -1

    grids_orig = deepcopy(grids)

    options = UltraDark.Config.SimulationConfig()
    output_config = OutputConfig(mktempdir(), [1, 2])

    @test_throws "Phase gradient is too large to start" simulate(grids, options, output_config)
    @test grids.ψx == grids_orig.ψx

    @test_throws "Phase gradient is too large to continue" evolve_to!(0, 1, grids, output_config, options)
    @test grids.ψx == grids_orig.ψx

end

ext: jl


text:
for (fn, name) in [(:o, :Open), (:h, :High), (:l, :Low), (:c, :Close), (:v, :Volume)]
    @eval @deprecate $fn(ta::TimeArray) getindex(ta, $name)
end


text:
# A DataFreeTree wraps a descendant of NNTree
# which does not contain a copy of the data
struct DataFreeTree{N <: NNTree}
    size::Tuple{Int,Int}
    hash::UInt64
    tree::N
end

function get_points_dim(data)
    if eltype(data) <: AbstractVector
        ndim = eltype(eltype(data))
        npoints = length(data)
    elseif typeof(data) <: Matrix
        ndim = size(data, 1)
        npoints = size(data, 2)
    else
        error("Unknown input data format")
    end
    return ndim, npoints
end

"""
    DataFreeTree(treetype, data[, reorderbufffer = similar(data), kwargs...]) -> datafreetree

Creates a `DataFreeTree` which wraps a `KDTree` or `BallTree`. Keywords arguments are passed
to their respective constructors.

The `KDTree` or `BallTree` will be stored without a reference to the underlaying data. `injectdata`
has to be used to re-link them to a data array before use.
"""
function DataFreeTree(::Type{T}, data, args...; reorderbuffer = data[:, 1:0], kargs...) where {T <: NNTree}
    tree = T(data, args...; storedata = false, reorderbuffer = reorderbuffer, kargs...)
    ndim, npoints = get_points_dim(data)
    DataFreeTree((ndim, npoints), hash(tree.reordered ? reorderbuffer : data), tree)
end

"""
    injectdata(datafreetree, data) -> tree

Returns the `KDTree`/`BallTree` wrapped by `datafreetree`, set up to use `data` for the points data.
"""
function injectdata(datafreetree::DataFreeTree, data::Matrix{T}) where {T}
    dim = size(data, 1)
    npoints = size(data, 2)
    if isbitstype(T)
        new_data = copy_svec(T, data, Val(dim))
    else
        new_data = SVector{dim,T}[SVector{dim,T}(data[:, i]) for i in 1:npoints]
    end
    new_hash = hash(data)
    injectdata(datafreetree, new_data, new_hash)
end

function injectdata(datafreetree::DataFreeTree, data::Vector{V}, new_hash::UInt64=0) where {V <: AbstractVector}
    if new_hash == 0
        new_hash = hash(data)
    end
    if length(V) != datafreetree.size[1] || length(data) != datafreetree.size[2]
        throw(DimensionMismatch("NearestNeighbors:injectdata: The size of 'data' $(length(data)) × $(length(V)) does not match the data array used to construct the tree $(datafreetree.size)."))
    end
    if new_hash != datafreetree.hash
        throw(ArgumentError("NearestNeighbors:injectdata: The hash of 'data' does not match the hash of the data array used to construct the tree."))
    end

    typ = typeof(datafreetree.tree)
    fields = map(x -> getfield(datafreetree.tree, x), fieldnames(typeof(datafreetree.tree)))[2:end]
    typ(data, fields...)
end


text:
"""
    findCircle(point_1, point_2, point_3)

Given three points, return center points of the circle with [y, x] order instead of
the more common [x, y] ordering.
"""
function find_circle(point_1, point_2, point_3)
    x1, y1 = point_1
    x2, y2 = point_2
    x3, y3 = point_3

    x12 = x1 - x2
    x13 = x1 - x3
    y12 = y1 - y2
    y13 = y1 - y3
    y31 = y3 - y1
    y21 = y2 - y1
    x31 = x3 - x1
    x21 = x2 - x1

    sx13 = x1^2 - x3^2
    sy13 = y1^2 - y3^2
    sx21 = x2^2 - x1^2
    sy21 = y2^2 - y1^2

    f = (
        ((sx13) * (x12) + (sy13) * (x12) + (sx21) * (x13) + (sy21) * (x13)) ÷
        (2 * ((y31) * (x12) - (y21) * (x13)))
    )

    g = (
        ((sx13) * (y12) + (sy13) * (y12) + (sx21) * (y13) + (sy21) * (y13)) ÷
        (2 * ((x31) * (y12) - (x21) * (y13)))
    )

    # eqn of circle be x^2 + y^2 + 2*g*x + 2*f*y + c = 0 where center is (h = -g, k = -f)
    center_insert = [-g, -f]

    return center_insert
end

"""
    get_pixel_size(header)

Get the pixel size of a DICOM image given the DICOM header
"""
function get_pixel_size(header)
    pixel_size = try
        header[(0x0028, 0x0030)]
    catch
        FOV = header[(0x0018, 0x1100)]
        matrix_size = header[(0x0028, 0x0010)]

        pixel_size = FOV / matrix_size
    end
    return pixel_size
end

"""
    angle_calc(side1, side2)

Calculate angle between two sides of rectangular triangle
"""
function angle_calc(side1, side2)
    if side1 == 0
        angle = 0
    elseif side2 == 0
        angle = π / 2
    else
        angle = atan(side1 / side2)
    end

    return angle
end

"""
    create_circular_mask(h, w, center_circle, radius_circle)

Create a circle mask
"""
function create_circular_mask(h, w, center_circle, radius_circle)
    Y, X = collect(1:h), collect(1:w)'
    dist_from_center = sqrt.((X .- center_circle[1]) .^ 2 .+ (Y .- center_circle[2]) .^ 2)
    mask = dist_from_center .<= radius_circle
    return mask
end

"""
    find_triangle_points(p1::Vector, center::Vector; offset=0, offset2=0)

Given the center-point `center` of an equilateral triangle and one single corner-point `p1`,
compute the other corner-points `p2` and `p3`
"""
function find_triangle_points(p1::Vector, center::Vector; offset=0, offset2=0)
	# Shift the points as if the center-point was located at (0, 0)
	p1 = p1 .- center

	# Calculate points 2 and 3
	R = norm(p1)
	θ = atan(p1[2], p1[1])
	p2 = [R*cos(θ + (2*π/3) + offset), R*sin(θ + (2*π/3) + offset)]
 	p3 = [R*cos(θ - (2*π/3) + offset2), R*sin(θ - (2*π/3) + offset2)]

	# Fudge (NEEDS TO BE FIXED)
	p2 = p2[1] + 2, p2[2] - 5

	# Shift the points back to the original center point
	p2, p3 = Int.(round.(p2 .+ center)), Int.(round.(p3 .+ center))
	return p2, p3
end

"""

"""
function mass_calibration(
    dcm_array, center_large_LD, center, cal_rod_slice, rows, cols, spacing
)
    center_LD = center_large_LD
    dist_x = abs(center_LD[1] - center[1])
    dist_y = abs(center_LD[2] - center[2])

    if dist_x == 0
        mass_center_x = center[1]
        if center_LD[2] > center[2]
            mass_center_y = round(center[1] - round(23 / spacing[1], RoundUp), RoundUp)
        else
            mass_center_y = round(center[1] + round(23 / spacing[1], RoundUp), RoundUp)
        end
    elseif dist_y == 0
        mass_center_y = center[2]
        if center_LD[1] > center[1]
            mass_center_x = round(center[1] - round(23 / spacing[1], RoundUp), RoundUp)
        else
            mass_center_x = round(center[0] + round(23 / spacing[1], RoundUp), RoundUp)
        end

    else
        mass_angle = atan(dist_y / dist_x)
        dist_x = (23 / spacing[1]) * cos(mass_angle)
        dist_y = (23 / spacing[1]) * sin(mass_angle)

        if (center_LD[1] < center[1] && center_LD[2] < center[2])
            mass_center_x = round(center[1] + dist_x, RoundUp)
            mass_center_y = round(center[2] + dist_y, RoundUp)
        elseif (center_LD[1] < center[1] && center_LD[2] > center[2])
            mass_center_x = round(center[1] + dist_x, RoundUp)
            mass_center_y = round(center[2] - dist_y, RoundUp)
        elseif (center_LD[1] > center[1] && center_LD[2] < center[2])
            mass_center_x = round(center[1] - dist_x, RoundUp)
            mass_center_y = round(center[2] + dist_y, RoundUp)
        elseif (center_LD[1] > center[1] && center_LD[2] > center[2])
            mass_center_x = round(center[1] - dist_x, RoundUp)
            mass_center_y = round(center[2] - dist_y, RoundUp)
        end
    end

    mass_cal_center = [mass_center_y, mass_center_x]
    x_distance = abs(center[1] - mass_cal_center[2])
    angled_distance = sqrt(
        (center[1] - mass_cal_center[2])^2 + (center[2] - mass_cal_center[2])^2
    )
    angle_0_200HA = acos(x_distance / angled_distance) * 180 / π
    mask_0HU = PhantomSegmentation.create_circular_mask(
        cols, rows, mass_cal_center, Int(round(6.9 / spacing[1]))
    )
    masked_0HU = mask_0HU .* dcm_array[:, :, cal_rod_slice]
    nonzero_count = length(findall(x -> x != 0, masked_0HU))
    mean_0HU = sum(masked_0HU) / nonzero_count
    std_0HU = 0

    for voxel in vec(masked_0HU)
        if voxel != 0
            std_0HU += (voxel - mean_0HU)^2
        end
    end

    std_0HU = sqrt(std_0HU / nonzero_count)
    mask_200HU = PhantomSegmentation.create_circular_mask(
        cols, rows, (center[2], center[1]), Int(round(6.9 / spacing[1]))
    )
    masked_200HU = mask_200HU .* dcm_array[:, :, cal_rod_slice]
    nonzero_count_200HU = length(findall(x -> x != 0, masked_200HU))
    mean_200HU = sum(masked_200HU) / nonzero_count_200HU
    mass_cal_factor = 0.2 / (mean_200HU - mean_0HU)
    water_rod_metrics = mean_0HU, std_0HU

    return mass_cal_factor, angle_0_200HA, water_rod_metrics
end


text:
# Mesh simplification via edge collapse.
#
# The implementation follows for the most part the algorithm
# described in:
#
#   Garland, Michael, and Paul S. Heckbert. "Surface
#   simplification using quadric error metrics."
#   Proceedings of the 24th annual conference on Computer
#   graphics and interactive techniques.
#   ACM Press/Addison-Wesley Publishing Co., 1997.
#
# The principal ways in which the implementation deviates are:
#  1. Only edges, and not vertex pairs in general, are
#     collapsible. We require that simplification preserves
#     topology.
#  2. Collapse is only performed on edges that are incident on
#     interior vertices. Boundaries are not simplified. This
#     allows for a variety of simplifications in the logic of
#     collapse.

###########################################################
#
# CollapsibleMesh
#
# A data structure representing a triangular mesh that
# admits edge collapse.
#
export simplify

type HalfEdge
    source   :: Int # vertex index at the root of the half-edge
    next     :: Int # the next edge in this (oriented) face
    opposite :: Int # the other edge in the adjoining face (if not on bdy)
    HalfEdge(s,n,o) = new(s,n,o)
end

type CollapsibleMesh
    vPositions :: Vector{Vertex}
    vEdges     :: Vector{Int}
    edges      :: Vector{HalfEdge}
end

# vertex accessors
edge(cm,v) = cm.vEdges[v]
position(cm,v) = cm.vPositions[v]

# edge accessors
source(cm,e) = cm.edges[e].source
next(cm,e) = cm.edges[e].next
opposite(cm,e) = cm.edges[e].opposite

# derived accessors
target(cm,e) = source(cm,next(cm,e))
previous(cm,e) = next(cm,next(cm,e))
clockwise(cm,e) = next(cm,opposite(cm,e))

# clockwise edge iterator
macro forEachEdge(cm,e,body)
    quote
        ee = $(esc(e))
        println($(esc(e)))
        while true
            $(esc(body))
            $(esc(e)) = clockwise($(esc(cm)),$(esc(e)))
            if $(esc(e)) == ee
                break
            end
        end
    end
end

# predicates
isDeletedEdge(cm,e) = next(cm,e) == 0
isDeletedVertex(cm,v) = edge(cm,v) == 0
isBoundaryEdge(cm,e) = opposite(cm,e) == 0

function isBoundaryVertex(cm,v)
    e = edge(cm,v)
    @forEachEdge cm e begin
        if isBoundaryEdge(cm,e)
            return true
        end
    end
    false
end

function isCollapsibleEdge(cm,e)

    # assert edge and vertices aren't deleted
    @assert !isDeletedEdge(cm,e)
    src = source(cm,e)
    tgt = target(cm,e)
    @assert !isDeletedVertex(cm,src)
    @assert !isDeletedVertex(cm,tgt)

    # check for boundary
    if isBoundaryEdge(cm,e); return false; end
    if isBoundaryVertex(cm,src); return false; end
    if isBoundaryVertex(cm,tgt); return false; end

    # check for topological conditions
    #
    # We only allow collapse if exactly two vertices are
    # connected by an edge to both the source and target vertex.

    srcNbrs = IntSet()
    se = edge(cm,src)
    @forEachEdge cm se add!(srcNbrs,target(cm,se))

    tgtNbrs = IntSet()
    te = edge(cm,tgt)
    @forEachEdge cm te add!(tgtNbrs,target(cm,te))

    if length(intersect(srcNbrs,tgtNbrs)) != 2; return false; end

    # if there are 4 nbrs, we're a tetrahedron: don't collapse
    if length(union(srcNbrs,tgtNbrs)) <= 4; return false; end

    true
end

# edge collapse
function collapse!(cm::CollapsibleMesh, e::Int, pos::Vertex)

    if !isCollapsibleEdge(cm,e) return false end

    # id the relevant edges, vertices
    #
    #          tgt
    #          +
    #         /|\
    #        / | \
    #    eno/  |  \opo
    #      /en | op\
    #     /    |    \
    # vl +    e|o    + vr
    #     \    |    /
    #      \ep | on/
    #    epo\  |  /ono
    #        \ | /
    #         \|/
    #          +
    #          src
    #
    en = next(cm,e)
    eno = opposite(cm,en)
    ep = previous(cm,e)
    epo = opposite(cm,ep)
    o = opposite(cm,e)
    on = next(cm,o)
    ono = opposite(cm,on)
    op = previous(cm,o)
    opo = opposite(cm,op)
    src = source(cm,e)
    tgt = source(cm,o)
    vl = source(cm,ep)
    vr = source(cm,op)

    # check that the new vertex position doesn't cause an inversion.
    function causesInversion(e)
        v0 = source(cm,e)
        v1 = target(cm,e)
        v2 = target(cm,next(cm,e))
        p0 = position(cm,v0)
        p1 = position(cm,v1)
        p2 = position(cm,v2)
        n1 = unit(cross(p1-p0,p2-p0))
        n2 = unit(cross(p1-pos,p2-pos))
        dot(n1,n2) <= 0
    end

    # circle source vertex
    ee = e
    p0 = position(cm,src)
    @forEachEdge cm ee begin
        if ee != e && ee != on
            if causesInversion(ee)
                return false
            end
        end
    end

    # circle target vertex
    ee = o
    @forEachEdge cm o begin
        if ee != o && ee != en
            if causesInversion(ee)
                return false
            end
        end
    end

    # now perform the actual collapse...

    # fix up the edge opposites for the edges that remain
    cm.edges[epo].opposite = eno;
    cm.edges[eno].opposite = epo;
    cm.edges[opo].opposite = ono;
    cm.edges[ono].opposite = opo;

    # ensure all the edges emanating from the source vertex
    # now emanate from the target vertex
    @forEachEdge cm e begin
        cm.edges[e].source = tgt
    end

    # ensure the three remaining vertices point to edges that remain
    cm.vEdges[tgt] = opo
    cm.vEdges[vl] = eno
    cm.vEdges[vr] = ono

    # update the remaining vertex position
    cm.vPositions[target(cm,e)] = pos;

    # delete the vertex and edges
    cm.vEdges[src] = 0
    cm.edges[ep].next = 0
    cm.edges[en].next = 0
    cm.edges[e].next = 0
    cm.edges[op].next = 0
    cm.edges[on].next = 0
    cm.edges[o].next = 0

    return true
end

###########################################################
#
# conversion between a vanilla mesh and a collapsible mesh
#

# construction from a vanilla mesh
function CollapsibleMesh(m::Mesh)
    vts = m.vertices
    fcs = m.faces

    nVts = length(vts)
    nFcs = length(fcs)

    # make edge, vEdge lists &
    # build edge dictionary
    vs = Array(Int,nVts)
    es = Array(HalfEdge,3*nFcs)
    edgeDict = Dict{(Int,Int),Int}()
    for i = 1:nFcs
        fc = fcs[i]

        # set vertex edges
        vs[fc.v1] = 3*i-2
        vs[fc.v2] = 3*i-1
        vs[fc.v3] = 3*i

        # set sources
        es[3*i-2] = HalfEdge(fc.v1,0,0)
        es[3*i-1] = HalfEdge(fc.v2,0,0)
        es[3*i]   = HalfEdge(fc.v3,0,0)

        # set nexts
        es[3*i-2].next = 3*i-1
        es[3*i-1].next = 3*i
        es[3*i].next   = 3*i-2

        # add edges to dictionary
        edgeDict[(fc.v1,fc.v2)] = 3*i-2
        edgeDict[(fc.v2,fc.v3)] = 3*i-1
        edgeDict[(fc.v3,fc.v1)] = 3*i
    end

    # fix up opposites
    for i = 1:3*nFcs
        src = es[i].source
        tgt = es[es[i].next].source
        es[i].opposite = get(edgeDict,(tgt,src),0)
    end

    CollapsibleMesh(vts,vs,es)
end

# conversion to a vanilla mesh
function Mesh(cm::CollapsibleMesh)

    # collect vertex indices
    nVold = length(cm.vEdges)
    vs = IntSet()
    for v = 1:nVold
        if !isDeletedVertex(cm,v)
            add!(vs,v)
        end
    end

    # get vertex positions, create reverse map
    nV = length(vs)
    vts = Array(Vertex,nV)
    invV = zeros(Int,nVold)
    ix = 1
    for v = vs
        vts[ix] = position(cm,v)
        invV[v] = ix
        ix += 1
    end

    # collect faces
    fcs = Array(Face{Int},0)
    nE = length(cm.edges)
    for e = 1:3:nE
        if !isDeletedEdge(cm,e)
            # make and push face
            v1 = invV[source(cm,e)]
            v2 = invV[source(cm,e+1)]
            v3 = invV[source(cm,e+2)]
            push!(fcs,Face{Int}(v1,v2,v3))
        end
    end

    Mesh(vts,fcs)
end

##########################################################
#
# mesh simplification based on vertex quadric error
#

typealias Plane Vector4{Float64}

# Given three vertices, determines coefficients of the
# corresponding plane equation
function plane(v1::Vertex,v2::Vertex,v3::Vertex)
    n = unit(cross(v1-v2,v3-v2))
    d = dot(n,v2)
    p = Plane(n.e1,n.e2,n.e3,-d)
end

# determines an error quadric associated with the given vertex
typealias Quadric Matrix4x4{Float64}
function quadric(cm::CollapsibleMesh, v::Int)
    q = zero(Quadric)
    if !isBoundaryVertex(cm,v)
        e = edge(cm,v)
        @forEachEdge cm e begin
            # determine the plane associated with the current face
            v1 = position(cm,source(cm,e))
            v2 = position(cm,source(cm,next(cm,e)))
            v3 = position(cm,source(cm,previous(cm,e)))
            p = plane(v1,v2,v3)

            # accumulate the outer product
            q += column(p)*row(p)
        end
    end
    q
end

function edgeCost(cm,qs,e)
    p1 = position(cm,source(cm,e))
    p2 = position(cm,target(cm,e))
    p  = 0.5*(p1+p2)
    hp = Plane(p.e1,p.e2,p.e3,1.0)
    c = hp*(qs[source(cm,e)]+qs[target(cm,e)])*hp
    (c,p)
end

# helper type for edge heap
immutable HeapNode
    cost     :: Float64
    edge     :: Int
    position :: Vertex
    HeapNode(c,e,p) = new(c,e,p)
end
<(hn1::HeapNode,hn2::HeapNode) = hn1.cost < hn2.cost
using DataStructures

function simplify(msh::Mesh,eps::Float64)
    # to a collapsible mesh
    cm = CollapsibleMesh(msh)

    # calculate the initial vertex quadrics
    nV = length(cm.vPositions)
    qs = Array(Quadric,nV)
    for v = 1:nV
        qs[v] = quadric(cm,v)
    end

    # initialize the edge heap
    h = mutable_binary_minheap(HeapNode)
    nE = length(cm.edges)
    ks = zeros(Int,nE) # keys into the heap

    # a convenience closure : recalculate the cost
    # of the edge and update the heap
    function updateCost(e)
        if isCollapsibleEdge(cm,e)
            (c,p) = edgeCost(cm,qs,e)
            if c <= eps
                hn = HeapNode(c,e,p)
                if ks[e] != 0
                    update!(h, ks[e], hn)
                else
                    ks[e] = push!(h, hn)
                end
            end
        end
    end

    # add all the appropriate edges to the heap
    for e = 1:nE
        updateCost(e)
    end

    # simplify
    while top(h).cost <= eps
        hn = pop!(h)
        e = hn.edge
        p = hn.position

        src = source(cm,e)
        tgt = target(cm,e)

        if collapse!(cm,e,p)
            # update the quadric
            qs[tgt] += qs[src]

            # update neighbor edges in the heap
            e = edge(cm,v)
            @forEachEdge cm e begin
                updateCost(e)
                updateCost(next(cm,e))
                updateCost(previous(cm,e))
                if !isBoundaryEdge(cm,next(cm,e))
                    updateCost(opposite(cm,next(cm,e)))
                end
            end
        end
    end

    # from a collapsible mesh
    Mesh(cm)
end


text:
using ArgParse2
using DataFrames
using DataStructures
using BioSequences
using CodecZlib
using BioAlignments
using DataFramesMeta
using Statistics
using st16SseqJuliaTools
using XAM

function main()
    parser = ArgumentParser(prog = "analyse_alnself.jl",
                        description = "Detect chimeras",
                        epilog = "Removing potential chimeras and repair snps")
    add_argument!(parser, "--inbam", "-b", help = "input bam ",type = String)
    add_argument!(parser, "--ref", "-r", help = "reference fasta",type = String)
    add_argument!(parser, "--ideal", "-i", help = "ideal contigs - for debugging",type = String,default="")
    add_argument!(parser, "--salmon-rez", "-s", help = "Salmon rezults",type = String)
    add_argument!(parser, "--out", "-o", help = "Fasta file to write out cleaned up sequences",default="chimerasfree.faSTA",type = String)
    #add_argument!(parser, "--output", "-o", help = "File with names",type = String)
    args = parse_args(parser)
    salmon_data = read_salmon(args.salmon_rez)
    ref = fasta_to_dict(args.ref)

    #Limits for real match ... mayve should be options
    minl = 100
    minid = 0.96
    pretrim_length = 250
    minid_for_close = 0.96 # Identity of first 250 fragments of contigs
    max_unaligned_length_to_consider_conservative_start_extention = 5

    costmodel = BioAlignments.CostModel(match=0, mismatch=1, insertion=1, deletion=1);
    reader = open(BAM.Reader,args.inbam) #,index = args.inbam*".bai")
    #known_chimeras = ["1_1868;size=92", "1_6093;size=54", "1_2188;size=22","1_1760;size=8"]
    args.ideal == "" ? known_good_contigs = [] : known_good_contigs = collect(keys(fasta_to_dict(args.ideal)))

    #intr = ["7_4;size=170", "7_169;size=1344"]
    pairs = Array{Tuple{String,String},1}()
    for record in reader
        if BAM.ismapped(record) #&& SAM.isprimary(record)
            rn = BAM.refname(record)
	        tempname = BAM.tempname(record)
            pos = BAM.position(record)
            seq = BAM.sequence(record)
            rpos = BAM.rightposition(record)
            aln = BAM.alignment(record)
            s = first(aln.anchors).refpos
            e = last(aln.anchors).refpos
            l = abs(s-e)
            qual = BAM.mappingquality(record)
            genus_t = split(tempname,"_")[1]
            genus_r = split(rn,"_")[1]
            #if (tempname != rn) && (genus_t != genus_r) && (genus_t == "1") && (tempname == "1_1160;size=1896") # (tempname == "1_89;size=8") #&& (tempname in known_chimeras) #&& (tempname=="1_1868;size=92")
            t_pos=BioAlignments.ref2seq(aln,pos)[1]
            t_rpos=BioAlignments.ref2seq(aln,rpos)[1]
            rtseq = ref[rn][pos:rpos]
            tseq = ref[tempname][pretrim_length+1:end][t_pos[1]:t_rpos[1]]

            #check a match of alignment in bam
            pairalnres = BioAlignments.pairalign(BioAlignments.EditDistance(),rtseq,tseq,costmodel)
            pairaln = BioAlignments.alignment(pairalnres)
            matches = BioAlignments.count_matches(pairaln)
            idfrac = matches/l

            #check if  close sequences
            t_pref = ref[tempname][1:250]
            r_pref = ref[rn][1:250]
            pairaln_pref = BioAlignments.pairalign(BioAlignments.EditDistance(),t_pref,r_pref,costmodel)
            pairaln_pref = BioAlignments.alignment(pairaln_pref)
            matches_pref = BioAlignments.count_matches(pairaln_pref)
            idfrac_pref = matches_pref/250

            is_cons5region_extention = (idfrac_pref >= minid_for_close && abs(t_pos+pretrim_length-pos)<= max_unaligned_length_to_consider_conservative_start_extention)
            if idfrac >= minid &&
                l >= minl &&
                !is_cons5region_extention
                if  (tempname in ["6_12;size=1"]) #&& (rn in["1_809;size=1 "])
                    println("UUUUUUU ",is_cons5region_extention)
                    println(idfrac_pref,"   ",idfrac," AAAAAAA")
                    println("********************************************")
                    println("PAIR: \n>r\n$rtseq\n>t\n$tseq\n---------------")
                    println(">tf")
                    println(ref[tempname][pretrim_length+1:end])
                    println(">rf")
                    println(ref[rn])
                    println(">tf_250")
                    println(ref[tempname][1:250])
                    println(">rf_250")
                    println(ref[rn][1:250])
                    println(">tf_full_$tempname")
                    println(ref[tempname])
                    println(">rf_full_$rn")
                    println(ref[rn])
                    println(tempname," ",rn," ",t_pos," ",t_rpos," ",pos," ",rpos)
                    println("AAAAAAAAAAAAAAAAAAAaaa",idfrac)
                    println(pairaln)
                end
                push!(pairs,(tempname,rn))
            end
        end
    end
    close(reader)
    #testset = unique(testset)
    #group matches based on template
    tn_rn_map = Dict{String,Array{String,1}}()

    for pair in pairs
        tn,rn =  pair
        if !(tn in keys(tn_rn_map))
            tn_rn_map[tn] = Array{String,1}()
        end
        push!(tn_rn_map[tn],rn)
        s_t = salmon_data[split(tn,";")[1]]
        s_r = salmon_data[split(rn,";")[1]]
        tn_good = (tn in known_good_contigs)
        #println(" $tn $rn $s_t $s_r $tn_good  ")
    end
    #detect if chimeric or not
    chimeras = Array{String,1}()
    intr = Array{String,1}()
    #println("--------------------------------------------------------------")
    for tn in keys(tn_rn_map)
        tn_s = salmon_data[split(tn,";")[1]]
        rn_ss = Array{Float64,1}()
        for rn in tn_rn_map[tn]
            rn_s = salmon_data[split(rn,";")[1]]
            push!(rn_ss, rn_s)
        end
        pot_chim = DataFrame(ref_names = tn_rn_map[tn],ref_salmon = rn_ss)
        sort!(pot_chim,[:ref_salmon],rev=true)
        max_r_s = pot_chim[1,:].ref_salmon
        max_r_id = pot_chim[1,:].ref_names
        chimeric = false
        if tn_s < max_r_s
            chimeric = true
        end
        good_t = (tn in known_good_contigs )
        good_r = (max_r_id in known_good_contigs )
        ct = 0
        println("
        Template:
            ID: $tn
            TPM: $tn_s
            good: $good_t
        Most abundant chimeric partner:
            ID: $max_r_id
            TPM: $max_r_s
            good: $good_r
        ")
         if  chimeric
            ct += 1
            push!(chimeras,tn)
            if chimeric && good_t
                println("##############MISCLASSIFICATION##############")
                println("
                Template:
                    ID: $tn
                    TPM: $tn_s
                    good: $good_t
                Most abundant chimeric partner:
                    ID: $max_r_id
                    TPM: $max_r_s
                    good: $good_r
                ")
                ts = ref[tn]
                rs = ref[max_r_id]
                pairalnres = BioAlignments.pairalign(BioAlignments.EditDistance(),ts,rs,costmodel)
                pairaln = BioAlignments.alignment(pairalnres)
                println(pairaln)
                println("Exiting...")
                exit()
            end
        end

    end
    println("##############RECHECK##############3")
    #println(intr)
    chimeras = unique(chimeras)
    filtered_out = OrderedDict{String,LongSequence{BioSequences.DNAAlphabet{4}}}()
    ct = 0
    ct_good = 0
    for k in keys(ref)
        if ! (k in  chimeras)
            ct_good += 1
            filtered_out[k]= ref[k]
        else
            ct += 1
        end
    end
    dict_to_fasta(filtered_out,args.out)
    println("$ct chimeras were detected, $ct_good sequences were  written to ",args.out)
end

main()


text:
using ProximalOperators
using ProximalAlgorithms

### this test includes two tests

## 1-testing combinations with two terms: Lasso

A = [  1.0  -2.0   3.0  -4.0  5.0;
       2.0  -1.0   0.0  -1.0  3.0;
      -1.0   0.0   4.0  -3.0  2.0;
      -1.0  -1.0  -1.0   1.0  3.0]
b = [1.0, 2.0, 3.0, 4.0]

m, n = size(A)

f = Translate(SqrNormL2(), -b)
f2 = LeastSquares(A, b)
lam = 0.1*norm(A'*b, Inf)
g = NormL1(lam)

x_star = [-3.877278911564627e-01, 0, 0, 2.174149659863943e-02, 6.168435374149660e-01]

stuff = [
  Dict( "theta"      => 2,
        "mu"         => 0,
        "it"         => (70,135,100),
      ),
  Dict( "theta"      => 1,
        "mu"         => 1,
        "it"         => (80,130,15),
      ),
  Dict( "theta"      => 0,
        "mu"         => 1,
        "it"         => (90,345,120),
      ),
  Dict( "theta"      => 0,
        "mu"         => 0,
        "it"         => (80,170,180),
      ),
  Dict( "theta"      => 1,
        "mu"         => 0,
        "it"         => (90,130,230),
      )
  ]

Random.seed!(0)

for i = 1:length(stuff)

theta = stuff[i]["theta"]
mu    = stuff[i]["mu"]
itnum = stuff[i]["it"]

x0 = randn(n)

# h\equiv 0 (FBS)
y0 = randn(n)
@time it, x, y, sol = ProximalAlgorithms.AFBA(x0, y0; g=g, f=f2, betaQ =opnorm(A'*A), theta=theta, mu=mu)
println("      nnz(x)    = $(norm(sol.x, 2))")
@test norm(x - x_star, Inf) <= 1e-4
@test it <=itnum[1]
println(sol)

@time it, (x, y) = ProximalAlgorithms.run!(sol)

# f=\equiv 0 (Chambolle-Pock)
y0 = randn(m)
@time it, x, y, sol = ProximalAlgorithms.AFBA(x0, y0; g=g, h=f, L=A, theta=theta, mu=mu)
@test norm(x - x_star, Inf) <= 1e-4
@test it <=itnum[2]
println(sol)

# g\equiv 0
y0 = randn(n) # since L= Identity
@time it, x, y, sol = ProximalAlgorithms.AFBA(x0, y0; h=g, f=f2, betaQ=opnorm(A'*A), theta=theta, mu=mu)
@test norm(x - x_star, Inf) <= 1e-4
@test it <=itnum[3]
println(sol)

end

## 2- testing with three terms: 1/2\|Ax-b\|^2+ λ\|x\|_1 + + λ_2*\|x\|^2

lam2= 1;

x0 = randn(n)
y0 = randn(n)
itnum= ((130,130),(40,40),(90,90),(150,150),(170,170)); # the number of iterations

for i = 1:length(stuff)
theta = stuff[i]["theta"]
mu    = stuff[i]["mu"]
@time it, x, y, sol = ProximalAlgorithms.AFBA(x0,y0; g=g,f=f2, h = SqrNormL2(lam2), betaQ =opnorm(A'*A), theta=theta, mu=mu)
println("      nnz(x)    = $(norm(sol.x, 2))")

 # the optimality conditions
temp = lam*sign.(x) + A'*(A*x-b) + lam2*x
ind= findall(x -> abs.(x)<1e-8,x)
t1= length(findall((temp[ind] .<= lam .* temp[ind] .>=-lam)==false))
t2= length(findall((abs.(deleteat!(temp,ind)) .<1e-8)==false))
@test t1+t2 == 0
@test it <=itnum[i][1]
println(sol)

@time it, x, y, sol = ProximalAlgorithms.AFBA(x0,y0; h=g,f=f2, g = SqrNormL2(lam2), betaQ =opnorm(A'*A), theta=theta, mu=mu)
println("      nnz(x)    = $(norm(sol.x, 2))")
 # the optimality conditions
temp = lam*sign.(x) + A'*(A*x-b) + lam2*x
ind= findall(x -> abs.(x)<1e-8,x)
t1= length(findall((temp[ind] .<= lam .* temp[ind] .>=-lam)==false))
t2= length(findall((abs.(deleteat!(temp,ind)) .<1e-8)==false))
@test t1+t2 == 0
@test it <=itnum[i][2]
println(sol)

end

# TODO: add test including function l


text:
module ComputationalHeatTransfer

using DocStringExtensions
using Reexport

@reexport using CartesianGrids
@reexport using ImmersedLayers
@reexport using RigidBodyTools
@reexport using ConstrainedSystems
@reexport using GridUtilities

using LinearAlgebra
#using SparseArrays

export HeatConduction,setstepsizes,timestep,newstate,
       ExternalProblem, InternalProblem, ExternalInternalProblem,
       temperature,temperature_linesource,
       HeatConductionParameters,LineSourceParams,
       PrescribedHeatFluxRegion,PrescribedHeatModelRegion,
       set_linesource_strength!

#=
export NavierStokes, PulseParams, PointForce, SpatialDGaussian,
       setstepsizes, timestep, timerange, newstate,
       update_immersion_operators!,
       vorticity, velocity, velocity!, streamfunction, streamfunction!,
       scalarpotential, scalarpotential!, convective_derivative, convective_derivative!,
       pressure, traction, force, moment, pressurejump
=#

abstract type PointMotionType end
abstract type StaticPoints <: PointMotionType end
abstract type MovingPoints <: PointMotionType end

abstract type ProblemSide end
abstract type ExternalProblem <: ProblemSide end
abstract type InternalProblem <: ProblemSide end
abstract type ExternalInternalProblem <: ProblemSide end

abstract type AbstractBC end
abstract type NeumannBC <: AbstractBC end
abstract type DirichletBC <: AbstractBC end
abstract type AdiabaticBC <: NeumannBC end
abstract type HomogeneousDirichletBC <: DirichletBC end

const NDIM = 2

include("utils/forcing.jl")
include("utils/ohp.jl")
include("heat_conduction.jl")
#include("plot_recipes.jl")

end


text:
module PointProcessDistance

using Distances: Euclidean, pairwise
using LinearAlgebra: diagind

include("summary_characteristics.jl")

export distance, distances, K, D, T, H

end


text:
using MacroTools
macro prop(e)
    if e.head != :(=) || length(e.args) != 2 || !(e.args[1] isa AbstractString)
        error("expected AbstractString = value")
    end
    keys = Symbol[Symbol(e.args[1])]
    esc(:(NamedTuple{Tuple($(keys))}(Tuple(Any[$(e.args[2])]))...))
end


text:
push!(LOAD_PATH, abspath(@__DIR__,".."))

using LinearAlgebra,Statistics,StatsBase,Distributions
using Plots,NamedColors ; theme(:dark) #; plotlyjs();
using SparseArrays
using SpikingRNNs; const global S = SpikingRNNs
using BenchmarkTools,Test
using ProgressMeter

using Random; Random.seed!(0)
##
const Ntot = 1000
const Ttot = 5.0
const mydensity = 0.3
const dt = 0.1E-3
const rates = rand(Uniform(20.,50.),Ntot)

# dummy neurons

nt_all = let sker = S.SKExp(Inf)
  spkgen = S.SGPoissonMulti(rates)
  S.NTInputConductance(spkgen,sker,-Inf)
end
ps_all = S.PSInputConductance(nt_all,Ntot)

## initial weights
weights_start = let ρstart = mydensity
  ret = S.sparse_constant_wmat(Ntot,Ntot,ρstart,1.0;no_autapses=true)
  ret
end

# test 3 :  weight-dependent structural plasticity,
# half synapses are killed in several realizations
# show weight distribution before and after.

weight_gen_distr = Uniform(5.0,30.0)
weights_start = let ρstart = mydensity
  ret = S.sparse_constant_wmat(Ntot,Ntot,ρstart,1.0;no_autapses=true)
  retnz = nonzeros(ret)
  rand!(weight_gen_distr,retnz)
  ret
end

## plasticity rule
plast_struct = let  νdeath = log(10)/(Ttot),
  temperature = 100.0,
  Δt = 30E-3,
  ρ = 0.0,
  syngen = S.SynapticGenerationConstant(2.0)
  srt_type = S.StructuralPlasticityWeightDependent(;
    connection_density=ρ,
    death_rate = νdeath, w_temperature = temperature )
  S.PlasticityStructural(srt_type,syngen,Δt)
end

##

conn_all=S.ConnectionPlasticityTest(copy(weights_start),plast_struct)
pop_all=S.Population(ps_all,(conn_all,ps_all))
myntw = S.RecurrentNetwork(dt,pop_all)

## Run
wstart = copy(weights_start)

function _one_run()
  times = (0:myntw.dt:Ttot)
  # reset weights
  copy!(conn_all.weights,weights_start)
  S.reset!(ps_all)
  S.reset!(conn_all)
  for t in times
    S.dynamics_step!(t,myntw)
  end
  wend = conn_all.weights
  return copy(nonzeros(wend))
end

wend_all = []
nsampl = 3
@showprogress for k in 1:nsampl
  push!(wend_all, _one_run())
end

wend_all = vcat(wend_all...)

histogram(nonzeros(wstart))
_ = let plt=plot()
  bins = range(0,35;length=80)
  h1 = normalize(fit(Histogram,nonzeros(wstart),bins))
  h2 = normalize(fit(Histogram,wend_all,bins))
  plot(h1;opacity = 0.5,label="w start")
  plot!(h2;opacity = 0.5,label="w end")
end


text:
export init, train

"abstract Hyperparameters"
abstract type AbstractHyperparam end;

"Abstract Model Data Object"
abstract type AbstractModelData end;

"Abstract Model Buffer Object"
abstract type AbstractModelBuffer end;

abstract type ModelType end;

abstract type InitialisationType end;

abstract type PosteriorInference end;

function init(X, model::ModelType, init::InitialisationType)
  throw(ErrorException("Initialisation $(typeof(init)) for $(typeof(model)) using $(typeof(X)) is not available."))
end

function extractpointestimate(B::AbstractModelBuffer)
  throw(ErrorException("No point estimate available for $(typeof(B))."))
end

function sampleparameters!(B::AbstractModelBuffer, P::AbstractHyperparam)
  throw(ErrorException("Parameter sampling for $(typeof(B)) using $(typeof(P)) is not available."))
end

function gibbs!(B::AbstractModelBuffer)
  throw(ErrorException("Gibbs sampling for $(typeof(B)) is not available."))
end

function slicesampling!(B::AbstractModelBuffer)
  throw(ErrorException("Slice sampling for $(typeof(B)) is not available."))
end

function variationalbayes!(B::AbstractModelBuffer)
  throw(ErrorException("Variational inference for $(typeof(B)) is not available."))
end

function train(B::AbstractModelBuffer, P::AbstractHyperparam, I::PosteriorInference)
  throw(ErrorException("Posterior inference for $(typeof(B)) with $(typeof(P)) and $(typeof(I)) is not available."))
end


text:
using Test, YaoBase, YaoArrayRegister, YaoBlocks

@testset "constructor" begin
    g = Roller(X, kron(X, Y), Z, Z)
    occupied_locs(g) == 1:5

    src = phase(0.1)
    g = rollrepeat(4, src)
    g.blocks[1].theta = 0.2
    @test src.theta == 0.1
    @test g.blocks[2].theta == 0.1
    blks = [X, Y, kron(3, 2=>Rx(0.3))]
    @test_throws LocationConflictError chsubblocks(g, blks)
    blks = [X, Y, kron(2, 2=>Rx(0.3))]
    @test collect(subblocks(chsubblocks(g, blks))) == blks
    @test rollrepeat(4, X) == roll(4, i=>X for i in 1:4)
end

@testset "copy" begin
    g = rollrepeat(4, phase(0.1))
    cg = copy(g)

    cg.blocks[1].theta = 1.0
    @test g.blocks[1].theta == 1.0
end

@testset "setindex" begin
    g = rollrepeat(5, phase(0.1))
    @test_throws MethodError g.blocks[1] = X
end

@testset "iteration" begin
    g = rollrepeat(5, phase(0.1))
    for each in g.blocks
        @test each.theta == 0.1
    end

    g = Roller(X, kron(X, Y), Z, Z)
    list = [X, kron(X, Y), Z, Z]
    for (src, tg) in zip(g.blocks, list)
        @test src == tg
    end

    for i in eachindex(g.blocks)
        @test g.blocks[i] == list[i]
    end
end

@testset "tile one block" begin
    g = rollrepeat(5, X)
    @test state(apply!(ArrayReg(bit"11111"), g)) == state(ArrayReg(bit"00000"))
    @test state(apply!(ArrayReg{3}(bit"11111"), g)) == state(ArrayReg{3}(bit"00000"))
end

@testset "roll multiple blocks" begin
    g = Roller((X, Y, Z, X, X))
    tg = kron(5, X, Y, Z, X, X)
    @test state(apply!(ArrayReg(bit"11111"), g)) == state(apply!(ArrayReg(bit"11111"), tg))
    @test state(apply!(ArrayReg{3}(bit"11111"), g)) == state(apply!(ArrayReg{3}(bit"11111"), tg))
end

@testset "matrix" begin
    g = roll(X, kron(Y, Z), X, X)
    tg = kron(X, Y, Z, X, X)
    @test mat(g) == mat(tg)
    @test linop2dense(r->apply!(ArrayReg(r), g) |> statevec, 5) == mat(tg)

    m = kron(Const.CNOT, Const.I2, Const.X, Const.I2)
    r = roll(5, I2, X, I2, CNOT)
    @test m == mat(r)
end


text:
using Comrade, ComradeDynesty, Distributions
using Test

load_ehtim()
include(joinpath(@__DIR__, "../../../test/test_util.jl"))

@testset "ComradeDynesty.jl" begin
    m, vis, amp, lcamp, cphase = load_data()
    prior = test_prior()
    lklhd = RadioLikelihood(lcamp, cphase)
    post = Posterior(lklhd, prior, test_model)
    a1 = NestedSampler(dimension(post))
    a2 = DynamicNestedSampler(dimension(post))

    chain, stats = sample(post, a1; dlogz=0.1, print_progress=false)
    chain, stats = sample(post, a2; print_progress=false)

    cpost = ascube(post)
    xopt = chain[end]
    @test isapprox(xopt.f1/xopt.f2, 2.0, atol=1e-2)
    @test isapprox(xopt.σ1*2*sqrt(2*log(2)), μas2rad(40.0), rtol=1e-2)
    @test isapprox(xopt.σ1*xopt.τ1*2*sqrt(2*log(2)), μas2rad(20.0), rtol=1e-2)
    @test isapprox(-xopt.ξ1, π/3, atol=1e-2)
    @test isapprox(xopt.σ2*2*sqrt(2*log(2)), μas2rad(20.0), atol=1e-2)
    @test isapprox(xopt.σ2*xopt.τ2*2*sqrt(2*log(2)), μas2rad(10.0), rtol=1e-2)
    @test isapprox(-xopt.ξ2, π/6, atol=1e-2)
    @test isapprox(xopt.x, μas2rad(30.0), rtol=1e-2)
    @test isapprox(xopt.y, μas2rad(30.0), rtol=1e-2)

end


text:
#addprocs(2)
#@show nprocs()

using RoME
using Base.Test

using JLD, HDF5

println("[TEST] numeric root...")
include("testhigherdimroots.jl")
println("[SUCCESS]")

println("[TEST] Camera function evaluations...")
include("testCameraFunctions.jl")
println("[SUCCESS]")

println("[TEST] MultipleFeatures constraints")
include("testmultiplefeatures.jl")
println("[SUCCESS]")

println("[TEST] Linear array functions evaluations...")
include("testDidsonFunctions.jl")
println("[SUCCESS]")

println("[TEST] Pose2 evaluations...")
include("TestPoseAndPoint2Constraints.jl")
println("[SUCCESS]")

println("[TEST] Pose3 evaluations...")
include("threeDimLinearProductTest.jl")
println("[SUCCESS]")

println("[TEST] ensure Pose3Pose3NH evaluations...")
include("testPose3Pose3NH.jl")
println("[SUCCESS]")

println("[TEST] saving to and loading from .jld file")
savejld(fg, file="tempfg.jld" )
fgu = loadjld( file="tempfg.jld" )
Base.rm("tempfg.jld")
println("Success")

println("[TEST] partial pose3 evaluations...")
include("testpartialpose3.jl")
println("[SUCCESS]")

println("[TEST] PartialPose3XYYaw evaluations...")
include("testPartialXYH.jl")
println("[SUCCESS]")

println("[TEST] packing converters...")
include("testpackingconverters.jl")
println("[SUCCESS]")

include("TestDefaultFGInitialization.jl")


text:
#=
This file is auto-generated. Do not edit.
=#

#! format: off

"""
    mutable struct Transfer <: Service
        name::String
        available::Bool
        requirement::Float64
        ext::Dict{String, Any}
        time_series_container::InfrastructureSystems.TimeSeriesContainer
        internal::InfrastructureSystemsInternal
    end

# Arguments
- `name::String`
- `available::Bool`
- `requirement::Float64`
- `ext::Dict{String, Any}`
- `time_series_container::InfrastructureSystems.TimeSeriesContainer`: internal time_series storage
- `internal::InfrastructureSystemsInternal`: power system internal reference, do not modify
"""
mutable struct Transfer <: Service
    name::String
    available::Bool
    requirement::Float64
    ext::Dict{String, Any}
    "internal time_series storage"
    time_series_container::InfrastructureSystems.TimeSeriesContainer
    "power system internal reference, do not modify"
    internal::InfrastructureSystemsInternal
end

function Transfer(name, available, requirement, ext=Dict{String, Any}(), time_series_container=InfrastructureSystems.TimeSeriesContainer(), )
    Transfer(name, available, requirement, ext, time_series_container, InfrastructureSystemsInternal(), )
end

function Transfer(; name, available, requirement, ext=Dict{String, Any}(), time_series_container=InfrastructureSystems.TimeSeriesContainer(), internal=InfrastructureSystemsInternal(), )
    Transfer(name, available, requirement, ext, time_series_container, internal, )
end

# Constructor for demo purposes; non-functional.
function Transfer(::Nothing)
    Transfer(;
        name="init",
        available=false,
        requirement=0.0,
        ext=Dict{String, Any}(),
        time_series_container=InfrastructureSystems.TimeSeriesContainer(),
    )
end

"""Get [`Transfer`](@ref) `name`."""
get_name(value::Transfer) = value.name
"""Get [`Transfer`](@ref) `available`."""
get_available(value::Transfer) = value.available
"""Get [`Transfer`](@ref) `requirement`."""
get_requirement(value::Transfer) = value.requirement
"""Get [`Transfer`](@ref) `ext`."""
get_ext(value::Transfer) = value.ext
"""Get [`Transfer`](@ref) `time_series_container`."""
get_time_series_container(value::Transfer) = value.time_series_container
"""Get [`Transfer`](@ref) `internal`."""
get_internal(value::Transfer) = value.internal

"""Set [`Transfer`](@ref) `available`."""
set_available!(value::Transfer, val) = value.available = val
"""Set [`Transfer`](@ref) `requirement`."""
set_requirement!(value::Transfer, val) = value.requirement = val
"""Set [`Transfer`](@ref) `ext`."""
set_ext!(value::Transfer, val) = value.ext = val
"""Set [`Transfer`](@ref) `time_series_container`."""
set_time_series_container!(value::Transfer, val) = value.time_series_container = val


text:
struct PipelineShaderStageCreateInfo
    mHandleRef::Ref{vk.VkPipelineShaderStageCreateInfo}
    mPreserve::Vector{Any}

    function PipelineShaderStageCreateInfo(;
        pNext::Ptr{Cvoid} = C_NULL,
        flags::vk.VkPipelineShaderStageCreateFlags = vk.VkFlags(0),
        stage::vk.VkShaderStageFlagBits = vk.VK_SHADER_STAGE_ALL,
        _module::vk.VkShaderModule, #required
        name::String = "",
        specializationInfo = nothing #::vk.VkSpecializationInfo
    )

        this = new(Ref(vk.VkPipelineShaderStageCreateInfo(
            vk.VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, #sType::VkStructureType
            pNext, #pNext::Ptr{Cvoid}
            flags, #flags::VkPipelineShaderStageCreateFlags
            stage, #stage::VkShaderStageFlagBits
            _module, #::vk.VkShaderModule
            Base.unsafe_convert(Cstring, name), #pName::Cstring
            ref_to_pointer(vk.VkSpecializationInfo, specializationInfo), #pSpecializationInfo::Ptr{VkSpecializationInfo}
        )), [specializationInfo])
    end
end

@class PipelineShaderStageCreateInfo

function handleRef(this::PipelineShaderStageCreateInfo)::Ref{vk.VkPipelineShaderStageCreateInfo}
    this.mHandleRef
end

function defaults(::Type{PipelineShaderStageCreateInfo};
    _module::ShaderModule, #required
    name::String = "main",
    stage::vk.VkShaderStageFlagBits = vk.VK_SHADER_STAGE_ALL
    )::PipelineShaderStageCreateInfo

    if (stage == vk.VK_SHADER_STAGE_ALL)
        moduleStage = getStage(_module)
        @assert (moduleStage != vk.VK_SHADER_STAGE_ALL) ("Couldn't find out the type of stage you want to add. Either " *
                                                                "load from a file with a hint for the stage in the name, or " *
                                                                "explicitly provide the stage either to the ShaderModule or " *
                                                                "when you add the stage.")
        stage = moduleStage
    end

    info = PipelineShaderStageCreateInfo(
        _module = handle(_module),
        name = name,
        stage = stage
    )

    return info
end

function addStage(outStages::Vector{vk.VkPipelineShaderStageCreateInfo},
                    _module::ShaderModule, #required
                       name::String = "main",
                      stage::vk.VkShaderStageFlagBits = vk.VK_SHADER_STAGE_ALL)
    stage = defaults(PipelineShaderStageCreateInfo, _module = _module, name = name, stage = stage)
    push!(outStages, stage.handleRef()[])
end

function createComputeStage(_module::ShaderModule, #required
                               name::String = "main")
    info = PipelineShaderStageCreateInfo(
              _module = handle(_module), name = name,
              stage = vk.VK_SHADER_STAGE_COMPUTE_BIT)
    return info
end


text:
# Entities
include("entities/FileDFG.jl")

# Services
include("services/FileDFG.jl")