Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add row/column algebra methods to Matrix for Vector #459

Open
Aweptimum opened this issue Dec 19, 2022 · 3 comments
Open

Add row/column algebra methods to Matrix for Vector #459

Aweptimum opened this issue Dec 19, 2022 · 3 comments

Comments

@Aweptimum
Copy link
Contributor

Aweptimum commented Dec 19, 2022

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.

@markrogoyski
Copy link
Owner

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?

@Aweptimum
Copy link
Contributor Author

Yes, it captures it perfectly 👍

@Aweptimum
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants