Composite data type in Julia
This example discusses the implementation of a compound data type in the Julia programming language for representing a point on a plane.
Introduction
A composite data type (or structure) in programming is used to create custom data types that can contain multiple related values. This is the main way to create complex data structures from simple components. In this case, we are creating a structure Point to represent a point on a two-dimensional plane with x and y coordinates.
The algorithm solves the problem of creating a custom data type that allows:
-
Group related data (x and y coordinates);
-
Provide strict typing for coordinates;
-
Create secure and readable data structures;
-
Support generalized programming through parametric polymorphism.
The main part
Defining the parametric structure of a Point
Defining the parametric structure Point with a typical parameter T
T <: Real means that T must be a subtype Real (Int, Float64, Rational etc.)
struct Point{T<:Real}
# Structure fields: x and y coordinates of type T
x::T
y::T
end
Creating instances of the Point structure
Creating points with different numeric types:
- A point with integer coordinates
point1 = Point(3, 4);
println("Type point1: ", typeof(point1)) # Point{Int64}
- A point with real coordinates
point2 = Point(3.14, 2.71);
println("Type point2: ", typeof(point2)) # Point{Float64}
- A point with rational coordinates
point3 = Point(1//2, 3//4);
println("Type point3: ", typeof(point3)) # Point{Rational{Int64}}
Access to the fields of the structure
Getting access to the coordinates of the points
println("point1 coordinates: x = $(rpad(point1.x,4)), y = $(point1.y)")
println("Point2 coordinates: x = $(point2.x), y = $(point2.y)")
We are trying to change the values (this will lead to an error because struct it is immunable)
try
point1.x = 5 # ERROR: setfield!: immutable struct of type Point cannot be changed
catch err
err
end
Creating a mutable version of the structure
If you need a mutable structure, use the keyword mutable
mutable struct MutablePoint{T<:Real}
x::T
y::T
end
# Creating a changeable point
mutable_point = MutablePoint(1.0, 2.0)
println("Initial coordinates: x = $(mutable_point.x), y = $(mutable_point.y)")
# Now you can change the values.
mutable_point.x = 5.0
mutable_point.y = 10.0
println("New coordinates: x = $(mutable_point.x), y = $(mutable_point.y)")
Adding methods for working with the structure
Defining a function for calculating the distance from a point to the origin
function distance_from_origin(p::Point{T}) where T<:Real
return sqrt(p.x^2 + p.y^2)
end
Defining a function to calculate the distance between two points
function distance(p1::Point{T}, p2::Point{T}) where T<:Real
return sqrt((p1.x - p2.x)^2 + (p1.y - p2.y)^2)
end
Creating points for testing
point_a = Point(0, 0);
point_b = Point(3, 4);
Calculating the distances
println("The distance from point b to the origin: ", distance_from_origin(point_b))
println("The distance between points a and b: ", distance(point_a, point_b))
Conclusion
In this example, we looked at the implementation of a composite data type. Point in the Julia language.
We have created a parametric structure that can work with various numeric types (integers, real, rational, and other subtypes). Real). We managed to create a secure and flexible data type for representing points on a plane, providing strict typing and the possibility of generalized programming. This approach is important for creating reliable code that can work with various numeric types without loss of performance and at the same time provides type checking at the compilation stage.
The example was developed using materials from Rosetta Code