In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the concept of closure in mathematics and its relationship with programming." Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to understand the concept of closure in mathematics and its relationship with programming"!
What exactly is a closure?
Closure in mathematics is a more abstract concept, from abstract algebra, so here is not intended to give its definition directly, otherwise we read the estimate or confused, for the convenience of understanding, or first from the specific example, and finally give its definition.
Take addition in the set of natural numbers as an example
Let us consider a very simple example of addition on the set of natural numbers and its results.
First of all, the natural number set, which is easy to understand, is 0, 1, 2, 3... The set of integers, of course, is an infinite set.
And then there's addition, which we're familiar with, which takes two operands, takes two arbitrary numbers from the set of natural numbers, and adds them:
For example, 1 + 2, 3 + 5, 6 + 4, etc.
And then these addition operations will have a result, like 1 + 2 = 3, 3 + 5 = 8, 6 + 4 = 10, etc., and when we look at these results, like 3, 8, 10, etc., it's not hard to see that they're still part of the set of natural numbers.
And at this point, you might think, well, that's obvious, and it feels like a bunch of crap! Let's not jump to conclusions.
So, we can give a preliminary definition of closure, first there is a set (the set of natural numbers), then there is an operation (the addition operation), this operation requires two elements of the set, the final result of the operation still belongs to the set.
Since the result still belongs to the set, we say that the set of natural numbers is closed for addition.
This is for the results, that is, no matter how you operate to operate, the results are still in the collection, a little Monkey how to somersault can not escape the palm of the Buddha that kind of feeling.
If a set of natural numbers is closed under an operation such as addition, we say that it satisfies a closure property.
More generally, a set is said to satisfy a closure property if it is closed under an operation (or series of operations).
A set that is closed under an operation or collection of operations is said to satisfy a closure property.
Another operation: multiplication
The above definition involves one or a series of operations. Here is another operation, such as multiplication, which is also a set of natural numbers. Take any two numbers and multiply them:
For example, 2 × 3, 3 × 7, 5 × 4 and so on.
And then there's a result, like 2 × 3 = 6, 3 × 7 = 21, 5 × 4 = 20, etc., and when we look at these results, like 6, 21, 20, etc., it's not hard to see that they're still part of the set of natural numbers.
Thus, according to the previous definition, we can say that the set of natural numbers is also closed for multiplication.
After all, multiplication is also an addition in some sense, so it is understandable that the set of natural numbers is closed to it.
The set of natural numbers is closed for addition and closed for multiplication, so that it can be said to be closed for a series of operations (addition and multiplication).
That is, a set can be closed not only for one operation, but also for many operations.
When subtraction is introduced
After adding and multiplying, I guess some students are impatient and say, what's strange about this? Is there anything that is not closed? Let's talk about another operation, subtraction, and then we'll see that things change a little bit.
If you take a cursory look at the set of natural numbers, take two numbers and subtract them:
For example, 3 - 2, 7 - 4, 5 - 1, etc.;
And then there's a result, like 3 - 2 = 1, 7 - 4 = 3, 5 - 1 = 4, and so on, and when we look at these results, like 1, 3, 4, and so on, it's not hard to see that they're still part of the set of natural numbers.
From the above, it seems that we can also say that the set of natural numbers is closed for subtraction operations as well.
But if we look more closely at cases like 2 - 4, 3 - 6, etc., then something's not right, 2 - 4 = -2, 3 - 6 = -3, like-2, -3--these results are not part of the set of natural numbers!
In ancient times, when people's cognitive ability was still very weak, in the face of such a provocative operation as 2 - 4, it was simply like an ostrich burying its head in the sand, turning a blind eye to it, people thought that such an operation was meaningless!
Thus, the set of natural numbers is not closed for subtraction. But if we expand the set of natural numbers to the set of integers, it contains the set of negative integers, zero and the set of positive integers.
And then, for a set of integers, no matter how you subtract, the result is always in the set of integers.
Once again, Sun Wukong could not escape from the palm of Buddha's hand by doing somersaults. This was a bigger palm!
Thus, although the set of natural numbers is not closed for subtraction, once extended to the set of integers, we can say that the set of integers is closed for subtraction.
Another case is that for a finite set, such as a set with only one element 0, denoted { 0 }, we can also say that it is closed for addition, multiplication and subtraction, because 0 + 0 = 0, 0 × 0 = 0, 0 - 0 = 0, and the result is still 0 no matter what, so it is closed. In this case, instead of expanding the scope of the set, or even on the contrary, narrowing the scope of the set, you can also get the closed property, which is of course a special case.
Another Definition of Closure
The definition of closure mentioned above is a closure property satisfied by a set, and there is another definition of closure.
When a set S is not closed under certain operations,
For example, the set of natural numbers is not closed for subtraction.
We can find a minimal set containing S such that the operation is closed,
For example, you can find the set of integers, the smallest set of natural numbers, which is closed for subtraction.
Then, this minimal set is called the closure of S sets (for those operations).
The set of integers is the closure of the set of natural numbers (for subtraction).
The specific English definition is as follows:
When a set S is not closed under some operations, one can usually find the smallest set containing S that is closed. This smallest closed set is called the closure of S (with respect to these operations)
This definition is different from the definition of closure property, but they are closely related, but the emphasis of the statement is different, but they all revolve around the closure of the operation result.
As the operation continues to increase
The set of integers is closed for addition, multiplication, and subtraction, but if you continue to introduce other operations, such as division, then new problems will arise, although the case of division such as 6 divided by 2, 10 divided by 5, the result is still in the set, but more such as 2 divided by 3, 5 divided by 8 results are not in the set.
Now if you want to close the division, you have to extend the set to, say, the set of rational numbers.
In addition, a restriction must be imposed that the divisor must not be 0.
For rational numbers, when we continue to expand the operation, such as the introduction of the square root operation, many results are not closed, then we have to expand to the irrational number set; and even to the irrational number set, for the square root of negative numbers is still helpless, then we need to further expand to the complex number set...
Obviously, as more and more operations are introduced, it becomes difficult to maintain closure.
Application in Programming
So far, we've talked about closure in mathematics, so naturally we're just going to scratch the surface, let's have a concept, and we don't understand it, and we're not going to talk about it, because it's too difficult.
So, what do you think of the concept of closure in mathematics? Programming doesn't seem to work for us either. And as I said at the beginning, the closure we're talking about here is not the closure we're talking about in the programming world, so what does this mathematical closure have for our programming language?
Well, it's impossible to say there's no impact, but I'm afraid a lot of people disagree about how much impact there is, and the problem is that these impacts tend to be very basic, so that we don't think of them as impacts.
For a very simple example, we might write something like this in our programming:
2 + 4 + 5
Instead of asking why, or even why it's possible, we feel like it's taken for granted.
But from the closure point of view, 2 + 4 + 5 is possible because the result of 2 + 4 is still in the set, and therefore can continue to participate in the next operation.
When we take another example, say another expression:
(3 > 2) + 1
It's a little tricky. For some programming languages, this is still possible, but it involves an implicit transformation; for others, it may simply be a syntax error.
From the closure point of view, the result of the operation 3 > 2 does not belong to the set of numbers, but to a boolean set with only two finite elements: {true, false}.
Because the result of this operation is not a number, the subsequent operations are meaningless.
For example, true + 1 or false + 1 is meaningless.
Some programming languages continue to operate because they implicitly convert true to 1 and false to 0, so they continue to execute 1 + 1 or 0 + 1.
Some languages are more restrictive, such as java, and you write statements like this to report errors:
if (age = 60) { //can retire}
One of the pitfalls here is that a single equal sign is an assignment operation, not a comparison, which uses two equal signs == like this:
if (age == 60) { //can retire}
Java programs written in this way compile, but some languages compile in both ways, but the first case actually becomes:
if (60) { //can retire}
But if actually requires a boolean set of operands, and at this point a conversion is performed, in general, 0 will be converted to false, others are true, so 60 will be converted to true, and the final statement essentially becomes:
if (true) { //can retire}
So, the whole judgment is completely redundant, it always turns out to be true, the statement in it always executes, and in many cases, it's a bug in the program.
Thus, loose adherence to closure properties may cause us trouble; on the other hand, well-designed closure properties may give us convenience.
For example, some languages allow array elements to be arrays, which brings a lot of convenience to many operations, which is actually a manifestation of the closure property.
But some languages do not allow this, array elements can only be other elements, but not arrays, which weakens the language's ability to express, and then in programming to bring us inconvenience.
For example, for many languages, the attributes of an object can continue to be objects, or the elements of a map can be another map. In this way, we can express many complex data structures nested, and their operations can be simplified, because the result of the operation is still in the collection, and we can repeatedly use the same operation to operate on it.
For example, a tree node can be a leaf node or a subtree, which is the case for folders and their files and/or folders, so you can recursively traverse all nodes with the same operation.
And all of this, in a deeper sense, is actually a manifestation of closure properties, and because of these properties, it makes it possible.
For our programming practice, an operation that combines data objects satisfies the closure property, that is, the result obtained by combining data objects through it can itself be recombined through the same operation.
The closure property is a key element of the power of any combinatorial function, because it enables us to establish hierarchical structures made up of parts, each of which is made up of its parts, and so on.
At this point, I believe everyone has a deeper understanding of "how to understand the concept of closure in mathematics and its relationship with programming." Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.