# Sympy string to equation

Stack Overflow Asked by Mel on December 3, 2020

Im working on a project with sympy. I want to save some equations in a JSON file. They are saved as a string (for example: "R * C * 1.1, ti"). I have tried to convert the string to a equation (with sy.S() and sy.Eq()), but it didn’t work.

Here is my code:

import sympy as sy

def solve():

R = sy.S(2)
C = sy.S(1)
ti = sy.S("ti")

equation = sy.Eq(sy.S("R * C * 1.1, ti"))

solution = sy.solve(equation, manual = 1)
print(solution)

solve()


I get this warning and a empty solution (when i exchange the string with a normal formula, it works):

Eq(expr) with rhs default to 0 has been deprecated since SymPy 1.5.

deprecated_since_version="1.5"
[]

There are several issues:

• sy.S("R * C * 1.1, ti") doesn't look at existing variables, but creates its own variables that happen to have the external names "R", "C" and "ti.
• sy.S("R * C * 1.1, ti") creates a tuple (R * C * 1.1, ti)
• sy.Eq() expects two parameters: the left-hand side and the right-hand side of an equation. Older versions of sympy where also happy with just the left-hand side and imagined the right-hand side to be 0. This still works, but is strongly discouraged as it won't work any more in future versions.
• With sy.Eq(sy.S("R * C * 1.1, ti")), there is only one parameter (a tuple) for sy.Eq. So this provokes the deprecated warning.
• To convert a tuple to parameters, Python now uses the * operator: func(*(a,b)) calls func with two parameters: func(a,b). On the other hand func((a,b)) calls func with just one parameter, the tuple (a,b).
• To assign values to the variables obtained via sy.S("R * C * 1.1, ti"), a dictionary that maps strings to values can be used.
• Although True and False are represented internally as 1 and 0, it is highly recommended to explicitly use the names. Among others, this helps with readability and maintainability of code.
import sympy as sy

def solve():
value_dict = {"R": 2, "C": 1}
equation = sy.Eq(*sy.S("R * C * 1.1, ti")).subs(value_dict)
solution = sy.solve(equation, manual=True)
print(solution)

solve()


If you need ti to be an accessible sympy variable, you can add it to the dictionary:

    ti = sy.symbols("ti")
value_dict = {"R": 2, "C": 1, "ti": ti}


Correct answer by JohanC on December 3, 2020

## Related Questions

### modal pop up after clicking button

1  Asked on August 19, 2020 by ashmoe

### No line break when passing value containing “n” received from server side to textarea

1  Asked on August 18, 2020 by ken-setoguchi

### Apply a common class to html tags based on some condition using jquery

4  Asked on August 18, 2020 by ityka-bandta

### How to use multiprocessing with GUI and backend classes?

0  Asked on August 17, 2020 by ggokturk

### How to import react libraries into pure jsx file

0  Asked on August 17, 2020 by dannycranfield

### How to handle and execute multiple SQL at once in Python?

2  Asked on August 16, 2020 by william

### Project requirement to Read input files from COBOL and bring on Mosaic Platform

1  Asked on August 15, 2020 by abhijeet-vipat

### ::slotted CSS selector for nested children in shadowDOM slot

1  Asked on August 15, 2020 by mendy

### Style changing with JS not responding

0  Asked on August 14, 2020 by alessandro

### Can I bundle Pepper Flash in an Electron.js app to run Flash in 2021+?

1  Asked on August 14, 2020 by rogiller

### Can’t use saved information to load into an array

1  Asked on August 13, 2020 by danny

### Posting values of input fields outside form tag containing submit button

2  Asked on August 11, 2020 by kuwir-singh

### Delete 1:1 parent form child entity without JPA mappings

1  Asked on August 11, 2020 by saran3h

0  Asked on August 10, 2020 by user12217822

### How to return pointer adress for structure variable

2  Asked on August 9, 2020 by mystheman

### What happens when compose String with difference sequence?

2  Asked on August 9, 2020 by shiwee

### Merging two PDF generating views in Django

1  Asked on August 8, 2020 by piethon

### How do you put a list inside of an object C#?

1  Asked on August 8, 2020 by forrest

### How to modify this C++ class to be as efficient as C code?

1  Asked on August 7, 2020 by victor-canoz