# -*- coding: utf-8 -*-
"""ComplexDimensionalityReduction.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1mhwOW-47XnZzDFJpeXodvP-0AMeXQZ0X
"""

import pandas as pd
import numpy as np

def create_complex_feature(dataframe, feature1, feature2):
    '''
    This function takes the names of two features in a pandas DataFrame
    and creates a new feature that is the complex combination of those features.
    '''
    return dataframe[feature1] + 1j * dataframe[feature2]

def squeeze_to_complex(dataset):
    '''
    This function takes a pandas DataFrame and returns a list of complex numbers
    created by pairing up adjacent columns. If there is an odd number of columns,
    the last column is padded with zeros.
    '''
    complexes = []
    for index, row in dataset.iterrows():
        # If the number of features is odd, pad with an extra zero
        if len(row) % 2 != 0:
            row = np.pad(row, (0, 1), 'constant')
        # Iterate over each pair of columns and create complex numbers
        for i in range(0, len(row), 2):
            c = complex(row[i], row[i+1])
            complexes.append(c)
    return complexes

def CDR(df: pd.DataFrame) -> pd.DataFrame:
    # Create a numpy array of complex numbers from the DataFrame
    complex_array = np.array(squeeze_to_complex(df))
    # Reshape the array to have two columns, real and imaginary parts of the complex numbers
    num_complex_features = (df.shape[1] + 1) // 2  # Account for odd number of columns
    return pd.DataFrame(complex_array.reshape(df.shape[0], num_complex_features))