Example #1
0
 public static function ex3()
 {
     header('Content-Type: application/json');
     // Load files and parse them
     $a = new SparseMatrix();
     $b = new SparseMatrix();
     $a->parseFile(self::A);
     $b->parseFile(self::B);
     $x = array();
     $n = $a->Count();
     for ($i = 0; $i <= $n; $i++) {
         $x[$i - 1] = $i;
     }
     $res_aorix = self::multiplyMatrixWithVector($a->Matrix(), $x);
     print_r($res_aorix);
     //TODO: response json
     echo json_encode(array("axb" => json_encode($res_aorix)));
 }
Example #2
0
 /**
  * Computes the beta vector from the given examples and labels. The
  * examples are represented as a sparse matrix where each row is an example
  * and each column a feature, and the labels as an array where each value
  * is either 1 or -1, corresponding to a positive or negative example. Note
  * that the first feature (column 0) corresponds to an intercept term, and
  * is equal to 1 for every example.
  *
  * @param object $X SparseMatrix of training examples
  * @param array $y example labels
  */
 function train(SparseMatrix $X, $y)
 {
     $n = $X->columns();
     $p = array_fill(0, $n, 0);
     $a = array_fill(0, $n, 0);
     $this->beta = array_fill(0, $n, 0.0);
     $beta =& $this->beta;
     foreach ($X as $i => $row) {
         foreach ($row as $j => $Xij) {
             if ($y[$i] == 1) {
                 $p[$j] += 1;
             } else {
                 $a[$j] += 1;
             }
         }
     }
     $beta[0] = $this->logit($p[0], $a[0]);
     for ($j = 1; $j < $n; $j++) {
         $beta[$j] = $this->logit($p[$j], $a[$j]) - $beta[0];
     }
 }
Example #3
0
 /**
  * Converts a SparseMatrix into an InvertedData instance. The data is
  * duplicated.
  *
  * @param object $X SparseMatrix instance to convert
  */
 function __construct(SparseMatrix $X)
 {
     $this->rows = $X->rows();
     $this->columns = $X->columns();
     $this->data = array();
     $this->index = array();
     foreach ($X as $i => $row) {
         foreach ($row as $j => $Xij) {
             $this->data[] = array($j, $i, $Xij);
         }
     }
     sort($this->data);
     $lastVar = -1;
     foreach ($this->data as $dataOffset => $x) {
         $currVar = $x[0];
         if ($currVar != $lastVar) {
             for ($var = $lastVar + 1; $var <= $currVar; $var++) {
                 $this->index[$var] = $dataOffset;
             }
             $lastVar = $currVar;
         }
     }
 }
Example #4
0
 /**
  * Given two sets of row indices, returns two new sparse matrices
  * consisting of the corresponding rows.
  *
  * @param array $a_indices row indices for first new sparse matrix
  * @param array $b_indices row indices for second new sparse matrix
  * @return array array with two entries corresponding to the first and
  * second new matrices
  */
 function partition($a_indices, $b_indices)
 {
     $a = new SparseMatrix(count($a_indices), $this->n);
     $b = new SparseMatrix(count($b_indices), $this->n);
     $new_i = 0;
     foreach ($a_indices as $i) {
         $a->setRow($new_i++, $this->data[$i]);
     }
     $new_i = 0;
     foreach ($b_indices as $i) {
         $b->setRow($new_i++, $this->data[$i]);
     }
     return array($a, $b);
 }
Example #5
0
 /**
  * ex1 method
  * @param void
  * @return void
  */
 public static function ex1()
 {
     header('Content-Type: application/json');
     // Load files and parse them
     define("TEST", 0);
     $p = 7;
     $kmax = 10000;
     $epsilon = pow(10, -$p);
     $epsilon2 = pow(10, 8);
     $a1 = new SparseMatrix();
     $a2 = new SparseMatrix();
     $a3 = new SparseMatrix();
     $a4 = new SparseMatrix();
     $m = array();
     $b = array();
     $n = array();
     $diagonal = array();
     $L = array();
     $U = array();
     $B = array();
     $C = array();
     $a1->parseFile(self::RareM1);
     if (!TEST) {
         $a2->parseFile(self::RareM2);
         $a3->parseFile(self::RareM3);
         $a4->parseFile(self::RareM4);
     }
     // Initialization
     $m[0] = $a1->Matrix();
     if (!TEST) {
         $m[1] = $a2->Matrix();
         $m[2] = $a3->Matrix();
         $m[3] = $a4->Matrix();
     }
     $b[0] = $a1->Vector();
     if (!TEST) {
         $b[1] = $a2->Vector();
         $b[2] = $a3->Vector();
         $b[3] = $a4->Vector();
     }
     $n[0] = count($a1);
     if (!TEST) {
         $n[1] = count($a2);
         $n[2] = count($a3);
         $n[3] = count($a4);
     }
     $xc = array();
     $nlen = count($n);
     //Check elements on the diagonal and build the diagonal matrix at the same time
     for ($q = 0; $q < $nlen; $q++) {
         for ($i = 0; $i < $n[$q]; $i++) {
             $el = $m[$q][$i]->FindCol($i);
             if (!$el) {
                 echo "Error, diagonal element null on the matrix " . $i;
                 die;
             } else {
                 $diagonal[$q][$i] = new SinglyList();
                 $diagonal[$q][$i]->Append($el->Value(), $i);
             }
         }
     }
     $output = "";
     // build upper and lower matrix
     for ($q = 0; $q < $nlen; $q++) {
         for ($i = 0; $i < $n[$q]; $i++) {
             $crwA = $m[$q][$i]->Tail();
             $U[$q][$i] = new SinglyList();
             $L[$q][$i] = new SinglyList();
             while ($crwA !== null) {
                 $j = $crwA->Column();
                 $output .= $j . '-' . $i . '|';
                 if ($j < $i) {
                     $U[$q][$i]->Append($crwA->Value(), $j);
                 } elseif ($j > $i) {
                     $L[$q][$i]->Append($crwA->Value(), $j);
                 }
                 $crwA = $crwA->Next();
             }
         }
     }
     $resp = array();
     // build b & c matrixes
     for ($q = 0; $q < $nlen; $q++) {
         $B[$q] = self::plusMatrix($L[$q], self::multiplyMatrixWithScalar($diagonal[$q], round(5 / 6, $p)));
         $C[$q] = self::substractMatrixes(self::multiplyMatrixWithScalar($diagonal[$q], -round(1 / 6, $p)), $U[$q]);
     }
     unset($L);
     unset($U);
     unset($d);
     for ($q = 0; $q < $nlen; $q++) {
         for ($i = 0; $i < $n[$q]; $i++) {
             if (TEST) {
                 $xc[$q][0][$i] = $i + 1;
             } else {
                 $xc[$q][0][$i] = 0;
             }
         }
         $k = 0;
         do {
             $xc[$q][$k + 1] = array();
             for ($i = 0; $i < $n[$q]; $i++) {
                 $sub = self::computeBSubstraction($m[$q], $i, $xc[$q][$k + 1], $xc[$q][$k]);
                 // cu plus primul x1 da ca in exemplu
                 if (TEST) {
                     $xc[$q][$k + 1][$i] = -0.2 * $xc[$q][$k][$i] + 1.2 * round(($b[$q][$i] + $sub) / $m[$q][$i]->FindCol($i)->Value(), $p);
                 } else {
                     $xc[$q][$k + 1][$i] = -0.2 * $xc[$q][$k][$i] + 1.2 * round(($b[$q][$i] - $sub) / $m[$q][$i]->FindCol($i)->Value(), $p);
                 }
             }
             if (TEST) {
                 print_r($xc);
             }
             $dx = self::getStandardNorm(self::subtractVectors($xc[$q][$k + 1], $xc[$q][$k], $n[$q], $p), $n[$q]);
             $k++;
         } while ($dx >= $epsilon && $k <= $kmax && $dx <= $epsilon2);
         if ($dx < $epsilon) {
             $resp[$q] = $xc[$q][$k - 1];
         } else {
             $resp[$q] = "divergenta";
         }
     }
     $norm = array();
     for ($q = 0; $q < $nlen; $q++) {
         @($norm[$q] = self::subtractVectors(self::multiplyMatrixWithVector($m[$q], $resp[$q]), $b[$q], $n[$q], $p));
     }
     echo json_encode(array("resp" => $resp, "norm" => $norm));
     die;
 }