Tag: programming
Blog Entry © Monday, September 8, 2025, by James Pate Williams, Jr., Comparison of Two Applications to Find the Minima of Classical Objective Functions
#pragma once
#include "pch.h"
class Functions
{
public:
// Ackley's function
static double lbx1[3];
static double ubx1[3];
static double fBest1;
static double xBest1[3];
static double f1(
int n, std::vector<double> z);
// Beale's function
static double lbx2[3];
static double ubx2[3];
static double fBest2;
static double xBest2[3];
static double f2(
int n, std::vector<double> z);
// Booth's function
static double lbx3[3];
static double ubx3[3];
static double fBest3;
static double xBest3[3];
static double f3(
int n, std::vector<double> z);
// Rosenbrock function
static double lbx4[3];
static double ubx4[3];
static double fBest4;
static double xBest4[3];
static double f4(
int n, std::vector<double> z);
// Holder table function
static double lbx5[3];
static double ubx5[3];
static double fBest5;
static double xBest5[3];
static double f5(
int n, std::vector<double> z);
// McCormick function
static double lbx6[3];
static double ubx6[3];
static double fBest6;
static double xBest6[3];
static double f6(
int n, std::vector<double> z);
static double rosenbrock(
int n, std::vector<double> x,
std::vector<double>& g);
static double mccormick(
int n, std::vector<double> z,
std::vector<double>& g);
};
#include "pch.h"
#include "Functions.h"
// Ackley's function
double Functions::lbx1[3] = { 0, -5, -5 };
double Functions::ubx1[3] = { 0, +5, +5 };
double Functions::fBest1 = 0.0;
double Functions::xBest1[3] = { 0, 0.0, 0.0 };
double Functions::f1(int n, std::vector<double> z)
{
double e = exp(1.0), pi = 4.0 * atan(1.0);
double x = z[1], y = z[2], pi2 = 2.0 * pi;
return -20 * exp(-0.2 * sqrt(0.5 * (x * x + y * y))) -
exp(0.5 * (cos(pi2 * x) + cos(pi2 * y))) + e + 20;
}
// Beale's function
double Functions::lbx2[3] = { 0, -4.5, -4.5 };
double Functions::ubx2[3] = { 0, +4.5, +4.5 };
double Functions::fBest2 = 0.0;
double Functions::xBest2[3] = { 0, 3.0, 2.5 };
double Functions::f2(int n, std::vector<double> z)
{
double x = z[1], y = z[2];
return pow(1.5 - x + x * y, 2) + pow(2.25 - x + x * y * y, 2) +
pow(2.625 - x + x * y * y * y, 2);
}
// Booth's function
double Functions::lbx3[3] = {0, -10, -10};
double Functions::ubx3[3] = {0, +10, +10};
double Functions::fBest3 = 0.0;
double Functions::xBest3[3] = {0, 1.0, 3.0};
double Functions::f3(int n, std::vector<double> z)
{
double x = z[1], y = z[2];
return pow(x + 2 * y - 7, 2) + pow(2 * x + y - 5, 2);
}
// Rosenbrock function
double Functions::lbx4[3] = {0, -100.0, -100.0};
double Functions::ubx4[3] = {0, +100.0, +100.0};
double Functions::fBest4 = 0.0;
double Functions::xBest4[3] = {0, 1.0, 1.0};
double Functions::f4(int n, std::vector<double> x)
{
double temp = x[2] - x[1] * x[1];
return temp * temp * 100.0 + (1.0 - x[1]) * (1.0 - x[1]);
}
// Holder table function
double Functions::lbx5[3] = {0, -10.0, -10.0};
double Functions::ubx5[3] = {0, +10.0, +10.0};
double Functions::fBest5 = -19.2085;
double Functions::xBest5[3] = {0, 8.05502, 9.66459};
double Functions::f5(int n, std::vector<double> z)
{
double pi = 4.0 * atan(1.0);
double x = z[1], y = z[2];
return -fabs(sin(x) * cos(y) *
exp(fabs(1.0 - sqrt(x * x + y * y) / pi)));
}
// McCormick function
double Functions::lbx6[3] = {0, -1.5, -3};
double Functions::ubx6[3] = {0, +4, +4};
double Functions::fBest6 = -1.9133;
double Functions::xBest6[3] = {0, -0.54719, -1.54719};
double Functions::f6(int n, std::vector<double> z)
{
double x = z[1], y = z[2];
return sin(x + y) + pow(x - y, 2) - 1.5 * x + 2.5 * y + 1;
}
double Functions::rosenbrock(
int n, std::vector<double> x,
std::vector<double>& g)
{
double temp;
temp = x[2] - x[1] * x[1];
g[1] = (-temp * 400.0 + 2.0) * x[1] - 2.0;
g[2] = temp * 200.0;
return temp * temp * 100.0 + (1.0 - x[1]) * (1.0 - x[1]);
}
double Functions::mccormick(
int n, std::vector<double> z,
std::vector<double>& g)
{
double x = z[1], y = z[2];
double c = cos(x + y);
double p = 2.0 * (x - y);
g[1] = c + p - 1.5;
g[2] = c - p + 2.5;
return sin(x + y) + pow(x - y, 2) - 1.5 * x + 2.5 * y + 1;
}
The PRAXIS and FLEMIN C source code can be found in the handbook “A Numerical Library in C for Scientists and Engineers” (c) 1996 by H. T. Lau, Ph.D. I translated the C code to C++ using Standard Template Library vectors.
Blog Entry © Wednesday, September 3, 2025, by James Pate Williams, Jr. Solution of Exercise 1.11 in Modern Quantum Chemistry an Introduction to Advanced Electronic Structure Theory © 1996 by Attila Szabo and Neil S. Ostlund
// EigenVV2x2.cpp (c) Wednesday, September 3, 2025
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
// Solution to Exercise 1.11 in "Quantum Chemistry
// an Introduction to Advanced Electronic Structure
// Theory (c) 1996 by Attila Szabo and Neil S. Ostlund
#include "pch.h"
#include "framework.h"
#include "EigenVV2x2.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
std::wstring text; // output wide string
// 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_EIGENVV2X2, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EIGENVV2X2));
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_EIGENVV2X2));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_EIGENVV2X2);
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;
}
#define IDC_STATIC1 1000
#define IDC_STATIC2 1010
#define IDC_STATIC3 1020
#define IDC_STATIC4 1030
#define IDC_EDIT_A11 2000
#define IDC_EDIT_A12 2010
#define IDC_EDIT_A21 2020
#define IDC_EDIT_A22 2030
#define IDC_EDIT_MULTILINE 3000
#define IDC_BUTTON_COMPUTE 4000
#define IDC_BUTTON_CANCEL 4010
//
// 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 = { };
static HWND hEditMultiline = { };
static HWND hEditA11 = { };
static HWND hEditA12 = { };
static HWND hEditA21 = { };
static HWND hEditA22 = { };
switch (message)
{
case WM_CREATE:
{
hFont = CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Courier New Bold");
CreateWindowEx(0, L"STATIC", L"A[1][1]:", WS_CHILD | WS_VISIBLE,
10, 10, 80, 20, hWnd, (HMENU)IDC_STATIC1, hInst, NULL);
CreateWindowEx(0, L"STATIC", L"A[1][2]:", WS_CHILD | WS_VISIBLE,
10, 40, 80, 20, hWnd, (HMENU)IDC_STATIC2, hInst, NULL);
CreateWindowEx(0, L"STATIC", L"A[2][1]:", WS_CHILD | WS_VISIBLE,
10, 70, 80, 20, hWnd, (HMENU)IDC_STATIC3, hInst, NULL);
CreateWindowEx(0, L"STATIC", L"A[2][2]:", WS_CHILD | WS_VISIBLE,
10, 100, 80, 20, hWnd, (HMENU)IDC_STATIC4, hInst, NULL);
hEditA11 = CreateWindowEx(0, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 10, 200, 20, hWnd, (HMENU)IDC_EDIT_A11, hInst, NULL);
hEditA12 = CreateWindowEx(0, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 40, 200, 20, hWnd, (HMENU)IDC_EDIT_A12, hInst, NULL);
hEditA21 = CreateWindowEx(0, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 70, 200, 20, hWnd, (HMENU)IDC_EDIT_A21, hInst, NULL);
hEditA22 = CreateWindowEx(0, L"EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 100, 200, 20, hWnd, (HMENU)IDC_EDIT_A22, hInst, NULL);
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,
310, 10, 300, 300, // 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, 130, 80, 30, hWnd, (HMENU)IDC_BUTTON_COMPUTE, hInst, NULL);
CreateWindowEx(0, L"BUTTON", L"Cancel", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
220, 130, 80, 30, hWnd, (HMENU)IDC_BUTTON_CANCEL, hInst, NULL);
SendMessage(hEditMultiline, WM_SETFONT, (WPARAM)hFont, TRUE);
SetDlgItemText(hWnd, IDC_EDIT_A11, L"3");
SetDlgItemText(hWnd, IDC_EDIT_A12, L"1");
SetDlgItemText(hWnd, IDC_EDIT_A21, L"1");
SetDlgItemText(hWnd, IDC_EDIT_A22, L"3");
}
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_BUTTON_COMPUTE:
{
WCHAR line[128] = L"";
text = L"";
// eigenvalues
std::vector<double> omega(3);
// matrix
std::vector<std::vector<double>> A(3,
std::vector<double>(3));
std::vector<std::vector<double>> B(3,
std::vector<double>(3));
// eigenvectors
std::vector<std::vector<double>> c(3,
std::vector<double>(3));
GetWindowText(hEditA11, line, 128);
std::wstring A11Str(line);
A[1][1] = std::stod(A11Str);
GetWindowText(hEditA12, line, 128);
std::wstring A12Str(line);
A[1][2] = std::stod(A12Str);
GetWindowText(hEditA21, line, 128);
std::wstring A21Str(line);
A[2][1] = std::stod(A21Str);
GetWindowText(hEditA22, line, 128);
std::wstring A22Str(line);
A[2][2] = std::stod(A22Str);
double term1 = A[1][1] + A[2][2];
double term2 = pow(A[2][2] - A[1][1], 2.0);
double term3 = 4.0 * A[1][2] * A[2][1];
// compute eigenvalues
omega[1] = 0.5 * (term1 - sqrt(term2 + term3));
omega[2] = 0.5 * (term1 + sqrt(term2 + term3));
swprintf_s(line, L"Eigenvalues:\r\n\r\n");
text += std::wstring(line);
swprintf_s(line, L"omega 1 = %13.10lf\r\n", omega[1]);
text += std::wstring(line);
swprintf_s(line, L"omega 2 = %13.10lf\r\n\r\n", omega[2]);
text += std::wstring(line);
// compute eigenvalues using a unitary transformation
// matrix A must be symmetric
double theta0 = 0.0;
if (A[1][1] == A[2][2])
{
theta0 = 0.5 * acos(0.0);
}
else
{
theta0 = 0.5 * atan(2.0 * A[1][2] /
(A[1][1] - A[2][2]));
}
// compute the eigenvalues
omega[1] = A[1][1] * pow(cos(theta0), 2.0) +
A[2][2] * pow(sin(theta0), 2.0) +
A[1][2] * sin(2.0 * theta0);
omega[2] = A[1][1] * pow(cos(theta0), 2.0) +
A[2][2] * pow(sin(theta0), 2.0) -
A[1][2] * sin(2.0 * theta0);
swprintf_s(line, L"Eigenvalues:\r\n\r\n");
text += std::wstring(line);
swprintf_s(line, L"omega 1 = %13.10lf\r\n", omega[1]);
text += std::wstring(line);
swprintf_s(line, L"omega 2 = %13.10lf\r\n\r\n", omega[2]);
text += std::wstring(line);
// compute eigenvectors
c[1][1] = cos(theta0);
c[1][2] = sin(theta0);
c[2][1] = sin(theta0);
c[2][2] = -cos(theta0);
swprintf_s(line, L"Eigenvectors:\r\n\r\n");
text += std::wstring(line);
swprintf_s(line, L"c 11 = %13.10lf\r\n", c[1][1]);
text += std::wstring(line);
swprintf_s(line, L"c 12 = %13.10lf\r\n", c[1][2]);
text += std::wstring(line);
swprintf_s(line, L"c 21 = %13.10lf\r\n", c[2][1]);
text += std::wstring(line);
swprintf_s(line, L"c 22 = %13.10lf\r\n", c[2][2]);
text += std::wstring(line);
SetWindowText(hEditMultiline, text.c_str());
break;
}
case IDC_BUTTON_CANCEL:
{
PostQuitMessage(0);
break;
}
break;
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;
}
Blog Entry (c) Wednesday, August 13, 2025, by James Pate Williams, Jr. Exercises from an Online Textbook
#include <complex>
#include <vector>
class CmpLinearAlgebra
{
public:
static void CmpPrintMatrix(
int m, int n,
std::vector<std::vector<std::complex<double>>>& Ac);
static void CmpAddition(
size_t m, size_t n,
std::vector<std::vector<std::complex<double>>>& A,
std::vector<std::vector<std::complex<double>>>& B,
std::vector<std::vector<std::complex<double>>>& C);
static void CmpSubtraction(
size_t m, size_t n,
std::vector<std::vector<std::complex<double>>>& A,
std::vector<std::vector<std::complex<double>>>& B,
std::vector<std::vector<std::complex<double>>>& C);
static void CmpMultiply(
size_t m, size_t n, size_t p,
std::vector<std::vector<std::complex<double>>>& A,
std::vector<std::vector<std::complex<double>>>& B,
std::vector<std::vector<std::complex<double>>>& C);
static void CmpAnticommutator(
size_t n,
std::vector<std::vector<std::complex<double>>>& A,
std::vector<std::vector<std::complex<double>>>& B,
std::vector<std::vector<std::complex<double>>>& C);
static void CmpCommutator(
size_t n,
std::vector<std::vector<std::complex<double>>>& A,
std::vector<std::vector<std::complex<double>>>& B,
std::vector<std::vector<std::complex<double>>>& C);
static void CmpAdjoint(
size_t m, size_t n,
std::vector<std::vector<std::complex<double>>>& Ac,
std::vector<std::vector<std::complex<double>>>& Ad);
static std::complex<double> CmpDeterminant(
bool& failure, int n,
std::vector<std::vector<std::complex<double>>>& A);
static bool CmpGaussianElimination(
int m, int n,
std::vector<std::vector<std::complex<double>>>& A,
std::vector<std::complex<double>>& b,
std::vector<std::complex<double>>& x,
std::vector<size_t>& pivot);
static bool CmpGaussianFactor(
int n, std::vector<std::vector<std::complex<double>>>& M,
std::vector<size_t>& pivot);
static bool CmpGaussianSolution(
int n, std::vector<std::vector<std::complex<double>>>& M,
std::vector<std::complex<double>>& b,
std::vector<std::complex<double>>& x,
std::vector<size_t>& pivot);
static bool CmpSubstitution(
int m, int n, std::vector<std::vector<std::complex<double>>>& M,
std::vector<std::complex<double>>& b,
std::vector<std::complex<double>>& x,
std::vector<size_t>& pivot);
static bool CmpInverse(
int n, std::vector<std::vector<std::complex<double>>>& M,
std::vector<std::vector<std::complex<double>>>& Mi);
static void CmpCharPolyAndAdjoint(
int n,
std::vector<std::vector<std::complex<double>>>& C,
std::vector<std::vector<std::complex<double>>>& I,
std::vector<std::vector<std::complex<double>>>& M,
std::vector<std::vector<std::complex<double>>>& adjoint,
std::vector<std::complex<double>>& a);
static void CmpMatrixKernel(
int m, int n,
std::vector<std::vector<std::complex<double>>>& M,
std::vector<std::vector<std::complex<double>>>& X,
size_t& r);
static void CmpMatrixImage(
int m, int n,
std::vector<std::vector<std::complex<double>>>& M,
std::vector<std::vector<std::complex<double>>>& N,
std::vector<std::vector<std::complex<double>>>& X,
int rank);
};
#include <vector>
class DblLinearAlgebra
{
public:
static void DblPrintMatrix(
int m, int n, std::vector<std::vector<double>>& A);
static void DblAddition(
size_t m, size_t n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
std::vector<std::vector<double>>& C);
static void DblSubtraction(
size_t m, size_t n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
std::vector<std::vector<double>>& C);
static void DblMultiply(
size_t m, size_t n, size_t p,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
std::vector<std::vector<double>>& C);
static void DblAnticommutator(
size_t n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
std::vector<std::vector<double>>& C);
static void DblCommutator(
size_t n,
std::vector<std::vector<double>>& A,
std::vector<std::vector<double>>& B,
std::vector<std::vector<double>>& C);
static double DblDeterminant(
bool& failure, int n,
std::vector<std::vector<double>>& A);
static bool DblGaussianElimination(
int m, int n, std::vector<std::vector<double>>& A,
std::vector<double>& b, std::vector<double>& x,
std::vector<size_t>& pivot);
static bool DblGaussianFactor(
int n, std::vector<std::vector<double>>& M,
std::vector<size_t>& pivot);
static bool DblGaussianSolution(
int n, std::vector<std::vector<double>>& M,
std::vector<double>& b, std::vector<double>& x,
std::vector<size_t>& pivot);
static bool DblSubstitution(
int n, std::vector<std::vector<double>>& M,
std::vector<double>& b, std::vector<double>& x,
std::vector<size_t>& pivot);
static bool DblInverse(
int n, std::vector<std::vector<double>>& M,
std::vector<std::vector<double>>& A);
static void DblCharPolyAndAdjoint(
int n,
std::vector<std::vector<double>>& C,
std::vector<std::vector<double>>& I,
std::vector<std::vector<double>>& M,
std::vector<std::vector<double>>& adjoint,
std::vector<double>& a);
static void DblMatrixKernel(
int m, int n,
std::vector<std::vector<double>>& M,
std::vector<std::vector<double>>& X,
size_t& r);
static void DblMatrixImage(
int m, int n,
std::vector<std::vector<double>>& M,
std::vector<std::vector<double>>& N,
std::vector<std::vector<double>>& X,
int rank);
};
// Exercises from "Modern Quantum Chemistry An Introduction to Advanced
// Electronic Structure Theory" by Attila Szabo and Neil S. Ostlund
// https://chemistlibrary.wordpress.com/wp-content/uploads/2015/02/modern-quantum-chemistry.pdf
// Program (c) Tuesday, August 12, 2025 by James Pate Williams, Jr.
#include <complex>
#include <iomanip>
#include <iostream>
#include <vector>
#include "DblLinearAlgebra.h"
#include "CmpLinearAlgebra.h"
int main()
{
double AArcb[3][3] = { { 2, 3, -1 }, { 4, 4, -3 }, { -2, 3, -1 } };
double AArso[3][3] = { { 1, 1, 0 }, { 1, 2, 2 }, { 0, 2, -1 } };
double BBrso[3][3] = { { 1, -1, 1 }, { -1, 0, 0 }, { 1, 0, 1} };
double BBr[3][3] = { { 1, -1, 1 }, { -1 , 0, 0 }, { 1, 0, 1 } };
double AAcr[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
double AAci[3][3] = { { 1, 1, 2 }, { 3, 0, 1 }, { 0, 2, 4 } };
double BBcr[3][3] = { { 1, 0, 1 }, { 1 , 1, 0 }, { 0, 1, 1 } };
double BBci[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int m = 3, n = 3, p = 3;
std::vector<double> br(3);
std::vector<size_t> pivot(3);
std::vector<std::vector<double>> Arcb(3, std::vector<double>(3));
std::vector<std::vector<double>> Arso(3, std::vector<double>(3));
std::vector<std::vector<double>> Brso(3, std::vector<double>(3));
std::vector<std::vector<double>> Br(3, std::vector<double>(3));
std::vector<std::vector<double>> Cr(3, std::vector<double>(3));
std::vector<std::vector<double>> Ai(3, std::vector<double>(3));
std::vector<std::vector<double>> Ari(3, std::vector<double>(3));
std::vector<std::vector<std::complex<double>>> Ac(3,
std::vector<std::complex<double>>(3));
std::vector<std::vector<std::complex<double>>> Bc(3,
std::vector<std::complex<double>>(3));
std::vector<std::vector<std::complex<double>>> Cc(3,
std::vector<std::complex<double>>(3));
std::vector<std::vector<std::complex<double>>> Dc(3,
std::vector<std::complex<double>>(3));
std::vector<std::vector<std::complex<double>>> Ec(3,
std::vector<std::complex<double>>(3));
std::vector<std::vector<std::complex<double>>> Fc(3,
std::vector<std::complex<double>>(3));
std::vector<std::vector<std::complex<double>>> Gc(3,
std::vector<std::complex<double>>(3));
for (int i = 0; i < m; i++)
{
for (int j = 0; j < p; j++)
{
Arcb[i][j] = AArcb[i][j];
Arso[i][j] = AArso[i][j];
Brso[i][j] = BBrso[i][j];
Ac[i][j]._Val[0] = AAcr[i][j];
Ac[i][j]._Val[1] = AAci[i][j];
}
}
for (int i = 0; i < p; i++)
{
for (int j = 0; j < n; j++)
{
Br[i][j] = BBr[i][j];
Bc[i][j]._Val[0] = BBcr[i][j];
Bc[i][j]._Val[1] = BBci[i][j];
}
}
DblLinearAlgebra::DblMultiply(3, 3, 3, Arcb, Br, Cr);
std::cout << "Ar * Br = Cr Conte & de Boor" << std::endl;
DblLinearAlgebra::DblPrintMatrix(3, 3, Cr);
std::cout << std::endl;
CmpLinearAlgebra::CmpMultiply(3, 3, 3, Ac, Bc, Cc);
std::cout << "Ac * Bc = Cc" << std::endl;
CmpLinearAlgebra::CmpPrintMatrix(3, 3, Cc);
std::cout << std::endl;
// Exercise 1.2
std::cout << "Exercise 1.2 page 5 Commutator" << std::endl;
DblLinearAlgebra::DblCommutator(3, Arso, Brso, Cr);
DblLinearAlgebra::DblPrintMatrix(3, 3, Cr);
std::cout << std::endl;
std::cout << "Exercise 1.2 page 5 Anticommutator" << std::endl;
DblLinearAlgebra::DblAnticommutator(3, Arso, Brso, Cr);
DblLinearAlgebra::DblPrintMatrix(3, 3, Cr);
std::cout << std::endl;
CmpLinearAlgebra::CmpAdjoint(3, 3, Cc, Dc);
std::cout << "Exercise 1.3 page 6 Cc adjoint" << std::endl;
CmpLinearAlgebra::CmpPrintMatrix(3, 3, Dc);
std::cout << std::endl;
CmpLinearAlgebra::CmpAdjoint(3, 3, Ac, Ec);
CmpLinearAlgebra::CmpAdjoint(3, 3, Bc, Fc);
CmpLinearAlgebra::CmpMultiply(3, 3, 3, Fc, Ec, Gc);
std::cout << "Exercise 1.3 page 6 Bc adjoint * Ac adjoint" << std::endl;
CmpLinearAlgebra::CmpPrintMatrix(3, 3, Gc);
std::cout << std::endl;
std::cout << "Ar matrix" << std::endl;
DblLinearAlgebra::DblPrintMatrix(3, 3, Arcb);
bool inv = DblLinearAlgebra::DblInverse(n, Arcb, Ai);
std::cout << std::endl;
std::cout << "Ar Conte & de Boor inverse = " << inv << std::endl;
DblLinearAlgebra::DblPrintMatrix(3, 3, Ai);
std::cout << std::endl;
std::cout << "Ar * Ar inverse" << std::endl;
DblLinearAlgebra::DblMultiply(3, 3, 3, Arcb, Ai, Ari);
DblLinearAlgebra::DblPrintMatrix(3, 3, Ari);
std::cout << std::endl;
std::cout << "Ac" << std::endl;
CmpLinearAlgebra::CmpPrintMatrix(3, 3, Ac);
std::cout << std::endl;
inv = CmpLinearAlgebra::CmpInverse(3, Ac, Bc);
std::cout << "Ac inverse = " << inv << std::endl;
CmpLinearAlgebra::CmpPrintMatrix(3, 3, Bc);
CmpLinearAlgebra::CmpMultiply(3, 3, 3, Ac, Bc, Cc);
std::cout << std::endl;
std::cout << "Ac * AC inverse" << std::endl;
CmpLinearAlgebra::CmpPrintMatrix(3, 3, Cc);
}
Blog Entry © Sunday, July 27, 2025, A Bit of Programming Nostalgia Prime Number Related Programs by James Williams, Jr.
/*
Author: Pate Williams c 1995
The following program is a solution to problem 18.15
in Pascalgorithms by Edwin D. Reilly and Francis D.
Federighi page 627. The program uses Simpson's rule
to calculate the number of primes less than or equal
a given number.
*/
#include <math.h>
#include <stdio.h>
typedef double real;
static real f(real x)
{
return(1.0 / log(x));
}
static real simpson(int n, real a, real b)
{
int i;
real evensum, h, oddsum, twoh, x;
if (n % 2 == 1) n = n - 1;
h = (b - a) / n;
twoh = h + h;
x = h + a;
oddsum = 0.0;
for (i = 1; i <= n / 2; i++)
{
oddsum += f(x);
x = twoh + x;
}
x = twoh + a;
evensum = 0.0;
for (i = 1; i <= n / 2 - 1; i++)
{
evensum += f(x);
x = twoh + x;
}
return(h / 3.0 * (f(a) + f(b) + 4.0 * oddsum + 2.0 * evensum));
}
int main(void)
{
int i, n, Nmaximum = 0, Nminimum = 0, Nstep;
printf("n = "); scanf_s("%d", &n);
printf("N minimum = "); scanf_s("%d", &Nminimum);
printf("N maximum = "); scanf_s("%d", &Nmaximum);
printf("N step = "); scanf_s("%d", &Nstep);
printf("\n");
printf("----------------------------------------\n");
printf("Min\t\tMax\t\tprimes\n");
printf("----------------------------------------\n");
for (i = Nminimum; i <= Nmaximum; i += Nstep)
{
printf("%8d\t%8d\t%8.0lf\n", Nminimum, i + Nstep,
simpson(n, Nminimum, i + Nstep));
}
printf("----------------------------------------\n");
return(0);
}
n = 1024
N minimum = 0
N maximum = 10000000
N step = 1000000
----------------------------------------
Min Max primes
----------------------------------------
0 1000000 78551
0 2000000 148923
0 3000000 216788
0 4000000 283122
0 5000000 348361
0 6000000 412754
0 7000000 476461
0 8000000 539590
0 9000000 602224
0 10000000 664424
0 11000000 726239
----------------------------------------
D:\PrimeCounter\x64\Release\PrimeCounter.exe (process 51884) exited with code 0 (0x0).
Press any key to close this window . . .
/*
Author: Pate Williams c 1995
The following is a translation of the Pascal program
sieve found in Pascalgorithms by Edwin D. Reilly and
Francis D. Federighi page 652. This program uses sets
to represent the sieve (see C Programming Language An
Applied Perspective by Lawrence Miller and Alec Qui-
lici pages 160 - 162).
*/
#include <math.h>
#include <stdio.h>
#define _WORD_SIZE 32
#define _VECT_SIZE 524288
#define SET_MIN 0
#define SET_MAX 16777215
typedef unsigned long SET[_VECT_SIZE];
typedef long ELEMENT;
typedef unsigned long LONG;
SET set;
static int get_bit_pos(int* long_ptr, int* bit_ptr,
ELEMENT element)
{
*long_ptr = element / _WORD_SIZE;
*bit_ptr = element % _WORD_SIZE;
return(element >= SET_MIN && element <= SET_MAX);
}
static void set_bit(ELEMENT element, int inset)
{
int bit, word;
if (get_bit_pos(&word, &bit, element))
{
if (inset > 0)
set[word] |= (01 << bit);
else
set[word] &= ~(01 << bit);
}
}
static int get_bit(ELEMENT element)
{
int bit, word;
return(get_bit_pos(&word, &bit, element) ?
(set[word] >> bit) & 01 : 0);
}
static void set_Add(ELEMENT element)
{
set_bit(element, 1);
}
static void set_Del(ELEMENT element)
{
set_bit(element, 0);
}
static int set_Mem(ELEMENT element)
{
return get_bit(element);
}
static void primes(long n)
{
long c, i, inc, k;
double x;
set_Add(2);
for (i = 3; i <= n; i++)
if ((i + 1) % 2 == 0)
set_Add(i);
else
set_Del(i);
c = 3;
do
{
i = c * c;
inc = c + c;
while (i <= n)
{
set_Del(i);
i = i + inc;
}
c += 2;
while (set_Mem(c) == 0) c += 1;
} while (c * c <= n);
k = 0;
for (i = 2; i <= n; i++)
if (set_Mem(i) == 1) k++;
x = n / log(n) - 5.0;
x = x + exp(1.0 + 0.15 * log(n) * sqrt(log(n)));
printf("%8ld\t%8ld\t%8.0lf\n", n, k, x);
}
int main(void)
{
long n = 100L;
printf("----------------------------------------\n");
printf("n\t\tprimes\t\ttheory\n");
printf("----------------------------------------\n");
do
{
primes((int)n);
n = 10L * n;
} while (n < (long)SET_MAX);
printf("----------------------------------------\n");
return(0);
}
----------------------------------------
n primes theory
----------------------------------------
100 25 29
1000 168 181
10000 1229 1261
100000 9592 9634
1000000 78498 78396
10000000 664579 665060
----------------------------------------
D:\Sieve\x64\Release\Sieve.exe (process 60092) exited with code 0 (0x0).
Press any key to close this window . . .
Blog Entry (c) Tuesday, June 3, 2025, Sorting Algorithms by James Pate Williams, Jr.
First, we make sure the sorts actually work as expected and then we do some timing tests.
^^ Integer Data ^^
** Options Menu **
1 Single Sorting Tests
2 Statistical Tests
3 Create log file and log events
4 Exit
Option number: 1
-- Single Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number: 1
Insertion Sort
PRNG Seed 1
Number of Samples 20
Maximum Sample Value 20
11 -17
-16 -16
9 -14
-17 -12
-14 -7
17 0
10 0
0 3
7 6
3 7
8 7
0 8
-7 9
11 10
-12 11
13 11
7 12
12 13
16 16
6 17
-- Single Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number: 2
std::qsort
PRNG Seed 1
Number of Samples 20
Maximum Sample Value 20
11 -17
-16 -16
9 -14
-17 -12
-14 -7
17 0
10 0
0 3
7 6
3 7
8 7
0 8
-7 9
11 10
-12 11
13 11
7 12
12 13
16 16
6 17
-- Single Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number: 3
Singleton's FORTRAN Sort
PRNG Seed 1
Number of Samples 20
Maximum Sample Value 20
11 -17
-16 -16
9 -14
-17 -12
-14 -7
17 0
10 0
0 3
7 6
3 7
8 7
0 8
-7 9
11 10
-12 11
13 11
7 12
12 13
16 16
6 17
-- Single Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number:
^^ Integer Data ^^
** Options Menu **
1 Single Sorting Tests
2 Statistical Tests
3 Create log file and log events
4 Exit
Option number: 2
-- Statistical Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number: 1
Insertion Sort
PRNG Seed 1
Number of Samples 1000
Maximum Sample Value 1000
Number of Experiments 100
Runtimes in microseconds
Minimum runtime 524
Maximum runtime 1751
Mean runtime 802
Median runtime 668
-- Statistical Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number: 2
std::qsort
PRNG Seed 1
Number of Samples 1000
Maximum Sample Value 1000
Number of Experiments 100
Runtimes in microseconds
Minimum runtime 115
Maximum runtime 1751
Mean runtime 481
Median runtime 391
-- Statistical Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number: 3
Singleton's FORTRAN Sort
PRNG Seed 1
Number of Samples 1000
Maximum Sample Value 1000
Number of Experiments 100
Runtimes in microseconds
Minimum runtime 93
Maximum runtime 1751
Mean runtime 363
Median runtime 174
-- Statistical Sorting Tests --
1 Insertion Sort
2 std::qsort
3 Singleton's FORTRAN Sort
4 Exit Submenu
5 Exit Program
Sort option number:
; Copilot and James Pate Williams, Jr.
; 2/8/2025 - 2/9/2025
; We use the eax register for array indices
; The array base in register ecx
; The register ebx is general purpose;
;
;class SortingCPP {
;public:
; static void InsertionSort(std::vector<T>& a)
; {
; for (size_t j = 1; j < a.size(); j++)
; {
; T key = a[j];
; int i = j - 1;
;
; while (i >= 0 && a[i] > key)
; {
; a[i + 1] = a[i];
; i--;
; }
;
; a[i + 1] = key;
; }
; };
.MODEL FLAT, C
.STACK 4096
.DATA
; Allocate space for uninitialized variables
i DWORD ?
j DWORD ?
key DWORD ?
n DWORD ?
t DwORD ?
.CODE
InsertionSortASM PROC
; Parameters:
; array = [esp + 8]
; n = [esp + 12]
push ebp
mov ebp, esp
sub esp, 16 ; Allocate space for local variables
mov ecx, [ebp + 8] ; base of array
mov eax, [ebp + 12] ; n number of array elements
mov [n], eax ; store n
; Initialize variables
mov dword ptr [i], 0 ; i = 0
mov dword ptr [j], 1 ; j = 1
for_loop:
mov eax, [j] ; load j into register
mov ebx, [n] ; load n
cmp eax, ebx ; compare j to n
je Exit ; we are done
mov ebx, [ecx + eax * 4] ; ebx = a[j]
mov [key], ebx ; key = a[j]
dec eax ; j = j - 1
mov [i], eax; ; i = j - 1
inc eax ; increment
inc eax ; j = j + 1
mov [j], eax ; store j
while_loop:
mov eax, [i] ; load i into register
cmp eax, -1 ; is i == -1 ?
jz end_while ; end the while loop
mov ebx, [ecx + eax * 4] ; load a[i]
mov eax, [key] ; load key into register
cmp ebx, eax ; compare a[i] to key
jle end_while ; end the while loop
mov eax, [i] ; load i
mov ebx, [ecx + eax * 4] ; load a[i]
inc eax ; eax = i + 1
mov edx, [ecx + eax * 4] ; load a[i + 1]
;mov [t], ebx ; t = a[i]
mov edx, ebx ; edx = a[i]
mov eax, [i] ; load i again
inc eax ; i + 1
mov [ecx + eax * 4], edx ; a[i + 1] = a[i]
dec eax ; i--
dec eax ; i--
mov [i], eax ; store updated i
jmp while_loop ; continue while
end_while:
mov eax, [i] ; load i
inc eax ; eax = i + 1
mov ebx, [key] ; ebx = key
mov [ecx + eax * 4], ebx ; a[i + 1] = key
jmp for_loop ; continue for loop
Exit:
mov esp, ebp
pop ebp
ret
InsertionSortASM ENDP
END
Rice-Golomb Encoder and Decoder Copyright (c) Thursday, April 3, 2025, to Sunday, April 6, 2025, by James Pate Williams, Jr. BA, BS, Master of Software Engineering, Doctor of Philosophy Computer Science
Online references:
https://en.wikipedia.org/wiki/Golomb_coding
// Rice-Golomb Encoder and Decoder
// Copyright (c) Thursday, April 3, 2025
// by James Pate Williams, Jr.
// BA, BS, Master of Software Engineering
// Doctor of Philosophy Computer Science
// Online references:
// https://en.wikipedia.org/wiki/Golomb_coding
// https://ntrs.nasa.gov/api/citations/19790014634/downloads/19790014634.pdf
#include <iostream>
#include <string>
#include <vector>
//#include <stdlib.h>
bool Encode(const char* NChars, size_t NCharsCount,
long M, long long& N, std::vector<char>& qBits,
std::vector<char>& rBits, unsigned int& qSize, unsigned int& rSize,
long long& q, long long& r, unsigned int& NSize) {
N = NChars[0] - (long long)'0';
for (unsigned int i = 1; i < NCharsCount; i++) {
N = 10 * N + (long long)NChars[i] - (long long)'0';
}
q = N / M;
r = N % M;
qSize = 0;
while (qSize < q) {
qBits.push_back('1');
qSize++;
}
qBits.push_back('0');
qSize++;
rSize = 0;
unsigned int b = (unsigned int)floor(log2(M));
if (b > 62) {
return false;
}
long long p = (long long)pow(2, b + 1);
if (r < p - M) {
long long rr = r;
while (rr > 0) {
long long digit = (rr & 1) == 1 ? 1 : 0;
rBits.push_back((char)digit + '0');
rSize++;
rr >>= 1;
}
rBits.push_back('0');
rSize++;
}
else {
long long rr = r + p - M;
while (rSize < b + 1) {
long long digit = rr & 1 ? 1 : 0;
rBits.push_back((char)digit + '0');
rSize++;
rr >>= 1;
}
}
long long rValue = rBits[0];
for (size_t i = 1; i < rSize; i++) {
rValue = rValue * 2 + rBits[i];
}
long long NBitCount = 0;
while (N > 0) {
N >>= 1;
NBitCount++;
}
std::cout << "q-bits size = " << qSize << std::endl;
std::cout << "r-bits size = " << rSize << std::endl;
std::cout << "N-bits size = " << qSize + rSize << std::endl;
std::cout << "N-Chars * 8-Bits per Char = " << NCharsCount * 8 << std::endl;
std::cout << "% Compression = " << 100.0 * (1.0 - (qSize + rSize) /
(NCharsCount * 8.0)) << std::endl;
return true;
}
void Decode(long long M, long long& N,
std::vector<char> qBits, std::vector<char> rBits,
unsigned int& qSize, unsigned int& rSize,
long long& q, long long& r) {
int count = 0;
while (qBits[count] != '0') {
count++;
}
q = count;
int c = (int)rSize - 1;
unsigned int b = (unsigned int)floor(log2(M));
long long p = (long long)pow(2, b + 1);
long long s = 0;
r = rBits[c--] - (long long)'0';
do {
r = 2 * r + rBits[c] - (long long)'0';
c--;
} while (c >= 0);
if (r < p - M) {
s = r;
}
else {
s = r + p - M;
c = 1;
r = rBits[0] - (long long)'0';
while (c < (int)(b + 1)) {
r = 2 * r + rBits[c] - (long long)'0';
c++;
}
s = r;
}
r = s;
N = q * M + r;
}
int main() {
char line[128] = { };
size_t NSize = 0, qSize = 0, rSize = 0;
long long M = 10, N = 42, q = -1, r = -1;
std::vector<char> qBits, rBits;
std::cout << "M = ";
std::cin.getline(line, 127);
std::string str1(line);
M = std::stoi(str1);
std::cout << "N = ";
std::cin.getline(line, 127);
std::string str2(line);
Encode(str2.c_str(), strlen(str2.c_str()), M, N,
qBits, rBits, qSize, rSize, q, r, NSize);
std::cout << "q = " << q << std::endl;
std::cout << "r = " << r << std::endl;
std::cout << "q-size = " << qSize << std::endl;
std::cout << "r-size = " << rSize << std::endl;
std::cout << "q ";
for (unsigned int i = 0; i < qSize; i++) {
std::cout << qBits[i] << ' ';
}
std::cout << std::endl;
std::cout << "r ";
for (int i = (int)rSize - 1; i >= 0; i--) {
std::cout << rBits[i] << ' ';
}
std::cout << std::endl;
Decode(M, N, qBits, rBits, qSize, rSize, q, r);
std::cout << "q = " << q << std::endl;
std::cout << "r = " << r << std::endl;
std::cout << "q-size = " << qSize << std::endl;
std::cout << "r-size = " << rSize << std::endl;
std::cout << "q ";
for (unsigned int i = 0; i < qSize; i++) {
std::cout << qBits[i] << ' ';
}
std::cout << std::endl;
std::cout << "r ";
for (int i = rSize - 1; i >= 0; i--) {
std::cout << rBits[i] << ' ';
}
std::cout << std::endl;
std::cout << "N = " << N << std::endl;
return 0;
}
M = 64
N = 1027
q-bits size = 17
r-bits size = 3
N-bits size = 20
N-Chars * 8-Bits per Char = 32
% Compression = 37.5
q = 16
r = 3
q-size = 17
r-size = 3
q 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
r 0 1 1
q = 16
r = 3
q-size = 17
r-size = 3
q 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
r 0 1 1
N = 1027
Chapter One Straight-Line Program Interpreter from “Modern Compiler Implementation in Java Second Edition” (c) 2002 by Andrew W. Appel, Translation to C++ by James Pate Williams, Jr. on Thursday, April 3, 2025
Wayback in the Spring Semester of 2006, after I was awarded my Doctor of Philosophy Degree in Computer Science, I partially audited a Compiler Design Course. Due to my mental aberrations, I was unable to complete the course. The instructor was on a Sabbatical from the United States Air Force Academy in Colorado Springs, Colorado. The textbook we used, and I still have a copy, was “Modern Compiler Implementation in Java Second Edition” © 2002 by Andrew W. Appel. Below is a translation from Java to C++ that I just completed.
// Chapter One program translated from Java to C++ by
// James Pate Williams, Jr. (c) Wednesday April 3, 2025
// Reference: "Modern Complier Implementation in Java
// Second Edition" (c) 2002 by Andrew W. Appel
#ifndef _SLPInterpreter_H
#include <iostream>
#include <stack>
#include <string>
#include <vector>
class TableEntry {
public:
std::string symbol, value;
TableEntry(std::string symbol, std::string value) {
this->symbol = symbol;
this->value = value;
}
};
std::stack<std::string> sStack;
std::vector<TableEntry> symbolTable;
class Exp {
public:
Exp() { };
virtual ~Exp() { };
};
std::stack<Exp> eStack;
class ExpList {
public:
ExpList() { };
virtual ~ExpList() { };
};
class Stm {
public:
Stm() { };
virtual ~Stm() { };
};
class CompoundStm : public Stm {
public:
Stm stm1, stm2;
CompoundStm(Stm stm1, Stm stm2) {
this->stm1 = stm1;
this->stm2 = stm2;
};
};
class AssignStm : public Stm {
public:
std::string id;
Exp exp;
AssignStm(std::string id, Exp exp) {
this->id = id;
this->exp = exp;
bool found = false;
for (int i = 0; !found && i < (int)symbolTable.size(); i++) {
if (symbolTable[i].symbol == id) {
found = true;
}
}
if (!found) {
symbolTable.push_back(TableEntry(id, ""));
}
};
void Print() {
std::cout << this->id << ' ';
};
};
class PrintStm : public Stm {
public:
ExpList exps;
PrintStm(ExpList exps) {
this->exps = exps;
};
};
class IdExp : public Exp {
public:
std::string id;
IdExp(std::string id) {
this->id = id;
Print();
TableEntry te(id, "");
};
void Print() {
std::cout << id << ' ';
};
};
class NumExp : public Exp {
public:
int num;
NumExp(int num) {
this->num = num;
Print();
char buffer[128] = { };
_itoa_s(num, buffer, 127, 10);
sStack.push(std::string(buffer));
};
void Print() {
std::cout << num << ' ';
};
};
enum class ArithmeticOp {
Plus, Minus, Times, Div
};
class OpExp : public Exp {
public:
Exp left, right;
ArithmeticOp op;
OpExp(Exp left, ArithmeticOp op, Exp right) {
this->left = left;
this->op = op;
this->right = right;
std::string ops = "";
switch (op) {
case ArithmeticOp::Plus:
ops = "+";
break;
case ArithmeticOp::Minus:
ops = "-";
break;
case ArithmeticOp::Times:
ops = "*";
break;
case ArithmeticOp::Div:
ops = "/";
break;
};
std::cout << ops << std::endl;
eStack.push(left);
eStack.push(right);
sStack.push(ops);
};
};
class EseqExp : public Exp {
public:
Stm stm; Exp exp;
EseqExp(Stm stm, Exp exp) {
this->stm = stm;
this->exp = exp;
};
};
class PairExpList : public ExpList {
public:
Exp head;
ExpList tail;
PairExpList(Exp head, ExpList tail) {
this->head = head;
this->tail = tail;
};
};
class LastExpList : public ExpList {
public:
Exp head;
LastExpList(Exp head) {
this->head = head;
};
};
#endif _SLPInterpreter_H
int main() {
int a = 0, b = 0;
Stm prog(CompoundStm(AssignStm("a",
OpExp(NumExp(5), ArithmeticOp::Plus, NumExp(3))),
CompoundStm(AssignStm("b",
EseqExp(PrintStm(PairExpList(IdExp("a"),
LastExpList(OpExp(IdExp("a"),
ArithmeticOp::Minus, NumExp(1))))),
OpExp(NumExp(10), ArithmeticOp::Times, IdExp("a")))),
PrintStm(LastExpList(IdExp("b"))))));
bool first = true;
int result = 0;
//sStack.push("0");
while (!sStack.empty()) {
std::string lts, ops, rts;
if (first) {
ops = sStack.top();
sStack.pop();
lts = sStack.top();
sStack.pop();
rts = sStack.top();
sStack.pop();
first = false;
}
else {
lts = sStack.top();
sStack.pop();
ops = sStack.top();
sStack.pop();
rts = sStack.top();
sStack.pop();
}
int lvi = std::stoi(lts);
int rvi = std::stoi(rts);
if (ops == "+") {
result = lvi + rvi;
}
else if (ops == "-") {
result = lvi - rvi;
}
else if (ops == "*") {
result = lvi * rvi;
}
else if (ops == "/") {
result = lvi / rvi;
}
char ascii[128] = { };
_itoa_s(result, ascii, 10);
if (sStack.size() != 0) {
sStack.push(std::string(ascii));
}
}
std::cout << "Result = " << result << std::endl;
return 0;
}
Blog Entry © Sunday, March 29, 2025, by James Pate Williams, Jr., BA, BS, Master of Software Engineering, PhD Slater Determinant Coefficients for Z = 2 to 4
Enter the atomic number Z (2 to 6 or 0 to quit): 2
2 1 1 + a(1)b(2)
1 0 0 - a(2)b(1)
# Even Permutations = 1
Enter the atomic number Z (2 to 6 or 0 to quit): 3
6 3 1 + a(1)b(2)c(3)
5 2 0 - a(1)b(3)c(2)
4 2 0 - a(2)b(1)c(3)
3 1 1 + a(2)b(3)c(1)
2 1 1 + a(3)b(1)c(2)
1 0 0 - a(3)b(2)c(1)
# Even Permutations = 3
Enter the atomic number Z (2 to 6 or 0 to quit): 4
24 12 0 + a(1)b(2)c(3)d(4)
23 11 1 - a(1)b(2)c(4)d(3)
22 11 1 - a(1)b(3)c(2)d(4)
21 10 0 + a(1)b(3)c(4)d(2)
20 10 0 + a(1)b(4)c(2)d(3)
19 9 1 - a(1)b(4)c(3)d(2)
18 9 1 - a(2)b(1)c(3)d(4)
17 8 0 + a(2)b(1)c(4)d(3)
16 8 0 + a(2)b(3)c(1)d(4)
15 7 1 - a(2)b(3)c(4)d(1)
14 7 1 - a(2)b(4)c(1)d(3)
13 6 0 + a(2)b(4)c(3)d(1)
12 6 0 + a(3)b(1)c(2)d(4)
11 5 1 - a(3)b(1)c(4)d(2)
10 5 1 - a(3)b(2)c(1)d(4)
9 4 0 + a(3)b(2)c(4)d(1)
8 4 0 + a(3)b(4)c(1)d(2)
7 3 1 - a(3)b(4)c(2)d(1)
6 3 1 - a(4)b(1)c(2)d(3)
5 2 0 + a(4)b(1)c(3)d(2)
4 2 0 + a(4)b(2)c(1)d(3)
3 1 1 - a(4)b(2)c(3)d(1)
2 1 1 - a(4)b(3)c(1)d(2)
1 0 0 + a(4)b(3)c(2)d(1)
# Even Permutations = 12
Enter the atomic number Z (2 to 6 or 0 to quit):
// AOPermutations.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Copyright (c) Saturday, March 29, 2025
// by James Pate Williams, Jr., BA, BS, MSwE, PhD
// Signs of the atomic orbitals in a Slater Determinant
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main()
{
char alpha[] = { 'a', 'b', 'c', 'd', 'e', 'f' }, line[128] = {};
int factorial[7] = { 1, 1, 2, 6, 24, 120, 720 };
while (true)
{
int col = 0, counter = 0, row = 0, sign = 1, t = 0, Z = 0, zfact = 0;
int numberEven = 0;
std::cout << "Enter the atomic number Z (2 to 6 or 0 to quit): ";
std::cin.getline(line, 127);
std::string str(line);
Z = std::stoi(str);
if (Z == 0)
{
break;
}
if (Z < 2 || Z > 6)
{
std::cout << "Illegal Z, please try again" << std::endl;
continue;
}
zfact = factorial[Z];
std::vector<char> orb(Z);
std::vector<int> tmp(Z), vec(Z);
for (int i = 0; i < Z; i++)
{
orb[i] = alpha[i];
vec[i] = i + 1;
}
do
{
for (int i = 0; i < (int)vec.size(); i++)
{
tmp[i] = vec[i];
}
t = 0;
do
{
t++;
} while (std::next_permutation(tmp.begin(), tmp.end()));
std::cout << t << '\t' << t / 2 << '\t';
std::cout << (t / 2 & 1) << '\t';
if (Z == 2 || Z == 3)
{
if ((t / 2 & 1) == 0)
{
std::cout << "-\t";
}
else
{
std::cout << "+\t";
numberEven++;
}
}
else
{
if ((t / 2 & 1) == 1)
{
std::cout << "-\t";
}
else
{
std::cout << "+\t";
numberEven++;
}
}
for (int i = 0; i < Z; i++)
{
std::cout << orb[i] << '(' << vec[i] << ')';
}
row++;
std::cout << std::endl;
if (zfact != 2 && row == zfact)
{
std::cout << std::endl;
break;
}
row %= Z;
} while (std::next_permutation(vec.begin(), vec.end()));
std::cout << "# Even Permutations = ";
std::cout << numberEven << std::endl;
}
return 0;
}
Blog Entry © Thursday, January 23, 2025, by James Pate Williams, Jr. Ackermann’s Super-Exponential Recursive Function in Vanilla C Programming Language
i = 2
j = 1
a(2, 1) =
4
# decimal digits = 1
enter another set (n to quit)? y
i = 2
j = 2
a(2, 2) =
16
# decimal digits = 2
enter another set (n to quit)? y
i = 2
j = 3
a(2, 3) =
65536
# decimal digits = 5
enter another set (n to quit)? y
i = 2
j = 4
a(2, 4) =
200352993040684646497907235156025575044782547556975141926501697371089\
405955631145308950613088093334810103823434290726318182294938211881266886\
950636476154702916504187191635158796634721944293092798208430910485599057\
015931895963952486337236720300291696959215610876494888925409080591145703\
767520850020667156370236612635974714480711177481588091413574272096719015\
183628256061809145885269982614142503012339110827360384376787644904320596\
037912449090570756031403507616256247603186379312648470374378295497561377\
098160461441330869211810248595915238019533103029216280016056867010565164\
...
506264233788565146467060429856478196846159366328895429978072254226479040\
061601975197500746054515006029180663827149701611098795133663377137843441\
619405312144529185518013657555866761501937302969193207612000925506508158\
327550849934076879725236998702356793102680413674571895664143185267905471\
716996299036301554564509004480278905570196832831363071899769915316667920\
895876857229060091547291963638167359667395997571032601557192023734858052\
112811745861006515259888384311451189488055212914577569914657753004138471\
712457796504817585639507289533753975582208777750607233944558789590571915\
6736
# decimal digits = 19729
enter another set (n to quit)?
/*
** Computation of Akermann's super
** exponential function by James
** Pate Williams, Jr. (c) Tuesday,
** August 27, 2024 lip version
*/
#include <stdio.h>
#include "lip.h"
verylong Ackermann(verylong zi, verylong zj) {
verylong a = 0;
if (zscompare(zi, 1) == 0) {
verylong ztwo = 0;
zintoz(2, &ztwo);
zexp(ztwo, zj, &a);
return a;
}
else if (zscompare(zj, 1) == 0)
{
verylong ztwo = 0, ziminus1 = 0;
zintoz(2, &ztwo);
zsadd(zi, -1, &ziminus1);
return Ackermann(ziminus1, ztwo);
}
else if (
zscompare(zi, 2) >= 0 &&
zscompare(zj, 2) >= 0) {
verylong ziminus1 = 0;
verylong zjminus1 = 0;
verylong temp = 0;
zsadd(zi, -1, &ziminus1);
zsadd(zj, -1, &zjminus1);
if (zscompare(ziminus1, 1) >= 0 &&
zscompare(zjminus1, 1) >= 0) {
return
Ackermann(ziminus1, Ackermann(zi, zjminus1));
}
}
return 0;
}
int DigitCount(verylong za) {
int count = 0;
while (zscompare(za, 0) > 0) {
zsdiv(za, 10, &za);
count++;
}
return count;
}
int main(void) {
for (;;) {
char buffer[256] = { '\0' };
int i = 0, j = 0, number = 0;
verylong za = 0, zi = 0, zj = 0;
buffer[0] = '\0';
printf_s("i = ");
scanf_s("%d", &i);
printf_s("j = ");
scanf_s("%d", &j);
zintoz(i, &zi);
zintoz(j, &zj);
printf_s("a(%d, %d) = \n", i, j);
za = Ackermann(zi, zj);
zwriteln(za);
number = DigitCount(za);
printf_s("# decimal digits = %d\n",
number);
printf_s("enter another set (n to quit)? ");
scanf_s("%s", buffer, sizeof(buffer));
zfree(&za);
if (buffer[0] == 'n')
break;
}
return 0;
}