Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Matrix\Operators;
4
 
5
use Matrix\Matrix;
6
use Matrix\Exception;
7
 
8
abstract class Operator
9
{
10
    /**
11
     * Stored internally as a 2-dimension array of values
12
     *
13
     * @property mixed[][] $matrix
14
     **/
15
    protected $matrix;
16
 
17
    /**
18
     * Number of rows in the matrix
19
     *
20
     * @property integer $rows
21
     **/
22
    protected $rows;
23
 
24
    /**
25
     * Number of columns in the matrix
26
     *
27
     * @property integer $columns
28
     **/
29
    protected $columns;
30
 
31
    /**
32
     * Create an new handler object for the operation
33
     *
34
     * @param Matrix $matrix The base Matrix object on which the operation will be performed
35
     */
36
    public function __construct(Matrix $matrix)
37
    {
38
        $this->rows = $matrix->rows;
39
        $this->columns = $matrix->columns;
40
        $this->matrix = $matrix->toArray();
41
    }
42
 
43
    /**
44
     * Compare the dimensions of the matrices being operated on to see if they are valid for addition/subtraction
45
     *
46
     * @param Matrix $matrix The second Matrix object on which the operation will be performed
47
     * @throws Exception
48
     */
49
    protected function validateMatchingDimensions(Matrix $matrix): void
50
    {
51
        if (($this->rows != $matrix->rows) || ($this->columns != $matrix->columns)) {
52
            throw new Exception('Matrices have mismatched dimensions');
53
        }
54
    }
55
 
56
    /**
57
     * Compare the dimensions of the matrices being operated on to see if they are valid for multiplication/division
58
     *
59
     * @param Matrix $matrix The second Matrix object on which the operation will be performed
60
     * @throws Exception
61
     */
62
    protected function validateReflectingDimensions(Matrix $matrix): void
63
    {
64
        if ($this->columns != $matrix->rows) {
65
            throw new Exception('Matrices have mismatched dimensions');
66
        }
67
    }
68
 
69
    /**
70
     * Return the result of the operation
71
     *
72
     * @return Matrix
73
     */
74
    public function result(): Matrix
75
    {
76
        return new Matrix($this->matrix);
77
    }
78
}