OnStrike
OnStrike

Reputation: 748

How to create a sympy equation for vectors?

I am trying to display both sides of a vector equation.

I am able to display() the rhs of the equation but I am unable to define an equality sympy.Eq(symbols("F_T"), F_Tx*N.x + F_Ty*N.y + F_Tz*N.z) using vectors. Ideally I would like F_T to its own symbol (for use in other equations) but creating a vector modifies the type() from "symbol" to "Vector".

Therefore I am unable to print the desired full equation (like a textbook might). I am open to printing in LaTeX but I am uncertain how to combine my sympy expressions with created LaTeX. Any help is appreciated, Thanks!

Current

I would like to be able to pretty-print: F_T = F_Tx*N.x + F_Ty*N.y + F_Tz*N.z

Upvotes: 2

Views: 1347

Answers (2)

Sc0ttyD
Sc0ttyD

Reputation: 1756

I've been wondering about how to do the same thing. The following tips are not a perfect solution, but they may prove useful to someone.

def create_vector(sym, refFrame):
    unit_vectors = refFrame.base_vectors()
    syms = sp.symbols(' '.join([sym + '_{' + str(ax) + '}' for ax in unit_vectors]))
    vector = reduce(add, [sym * unit for sym, unit in zip(syms, unit_vectors)])
    return (vector,) + syms

This function allows you to quickly create a vector with unresolved symbolic components, like so:

XYZ = CoordSys3D('XYZ', vector_names=('I', 'J', 'K'))
r_rel, r_rel_i, r_rel_j, r_Rel_k = create_vector('\mathbf{r_{rel}}', XYZ)

r_rel

Now, if you try to render this in Jupyter, you get the ugly component representation:

enter image description here

But, if you are prepared to be a bit hacky, you can work around this. The Zero vector stores it's own latex representation internally, as self._latex_form. The sympy latex printer determines if a vector is the zero vector by checking if expr == expr.zero. We can abuse these and trick the latex printer into printing a nicer representation:

r_rel.zero = r_rel
r_rel._latex_form = '\mathbf{r_{rel}}'
r_rel

For which we get this prettier representation:

enter image description here

Admittedly, this could break things, and it will make you feel dirty. But it may help someone.

Upvotes: 1

smichr
smichr

Reputation: 19115

Try creating an equality to display: eq = Eq(F_T, F_Tx*N.x ...). An equality is printed with an equals sign separating the lhs and rhs of the equality.

Upvotes: 0

Related Questions