math-php icon indicating copy to clipboard operation
math-php copied to clipboard

Add row/column algebra methods to Matrix for Vector

Open Aweptimum opened this issue 3 years ago • 3 comments

This request doesn't really have any justification in the pure linear algebra world, but I have run into a use case where I want to treat 3 columns of a 3x3 matrix as vectors. This 3x3 matrix is constructed from 3 column vectors so that I can easily multiply it by another 3x3 rotation matrix (rotating the 3 vectors in one operation). The issue is that I'd still like to operate on the individual columns of the matrix. It would be convenient if there were methods similar to the columnAdd/rowAdd methods and friends that took a row/column index and a vector as arguments.

$mat = MatrixFactory::createIdentity(3);
$displacement = new Vector([2,0,0]);
$mat  = $mat->columnAdd(0, $displacement);
var_dump($mat->getColumn(0)) // prints [3,0,0]

Currently I'm doing this instead:

$mat = MatrixFactory::createIdentity(3);
$zero = new Vector([0,0,0]);
$displacement = new Vector([2,0,0]);
$displacementMat = MatrixFactory::createFromVectors([
    $displacement, $zero, $zero
]);
$mat = $mat->add($displacementMat);

Which isn't much more trouble to write, it's just less clear what's going on.

Understandable if it's preferred to not add these kinds of methods.

Aweptimum avatar Dec 19 '22 19:12 Aweptimum

Hi @Aweptimum,

Thank you for your interest in MathPHP.

If I understand the request, it would be something like a new Matrix function called columnAddVector that:

  • GIVEN a matrix
  • AND a column
  • WHEN the vector is added to a specific column of the matrix
  • THEN the result is a new matrix with the specified column being the column sum, and the rest of the matrix unchanged.

Example

    | 1 1 1 |       | 1 |
M = | 2 2 2 |   V = | 2 |
    | 3 3 3 |       | 3 |

$R = $M->columnAddVector(0, $V);

    | 2 1 1 |
R = | 4 2 2 |
    | 6 3 3 |

And there would be equivalent ones for a row add as well.

Does that capture the intent of the feature request?

markrogoyski avatar Dec 21 '22 07:12 markrogoyski

Yes, it captures it perfectly 👍

Aweptimum avatar Dec 21 '22 14:12 Aweptimum

Forgot to mention I might have the time to implement this myself next Friday if you approve of the feature

Aweptimum avatar Dec 23 '22 18:12 Aweptimum