Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Added Equation and MathML datatype #48

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Added Equation and MathML datatype #48

wants to merge 16 commits into from

Conversation

JR-1991
Copy link
Collaborator

@JR-1991 JR-1991 commented Feb 16, 2024

This pull request adds two new math types to handle and evaluate equations. The first type is called Equation, representing equations as strings. It then converts them to sympy expressions and extracts all symbols. The second type is the MathML implementation, which works similarly to Equation but serializes to MathML. However, the MathML implementation cannot be serialized into JSON due to its structure since the operations are empty elements that are not supported in JSON.

Examples

To use the new datatypes, the following markdown demonstrates the necessary type names:

### KineticLawEq

- kinetic_law
   - Type: Equation

### KineticLawMathML

- kinetic_law
   - Type: MathML

Both types are used in the same way, yet differ in their respective output. Here is a demonstration how both can be used and serialized:

Equation Type

ds = lib.KineticLawEq(kinetic_law="a*x + b")
print(ds.json())
{
  "id": "5c37c618-adb8-4497-972f-8df4fe4c2088",
  "kinetic_law": {
    "equation": "a*x + b",
    "symbols": [
      "x",
      "b",
      "a"
    ]
  }
}

MathML Type

Important

The resulting XML tree will always use the math tag, no matter what the attribute name is. This is necessary to comply to the MathML schema.

ds = lib.KineticLawMathML(kinetic_law="a*x + b")
print(ds.xml())
<?xml version='1.0' encoding='UTF-8'?>
<KineticLaw id="1aed15ed-7cb7-4de5-aea0-c68fa969dc1e">
  <math xmlns="http://www.w3.org/1998/Math/MathML">
    <apply>
      <plus/>
      <apply>
        <times/>
        <ci>a</ci>
        <ci>x</ci>
      </apply>
      <ci>b</ci>
    </apply>
  </math>
</KineticLaw>

Evaluation

Both data types evaluate upon being called and can thus be used for calculations. This can be done in the following way:

ds = lib.KineticLawEq(kinetic_law="a*x + b")
ds.kinetic_law(a=1, b=2, x=3)
5.0

@JR-1991 JR-1991 added the enhancement New feature or request label Feb 16, 2024
@JR-1991 JR-1991 requested a review from haeussma February 16, 2024 22:02
@JR-1991 JR-1991 self-assigned this Feb 16, 2024
@JR-1991 JR-1991 added this to the 0.3.0 milestone Mar 4, 2024
Copy link
Member

@haeussma haeussma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the new types and methods on an existing data model. Works as expected.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants