Comments (7)
I was also thinking about Square and Diagonal could also be subclasses of Matrix. Certain matrix operations only work on square matrixes, or diagonals, etc. Also, matrix decompositions are a kind of special case of a Matrix.
from math-php.
There are a bunch of cases where triangle matrices are expected as well.
Have you given any thought to tensors? Rank 2 tensors are represented by square matrices. Similarly, rank 1 tensors are Vectors, which are represented by an array.
from math-php.
I was thinking this over as I was in bed last night. I would need a really good argument for what advantages there would be to having a special square matrix class.
I can imagine a diagonal matrix having its own constructor, wherein it accepts an array. Maybe there would be some memory management savings due to storing only the array of diagonals instead of all the zeros as well. Though, once this diagonal matrix is used in some operations , it will have to be converted into a full-fledged Matrix. Can a Matrix parent class somehow self convert to a Diagonal if a test for isDiagonal() == TRUE? The calculation for the matrix determinant is certainly easier for a diagonal matrix.
A Jacobian on the other hand, is a completely different type of matrix, it's a matrix of functions, which can be evaluated with different parameters. For example, you can do something like:
$J_functions = [
[function ($x,$y){return $y * $x ** 2;}],
[function ($x,$y){return 5 * $x + sin($y);}],
]
// Tell the constructor that there are two parameters in the functions.
$J = new Jacobian($J_functions, 2);
$J->evaluate(1,2);
// The evaluate method would use numerical differentiation to produce the following and evaluate at x=1 and y=2
//[
// [2*$x*$y, $x **2],
// [5, cos($y)],
//];
//
//OR
//[
// [ 4, 1],
// [5, -.41615],
// ]
$J->determinent(4,5); // equals 2*x*y*cos(y) - 5*x²
from math-php.
The benefit of a Square matrix class I was thinking of in terms of type hinting. For example, if a method only worked on a Square matrix, it could type hint instead of doing a manual check:
public static someMethod(Square $matrix)
However, the methods that would only work on a Square matrix, like trace and determinant, probably aren't going to take any parameters, so the benefit wouldn't be realized.
Anyways, if we were to do something like this, I'd imagine a factory method that would create the Matrices, instead of using the individual constructors. For example:
$matrix = MatrixFactory::createMatrix($array);
Then, depending on the values passed in, the factory method would return a Matrix of the subtype that best fits the data. Maybe this is all over engineering and not really necessary at this time. Just an idea. For now I think we are OK to continue with just the base Matrix class.
The Jacobian matrix sounds like a good case for a new class, since it will have different private attribute variables than a regular Matrix.
Btw, I'm not so familiar with tensors, so if you have an idea of how to implement those, let me know, or go ahead and do a pull request. Don't worry if it isn't perfect first try. Code is made to be refactored in a continuous improvement process. Any ideas on the base Matrix class as well, or if you want to go ahead and implement something because I'm taking too long, go ahead.
And always, thanks for the feedback.
from math-php.
If the MIT license allow, I'd say grab the determinant and inverse code from that other project I linked to and just stick it in this library. That's the only thing I'm waiting for.
from math-php.
I'm planning on extending Matrix by two more classes. First will be a FunctionMatrix, which will be a matrix of functions. Them I'll extend this to the Jacobian Matrix, which is a special type of function matrix, where each element is a partial derivative of a function. Any preference on how parameters should be passed to the functions?
I imagine the most generic and flexible way would be something like:
$A = [
[function (array $params){
$y = $params['y'];
$x = $params['x'];
return $y * $x ** 2;
}],
[function (array $params){
$y = $params['y'];
$x = $params['x'];
$z = $params['z'];
return 5 * $x + sin($y) ** $z;
}],
]
$M = new FunctionMatrix($A);
$params = [
['x' => 1, 'y' => 2,],
['x' => 3, 'y' => 4, 'z'=> 1,],
];
$E = $M->functionEvaluate($params); // $E = [ [2], [15.0698] ]
Let me know if you see any initial problems.
from math-php.
I think being explicit with the parameters by using an associative array is probably a good idea. Go ahead and implement it and we can refactor as necessary.
Btw, if you use any of the Matrix private attributes, you will have to update the Matrix class and change them to protected so the inheriting class can access them.
from math-php.
Related Issues (20)
- What is the point of the complex polar form? HOT 7
- Division by zero in Probability\Distribution\Continuous\Continuous HOT 21
- Dealing with NANs HOT 3
- Integer Modulo n HOT 3
- Add vector, matrix and quaternion operations for 3d geometry HOT 1
- Could math-php support KPHP? HOT 1
- Truncated Mean Percentage HOT 5
- Incompatibility with Iterator and ArrayAccess interfaces HOT 1
- Quartiles calculation in Stats Descriptive HOT 1
- MiddlePayment For Loan HOT 2
- `number` is not a valid PHP type HOT 5
- Kernel Density Estimation: Improved Sheather-Jones algorithm HOT 4
- Bug for the t-test? HOT 5
- Add row/column algebra methods to Matrix for Vector HOT 3
- Document additional Matrix Row Operations and add their Columnar counterparts HOT 5
- Implicit Behavior of ImmutableSet
- Add Moore-Penrose Inverse HOT 12
- Psalm does not understand `number` pseudo-type HOT 3
- Add a factor method for Polynomials HOT 4
- Repeated measures ANOVA HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from math-php.