PHP Logical Matrix package

3.1.0 2018-07-04 22:43 UTC

This package is auto-updated.

Last update: 2024-05-22 22:40:44 UTC


Quality Assurance

PHP 5.6 PHP 7 Build Status Test Coverage Code Climate

See the Test Contract

Please note that developer support for PHP5.4 & 5.5 was withdrawn at version 3.0.0 of this library. If you need support for PHP 5.4 or 5.5, please use a version >=2,<3


This library aims to provide logic matrix functionality and builds on the chippyash/Matrix matrix data structure library:

  • Everything has a test case
  • It's PHP 5.4+


The current library covers basic logical matrix manipulation.

If you want more, either suggest it, or better still, fork it and provide a pull request.

Check out chippyash/Matrix for Matrix data type support.

Check out chippyash/Math-Matrix for mathematical matrix operations

Check out ZF4 Packages for more packages

Operations supported

  • AndMatrix - return the result of two matrices ANDed
  • AndOperand - return matrix ANDed with a boolean operand
  • Not - return !matrix
  • OrMatrix - return the result of two matrices ORed
  • OrOperand - return matrix ORed with a boolean operand
  • XorMatrix - return the result of two matrices XORed
  • XorOperand - return matrix XORed with a boolean operand

The library is released under the GNU GPL V3 or later license


Please see the chippyash/Matrix for underlying functionality. Anything you can do with a Matrix, you can do with a LogicalMatrix.

Coding Basics

A LogicalMatrix is a matrix for which all entries are a boolean value; true or false

A shortcut for a single item matrix is to supply a single array

    use chippyash\Logic\Matrix\LogicalMatrix;

    $mA = new LogicalMatrix([]);  //empty matrix
    $mA = new LogicalMatrix([[]]);  //empty matrix
    $mA = new LogicalMatrix([true]);  //single item matrix
    $mA = new LogicalMatrix([2, false]);  //1x2 matrix
    $mA = new LogicalMatrix([2, false],[true, 'foo']);  //2x2 matrix

N.B. A matrix construction values are converted to their boolean equivalent, so '' = false, 'foo' = true, 1 = true, 0 = false etc, according to normal PHP casting rules for boolean.

As with any TDD application, the tests tell you everything you need to know about the SUT. Read them! However for the short of temper amongst us, the salient points are:

A Logical Matrix type is supplied

  • LogicalMatrix(array $source, bool $normalizeDefault = false)

Logical Matrices have additional attributes over and above a Matrix

  • Attributes always return a boolean.
  • You can use the is() method of a Matrix to test for an attribute
  • Attributes implement the chippyash\Matrix\Interfaces\AttributeInterface
    //assuming $mA is a LogicalMatrix - this will return true
    if ($mA->is('Logical')) {}
    //is the same as, which can also be used on ordinary matrices
    $attr = new Logic\Matrix\Attribute\IsLogical();
    if ($attr($mA) {}

Logical Matrices have operations

  • Operations always returns a Logical Matrix
  • The original matrix is untouched
  • You can use the magic __invoke functionality
  • Operations implement the chippyash\Logical\Matrix\Interfaces\OperationInterface
    $mC = $mA("AndMatrix", $mB);
    //same as :
    $op = new Logic\Matrix\Operation\AndMatrix;
    $mC = $op($mA, $mB);

The following operations are supplied:

  • AndMatrix - AND two matrices
  • AndOperand - AND matrix with boolean
  • Not - NOT a matrix
  • OrMatrix - OR two matrices
  • OrOperand - OR matrix with boolean
  • XorMatrix - XOR two matrices
  • XorOperand - XOR matrix with boolean

The magic invoke methods allow you to write in a functional way

        $fAnd = new AndMatrix();
        $fOr = new OrOperand();
        //($mA && $mB) || true
        return $fOr($fAnd($mA, $mB), true);

Changing the library

  1. fork it
  2. write the test
  3. amend it
  4. do a pull request

Found a bug you can't figure out?

  1. fork it
  2. write the test
  3. do a pull request

NB. Make sure you rebase to HEAD before your pull request


The library is hosted at Github. It is available at


Install Composer

For production


    "chippyash/logical-matrix": "~2.0"

to your composer.json "requires" section

For development

Clone this repo, and then run Composer in local repo root to pull in dependencies

    git clone LogicMatrix
    cd LogicMatrix
    composer update

To run the tests:

    cd LogicMatrix
    vendor/bin/phpunit -c test/phpunit.xml test/


This software library is released under the BSD 3 Clause license

This software library is Copyright (c) 2014-2018, Ashley Kitson, UK


V0... pre releases

V1.0.0 Original release

V1.0.5 Update for underlying library dependency

V1.0.6 Update phpunit version

V2.0.0 BC Break: change namespace from chippyash to Chippyash

V2.0.1 Add link to packages

V2.0.2 Verify PHP7 compatibility

V2.0.3 Update build script

V3.0.0 BC Break. Withdraw support for old PHP versions

V3.1.0 Change of license from GPL V3 to BSD 3 Clause