Reputation: 368
I'm looking for an alternative to MathCad to make simple calculations but I wanted the expressions to look like if I was using pen and paper and make it easy to read for people who don't know programming. I tried Sweave, Knitr but I was unhappy with it. I recently found Jupyter notebook with SymPy and it's still not as easy as MathCad for me, but I'll give it a try. With Jupyter I'm having trouble printing formulas: I want to print both sides of the equation automatically.
ccode doesn't return latex and it's boring always typing "assign_to"
Upvotes: 4
Views: 1244
Reputation: 91480
You need to use sympy.Eq
if you want to print an equation. The =
operator just assigns variables in Python, meaning in your example, the Python variable R_r
is assigned to the SymPy expression (c_1 + (c_2*x/G) + c_3*V)*G
.
In general in Python, there is no reverse association with an object and the variable it is assigned to. There is no way for the expression to "know" that it is assigned to a variable named R_r
.
Instead, you need to create a symbol named R_r
and use Eq(R_r, (c_1 + (c_2*x/G) + c_3*V)*G)
.
See also http://docs.sympy.org/latest/tutorial/gotchas.html and http://nedbatchelder.com/text/names.html.
Upvotes: 2
Reputation: 630
If you need a more convenient way, you can define a wrapper function as follows.
class Equation(object):
def __init__(self, left, right, mode='latex'):
self.mode = mode
self.left = left
self.right = right
self._eq = sym.Eq(left, right)
self._latex = sym.latex(self._eq)
def __repr__(self):
if self.mode == 'latex':
return self._latex.__repr__()
elif self.mode == 'sympy':
return self._eq.__repr__()
def __str__(self):
if self.mode == 'latex':
return self._latex
elif self.mode == 'sympy':
return self.eq.__str__()
def eq(self):
return self._eq
def latex(self):
return self._latex
@property
def mode(self):
return self._mode
@mode.setter
def mode(self, val):
self._mode = val
@property
def left(self):
return self._left
@left.setter
def left(self, val):
self._left = sym
@property
def right(self):
return self._right
@right.setter
def right(self, val):
self._right = val
# end of class
Upvotes: 4
Reputation: 630
You can use sympy.printing.latex.
Refer to the following link for more information.
http://docs.sympy.org/dev/modules/printing.html?highlight=sympy.printing#module-sympy.printing.latex
Upvotes: 0