Build a diagonal matrix with the value of each element along the
diagonal and 0s everywhere else.

@param array $elements
@throws \InvalidArgumentException
@return self
977301
public static function diagonal(array $elements) : self
    {
        $n = count($elements);

        if ($n < 1) {
            throw new InvalidArgumentException('Dimensionality must be'
                . ' greater than 0 on all axes.');
        }

        foreach ($elements as $element) {
            if (!is_int($element) and !is_float($element)) {
                throw new InvalidArgumentException('Diagonal element'
                    . ' must be an integer or float, '
                    . gettype($element) . ' found.');
            }
        }

        $a = [];

        for ($i = 0; $i < $n; $i++) {
            $temp = [];

            for ($j = 0; $j < $n; $j++) {
                $temp[] = $i === $j ? $elements[$i] : 0;
            }

            $a[] = $temp;
        }

        return self::quick($a);
    }
60985
public static function calculateDeterminant($matrix) : float {
        // The ´rows´ args is the only needed one. Determinants can only be calculated
        // from a square matrix. Hence we use the row as size for both row and column.
        $determinant = 0;
        $rcCount     = count($matrix);
        if ($rcCount !== count($matrix[0])) {
            throw new InvalidArgumentException(
                "Can only calculate determinant from square matrix (same amount of rows as columns)."
            );
        }

        // If only one element, return the value from it.
        if ($rcCount === 1) {
            return $matrix[0][0];
        }

        $sign = 1;
        // Go through each element in first row.
        for ($counter=0;$counter<$rcCount;$counter++) {
            // Create a cofactor sub-matrix ($rcCount-1 in size).
            $subMatrix = self::getSubMatrix($matrix, 0, $counter);
            // Calculate the determinant of the sub-matrix.
            $determinant += $sign * $matrix[0][$counter] * self::calculateDeterminant($subMatrix);
            $sign         = -$sign;
        }

        return $determinant;
    }
997960
public function multiply(Matrix $matrix)
    {
        if ($this->columns != $matrix->getRows()) {
            throw InvalidArgumentException::inconsistentMatrixSupplied();
        }

        $product = [];
        $multiplier = $matrix->toArray();
        for ($i = 0; $i < $this->rows; ++$i) {
            $columns = $matrix->getColumns();
            for ($j = 0; $j < $columns; ++$j) {
                $product[$i][$j] = 0;
                for ($k = 0; $k < $this->columns; ++$k) {
                    $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j];
                }
            }
        }

        return new self($product, false);
    }
746802
public function bind($values = [])
    {
        if (is_object($values)) {
            $values = get_object_vars($values);
        }

        if (!is_array($values)) {
            throw new \InvalidArgumentException('Please give me array or object');
        }

        foreach ($values as $key => $value) {
            $this->$key = $value;
        }

        return $this;
    }
1101509
public static function createIdentityMatrix($dimensions)
    {
        $grid = static::createFilledMatrix(null, $dimensions)->toArray();

        for ($x = 0; $x < $dimensions; ++$x) {
            $grid[$x][$x] = 1;
        }

        return new Matrix($grid);
    }
510079
public function addAll(array $elements) : Set
    {
        $this->elements = self::sanitize(array_merge($this->elements, $elements));

        return $this;
    }
274264
protected function indentArray(array $elements) {
		return self::INDENTATION . \str_replace(PHP_EOL, PHP_EOL . self::INDENTATION, \implode(PHP_EOL, $elements));
	}
19329
public static function gaussian(int $n) : self
    {
        if ($n < 1) {
            throw new InvalidArgumentException('The number of elements'
                . " must be greater than 0, $n given.");
        }

        $a = [];

        for ($i = 0; $i < $n; $i += 2) {
            $r1 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
            $r2 = rand(0, PHP_INT_MAX) / PHP_INT_MAX;
            
            $r = sqrt(-2. * log($r1));

            $a[] = $r * sin($r2 * self::TWO_PI);
            $a[] = $r * cos($r2 * self::TWO_PI);
        }

        if (count($a) > $n) {
            $a = array_slice($a, 0, $n);
        }

        return static::quick($a);
    }
1652
public function columns($columns)
  {
    if ( is_null($this->columns) ){
      $this->columns = new Element('()', $columns);
    }
    else {
      $this->columns->append($columns);
    }

    return $this;
  }
375492
public static function createFromArray(array $input): self
    {
        if (2 != count($input)) {
            throw new InvalidArgumentException(
                sprintf("%s() expects an array with exactly two elements", __METHOD__)
            );
        }
        if (array_key_exists('center', $input) && array_key_exists('radius', $input)) {
            $center = $input['center'];
            $radius = $input['radius'];
        } else {
            $center = array_shift($input);
            $radius = array_shift($input);
        }
        if (is_array($center)) {
            $center = Point::createFromArray($center);
        }
        return new self($center, $radius);
    }
224333
public function classes(array $classes) : Element
    {
        $this->classes = array_merge($this->classes, $classes);

        return $this;
    }
50776
public function divideByScalar($value)
    {
        $newMatrix = [];
        for ($i = 0; $i < $this->rows; ++$i) {
            for ($j = 0; $j < $this->columns; ++$j) {
                $newMatrix[$i][$j] = $this->matrix[$i][$j] / $value;
            }
        }

        return new self($newMatrix, false);
    }
841777
public function addElements(array $elements) {
		foreach ($elements as $name => $element) {
			if (is_string($name)) {
				$this->addElement($element, $name);
			} else {
				$this->addElement($element);
			}
		}
		
		return $this;
	}
41778
public function setColumnNames(array $names)
    {
        if ($this->featureCount !== 0 && count($names) !== $this->featureCount) {
            throw new InvalidArgumentException(sprintf('Length of the given array should be equal to feature count %s', $this->featureCount));
        }

        $this->columnNames = $names;

        return $this;
    }
1011388
public function setArray($key, array $values)
	{
		$this->validateKey($key);
		if (empty($values)) {
			throw new \InvalidArgumentException("Missing value.");
		}
		foreach ($values as $v) {
			$this->validateValue($v);
		}
		$this->params[$key] = [];
		foreach ($values as $v) {
			$this->params[$key][] = $v;
		}
		return $this;
	}
1082202
public function buildArray(array $inputs)
    {
        $newArray = [];

        foreach ($inputs as $path => $value) {
            if (is_array($value)) {
                throw new InvalidArgumentException('Input array CANNOT contain nested arrays.');
            }

            if (isset($path[0])) {
                if ($path[0] === '_' || $path[0] === '.') {
                    unset($inputs[$path]);
                    continue;
                }
            }

            $this->assignArrayByDotPath($newArray, $path, $value);
        }

        return $newArray;
    }
176966
public static function stack(array $vectors) : self
    {
        $a = [];

        foreach ($vectors as $vector) {
            if (!$vector instanceof Vector) {
                throw new InvalidArgumentException('Cannot stack a non'
                    . ' vector, ' . gettype($vector) . ' found.');
            }

            $a[] = $vector->asArray();
        }

        return self::quick($a);
    }