Steffen Beyer > Math-MatrixBool-5.8 > Math::MatrixBool

Download:
Math-MatrixBool-5.8.tar.gz

Dependencies

Annotate this POD

Related Modules

Bit::Vector
Inline::C
more...
By perlmonks.org

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 5.8   Source  

NAME ^

Math::MatrixBool - Matrix of Booleans

Easy manipulation of matrices of booleans (Boolean Algebra)

SYNOPSIS ^

Please refer to "OVERLOADED OPERATORS" below for ways of using overloaded operators instead of explicit method calls in order to facilitate calculations with matrices!

DESCRIPTION ^

This class lets you dynamically create boolean matrices of arbitrary size and perform all the basic operations for matrices on them, like

-

setting or deleting elements,

-

testing wether a certain element is set,

-

computing the sum, difference, product, closure and complement of matrices,

(you can also compute the union, intersection, difference and exclusive-or of the underlying bit vector)

-

copying matrices,

-

testing two matrices for equality or inclusion (subset relationship), and

-

computing the number of elements and the norm of a matrix.

Please refer to "OVERLOADED OPERATORS" below for ways of using overloaded operators instead of explicit method calls in order to facilitate calculations with matrices!

OVERLOADED OPERATORS ^

Calculations with matrices can not only be performed with explicit method calls using this module, but also through "magical" overloaded arithmetic and relational operators.

For instance, instead of writing

    $matrix1 = Math::MatrixBool->new($rows,$columns);
    $matrix2 = Math::MatrixBool->new($rows,$columns);
    $matrix3 = Math::MatrixBool->new($rows,$columns);

    [...]

    $matrix3->Multiplication($matrix1,$matrix2);

you can just say

    $matrix1 = Math::MatrixBool->new($rows,$columns);
    $matrix2 = Math::MatrixBool->new($rows,$columns);

    [...]

    $matrix3 = $matrix1 * $matrix2;

That's all!

Here is the list of all "magical" overloaded operators and their semantics (meaning):

Unary operators: '-', '~', 'abs', testing, '!', '""'

Binary (arithmetic) operators: '+', '*', '|', '-', '&', '^'

Binary (relational) operators: '==', '!=', '<', '<=', '>', '>='

Binary (relational) operators: 'cmp', 'eq', 'ne', 'lt', 'le', 'gt', 'ge'

Note that both arguments to a binary operator from the list above must be matrices; numbers or other types of data are not permitted as arguments and will produce an error message.

'-'

Unary Minus / Complement ( $matrix2 = -$matrix1; )

The unary operator '-' computes the complement of the given matrix.

'~'

Transpose ( $matrix2 = ~$matrix1; )

The operator '~' computes the transpose of the given matrix.

abs

Absolute Value ( $no_of_elem = abs($matrix); )

Here, the absolute value of a matrix has been defined as the number of elements the given matrix contains. This is NOT the same as the "norm" of a matrix!

test

Boolean Test ( if ($matrix) { ... } )

You can actually test a matrix as though it were a boolean value.

No special operator is needed for this; Perl automatically calls the appropriate method in this package if "$matrix" is a blessed reference to an object of the "Math::MatrixBool" class or one of its derived classes.

This operation returns "true" (1) if the given matrix is not empty and "false" ('') otherwise.

'!'

Negated Boolean Test ( if (! $matrix) { ... } )

You can also perform a negated test on a matrix as though it were a boolean value. For example:

    if (! $matrix) { ... }

    unless ($matrix) { ... }     #  internally, same as above!

This operation returns "true" (1) if the given matrix is empty and "false" ('') otherwise.

'""""'

"Stringification" ( print "$matrix"; )

It is possible to get a string representation of a given matrix by just putting the matrix object reference between double quotes.

Note that in general the string representation of a matrix will span over multiple lines (i.e., the string which is generated contains "\n" characters, one at the end of each row of the matrix).

Example:

    $matrix = new Math::MatrixBool(5,6);
    $matrix->One();
    print "$matrix";

This will print:

    [ 1 0 0 0 0 0 ]
    [ 0 1 0 0 0 0 ]
    [ 0 0 1 0 0 0 ]
    [ 0 0 0 1 0 0 ]
    [ 0 0 0 0 1 0 ]
'+'

Addition ( $matrix3 = $matrix1 + $matrix2; )

The '+' operator calculates the sum of two matrices.

Examples:

    $all   =  $odd + $even;

    $all  +=  $odd;
    $all  +=  $even;

Note that the '++' operator will produce an error message if applied to an object of this class because adding a number to a matrix makes no sense.

'*'

Multiplication ( $matrix3 = $matrix1 * $matrix2; )

The '*' operator calculates the matrix product of two matrices.

Examples:

    $test   =  $one * $one;

    $test  *=  $one;
    $test  *=  $test;

Note that you can use matrices of any size as long as their numbers of rows and columns correspond in the following way (example):

        $matrix_3 = $matrix_1 * $matrix_2;

                          [ 2 2 ]
                          [ 2 2 ]
                          [ 2 2 ]

              [ 1 1 1 ]   [ 3 3 ]
              [ 1 1 1 ]   [ 3 3 ]
              [ 1 1 1 ]   [ 3 3 ]
              [ 1 1 1 ]   [ 3 3 ]

I.e., the number of columns of matrix #1 is the same as the number of rows of matrix #2, and the number of rows and columns of the resulting matrix #3 is determined by the number of rows of matrix #1 and the number of columns of matrix #2, respectively.

This way you can also perform the multiplication of a matrix with a vector, since a vector is just a degenerated matrix with several rows but only one column, or just one row and several columns.

'|'

Union ( $matrix3 = $matrix1 | $matrix2; )

The '|' operator is used to calculate the union of two matrices (of corresponding elements).

Examples:

    $all   =  $odd | $even;

    $all  |=  $odd;
    $all  |=  $even;
'-'

Difference ( $matrix3 = $matrix1 - $matrix2; )

The operator '-' calculates the (dotted) difference of two matrices, i.e.,

    0 - 0 == 0
    0 - 1 == 0
    1 - 0 == 1
    1 - 1 == 0

for each corresponding element.

Examples:

    $odd   =  $all  - $even;

    $all  -=  $even;

Note that the '--' operator will produce an error message if applied to an object of this class because subtracting a number from a matrix makes no sense.

'&'

Intersection ( $matrix3 = $matrix1 & $matrix2; )

The '&' operator is used to calculate the intersection of two matrices (of the corresponding elements).

Examples:

    $rest  =  $all & $even;

    $all  &=  $even;
'^'

ExclusiveOr ( $matrix3 = $matrix1 ^ $matrix2; )

The '^' operator is used to calculate the exclusive-or of two matrices (of their corresponding elements).

In fact this operation is identical with the addition of two matrices in this case of a Boolean Algebra.

Examples:

    $odd   =  $all  ^ $even;

    $all  ^=  $even;
'=='

Test For Equality ( if ($matrix1 == $matrix2) { ... } )

This operator tests two matrices for equality.

Note that without operator overloading, ( $matrix1 == $matrix2 ) would test wether the two references pointed to the same object! (!)

With operator overloading in effect, ( $matrix1 == $matrix2 ) tests wether the two matrix objects contain exactly the same elements!

'!='

Test For Non-Equality ( if ($matrix1 != $matrix2) { ... } )

This operator tests wether two matrices are different.

Note again that this tests wether the contents of the two matrices are not the same, and not wether the two references are different!

'<'

Test For True Subset ( if ($matrix1 < $matrix2) { ... } )

This operator tests wether $matrix1 is a true subset of $matrix2, i.e. wether the elements contained in $matrix1 are also contained in $matrix2, but not all elements contained in $matrix2 are contained in $matrix1.

Example:

        [ 1 0 0 0 0 ]                       [ 1 0 0 0 1 ]
        [ 0 1 0 0 0 ]                       [ 0 1 0 0 0 ]
        [ 0 0 1 0 0 ]  is a true subset of  [ 0 0 1 0 0 ]
        [ 0 0 0 1 0 ]                       [ 0 0 0 1 0 ]
        [ 1 0 0 0 1 ]                       [ 1 0 0 0 1 ]

        [ 1 0 0 0 0 ]                       [ 1 0 0 0 1 ]
        [ 0 1 0 0 0 ]                       [ 0 1 0 0 0 ]
   but  [ 0 0 1 0 0 ]   is not a subset of  [ 0 0 1 0 0 ]
        [ 0 0 0 1 0 ]                       [ 0 0 0 1 0 ]
        [ 1 0 0 0 1 ]                       [ 0 0 0 0 1 ]

(nor vice-versa!)

        [ 1 0 0 0 1 ]                       [ 1 0 0 0 1 ]
        [ 0 1 0 0 0 ]                       [ 0 1 0 0 0 ]
   and  [ 0 0 1 0 0 ]     is a subset of    [ 0 0 1 0 0 ]
        [ 0 0 0 1 0 ]                       [ 0 0 0 1 0 ]
        [ 1 0 0 0 1 ]                       [ 1 0 0 0 1 ]

but not a true subset because the two matrices are identical.

'<='

Test For Subset ( if ($matrix1 <= $matrix2) { ... } )

This operator tests wether $matrix1 is a subset of $matrix2, i.e. wether all elements contained in $matrix1 are also contained in $matrix2.

This also evaluates to "true" when the two matrices are the same.

'>'

Test For True Superset ( if ($matrix1 > $matrix2) { ... } )

This operator tests wether $matrix1 is a true superset of $matrix2, i.e. wether all elements contained in $matrix2 are also contained in $matrix1, but not all elements contained in $matrix1 are contained in $matrix2.

Note that ($matrix1 > $matrix2) is exactly the same as ($matrix2 < $matrix1).

'>='

Test For Superset ( if ($matrix1 >= $matrix2) { ... } )

This operator tests wether $matrix1 is a superset of $matrix2, i.e. wether all elements contained in $matrix2 are also contained in $matrix1.

This also evaluates to "true" when the two matrices are equal.

Note that ($matrix1 >= $matrix2) is exactly the same as ($matrix2 <= $matrix1).

cmp

Compare ( $result = $matrix1 cmp $matrix2; )

This operator compares the two matrices lexically, i.e. it regards the two bit vectors representing the two matrices as two large (unsigned) numbers in binary representation and returns "-1" if the number for $matrix1 is smaller than that for $matrix2, "0" if the two numbers are the same (i.e., when the two matrices are equal!) or "1" if the number representing $matrix1 is larger than the number representing $matrix2.

Note that this comparison has nothing to do whatsoever with algebra, it is just an arbitrary order relationship!

It is only intended to provide an (arbitrary) order by which (for example) an array of matrices can be sorted, for instance to find out quickly (using binary search) if a specific matrix has already been produced before in some matrix-producing process or not.

eq

"equal"

ne

"not equal"

lt

"less than"

le

"less than or equal"

gt

"greater than"

ge

"greater than or equal"

These are all operators derived from the "cmp" operator (see above).

They can be used instead of the "cmp" operator to make the intended type of comparison more obvious in your code.

For instance, ($matrix1 le $matrix2) is much more readable and clearer than (($matrix1 cmp $matrix2) <= 0)!

SEE ALSO ^

Bit::Vector(3), Math::MatrixReal(3), DFA::Kleene(3), Math::Kleene(3), Set::IntegerFast(3), Set::IntegerRange(3).

VERSION ^

This man page documents "Math::MatrixBool" version 5.8.

AUTHOR ^

Steffen Beyer <STBEY@cpan.org>.

COPYRIGHT ^

Copyright (c) 1995 - 2009 by Steffen Beyer. All rights reserved.

LICENSE AGREEMENT ^

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: