I aim to divide elements of two different interval vectors, alpha, and gamma, and want to store the resultant intervals in a vector form. I am trying to run this code in Pluto-Julia but not getting the results:
using IntervalArithmetic, Plots, PlutoUI, Arblib, RecursiveArrayTools, StaticArrays
begin
α = [(200..225); (225..250); (250..275); (275..300)]
γ = [(2..2.25); (2.25..2.5); (2.5..2.75); (2.75..3)]
local kk = 0
for i in 1:4
for j in 1:4
ol = α[i]/γ[j]
kk = kk + 1
end
end
@show ol
end
I expect that the resultant interval vector should contain 16 elements (4*4). I think there is a syntax error. I would be great help for me if code is provided for generalized case, means i and j will not be just 4 and 4 but can be changed.
I don't know why you are using a
begin
block, but if you intend to use a hard scope, the best choice is to wrap it in a function (Also, it's possible to use thelet
block, but I won't recommend it generally) and thebein
blocks do not introduce a new scope.Instead, you can separate elements of a container via comma or semicolon. In your case, you can do it like this:
begin
blocks do not introduce a new scope (read here about scopes in Julia), then you don't need to declare thelocal
variable (unless there is further code that you didn't provide in the question).for
loop in a container:One way is first to define initialized containers and then update their values (as it seems you intended such an approach. Although the structure can be improved much more, I won't change the overall code structure of OP, but the content in the following code block):
Note that the result is a 4*4 Matrix (as you expected) with 16 elements.
ol = Matrix{NTuple{2, Float64}}(undef, 4, 4)
?Here, I defined an Initialized
Matrix
with asize
of 4*4 in which elements are Tuples with alength
of 2 and theFloat64
element type.ol[i, j] = (α[i][1]/β[j][1], α[i][2]/β[j][2])
?Here, I'm trying to divide the first member of the
i
'th element of theα
by the first member of thej
'th element of theβ
(in theα[i][1]/β[j][1]
), and the second member of thei
'th element of theα
by the second member of thej
'th element of theβ
(in theα[i][2]/β[j][2]
) and replace the result with the[i, j]
element of theol
.Update
Here is a similar structure using the
IntervalArithmetic.jl
package:Then if I check for the
ol
and thekk
:Since this approach is what you desire, we can write it least a little bit better. First, I begin by defining a function. Second, we can use
Iterators.product
to avoid nestedfor
loop:This function is written dynamic and can be applied on the
α
andβ
with the type ofVector{Interval{Float64}}
in any length. Also, we can make it even better by using broadcasting in afor
loop: