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);
    }
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);
    }
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 ) ; }
Build a diagonal matrix with the value of each element along the
diagonal and 0s everywhere else.

@param array $elements
@throws \InvalidArgumentException
@return self
Build a diagonal matrix with the value of each element along the diagonal and 0s everywhere else .