b45ch1 / algopy

AlgoPy is a Research Prototype for Algorithmic Differentation in Python
79 stars 14 forks source link

TypeError: unsupported operand type(s) for /: 'int' and 'Function' #25

Closed zwenson closed 11 years ago

zwenson commented 11 years ago

Hi! First, great work.

I use Algopy to solve bigger nonlinear equation systems. Therefor I need to calculate Jacobian Matrices for a gauss-newton-solver algorithm. I build the equations with Sympy and convert them into a function with lambdify. Anyway this works pretty well with a various number of mathematical expressions like (sin,cos,tan,pow,exp ...) but crashes if there is a simple division like 1/x[0]. I am not sure what the problem is or if it really is an Algopy issue but I would be glad if someone could help me.

some examples works: http://ubuntuone.com/1GfIuFZzdYLEJbCHqiztPQ doesn't work http://ubuntuone.com/5krZDKatoUPZVgi3GwxNZx

argriffing commented 11 years ago

The issue is an incompleteness in AlgoPy, and you can work around it by using x**(-1.0) instead of 1/x.

The problem is that the AlgoPy Function class does not implement __truediv__ or __rtruediv__ which are expected by sympy's lambdification. It should be easy to fix in algopy, for example https://github.com/argriffing/algopy/commit/9a1ca740099ddf5975e8fea39a4626b89c5c983d .

Also I am confused about why your "works.py" and "doesnt_work.py" are different at these lines: GLSfoo = sp.lambdify(x,GLS,modules='sympy') GLSfoo = sp.lambdify(x,GLS,modules=modul) Did you mean for them to both call lambdify with modules="numpy"?

zwenson commented 11 years ago

I implemented your fix, seems to fix the issue.

Also I am confused about why your "works.py" and "doesnt_work.py" are different at these lines:

This wasn't on purpose, I should be just 'numpy'.

Thank you very much!

argriffing commented 11 years ago

By the way, I suspect that theano might work well for this particular problem. Because you only care about the first derivatives as opposed to higher order derivatives, and you are not taking gradients of crazy things like SVD, the special features of algopy do not come into play.

zwenson commented 11 years ago

Theano seems like an interesting project. But by now Algopy does the job.