Home /Modules/math/SLNumericalSolver.sleela

SLNumericalSolver.sleela

lib/math/SLNumericalSolver.sleela · 106 line(s) · sleela

<< lib/math/SLNumericalSolver.sleela
   SLeeLa Standard Library Definition.
   Definition: Defines the SLeeLa standard-library type SLNumericalSolver used within the math library family. >>
#sleela 2.5
material;
< SLeeLa math front-end contract. >
class SLNumericalSolver {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 615;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  String metaDate = "2026-10-16";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-09-28";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  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; }

  double sqrt(double x) {
    if (x <= 0.0) { return 0.0; }
    double g = x;
    int i = 0;
    while (i < 60) { g = (g + x / g) / 2.0; i = i + 1; }
    return g;
  }
  << Solve a linear system A x = b (A row-major n x n) by Gaussian elimination
     with partial pivoting; the solution is written into x. Returns false if the
     matrix is singular. >>
  boolean solveLinear(double[] aIn, double[] bIn, double[] x, int n) {
    double[] a = new double[n * n];
    double[] b = new double[n];
    int i = 0;
    while (i < n * n) { a[i] = aIn[i]; i = i + 1; }
    i = 0;
    while (i < n) { b[i] = bIn[i]; i = i + 1; }
    int col = 0;
    while (col < n) {
      int pivot = col;
      double best = a[col * n + col];
      if (best < 0.0) { best = 0.0 - best; }
      int r = col + 1;
      while (r < n) {
        double v = a[r * n + col];
        if (v < 0.0) { v = 0.0 - v; }
        if (v > best) { best = v; pivot = r; }
        r = r + 1;
      }
      if (a[pivot * n + col] == 0.0) { return false; }
      if (pivot != col) {
        int k = 0;
        while (k < n) { double t = a[col * n + k]; a[col * n + k] = a[pivot * n + k]; a[pivot * n + k] = t; k = k + 1; }
        double tb = b[col]; b[col] = b[pivot]; b[pivot] = tb;
      }
      int rr = col + 1;
      while (rr < n) {
        double f = a[rr * n + col] / a[col * n + col];
        int k = col;
        while (k < n) { a[rr * n + k] = a[rr * n + k] - f * a[col * n + k]; k = k + 1; }
        b[rr] = b[rr] - f * b[col];
        rr = rr + 1;
      }
      col = col + 1;
    }
    int bi = n - 1;
    while (bi >= 0) {
      double s = b[bi];
      int k = bi + 1;
      while (k < n) { s = s - a[bi * n + k] * x[k]; k = k + 1; }
      x[bi] = s / a[bi * n + bi];
      bi = bi - 1;
    }
    return true;
  }
}