Nektar++
Loading...
Searching...
No Matches
_ewriter.py
Go to the documentation of this file.
2# Nektar expression writer
3#
4# This file is derived from AnsiCExporter in myokit.
5# See http://myokit.org for copyright, sharing, and licensing details.
6#
7from __future__ import absolute_import, division
8from __future__ import print_function, unicode_literals
9
10import myokit
11from myokit.formats.python import PythonExpressionWriter
12
13
14class NektarExpressionWriter(PythonExpressionWriter):
15 """
16 This :class:`ExpressionWriter <myokit.formats.ExpressionWriter>` writes
17 equations for variables in a Nektar-style syntax.
18 """
19 def __init__(self):
20 super(NektarExpressionWriter, self).__init__()
22 self._fcond = None
23
24 def set_condition_function(self, func=None):
25 """
26 Sets a function name to use for if statements
27
28 By setting func to None you can revert back to the default behavior
29 (the ternary operator). Any other value will be interpreted as the
30 name of a C function taking arguments (condition, value_if_true,
31 value_if_false).
32 """
33 self._fcond = func
34
35 #def _ex_name(self, e):
36 #def _ex_derivative(self, e):
37 #def _ex_number(self, e):
38 #def _ex_prefix_plus(self, e):
39 #def _ex_prefix_minus(self, e):
40 #def _ex_plus(self, e):
41 #def _ex_minus(self, e):
42 #def _ex_multiply(self, e):
43 #def _ex_divide(self, e):
44
45 def _ex_quotient(self, e):
46 # Note that this _must_ round towards minus infinity!
47 # See myokit.Quotient !
48 return self.ex(myokit.Floor(myokit.Divide(e[0], e[1])))
49
50 def _ex_remainder(self, e):
51 # Note that this _must_ use the same round-to-neg-inf convention as
52 # myokit.Quotient! Implementation below is consistent with Python
53 # convention:
54 return self.ex(myokit.Minus(
55 e[0], myokit.Multiply(e[1], myokit.Quotient(e[0], e[1]))))
56
57 def _ex_power(self, e):
58 return 'pow(' + self.ex(e[0]) + ', ' + self.ex(e[1]) + ')'
59
60 #def _ex_sqrt(self, e):
61 #def _ex_sin(self, e):
62 #def _ex_cos(self, e):
63 #def _ex_tan(self, e):
64 #def _ex_asin(self, e):
65 #def _ex_acos(self, e):
66 #def _ex_atan(self, e):
67 #def _ex_exp(self, e):
68
69 def _ex_log(self, e):
70 if len(e) == 1:
71 return self._ex_function(e, 'log')
72 return '(log(' + self.ex(e[0]) + ') / log(' + self.ex(e[1]) + '))'
73
74 #def _ex_log10(self, e):
75 #def _ex_floor(self, e):
76 #def _ex_ceil(self, e):
77
78 def _ex_abs(self, e):
79 return self._ex_function(e, 'fabs')
80
81 def _ex_not(self, e):
82 return '!(' + self.ex(e[0]) + ')'
83
84 #def _ex_equal(self, e):
85 #def _ex_not_equal(self, e):
86 #def _ex_more(self, e):
87 #def _ex_less(self, e):
88 #def _ex_more_equal(self, e):
89 #def _ex_less_equal(self, e):
90
91 def _ex_and(self, e):
92 return self._ex_infix_condition(e, '&&')
93
94 def _ex_or(self, e):
95 return self._ex_infix_condition(e, '||')
96
97 def _ex_if(self, e):
98 ite = (self.ex(e._i), self.ex(e._t), self.ex(e._e))
99 if self._fcond is None:
100 return '(%s ? %s : %s)' % ite
101 else:
102 return '%s(%s, %s, %s)' % ((self._fcond,) + ite)
103
104 def _ex_piecewise(self, e):
105 s = []
106 n = len(e._i)
107 if self._fcond is None:
108 for i in range(0, n):
109 s.append('(%s ? %s : ' % (self.ex(e._i[i]), self.ex(e._e[i])))
110 s.append(self.ex(e._e[n]))
111 s.append(')' * n)
112 else:
113 for i in range(0, n):
114 s.append(
115 '%s(%s, %s, ' % (
116 self._fcond, self.ex(e._i[i]), self.ex(e._e[i])))
117 s.append(self.ex(e._e[n]))
118 s.append(')' * n)
119 return ''.join(s)
120
set_condition_function(self, func=None)
Definition _ewriter.py:24