text:
# # Tutorial
#
# Here we will see what are the basic building blocks of AlgebraOfGraphics, and how to
# combine them to create complex plots based on tables or other formats.
#
# ## Basic building blocks
#
# The most important functions are `mapping`, and `visual`.
# `mapping` determines the mappings from data to plot. Its positional arguments correspond to
# the `x`, `y` or `z` axes of the plot, whereas the keyword arguments correspond to plot
# attributes that can vary continuously or discretely, such as `color` or `markersize`.
# Variables in `mapping`  are split according to the categorical attributes in it, and then converted
# to plot attributes using a default palette.
# Finally `visual` can be used to give data-independent visual information about the plot
# (plotting function or attributes).
#
# `mapping` and `visual` work in various context. In the following we will explore
# `DataContext`, which is introduced doing `data(df)` for any tabular mapping structure `df`.
# In this context, `mapping` accepts symbols and integers, which correspond to
# columns of the data.
#
# ## Operations
#
# The outputs of `mapping`, `visual`, and `data` can be combined with `+` or `*`,
# to generate an `AlgebraicList` object, which can then be plotted using the
# function `draw`. The actual drawing is done by AbstractPlotting.
#
# The operation `+` is used to create separate layer. `a + b` has as many layers as `la + lb`,
# where `la` and `lb` are the number of layers in `a` and `b` respectively.
#
# The operation `a * b` create `la * lb` layers, where `la` and `lb` are the number of layers
# in `a` and `b` respectively. Each layer of `a * b` contains the combined information of
# the corresponding layer in `a` and the corresponding layer in `b`. In simple cases,
# however, both `a` and `b` will only have one layer, and `a * b` simply combines the
# information.
#
# ## Working with tables

using RDatasets: dataset
using AlgebraOfGraphics, CairoMakie
mpg = dataset("ggplot2", "mpg");
cols = mapping(:Displ, :Hwy);
grp = mapping(color = :Cyl => categorical);
scat = visual(Scatter)
pipeline = cols * scat
data(mpg) * pipeline |> draw
AbstractPlotting.save("scatter.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](scatter.svg)
#
# Now let's simply add `grp` to the pipeline to color according to `:Cyl`.

data(mpg) * grp * pipeline |> draw
AbstractPlotting.save("grouped_scatter.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](grouped_scatter.svg)
# Traces can be added together with `+`.

using AlgebraOfGraphics: linear
pipenew = cols * (scat + linear)
data(mpg) * pipenew |> draw
AbstractPlotting.save("linear.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](linear.svg)
# We can put grouping in the pipeline (we get a warning because of a degenerate group).

data(mpg) * grp * pipenew |> draw
AbstractPlotting.save("grouped_linear.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](grouped_linear.svg)
# This is a more complex example, where we split the scatter plot,
# but do the linear regression with all the data. Moreover, we pass weights to `linear`
# to compute the regression line with weighted least squares.

different_grouping = grp * scat + linear * mapping(wts=:Hwy)
data(mpg) * cols * different_grouping |> draw
AbstractPlotting.save("semi_grouped.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](semi_grouped.svg)
#
# Different analyses are also possible, always with the same syntax:

using AlgebraOfGraphics: smooth, density, frequency, reducer
data(mpg) * cols * grp * (scat + smooth(span = 0.8)) |> draw
AbstractPlotting.save("loess.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](loess.svg)

data(mpg) * cols * density |> draw
AbstractPlotting.save("density.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](density.svg)

data(mpg) * mapping(:Cyl => categorical) * frequency |> draw
AbstractPlotting.save("frequency.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](frequency.svg)

data(mpg) * mapping(:Cty, :Hwy) * reducer(agg = +) |> draw
AbstractPlotting.save("reducer.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](reducer.svg)
#
# We can also add visual information that only makes sense in one recipe (e.g. `markersize`) by
# multiplying them:
#
newmapping = mapping(markersize = :Cyl) * visual(markersize = (0.1, 5))
data(mpg) * cols * (scat * newmapping + smooth(span = 0.8)) |> draw
AbstractPlotting.save("loess_markersize.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](loess_markersize.svg)
#
# ## Layout
#
# Thanks to the MakieLayout package it is possible to create plots where categorical variables
# inform the layout.

iris = dataset("datasets", "iris")
cols = mapping(:SepalLength, :SepalWidth)
grp = mapping(layout_x = :Species)
geom = visual(Scatter) + linear
data(iris) * cols * grp * geom |> draw
AbstractPlotting.save("layout.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](layout.svg)

iris = dataset("datasets", "iris")
cols = mapping(:SepalLength)
grp = mapping(layout_x = :Species)
geom = AlgebraOfGraphics.histogram
data(iris) * cols * grp * geom |> draw
AbstractPlotting.save("hist.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](hist.svg)
#
# ## Non tabular mapping (slicing context)
#
# The framework is not specific to tables, but can be used in different contexts.
# For instance, `dims()` introduces a context where each entry of the array corresponds
# to a trace.

x = [-pi..0, 0..pi]
y = [sin cos] # We use broadcasting semantics on `tuple.(x, y)`.
dims() * mapping(x, y, color = dims(1), linestyle = dims(2)) |> draw
AbstractPlotting.save("functions.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](functions.svg)

using Distributions
distributions = InverseGaussian.(1:4, [6 10])
dims() * mapping(fill(0..5), distributions, color = dims(1), linestyle = dims(2)) |> draw
AbstractPlotting.save("distributions.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](distributions.svg)
#
# More generally, one can pass arguments to `dims` to implement the
# "slices are series" approach.

s = dims(1) * mapping(rand(50, 3), rand(50, 3, 2))
grp = mapping(color = dims(2), layout_x = dims(3))
s * grp * visual(Scatter) |> draw
AbstractPlotting.save("arrays.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](arrays.svg)

# This approach can be used in combination with the tabular context to work
# with "wide" data, where grouping is done by column.

iris = dataset("datasets", "iris")
cols = mapping([:SepalLength, :SepalWidth], [:PetalLength :PetalWidth])
grp = mapping(layout_x = dims(1), layout_y = dims(2), color = :Species)
geom = visual(Scatter) + linear
data(iris) * cols * grp * geom |> draw
AbstractPlotting.save("layout_wide.svg", AbstractPlotting.current_scene()); nothing #hide

# ![](layout_wide.svg)


text:
using Snopt
using DelimitedFiles
using PyPlot

# import model set with wind farm and related details
include("./model_sets/model_set_10_amalia_wind_park.jl")

AEP = ff.calculate_aep(turbine_x, turbine_y, turbine_z, rotor_diameter,
                hub_height, turbine_yaw, ct_models, generator_efficiency, cut_in_speed,
                cut_out_speed, rated_speed, rated_power, windresource, power_models, model_set,
                rotor_sample_points_y=rotor_points_y,rotor_sample_points_z=rotor_points_z)

# add turbine locations to plot
for i = 1:length(turbine_x)
    plt.gcf().gca().add_artist(plt.Circle((turbine_x[i],turbine_y[i]), rotor_diameter[1]/2.0, fill=false,color="C0"))
end

# add boundary
println(maximum(turbine_x), maximum(turbine_y))

println("AEP: ", AEP)

# set up and show plot
axis("square")
plt.show()

ext: jl


text:
# by lazarusA # HIDE
# using GLMakie # HIDE
using CairoMakie, TimeSeries, MarketData, Dates
CairoMakie.activate!() # HIDE
let
    # dummy data
    dates = Date(2018, 1, 1):Day(1):Date(2019, 12, 31)
    ta = TimeArray(dates, rand(length(dates)))
    vals = 0.5*values(ta)
    tempo = string.(timestamp(ta))
    lentime = length(tempo)
    slice_dates = range(1,lentime, step = lentime÷11)

    fig = Figure(resolution = (700, 450), font =:sans)
    ax = Axis(fig, xlabel = "Date", ylabel = "value")
    line1 = lines!(ax,1:lentime, vals, color = :black, linewidth = 0.85)
    ax.xticks = (slice_dates, tempo[slice_dates])
    ax.xticklabelrotation = π/4
    #ax.xticklabelalign = (:center, :center)
    fig[1,1] = ax
    fig
    save(joinpath(@__DIR__, "output", "timeSeries.png"), fig, px_per_unit = 2) # HIDE
end

using Pkg # HIDE
Pkg.status(["CairoMakie","TimeSeries","MarketData","Dates"]) # HIDE

max_stars_repo_name: MisterBiggs/BeautifulMakie

max_issues_repo_name: MisterBiggs/BeautifulMakie

max_forks_repo_name: MisterBiggs/BeautifulMakie


text:
# Get the mean approximation error in random interior points.
function abserror(f::Function, F::Expansion; vals = 200)
    rgrid = randomgrid(support(F),vals)
    @debug "Random evaluation in abserror"
    Fval = F(rgrid)
    fval = sample(rgrid,f,codomaintype(F))
    sum(abs.(Fval-fval))/vals
end

# Get the max approximation error in random interior points
function maxerror(f::Function, F::Expansion; vals = 200, options...)
    rgrid = randomgrid(support(F),vals)
    @debug "Random evaluation in maxerror"
    Fval = F(rgrid; options...)
    fval = sample(rgrid,f,codomaintype(F))
    maximum(abs.(Fval-fval))
end

using QuadGK

function L2error(f::Function, F::Expansion{S,T}; rtol = eps(real(T)), atol = eps(real(T)), options...) where {S,T}
    I = QuadGK.quadgk(x->abs(F(x)-f(x))^2, infimum(support(dictionary(F))), supremum(support(dictionary(F))), rtol=rtol, atol=atol)
    @assert I[2] < 100max(rtol*I[1],atol)
    sqrt(I[1])
end

function residual(f::Function, F::Expansion; residualtype = :l2, options...)
    A, B = discretization(f, dictionary(F); options...)
    R = A*coefficients(F)-B
    if residualtype == :l2
        norm(R)
    elseif residualtype == :relativel2
        norm(R)/norm(B)
    elseif residualtype == :maxnorm
        maximum(abs.(R))
    else
        error("Unknown residualtype: $residualtype")
    end
end


text:
using JuMP, EAGO

                     m = Model()

                     EAGO.register_eago_operators!(m)

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

                     @variable(m, -7.107954588851326 <= q <= 8.439913276188518)

                     add_NL_constraint(m, :((-0.4953615570310643 + -0.5197659840811282*(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + -0.07122185861106178*(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + 0.004105284291951339*(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))))/(1 + exp(-(-0.4953615570310643 + -0.5197659840811282*(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + -0.07122185861106178*(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + 0.004105284291951339*(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))))))) + (0.7344840166166842 + -0.5219551557665656*(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + -0.12237894168399821*(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + 0.5945370485807278*(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))))/(1 + exp(-(0.7344840166166842 + -0.5219551557665656*(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + -0.12237894168399821*(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + 0.5945370485807278*(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))))))) + (-0.7546740103129919 + -0.5743271753296177*(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + 0.959961363428667*(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + -0.0685664132974031*(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))))/(1 + exp(-(-0.7546740103129919 + -0.5743271753296177*(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(0.7941458217392698 + 0.49653685598476827*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.8533482485058403*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + -0.13525586788633204*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + 0.959961363428667*(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.06420985369672083 + 0.9131512287471262*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.03263883937508805*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.0015134736925910275*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))))))) + -0.0685664132974031*(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))/(1 + exp(-(-0.8124442669202567 + 0.4437121866105662*(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3]))/(1 + exp(-(-0.2081348932235647 + -0.9819593709419507*$(x[1]) + 0.4754911924134797*$(x[2]) + -0.6006888917392823*$(x[3])))) + 0.26832550078458484*(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3]))/(1 + exp(-(0.14812040410194793 + -0.8433474401842953*$(x[1]) + -0.1761112323855083*$(x[2]) + 0.6144836466445156*$(x[3])))) + 0.2804882536336004*(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3]))/(1 + exp(-(-0.3937152244783322 + 0.36708048782945024*$(x[1]) + 0.138776295405171*$(x[2]) + -0.8492811105063662*$(x[3])))))))))) - $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:
export intersection_params, line_segment_point, each_intersect, find_juncts, find_connect, connections

"""
```julia
intersection_params(w₁::Line, w₂::Line)
```
Given two lines computes the parameters `[t,s]` so that
the intersection of the lines occurs at
`(w₁.p₂-w₁.p₁)t+w₁.p₁==(w₂.p₂-w₂.p₁)s+w₂.p₁`. If the
lines are parallel, one is a point and not a line, or
the lines are colinear the system of equations will
not be consitent and so will return some solution
like `[NaN,NaN]` or `[Inf,Inf]`. The lines intersect on
the segments given whenever both `0≤t≤1` and `0≤s≤1`.
"""
function intersection_params(w₁::Line, w₂::Line)
    hcat(w₁.p₂-w₁.p₁,w₂.p₁-w₂.p₂)\(w₂.p₁-w₁.p₁)
end

"""
```julia
line_segment_point(t::T, w::Line)
```
Computes the point on the line `w` at parameter `t` where
`t` is the parameter of the line so that `p₂*t+(1-t)p₁`
is the parametric equation of the line.
"""
function line_segment_point(t::T, w::Line) where T<:Real
    w.p₂*t+(1-t)*w.p₁
end

"""
```julia
each_intersect(f!,arr₁,arr₂,dims,arg)
```
    Executes `f!(i₁,i₂,arr₁,arr₂,dims,ps,p,arg)` for any intersection between pairs of
    lines in `arr₁` and `arr₂`. `i₁` and `i₂` are the indices of the lines in `arr₁`
    and `arr₂` respectively, `ps` is the vector of line intersection parameters as
    computed by `intersection_params(arr₁[i₁],arr₂[i₂])`, and `p` is the point at which
    the intersection occurs.
"""
function each_intersect(f!, arr₁::Array{Line{S,N}}, arr₂::Array{Line{S,N}}, dims::SVector{N,S}, arg) where {S,N}
    for i₁ ∈ eachindex(arr₁)
        for i₂ ∈ eachindex(arr₂)
            ps = intersection_params(arr₁[i₁],arr₂[i₂])
            if (0.0 ≤ ps[1] ≤ 1.0) && (0.0 ≤ ps[2] ≤ 1.0)
                p = line_segment_point(ps[1],arr₁[i₁])
                if sum( zero(SVector{N, S}) .≤ p .≤ dims ) == N
                    f!(i₁,i₂,arr₁,arr₂,dims,ps,p,arg)
                end
            end
        end
    end
    arg
end

"""
```julia
each_intersect(f!,arr,dims,arg)
```
    Executes `f!(i₁,i₂,arr,dims,ps,p,arg)` for any intersection between pairs of
    lines in `arr`. `i₁` and `i₂` are the indices of the lines in `arr`, `ps` is
    the vector of line intersection parameters as computed by
    `intersection_params(arr[i₁],arr[i₂])`, and `p` is the point at which the
    intersection occurs.
"""
function each_intersect(f!, arr::Array{Line{S,N}}, dims::SVector{N,S}, arg) where {S,N}
    n = length(arr)
    for i₁ ∈ 2:(n)
        for i₂ ∈ 1:(i₁-1)
            ps = intersection_params(arr[i₁],arr[i₂])
            if (0.0 ≤ ps[1] ≤ 1.0) && (0.0 ≤ ps[2] ≤ 1.0)
                p = line_segment_point(ps[1],arr[i₁])
                if sum( zero(SVector{N, S}) .≤ p .≤ dims ) == N
                    f!(i₁,i₂,arr,dims,ps,p,arg)
                end
            end
        end
    end
    arg
end

"""
```julia
find_juncts(net)
```
Given an `StickNetwork` object finds all points of intersection between lines.
"""
function find_juncts(net::StickNetwork{T,N}) where {T,N}
    each_intersect(net.lines, net.dims, Array{Tuple{SVector{N,T},Tuple{Int,Int}},1}()) do i₁,i₂,arr,dims,ps,p,juncts
        push!(juncts,(p,(i₁,i₂)))
    end
end

"""
```julia
find_connect(net)
```
Given an `StickNetwork` object finds all pairs `(i₁,i₂)` with an intersection between lines `i₁` and `i₂` in `net`.
"""
function find_connect(net::StickNetwork{T,N}) where {T,N}
    each_intersect(net.lines, net.dims, Array{Tuple{Int,Int},1}()) do i₁,i₂,arr,dims,ps,p,connects
        push!(connects,(i₁,i₂))
    end
end

"""
```julia
connections(arr₁,arr₂,dims)
```
Finds all connections between arrays of `Line`s `arr₁` and `arr₂`.
Returns `Tuple{Vector{Int},Vector{Int}}` with each vector representing the source and destination line indices for each connection.
"""
function connections(arr₁::Array{Line{S,N}}, arr₂::Array{Line{S,N}}, dims::SVector{N,S}) where {S,N}
    each_intersect(arr₁, arr₂, dims, (Int[],Int[])) do i₁,i₂,arr₁,arr₂,dims,ps,p,connects
        push!(connects[1],i₁)
        push!(connects[2],i₂)
    end
end

"""
```julia
connections(arr,dims)
```
Finds all connections between `Line`s in `arr`.
Returns `Tuple{Vector{Int},Vector{Int}}` with each vector representing the source and destination line indices for each connection.
"""
function connections(arr::Array{Line{S,N}}, dims::SVector{N,S}) where {S,N}
    each_intersect(arr, dims, (Int[],Int[])) do i₁,i₂,arr,dims,ps,p,connects
        push!(connects[1],i₁)
        push!(connects[2],i₂)
    end
end


text:
module Access

using DataFrames
using Downloads
using JavaCall
using JDBC
using Pkg.Artifacts
using ZipFile

function __init__()
    basepath = joinpath(artifact"ucanaccess", "UCanAccess-5.0.1.bin")

    libs = [
        "commons-lang3-3.8.1.jar",
        "commons-logging-1.2.jar",
        "hsqldb-2.5.0.jar",
        "jackcess-3.0.1.jar",
    ]

    for l in libs
        fn = joinpath(basepath, "lib", l)
        JavaCall.addClassPath(fn)
    end

    fn = joinpath(basepath, "ucanaccess-5.0.1.jar")

    JDBC.usedriver(joinpath(basepath, "ucanaccess-5.0.1.jar"))
    JDBC.init()

    return nothing
end

function filecursor(fn; immediaterelease=false, noinactivitytimeout=false)
    fn = replace(fn, "\\" => "/")
    timeout = ""
    if noinactivitytimeout
        timeout = ";inactivityTimeout=0"
    end

    return cursor("jdbc:ucanaccess://$fn;immediatelyReleaseResources=$(string(immediaterelease))$(timeout)")
end

function query(fn, qstr; imrelease=false, notimeout=true)
    fn = replace(fn, "\\" => "/")
    df = JDBC.load(
        DataFrame,
        filecursor(fn; immediaterelease=imrelease, noinactivitytimeout=notimeout),
        qstr,
    )
    return df
end

# TODO Quote strings
function createInsert(df, tablename)
    function qstring(s)
        return s
    end
    function qstring(s::String)
        return "\"" * s * "\""
    end
    function qkey(s)
        return "[" * string(s) * "]"
    end

    ns = names(df)
    SQLquery = String[]
    for r in eachrow(df)
        keys = join((qkey(n) for n in ns), ", ")
        vals = join((qstring(r[n]) for n in ns), ", ")
        push!(SQLquery, "INSERT INTO $tablename ($keys) VALUES ($vals);")
    end
    return SQLquery
end

function query!(fn, SQLQuery; imrelease=false, notimeout=false)
    csr = filecursor(fn; immediaterelease=imrelease, noinactivitytimeout=notimeout)
    return execute!(csr, SQLQuery)
end

# Insert all rows from a DataFrame into table"
function insertdf(fn, df::DataFrame, tablename::String; imrelease=false, notimeout=true)
    csr = filecursor(fn; immediaterelease=imrelease, noinactivitytimeout=notimeout)
    ns = names(df)
    SQLquery = createInsert(df, tablename)
    for q in SQLquery
        execute!(csr, q)
    end
end

# Insert all rows from a DataFram into table with same name as DataFrame
macro insertdf(fn, df)
    return quote
        tn = $(string(df))
        insertdf($(esc(fn)), $(esc(df)), tn)
    end
end

end # module

max_stars_repo_name: hellemo/Access

max_issues_repo_name: hellemo/Access

max_forks_repo_name: hellemo/Access


text:
@inline function initialize_states!(
    baroclinic::HBModel{C},
    barotropic::SWModel{C},
    model_bc,
    model_bt,
    state_bc,
    state_bt,
) where {C <: Coupled}
    model_bc.state_auxiliary.ΔGᵘ .= -0

    return nothing
end

@inline function tendency_from_slow_to_fast!(
    baroclinic::HBModel{C},
    barotropic::SWModel{C},
    model_bc,
    model_bt,
    state_bc,
    state_bt,
    forcing_tendency,
) where {C <: Coupled}
    FT = eltype(state_bc)
    info = basic_grid_info(model_bc)
    Nqh, Nqk = info.Nqh, info.Nqk
    nelemv, nelemh = info.nvertelem, info.nhorzelem
    nrealelemh = info.nhorzrealelem

    #### integrate the tendency
    model_int = model_bc.modeldata.integral_model
    integral = model_int.balance_law
    update_auxiliary_state!(model_int, integral, forcing_tendency, 0)

    ### properly shape MPIStateArrays
    num_aux_int = number_states(integral, Auxiliary())
    data_int = model_int.state_auxiliary.data
    data_int = reshape(data_int, Nqh, Nqk, num_aux_int, nelemv, nelemh)

    num_aux_bt = number_states(barotropic, Auxiliary())
    data_bt = model_bt.state_auxiliary.data
    data_bt = reshape(data_bt, Nqh, num_aux_bt, nelemh)

    num_aux_bc = number_states(baroclinic, Auxiliary())
    data_bc = model_bc.state_auxiliary.data
    data_bc = reshape(data_bc, Nqh, Nqk, num_aux_bc, nelemv, nelemh)

    ### get vars indices
    index_∫du = varsindex(vars_state(integral, Auxiliary(), FT), :(∫x))
    index_Gᵁ = varsindex(vars_state(barotropic, Auxiliary(), FT), :Gᵁ)
    index_ΔGᵘ = varsindex(vars_state(baroclinic, Auxiliary(), FT), :ΔGᵘ)

    ### get top value (=integral over full depth) of ∫du
    ∫du = @view data_int[:, end, index_∫du, end, 1:nrealelemh]

    ### copy into Gᵁ of barotropic model
    Gᵁ = @view data_bt[:, index_Gᵁ, 1:nrealelemh]
    Gᵁ .= ∫du

    ### get top value (=integral over full depth) of ∫du
    ∫du = @view data_int[:, end:end, index_∫du, end:end, 1:nrealelemh]

    ### save vertically averaged tendency to remove from 3D tendency
    ### need to reshape for the broadcast
    ΔGᵘ = @view data_bc[:, :, index_ΔGᵘ, :, 1:nrealelemh]
    ΔGᵘ .-= ∫du / baroclinic.problem.H

    return nothing
end

@inline function cummulate_fast_solution!(
    baroclinic::HBModel{C},
    barotropic::SWModel{C},
    model_bt,
    state_bt,
    fast_time,
    fast_dt,
    substep,
) where {C <: Coupled}
    return nothing
end

@inline function reconcile_from_fast_to_slow!(
    baroclinic::HBModel{C},
    barotropic::SWModel{C},
    model_bc,
    model_bt,
    state_bc,
    state_bt,
) where {C <: Coupled}
    FT = eltype(state_bc)
    info = basic_grid_info(model_bc)
    Nqh, Nqk = info.Nqh, info.Nqk
    nelemv, nelemh = info.nvertelem, info.nhorzelem
    nrealelemh = info.nhorzrealelem

    ### integrate the horizontal velocity
    model_int = model_bc.modeldata.integral_model
    integral = model_int.balance_law
    update_auxiliary_state!(model_int, integral, state_bc, 0)

    ### properly shape MPIStateArrays
    num_aux_int = number_states(integral, Auxiliary())
    data_int = model_int.state_auxiliary.data
    data_int = reshape(data_int, Nqh, Nqk, num_aux_int, nelemv, nelemh)

    num_aux_bt = number_states(barotropic, Auxiliary())
    data_bt_aux = model_bt.state_auxiliary.data
    data_bt_aux = reshape(data_bt_aux, Nqh, num_aux_bt, nelemh)

    num_state_bt = number_states(barotropic, Prognostic())
    data_bt_state = state_bt.data
    data_bt_state = reshape(data_bt_state, Nqh, num_state_bt, nelemh)

    num_state_bc = number_states(baroclinic, Prognostic())
    data_bc_state = state_bc.data
    data_bc_state =
        reshape(data_bc_state, Nqh, Nqk, num_state_bc, nelemv, nelemh)

    ### get vars indices
    index_∫u = varsindex(vars_state(integral, Auxiliary(), FT), :(∫x))
    index_Δu = varsindex(vars_state(barotropic, Auxiliary(), FT), :Δu)
    index_U = varsindex(vars_state(barotropic, Prognostic(), FT), :U)
    index_u = varsindex(vars_state(baroclinic, Prognostic(), FT), :u)
    index_η_3D = varsindex(vars_state(baroclinic, Prognostic(), FT), :η)
    index_η_2D = varsindex(vars_state(barotropic, Prognostic(), FT), :η)

    ### get top value (=integral over full depth)
    ∫u = @view data_int[:, end, index_∫u, end, 1:nrealelemh]

    ### Δu is a place holder for 1/H * (Ū - ∫u)
    Δu = @view data_bt_aux[:, index_Δu, 1:nrealelemh]
    U = @view data_bt_state[:, index_U, 1:nrealelemh]
    Δu .= 1 / baroclinic.problem.H * (U - ∫u)

    ### copy the 2D contribution down the 3D solution
    ### need to reshape for the broadcast
    data_bt_aux = reshape(data_bt_aux, Nqh, 1, num_aux_bt, 1, nelemh)
    Δu = @view data_bt_aux[:, :, index_Δu, :, 1:nrealelemh]
    u = @view data_bc_state[:, :, index_u, :, 1:nrealelemh]
    u .+= Δu

    ### copy η from barotropic mode to baroclinic mode
    ### need to reshape for the broadcast
    data_bt_state = reshape(data_bt_state, Nqh, 1, num_state_bt, 1, nelemh)
    η_2D = @view data_bt_state[:, :, index_η_2D, :, 1:nrealelemh]
    η_3D = @view data_bc_state[:, :, index_η_3D, :, 1:nrealelemh]
    η_3D .= η_2D

    return nothing
end


text:
module GithubMarkdown

export rendergfm

const libpath = normpath(joinpath(@__DIR__, "..", "deps", "usr", "lib"))
const binary_path = normpath(joinpath(@__DIR__, "..", "deps", "usr", "bin", "cmark-gfm"))

@static if Sys.iswindows()
    const execenv = ("PATH" => string(libpath, ";", Sys.BINDIR))
elseif Sys.isapple()
    const execenv = ("DYLD_LIBRARY_PATH" => libpath)
else
    const execenv = ("LD_LIBRARY_PATH" => libpath)
end

# Load in `deps.jl`, complaining if it does not exist
const depsjl_path = joinpath(@__DIR__, "..", "deps", "deps.jl")
if !isfile(depsjl_path)
    error("GithubMarkdown not installed properly, run Pkg.build(\"GithubMarkdown\"), restart Julia and try again")
end
include(depsjl_path)

# Module initialization function
function __init__()
    # Always check your dependencies from `deps.jl`
    check_deps()
end

const EXTENSIONS = [
    "footnotes",
    "table",
    "strikethrough",
    "autolink",
    "tagfilter",
    "tasklist"
]

const FORMATS = [
    "html",
    "xml",
    "man",
    "commonmark",
    "plaintext",
    "latex"
]

"""
    rendergfm(output::IO, input::IO; documenter = false, format="html", removehtml = false, extensions=EXTENSIONS)
    rendergfm(output::IO, input::AbstractString; documenter = false, format="html", removehtml = false, extensions=EXTENSIONS)
    rendergfm(output::AbstractString, input::AbstractString; documenter = false, format="html", removehtml = false, extensions=EXTENSIONS)

Render the markdown document `input` to `output`, following the cmark-gfm spec.

- `documenter`: Wraps the output in a Documenter `@raw`-block of the specified format.
- `format`: Can be one of `html`, `xml`, `man`, `commonmark`, `plaintext`, `latex`.
- `removehtml`: Removes all literal HTML input and potentially dangerous links if `true`. `false`
  by default. The `tagfilter` extension (enabled by default) will remove most malicious raw HTML.
  It's recommended to sanitize the resulting HTML when `removehtml == false`.
- `extensions`: An array of extensions to use. Valid extensions are `footnotes`, `table`,
  `strikethrough`, `autolink`, `tagfilter`, `tasklist`. All of those are enabled by default.

Spec: https://github.github.com/gfm/
"""
function rendergfm end

"""
    rendergfm(input::AbstractString; documenter = false, format="html", removehtml = false, extensions=EXTENSIONS)::String

Render the `input` string and return the resulting HTML as a string.
"""
function rendergfm(input::AbstractString; kwargs...)
    out = IOBuffer()
    rendergfm(out, IOBuffer(input); kwargs...)
    return String(take!(out))
end

function rendergfm(output::AbstractString, input::AbstractString; kwargs...)
    io = IOBuffer()
    rendergfm(io, input; kwargs...)

    open(output, "w") do input
        write(input, seekstart(io))
    end

    return nothing
end

function rendergfm(output::IO, input::AbstractString; kwargs...)
    isfile(input) || throw(ErrorException("File not found."))

    open(input) do input
        rendergfm(output, input; kwargs...)
    end

    return nothing
end

function rendergfm(output::IO, input::IO; documenter = false, format="html", removehtml = false, extensions=EXTENSIONS)
    if !(format in FORMATS)
        throw(ArgumentError("""
            Invalid format `$(format)`.
            Only $(join(map(s -> string('`', s, '`'), FORMATS), ", ")) are supported.
        """))
    end

    for ext in extensions
        if !(ext in EXTENSIONS)
            throw(ArgumentError("""
                Invalid extension `$(ext)`.
                Only $(join(map(s -> string('`', s, '`'), EXTENSIONS), ", ")) are supported.
            """))
        end
    end

    flags = String["-t", format, "--width", "100"]

    !removehtml && push!(flags, "--unsafe")

    for ext in extensions
        push!(flags, "-e")
        push!(flags, ext)
    end

    withenv(execenv) do
        documenter && println(output, "````````````@raw ", format)
        p = pipeline(input, `$(binary_path) $(flags)`)
        print(output, read(p, String))
        documenter && println(output, "\n````````````")
    end

    return nothing
end

end # module


text:
import Cosmology.cosmology

"""
    cosmology(h::AbstractGadgetHeader)

Defines a `Cosmology.AbstractCosmology` from the properties of `h`.
"""
cosmology(h::AbstractGadgetHeader) = cosmology(h=h.h0, OmegaM=h.omega_0)