3 @c logic.mac--Logic algebra package for Maxima CAS.
4 @c Copyright (c) 2008--2009 Alexey Beshenov <al@beshenov.ru>.
6 @c Version 2.1. Last modified 2009-01-07
8 @c logic.mac is free software; you can redistribute it and/or modify it
9 @c under the terms of the GNU Lesser General Public License as published
10 @c by the Free Software Foundation; either version 2.1 of the License,
11 @c or (at your option) any later version.
13 @c logic.mac is distributed in the hope that it will be useful, but
14 @c WITHOUT ANY WARRANTY; without even the implied warranty of
15 @c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 @c General Public License for more details.
18 @c You should have received a copy of the GNU General Public License
19 @c along with the logic.mac; see the file COPYING. If not, write to the
20 @c Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 @c Boston, MA 02110-1301, USA.
23 @c Remove the empty page before a new chapter.
24 @setchapternewpage off
26 @setfilename logic.info
35 @dircategory Mathematics/Maxima
37 * logic: (maxima/logic). Maxima share package logic for operations on Boolean variables.
40 @node Top, Introduction to logic, (dir), (dir)
43 * Introduction to logic::
44 * Definitions for logic::
45 * Function and variable index::
49 @node Introduction to logic, Definitions for logic, Top, Top
50 @section Introduction to logic
52 This is a draft version of logic algebra package for Maxima.
53 It is being developed by Alexey@tie{}Beshenov@tie{}(al@@beshenov.ru).
54 All source code is available uder the terms of GNU@tie{}GPL@tie{}2.1.
56 List of recognized operators:
58 @multitable @columnfractions .15 .10 .10 .45 .2
59 @headitem Operator @tab Type @tab Binding power @tab Description @tab Properties
63 @tab Logical NOT (negation)
68 @tab Logical AND (conjunction)
73 @tab Sheffer stroke (alternative denial, NAND)
78 @tab Webb-operation or Peirce arrow (Quine's dagger, NOR)
83 @tab Logical OR (disjunction)
98 @tab Sum modulo 2 (exclusive or)
103 @section @TeX{}@tie{}output
105 logic.mac assigns the following @TeX{}@tie{}output:
129 (%i1) load ("logic.mac")$
131 (%i2) tex (a implies b)$
135 (%i3) tex ((a nor b) nand c)$
136 $$\left(a \downarrow b\right) \mid c$$
139 (%i4) tex (zhegalkin_form (a or b or c))$
140 $$a \wedge b \wedge c \oplus a \wedge b \oplus a \wedge c \oplus b
141 \wedge c \oplus a \oplus b \oplus c$$
144 (%i5) tex (boolean_form (a implies b implies c));
145 $$ \neg \left( \neg a \vee b\right) \vee c$$
148 (%i6) tex (a eq b eq c);
156 $$\left(a \downarrow b\right) \mid c$$
157 $$a \wedge b \wedge c \oplus a \wedge b \oplus a \wedge c \oplus b
158 \wedge c \oplus a \oplus b \oplus c$$
159 $$ \neg \left( \neg a \vee b\right) \vee c$$
163 @node Definitions for logic, Function and variable index, Introduction to logic, Top
164 @section Definitions for logic
166 @deffn {Function} logic_simp (@var{expr})
168 Returns a simplified version of logical expression @var{expr}.
173 (%i1) load ("logic.mac")$
175 (%i2) logic_simp (a or (b or false or (a or b)));
179 (%i3) logic_simp (b eq a eq false eq true);
180 (%o3) eq a eq b false
183 (%i4) logic_simp ((a xor true) xor b xor true);
188 The function applies only basic simplification rules without introducing new
191 N.B. It should be merged somehow with the basic Maxima simplifier.
195 @deffn {Function} characteristic_vector (@var{expr}, @var{var_1}, ..., @var{var_n})
197 Returns a list of size @math{2^n} with all possible values of @var{expr}.
199 For example, @code{characteristic_vector (f(x,y,z), x, y, z)} is equivalent to
205 f (false, false, false),
206 f (false, false, true),
207 f (false, true, false),
208 f (false, true, true),
209 f ( true, false, false),
210 f ( true, false, true),
211 f ( true, true, false),
212 f ( true, true, true)
217 If @code{@var{var_1}, ..., @var{var_n}} is omitted, it is assumed that
220 [@var{var_1}, ..., @var{var_n}] = sort(listofvars(@var{expr}))
226 (%i1) load ("logic.mac")$
228 (%i2) characteristic_vector (true);
232 (%i3) characteristic_vector (a xor b);
233 (%o3) [false, true, true, false]
236 (%i4) characteristic_vector (a implies b);
237 (%o4) [true, true, false, true]
240 (%i5) characteristic_vector (a implies b, a, b);
241 (%o5) [true, true, false, true]
244 (%i6) characteristic_vector (a implies b, b, a);
245 (%o6) [true, false, true, true]
251 @deffn {Function} zhegalkin_form (@var{expr})
253 Returns the representation of @var{expr} in Zhegalkin basis
254 @code{@{xor, and, true@}}.
259 (%i1) load ("logic.mac")$
261 (%i2) zhegalkin_form (a or b or c);
262 (%o2) (a and b and c) xor (a and b) xor (a and c)
263 xor (b and c) xor a xor b xor c
266 (%i3) zhegalkin_form ((a implies b) or c);
267 (%o3) (a and b and c) xor (a and b) xor (a and c) xor a
274 @deffn {Function} logic_equiv (@var{expr_1}, @var{expr_2})
276 Returns @code{true} if @var{expr_1} is equivalent to @var{expr_2} and
277 @code{false} otherwise.
282 (%i1) load ("logic.mac")$
283 (%i2) e : ((a or b) xor c) and d$
285 (%i3) zhegalkin_form (e);
286 (%o3) (a and b and d) xor (a and d) xor (b and d)
290 (%i4) logic_equiv (%i2, %o3);
294 (%i5) is (characteristic_vector(%i2) = characteristic_vector(%o3));
298 (%i6) logic_equiv (x and y eq x, x implies y);
305 @deffn {Function} dual_function (@var{expr})
308 dual_function (f (x_1, ..., x_n)) := not f (not x_1, ..., not x_n).
314 (%i1) load ("logic.mac")$
316 (%i2) dual_function (x or y);
317 (%o2) not ((not x) or (not y))
327 @deffn {Function} self_dual (@var{expr})
328 Returns @code{true} if @var{expr} is equivalent to
329 @code{dual_function (@var{expr})} and @code{false} otherwise.
334 (%i1) load ("logic.mac")$
340 (%i3) self_dual (not a);
344 (%i4) self_dual (a eq b);
351 @deffn {Function} closed_under_f (@var{expr})
352 @code{closed_under_f (f (x_1, ..., x_n)} returns @code{true} if
353 @code{f (false, ..., false) = false} and @code{false} otherwise.
358 (%i1) load ("logic.mac")$
360 (%i2) closed_under_f (x and y);
364 (%i3) closed_under_f (x or y);
371 @deffn {Function} closed_under_t (@var{expr})
372 @code{closed_under_t (f (x_1, ..., x_n)} returns @code{true} if
373 @code{f (true, ..., true) = true} and @code{false} otherwise.
378 (%i1) load ("logic.mac")$
380 (%i2) closed_under_t (x and y);
384 (%i3) closed_under_t (x or y);
391 @deffn {Function} monotonic (@var{expr})
392 Returns @code{true} if characteristic vector of @var{expr} is monotonic, i.e.
395 charvec : characteristic_vector(expr)
396 charvec[i] <= charvec[i+1], i = 1, ..., n-1
399 where @code{a<=b := (a=b or (a=false and b=true))}.
404 (%i1) load ("logic.mac")$
406 (%i2) monotonic (a or b);
410 (%i3) monotonic (a and b);
414 (%i4) monotonic (a implies b);
418 (%i5) monotonic (a xor b);
422 (%i6) characteristic_vector (a or b);
423 (%o6) [false, true, true, true]
426 (%i7) characteristic_vector (a and b);
427 (%o7) [false, false, false, true]
430 (%i8) characteristic_vector (a implies b);
431 (%o8) [true, true, false, true]
434 (%i9) characteristic_vector (a xor b);
435 (%o9) [false, true, true, false]
441 @deffn {Function} linear (@var{expr})
442 Returns @code{true} if @code{zhegalkin_form(@var{expr})} is linear and
443 @code{false} otherwise.
448 (%i1) load ("logic.mac")$
450 (%i2) linear (a or b);
454 (%i3) linear (a eq b);
458 (%i4) zhegalkin_form (a or b);
459 (%o4) (a and b) xor a xor b
462 (%i5) zhegalkin_form (a eq b);
463 (%o5) a xor b xor true
467 Linear functions are also known as counting or alternating functions.
471 @deffn {Function} functionally_complete (@var{expr_1}, ..., @var{expr_n})
472 Returns @code{true} if @var{expr_1}, ..., @var{expr_n} is a functionally
473 complete system and @code{false} otherwise.
474 The constants are essential (see the example below).
479 (%i1) load ("logic.mac")$
481 (%i2) functionally_complete (x and y, x xor y);
485 (%i3) functionally_complete (x and y, x xor y, true);
489 (%i4) functionally_complete (x and y, x or y, not x);
496 @deffn {Function} logic_basis (@var{expr_1}, ..., @var{expr_n})
497 Returns @code{true} if @var{expr_1}, ..., @var{expr_n} is a functionally
498 complete system without redundant elements and @code{false} otherwise.
503 (%i1) load ("logic.mac")$
505 (%i2) logic_basis (x and y, x or y);
509 (%i3) logic_basis (x and y, x or y, not x);
513 (%i4) logic_basis (x and y, not x);
517 (%i5) logic_basis (x or y, not x);
521 (%i8) logic_basis (x and y, x xor y, true);
529 (%i1) load ("logic.mac")$
530 (%i2) logic_functions : @{ not x, x nand y, x nor y,
531 x implies y, x and y, x or y,
532 x eq y, x xor y, true, false @}$
534 (%i3) subset (powerset(logic_functions),
535 lambda ([s], apply ('logic_basis, listify(s))));
536 (%o3) @{@{false, x eq y, x and y@}, @{false, x eq y, x or y@},
537 @{false, x implies y@}, @{true, x xor y, x and y@},
538 @{true, x xor y, x or y@}, @{not x, x implies y@},
539 @{not x, x and y@}, @{not x, x or y@},
540 @{x eq y, x xor y, x and y@}, @{x eq y, x xor y, x or y@},
541 @{x implies y, x xor y@}, @{x nand y@}, @{x nor y@}@}
546 @deffn {Function} logic_diff (@var{f}, @var{x})
547 Returns the logic derivative @math{df/dx} of @math{f} wrt @math{x}.
551 logic_diff (f (x_1, ..., x_k, ..., x_n), x_k) :=
552 f (x_1, ..., true, ..., x_n) xor
553 f (x_1, ..., false, ..., x_n)
560 (%i1) load ("logic.mac")$
562 (%i2) logic_diff (a or b or c, a);
563 (%o2) (b and c) xor b xor c xor true
566 (%i3) logic_diff (a and b and c, a);
570 (%i4) logic_diff (a or (not a), a);
577 @deffn {Function} boolean_form (@var{expr})
579 Returns the representation of @var{expr} in Boolean basis
580 @code{@{and, or, not@}}.
585 (%i1) load ("logic.mac")$
587 (%i2) boolean_form (a implies b implies c);
588 (%o2) (not ((not a) or b)) or c
592 (%o3) ((not b) and a) or c
595 (%i4) logic_equiv (boolean_form (a implies b implies c),
596 zhegalkin_form (a implies b implies c));
603 @deffn {Function} demorgan (@var{expr})
605 Applies De Morgan's rules to @var{expr}:
608 not (x_1 and ... and x_n) => (not x_1 or ... or not x_n)
609 not (x_1 or ... or x_n) => (not x_1 and ... and not x_n)
615 (%i1) load ("logic.mac")$
617 (%i2) demorgan (boolean_form (a nor b nor c));
618 (%o2) (not a) and (not b) and (not c)
624 @deffn {Function} pdnf (@var{expr})
626 Returns the perfect disjunctive normal form of @var{expr}.
631 (%i1) load ("logic.mac")$
633 (%i2) pdnf (x implies y);
634 (%o2) (x and y) or ((not x) and y) or ((not x) and (not y))
640 @deffn {Function} pcnf (@var{expr})
642 Returns the perfect conjunctive normal form of @var{expr}.
647 (%i1) load ("logic.mac")$
649 (%i2) pcnf (x implies y);
655 @node Function and variable index, , Definitions for logic, Top
656 @appendix Function and variable index