# Vector in 3 space

**URL:** <https://discourse.julialang.org/t/vector-in-3-space/64701>\
**Category:** General Usage\
**Created:** [July 15, 2021, 3:51pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701 "2021-07-15T15:51:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 15, 2021, 3:51pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/1 "2021-07-15T15:51:01Z")

</div>

I’m trying to define a vector in 3 dimensional space for a geometry application.

I can define

```julia
const Vec3 = Array{Number, 3}

Vec3(x::Number, y::Number, z::Number) = Array{Number, 3}(x, y, z)

```

in the REPL and call Vec3 to construct a Vec3, but if I put these definitions in a file and `include` it, when I try to construct a Vec3 I get a stack overflow

```julia
julia
include("src/vector_utils.jl")
distance (generic function with 1 method)
Vec3(1,2,3)
ERROR: StackOverflowError:
Stacktrace:
 [1] Array{Number, 3}(x::Int64, y::Int64, z::Int64) (repeats 79984 times)
   @ Main ~\.julia\dev\Tangle\src\vector_utils.jl:6

```

What’s up with that?

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 15, 2021, 3:55pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/2 "2021-07-15T15:55:38Z")

</div>

Oops. Looks likeI accidentally lost some text. To be clear, if the type alias and constructor are defined in the REPL the constructor works as expected, but if the definitions are loaded from a file then I get a strack overflow when calling the constructor.

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [July 15, 2021, 3:59pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/3 "2021-07-15T15:59:49Z")

</div>

I’m pretty sure that if you say `const Vec3 = Array{Number, 3}`, then that is a type alias and so your second line then expands to `Array{Number, 3}(x, y, z) = Array{Number, 3}(x, y, z)`, which will definitely cause a stack overflow.

As an aside, by the way, if you want specific three-vectors, you might look at `StaticArrays.jl`. Your current type annotation of `3` refers to the dimension of the array, not the length of it like I would guess you expect it to be doing.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 15, 2021, 4:04pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/4 "2021-07-15T16:04:17Z")

</div>

> [@Mark\_Nahabedian](#):
>
> ```julia
> const Vec3 = Array{Number, 3}
> 
> ```

This definition means that your alias is an array with 3 dimensions, meaning it’s a cuboid, not a vector. It has N\*M\*K elements (N, M and K being the size in each dimension), not 3. The second type parameter of `Array` is the number of dimensions, not the number of elements or the size in each dimension.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 15, 2021, 4:05pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/5 "2021-07-15T16:05:57Z")

</div>

I’m seeing a stack overflow error when starting from a fresh REPL session. Let’s put that aside for a minute, though - I don’t think your type definition is doing what you think it’s doing. The first parameter in to the `Array` constructor is the eltype, and if you set that to `Number`, the array will be able to hold any combination of `Number` elements, which makes life difficult for the compiler (and leads to slow code):

```julia
julia> Number[1, 2.0, π]
3-element Vector{Number}:
 1
 2.0
 π = 3.1415926535897...

julia> eltype.(ans)
3-element Vector{DataType}:
 Int64
 Float64
 Irrational{:π}

```

The second parameter is the dimension of the array, not the length, as mentioned above. Besides StaticArrays, you may also want to check out Meshes.jl, which provides several useful types (and associated methods) for geometry applications: [Vectors · Meshes.jl](https://juliageometry.github.io/Meshes.jl/dev/vectors.html)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 15, 2021, 4:14pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/6 "2021-07-15T16:14:52Z")

</div>

Independently of that particular syntax problem, use static arrays:

```julia
julia> using StaticArrays

julia> x = SVector{3,Float64}(1,2,3)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

#or
julia> struct Vec3{T} <: FieldVector{3,T}
         x::T
         y::T
         z::T
       end

julia> x = Vec3{Float64}(1,2,3)
3-element Vec3{Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

julia> x[1]
1.0

julia> x.x
1.0

```

---

<div class="post-metadata">

**Author:** ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)\
**Post date:** [July 15, 2021, 5:24pm UTC](https://discourse.julialang.org/t/vector-in-3-space/64701/7 "2021-07-15T17:24:39Z")

</div>

Thanks everyone.

Now that I try it again, I also get a stack overflow when Vec3 is defined in a REPL. I’m not going to bother to investigate the context in which this occurred several times:

```julia
Vec3(2, 2, 2)
3-element Vec3 with indices SOneTo(3):
 2
 2
 2

```

Regarding my 3 dimesnional array versus 3 element vector confusion, I thought I saw parameterized type syntax of the form Array{elementtype, dims…} but I can’t find that now.

Thank you for referring me to StaticArrays.FieldVector. That is doing what I want.
