Skip to content

Commit

Permalink
(More) rules and tests for laplacian, div, curl, gradient.
Browse files Browse the repository at this point in the history
  • Loading branch information
NSoiffer committed Jan 22, 2025
1 parent 3fe7baa commit 9597c35
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
20 changes: 17 additions & 3 deletions Rules/Intent/calculus.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
20 changes: 15 additions & 5 deletions Rules/Languages/en/SharedRules/calculus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand All @@ -21,7 +31,7 @@

- name: curl
tag: curl
match: "."
match: "count(*) = 1"
replace:
- t: "curl" # phrase(the 'curl of' a field)
- test:
Expand All @@ -34,7 +44,7 @@

- name: gradient
tag: gradient
match: "."
match: "count(*) = 1"
replace:
- test:
if: "$Verbosity!='Terse'"
Expand Down
20 changes: 14 additions & 6 deletions Rules/Languages/en/SharedRules/linear-algebra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
17 changes: 17 additions & 0 deletions tests/Languages/en/SimpleSpeak/linear_algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@ fn norm_subscripted() {
</math>
";
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#"<math>
<mo>(</mo>
<mi>b</mi>
<mo>&#x22C5;</mo>
<mrow>
<mo>&#x2207;</mo>
</mrow>
<mo>)</mo>
<mi>a</mi>
</math>
"#;
test("en", "SimpleSpeak", expr, "open paren, b times nahblah, close paren; times eigh");
}
37 changes: 28 additions & 9 deletions tests/Languages/intent/calculus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,38 @@ use crate::common::*;
#[test]
fn laplacian() {
let mathml = r#"<math>
<msup>
<mo>&#x2207;</mo>
<mn>2</mn>
</msup>
<msup> <mo>∇</mo> <mn>2</mn> </msup>
<mi>&#x3C8;</mi>
</math>"#;
let intent = r#"<math data-from-mathml='math' >
<mrow data-from-mathml='mrow' data-changed='added'>
<laplacian data-from-mathml='msup'>
<mo data-from-mathml='mo'>∇</mo>
<laplacian data-from-mathml='mrow' data-changed='added'>
<mi data-from-mathml='mi'>ψ</mi>
</laplacian>
<mi data-from-mathml='mi'>ψ</mi>
</mrow>
</math>"#;
test_intent(mathml, intent, vec![]);
}

#[test]
fn laplacian_as_vector() {
let mathml = r#"<math>
<msup> <mover><mo>∇</mo><mo>&#x2192;</mo></mover> <mn>2</mn> </msup>
<mi>&#x3C8;</mi>
</math>"#;
let intent = r#"<math data-from-mathml='math' >
<laplacian data-from-mathml='mrow' data-changed='added'>
<mi data-from-mathml='mi'>ψ</mi>
</laplacian>
</math>"#;
test_intent(mathml, intent, vec![]);
}

#[test]
fn laplacian_as_operator() {
let mathml = r#"<math>
<msup> <mo>𝛁</mo> <mn>2</mn> </msup>
</math>"#;
let intent = r#"<math data-from-mathml='math' >
<laplacian data-from-mathml='msup' />
</math>"#;
test_intent(mathml, intent, vec![]);
}
Expand Down

0 comments on commit 9597c35

Please sign in to comment.