Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Public Member Functions | Private Member Functions | List of all members
CellMLToNektar.pycml.mathml_constructor Class Reference
Inheritance diagram for CellMLToNektar.pycml.mathml_constructor:
Inheritance graph
[legend]
Collaboration diagram for CellMLToNektar.pycml.mathml_constructor:
Collaboration graph
[legend]

Public Member Functions

def __init__
 
- Public Member Functions inherited from CellMLToNektar.pycml.mathml
def __init__
 
def __repr__
 
def __deepcopy__
 
def clone_self
 
def get_original_of_clone
 
def get_component
 
def model
 
def eval
 
- Public Member Functions inherited from CellMLToNektar.pycml.element_base
def __init__
 
def __delattr__
 
def __setattr__
 
def rootNode
 
def cmeta_id
 
def xml_remove_child_at
 
def xml_doc
 
def xml_properties
 

Private Member Functions

def _tree_complexity
 
def _get_element_binding_time
 
def _get_element_units
 
def _reduce_elt
 
def _eval_self
 
def _update_usage_counts
 

Additional Inherited Members

- Static Public Member Functions inherited from CellMLToNektar.pycml.mathml
def clone
 
- Public Attributes inherited from CellMLToNektar.pycml.element_base
 xml_attributes
 
- Properties inherited from CellMLToNektar.pycml.mathml
 component = property(get_component)
 

Detailed Description

Base class for MathML constructor elements, e.g. apply and piecewise.

Definition at line 4000 of file pycml.py.

Constructor & Destructor Documentation

def CellMLToNektar.pycml.mathml_constructor.__init__ (   self)

Definition at line 4004 of file pycml.py.

4005  def __init__(self):
4006  super(mathml_constructor, self).__init__()
4007  return

Member Function Documentation

def CellMLToNektar.pycml.mathml_constructor._eval_self (   self)
private
Evaluate self and return <cn>, <true> or <false>, as appropriate.

Definition at line 4108 of file pycml.py.

References CellMLToNektar.pycml.mathml._ensure_units_exist(), CellMLToNektar.pycml.mathml_cn.evaluate(), CellMLToNektar.pycml.mathml_ci.evaluate(), CellMLToNektar.pycml.mathml_apply.evaluate(), CellMLToNektar.pycml.mathml_piecewise.evaluate(), CellMLToNektar.pycml.mathml_plus.evaluate(), CellMLToNektar.pycml.mathml_minus.evaluate(), CellMLToNektar.pycml.mathml_times.evaluate(), CellMLToNektar.pycml.mathml_divide.evaluate(), CellMLToNektar.pycml.mathml_exp.evaluate(), CellMLToNektar.pycml.mathml_ln.evaluate(), CellMLToNektar.pycml.mathml_log.evaluate(), CellMLToNektar.pycml.mathml_abs.evaluate(), CellMLToNektar.pycml.mathml_power.evaluate(), CellMLToNektar.pycml.mathml_root.evaluate(), CellMLToNektar.pycml.mathml_and.evaluate(), CellMLToNektar.pycml.mathml_or.evaluate(), CellMLToNektar.pycml.mathml_leq.evaluate(), CellMLToNektar.pycml.mathml_lt.evaluate(), CellMLToNektar.pycml.mathml_geq.evaluate(), CellMLToNektar.pycml.mathml_gt.evaluate(), CellMLToNektar.pycml.mathml_neq.evaluate(), CellMLToNektar.pycml.mathml_eq.evaluate(), CellMLToNektar.pycml.mathml_rem.evaluate(), CellMLToNektar.pycml.mathml_logbase.evaluate(), CellMLToNektar.pycml.mathml_degree.evaluate(), CellMLToNektar.pycml.mathml_sin.evaluate(), CellMLToNektar.pycml.mathml_cos.evaluate(), CellMLToNektar.pycml.mathml_tan.evaluate(), CellMLToNektar.pycml.mathml_arcsin.evaluate(), CellMLToNektar.pycml.mathml_arccos.evaluate(), and CellMLToNektar.pycml.mathml_arctan.evaluate().

Referenced by CellMLToNektar.pycml.mathml_apply._reduce(), and CellMLToNektar.pycml.mathml_piecewise._reduce().

4109  def _eval_self(self):
4110  """Evaluate self and return <cn>, <true> or <false>, as appropriate."""
4111  value = self.evaluate()
4112  if value is True:
4113  new_elt = self.xml_create_element(u'true', NSS[u'm'])
4114  elif value is False:
4115  new_elt = self.xml_create_element(u'false', NSS[u'm'])
4116  else:
4117  # Add a new <units> element to the document if needed
4118  attrs = self._ensure_units_exist()
4119  new_elt = self.xml_create_element(u'cn', NSS[u'm'],
4120  content=unicode("%.17g" % value),
4121  attributes=attrs)
4122  return new_elt
def CellMLToNektar.pycml.mathml_constructor._get_element_binding_time (   self,
  elt 
)
private
Helper method to get the binding time of a MathML element.

Definition at line 4057 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_apply._get_binding_time(), CellMLToNektar.pycml.mathml_piecewise._get_binding_time(), CellMLToNektar.pycml.mathml_piecewise._reduce(), and CellMLToNektar.pycml.mathml_apply.get_units().

4058  def _get_element_binding_time(self, elt):
4059  """Helper method to get the binding time of a MathML element."""
4060  if hasattr(elt, '_get_binding_time'):
4061  # Call the element's method
4062  return elt._get_binding_time()
4063  elif elt.localName in [u'true', u'false', u'exponentiale', u'pi']:
4064  return BINDING_TIMES.static
4065  else:
4066  raise EvaluationError("Don't know how to compute binding time"
4067  " of element " + elt.localName)
def CellMLToNektar.pycml.mathml_constructor._get_element_units (   self,
  elt,
  return_set = True 
)
private
Helper method to get the units of a MathML element.

Definition at line 4068 of file pycml.py.

Referenced by CellMLToNektar.pycml.mathml_apply._get_operand_units(), CellMLToNektar.pycml.mathml_apply.get_units(), and CellMLToNektar.pycml.mathml_piecewise.get_units().

4069  def _get_element_units(self, elt, return_set=True):
4070  """Helper method to get the units of a MathML element."""
4071  if hasattr(elt, 'get_units'):
4072  # Element has a method to get its units, so call it
4073  u = elt.get_units()
4074  elif hasattr(elt, '_cml_units') and elt._cml_units:
4075  # Units have been cached
4076  u = elt._cml_units
4077  else:
4078  # Let's figure it out ourselves...
4079  if elt.localName in [u'false', u'true']:
4080  u = UnitsSet(
4081  [self.component.get_units_by_name('cellml:boolean')],
4082  expression=elt)
4083  elif elt.localName in [u'notanumber', u'pi', u'infinity',
4084  u'exponentiale']:
4085  u = UnitsSet(
4086  [self.component.get_units_by_name('dimensionless')],
4087  expression=elt)
4088  else:
4089  # Unknown or unexpected element
4090  raise UnitsError(self, u''.join([
4091  u'Unsupported element "', elt.localName, '".']))
4092  if not return_set:
4093  u = u.extract()
4094  return u
def CellMLToNektar.pycml.mathml_constructor._reduce_elt (   self,
  elt 
)
private
Try to reduce the given element.

Call the _reduce method on elt, if it has one.
If not, do nothing (we assume elt cannot be reduced).

Definition at line 4095 of file pycml.py.

References CellMLToNektar.utilities.DEBUG().

Referenced by CellMLToNektar.pycml.mathml_apply._reduce(), and CellMLToNektar.pycml.mathml_piecewise._reduce().

4096  def _reduce_elt(self, elt):
4097  """Try to reduce the given element.
4098 
4099  Call the _reduce method on elt, if it has one.
4100  If not, do nothing (we assume elt cannot be reduced).
4101  """
4102  if hasattr(elt, '_reduce') and callable(elt._reduce):
4103  elt._reduce()
4104  else:
4105  DEBUG('partial-evaluator', "Don't know how to reduce",
4106  elt.localName)
4107  return
def CellMLToNektar.pycml.mathml_constructor._tree_complexity (   self,
  elt,
  kw 
)
private
Calculate a rough estimate of the computation time for
evaluating the given element.

If lookup_tables is True, then assume we're using lookup tables
where possible.
If store_result is True, the complexity is saved to the
_cml_complexity attribute.
If algebraic is True, the complexity is calculated as a dictionary,
mapping node types to the number of occurences of that type.

Definition at line 4008 of file pycml.py.

References CellMLToNektar.pycml.mathml_constructor._tree_complexity(), CellMLToNektar.pycml.child_i(), and CellMLToNektar.pycml.element_base.rootNode().

Referenced by CellMLToNektar.pycml.mathml_constructor._tree_complexity(), CellMLToNektar.pycml.mathml_apply.tree_complexity(), and CellMLToNektar.pycml.mathml_piecewise.tree_complexity().

4009  def _tree_complexity(self, elt, **kw):
4010  """
4011  Calculate a rough estimate of the computation time for
4012  evaluating the given element.
4013 
4014  If lookup_tables is True, then assume we're using lookup tables
4015  where possible.
4016  If store_result is True, the complexity is saved to the
4017  _cml_complexity attribute.
4018  If algebraic is True, the complexity is calculated as a dictionary,
4019  mapping node types to the number of occurences of that type.
4020  """
4021  kw['lookup_tables'] = kw.get('lookup_tables', False)
4022  kw['store_result'] = kw.get('store_result', False)
4023  kw['algebraic'] = kw.get('algebraic', False)
4024  if kw['algebraic']:
4025  ac = {}
4026  if kw['lookup_tables'] and \
4027  elt.getAttributeNS(NSS['lut'], u'possible', u'no') == u'yes':
4028  # This elt will be replaced by a lookup table
4029  if hasattr(self.rootNode, 'num_lookup_tables'):
4030  # Count the number of used lookup tables
4031  self.rootNode.num_lookup_tables += 1
4032  # Cost per table: 2 lookups, 2 +, -, *, 3 ci
4033  if kw['algebraic']:
4034  ac['lookup'] = 2
4035  ac['op'] = 3
4036  ac['times'] = 1
4037  ac['variable'] = 3
4038  else:
4039  ac = 2*1 + 2*1 + 1 + 1 + 3*0.7
4040  elif hasattr(elt, 'tree_complexity') \
4041  and callable(elt.tree_complexity):
4042  ac = elt.tree_complexity(**kw)
4043  elif elt.localName in ['true', 'false', 'cn', 'exponentiale', 'pi']:
4044  if kw['algebraic']: ac['constant'] = 1
4045  else: ac = 0.5
4046  elif elt.localName == 'ci':
4047  if kw['algebraic']: ac['variable'] = 1
4048  else: ac = 0.7
4049  elif elt.localName in ['degree', 'logbase']:
4050  ac = self._tree_complexity(child_i(elt, 1), **kw)
4051  else:
4052  raise EvaluationError("Don't know complexity of element " +
4053  elt.localName)
4054  if kw['store_result'] and isinstance(elt, mathml):
4055  elt._cml_complexity = ac
4056  return ac
def CellMLToNektar.pycml.mathml_constructor._update_usage_counts (   self,
  expr,
  remove = False 
)
private
Update usage counts of variables used in the given expression.

By default, increment the usage count of any variable occuring
in a <ci> element within expr.  If remove is set to True,
then decrement the usage counts instead.

Definition at line 4123 of file pycml.py.

References CellMLToNektar.pycml.mathml_constructor._update_usage_counts().

Referenced by CellMLToNektar.pycml.mathml_apply._reduce(), CellMLToNektar.pycml.mathml_piecewise._reduce(), and CellMLToNektar.pycml.mathml_constructor._update_usage_counts().

4124  def _update_usage_counts(self, expr, remove=False):
4125  """Update usage counts of variables used in the given expression.
4126 
4127  By default, increment the usage count of any variable occuring
4128  in a <ci> element within expr. If remove is set to True,
4129  then decrement the usage counts instead.
4130  """
4131  if isinstance(expr, mathml_ci):
4132  if remove:
4133  expr.variable._decrement_usage_count()
4134  else:
4135  raise NotImplementedError("_update_usage_counts currently only reliable for remove=True")
4136  expr.variable._used()
4137  elif isinstance(expr, mathml_apply) and isinstance(expr.operator(),
4138  mathml_diff):
4139  # TODO: Check if this is a suitable handling of ODEs on a RHS
4140  # It matches the current behaviour in apply.classify_variables.
4141  pass
4142  else:
4143  for e in self.xml_element_children(expr):
4144  self._update_usage_counts(e, remove=remove)