



import logging

import numpy as np
import pytest
from scipy.linalg import eigh

logging.basicConfig(level=logging.INFO, format='''%(message)s''')





def 					__snake_case     (      _UpperCAmelCase    ):
    return input_array.reshape((input_array.size, 1)    )





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    ):
    __a							=    np.nan
    for i in range(_UpperCAmelCase    ):
        __a							=    features[:, labels == i]
        __a							=    data.mean(1    )
        # Centralize the data of class i
        __a							=    data - column_reshape(_UpperCAmelCase    )
        if i > 0:
            # If covariance_sum is not None
            covariance_sum += np.dot(_UpperCAmelCase       ,			centered_data.T    )
        else:
            # If covariance_sum is np.nan (i.e. first loop)
            __a							=    np.dot(_UpperCAmelCase       ,			centered_data.T    )

    return covariance_sum / features.shape[1]





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    ):
    __a							=    features.mean(1    )
    __a							=    np.nan
    for i in range(_UpperCAmelCase    ):
        __a							=    features[:, labels == i]
        __a							=    data.shape[1]
        __a							=    data.mean(1    )
        if i > 0:
            # If covariance_sum is not None
            covariance_sum += device_data * np.dot(
                column_reshape(_UpperCAmelCase    ) - column_reshape(_UpperCAmelCase    )       ,			(column_reshape(_UpperCAmelCase    ) - column_reshape(_UpperCAmelCase    )).T       ,			)
        else:
            # If covariance_sum is np.nan (i.e. first loop)
            __a							=    device_data * np.dot(
                column_reshape(_UpperCAmelCase    ) - column_reshape(_UpperCAmelCase    )       ,			(column_reshape(_UpperCAmelCase    ) - column_reshape(_UpperCAmelCase    )).T       ,			)

    return covariance_sum / features.shape[1]





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase    ):

    # Check if the features have been loaded
    if features.any():
        __a							=    features.mean(1    )
        # Center the dataset
        __a							=    features - np.reshape(_UpperCAmelCase       ,			(data_mean.size, 1)    )
        __a							=    np.dot(_UpperCAmelCase       ,			centered_data.T    ) / features.shape[1]
        __a	,       __a							=    np.linalg.eigh(_UpperCAmelCase    )
        # Take all the columns in the reverse order (-1), and then takes only the first
        __a							=    eigenvectors[:, ::-1][:, 0:dimensions]
        # Project the database on the new space
        __a							=    np.dot(filtered_eigenvectors.T       ,			_UpperCAmelCase    )
        logging.info('''Principal Component Analysis computed'''    )

        return projected_data
    else:
        logging.basicConfig(level=logging.ERROR       ,			format='''%(message)s'''       ,			force=_UpperCAmelCase    )
        logging.error('''Dataset empty'''    )
        raise AssertionError





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    ):
    assert classes > dimensions

    # Check if features have been already loaded
    if features.any:
        __a	,       __a							=    eigh(
            covariance_between_classes(_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    )       ,			covariance_within_classes(_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    )       ,			)
        __a							=    eigenvectors[:, ::-1][:, :dimensions]
        __a	,       __a	,       __a							=    np.linalg.svd(_UpperCAmelCase    )
        __a							=    svd_matrix[:, 0:dimensions]
        __a							=    np.dot(filtered_svd_matrix.T       ,			_UpperCAmelCase    )
        logging.info('''Linear Discriminant Analysis computed'''    )

        return projected_data
    else:
        logging.basicConfig(level=logging.ERROR       ,			format='''%(message)s'''       ,			force=_UpperCAmelCase    )
        logging.error('''Dataset empty'''    )
        raise AssertionError





def 					__snake_case     (      ):
    # Create dummy dataset with 2 classes and 3 features
    __a							=    np.array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]    )
    __a							=    np.array([0, 0, 0, 1, 1]    )
    __a							=    2
    __a							=    2

    # Assert that the function raises an AssertionError if dimensions > classes
    with pytest.raises(_UpperCAmelCase    ) as error_info:
        __a							=    linear_discriminant_analysis(
            _UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    )
        if isinstance(_UpperCAmelCase       ,			np.ndarray    ):
            raise AssertionError(
                '''Did not raise AssertionError for dimensions > classes'''    )
        assert error_info.type is AssertionError





def 					__snake_case     (      ):
    __a							=    np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]    )
    __a							=    2
    __a							=    np.array([[6.92_82_03_23, 8.66_02_54_04, 10.39_23_04_85], [3.0, 3.0, 3.0]]    )

    with pytest.raises(_UpperCAmelCase    ) as error_info:
        __a							=    principal_component_analysis(_UpperCAmelCase       ,			_UpperCAmelCase    )
        if not np.allclose(_UpperCAmelCase       ,			_UpperCAmelCase    ):
            raise AssertionError
        assert error_info.type is AssertionError


if __name__ == "__main__":
 import doctest

 doctest.testmod()










def 					__snake_case     (      _UpperCAmelCase = 1000000    ):
    __a							=    limit + 1
    __a							=    [0] * limit
    for first_term in range(1       ,			_UpperCAmelCase    ):
        for n in range(_UpperCAmelCase       ,			_UpperCAmelCase       ,			_UpperCAmelCase    ):
            __a							=    first_term + n / first_term
            if common_difference % 4:  # d must be divisble by 4
                continue
            else:
                common_difference /= 4
                if (
                    first_term > common_difference
                    and first_term < 4 * common_difference
                ):  # since x,y,z are positive integers
                    frequency[n] += 1  # so z>0 and a>d ,also 4d<a

    __a							=    sum(1 for x in frequency[1:limit] if x == 10    )

    return count


if __name__ == "__main__":
 print(f'{solution() = }')





