Engee documentation
Notebook

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.)

In [ ]:
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:

  1. A point with integer coordinates
In [ ]:
point1 = Point(3, 4);
println("Type point1: ", typeof(point1))  # Point{Int64}
Тип point1: Point{Int64}
  1. A point with real coordinates
In [ ]:
point2 = Point(3.14, 2.71);
println("Type point2: ", typeof(point2))  # Point{Float64}
Тип point2: Point{Float64}
  1. A point with rational coordinates
In [ ]:
point3 = Point(1//2, 3//4);
println("Type point3: ", typeof(point3))  # Point{Rational{Int64}}
Тип point3: Point{Rational{Int64}}

Access to the fields of the structure

Getting access to the coordinates of the points

In [ ]:
println("point1 coordinates: x = $(rpad(point1.x,4)), y = $(point1.y)")
println("Point2 coordinates: x = $(point2.x), y = $(point2.y)")
Координаты point1: x = 3   , y = 4
Координаты point2: x = 3.14, y = 2.71

We are trying to change the values (this will lead to an error because struct it is immunable)

In [ ]:
try
    point1.x = 5  # ERROR: setfield!: immutable struct of type Point cannot be changed
catch err
    err
end
Out[0]:
ErrorException("setfield!: immutable struct of type Point cannot be changed")

Creating a mutable version of the structure

If you need a mutable structure, use the keyword mutable

In [ ]:
mutable struct MutablePoint{T<:Real}
    x::T
    y::T
end
In [ ]:
# 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)")
Исходные координаты: x = 1.0, y = 2.0
Новые координаты:    x = 5.0, y = 10.0

Adding methods for working with the structure

Defining a function for calculating the distance from a point to the origin

In [ ]:
function distance_from_origin(p::Point{T}) where T<:Real
    return sqrt(p.x^2 + p.y^2)
end
Out[0]:
distance_from_origin (generic function with 1 method)

Defining a function to calculate the distance between two points

In [ ]:
function distance(p1::Point{T}, p2::Point{T}) where T<:Real
    return sqrt((p1.x - p2.x)^2 + (p1.y - p2.y)^2)
end
Out[0]:
distance (generic function with 1 method)

Creating points for testing

In [ ]:
point_a = Point(0, 0);
point_b = Point(3, 4);

Calculating the distances

In [ ]:
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))
Расстояние от точки b до начала координат: 5.0
Расстояние между точками a и b: 5.0

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