Blog Entry © Friday, October 31, 2025, by James Pate Williams, Jr. Quantum Mechanical Harmonic Oscillator Matrix Elements

// QMHOMatrixElements.cpp : Defines the entry point for the application.
//

#include "pch.h"
#include "framework.h"
#include "QMHOMatrixElements.h"
#include "Integration.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
WCHAR buffer[32768], line[128];
int n, m;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_QMHOMATRIXELEMENTS, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_QMHOMATRIXELEMENTS));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex = { };

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_QMHOMATRIXELEMENTS));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_QMHOMATRIXELEMENTS);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

static double Factorial(int n)
{
    // n! = n ... 2 1

    double factorial = 1.0;

    for (int i = 2; i <= n; i++)
    {
        factorial *= i;
    }

    return factorial;
}

static double N(int n)
{
    // normilization factor

    double pi = 4.0 * atan(1.0);
    double factor1 = sqrt(1.0 / pi);
    double factor2 = 1.0 / (pow(2.0, n) * Factorial(n));
    return sqrt(factor1 * factor2);
}

static double H(double xi, int n)
{
    // Hermite polynomial

    if (n == 1)
    {
        return 2.0 * xi;
    }

    else if (n == 2)
    {
        return 4.0 * xi * xi - 2.0;
    }

    else if (n == 3)
    {
        return xi * (8.0 * xi * xi - 12.0);
    }

    else if (n == 4)
    {
        return 16.0 * pow(xi, 4.0) - 48.0 * pow(xi, 2.0) + 12.0;
    }

    else if (n == 5)
    {
        return 32.0 * pow(xi, 5.0) - 160.0 * pow(xi, 3.0) +
            120.0 * xi;
    }

    else if (n == 6)
    {
        return 64.0 * pow(xi, 6.0) - 480.0 * pow(xi, 4.0) +
            720.0 * xi * xi - 120.0;
    }

    else if (n == 7)
    {
        return 128.0 * pow(xi, 7.0) - 1344.0 * pow(xi, 5.0) +
            3360.0 * xi * xi * xi - 1680.0 * xi;
    }

    else if (n == 8)
    {
        return 256.0 * pow(xi, 8.0) - 3584.0 * pow(xi, 6.0) +
            13440.0 * pow(xi, 4.0) - 13440.0 * xi * xi + 1680.0;
    }

    else if (n == 9)
    {
        return 512.0 * pow(xi, 9.0) - 9216.0 * pow(xi, 7.0) +
            48384.0 * pow(xi, 5.0) - 80640.0 * xi * xi * xi + 30240.0 * xi;
    }

    else if (n == 10)
    {
        return 1024.0 * pow(xi, 10.0) - 23040.0 * pow(xi, 8.0) +
            161280.0 * pow(xi, 6.0) - 403200.0 * pow(xi, 4.0) +
            302400.0 * xi * xi - 30240.0;
    }

    else
    {
        return 0.0;
    }
}

static double Psi(double xi, int n)
{
    return N(n) * exp(-xi * xi / 2.0) * H(xi, n);
}

static double Dx(double xi)
{
    return xi * Psi(xi, n) * Psi(xi, m);
}

static double Ex(double xi)
{
    return xi * xi * Psi(xi, n) * Psi(xi, m);
}

static double Fx(double xi)
{
    return pow(xi, 3.0) * Psi(xi, n) * Psi(xi, m);
}

static double Gx(double xi)
{
    return pow(xi, 4.0) * Psi(xi, n) * Psi(xi, m);
}

#define IDC_COMPUTE_BUTTON  1010
#define IDC_CANCEL_BUTTON   1020
#define IDC_CLEAR_BUTTON    1030
#define IDC_EDIT_MULTILINE  1040

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HFONT hFont = nullptr;
    static HWND hCombo1 = nullptr;
    static HWND hCombo2 = nullptr;
    static HWND hCombo3 = nullptr;
    static HWND hEditMultiline = nullptr;

    switch (message)
    {
    case WM_CREATE:
        CreateWindowEx(0, L"STATIC", L"n:", WS_CHILD | WS_VISIBLE,
            10, 10, 80, 20, hWnd, (HMENU)IDC_STATIC, hInst, NULL);
        hCombo1 = CreateWindowEx(0, L"COMBOBOX", nullptr,
            WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL |
            ES_AUTOVSCROLL, 120, 10, 120, 100, hWnd, nullptr, hInst, nullptr);
        CreateWindowEx(0, L"STATIC", L"m:", WS_CHILD | WS_VISIBLE,
            10, 40, 80, 20, hWnd, (HMENU)IDC_STATIC, hInst, NULL);
        hCombo2 = CreateWindowEx(0, L"COMBOBOX", nullptr,
            WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_VSCROLL |
            ES_AUTOVSCROLL, 120, 40, 120, 150, hWnd, nullptr, hInst, nullptr);
        hEditMultiline = CreateWindowEx(
            WS_EX_CLIENTEDGE,                       // Extended style for sunken border
            TEXT("EDIT"),                           // Class name
            TEXT(""),                               // Initial text (can be blank)
            WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOHSCROLL |
            ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | WS_HSCROLL | WS_VSCROLL,
            250, 10, 600, 420,                      // Position and size
            hWnd,                                   // Parent window handle
            (HMENU)IDC_EDIT_MULTILINE,              // Unique control ID
            hInst,                                  // Application instance
            NULL                                    // Extra parameter
        );
        CreateWindowEx(0, L"BUTTON", L"Compute", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            10, 80, 80, 30, hWnd, (HMENU)IDC_COMPUTE_BUTTON, hInst, NULL);
        CreateWindowEx(0, L"BUTTON", L"Cancel", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            10, 120, 80, 30, hWnd, (HMENU)IDC_CANCEL_BUTTON, hInst, NULL);
        CreateWindowEx(0, L"BUTTON", L"Clear", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            10, 160, 80, 30, hWnd, (HMENU)IDC_CLEAR_BUTTON, hInst, NULL);
        hFont = CreateFont(16, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
            UNICODE, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
            DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Courier New");
        SendMessage(hCombo1, WM_SETFONT, (WPARAM)hFont, 0);
        SendMessage(hCombo2, WM_SETFONT, (WPARAM)hFont, 0);
        SendMessage(hEditMultiline, WM_SETFONT, (WPARAM)hFont, 0);
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"0");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"1");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"2");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"3");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"4");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"5");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"6");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"7");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"8");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"9");
        SendMessage(hCombo1, CB_ADDSTRING, 0, (LPARAM)L"10");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"0");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"1");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"2");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"3");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"4");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"5");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"6");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"7");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"8");
        SendMessage(hCombo2, CB_ADDSTRING, 0, (LPARAM)L"9");
        SendMessage(hCombo3, CB_ADDSTRING, 0, (LPARAM)L"10");
        SendMessage(hCombo1, CB_SETCURSEL, 0, 0);
        SendMessage(hCombo2, CB_SETCURSEL, 0, 0);
        //ShowWindow(hWnd, SW_SHOWMAXIMIZED);
        buffer[0] = L'\0';
        break;
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDC_COMPUTE_BUTTON:
            {
                GetWindowText(hCombo1, line, 128);
                std::wstring nStr(line);
                n = stoi(nStr);
                GetWindowText(hCombo2, line, 128);
                std::wstring mStr(line);
                m = stoi(mStr);
                double x0 = -10.0, x1 = 10.0;
                double integ1 = Integration::SimpsonsRule(1024, x0, x1, Dx);
                double integ2 = Integration::SimpsonsRule(1024, x0, x1, Ex);
                double integ3 = Integration::SimpsonsRule(1024, x0, x1, Fx);
                double integ4 = Integration::SimpsonsRule(1024, x0, x1, Gx);
                swprintf_s(line, L"n = %d\tm = %d\t(x^1) = %+lf\r\n", n, m, integ1);
                wcscat_s(buffer, line);
                swprintf_s(line, L"n = %d\tm = %d\t(x^2) = %+lf\r\n", n, m, integ2);
                wcscat_s(buffer, line);
                swprintf_s(line, L"n = %d\tm = %d\t(x^3) = %+lf\r\n", n, m, integ3);
                wcscat_s(buffer, line);
                swprintf_s(line, L"n = %d\tm = %d\t(x^4) = %+lf\r\n", n, m, integ4);
                wcscat_s(buffer, line);
                SetWindowText(hEditMultiline, buffer);
                break;
            }
            case IDC_CLEAR_BUTTON:
                buffer[0] = '\0';
                SetWindowText(hEditMultiline, buffer);
                break;
            case IDC_CANCEL_BUTTON:
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

Unknown's avatar

Author: jamespatewilliamsjr

My whole legal name is James Pate Williams, Jr. I was born in LaGrange, Georgia approximately 70 years ago. I barely graduated from LaGrange High School with low marks in June 1971. Later in June 1979, I graduated from LaGrange College with a Bachelor of Arts in Chemistry with a little over a 3 out 4 Grade Point Average (GPA). In the Spring Quarter of 1978, I taught myself how to program a Texas Instruments desktop programmable calculator and in the Summer Quarter of 1978 I taught myself Dayton BASIC (Beginner's All-purpose Symbolic Instruction Code) on LaGrange College's Data General Eclipse minicomputer. I took courses in BASIC in the Fall Quarter of 1978 and FORTRAN IV (Formula Translator IV) in the Winter Quarter of 1979. Professor Kenneth Cooper, a genius poly-scientist taught me a course in the Intel 8085 microprocessor architecture and assembly and machine language. We would hand assemble our programs and insert the resulting machine code into our crude wooden box computer which was designed and built by Professor Cooper. From 1990 to 1994 I earned a Bachelor of Science in Computer Science from LaGrange College. I had a 4 out of 4 GPA in the period 1990 to 1994. I took courses in C, COBOL, and Pascal during my BS work. After graduating from LaGrange College a second time in May 1994, I taught myself C++. In December 1995, I started using the Internet and taught myself client-server programming. I created a website in 1997 which had C and C# implementations of algorithms from the "Handbook of Applied Cryptography" by Alfred J. Menezes, et. al., and some other cryptography and number theory textbooks and treatises.

Leave a comment