@@ -507,118 +507,3 @@ def indent_code(self, code):
507
507
level += increase [n ]
508
508
return pretty
509
509
510
-
511
- def lua_code (expr , assign_to = None , ** settings ):
512
- """Converts an expr to a string of Lua code
513
-
514
- expr : Expr
515
- A sympy expression to be converted.
516
- assign_to : optional
517
- When given, the argument is used as the name of the variable to which
518
- the expression is assigned. Can be a string, ``Symbol``,
519
- ``MatrixSymbol``, or ``Indexed`` type. This is helpful in case of
520
- line-wrapping, or for expressions that generate multi-line statements.
521
- precision : integer, optional
522
- The precision for numbers such as pi [default=15].
523
- user_functions : dict, optional
524
- A dictionary where the keys are string representations of either
525
- ``FunctionClass`` or ``UndefinedFunction`` instances and the values
526
- are their desired C string representations. Alternatively, the
527
- dictionary value can be a list of tuples i.e. [(argument_test,
528
- cfunction_string)]. See below for examples.
529
- dereference : iterable, optional
530
- An iterable of symbols that should be dereferenced in the printed code
531
- expression. These would be values passed by address to the function.
532
- For example, if ``dereference=[a]``, the resulting code would print
533
- ``(*a)`` instead of ``a``.
534
- human : bool, optional
535
- If True, the result is a single string that may contain some constant
536
- declarations for the number symbols. If False, the same information is
537
- returned in a tuple of (symbols_to_declare, not_supported_functions,
538
- code_text). [default=True].
539
- contract: bool, optional
540
- If True, ``Indexed`` instances are assumed to obey tensor contraction
541
- rules and the corresponding nested loops over indices are generated.
542
- Setting contract=False will not generate loops, instead the user is
543
- responsible to provide values for the indices in the code.
544
- [default=True].
545
- locals: dict
546
- A dictionary that contains the list of local symbols. these symbols will
547
- be preceeded by local for their first assignment.
548
-
549
- Examples
550
-
551
- >>> from sympy import lua_code, symbols, Rational, sin, ceiling, Abs, Function
552
- >>> x, tau = symbols("x, tau")
553
- >>> lua_code((2*tau)**Rational(7, 2))
554
- '8*1.4142135623731*tau.powf(7_f64/2.0)'
555
- >>> lua_code(sin(x), assign_to="s")
556
- 's = x.sin();'
557
-
558
- Simple custom printing can be defined for certain types by passing a
559
- dictionary of {"type" : "function"} to the ``user_functions`` kwarg.
560
- Alternatively, the dictionary value can be a list of tuples i.e.
561
- [(argument_test, cfunction_string)].
562
-
563
- >>> custom_functions = {
564
- ... "ceiling": "CEIL",
565
- ... "Abs": [(lambda x: not x.is_integer, "fabs", 4),
566
- ... (lambda x: x.is_integer, "ABS", 4)],
567
- ... "func": "f"
568
- ... }
569
- >>> func = Function('func')
570
- >>> lua_code(func(Abs(x) + ceiling(x)), user_functions=custom_functions)
571
- '(fabs(x) + x.CEIL()).f()'
572
-
573
- ``Piecewise`` expressions are converted into conditionals. If an
574
- ``assign_to`` variable is provided an if statement is created, otherwise
575
- the ternary operator is used. Note that if the ``Piecewise`` lacks a
576
- default term, represented by ``(expr, True)`` then an error will be thrown.
577
- This is to prevent generating an expression that may not evaluate to
578
- anything.
579
-
580
- >>> from sympy import Piecewise
581
- >>> expr = Piecewise((x + 1, x > 0), (x, True))
582
- >>> print(lua_code(expr, tau))
583
- tau = if (x > 0) {
584
- x + 1
585
- } else {
586
- x
587
- };
588
-
589
- Support for loops is provided through ``Indexed`` types. With
590
- ``contract=True`` these expressions will be turned into loops, whereas
591
- ``contract=False`` will just print the assignment expression that should be
592
- looped over:
593
-
594
- >>> from sympy import Eq, IndexedBase, Idx
595
- >>> len_y = 5
596
- >>> y = IndexedBase('y', shape=(len_y,))
597
- >>> t = IndexedBase('t', shape=(len_y,))
598
- >>> Dy = IndexedBase('Dy', shape=(len_y-1,))
599
- >>> i = Idx('i', len_y-1)
600
- >>> e=Eq(Dy[i], (y[i+1]-y[i])/(t[i+1]-t[i]))
601
- >>> lua_code(e.rhs, assign_to=e.lhs, contract=False)
602
- 'Dy[i] = (y[i + 1] - y[i])/(t[i + 1] - t[i]);'
603
-
604
- Matrices are also supported, but a ``MatrixSymbol`` of the same dimensions
605
- must be provided to ``assign_to``. Note that any expression that can be
606
- generated normally can also exist inside a Matrix:
607
-
608
- >>> from sympy import Matrix, MatrixSymbol
609
- >>> mat = Matrix([x**2, Piecewise((x + 1, x > 0), (x, True)), sin(x)])
610
- >>> A = MatrixSymbol('A', 3, 1)
611
- >>> print(lua_code(mat, A))
612
- A = [x.powi(2), if (x > 0) {
613
- x + 1
614
- } else {
615
- x
616
- }, x.sin()];
617
- """
618
-
619
- return LuaCodePrinter (settings ).doprint (expr , assign_to )
620
-
621
-
622
- def print_lua_code (expr , ** settings ):
623
- """Prints Lua representation of the given expression."""
624
- print ((lua_code (expr , ** settings )))
0 commit comments