Blog Entry © Monday, May 12, 2025, by James Pate Williams, Jr., BA Chemistry, BS Computer Science, Master of Software Engineering, PhD Computer Science Two Minor Catastrophes

Four Methods of Numerical Double Integration: Sequential Simpson’s Rule, Multitasking Simpson’s Rule, Sequential Monte Carlo and Multitasking Monte Carlo Methods © Wednesday April 16 – 18, 2025, by James Pate Williams, Jr.

Blog Entry (c) Friday, April 11, 2025, by James Pate Williams, Jr. Multiplication and Division of Finite Power Series

x = 0.25
N = 5
Series Cosine(x)  = 0.968912421711
C++ cos(x)        = 0.968912421711
Series sine(x)    = 0.247403959255
C++ sin(x)        = 0.247403959255
Series Tangent(x) = 0.255341921221
C++ tan(x)        = 0.255341921221
Series sin(2x)    = 0.479425538604
C++ sin(2x)       = 0.479425538604
C++ 2sin(x)cos(x) = 0.479425538604
End app ? y = yes = n
x = 0.5
N = 5
Series Cosine(x)  = 0.877582561890
C++ cos(x)        = 0.877582561890
Series sine(x)    = 0.479425538604
C++ sin(x)        = 0.479425538604
Series Tangent(x) = 0.546302489844
C++ tan(x)        = 0.546302489844
Series sin(2x)    = 0.841470984807
C++ sin(2x)       = 0.841470984808
C++ 2sin(x)cos(x) = 0.841470984808
End app ? y = yes = y
x = 0.75
N = 5
Series Cosine(x)  = 0.731688868808
C++ cos(x)        = 0.731688868874
Series sine(x)    = 0.681638760020
C++ sin(x)        = 0.681638760023
Series Tangent(x) = 0.931596460023
C++ tan(x)        = 0.931596459944
Series sin(2x)    = 0.997494986509
C++ sin(2x)       = 0.997494986604
C++ 2sin(x)cos(x) = 0.997494986604
End app ? y = yes = n
 
x = 1.00
N = 5
Series Cosine(x)  = 0.540302303792
C++ cos(x)        = 0.540302305868
Series sine(x)    = 0.841470984648
C++ sin(x)        = 0.841470984808
Series Tangent(x) = 1.557407730344
C++ tan(x)        = 1.557407724655
Series sin(2x)    = 0.909297423159
C++ sin(2x)       = 0.909297426826
C++ 2sin(x)cos(x) = 0.909297426826
End app ? y = yes = n
x = 1.25
N = 5
Series Cosine(x)  = 0.315322332275
C++ cos(x)        = 0.315322362395
Series sine(x)    = 0.948984616456
C++ sin(x)        = 0.948984619356
Series Tangent(x) = 3.009569952151
C++ tan(x)        = 3.009569673863
Series sin(2x)    = 0.598472085108
C++ sin(2x)       = 0.598472144104
C++ 2sin(x)cos(x) = 0.598472144104
End app ? y = yes = n
x = 1.50
N = 5
Series Cosine(x)  = 0.070736934117
C++ cos(x)        = 0.070737201668
Series sine(x)    = 0.997494955682
C++ sin(x)        = 0.997494986604
Series Tangent(x) = 14.101472846329
C++ tan(x)        = 14.101419947172
Series sin(2x)    = 0.141119469924
C++ sin(2x)       = 0.141120008060
C++ 2sin(x)cos(x) = 0.141120008060
End app ? y = yes = y

// DivMulPowerSeries.cpp (c) Tuesday, April 8, 2025
// by James Pate Williams, Jr.
// https://math.libretexts.org/Bookshelves/Calculus/Calculus_(OpenStax)/10%3A_Power_Series/10.02%3A_Properties_of_Power_Series
// https://en.wikipedia.org/wiki/Formal_power_series

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

std::vector<double> Multiplication(
	std::vector<double> c,
	std::vector<double> d,
	int N) {
	std::vector<double> e(N + 1);

	for (int n = 0; n <= N; n++) {
		double sum = 0.0;

		for (int k = 0; k <= n; k++) {
			sum += c[k] * d[n - k];
		}

		e[n] = sum;
	}

	return e;
}

std::vector<double> Division(
	std::vector<double> a,
	std::vector<double> b,
	int N) {
	double a0 = a[0];
	std::vector<double> c(N + 1);

	for (int n = 0; n <= N; n++) {
		double sum = 0.0;

		for (int k = 1; k <= n; k++) {
			sum += a[k] * c[n - k];
		}

		c[n] = (b[n] - sum) / a0;
	}

	return c;
}

double Factorial(int n) {
	double nf = 1.0;

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

	return nf;
}

std::vector<double> Cosine(double x, int N, double& fx) {
	std::vector<double> series(N + 1);

	fx = 0.0;

	for (int n = 0; n <= N; n++) {
		int argument = 2 * n;
		double coeff = pow(-1, n) / Factorial(argument);
		series[n] = coeff;
		fx += coeff * pow(x, argument);
	}

	return series;
}

std::vector<double> Sine(double x, int N, double& fx) {
	std::vector<double> series(N + 1);
	
	fx = 0.0;

	for (int n = 0; n <= N; n++) {
		int argument = 2 * n + 1;
		double coeff = pow(-1, n) / Factorial(argument);
		series[n] = coeff;
		fx += coeff * pow(x, argument);
	}

	return series;
}

std::vector<double> Tangent(double x, int N, double& fx) {
	double fc = 0.0, fs = 0.0;
	std::vector<double> seriesC = Cosine(x, N, fc);
	std::vector<double> seriesS = Sine(x, N, fs);
	std::vector<double> seriesT = Division(seriesS, seriesC, N);
	fx = fs / fc;

	return seriesT;
}

std::vector<double> Sine2x(double x, int N, double& fx) {
	double fc = 0.0, fs = 0.0;
	std::vector<double> seriesC = Cosine(x, N, fc);
	std::vector<double> seriesS = Sine(x, N, fs);
	std::vector<double> series2 = Multiplication(seriesS, seriesC, N);
	fx = 2.0 * fs * fc;

	return series2;
}

int main()
{
	while (true) {
		char line[128] = { };
		std::cout << "x = ";
		std::cin.getline(line, 127);
		std::string str1(line);
		double x = std::stod(str1);
		std::cout << "N = ";
		std::cin.getline(line, 127);
		std::string str2(line);
		int N = std::stoi(str2);
		double cx = 0.0, sx = 0.0, tx = 0.0, xx = 0.0;
		std::vector<double> cSeries = Cosine(x, N, cx);
		std::vector<double> sSeries = Sine(x, N, sx);
		std::vector<double> tSeries = Tangent(x, N, tx);
		std::vector<double> xSeries = Sine2x(x, N, xx);
		std::cout << std::fixed << std::setprecision(12);
		std::cout << "Series Cosine(x)  = " << cx << std::endl;
		std::cout << "C++ cos(x)        = " << cos(x) << std::endl;
		std::cout << "Series sine(x)    = " << sx << std::endl;
		std::cout << "C++ sin(x)        = " << sin(x) << std::endl;
		std::cout << "Series Tangent(x) = " << tx << std::endl;
		std::cout << "C++ tan(x)        = " << tan(x) << std::endl;
		std::cout << "Series sin(2x)    = " << xx << std::endl;
		std::cout << "C++ sin(2x)       = " << sin(x + x) << std::endl;
		std::cout << "C++ 2sin(x)cos(x) = " << 2.0 * sin(x) * cos(x);
		std::cout << std::endl;
		std::cout << "End app ? y = yes = ";
		std::cin.getline(line, 127);

		if (line[0] == 'Y' || line[0] == 'y') {
			break;
		}
	}

	return 0;
}

Blog Entry, Tuesday, April 8, 2025, (c) James Pate Williams, Jr. PROBLEMS from “Mathematical Methods in the Physical Sciences Second Edition” (c) 1983 by Mary L. Boas CHAPTER 1 SECTION 13

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