Calculate the row echelon form (REF) of the matrix. Return the matrix
and the number of swaps in a tuple.

@return array
194355
public function ref() : array
    {
        try {
            $ref = $this->gaussianElimination();
        } catch (RuntimeException $e) {
            $ref = $this->rowReductionMethod();
        }

        return $ref;
    }
66007
public function items() {
        if ( $this->_max <= 1 ) {
            return [];
        }
        $end = min( $this->_max, $this->_current + floor( $this->count / 2 ) );
        $start = max( 1, $end - $this->count + 1 );
        $items = [];
        if ( $start != 1 ) {
            $items[] = $this->item( $this->label_to_start, 1);
        }
        for ( $i = $start; $i <= $end; $i ++ ) {
            $items[] = $this->item( $i, $i, ($i == $this->_current) );
        }
        if ( $this->_current != $this->_max ) {
            $items[] = $this->item( $this->label_next, ($this->_current + 1) );
        }
        return $items;
    }
597720
public function rows()
	{
		$rows = parent::rows();

		// Now remove any associative fields
		foreach ($rows as $row)
		{
			$associatives = new \stdClass();

			foreach ($row->getAttributes() as $k => $v)
			{
				if (strpos($k, 'associative_') === 0)
				{
					$key = substr($k, 12);
					$associatives->$key = $v;
					$row->removeAttribute($k);
				}
			}

			if (!empty($associatives))
			{
				$row->associated = $associatives;
			}
		}

		return $rows;
	}
1060089
public function result() : Matrix
    {
        if (!$this->result) {
            $this->result = call_user_func($this->computation);
        }

        return $this->result;
    }
244887
public function toArray() {
        $out = [];
        for ($i=static::ROWS;$i-->0;) {
            $out[$i] = $this[$i]->toArray();
        }
        return $out;
    }
882088
public function getR()
    {
        for ($i = 0; $i < $this->n; ++$i) {
            for ($j = 0; $j < $this->n; ++$j) {
                if ($i < $j) {
                    $R[$i][$j] = $this->QR[$i][$j];
                } elseif ($i == $j) {
                    $R[$i][$j] = $this->Rdiag[$i];
                } else {
                    $R[$i][$j] = 0.0;
                }
            }
        }
        return new PHPExcel_Shared_JAMA_Matrix($R);
    }
100871
public function getU()
    {
        $U = [];
        for ($i = 0; $i < $this->n; ++$i) {
            for ($j = 0; $j < $this->n; ++$j) {
                if ($i <= $j) {
                    $U[$i][$j] = $this->LU[$i][$j];
                } else {
                    $U[$i][$j] = 0.0;
                }
            }
        }
        return new Matrix($U);
    }
166455
public function getSwapTotal()
    {
        if (count($this->_swapDevices) > 0) {
            $total = 0;
            foreach ($this->_swapDevices as $dev) {
                $total += $dev->getTotal();
            }

            return $total;
        } else {
            return null;
        }
    }
1204241
public static function getSubMatrix($matrix, $skipRow = 0, $skipColumn = 0) : array {
        $size     = count($matrix);
        $newSize  = $size-1;
        $cfRow    = 0;
        $cfColumn = 0;
        $cfMatrix = [];

        for ($row=0;$row<$size;$row++) {
            for ($col=0;$col<$size;$col++) {

                if ($row !== $skipRow && $col !== $skipColumn) {
                    if (!isset($cfMatrix[$cfRow])) {
                        $cfMatrix[$cfRow] = [];
                    }
                    $cfMatrix[$cfRow][$cfColumn++] = $matrix[$row][$col];
                    if ($cfColumn === ($newSize)) {
                        $cfColumn = 0;
                        $cfRow++;
                    }
                }
            }
        }
        return $cfMatrix;
    }
1194756
public function rref() : self
    {
        [$b, $swaps] = $this->ref();

        $b = $b->asArray();

        $row = $col = 0;

        while ($row < $this->m and $col < $this->n) {
            $t = $b[$row];

            if (abs($t[$col]) == 0) {
                $col++;

                continue 1;
            }

            $divisor = $t[$col];

            if ($divisor != 1) {
                for ($i = 0; $i < $this->n; $i++) {
                    $t[$i] /= $divisor;
                }
            }

            for ($i = $row - 1; $i >= 0; $i--) {
                $scale = $b[$i][$col];

                if ($scale != 0) {
                    for ($j = 0; $j < $this->n; $j++) {
                        $b[$i][$j] += -$scale * $t[$j];
                    }
                }
            }

            $b[$row] = $t;

            $row++;
            $col++;
        }

        return self::quick($b);
    }
735771
private static function _calculateChanges()
    {
        $current = self::$_currentEnvironmentData;
        $newData = self::$_runtimeClient->getRoleEnvironmentData();

        $changes = self::_calculateConfigurationChanges($current, $newData);

        $currentRoles = $current->getRoles();
        $newRoles = $newData->getRoles();
        $changedRoleSet = [];

        foreach ($currentRoles as $roleName => $role) {
            if (array_key_exists($roleName, $newRoles)) {
                $currentRole = $currentRoles[$roleName];
                $newRole = $newRoles[$roleName];

                $currentRoleInstances = $currentRole->getInstances();
                $newRoleInstances = $newRole->getInstances();

                $changedRoleSet = array_merge(
                    $changedRoleSet,
                    self::_calculateNewRoleInstanceChanges(
                        $role,
                        $currentRoleInstances,
                        $newRoleInstances
                    )
                );
            } else {
                $changedRoleSet[] = $role;
            }
        }

        foreach ($newRoles as $roleName => $role) {
            if (array_key_exists($roleName, $currentRoles)) {
                $currentRole = $currentRoles[$roleName];
                $newRole = $newRoles[$roleName];

                $currentRoleInstances = $currentRole->getInstances();
                $newRoleInstances = $newRole->getInstances();

                $changedRoleSet = array_merge(
                    $changedRoleSet,
                    self::_calculateCurrentRoleInstanceChanges(
                        $role,
                        $currentRoleInstances,
                        $newRoleInstances
                    )
                );
            } else {
                $changedRoleSet[] = $role;
            }
        }

        foreach ($changedRoleSet as $role) {
            $changes[] = new RoleEnvironmentTopologyChange($role);
        }

        return $changes;
    }
291508
public function determinant()
    {
        if (!$this->isSquare()) {
            throw new RuntimeException('Determinant is undefined'
                . ' for a non square matrix.');
        }

        [$b, $swaps] = $this->ref();

        $pi = $b->diagonalAsVector()->product();

        return $pi * (-1.) ** $swaps;
    }
944532
public double[][] buildBondMatrix() {
        int i, j, k, m;
        for (i = 0; i < nodeNumber; i++) {
            for (j = 1; j <= layerNumber; j++) {
                bondMatrix[i][j - 1] = 0.0;
                for (k = 0; k < nodeNumber; k++) {
                    if (j == 1) {
                        if (apspMatrix[i][k] == j) {
                            bondMatrix[i][j - 1] += adjaMatrix[i][k];
                        }
                    } else {
                        if (apspMatrix[i][k] == j) {
                            for (m = 0; m < nodeNumber; m++) {
                                if (apspMatrix[i][m] == (j - 1)) {
                                    bondMatrix[i][j - 1] += adjaMatrix[k][m];
                                }
                            }
                        }
                    }
                }
            }
        }
        return bondMatrix;
    }
156366
private function obtainNumRows()
    {
        $count = 0;
        if (false !== $this->internalFetch(SQLITE3_NUM)) {
            $this->getFields();
            $count = 1;
        }
        while (false !== $this->internalFetch(SQLITE3_NUM)) {
            $count = $count + 1;
        }
        $this->internalReset();
        return $count;
    }
1241585
def reduced_row_echelon_form(ary)
      lead = 0
      rows = ary.size
      cols = ary[0].size
      rary = convert_to_rational(ary)  # use rational arithmetic
      catch :done  do
        rows.times do |r|
        throw :done  if cols <= lead
          i = r
          while rary[i][lead] == 0
            i += 1
            if rows == i
              i = r
              lead += 1
              throw :done  if cols == lead
            end
          end
          # swap rows i and r 
          rary[i], rary[r] = rary[r], rary[i]
          # normalize row r
          v = rary[r][lead]
          rary[r].collect! {|x| x /= v}
          # reduce other rows
          rows.times do |i|
            next if i == r
            v = rary[i][lead]
            rary[i].each_index {|j| rary[i][j] -= v * rary[r][j]}
          end
          lead += 1
        end
      end
      rary
    end
1174215
public function numRows() {
        if ($this->numRows === null) {
            // no support for num_rows() in SQLite3, need to count manually
            $previous = $this->nextRowIndex;

            while ($this->fetchRow());                          // loop from current position to the end

            // we hit the end
            if ($this->numRows) {
                $this->result->reset();                         // back to start
                $this->nextRowIndex = 0;
                while ($previous--) {                           // loop back to former position
                    $this->fetchRow();
                }
            }
        }
        return $this->numRows;
    }
854386
private int[] rows() {
        int[] rowIndexes = new int[rowCount()];
        for (int i = 0; i < rowCount(); i++) {
            rowIndexes[i] = i;
        }
        return rowIndexes;
    }