Comments (4)
Yeah, the behavior should roughly match the sympy equivalent (we should document this better). However, when SymForce is using the SymEngine symbolic API (the default), it calls symengine.solve
instead of sympy.solve
, and symengine.solve
does not accept a dict
argument.
If you want something in sympy.solve
that's not in symengine.solve
, you can either symforce.set_symbolic_api('sympy')
, or do something like:
import sympy as sm
import symforce.symbolic as sf
x, y, z = sf.symbols("x y z")
solutions = sm.solve([x**2 + y - 2*z, y + 4*z], x, y, dict=True)
print(solutions)
sf_solutions = [{sf.S(k): sf.S(v) for k, v in solution.items()} for solution in solutions]
to call sympy.solve
while SymForce is using the SymEngine symbolic API
from symforce.
Thank you, the explanation makes sense. The workaround works when I use simple symbols that already exists in sympy, but this workaround means that I can't use more complex symforce types?
E.g.
import symforce.symbolic as sf
import sympy as sm
R = sf.Rot2.symbolic("R")
solutions = sm.solve(R.to_tangent(epsilon=sf.epsilon())[0] - 0, R, dict=True)
print(solutions)
fails with the (shortened) error message:
SymPyDeprecationWarning:
The string fallback in sympify() is deprecated.
To explicitly convert the string form of an object, use
sympify(str(obj)). To add define sympify behavior on custom
objects, use sympy.core.sympify.converter or define obj._sympy_
(see the sympify() docstring).
sympify() performed the string fallback resulting in the following string:
'<Rot2 <C real=R_re, imag=R_im>>'
See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-sympify-string-fallback
for details.
This has been deprecated since SymPy version 1.6. It
will be removed in a future version of SymPy.
return list(map(sympify, w if iterable(w) else [w]))
ValueError: Error from parse_expr with transformed code: "<Symbol ('Rot2' )<Symbol ('C' )Symbol ('real' )=Symbol ('R_re' ),imag =Symbol ('R_im' )>>"
Is there a way to use sympys solve
with an expression including symforce types, or do I have to choose one of them? I.e. if I understand it correctly, I have two options: 1: I either can use symengine backend that allows complex types, but without the option to solve a set of coupled equations, or 2: I can use the sympy backend that does not support complex types but can solve a set of coupled equations. Is there a way to get both symforce types and a solve function that accepts the dict
argument?
from symforce.
You can at least do something like this, although it's a little cumbersome:
In [3]: solutions = sm.solve(R.to_tangent(epsilon=sf.epsilon())[0] - 0, R.to_storage(), dict=True)
In [4]: solutions
Out[4]: [{R_im: 0}]
Noting that I don't think the symengine API supports solve
with complex types either, e.g. I get this:
In [6]: solutions = sf.solve(R.to_tangent(epsilon=sf.epsilon())[0] - 0, R)
---------------------------------------------------------------------------
SympifyError Traceback (most recent call last)
Cell In[6], line 1
----> 1 solutions = sf.solve(R.to_tangent(epsilon=sf.epsilon())[0] - 0, R)
File ~/symforce/symforce/internal/symbolic.py:529, in solve(*args, **kwargs)
528 def solve(*args: T.Any, **kwargs: T.Any) -> T.List[Scalar]:
--> 529 solution = sympy.solve(*args, **kwargs)
530 from symengine.lib.symengine_wrapper import EmptySet as _EmptySet
532 if isinstance(solution, FiniteSet):
File symengine_wrapper.pyx:5402, in symengine.lib.symengine_wrapper.solve()
File symengine_wrapper.pyx:547, in symengine.lib.symengine_wrapper.sympify()
File symengine_wrapper.pyx:590, in symengine.lib.symengine_wrapper._sympify()
File symengine_wrapper.pyx:513, in symengine.lib.symengine_wrapper.sympy2symengine()
SympifyError: sympy2symengine: Cannot convert '<Rot2 <C real=R_re, imag=R_im>>' (of type <class 'symforce.geo.rot2.Rot2'>) to a symengine type.
from symforce.
That works, but you are right that it is a little cumbersome. Thanks for the explanation.
from symforce.
Related Issues (20)
- Distrubute pip packages for Linux ARM64 HOT 1
- Image Jacobians HOT 1
- Add a C++ linter like clang-tidy or cpplint
- sf.logical_or HOT 3
- Need to call make three times in a row to make with cmake HOT 4
- Values accept Dict HOT 4
- more resources for generating factor graph HOT 3
- Convert gtsam::Rot3 to sym::Rot3 HOT 2
- Failure to build from source on MacOS HOT 3
- Unreasonable high covariances HOT 6
- gmp install with mamba on macOS doesn't work
- Rust codegen support HOT 7
- Idea: `PerformanceWarning` HOT 2
- feat: Symbolic integration HOT 4
- Double free when unused but optimized variable appears appears in cpp solver
- Returning a `Values` with fixed-size Matrix types results in `VectorXd` in resulting LCM struct HOT 2
- Using symforce as submodule, fixing rerun_if_needed.py HOT 2
- Epsilon tutorial: Plot of sin(x)/x doesn't show on website dark mode
- Add example for nonlinear MPC from ICRA 2023 workshop HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from symforce.