Tuesday, July 22, 2014

Weekly Log: July 14 - July 20

I am posting one day late this week so I apologize. I have been working on the flattening routine for the arrays package. I implemented the algorithm and the routine is completed in two passes:

1. Flatten dimension and index objects

Essentially, what I am doing to flatten SBase objects with dimension objects is making n copies of the arrayed sbase, where n is the product of each dimension size. For instance, imagine you have this SBML model. For the sake of simplicity, I would write this in a Java-base syntax:

Parameter size = 2;
Parameter X = new Parameter[size][size];

Essentially, I would turn this into:

Parameter size = 2;
Parameter X_0_0;
Parameter X_0_1;
Parameter X_1_0;
Parameter X_1_1;

If an SBase you are flattening has math associated with it and in the math there is a dimension id, then I replace the dimension id for the actual index value. When there is an assignment, I update the id of the SBaseRef. For example:

Parameter size = 2;
Parameter X = new Parameter[size][size];
Parameter Y = new Parameter[size][size];

for (int i  = 0; i < 2; ++i) 
  for (int i  = 0; i < 2; ++i) 
    X[i][j] = Y[j][i];

The above example would be turned into:

Parameter size = 2;
Parameter X_0_0, X_0_1, X_1_0, X_1_1;
Parameter Y_0_0, Y_0_1, Y_1_0, Y_1_1;
X_0_0 = {{Y_0_0, Y_0_1}, {Y_1_0, Y_1_1}}[0][0];
X_0_1 = {{Y_0_0, Y_0_1}, {Y_1_0, Y_1_1}}[1][0];
X_1_0 = {{Y_0_0, Y_0_1}, {Y_1_0, Y_1_1}}[0][1];
X_1_1 = {{Y_0_0, Y_0_1}, {Y_1_0, Y_1_1}}[1][1];

Last, I update the Metaid of each SBase.


2. Flatten vector and selector math

After I flatten dimension and index objects, I evaluate vector and selector MathML objects to be just a scalar value. In the above example, the assignments would be turned into:

X_0_0 = Y_0_0;
X_0_1 = Y_1_0;
X_1_0 = Y_0_1;
X_1_1 = Y_1_1;

Vectors are also evaluated and at the end you end up with scalars only.

Here is an example in SBML:

Arrayed model:
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" arrays:required="true" xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1" version="1">
  <model>
    <listOfParameters>
      <parameter id="n" value="10"/>
      <parameter id="X" value="1">
        <arrays:listOfDimensions xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1">
          <arrays:dimension arrays:id="i" arrays:size="n" arrays:arrayDimension="0"/>
        </arrays:listOfDimensions>
      </parameter>
      <parameter id="Y" value="2">
        <arrays:listOfDimensions xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1">
          <arrays:dimension arrays:id="i" arrays:size="n" arrays:arrayDimension="0"/>
        </arrays:listOfDimensions>
      </parameter>
    </listOfParameters>
    <listOfInitialAssignments>
      <initialAssignment symbol="X">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <vector>
            <cn type="integer"> 1 </cn>
            <cn type="integer"> 2 </cn>
            <cn type="integer"> 3 </cn>
            <cn type="integer"> 4 </cn>
            <cn type="integer"> 5 </cn>
            <cn type="integer"> 6 </cn>
            <cn type="integer"> 7 </cn>
            <cn type="integer"> 8 </cn>
            <cn type="integer"> 9 </cn>
            <cn type="integer"> 10 </cn>
          </vector>
        </math>
              <arrays:listOfDimensions xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1">
          <arrays:dimension arrays:id="i" arrays:size="n" arrays:arrayDimension="0"/>
        </arrays:listOfDimensions>
      </initialAssignment>
    </listOfInitialAssignments>
    <listOfRules>
      <assignmentRule variable="Y">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <apply>
            <selector/>
            <ci> X </ci>
            <ci> i </ci>
          </apply>
        </math>
              <arrays:listOfDimensions xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1">
          <arrays:dimension arrays:id="i" arrays:size="n" arrays:arrayDimension="0"/>
        </arrays:listOfDimensions>
        <arrays:listOfIndices xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1">
          <arrays:index arrays:referencedAttribute="variable" arrays:arrayDimension="0">
            <math xmlns="http://www.w3.org/1998/Math/MathML">            
              <apply>
                <minus/>
                <cn type="integer"> 9 </cn>
                <ci> i </ci>
              </apply>
            </math>
                    </arrays:index>
        </arrays:listOfIndices>
      </assignmentRule>
    </listOfRules>
  </model>

</sbml>

Flatten model:
<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<sbml xmlns="http://www.sbml.org/sbml/level3/version1/core" level="3" arrays:required="true" xmlns:arrays="http://www.sbml.org/sbml/level3/version1/arrays/version1" version="1">
  <model>
    <listOfParameters>
      <parameter id="n" value="10"/>
      <parameter id="X_0" value="1"/>
      <parameter id="X_1" value="1"/>
      <parameter id="X_2" value="1"/>
      <parameter id="X_3" value="1"/>
      <parameter id="X_4" value="1"/>
      <parameter id="X_5" value="1"/>
      <parameter id="X_6" value="1"/>
      <parameter id="X_7" value="1"/>
      <parameter id="X_8" value="1"/>
      <parameter id="X_9" value="1"/>
      <parameter id="Y_0" value="2"/>
      <parameter id="Y_1" value="2"/>
      <parameter id="Y_2" value="2"/>
      <parameter id="Y_3" value="2"/>
      <parameter id="Y_4" value="2"/>
      <parameter id="Y_5" value="2"/>
      <parameter id="Y_6" value="2"/>
      <parameter id="Y_7" value="2"/>
      <parameter id="Y_8" value="2"/>
      <parameter id="Y_9" value="2"/>
    </listOfParameters>
    <listOfInitialAssignments>
      <initialAssignment symbol="X_0">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 1 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_1">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 2 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_2">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 3 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_3">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 4 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_4">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 5 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_5">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 6 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_6">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 7 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_7">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 8 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_8">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 9 </cn>
        </math>
            </initialAssignment>
      <initialAssignment symbol="X_9">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <cn type="integer"> 10 </cn>
        </math>
            </initialAssignment>
    </listOfInitialAssignments>
    <listOfRules>
      <assignmentRule variable="Y_9">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_0 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_8">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_1 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_7">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_2 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_6">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_3 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_5">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_4 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_4">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_5 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_3">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_6 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_2">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_7 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_1">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_8 </ci>
        </math>
            </assignmentRule>
      <assignmentRule variable="Y_0">
        <math xmlns="http://www.w3.org/1998/Math/MathML">        
          <ci> X_9 </ci>
        </math>
            </assignmentRule>
    </listOfRules>
  </model>

</sbml>


No comments:

Post a Comment