Giter Site home page Giter Site logo

morpheus's Introduction

GitHub tag License Travis Packagist

Morpheus

Morpheus is a matrix calculation class for PHP

Installation

$ composer require devtronic/morpheus

Usage

Create a Matrix

<?php

use Devtronic\Morpheus\Matrix;

$matrix = new Matrix([
    [1, 2, 3],
    [4, 5, 6]
]);

// or

$matrix = new Matrix();
$matrix->setData([
    [1, 2, 3],
    [4, 5, 6]
]);

Simple Operations

Add

<?php

use Devtronic\Morpheus\Matrix;

$matrixA = new Matrix([
    [1, 2, 3],
]);

$matrixB = new Matrix([
    [4, 5, 6],
]);

$matrixA->add($matrixB);

print_r($matrixA->getData());
// [ [5, 7, 9] ]

Subtract

<?php

use Devtronic\Morpheus\Matrix;

$matrixA = new Matrix([
    [4, 5, 6],
]);

$matrixB = new Matrix([
    [1, 2, 3],
]);

$matrixA->subtract($matrixB);

print_r($matrixA->getData());
// [ [3, 3, 3] ]

Multiply

<?php

use Devtronic\Morpheus\Matrix;

$matrixA = new Matrix([
    [1, 2, 3],
    [3, 2, 1],
]);

$matrixB = new Matrix([
    [1, 2],
    [10, 20],
    [100, 200],
]);

$matrixA->subtract($matrixB);

print_r($matrixA->getData());
// [
//     [321, 642],
//     [123, 246],
// ]

Scalar Operations

Scalar Multiply

<?php

use Devtronic\Morpheus\Matrix;

$matrix = new Matrix([
    [1, 2, 3],
    [3, 2, 1],
]);

$matrix->scalarMultiply(5);

print_r($matrix->getData());
// [
//     [5, 10, 15],
//     [15, 10, 5],
// ]

Scalar Division

<?php

use Devtronic\Morpheus\Matrix;

$matrix = new Matrix([
    [10, 15, 30],
    [30, 10, 15],
]);

$matrix->scalarDivide(5);

print_r($matrix->getData());
// [
//     [2, 3, 10],
//     [10, 2, 3],
// ]

Custom Operations

Scalar Operations

<?php

use Devtronic\Morpheus\Matrix;

$matrix = new Matrix([
    [1, 0, 0],
    [1, 1, 0],
]);

$matrix->scalarMatrixOperation(function($element) {
    return $element == 1 ? 0 : 1;
});

print_r($matrix->getData());
// [
//     [0, 1, 1],
//     [0, 0, 1],
// ]

"Synchronous" Operations

<?php

use Devtronic\Morpheus\Matrix;

$matrixA = new Matrix([
    [1, 0, 0],
    [1, 1, 0],
]);

$matrixB = new Matrix([
    [1, 1, 0],
    [0, 1, 0],
]);

// Simple XOR Operation
$matrixA->synchronousMatrixOperation($matrixB, function($left, $right) {
    return intval($left ^ $right);
});

print_r($matrixA->getData());
// [
//     [0, 1, 0],
//     [1, 0, 0],
// ]

Transformation

Transpose

<?php

use Devtronic\Morpheus\Matrix;

$matrix = new Matrix([
    [1, 2],
    [3, 4],
    [5, 6],
]);

$matrix->transpose();

print_r($matrixA->getData());
// [
//     [1, 3, 5],
//     [2, 4, 6],
// ]

morpheus's People

Contributors

devtronic avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.