MathDemo.sleela
lib/math/examples/MathDemo.sleela · 105 line(s) · sleela
<< lib/math/examples/MathDemo.sleela
SLeeLa Standard Library Definition.
Definition: A runnable tour of the lib/math package exercising a cross-section
of the real implementations: number theory, statistics, linear algebra,
calculus, geometry, a distribution, and a linear solve. Sibling .sleela files
in the package are auto-linked by the runner. >>
#sleela 2.5
material;
class MathDemo {
< ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
<< Baseline identity for a worker-class example. >>
String metaVersion = "1.0.0";
int metaId = 7901;
String metaAuthor = "Max Rupplin";
String metaDate = "2026-11-06";
String metaCopyright = "MEARVK LLC";
String metaTrademark = "none";
String metaCorporation = "MEARVK LLC";
String metaOrigin = "International";
String metaBirthDate = "2026-11-06";
int metaScienceRating = 6;
String metaNationalSchedule = "none";
String getMetaVersion() { return metaVersion; }
void setMetaVersion(String v) { metaVersion = v; }
int getMetaId() { return metaId; }
void setMetaId(int v) { metaId = v; }
String getAuthor() { return metaAuthor; }
void setAuthor(String v) { metaAuthor = v; }
String getDate() { return metaDate; }
void setDate(String v) { metaDate = v; }
String getCopyright() { return metaCopyright; }
void setCopyright(String v) { metaCopyright = v; }
String getTrademark() { return metaTrademark; }
void setTrademark(String v) { metaTrademark = v; }
String getCorporation() { return metaCorporation; }
void setCorporation(String v) { metaCorporation = v; }
String getOrigin() { return metaOrigin; }
void setOrigin(String v) { metaOrigin = v; }
String getBirthDate() { return metaBirthDate; }
void setBirthDate(String v) { metaBirthDate = v; }
int getScienceRating() { return metaScienceRating; }
void setScienceRating(int v) { metaScienceRating = v; }
String getNationalSchedule() { return metaNationalSchedule; }
void setNationalSchedule(String v) { metaNationalSchedule = v; }
void main() {
print("lib/math demo -- real implementations");
SLGcd gcd = new SLGcd();
print("gcd(48,36)=" + gcd.compute(48, 36));
SLPrime prime = new SLPrime();
print("isPrime(97)=" + prime.isPrime(97));
double[] data = new double[5];
data[0] = 2.0; data[1] = 4.0; data[2] = 4.0; data[3] = 4.0; data[4] = 6.0;
SLStandardDeviation sd = new SLStandardDeviation();
print("populationSD=" + sd.population(data, 5));
SLDeterminant det = new SLDeterminant();
double[] m = new double[4];
m[0] = 4.0; m[1] = 7.0; m[2] = 2.0; m[3] = 6.0;
print("det([4 7; 2 6])=" + det.of(m, 2));
SLSimpsonRule simp = new SLSimpsonRule();
double[] ys = new double[5];
int i = 0;
while (i < 5) { double x = 0.5 * i; ys[i] = x * x; i = i + 1; }
print("integral x^2 on [0,2]=" + simp.composite(ys, 0.0, 2.0, 4));
SLGeometry geo = new SLGeometry();
print("triangleArea(3,4 legs)=" + geo.triangleArea(0.0, 0.0, 4.0, 0.0, 0.0, 3.0));
SLNormalDistribution nd = new SLNormalDistribution();
nd.configure(0.0, 1.0);
print("normalCdf(1.96)=" + nd.cdf(1.96));
SLNumericalSolver solver = new SLNumericalSolver();
double[] A = new double[4];
double[] b = new double[2];
double[] xsol = new double[2];
A[0] = 2.0; A[1] = 1.0; A[2] = 1.0; A[3] = 3.0;
b[0] = 5.0; b[1] = 10.0;
solver.solveLinear(A, b, xsol, 2);
print("solve 2x+y=5, x+3y=10 -> x=" + xsol[0] + " y=" + xsol[1]);
<< High-precision rendering: the runtime prints a double with about six
significant figures, but SLNumericFormatter recovers the full precision
of the computed value (about fifteen significant digits). >>
SLNumericFormatter fmt = new SLNumericFormatter();
SLExponential ex = new SLExponential();
SLLogarithm lg = new SLLogarithm();
print("pi (default print) = " + 3.141592653589793);
print("pi (12 places) = " + fmt.toFixed(3.141592653589793, 12));
print("1/3 (12 places) = " + fmt.toFixed(1.0 / 3.0, 12));
print("e (12 places) = " + fmt.toFixed(ex.exp(1.0), 12));
print("ln(2) (12 places) = " + fmt.toFixed(lg.ln(2.0), 12));
SLPrecision prec = new SLPrecision();
print("agreeing digits(1/3, 0.3333333333) = "
+ prec.agreeingDigits(1.0 / 3.0, 0.3333333333));
}
}