Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
arguments.append(("general_helper","double const *__restrict const"))
if do_cse:
import sympy
get_helper = sympy.Function("get_jac_helper")
set_helper = symengine.Function("set_jac_helper")
jac_sym_wc = sympy.Matrix([
[ sympy.sympify(entry) for entry in line ]
for line in jac_sym_wc
])
_cse = sympy.cse(
sympy.sympify(jac_sym_wc),
symbols = (get_helper(i) for i in count())
)
more_helpers = symengine.sympify(_cse[0])
jac_sym_wc = symengine.sympify(_cse[1][0].tolist())
if more_helpers:
arguments.append(("jac_helper","double *__restrict const"))
self.render_and_write_code(
(set_helper(i, helper[1]) for i,helper in enumerate(more_helpers)),
name = "jac_helpers",
chunk_size = chunk_size,
arguments = arguments,
omp = False
)
self._number_of_jac_helpers = len(more_helpers)
set_dfdy = symengine.Function("set_dfdy")
self.render_and_write_code(
helpers_wc = list(self.helpers) # list is here for copying
if simplify is None:
simplify = self.n<=10
if simplify:
f_sym_wc = (entry.simplify(ratio=1.0) for entry in f_sym_wc)
if do_cse:
import sympy
additional_helper = sympy.Function("additional_helper")
_cse = sympy.cse(
sympy.Matrix(sympy.sympify(list(f_sym_wc))),
symbols = (additional_helper(i) for i in count())
)
helpers_wc.extend(symengine.sympify(_cse[0]))
f_sym_wc = symengine.sympify(_cse[1][0])
arguments = [
("self", "dde_integrator * const"),
("t", "double const"),
("y", "double", self.n),
]
helper_i = 0
anchor_i = 0
converted_helpers = []
self.past_calls = 0
self.substitutions = {
control_par: symengine.Symbol("self->parameter_"+control_par.name)
for control_par in self.control_pars
}
for i in range(n_lyap):
jacs = [_jac(f, helpers, delay, n) for delay in delays]
for _ in range(n):
expression = 0
for delay,jac in zip(delays,jacs):
for k,entry in enumerate(next(jac)):
expression += entry * y(k+(i+1)*n,t-delay)
if simplify:
expression = expression.simplify(ratio=1.0)
yield expression
for _ in range(zero_padding):
yield symengine.sympify(0)
def build_functions(sympy_graph, variables, parameters=None, wrt=None, include_obj=True, include_grad=False, include_hess=False, cse=True):
if wrt is None:
wrt = sympify(tuple(variables))
if parameters is None:
parameters = []
else:
parameters = [wrap_symbol_symengine(p) for p in parameters]
variables = tuple(variables)
parameters = tuple(parameters)
func, grad, hess = None, None, None
inp = sympify(variables + parameters)
graph = sympify(sympy_graph)
if count_ops(graph) > BACKEND_OPS_THRESHOLD:
backend = 'lambda'
else:
backend = 'llvm'
# TODO: did not replace zoo with oo
if include_obj:
func = lambdify(inp, [graph], backend=backend, cse=cse)
if include_grad or include_hess:
grad_graphs = list(graph.diff(w) for w in wrt)
grad_ops = sum(count_ops(x) for x in grad_graphs)
if grad_ops > BACKEND_OPS_THRESHOLD:
grad_backend = 'lambda'
else:
grad_backend = 'llvm'
if include_grad:
arguments = self._default_arguments()
if self.helpers:
arguments.append(("general_helper","double const *__restrict const"))
if do_cse:
import sympy
get_helper = sympy.Function("get_f_helper")
set_helper = symengine.Function("set_f_helper")
_cse = sympy.cse(
sympy.Matrix(sympy.sympify(list(f_sym_wc))),
symbols = (get_helper(i) for i in count())
)
more_helpers = symengine.sympify(_cse[0])
f_sym_wc = symengine.sympify(_cse[1][0])
if more_helpers:
arguments.append(("f_helper","double *__restrict const"))
self.render_and_write_code(
(set_helper(i,helper[1]) for i,helper in enumerate(more_helpers)),
name = "f_helpers",
chunk_size = chunk_size,
arguments = arguments,
omp = False,
)
self._number_of_f_helpers = len(more_helpers)
set_dy = symengine.Function("set_dy")
self.render_and_write_code(
(set_dy(i,entry) for i,entry in enumerate(f_sym_wc)),
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import absolute_import
import symengine
Integer = symengine.Integer
Real = symengine.RealNumber
Basic = symengine.Basic
Number = symengine.Number
Zero = Integer(0)
One = Integer(1)
NegativeOne = Integer(-1)
sympify = symengine.sympify
Add = symengine.Add
Mul = symengine.Mul
Pow = symengine.Pow
class UniqueSymbol(symengine.Symbol):
"""
Present a uniform facade to the ``symengine.Symbol`` class.
``symengine.Symbol`` already creates unique objects even when they have
the same name. The use of ``__slots__`` is propagated.
Attributes
----------
name : str
if simplify is None:
simplify = self.n<=10
if simplify:
f_sym_wc = (entry.simplify(ratio=1.0) for entry in f_sym_wc)
if do_cse:
import sympy
additional_helper = sympy.Function("additional_helper")
_cse = sympy.cse(
sympy.Matrix(sympy.sympify(list(f_sym_wc))),
symbols = (additional_helper(i) for i in count())
)
helpers_wc.extend(symengine.sympify(_cse[0]))
f_sym_wc = symengine.sympify(_cse[1][0])
arguments = [
("self", "dde_integrator * const"),
("t", "double const"),
("y", "double", self.n),
]
helper_i = 0
anchor_i = 0
converted_helpers = []
self.past_calls = 0
self.substitutions = {
control_par: symengine.Symbol("self->parameter_"+control_par.name)
for control_par in self.control_pars
}
def finalise(expression):
if do_cse:
import sympy
get_helper = sympy.Function("get_jac_helper")
set_helper = symengine.Function("set_jac_helper")
jac_sym_wc = sympy.Matrix([
[ sympy.sympify(entry) for entry in line ]
for line in jac_sym_wc
])
_cse = sympy.cse(
sympy.sympify(jac_sym_wc),
symbols = (get_helper(i) for i in count())
)
more_helpers = symengine.sympify(_cse[0])
jac_sym_wc = symengine.sympify(_cse[1][0].tolist())
if more_helpers:
arguments.append(("jac_helper","double *__restrict const"))
self.render_and_write_code(
(set_helper(i, helper[1]) for i,helper in enumerate(more_helpers)),
name = "jac_helpers",
chunk_size = chunk_size,
arguments = arguments,
omp = False
)
self._number_of_jac_helpers = len(more_helpers)
set_dfdy = symengine.Function("set_dfdy")
self.render_and_write_code(
(
arguments = self._default_arguments()
if self.helpers:
arguments.append(("general_helper","double const *__restrict const"))
if do_cse:
import sympy
get_helper = sympy.Function("get_f_helper")
set_helper = symengine.Function("set_f_helper")
_cse = sympy.cse(
sympy.Matrix(sympy.sympify(list(f_sym_wc))),
symbols = (get_helper(i) for i in count())
)
more_helpers = symengine.sympify(_cse[0])
f_sym_wc = symengine.sympify(_cse[1][0])
if more_helpers:
arguments.append(("f_helper","double *__restrict const"))
self.render_and_write_code(
(set_helper(i,helper[1]) for i,helper in enumerate(more_helpers)),
name = "f_helpers",
chunk_size = chunk_size,
arguments = arguments,
omp = False,
)
self._number_of_f_helpers = len(more_helpers)
set_dy = symengine.Function("set_dy")
self.render_and_write_code(
(set_dy(i,entry) for i,entry in enumerate(f_sym_wc)),
name = "f",
def _find_max_delay(delays):
if all(symengine.sympify(delay).is_Number for delay in delays):
return float(symengine.sympify(max(delays)).n(real=True))
else:
raise ValueError("Delay depends on time or dynamics; cannot determine max_delay automatically. You have to pass it as an argument to jitcdde.")