Да-нет в среде Mathcad - PTC



Study 11Yes-No in the Mathcad environmentorBoole’s origami In the study, you will learn about Boolean and fuzzy logic, about the features of its application in programming.Mathematics: Boolean function, Boolean algebra, Boolean function extremum, fuzzy logic.Physics: switches of electrical circuits, voting machine.IT: Boolean operators, tree operator, vectorization of computer calculations.Etude site: Mathcad 15 environment, there are operators implemented as prefix and infix operators - see the right-hand side of Fig. 11.1, which return only two values: 1 (not zero) or 0 (zero): Yes - No, True - False. These operators are collected on the Boolean toolbar in Mathcad 15 (see the left part of Figure 11.1) and Comparison in Mathcad Prime (see Figure 11.2).Fig.11.1. Panel of Boolean operators in Mathcad 15 environment (left)Fig.11.2. Panel of Boolean operators in Mathcad Prime environmentSometimes Boolean operators (or, otherwise, Boolean functions) are called binary or logical. The operands of these operators (the arguments of these functions) in the Mathcad environment can be real numbers, however a number different from zero is perceived as true while zero as false.In some programming languages ??(Pascal, for example), you can start working with Boolean and other types of variables only after they are listed in the variable type declaration. This is done to save computer's memory: much less space is allocated for storing the Boolean variable than for storing an integer, and even more so material. In our days the memory of a computer, as a rule, is not a limiting factor in solving problems, and such pre-declaration of variable types is abandoned. So, at least, it's in the Mathcad environment.We will talk about Boolean functions [63], starting from the problems that arise when working with them in the Mathcad environment or in other mathematical programs, as well as in programming languages, and in general, when using digital computer technology, which is based on the binary "atom" - Elementary element of memory, located in one of two states (charged-discharged, magnetized-demagnetized, etc.). From "atoms" (bits) are made "molecules" (bytes), which in turn form new "connections" - variables, arrays of variables (see Etude 2), etc. - everything that programmers operate. Arithmetic and other actions on numbers variables and on arrays of variables is essentially nothing but a variety of Boolean (bitwise) operations.1. The Bases of Boolean Mathematics1.1. Boolean functions of one argumentThere are four basic Boolean functions of one argument: see Table 11.1, but in practice only one - f1 works, which is called negation (inversion). The other three functions return either their argument (function f2), or the constants 1 (f3) and 0 (f4).Table 11.1. Boolean functions of one argumentaf1f2f3f40101010110Notation?aa10Not(a)???!a?aa?In the first row of the notation graphics in Table 11.1 (and Table 11.2 below),are shown the Mathcad operators, that are used to implement the Boolean function. The symbol (prefix operator of negation or inversion) is present on the panels shown in Fig. 11.1 and 11.2.From these functions it is possible to compose more complex functions of one argument, but they, as a rule, are not required. Let's see how, for example, the function f2f4f1a operates. If a=1, then f1a=0, f4f1a=0, f2f4f1a=0. If a = 0, then f1a=1, f4f1a=0, f2f4f1a=0. The table of values tells us that f2f4f1a=f4a, so it is meaningful to replace a simple function with a basic Boolean function.1.2. Boolean functions of two argumentsThe basic Boolean functions of two arguments are sixteen: see Table 11.2.Table 11.2. Boolean functions of two arguments Table 11.2 is contingently divided into two halves - the "nominal" (f1 — f8) and the nameless (f9 — f16). Here are the names of the first eight functions. Rather, seven (another "gorgeous seven" - see etude 4): two functions (f5 and f6) are called the same - implication or logical consequence.f1 —conjunction (logical multiplication);f2 —disjunction (logical addition);f3 — равнозначность (эквивалентность, тождественность); equivalence (equivalence, identity);f4 — неравнозначность (неэквивалентность, разделительная дизъюнкция, сумма по модулю 2); non-equivalence (nonequivalence, separating disjunction, sum modulo 2);f5 and f6 — implication (logical consequence): f5 - implication from a to b, f6 - implication from b to a;f7 —Pierce function (Webb function, the Dagger function, the anti-disjunction - we'll talk about it in detail: see Figures 11.7 and 11.8);f8 — function (prime) of Scheffer (anti-conjunction);The other eight functions of Table 11.2 (f9 — f16, as well as the last three functions of Table 11.1), we repeat, have neither names nor practical applications . These are either constants (f15 and f16), or functions of only one argument (f11 — f14). The name, and even that conditionally, can be given only to the functions f9 and f10: the inversion (negation) of the implication. But these two functions, as a rule, apply only to real arguments: the example π?>?e?= 1 (yes, the numerical value of the ratio of the length of the circle to its diameter is greater than the numerical value of the base of the natural logarithm).Figure 11.3 shows the calling operators in the Mathcad 15 environment of some basic Boolean functions of two arguments from Table 11.2. These operators are implemented in the form of infix operators - operators, the symbol of which, as already noted, is between two operand arguments. The arrow over the vector operands in Fig. 11.3 is a special vectorization operator, i.e. elementwise execution of the action on vectors.Fig. 11.3. Operators for calling Boolean functions in Mathcad 15For some operators shown in Fig. 11.3, there can only be Boolean operands (0 or 1), and for others - also real ones: 0.1, 0.9, 2.5, etc. Figure 11.4 shows the operation of the operators Exclusive or (Xor) and Not equal to real operands: not with 0 and 1, but with 0.1 and 0.9. These operators for Boolean operands (Figure 11.3) gave identical answers, while for real operands (Figure 11.4) the answers are different. Fig. 11.4. Operator "Exclusive" or "Not equal" with real argumentsThe answer of the first operator in Fig. 11.4 (only zeros) is due to the fact that numbers other than zero are perceived as units (Truth) by Boolean functions of the Mathcad package. This is due to the fact that in earlier versions of Mathcad there were no And and Or operators, and Boolean operations were implemented through arithmetic operators of multiplication and addition - see Fig. 11.5. If you add two units (the Or operator), you get a two (true) - a number that is different from zero.Fig. 11.5. The implementation of logical multiplication and logical addition in the Mathcad package of earlier versionsBoolean functions And and Or are often illustrated by an electrical circuit: the serial connection of switches (devices in two possible positions 0 and 1) is a conjunction (logical multiplication), and the parallel one is a disjunction (logical addition). In Fig. 11.6 also shows a less trivial example - the electrical analogue of the equivalence (Eqv): a circuit for connecting two switches, so that the light is independently switched on and extinguished from two different places. Such switches will be useful in a long corridor or on the stairs of a multi-storey building, in a suburban area and in other places. On the author's site you can work with such a virtual switch.Fig. 11.6. The electrical analogs of the three Boolean functions And, Or and EqvTables 11.1 and 11.2 attempt to collect all the names of the base functions and the symbols of the basic operators used to implement the Boolean algebra. The list is, of course, incomplete. You can extend it with examples from other programming languages (Pascal, Fortran, etc.) and mathematical programs (Maple, MatLab, Mathematica, etc.).It is possible to note some redundancy of the functions collected in Tables 11.1 and 11.2. In specific programming languages, there is a certain limited set of built-in Boolean functions and operators, with which you can make any other Boolean functions. To prove the sufficiency of such a set for produce any Boolean operators is a difficult mathematical task. If a finite number of functions are chosen, the actions that are subordinate to special rules are described with them and the possibility of realizing all other functions by means of these functions and actions is proved, then it is said an algebra.Here is a list of such functions and operators embedded in popular software environments:BASIC programming language: Not, And, Or, Xor and ImpC programming language: !, & (logical multiplication), && (bitwise conjunction), !=, ║and = = mathematical program Mathcad: ?, ^, ? and (see Figures 11.1 and 11.2)The programmer can enter missing binary functions (operators) into the program itself through the mechanism of user functions, and we'll show it below.Distinction of binary functions and operators into main (basic) and auxiliary ones appeared long before computers and was legitimized in the form of binary algebras (in parentheses their base notation):algebra of logic (?, &, ?, → and ?);Boolean algebra (?, & and ?);Zhegalkin algebra (&, ? and );Peirce algebra (↓);Sheffer algebra (│).The last two binary algebras are noteworthy because in their basis there is only one binary function ↓ or |, from which it is possible to build all the remaining ones.Fig. 11.7. Creating the Boolean Peirce function in the Mathcad environment using negation and logical addition31711902420620or020000or31745211276709or020000or1431985120770With the unique Pierce function, we determine all the remaining functions00With the unique Pierce function, we determine all the remaining functionsFig. 11.8. Building the Boolean functions using the Pierce functionFigure 11.8 shows the Mathcad 15 document, where other Boolean functions are built using the Pierce function (↓ - see Fig. 11.7): one function of one argument (negation, inversion - Not) and five functions of two arguments: And, Or, Imp, Sheffer's stroke (|) and Eqv. The last three functions (Imp, Sheffer's stroke and Eqv) are defined using previously defined functions. This is done to make the drawing more compact, but you can refuse the mechanism of embedding user functions for example, (Imp (a, b): = Or (Not (a), b))and operate "for the purity of the experiment" only by Pierce's (stroke) function.Once, high hopes were placed on the Peirce algebra and on the Schaeffer algebra, in the sense of building a digital computer of the same type. Then this idea was abandoned for a number of reasons, the main one being that any computer already consists only of certain types of elements - from transistors integrated into integrated circuits (chips).One can also note a certain insufficiency of the set of mathematical tools displayed in Tables 11.1 and 11.2. Take, for example, probably the most popular function of Boolean algebra - a conjunction (a consequence of its popularity and that it has the highest number of names and symbols for its designation - see column f1 in Table 11.2). The truth-of-truth (the truth of conjunction) column in the truth table ( so are called tables 11.1 and 11.2) should ideally be as shown in Table 11.3.Table 11.3. Closed conjunctionA dash beside zero in the first column of Table 11.3 means that if the first (a) argument is zero, then there is no need to check what the second argument (b) is equal to, and vice versa. This is provided by creating some ("not lazy") programming languages - the C language, for example. When programming the same in the BASIC environment, the conditional jump on a conjunction can be written as follows:If a And b Then… (1st method)but it is better this way:If a Then If b Then… or If b Then If a Then… (2nd method)The second way of coding allows not only to speed up the calculations, but also to avoid some errors - the logical expression b can make sense only if the alternative question a is answered positively. Here is a typical example of such a "programmer" situation:If I> 0 Then If V(i) >V(i-1) Then… We can say that in the BASIC language there are two conjunctions: And and Then If.In Tables 11.1 and 11.2, we repeat, we collected the Boolean functions of one (table 11.1) and two (table 11.2) arguments. But, returning to the conjunction, we can say that this function has not two but ... one and a half arguments - see Table 11.3. The same non-integer number of arguments can be observed for other Boolean functions:Table 11.4. Boolean functions of one and a half, one and zero of the argumentsIt can be noted that operators in Table 11.2 were originally designed to work not with binary but with real operands: ">", "<", "≥", "≤", "=" and "≠". But if we take into account the fact that zero and one are in the set of real numbers, then the inclusion of these operators in Table 11.2 is completely natural. In this series (">","<","≥","≤","=" and "≠")redundancy and insufficiency can also be noted. With redundancy, everything is more or less clear (for example, "more", is the inversion from the operator "less than or equal to", etc.). Insufficiency can be observed, for example, when working with real variables, since in this case instead of the "equal" operator, it is more appropriate to use the "approximately equal" operator, which is not among the operators in the built-in lists. One can also recall the existence of the concepts "much more" or "much less". These relational operators also return binary values, but in fact they have not two but three arguments (operands): a comparable pair of real numbers and some kind of contextual notion of what is "about" or "much".Figure 11.9 shows the creation of the Boolean operator “Approximately equal”. in the Mathcad 15 environment.Fig. 11.9. Creating a Boolean operator Approximately equal to in Mathcad 15 In the Mathcad Prime environment, an option appeared that makes it possible to turn the "equals" operator into an "approximately equal" operator - see Fig. 11.10.Fig. 11.10. Approximately equal in Mathcad Prime environmentBoolean functions in Mathcad can be used not only for logical computations. They can also be used to solve equations.Example 1. Solve the equation:4sinπx-cos1.7x=416-x2-x6+x5-4x+9 .For the solution, one of the solver operators described in Etude 4 could be used. And it would be nice to first plot the graphs of both sides of the equation (note that it is sufficient to plot only the interval [-4, 4], since the inequality 16≥х2) in order to locate the available roots. But you can do otherwise.We introduce the following logical variables: a := sinπx≥0; b := cos1.7x≥0;c := x6+x5-4x+9≥0; p := 4sinπx-cos1.7x≥0;q := 416-x2-x6+x5-4x+9. Clearly, the roots of the equation will be found approximately (with the exception of the obvious integer root x = 0), so we must choose the desired step, and for each point of the above segment obtained with this step, we need to verify the truth of the statement.f13a∧f13b∧f13c∧f13p∧f13q?f11p?f11q?p≈q.The test statement can be simplified if we transform the original equation to the form 4sinπx+416-x2=x6+x5-4x+9+cos1.7x. Such a form will make it impossible to verify the truth of the statement that both sides of the equation at each admissible point have the same signs. And for r := 4sinπx+416-x2 and t := x6+x5-4x+9+cos1.7x . It will be required at each point to check the truth of the statement f13a∧f13b∧f13c∧r≈t.Example 2. Solve the equation 4sinπx+cos1.7y=416-x4+9-y2 .The solution of one equation with two variables can be any (including infinite) number of points in the plane. For example, the equation-x2-y2=0 has a unique solution (0,0), while the equation x2+y2=1 has infinitely many solutions - points forming a circle of unit radius and center at zero. The solutions of equations with two unknowns are discussed in Htude 4. Here we again apply the logical variables:a := sinπx≥0; b := cos1.7y≥0; p := 4sinπx+cos1.7y; q := 416-x4+9-y2, and we will verify the truth of the statement f13a∧f13b∧p≈q at the points of the rectangle -2≤x≤2, -3≤y≤3, defined with a certain step in each of the variables. All the points found in this way will give us an approximate solution of the original equation.The classical Boolean algebra operates with argument-statements that can be true or false. For example, saying a: "The student passed the math exam", saying b: "The student passed the computer science exam", saying a∧b: "The student passed the exams in mathematics and computer science". Statements can be naturally related to sets. For example, A is the set of students who passed the math exam; B is the of students who passed the exam in computer science. The question can be posed as follows: "Is it true or false that this student belongs to the set A ? B?Such a natural correspondence of Boolean algebra and algebra of sets allows one to take advantage of this or that interpretation of the statements. Thus, a geometric visual representation of the relations between statements (sets) in a Boolean algebra is represented by circles or other figures. Such figure is called a Venn diagram . Element 1 of Boolean algebra is the universal set, or a true statement, it can be represented as a square and denoted by the letter Ω. To the element 0 corresponds an empty set, or a false statement, it can be denoted by ?. Some non-empty set A, or some statement а, is represented as a circle or another figure located in a square. What lies outside the set A is the complement of A or the negation of the statement а, that is, ?A. The sum (disjunction) of two sets A and B is represented as a union of the figures representing them and can be indicated by the symbol "+". The product (conjunction) of two sets A and B is represented as an intersection of the two figures representing them and can be designated by the symbol "?". The Venn diagram is used to illustrate the validity of axioms and Boolean algebra theorems, as well as to represent the relations between logical statements. As a special case of Euler circles, the Venn diagram depicts a finite Boolean algebra, clearly describing all its properties and combinations of these properties. For n = 3, this diagram is usually represented as three circles with centers at the vertices of an equilateral triangle and with the same radius, slightly smaller than the length of the side of the triangle. All this design is placed in a square.With the help of Venn diagrams, you can simplify or compose several Boolean functions from table functions. It allows us to understand the validity of logical identities. Clearly, the same figures on the Venn diagram correspond to the (left and right) parts of the logical expressions. Thus, the union of the circle A and the intersection A ? B is the circle A, therefore, we get that A + A ? B = A. Note that for any statement (set) A the Boolean expression а + (?а) is true, i.e. A + (?A) = Ω. The same is equivalent to the property ? (?а) = а, which is also called the "Excluded Third Law". Indeed, if A is a circle inside a square, then (?A) is a part of a square without a circle, but then ? (?A) is the part of the square from which the part of the square without the circle is excluded; just the circle.We list the main properties of Boolean algebra. They are easy to understand by giving the statements a, b, c the values "true" and "false", or by shading certain areas on the Venn diagram for the sets A, B and C.А+А = А;А?А = А;А+В = В+А;А?В = В?А;А+А?В = А;А+(В+С) = (А+В)+С;А?(В?С) = (А?В) ?С;А?(В+С) = А?В+А?С;(А+В)?(А+С) = А+ В?С; А+? = ?;? ? = ?; ? ? = ?; А+ ? = А;А?? = А;А? ? = ?;А+(? А) = ?;А?(? А) = ?;? (А+В) = (?А)? (?В);? (А?В) = (?А)+ (?В);? (?А)=А.These properties are most often used to prove equalities that define complex statements or sets. For example, you want to simplify a logical statement f(a,b,c)=a?a∧b?b∧c?(?a)∧c. Of course, you can, using basic logic functions, create a table of values for this function, and then look for another, simpler function with exactly the same values (if it exists). But we proceed as follows. We draw the Venn diagram in the case of the intersection of all three sets (for convenience we replaced the circles in rectangles in Fig. 11.11). Given the location of the rectangles, we see thatA+A?B+B?C+(?A)?C=A+C. Now we strictly prove this equality using the properties of a Boolean algebra:A+A?B+B?C+(?A)?C=(A+(?A)?C)+(A?B+B?C)=(A+(?A)?C)+B?(A+C)=?=(A+(?A))?(A+C)+B?(A+C)=??(A+C)+B?(A+C)=(?+B)?(A+C)=??(A+C)=A+C.?It is not always possible to obtain a rigorous proof of the assumed equality. And if equality is not right, then it is even more difficult to find a solution, since, when carrying out the transformations, it is necessary to bring equality to a contradiction. A graphical method using Venn diagrams is not a rigorous or complete solution if the equation is verified, since in the case of a graphical solution it will be necessary to consider all possible locations of all sets. But the diagrams give an unambiguous answer in the case when the equation is not verified. Indeed, if it was possible to find, at least one example, when the equation is not true, then it is not exactly satisfied.?АА?ВВВ?СС(? A)?CThe Venn diagram for A + A ? B + B ? C + (? A) ? C.Clearly, the use of the diagram is especially convenient if there are a lot of statements.If we talk not about the classical Boolean algebra, but about the actual programming practice, it should be recognized that the variables appearing in Tables 11.1 and 11.2 can take not two (0 or 1) but three values: 0, 1 and indefinitely. This feature we have already fixed in Tables 11.3 and 11.4, where instead of specific values ??of the arguments (0 or 1) there is a dash. In programming languages, there are tools that handle such "dashes" in truth tables. If the argument of the Boolean function is not defined, then the calculation can either be interrupted by an error message, or go through the third scenario.Arguments of Boolean functions can take not two but three, but ... an infinite set of real values. This set is divided into two extremely unequal parts: zero and nonzero (?0, if we speak in the language of Table 11.1 for non-zero numbers, which we repeat, Boolean functions of the Mathcad package are perceived as units). It also happens that the Boolean function returns not only zeros and ones. Here, for example, how the Or function works in one of the implementations of the BASIC language (or rather, the version of the BASIC language, which was completed with the popular PC "Iskra-226" - the tracing machine "Wang-2000"): see table 11.5.Table 11.5. "Disjunctive conjunction"It is possible to admit also such work of the expanded operator Or: see the table 11.6. Table 11.6. Extended conjunction (disjunction)It's one thing when the first argument (operand) is not zero, the other - when the second, and the third - when both are not zero at the same time.Summarizing our analysis of Tables 11.1 and 11.2, we can say that the described Boolean functions in real computer implementations can have non-binary arguments and return again non-binary results. But there is no special non-binary meaning in this. Simply, we repeat, for technological reasons, the real variables in the described implementations of programming languages ??(BASIC, Mathcad) "concurrently" play the role of binary ones. Boolean functions take their real arguments "binary": zero is zero ("No", "False"), and the rest is a unit ("Yes", "True").This, one might say, "categorical" behavior of the described built-in functions contradicts the provisions of fuzzy sets theory (see Etude 12) and fuzzy logic theory (64, 65). It is necessary, for example, to process on the computer not only the "black and white" (binary) answers of the questionnaire type "Yes (1)" - "No (0)", but also the "color" (real) answers: "Yes (1) "," Yes, rather than no (0.75, for example) "," No, yes, no (0.5) "," Rather no than yes (0.25, for example) "and" No (0) ". If we talk not about statistics, but about electrical engineering and return to the electrical circuits, which are often used to illustrate the work of Boolean functions (see Figure 11.6), it is possible to mention the fact that now in everyday life switches smoothly change the glow of lamps from 100% to zero. Such devices were earlier used in theaters and cinemas. Doctors say that a smooth transition from light to darkness through the twilight does not spoil the eyesight. (In cinemas, light is extinguished smoothly, of course, not for medical reasons, but for other reasons - if the light is abruptly extinguished, panic may occur.)There are many other examples that lead to the fact that the arguments of the functions listed in Tables 11.1 and 11.2 can and should not only be binary, but also real numbers from zero to one. And the functions listed in Tables 11.1 and 11.2 must return real values that smoothly change from zero to one. Here's how, for example, you can specify a "smooth" negation function - see Fig. 11.12.Fig. 11.12. Smooth negation"Smooth" conjunctions (And) and "smooth" disjunction (Or) turn out on their own if one remembers that one of the notations of the conjunction is min (minimum - see column f1 in Table 11.2), and one of the disjunction symbols is max (maximum - see column f2):And(a, b) := min(a, b) Or(a, b) := min(a, b)Figures 11.13 - 11.16 show a three-dimensional graphical representation of fuzzy Boolean functions And, Or and Eqv. The latter function is represented in two variants of Eqv (Figure 11.15) and Eqv1 (Figure 11.16).Fig. 11.13. Fuzzy Graph AndFig. 11.14. Fuzzy Graph OrFig. 11.15. Fuzzy Graph EqvFig. 11.16. Fuzzy Graph Eqv (second option)The broken surfaces shown in Figures 11.13-11.16 and 11.19 below are similar to the Japanese origami figures, folded from a sheet of paper.Figure 11.17 shows the creation of fuzzy Boolean functions and their display on the graphs of lines of the same level (on contour charts).Fig. 11.17. Fuzzy Boolean functions1.3. Functions of many argumentsBoolean functions of three arguments are 256, and four ... Reader, define it yourself!The functions And and Or, if they are identified with the min and max functions (see above), can have a variable number of arguments - with Boolean (0 or 1) or fuzzy (from 0 to 1) values. This property is also possessed by some other Boolean functions. What kind? Reader, find again this by yourself!Figures 11.18 and 11.19 show the formation in Mathcad of a fuzzy function of three arguments, returning ... the decision of the jury, which can issue no longer "black and white" answers (guilty - innocent), but ... "color": guilty of 30%, innocent by 70%, etc. In the electric analogue of the voting machine, the switches (0 or 1) are replaced by rheostats (from 0 to 1).A sad noteIt is said that in the United States, an electric chair leads to the death of several people. In this case, the real switch is activated by only one person. The remaining members of this execution include fake switches. And no one knows where the fake, and where the real switch. Such a somewhat sanctimonious procedure makes it possible for each such executioner to think that it was not him, but someone else was the cause of the person's death. If all the actual switches are changed with rheostats (see Figure 11.18 below), smoothly changing the voltage, then death can be replaced by non-lethal punishment: the convicted criminal will receive an electric shock (strong or weak), but will remain alive.Fig. 11.18. Voting machine: parallel (Or) connection in series (And) connected switches (rheostats) Fig. 11.19. Function of fuzzy voting of three members of the juryThe Verdict function, shown in Fig. 11.19, with binary arguments returns a binary response, and for real arguments it returns a real one, naturally. The corresponding "Boolean die" (origami) is shown at a = 0.7 - we see a hybrid of the conjunction with disjunction: the fuzzy (from 0 to 1) opinion of one member of the jury smoothly translates the verdict from the Or area (see Figure 11.14) to the And area (see Figure 11.13). Task for readers:Create a function similar to the one shown in Fig. 11.19, but not for three, but for any number of jury members who make their verdict vague - a real number from 0 to 1.Create a Boolean function to turn on the light from anywhere.Find online logic puzzles and try to solve them using the tools described in this etude. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery