How can I use universal and existential quantification in julia? - julia

I want to code domination definition in Julia. x dom y. x , y are 2 vectors.
b=all(x<=y) && any(x<y)
would you please help me. How can I code this concept in Julia?
Thank you

The simplest approach can be almost like you have specified it:
dom(x, y) = all(x .<= y) && any(x .< y)
You could also use a loop e.g. like this:
function dom(x::AbstractVector, y::AbstractVector)
#assert length(x) == length(y)
wasless = false
for (xi, yi) in zip(x, y)
if xi < yi
wasless = true
elseif xi > yi
return false
end
end
return wasless
end

Related

Optim Julia parameter meaning

I'm trying to use Optim in Julia to solve a two variable minimization problem, similar to the following
x = [1.0, 2.0, 3.0]
y = 1.0 .+ 2.0 .* x .+ [-0.3, 0.3, -0.1]
function sqerror(betas, X, Y)
err = 0.0
for i in 1:length(X)
pred_i = betas[1] + betas[2] * X[i]
err += (Y[i] - pred_i)^2
end
return err
end
res = optimize(b -> sqerror(b, x, y), [0.0,0.0])
res.minimizer
I do not quite understand what [0.0,0.0] means. By looking at the document http://julianlsolvers.github.io/Optim.jl/v0.9.3/user/minimization/. My understanding is that it is the initial condition. However, if I change that to [0.0,0., 0.0], the algorithm still work despite the fact that I only have two unknowns, and the algorithm gives me three instead of two minimizer. I was wondering if anyone knows what[0.0,0.0] really stands for.
It is initial value. optimize by itself cannot know how many values your sqerror function takes. You specify it by passing this initial value.
For example if you add dimensionality check to sqerror you will get a proper error:
julia> function sqerror(betas::AbstractVector, X::AbstractVector, Y::AbstractVector)
#assert length(betas) == 2
err = 0.0
for i in eachindex(X, Y)
pred_i = betas[1] + betas[2] * X[i]
err += (Y[i] - pred_i)^2
end
return err
end
sqerror (generic function with 2 methods)
julia> optimize(b -> sqerror(b, x, y), [0.0,0.0,0.0])
ERROR: AssertionError: length(betas) == 2
Note that I also changed the loop condition to eachindex(X, Y) to ensure that your function checks if X and Y vectors have aligned indices.
Finally if you want performance and reduce compilation cost (so e.g. assuming you do this optimization many times) it would be better to define your optimized function like this:
objective_factory(x, y) = b -> sqerror(b, x, y)
optimize(objective_factory(x, y), [0.0,0.0])

Symbolics: Replacing symbolic equation with variables to solve

In Symbolics.jl, I can formulate a set of equations purely symbolically.
I can for example define this differential equation using #syms:
using Symbolics
#syms α ρ[1:2, 1:2] dαdt dρdt[1:2, 1:2]
eqs = []
push!(eqs, dαdt == α*(ρ[1,1] +ρ[1,2] + ρ[2,1] + ρ[2,2]))
for i in 1:2, j in 1:2
push!(eqs, dρdt[i,j] == α*ρ[j,i])
end
eqs then has the form:
5-element Vector{Any}:
dαdt == (α*(ρ[1, 1] + ρ[1, 2] + ρ[2, 1] + ρ[2, 2]))
dρdt[1, 1] == (α*ρ[1, 1])
dρdt[1, 2] == (α*ρ[2, 1])
dρdt[2, 1] == (α*ρ[1, 2])
dρdt[2, 2] == (α*ρ[2, 2])
To solve the above equation using e.g. ModelingToolkit.jl, the symbolic parameters must be replaced by variables, e.g.
#variables t::Real, αvar(t)::Complex{Real}, ρvar(t)[1:2, 1:2]::Complex{Real}
Question: How can I transform the above symbolic equation to the correct variables, so that it can be solved using e.g. an ODEProblem?
Remark: A trivial solution is of course to just use the variables as defined above in the first place. This is however not the point of this question.
Something like this may work:
using Symbolics
#syms τ α ρ[1:2, 1:2]
D = Differential(τ)
eqs = []
push!(eqs, D(α) ~ α*(ρ[1,1] +ρ[1,2] + ρ[2,1] + ρ[2,2]))
for i in 1:2, j in 1:2
push!(eqs, D(ρ[i,j]) ~ α*ρ[j,i])
end
and then
using ModelingToolkit
#parameters t
#variables αvar(t), ρvar(t)[1:2, 1:2]
subs = Dict(τ => t, α => αvar, ρ => ρvar)
eqs_subbed = substitute.(eqs, Ref(subs))

JuMPDict change of dimension

I am using Julia 0.6.2 and JuMP 0.18.5 (I can't use a more recent version since I need to use an old package).
Creating JuMP variables with conditions on the index lead to a JuMPDict instead of an Array.
For example:
m = Model(solver = CplexSolver())
# type of x: JuMP.JuMPDict{JuMP.Variable,2}
#variable(m, x[i in 1:3, j in 1:3; i < j] >= 0)
# type of y: JuMP.JuMPDict{JuMP.Variable,3}
#variable(m, y[i in 1:3, j in 1:3, k in 1:3; i < j] >= 0)
I would like to apply a function f to x and to y[:, :, k] for all k in 1:3. However, I don't know how to define such a generic function.
I tried to set the argument type of f to JuMP.JuMPDict{JuMP.Variable,2}:
function f(input::JuMP.JuMPDict{JuMP.Variable,2})
...
end
I can use the function on x but not on y:
f(x) # Works
for k in 1:3
f(y[:, :, k]) # does not work as y is not an array
end
My last idea was to convert y into several JuMP.JuMPDict{JuMP.Variable,2}:
function convertTo2D(dict3D::JuMP.JuMPDict{JuMP.Variable,3}, k::Int)
dict2D = JuMP.JuMPDict{JuMP.Variable,2}() # This line returns "ERROR: KeyError: key :model not found"
for (key, value) in keys(dict3D)
if key[3] == k
dict2D[(key[1], key[2])] = value # Not sure if it will work
end
end
return dict2D
end
If this was working I could use:
for k in 1:3
f(convertTd2D(y, k))
end
Do you know how I could fix convertTo2D or do what I want another way?
Anonymous variables solved my problem. Thanks to them I can successively create the variables of y in a for loop. Variable y is now an array of "2D dictionaries" rather than a "3D dictionaries":
y = Array{JuMP.JuMPDict{JuMP.Variable,2}, 1}([])
for k in 1:3
yk = #variable(m, [i in 1:3, j in 1:3; i < j] >= 0)
f(yk)
push!(y, yk)
end

How do I raise a float to an exponent in OCaml?

I am trying to write a function that takes x and raises it to the power of n.
This code works if x and n are integers:
let rec pow x n =
if n == 0 then 1 else
if (n mod 2 = 0) then pow x (n/2) * pow x (n/2) else
x * pow x (n/2) * pow x (n/2);;
If I try to change the code to work if x is a float, it falls apart:
let rec float_pow x n =
if n == 0.0 then 1.0 else
if n mod_float 2.0 == 0.0 then float_pow x (n /. 2) *. float_pow x (n /. 2) else
x *. float_pow x (n /. 2) *. float_pow x (n /. 2);;
I get this error:
Error: This expression has type float
This is not a function; it cannot be applied.
What do I do?
The key problem, I think, is that mod is a keyword in OCaml, and is treated as an infix operator. But mod_float is just an ordinary function. You need to use it in prefix form.
So x mod n should be translated to mod_float x n.
You have another problem, which is that you're using the special-purpose == operator for equality comparison. You want to use = for equality comparisons in OCaml unless you need a "physical" comparison (which is not what you want here).
This isn't just stylistic--it really makes a difference. Note the following results:
# 0.0 == 0.0;;
- : bool = false
# 0.0 = 0.0;;
- : bool = true

Sage's (or Maxima's) solve gives a bad answer for diff(p, x) == 0?

I am using Sage to (within a script) solve a simple equation in two variables:
sage: x, y = var("x y")
sage: p = x*y + x/y + 1/x
sage: diff(p, x)
y + 1/y - 1/x^2
sage: diff(p, y)
x - x/y^2
sage: solve([diff(p,x)==0, diff(p,y)==0], [x,y])
[[x == 0, y == 0], [x == -1/2*sqrt(2), y == 1],
[x == 1/2*sqrt(2), y == 1], [x == -1/2*I*sqrt(2), y == -1],
[x == 1/2*I*sqrt(2), y == -1]]
For some reason, Sage returns a solution that isn't a solution at all, here [x == 0, y == 0] can easily be seen NOT to be an answer of [y + 1/y - 1/x^2 == 0, x - x/y^2 == 0].
Is this a bug? a known bug? or am I doing something wrong?
UPDATE: rephrased the title, and I am wondering, worst case, how can I substitute the solutions back in the system to manually check if the equations are verified?
PS: I would post this on AskSage, but it is currently down.
Well, looks like Maxima's solve function is returning the spurious solution [x = 0, y = 0]. I see that Maxima's to_poly_solve is better behaved here.
p : x*y + x/y + 1/x;
load (to_poly_solve);
[dpx, dpy] : [diff (p, x), diff (p, y)];
to_poly_solve ([dpx, dpy], [x, y]);
=> %union([x = -1/sqrt(2),y = 1],[x = 1/sqrt(2),y = 1],
[x = -%i/sqrt(2),y = -1],[x = %i/sqrt(2),y = -1])
for xy in args (%) do print (subst (xy, [dpx, dpy]));
=>
[0,0]
[0,0]
[0,0]
[0,0]
I don't know how to call to_poly_solve from Sage, although I'm pretty sure it's possible.
Hope this helps. Good luck & have fun.

Resources