I would add, having just found this out, I can’t use tuple. I need the elements of the point to be mutable. So I need to change x and y without destroying the point and creating a new one. So far hand rolled code looks like this, but I can’t use a linearAlgebra provided dot function, since it fails for tuple:
module point2D
using LinearAlgebra
export x, y, Point, dot
mutable struct PointType{S}
# x::Float64
# y::Float64
data::NTuple{S,Float64}
# PointType{2}(x::Float64,y::Float64) = new{2}(x, y)
end
x(p::PointType{2}) = p.data[1]
y(p::PointType{2}) = p.data[2]
Point(x::Float64, y::Float64) = PointType{2}((x, y))
Point() = PointType{2}((0.0, 0.0))
function dot(u::PointType{2}, v::PointType{2})
return u.data[1]*v.data[1] + u.data[2]*v.data[2]
end
x(p::PointType{3}) = p.data[1]
y(p::PointType{3}) = p.data[2]
z(p::PointType{3}) = p.data[3]
Point(x::Float64, y::Float64, z::Float64) = PointType{3}((x, y, z))
Point() = PointType{3}((0.0, 0.0, 0.0))
# test code
p1 = Point(1.0, 9.0)
p2 = Point(5.0, 4.0)
r = dot(p1, p2)
println(r)
end
This is all academic now and back to the drawing board as I need to mutate the types within the point so tuple is out. I would like the above with an Array where I provide “S”. Can you help with that. I would like to not have to pay the price for a variable length array when I know the size is 2 or 3, but I’d like that type to be compatible with linAlg functions. Thoughts?
Thanks again,
Andy