Simple Matrix Mathematics Library for PHP (Matrix Det)


I wrote these PHP functions to compute matrix determinant for 2×2 and 3×3 matrices long time ago and I share these three simple functions here just in case someone needs it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
  /*
    Matrix Mathematics Library for PHP 
    https://helloacm.com
  */
 
  function det2x2($a, $b, $c, $d)
  {
    return ($a * $d - $b * $c);
  }
  
  /*
   * double = det3x3(  a1, a2, a3, b1, b2, b3, c1, c2, c3 )
   * 
   * calculate the determinant of a 3x3 matrix
   * in the form
   *
   *     | a1,  b1,  c1 |
   *     | a2,  b2,  c2 |
   *     | a3,  b3,  c3 |
  */
 
  function det3x3($a1, $a2, $a3, $b1, $b2, $b3, $c1, $c2, $c3)
  {
    return ($a1 * det2x2( $b2, $b3, $c2, $c3 )
            - $b1 * det2x2( $a2, $a3, $c2, $c3 )
            + $c1 * det2x2( $a2, $a3, $b2, $b3 ));
  }
    
  /*
    A=| 1 y1 z1 |
      | 1 y2 z2 |
      | 1 y3 z3 |
    
    B=| x1 1 z1 |
      | x2 1 z2 |
      | x3 1 z3 |
    
    C=| x1 y1 1 |
      | x2 y2 1 |
      | x3 y3 1 |
    
   -D=| x1 y1 z1 |
      | x2 y2 z2 |
      | x3 y3 z3 |
  
  */
  function ABCD(&$A, &$B, &$C, &$D, $x1, $y1, $z1, $x2, $y2, $z2, $x3, $y3, $z3)
  {
    $A =  $y1 * ($z2 - $z3) + $y2 * ($z3 - $z1) + $y3 * ($z1 - $z2);
    $B =  $z1 * ($x2 - $x3) + $z2 * ($x3 - $x1) + $z3 * ($x1 - $x2);
    $C =  $x1 * ($y2 - $y3) + $x2 * ($y3 - $y1) + $x3 * ($y1 - $y2);
    $D = -$x1 * ($y2 * $z3  - $y3 *  $z2)- $x2 * ($y3 * $z1 - $y1 * $z3) - $x3 * ($y1 * $z2 - $y2 * $z1);  
  }
<?php
  /*
    Matrix Mathematics Library for PHP 
    https://helloacm.com
  */

  function det2x2($a, $b, $c, $d)
  {
    return ($a * $d - $b * $c);
  }
  
  /*
   * double = det3x3(  a1, a2, a3, b1, b2, b3, c1, c2, c3 )
   * 
   * calculate the determinant of a 3x3 matrix
   * in the form
   *
   *     | a1,  b1,  c1 |
   *     | a2,  b2,  c2 |
   *     | a3,  b3,  c3 |
  */

  function det3x3($a1, $a2, $a3, $b1, $b2, $b3, $c1, $c2, $c3)
  {
    return ($a1 * det2x2( $b2, $b3, $c2, $c3 )
            - $b1 * det2x2( $a2, $a3, $c2, $c3 )
            + $c1 * det2x2( $a2, $a3, $b2, $b3 ));
  }
    
  /*
    A=| 1 y1 z1 |
      | 1 y2 z2 |
      | 1 y3 z3 |
    
    B=| x1 1 z1 |
      | x2 1 z2 |
      | x3 1 z3 |
    
    C=| x1 y1 1 |
      | x2 y2 1 |
      | x3 y3 1 |
    
   -D=| x1 y1 z1 |
      | x2 y2 z2 |
      | x3 y3 z3 |
  
  */
  function ABCD(&$A, &$B, &$C, &$D, $x1, $y1, $z1, $x2, $y2, $z2, $x3, $y3, $z3)
  {
    $A =  $y1 * ($z2 - $z3) + $y2 * ($z3 - $z1) + $y3 * ($z1 - $z2);
    $B =  $z1 * ($x2 - $x3) + $z2 * ($x3 - $x1) + $z3 * ($x1 - $x2);
    $C =  $x1 * ($y2 - $y3) + $x2 * ($y3 - $y1) + $x3 * ($y1 - $y2);
    $D = -$x1 * ($y2 * $z3  - $y3 *  $z2)- $x2 * ($y3 * $z1 - $y1 * $z3) - $x3 * ($y1 * $z2 - $y2 * $z1);  
  }

These three tiny functions are straightforward as it looks like. You can easily translate them to other programming languages without any difficulties. What each function does is described in the preceding comment.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
302 words
Last Post: How to Show Tweets Archive in WordPress using PHP and MySQL with Crontab Support?
Next Post: Simple PHP Vector (3D) class

The Permanent URL is: Simple Matrix Mathematics Library for PHP (Matrix Det)

Leave a Reply