diff --git a/src/main.cpp b/src/main.cpp index 58be517..a39ad63 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,39 +2,180 @@ extern "C" { #include } -#include +#include + +void evaluate(WSLINK mlp, const std::string& label, const std::string& expr) { + std::string wrapped = "ToString[" + expr + ", InputForm]"; + + WSPutFunction(mlp, "EvaluatePacket", 1); + WSPutFunction(mlp, "ToExpression", 1); + WSPutString(mlp, wrapped.c_str()); + WSEndPacket(mlp); + WSFlush(mlp); + + int pkt; + while ((pkt = WSNextPacket(mlp)) != RETURNPKT) { + WSNewPacket(mlp); + } + + const char* result = nullptr; + if (WSGetString(mlp, &result)) { + spdlog::info("[{}]\n expr => {}\n result => {}\n", label, expr, result); + WSReleaseString(mlp, result); + } else { + spdlog::error("[{}] Failed to read result", label); + } + WSNewPacket(mlp); +} int main() { - WSEnvironment env = WSInitialize(nullptr); - if (!env) { - spdlog::error("Failed to initialize WSTP environment"); - return 1; - } + WSEnvironment env = WSInitialize(nullptr); + if (!env) { + spdlog::error("Failed to initialize WSTP environment"); + return 1; + } - const char* args[] = { - "client", - "-linkname", "1240@localhost", - "-linkprotocol", "TCPIP", - "-linkmode", "connect" - }; - int err = 0; - - WSLINK mlp = WSOpenArgv(env, - const_cast(args), - const_cast(args) + (sizeof(args)/sizeof(args[0])), - &err); - if (!mlp || err != WSEOK) { - spdlog::error("Failed to open WSTP link (err={})", err); - WSDeinitialize(env); - return 1; - } - - LLU::WSStream stream(mlp); - - stream << 42 << LLU::WS::Flush; - - WSClose(mlp); + const char* args[] = {"client", "-linkname", "1240@localhost", + "-linkprotocol", "TCPIP", "-linkmode", + "connect"}; + int err = 0; + WSLINK mlp = WSOpenArgv( + env, const_cast(args), + const_cast(args) + (sizeof(args) / sizeof(args[0])), &err); + if (!mlp || err != WSEOK) { + spdlog::error("Failed to open WSTP link (err={})", err); WSDeinitialize(env); - spdlog::info("Done"); - return 0; + return 1; + } + + // Drain initial InputName packet + int pkt; + while ((pkt = WSNextPacket(mlp)) != INPUTNAMEPKT) { + WSNewPacket(mlp); + } + WSNewPacket(mlp); + spdlog::info("Kernel ready\n"); + + // ─── BASIC ARITHMETIC ──────────────────────────────────────────────────── + evaluate(mlp, "Addition", "2 + 2"); + evaluate(mlp, "Subtraction", "100 - 37"); + evaluate(mlp, "Multiplication", "123 * 456"); + evaluate(mlp, "Division", "355 / 113"); + evaluate(mlp, "Integer Division", "Quotient[355, 113]"); + evaluate(mlp, "Modulo", "Mod[355, 113]"); + evaluate(mlp, "Power", "2^100"); + evaluate(mlp, "Sqrt", "Sqrt[144]"); + evaluate(mlp, "Nested Arithmetic", "((3 + 5) * 2 - 4) / (7 - 3)^2"); + evaluate(mlp, "Big Factorial", "100!"); + evaluate(mlp, "GCD", "GCD[123456, 789012]"); + evaluate(mlp, "LCM", "LCM[123456, 789012]"); + evaluate(mlp, "Abs", "Abs[-42]"); + evaluate(mlp, "Rational", "1/3 + 1/7 + 1/11"); + evaluate(mlp, "High Precision Pi", "N[Pi, 50]"); + evaluate(mlp, "High Precision E", "N[E, 50]"); + + // ─── ALGEBRA ───────────────────────────────────────────────────────────── + evaluate(mlp, "Expand", "Expand[(x+y+z)^6]"); + evaluate(mlp, "Factor", "Factor[x^6 - 1]"); + evaluate(mlp, "Simplify", "Simplify[(x^2 - 1)/(x - 1)]"); + evaluate(mlp, "FullSimplify", "FullSimplify[Sin[x]^2 + Cos[x]^2]"); + evaluate(mlp, "Solve Quadratic", "Solve[x^2 - 5x + 6 == 0, x]"); + evaluate(mlp, "Solve Quintic", "Solve[x^5 - x - 1 == 0, x]"); + evaluate(mlp, "Reduce", + "Reduce[x^2 + y^2 == 25 && x > 0 && y > 0, {x,y}, Integers]"); + evaluate(mlp, "Groebner Basis", + "GroebnerBasis[{x^2+y^2-1, x*y - 1/2}, {x,y}]"); + evaluate(mlp, "Partial Fractions", "Apart[1/(x^4 - 1)]"); + evaluate(mlp, "Polynomial GCD", "PolynomialGCD[x^4-1, x^3-1]"); + + // ─── CALCULUS ──────────────────────────────────────────────────────────── + evaluate(mlp, "Derivative", "D[x^5 * Sin[x] * E^x, x]"); + evaluate(mlp, "Second Derivative", "D[Sin[x]^3, {x, 2}]"); + evaluate(mlp, "Partial Derivative", "D[x^3*y^2 + Sin[x*y], x, y]"); + evaluate(mlp, "Indefinite Integral", "Integrate[x^3 * Log[x], x]"); + evaluate(mlp, "Definite Integral", "Integrate[Sin[x]^2, {x, 0, Pi}]"); + evaluate(mlp, "Your Integral", + "Integrate[1/(x^4+x^3+x^2+x+1), {x, -Infinity, Infinity}]"); + evaluate(mlp, "Gaussian Integral", + "Integrate[x^10 * Exp[-x^2], {x, -Infinity, Infinity}]"); + evaluate(mlp, "Double Integral", + "Integrate[Sin[x*y], {x, 0, Pi}, {y, 0, Pi}]"); + evaluate(mlp, "Limit", "Limit[Sin[x]/x, x -> 0]"); + evaluate(mlp, "Limit Infinity", "Limit[(1 + 1/n)^n, n -> Infinity]"); + evaluate(mlp, "Series", "Series[Exp[Sin[x]], {x, 0, 8}]"); + evaluate(mlp, "Taylor Log", "Series[Log[1+x], {x, 0, 10}]"); + evaluate(mlp, "Laplace Transform", + "LaplaceTransform[t^3 * E^(-2*t) * Sin[3*t], t, s]"); + evaluate(mlp, "Fourier Transform", "FourierTransform[E^(-x^2/2), x, w]"); + + // ─── ODEs ──────────────────────────────────────────────────────────────── + evaluate(mlp, "ODE Linear", + "DSolve[{y''[x] + 4*y'[x] + 4*y[x] == E^(-2*x), y[0]==1, y'[0]==0}, " + "y[x], x]"); + evaluate(mlp, "ODE Nonlinear", "DSolve[y'[x] == y[x]^2 - x, y[x], x]"); + evaluate(mlp, "ODE System", + "DSolve[{x'[t] == y[t], y'[t] == -x[t]}, {x[t], y[t]}, t]"); + + // ─── LINEAR ALGEBRA ────────────────────────────────────────────────────── + evaluate(mlp, "Determinant", "Det[{{1,2,3},{4,5,6},{7,8,10}}]"); + evaluate(mlp, "Inverse Matrix", "Inverse[{{1,2},{3,4}}]"); + evaluate(mlp, "Eigenvalues", "Eigenvalues[{{1,2,3},{4,5,6},{7,8,9}}]"); + evaluate(mlp, "Eigenvectors", "Eigenvectors[{{2,1},{1,2}}]"); + evaluate(mlp, "Matrix Multiply", "{{1,2},{3,4}} . {{5,6},{7,8}}"); + evaluate(mlp, "Matrix Power", "MatrixPower[{{1,1},{1,0}}, 20]"); + evaluate(mlp, "SVD", "SingularValueList[{{1,2,3},{4,5,6}}]"); + evaluate(mlp, "Null Space", "NullSpace[{{1,2,3},{4,5,6},{7,8,9}}]"); + evaluate(mlp, "Linear Solve", + "LinearSolve[{{2,1,-1},{-3,-1,2},{-2,1,2}}, {8,-11,-3}]"); + + // ─── NUMBER THEORY ─────────────────────────────────────────────────────── + evaluate(mlp, "Factor Integer", "FactorInteger[2^128 + 1]"); + evaluate(mlp, "Mersenne Prime", "PrimeQ[2^127 - 1]"); + evaluate(mlp, "Next Prime", "NextPrime[10^15]"); + evaluate(mlp, "Euler Phi", "EulerPhi[123456789]"); + evaluate(mlp, "Partitions P(100)", "PartitionsP[100]"); + evaluate(mlp, "Bell Number B(20)", "BellB[20]"); + evaluate(mlp, "Fibonacci(1000)", "Fibonacci[1000]"); + evaluate(mlp, "ContinuedFrac Pi", "ContinuedFraction[Pi, 20]"); + + // ─── SERIES / SPECIAL FUNCTIONS ────────────────────────────────────────── + evaluate(mlp, "Zeta(2)", "Sum[1/n^2, {n, 1, Infinity}]"); + evaluate(mlp, "Zeta(4)", "Sum[1/n^4, {n, 1, Infinity}]"); + evaluate(mlp, "Leibniz Pi", "Sum[(-1)^n / (2n+1)^3, {n, 0, Infinity}]"); + evaluate(mlp, "Apery Zeta(3)", "N[Zeta[3], 50]"); + evaluate(mlp, "Euler Gamma", "N[EulerGamma, 50]"); + evaluate(mlp, "Gamma(1/2)", "Gamma[1/2]"); + evaluate(mlp, "Log Gamma Int", "Integrate[Log[Gamma[x]], {x, 0, 1}]"); + evaluate(mlp, "Bessel", "BesselJ[0, 1]"); + evaluate(mlp, "Hypergeometric", "HypergeometricPFQ[{1,1},{2}, -1]"); + + // ─── PROBABILITY ───────────────────────────────────────────────────────── + evaluate(mlp, "Normal PDF", "PDF[NormalDistribution[0,1], x]"); + evaluate(mlp, "E[X^4] Normal", + "Expectation[x^4, x \\[Distributed] NormalDistribution[0,1]]"); + evaluate(mlp, "Binomial Prob", "PDF[BinomialDistribution[20, 1/3], 7]"); + evaluate(mlp, "Poisson CDF", "CDF[PoissonDistribution[5], 3]"); + + // ─── COMPLEX NUMBERS ───────────────────────────────────────────────────── + evaluate(mlp, "Complex Mult", "(3 + 4*I) * (1 - 2*I)"); + evaluate(mlp, "Complex Power", "(1 + I)^20"); + evaluate(mlp, "Euler Identity", "FullSimplify[E^(I*Pi) + 1]"); + evaluate(mlp, "Complex Integrate", + "Integrate[1/(z^2+1), {z, -Infinity, Infinity}]"); + + // ─── GEOMETRY ──────────────────────────────────────────────────────────── + evaluate(mlp, "N-Ball Volumes", + "Table[N[Pi^(n/2)/Gamma[n/2+1], 5], {n, 1, 10}]"); + evaluate(mlp, "Sphere Surface", "4 * Pi * r^2"); + + // ─── LOGIC & COMBINATORICS ─────────────────────────────────────────────── + evaluate(mlp, "Binomial(50,25)", "Binomial[50, 25]"); + evaluate(mlp, "Stirling", "StirlingS2[10, 4]"); + evaluate(mlp, "Derangements", "Subfactorial[10]"); + evaluate(mlp, "Catalan(10)", "CatalanNumber[10]"); + + // Leave kernel alive + WSDeinitialize(env); + spdlog::info("\nAll done — kernel still running"); + return 0; }