src/LinearAlgebra/MatrixFactory.php
private static function determineMatrixType(array $A, int $vandermonde_n = null): string
    {
        $m = count($A);

        // 1-dimensional array is how we create diagonal and vandermonde matrices,
        // as well as matrices from an array of vectors
        $one_dimensional = count(array_filter($A, 'is_array')) === 0;
        if ($one_dimensional) {
            $is_array_of_vectors = array_reduce(
                $A,
                function ($carry, $item) {
                    return $carry && ($item instanceof Vector);
                },
                true
            );
            if ($is_array_of_vectors) {
                return 'from_vectors';
            }
            if (is_null($vandermonde_n)) {
                return 'diagonal';
            }
            if ($m === $vandermonde_n) {
                return 'vandermonde_square';
            }
            return 'vandermonde';
        }

        // Square Matrices have the same number of rows (m) and columns (n)
        $n = count($A[0]);
        if ($m === $n) {
            // closures are objects, so we need to separate them out.
            if (is_object($A[0][0])) {
                if ($A[0][0] instanceof \Closure) {
                    return 'function_square';
                } else {
                    return 'object_square';
                }
            }
            return 'square';
        }

        // Non square Matrices
        // First check to make sure it isn't something strange
        if (is_array($A[0][0])) {
            return 'unknown';
        }
        // Then check remaining matrix types
        if (is_callable($A[0][0])) {
            return 'function';
        }
        return 'matrix';
    }
private static function determineMatrixType(array $A, int $vandermonde_n = null): string
    {
        $m = count($A);

        // 1-dimensional array is how we create diagonal and vandermonde matrices,
        // as well as matrices from an array of vectors
        $one_dimensional = count(array_filter($A, 'is_array')) === 0;
        if ($one_dimensional) {
            $is_array_of_vectors = array_reduce(
                $A,
                function ($carry, $item) {
                    return $carry && ($item instanceof Vector);
                },
                true
            );
            if ($is_array_of_vectors) {
                return 'from_vectors';
            }
            if (is_null($vandermonde_n)) {
                return 'diagonal';
            }
            if ($m === $vandermonde_n) {
                return 'vandermonde_square';
            }
            return 'vandermonde';
        }

        // Square Matrices have the same number of rows (m) and columns (n)
        $n = count($A[0]);
        if ($m === $n) {
            // closures are objects, so we need to separate them out.
            if (is_object($A[0][0])) {
                if ($A[0][0] instanceof \Closure) {
                    return 'function_square';
                } else {
                    return 'object_square';
                }
            }
            return 'square';
        }

        // Non square Matrices
        // First check to make sure it isn't something strange
        if (is_array($A[0][0])) {
            return 'unknown';
        }
        // Then check remaining matrix types
        if (is_callable($A[0][0])) {
            return 'function';
        }
        return 'matrix';
    }
private static function determineMatrixType ( array $ A , int $ vandermonde_n = null ) : string { $ m = count ( $ A ) ; // 1-dimensional array is how we create diagonal and vandermonde matrices, // as well as matrices from an array of vectors $ one_dimensional = count ( array_filter ( $ A , 'is_array' ) ) === 0 ; if ( $ one_dimensional ) { $ is_array_of_vectors = array_reduce ( $ A , function ( $ carry , $ item ) { return $ carry && ( $ item instanceof Vector ) ; } , true ) ; if ( $ is_array_of_vectors ) { return 'from_vectors' ; } if ( is_null ( $ vandermonde_n ) ) { return 'diagonal' ; } if ( $ m === $ vandermonde_n ) { return 'vandermonde_square' ; } return 'vandermonde' ; } // Square Matrices have the same number of rows (m) and columns (n) $ n = count ( $ A [ 0 ] ) ; if ( $ m === $ n ) { // closures are objects, so we need to separate them out. if ( is_object ( $ A [ 0 ] [ 0 ] ) ) { if ( $ A [ 0 ] [ 0 ] instanceof \ Closure ) { return 'function_square' ; } else { return 'object_square' ; } } return 'square' ; } // Non square Matrices // First check to make sure it isn't something strange if ( is_array ( $ A [ 0 ] [ 0 ] ) ) { return 'unknown' ; } // Then check remaining matrix types if ( is_callable ( $ A [ 0 ] [ 0 ] ) ) { return 'function' ; } return 'matrix' ; }
Determine what type of matrix to create

@param  array    $A 1- or 2-dimensional array of Matrix data
1-dimensional array for Diagonal and Vandermonde matrices
2-dimensional array for Square, Function, and regular Matrices
@param  int|null $vandermonde_n Optional n for Vandermonde matrix

@return string indicating what matrix type to create
Determine what type of matrix to create