Sharedjulia-1.2.ipynbOpen in CoCalc
Author: Harald Schilly
Views : 164

# Julia 1.2

https://docs.julialang.org/en/v1.2/NEWS/#

In [1]:
VERSION

v"1.2.0"
In [2]:
for i = 1:10
println(i)
end

1 2 3 4 5 6 7 8 9 10
In [3]:
using DifferentialEquations

In [4]:
f(u,p,t) = 1.01*u
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8)
using Plots
plot(sol,linewidth=5,title="Solution to the linear ODE with a thick line",
xaxis="Time (t)",yaxis="u(t) (in μm)",label="My Thick Line!") # legend=false
plot!(sol.t, t->0.5*exp(1.01t),lw=3,ls=:dash,label="True Solution!")

In [1]:
using Compat

In [1]:
using PyPlot
x = range(0, stop = 4*pi, length=1000)
y = sin.(3*x + 1.5*cos.(2*x))
plot(x, y, color="red", linewidth=2.0, linestyle="--")

1-element Array{PyCall.PyObject,1}: PyObject <matplotlib.lines.Line2D object at 0x7f369a09d940>
In [2]:
using SymPy
x = symbols("x")      # or   @vars x, Sym("x"), or  Sym(:x)
y = sin(10 * pi*x)
y(1), y(2.2), y(123456)

(-1.22464679914735e-15, 4.41120439947682e-15, 6.39055017337619e-11)
In [3]:
using Test

foo(x) = length(x)^2

α = 1

@testset "foo" begin
@test α == 2-1
@test π ≈ 3.14 atol=0.01
@test isapprox(9.81, 10, atol=0.2)
@test foo("foo") == 9
end

Test Summary: | Pass Total foo | 4 4 
Test.DefaultTestSet("foo", Any[], 4, false)

## HomotopyContinuation

In [5]:
using HomotopyContinuation, LinearAlgebra, DynamicPolynomials

c² = 2

@polyvar z[1:3, 1:6]
z_vec = vec(z)[1:17] # the 17 variables in a vector
Z = [zeros(3) z[:,1:5] [z[1,6]; z[2,6]; 0] [√c²; 0; 0]] # the eight points in a matrix

# define the functions for cyclooctane:
F1 = [(Z[:, i] - Z[:, i+1]) ⋅ (Z[:, i] - Z[:, i+1]) - c² for i in 1:7]
F2 = [(Z[:, i] - Z[:, i+2]) ⋅ (Z[:, i] - Z[:, i+2]) - 8c²/3 for i in 1:6]
F3 = (Z[:, 7] - Z[:, 1]) ⋅ (Z[:, 7] - Z[:, 1]) - 8c²/3
F4 = (Z[:, 8] - Z[:, 2]) ⋅ (Z[:, 8] - Z[:, 2]) - 8c²/3
f = [F1; F2; F3; F4]


15-element Array{Polynomial{true,Float64},1}: z₁₋₁² + z₂₋₁² + z₃₋₁² - 2.0 z₁₋₁² - 2.0z₁₋₁z₁₋₂ + z₂₋₁² - 2.0z₂₋₁z₂₋₂ + z₃₋₁² - 2.0z₃₋₁z₃₋₂ + z₁₋₂² + z₂₋₂² + z₃₋₂² - 2.0 z₁₋₂² - 2.0z₁₋₂z₁₋₃ + z₂₋₂² - 2.0z₂₋₂z₂₋₃ + z₃₋₂² - 2.0z₃₋₂z₃₋₃ + z₁₋₃² + z₂₋₃² + z₃₋₃² - 2.0 z₁₋₃² - 2.0z₁₋₃z₁₋₄ + z₂₋₃² - 2.0z₂₋₃z₂₋₄ + z₃₋₃² - 2.0z₃₋₃z₃₋₄ + z₁₋₄² + z₂₋₄² + z₃₋₄² - 2.0 z₁₋₄² - 2.0z₁₋₄z₁₋₅ + z₂₋₄² - 2.0z₂₋₄z₂₋₅ + z₃₋₄² - 2.0z₃₋₄z₃₋₅ + z₁₋₅² + z₂₋₅² + z₃₋₅² - 2.0 z₁₋₅² - 2.0z₁₋₅z₁₋₆ + z₂₋₅² - 2.0z₂₋₅z₂₋₆ + z₃₋₅² + z₁₋₆² + z₂₋₆² - 2.0 z₁₋₆² + z₂₋₆² - 2.8284271247461903z₁₋₆ + 4.440892098500626e-16 z₁₋₂² + z₂₋₂² + z₃₋₂² - 5.333333333333333 z₁₋₁² - 2.0z₁₋₁z₁₋₃ + z₂₋₁² - 2.0z₂₋₁z₂₋₃ + z₃₋₁² - 2.0z₃₋₁z₃₋₃ + z₁₋₃² + z₂₋₃² + z₃₋₃² - 5.333333333333333 z₁₋₂² - 2.0z₁₋₂z₁₋₄ + z₂₋₂² - 2.0z₂₋₂z₂₋₄ + z₃₋₂² - 2.0z₃₋₂z₃₋₄ + z₁₋₄² + z₂₋₄² + z₃₋₄² - 5.333333333333333 z₁₋₃² - 2.0z₁₋₃z₁₋₅ + z₂₋₃² - 2.0z₂₋₃z₂₋₅ + z₃₋₃² - 2.0z₃₋₃z₃₋₅ + z₁₋₅² + z₂₋₅² + z₃₋₅² - 5.333333333333333 z₁₋₄² - 2.0z₁₋₄z₁₋₆ + z₂₋₄² - 2.0z₂₋₄z₂₋₆ + z₃₋₄² + z₁₋₆² + z₂₋₆² - 5.333333333333333 z₁₋₅² + z₂₋₅² + z₃₋₅² - 2.8284271247461903z₁₋₅ - 3.3333333333333326 z₁₋₆² + z₂₋₆² - 5.333333333333333 z₁₋₁² + z₂₋₁² + z₃₋₁² - 2.8284271247461903z₁₋₁ - 3.3333333333333326
In [2]:
n = 2 # dimension of the cyclooctane variety
N = 17 # ambient dimension
@polyvar Aᵥ[1:n, 1:N] bᵥ[1:n] # variables for the linear equations
p = [vec(Aᵥ); bᵥ] # parameters
F = [f; Aᵥ * z_vec - bᵥ] # the polynomial system we have to solve

# now we solve one particular instance for A,b complex. we use this as start system
A₀ = randn(ComplexF64, n, N)
b₀ = randn(ComplexF64, n)
p₀ = [vec(A₀); b₀]

F₀ = [subs(Fᵢ, p => [vec(A₀); b₀]) for Fᵢ in F]
complex_result = solve(F₀)
S_p₀ = solutions(complex_result)

Tracking 32768 paths... 100%|███████████████████████████| Time: 0:02:13 # paths tracked: 32768 # non-singular solutions (real): 1408 (0) # singular solutions (real): 0 (0) # total solutions (real): 1408 (0) 
WARNING: Some output was deleted.
In [3]:
tracker = pathtracker(F; parameters=p, generic_parameters=p₀)

# we compute 100 random intersections
data = [randn(n*N+n) for _ in 1:100]
Ω = map(data) do pp
# We want to store all solutions. Create an empty array.
S_p = similar(S_p₀, 0)
for s in S_p₀
result = track(tracker, s; target_parameters=pp, details=:minimal)
# check that the tracking was successfull and that we have a real solution
if is_success(result) && is_real(result)
# only store the solutions
push!(S_p, solution(result))
end
end
# return an array of type Array{Float64}
# (and not Array{ComplexF64})
real.(S_p)
end
Ω = vcat(Ω...)

764-element Array{Array{Float64,1},1}: [-0.47140452079103135, 1.2326051093188215, 0.508391996651122, -0.30597093373475887, 1.257533326145154, 1.9126748429038112, 0.923698071307032, 0.6477483267747983, 2.2533746999369892, 1.9420501401856454, 1.6283775302912171, 2.289781141968623, 2.7280497612661097, 1.535488757197196, 1.117784439117265, 1.885618083164127, 1.3333333333333333] [-0.47140452079103135, 1.2553172837447908, 0.4493954771794869, 0.6280310456542709, 2.0792476157256514, 0.7846271034873377, 0.9540968294357605, 1.8982495565631883, 2.148782815723439, 2.3110346096566974, 1.516422411034731, 2.2624837020289847, 2.898483826557636, 1.4752312155407807, 0.9767128425924155, 1.8856180831641267, 1.3333333333333333] [-0.4714045207910314, 1.235737267858546, 0.5007306487556693, 0.6279303653341242, 2.043778076028783, 0.8729306762674646, 1.0517107574337063, 1.6922512848645872, 2.175558441350694, 2.46451065023281, 1.6422380760169137, 2.214225505469927, 2.965454550958918, 1.451553488858221, 0.905525923233641, 1.8856180831641267, 1.3333333333333333] [-0.4714045207910314, 1.2893928400734962, 0.3394758927302223, -0.9580480073689265, 1.2789248356128744, 1.667281803943876, 0.05826154702017344, 0.8222604067892707, 2.5382424741837215, 1.2753149117920157, 1.4699261296810506, 2.223107235455003, 1.1661445041040515, 2.0877056566523016, 0.9556569294704171, 1.885618083164127, 1.3333333333333333] [-0.4714045207910314, 1.0879500949178367, -0.7708063107850432, 0.05475223538577994, 2.294714777854062, -0.25420388343173794, -0.08519534087169063, 2.305755236807526, 1.1530248769927196, 1.1504464487250654, 2.6528381753746366, 1.7469295844650088, 2.1471496767841898, 1.7408679516616976, 1.3287274966683371, 1.8856180831641267, 1.333333333333333] [-0.47140452079103135, 1.1992117737273316, -0.5828112040202429, -1.4856463440611387, 1.7527340401606137, 0.23261912684333952, -1.096703595670501, 1.670457260423292, 1.589805212091193, 0.3125178531689105, 1.5847583214098786, 1.6719672470156344, 0.8887883080858698, 2.185765880156581, 0.5288564574947192, 1.8856180831641267, 1.3333333333333335] [-0.47140452079103135, 1.3158954689307885, -0.21493462407252797, -0.9552370983716876, 1.8480821152284495, 1.0027202574000946, -0.011424232423696947, 1.6109684277087135, 2.0288753983324166, 1.1009625892381796, 2.466358048770144, 1.8530796214395364, 2.1849358373071377, 1.7275085264913077, 1.3247772639782347, 1.885618083164127, 1.3333333333333333] [-0.4714045207910314, 1.281487529249784, -0.3681949078342356, -0.5587393775900367, 2.0999953522726544, 0.7817692509282268, 0.4742489340260212, 3.065445658388693, 0.7527717599151275, 1.428027443727541, 2.7655100861041246, 1.7529443484766596, 2.25270644298772, 1.7035479990703786, 1.3146053542243128, 1.8856180831641267, 1.3333333333333333] [-0.47140452079103135, 1.1971240355475838, -0.5870875754877164, -1.482692955309895, 1.7557721082651363, 0.22851528930454842, -1.097166257187905, 1.6649624964804146, 1.5861318697287357, 0.31219716834773537, 1.5850487143488565, 1.6716217649671508, 0.8886309060341802, 2.185821530185643, 0.5284699072472879, 1.8856180831641267, 1.333333333333333] [-0.47140452079103135, 1.025227362072851, -0.8524591684268058, -0.1824745793030924, 2.2852208804638416, -0.2789298993180118, -0.5187576489202065, 2.266233347731565, 1.094588637393318, 0.4730007813603489, 1.5589117336696765, 1.8129975392370627, 1.7493986205351981, 1.881494186210584, 1.2965199332729025, 1.8856180831641267, 1.3333333333333333] [-0.47140452079103135, 1.0004267593877023, -0.8814329678874046, 0.29724730549241596, 0.9832194663433499, -2.068394752873428, 1.578186555990557, 0.45100016927718595, -1.79282759887343, 1.6424301822983485, -0.884927524290561, -2.2523599198668327, 2.322479954963902, -1.6788793373375992, -1.29990371338968, 1.8856180831641267, -1.333333333333333] [-0.47140452079103135, -0.16461415903870946, -1.3231326299437092, -0.9243486315072974, -1.492551499859988, -1.5004009334273356, 0.08921458980148116, -2.3937754089490504, -1.0997950646530488, 1.1886689923544254, -2.2896817533814753, -1.9831775002818803, 2.2800087643301645, -1.69389517078869, -1.3093705174549355, 1.8856180831641267, -1.3333333333333335] [-0.4714045207910314, 0.18205038706760066, -1.3208464840193637, -0.7799085776106996, -1.075809032605098, -1.888838497402007, 0.12075564749359519, -2.048845321472395, -1.396899137150524, 1.3231863192886764, -1.996411628083961, -2.1394697986529003, 2.393153915385643, -1.6538923190038368, -1.2804879209571292, 1.8856180831641267, -1.3333333333333335] ⋮ [-0.47140452079103135, 0.722395304218996, 1.120679616224067, -1.3133719936625183, -0.10776633386630607, 1.8965162158234479, -0.6740434397242991, -1.3489944110954242, 2.1214870148560596, 0.7250581831029151, -1.1890321680397409, 1.991384248556304, 1.2102401853819156, -2.072115479025991, 0.9990323500232781, 1.8856180831641267, -1.3333333333333333] [-0.47140452079103135, 0.6555863309096317, 1.1610272781042765, -1.3597911052366942, -0.19738222556314525, 1.8561631772245248, -0.7157391126262013, -1.4261728124780602, 2.1305182834714325, 0.6808495253614929, -1.272703792933071, 1.9693120916437665, 1.1274446144340196, -2.101388133860726, 0.913928232795433, 1.885618083164127, -1.333333333333333] [-0.47140452079103135, 1.074634020738493, 0.789265164092017, -1.484984962659152, 1.7665433299893878, 0.08647344840779318, -0.9181937875230429, 2.4298460517438967, -1.0265302636708717, 0.35888546717209596, 2.927742720003681, -0.6784390615942959, 1.3531228216284434, 2.0215988385241266, -1.1147822167609718, 1.8856180831641267, 1.333333333333333] [-0.47140452079103135, 1.2518237011942697, 0.45903725219861796, -0.8150703160024418, 2.0611367994458374, -0.6486206959966524, -0.24104340375341832, 3.34399779541793, -0.49126514627774265, 1.1100517971809019, 3.3101876937278996, -0.9076767635940185, 1.4820463840095637, 1.9760174759169171, -1.1933511538994312, 1.8856180831641267, 1.3333333333333335] [-0.4714045207910314, 1.2824146374077618, -0.3649527031549381, 0.053523015493046985, 2.248384955500908, 0.5246272124309266, 0.26640211838511696, 3.462036280805871, -0.16944263141576765, 1.3447803988419158, 3.3065643692500606, -1.0710682571364543, 1.6125484024517576, 1.9298780448174135, -1.2528237532374702, 1.8856180831641267, 1.3333333333333333] [-0.47140452079103135, 1.1016716396018973, 0.7510641625551344, -1.1937210588822673, 1.9746652389654331, -0.09518697881496145, -0.2882750341491255, 2.74339470591559, -0.8627952792765178, 0.4468356728561511, 1.8904245330213985, -1.718396899540911, 1.7999981922639015, 1.8636045360633335, -1.3082360971313676, 1.885618083164127, 1.3333333333333333] [-0.4714045207910314, 0.4576696397131336, 1.2523243504230956, -0.6278163112933529, -0.6420102091170805, 2.1276754696139126, -0.7557887606313175, -1.8309899310506639, 1.372724959288115, -0.4111292906393385, -1.5733991267370306, 0.025558738186240992, 0.8133474436465103, -2.2124382535684046, -0.2782269125138174, 1.8856180831641267, -1.3333333333333333] [-0.47140452079103135, 0.3369287940648754, 1.290060760393776, -1.4214078350579298, -0.6226953842485068, 1.71031680053868, -0.7613519514821313, -1.833414184517118, 2.0241419385265518, -0.02222341243120166, -2.2721877703091966, 0.9011251340344733, 1.353116178681864, -2.0216011871604134, 1.11477759356341, 1.885618083164127, -1.3333333333333333] [-0.47140452079103135, 0.9743696468650012, 0.910154695118336, -0.045837340028939265, 2.256179369657305, 0.4908023263241195, -0.4484400225248901, 2.4720791865667273, -0.8475916175898323, 0.39776841949004216, 1.745874692368524, -1.7173952191396264, 1.7399659375631087, 1.8848291432577582, -1.2940779822397193, 1.8856180831641267, 1.3333333333333335] [-0.4714045207910314, 0.4794578745800835, 1.2441454594543706, 0.0267920843554683, -0.339117854766657, 2.2842098410889666, -0.5201110302497955, -1.6378437061990452, 2.1650119130185557, 0.22631383815274328, -2.3787142349450425, 1.2195169233618794, 1.5720186760677268, -1.9442074670003051, 1.236320429615807, 1.885618083164127, -1.3333333333333333] [-0.47140452079103135, 1.0650714017707832, -0.8021226133876271, -0.7376749493117858, 2.1883671937491256, 0.014765764923095616, 0.33158064912144763, 3.1084630038792422, -0.08581338500467052, 0.9591743854591016, 2.966031223048765, -1.3451144967008382, 1.8307371195406092, 1.8527366841014539, -1.314232947023701, 1.8856180831641267, 1.3333333333333333] [-0.47140452079103135, 1.0336530663583263, 0.8422227236223249, -0.5010988447708551, 0.5752505410033516, 2.1797523015650193, -0.15824008382455215, -0.7963008699897176, 2.215732810806653, 1.2491306984276427, -0.9314071831670855, 2.1832710578893275, 1.6051980765520872, -1.9324767774612075, 1.2499567005501802, 1.885618083164127, -1.3333333333333333]
In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [6]:
using JuMP
using Ipopt
m = Model(with_optimizer(Ipopt.Optimizer, print_level=0))
@variable(m, 0 <= x <= 2 )
@variable(m, 0 <= y <= 30 )
@objective(m, Min, x*x+ 2x*y + y*y )
@constraint(m, x + y >= 1 )
optimize!(m)
println(termination_status(m))
println("| x = ", JuMP.value(x), "| y = ", JuMP.value(y))