@johnhenry/math 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Symbolic.ts CHANGED
@@ -8,6 +8,7 @@ import { FUNC_DERIVATIVE_RULES } from "./differentiation-rules.ts";
8
8
  import { MatrixMath } from "./MatrixMath.ts";
9
9
  import { Numerical } from "./Numerical.ts";
10
10
  import { Rational } from "./Rational.ts";
11
+ import { compileShader, type ShaderOptions, type ShaderResult } from "./Shader.ts";
11
12
  import { SpecialFunctions } from "./SpecialFunctions.ts";
12
13
  import type { Structure } from "./Structure.ts";
13
14
 
@@ -864,6 +865,23 @@ export class Symbolic {
864
865
  return render(typeof expr === "string" ? Symbolic.parse(expr) : expr, 0);
865
866
  }
866
867
 
868
+ /**
869
+ * Compile `expr` to a GLSL ES 3.0 expression over `float` (default) or, with
870
+ * `{ complex: true }`, over `vec2` complex numbers (the helpers it needs, such as
871
+ * `mx_cmul`, are returned in `helpers` and emitted once). Output is deterministic.
872
+ * Refuses nodes with no shader form (`sum`, `product`, `erf`, `gcd`, `lcm`, and anything
873
+ * not closed under the complex numbers in complex mode) with a `ShaderUnsupportedError`.
874
+ * Shader `pow` of a negative base with a non-integer exponent is undefined, unlike JS `NaN`.
875
+ */
876
+ static toGLSL(expr: Expr | string, options: ShaderOptions = {}): ShaderResult {
877
+ return compileShader(typeof expr === "string" ? Symbolic.parse(expr) : expr, "glsl", options);
878
+ }
879
+
880
+ /** As {@link toGLSL}, for WGSL (`f32` / `vec2<f32>`; `select` replaces the ternary). */
881
+ static toWGSL(expr: Expr | string, options: ShaderOptions = {}): ShaderResult {
882
+ return compileShader(typeof expr === "string" ? Symbolic.parse(expr) : expr, "wgsl", options);
883
+ }
884
+
867
885
  /** Render `expr` as a LaTeX string, e.g. `"x/2"` -> `"\\frac{x}{2}"`. */
868
886
  static toLatex(expr: Expr | string): string {
869
887
  return toLatexRec(typeof expr === "string" ? Symbolic.parse(expr) : expr, 0);
package/src/index.ts CHANGED
@@ -100,6 +100,7 @@ export { Rational } from "./Rational.ts";
100
100
  // Numeric cores
101
101
  export * as RealMath from "./RealMath.ts";
102
102
  export { Rotor4 } from "./Rotor4.ts";
103
+ export { type ShaderDialect, type ShaderOptions, type ShaderResult, ShaderUnsupportedError } from "./Shader.ts";
103
104
  export { SpecialFunctions } from "./SpecialFunctions.ts";
104
105
  export { SpecialOperator } from "./SpecialOperator.ts";
105
106
  export * as Statistics from "./Statistics.ts";