-
Notifications
You must be signed in to change notification settings - Fork 17
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
Matrix support and operations #14
Comments
I think it makes sense to add basic support for this. Regarding NN, there's even godotengine/godot#30613 which attracted quite a bunch of 👍, so this would facilitate NN development indeed. Note that the methods should follow
This could be
Taking into account above,
This can be implemented by overriding
I'd rename this to something which doesn't imply corresponding
Should have corresponding
✔️
As someone not particularly versed in linear algebra, I find this to be not descriptive, maybe rename to
✔️ Regarding the usage of I mean, internally, it would be best if the matrix data is represented in a single What about changing the elements of the existing matrix? Talking about Also, for a class like this, I'd also expect some unit tests to be written. 😛 |
Matrix is nothing else than a array of arrays:
|1x2 in my class I'm using here's the representation in cpp header:
|
I don't know how exactly matrix data is usually implemented, but representing a matrix as a vector of vector of doubles seems to be wasteful. I've just looked up some existing C libraries which perform matrix operations, for instance see https://github.com/nhomble/yasML/blob/master/yasML.h. In fact the API could be taken as a reference for implementing similar functionality in Godot to work natively with Godot data types. In there, the matrix is represented as a structure with a pointer to I guess performance is the number one reason why this feature proposal is created, so I'm suggesting implementing this in a way which could be faster. 🙂 |
Hmm I took a look at this yasML, seems to be way better implemented, I see no reasons to just port it to godot types |
Yes, the way I see it, you'd need to expose Then provide public API which can happily accept data in |
Matrices doesnt need to grow dinamically, in ML at least, once defined you just create another with calculations: Matrix A,B the sum will result in a Matrix C, same for any operation like transpose! |
Yeah, the ability to grow a matrix dynamically could be added later, if needed. |
I agree, but in another class like DynamicMatrix, cuz dynamic requires more processing and memalocations |
I've implemented The base implementation behind #45 is an example of how Likewise, if you look at Alternatively, this could be implemented as row-column as seen in |
well, the main target for matrix support is machine learning, which relies on performance! |
It is great to have a matrix data structure. In terms of machine learning type of usage, one should also add the main linear algebra methods besides matrix multiplication, in particular
I do not think it makes sense to implement this functionality from scratch, rather use an existing package that uses well-tested and documented algorithms, can leverage modern cpu features (like AVX) and such. It would also be nice to design API in a way that the backend library can be swapped out, e.g. if someone needs float32 tensors (a common data type on gpu-s). |
Geometry component in Goost has ability to define various backends for polygon clipping/decomposition: https://github.com/goostengine/goost/blob/b6361fc9eff6047463bd030e5e63ad30ae90ee23/core/math/geometry/2d/poly/poly_backends.h, so similar approach could be taken I guess. As long as those backends are implemented cleanly to hide implementation details, it's ok, but it would likely make sense to start simple by integrating a single library without introducing backends system. Geometry component has various backends just because of known limitations that don't always work depending on concrete use cases (like inability to handle degenerate input). Matrix operations/linear algebra could also have similar limitations, but I guess it would be more about performance and level of computational robustness there. |
the other thread on this was closed so cross-posting here on the open one
|
Frankly I still haven't needed support for matrix operations myself, but at some point I realized that I may need to solve a system of equations. For instance, I need to calculate trajectories and/or time of impact of a projectile (for AI). There's even a proposal for it at Godot: godotengine/godot-proposals#740. I realize that physics could be simulated, but if we're talking about solving for:
This likely asks for solving (non)-linear equations via matrices to do this efficiently in real-time. I've actually managed to solve a quadratic equation for this kind of thing in GDScript, but fail to solve this when other forces affect a projectile (like wind). Some months ago I delved into some physics stuff, and even ported Box2D Lite to GDScript for learning purposes: https://github.com/Xrayez/box2d-lite-gdscript. Physics have various constraints (such as interpenetration and joints) that must be solved. It turns out that most physics engines don't find an exact solution (using matrices) as it would greatly degrade performance with many physics bodies, so iterative methods are predominantly used there. However, for other use cases that don't require heavy real-time calculations with n number of entities, finding an exact solution would be ideal for a use case of finding trajectories etc. If someone has any ideas, what exactly is needed to know in order to solve a problem I described above with matrices, that would be great, and perhaps this could speed up the process of implementing matrix operations in Goost, as I'm mostly goal-oriented. 🙂 |
Graph data structure could return adjacency matrix representation. See godotengine/godot-proposals#3848. |
I'll take a look. not sure if that's what I need though. honestly for now I've decided to pivot to a different game idea that I can whip up w/o need for deep maths the engine can't handle right now without some c++ work to either implement what I already wrote in GDScript or to hook into another library. I do want to revisit this at some point but right now I'm focusing everything on this 2d sprite game I have in mind I think is going to be rad... |
No worries, I'm just cross-referencing issues/proposals, which are only part of the solution, or just connected to the topic somehow. In the case of graphs, it means that if we get to implement graph data structure as seen in #172, then it may ask for a method to return an adjacency matrix (with weights and whatnot), so that would be the initial reason/need to implement the |
This is the feature I want the most and it surprises me how long it has been ignored. |
@Jorvan758 I no longer maintain the project. If you imply that this kind of feature is ignored by Godot, that's also unfortunate... For these reasons and more, I have abandoned Godot altogether, and everything related to it. I have invited interested contributors to lead the Goost project, but they haven't done a lot. In any case, maintaining a semi-fork of Godot is quite demanding, I'd say, even when in reality, Goost is a module for Godot. See #200 for unfinished port of Goost to match up with Godot 4, if curious. |
** Based on any issue? **
godotengine/godot#26793
** Solution **
Implement basic matrix operations, these are building blocks for NNs (Neural Networks) and other stuff
** API Description **
The Matrix will be a class containing the following methods:
ArrayToMatrix(arr:Array) -> void
- Convert a input array to a 1 column matrix e.g: [1,2,3] will result in a matrix: [[1],[2],[3]]MatrixToArray() -> Array
- The inverse operation of the above methodPrettyPrint() -> void
- Prints a Matrix the beatiful waySetRandomData(int rows, int columns) -> void
- Generate a random value with rows and columnsSetData(arr:Array) -> void
- Set the matrix data e.g: [[1,2,3],[4,5,6]] a Matrix 2x3Transpose() -> void
- Rows becomes columns and vice-versa e.g: [[1,2,3],[4,5,6]] will become [[1,4],[2,5],[3,6]]Multiply(by:Matrix) -> Matrix
- Returns Matrices multiplication resultMultiplyScalar(value:float) -> Matrix
- Multiply a entire matrix with a single valueHadamard(by:Matrix) -> Matrix
- Different from matrices multiplication each value from Matrix A is multiplied with each value from BAdd(by:Matrix) -> Matrix
- Returns a Matrices Addition operation resultSubtract(by:Matrix) -> Matrix
- Returns a Matrices Subtraction operation resultThe text was updated successfully, but these errors were encountered: