Zend Framework: Zend_Math Component Proposal
| Proposed Component Name | Zend_Math |
|---|---|
| Developer Notes | http://framework.zend.com/wiki/display/ZFDEV/Zend_Math |
| Proposers | Walter Tamboer |
| Zend Liaison | Dolf Schimmel (Freeaqingme) |
| Revision | 2.0 - 02 Jun 2011: Finished the proposal code so that it can be reviewed. 1.0 - 29 May 2011: Initial Draft. (wiki revision: 6) |
Table of Contents
1. Overview
Zend_Math is a component which contains math related functions and classes. The purpose of this component is to make life easier for people who are doing vector and matrix math for example.
2. References
- http://en.wikipedia.org/wiki/Mathematics
- http://en.wikipedia.org/wiki/Euclidean_vector
- http://en.wikipedia.org/wiki/Matrix_(mathematics)
- http://www.math.com
3. Component Requirements, Constraints, and Acceptance Criteria
- This component will support helper methods such as "clamp" and "lerp".
- This component will support vector classes to do vector math.
- This component will support matrix classes to do matrix math.
- This component will support functionality to detect intersections.
4. Dependencies on Other Framework Components
- Zend_Exception
- SPL Iterator
- SPL Exceptions
5. Theory of Operation
This component can be used as is. It can be used to do calculations.
6. Milestones / Tasks
- Milestone 1: Setup the proposal and consider feedback.
- Milestone 2: Write and finish component source code.
- Milestone 3: Write unit tests
- Milestone 4: Write documentation
7. Class Index
- Zend_Math
- Zend_Math_BoundingBox
- Zend_Math_BoundingFrustum
- Zend_Math_BoundingSphere
- Zend_Math_BoundingVolume
- Zend_Math_Exception
- Zend_Math_Exception_DivideByZero
- Zend_Math_Interface
- Zend_Math_Matrix
- Zend_Math_Matrix_Matrix44
- Zend_Math_Plane
- Zend_Math_Point
- Zend_Math_Quaternion
- Zend_Math_Ray
- Zend_Math_Rectangle
- Zend_Math_Size
- Zend_Math_Vector
- Zend_Math_Vector_Vector2
- Zend_Math_Vector_Vector3
- Zend_Math_Vector_Vector4
8. Use Cases
| UC-01 |
|---|
| UC-02 |
|---|
9. Class Skeletons
The code for this proposal can be found at http://code.google.com/p/zend-math/source/browse/
5 Comments
comments.show.hideJun 02, 2011
Benoît Durand
You should write your code in PHP5.3 style for ZF2 (namespace, refactor exceptions, ...).
I think it would be more readable if you use tabs for skeletons.
Jun 02, 2011
Denis Portnov
I very much like the Zend_Math idea, but I agree that it should be coded against ZF2 PHP 5.3
as far as I understand ZF1 is in feature freeze state
few other points:
not every calculation needs PI with 50 digits precision
so I guess it would be nice to have Zend\Math::setPrecision() to set precisions for all calculations performed by Zend\Math
Math is a very broad namespace, so mabe it's good idea to separate linear algebra from geometry namespace
and mabe even separate geometry into 2d & 3d namespaces
so I prefer something like:
over
Jun 04, 2011
Walter Tamboer
Hi Benoît and Denis,
Thank you very much for your feedback. I have moved the code to Google Code since that makes reviewing easier. I just put everything in the Zend\Math namespace. I'm pretty new to this new style so if I missed anything while converting the code, just let me know.
Denis, I'd like to respond to your feedback:
1. Is there a performance issue when using this amount of digits behind the comma? If not I do not see a problem to have it like this since I can't imagine anyone to like loosing performance during calculations. If one does not want this amount of digits one can use the appropriate PHP function to show the preferred amount of digits. But again, if there is a performance issue it should be changed.
2. I agree with you about the naming of the namespaces. Math is indeed very broad but I am not creative enough to come with good names. Any suggestion is welcome.
3. Vectors and matrices are very dynamic. That is the reason that I made those two classes. The Vector2, Vector3, Vector4 and Matrix44 classes are there since they are used very often during game development for example. You could call them "helpers" I think. I do see the point you are making though and I am not sure how to solve it. I would like to hear more ideas about that.
The example you made is not entirely correct. Once one uses class methods, it should be clear that they change something on the instance of the class. The add, sub, mul and div methods are doing that.
My opinion is that static methods should be used as factory methods where new instances are created. The cross product method should be a static method I think. I do not like the idea to create a Utils class. That might complicate code writing...
Thanks again for your feedback guys!
Jun 05, 2011
Denis Portnov
Hi Walter
there is some performance hit with higher pricision numbers,
it's insignificant though, so i guess predefined precision values are ok
take a look at ZF2 coding standards Interfaces section http://framework.zend.com/wiki/display/ZFDEV2/ZF+2.0+Coding+Standards+Addendums
so basically according to ZF2 standards there should be an interface to define math entities, or math operations
but not the whole math namespace
so with your approach it's something like
there are severall approaches
1) $a->mul($b); multiplication of $a & $b changes $a
2) $c = $a->mul($b); multiplication of $a & $b creates new object $c, leaving $a intact
3) $op = new Zend\Math\Matrix\Op(); $c = $op->mul($a, $b); separate class to manipulate objects, leaves $a intact
what I was saying is my personal preference is to separate manipulation class
the problem I see with your approach, is let's say multiplication of matrices with difeerent dimentions
A[m x n] * B[n x k] = C[m x k] wich is ok since number of cols in A equals numbers of rows in B
with your approach, after $a->mul($b) object $a was originally [m x n] becomes [m x k]
also with separate class you can have a correct interface definition, let's say
but again, it's just my personal preference
I've checked some Java and C++ classes and there is no general way
anyway, bottom line is
since Math is a broad namespace, and there is nothing remotely close in current ZF
I think you need more community input
I personnaly interested in geometry stuff, point, polyline, rectanle, plane etc
so let me know if you need any help with that
Jun 19, 2011
Michiel Staessen
I Think that's about it