Skip to end of metadata
Go to start of metadata

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

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/

Labels:
math math Delete
vector vector Delete
matrix matrix Delete
algebra algebra Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jun 02, 2011

    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.

  2. Jun 02, 2011

    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:

    • I don't like the idea of forcing user to use precision defined in class const
      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
    • not sure about naming and namespaces at all
      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
    • personally don't like that Matrix, Vector etc objects are both data containers and utility classes

    so I prefer something like:

    over

  3. Jun 04, 2011

    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!

  4. Jun 05, 2011

    Hi Walter

    • on precision, I did some benchmarks
      there is some performance hit with higher pricision numbers,
      it's insignificant though, so i guess predefined precision values are ok

    so with your approach it's something like

    • on Matrix/Vector operations,
      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

  5. Jun 19, 2011

    1. I can't really see any advantage to the VectorX and MatrixXY classes. They make me think of "bad design". Instead, I'd rather have some kind of Factory to create such objects. Other options include identity matrices, random matrices, ...
    2. A Vector is basically a Matrix with only one Column. Why implement it twice? I would rather extend Matrix for that purpose.
    3. I don't know why a Matrix must be identity on creation. I'd rather create a zero matrix and provide a static method to create an identity matrix. This also refers to the factory mentioned earlier.
    4. The dimension checks for calculations are way to weak. You simply cannot subtract a 2x2 matrix from a 3x3 matrix. The dimensions must match. Also, a 1x1 matrix is a scalar: you should cover that.
    5. I am missing some key functionality like inverse matrices (although that's a numerical tough one), row reducing methods, rank, ... also more advanced stuff as eigenvalues and eigenvectors, ... (you never knwo who is going to use it, but I'd put these at the bottom of the todo-list )
    6. The operators are all in one interface. Might be useful, but don't know if it is the way to go (Like Dennis suggests). I'd rather make more interfaces for operations. I think Anonymous functions are perfect for this (There is a webinar from Matthew Weier O'Phinney on this subject: http://www.zend.com/en/webinar/PHP/70170000000bYYX-webinar-anonymous-functions-in-php-53-20110426.flv). Also, make sure all operations are consistent (either they all change the arguments or either all return new objects. Personally, I prefer the last one (I hate Zend_Date for that reason).
    7. I would also refactor methods like getRows() and getColumns() to getNbRobs() and getNbColumns() because I expect getColumns() to give me the Columns as Vectors and getRows() the Rows as vectors. That also something I'm missing: the functionality to extract a row or column (with or without modifying the original object (extract/extract&remove)). It might be useful.
    8. Math is indeed a very broad namespace. A LinearAlgebra namespace might be a good idea.

    I Think that's about it