From 9597c357a6026c26b6f8558a571a815919d39c25 Mon Sep 17 00:00:00 2001 From: NSoiffer Date: Tue, 21 Jan 2025 17:58:38 -0800 Subject: [PATCH] (More) rules and tests for laplacian, div, curl, gradient. --- Rules/Intent/calculus.yaml | 20 ++++++++-- Rules/Languages/en/SharedRules/calculus.yaml | 20 +++++++--- .../en/SharedRules/linear-algebra.yaml | 20 +++++++--- .../en/SimpleSpeak/linear_algebra.rs | 17 +++++++++ tests/Languages/intent/calculus.rs | 37 ++++++++++++++----- 5 files changed, 91 insertions(+), 23 deletions(-) diff --git a/Rules/Intent/calculus.yaml b/Rules/Intent/calculus.yaml index e115b354..f6cd6106 100644 --- a/Rules/Intent/calculus.yaml +++ b/Rules/Intent/calculus.yaml @@ -1,12 +1,26 @@ --- - + name: "laplacian" + tag: mrow + match: "count(*)=2 and + *[1][self::m:msup and + *[1][ self::m:mo[.='∇' or .='𝛁'] or self::m:mover[*[1][.='∇'] and *[2][.='→' or .='⇀']] ] and + *[2][self::m:mn[.='2']]]" + replace: + - intent: + name: "laplacian" + children: [x: "*[2]"] + +- + # could be stand alone operator -- catch this case name: "laplacian" tag: msup - match: "*[1][self::m:mo[.='∇' or .='𝛁']] and *[2][self::m:mn[.='2']]" + match: "*[1][ self::m:mo[.='∇' or .='𝛁'] or self::m:mover[*[1][.='∇'] and *[2][.='→' or .='⇀']] ] and + *[2][self::m:mn[.='2']]" replace: - intent: name: "laplacian" - children: [x: "*[1]"] # can't be empty, so we include the first child since at least it might be different + children: [] - name: divergence-and-curl @@ -21,7 +35,7 @@ name: gradient tag: mrow match: "count(*)=2 and - *[1][ self::m:mo[.='∇' or .='𝛁'] or self::m:mover[*[1][.='∇' or .='𝛁'] and *[2][.='→' or .='⇀']] ] and + *[1][ self::m:mo[.='∇' or .='𝛁'] or self::m:mover[*[1][.='∇'] and *[2][.='→' or .='⇀']] ] and name(BaseNode(*[2])) != 'mo'" replace: - intent: diff --git a/Rules/Languages/en/SharedRules/calculus.yaml b/Rules/Languages/en/SharedRules/calculus.yaml index 24e2a1fd..4fd02fb5 100644 --- a/Rules/Languages/en/SharedRules/calculus.yaml +++ b/Rules/Languages/en/SharedRules/calculus.yaml @@ -2,13 +2,23 @@ - name: laplacian tag: laplacian - match: "." + match: "count(*) <= 1" # can be on ∇^2 or on enclosing mrow replace: - - t: "laplacian of" # phrase(systems 'of' linear equations) + - t: "LahPlahsian" # phrase('laplacian' of x) -- "LahPlahsian" sounds better with speech engines tested + - test: + if: "count(*) = 1" + then: + - test: + if: "$Verbosity!='Terse'" + then: [t: "of"] # phrase('div' is short for divergence) -- note OneCore voices spell out "div" + - test: + if: "not(IsNode(*[1], 'leaf'))" + then: [pause: short] + - x: "*[1]" - name: divergence tag: divergence - match: "." + match: "count(*) = 1" replace: - test: if: "$Verbosity='Terse'" @@ -21,7 +31,7 @@ - name: curl tag: curl - match: "." + match: "count(*) = 1" replace: - t: "curl" # phrase(the 'curl of' a field) - test: @@ -34,7 +44,7 @@ - name: gradient tag: gradient - match: "." + match: "count(*) = 1" replace: - test: if: "$Verbosity!='Terse'" diff --git a/Rules/Languages/en/SharedRules/linear-algebra.yaml b/Rules/Languages/en/SharedRules/linear-algebra.yaml index ed80d8f7..d310bde0 100644 --- a/Rules/Languages/en/SharedRules/linear-algebra.yaml +++ b/Rules/Languages/en/SharedRules/linear-algebra.yaml @@ -124,15 +124,23 @@ match: "." variables: [OperatorName: "IfThenElse($Verbosity='Terse', 'dot', 'dot product')"] replace: - - insert: - nodes: "*" - replace: [x: "$OperatorName", pause: auto] + - test: + if: "*" + then: + - insert: + nodes: "*" + replace: [x: "$OperatorName", pause: auto] + else: [x: "$OperatorName", pause: auto] - name: default tag: cross-product match: "." variables: [OperatorName: "IfThenElse($Verbosity='Terse', 'cross', 'cross product')"] replace: - - insert: - nodes: "*" - replace: [x: "$OperatorName", pause: auto] + - test: + if: "*" + then: + - insert: + nodes: "*" + replace: [x: "$OperatorName", pause: auto] + else: [x: "$OperatorName", pause: auto] diff --git a/tests/Languages/en/SimpleSpeak/linear_algebra.rs b/tests/Languages/en/SimpleSpeak/linear_algebra.rs index 6e04d1a0..e3e4f2e7 100644 --- a/tests/Languages/en/SimpleSpeak/linear_algebra.rs +++ b/tests/Languages/en/SimpleSpeak/linear_algebra.rs @@ -57,4 +57,21 @@ fn norm_subscripted() { "; test("en", "SimpleSpeak", expr, "p norm of f"); +} + +#[test] +fn not_gradient() { + // the nabla is at the end, so it can't be gradient because it doesn't operate on anything + let expr = r#" + ( + b + + + + + ) + a + +"#; + test("en", "SimpleSpeak", expr, "open paren, b times nahblah, close paren; times eigh"); } \ No newline at end of file diff --git a/tests/Languages/intent/calculus.rs b/tests/Languages/intent/calculus.rs index 4fca3585..c99b750c 100644 --- a/tests/Languages/intent/calculus.rs +++ b/tests/Languages/intent/calculus.rs @@ -6,19 +6,38 @@ use crate::common::*; #[test] fn laplacian() { let mathml = r#" - - - 2 - + 2 ψ "#; let intent = r#" - - - + + ψ - ψ - + "#; + test_intent(mathml, intent, vec![]); +} + +#[test] +fn laplacian_as_vector() { + let mathml = r#" + 2 + ψ + "#; + let intent = r#" + + ψ + + "#; + test_intent(mathml, intent, vec![]); +} + +#[test] +fn laplacian_as_operator() { + let mathml = r#" + 𝛁 2 + "#; + let intent = r#" + "#; test_intent(mathml, intent, vec![]); }