sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.09k stars 394 forks source link

jacobian fails for pynac expressions #5546

Closed burcin closed 14 years ago

burcin commented 15 years ago

Reported by Alex Raichev on sage-support:

----------------------------------------------------------------------
| Sage Version 3.4, Release Date: 2009-03-11                         |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: var('x,y', ns=1)
(x, y)
sage: f= x+y
sage: type(f)
<type 'sage.symbolic.expression.Expression'>
sage: jacobian(f,[x,y])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call
last)

/Users/arai021/<ipython console> in <module>()

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
functions.pyc in jacobian(functions, variables)
    136
    137     if not isinstance(variables, (tuple, list)) and not
is_Vector(variables):
    138         variables = [variables]
    139
--> 140     return matrix([[diff(f, v) for v in variables] for f in  
functions])

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
functional.pyc in derivative(f, *args, **kwds)
    145         pass
    146     if not isinstance(f, SymbolicExpression):
--> 147         f = SR(f)  
    148     return f.derivative(*args, **kwds)
    149

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
calculus.pyc in __call__(self, x)
    504                 msg, s, pos = err.args
    505                 raise TypeError, "%s: %s !!! %s" % (msg, s
[:pos], s[pos:])
--> 506         return self._coerce_impl(x)  
    507
    508     def _coerce_impl(self, x):

/Applications/sage/local/lib/python2.5/site-packages/sage/calculus/
calculus.pyc in _coerce_impl(self, x)
    566             return self(x._sage_())
    567         else:
--> 568             raise TypeError, "cannot coerce type '%s' into a  
SymbolicExpression."%type(x)
    569
    570     def _repr_(self):

TypeError: cannot coerce type '<type
'sage.symbolic.expression.Expression'>' into a SymbolicExpression.  

sage.symbolic.expression.Expression doesn't support .derivative(), and the interface to .diff() doesn't match the Sage conventions.

CC: @williamstein @mwhansen @robertwb

Component: symbolics

Issue created by migration from https://trac.sagemath.org/ticket/5546

burcin commented 15 years ago
comment:1

Attached patches allow forming matrices and vectors with pynac expressions, and make them use the multi_derivative framework. This makes the jacobian command in the example above work.

burcin commented 15 years ago

allow forming matrices and vectors from pynac expressions

burcin commented 15 years ago

Attachment: trac_5546-1-pynac_matrix_vector.patch.gz

Attachment: trac_5546-2-pynac_derivative.patch.gz

make pynac expressions use the multi_derivative framework

93749b3a-c0a4-47a7-b178-004ba08b0b97 commented 15 years ago
comment:2

With these two patches applied, you can get some wild things:

sage: var('x,y,z',ns=1)
(x, y, z)
sage: M = matrix(2,2,[x,y,z,x])
sage: M.base_ring()
New Symbolic Ring
sage: v = vector([x,y])
sage: v.base_ring()
New Symbolic Ring
sage: M * v
Exception exceptions.TypeError: 'mutable matrices are unhashable' in 'sage.modules.free_module_element.FreeModuleElement._cmp_same_ambient_c' ignored
<ERROR: mutable matrices are unhashable>
(([x y]
[z x])*x, ([x y]
[z x])*y)
sage: v * M
(x^2 + y*z, 2*x*y)
sage: M * v
<ERROR: mutable matrices are unhashable>
(([x y]
[z x])*x, ([x y]
[z x])*y)
sage: v * M * v
2*x*y^2 + (x^2 + y*z)*x
sage: v * (M * v)
/Users/ncalexan/.sage/temp/dhcp_v007_000.mobile.uci.edu/36399/_Users_ncalexan__sage_init_sage_0.py:1: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
  # -*- coding: utf-8 -*-
([x y] + ([x y]
burcin commented 15 years ago

Attachment: pynac-hash_exception.patch.gz

patch to pynac to handle exceptions during hashing

burcin commented 15 years ago

doctests for the fix

burcin commented 15 years ago
comment:3

Attachment: 5546_doctests.patch.gz

attachment: pynac-hash_exception.patch fixes exception handling during hashing in pynac and the problems reported by Nick in comment:2.

I will hold off on doing another pynac release to address this, since I don't know if mhansen made any changes which might be affected.

BTW, another pynac release, 0.1.6 in this case, would introduce circular dependencies in trac. :)

Comments on how to proceed?

williamstein commented 15 years ago
comment:4

Comments on how to proceed?

I'd just like to remark that Mike Hansen is probably the best person I've ever met at using rebasing patches and using revision control systems. With him, I would not be worried about doing something that conflicts with what he has done or with circular trac dependencies.

burcin commented 15 years ago
comment:5

The necessary changes for pynac, i.e., attachment: pynac-hash_exception.patch, is included in the new package for pynac-0.1.6 at #5777. The doctests in attachment: 5546_doctests.patch are also posted in #5777.

This now introduces a circular dependency in trac. I hope this won't cause problems, since all these patches will go in together. Faster reviews in the future may prevent this from happening again.

Note that only:

needs to be applied from this ticket.

burcin commented 14 years ago
comment:6

Some variants of these patches seem to be in 4.0.rc0. This bug should be closed. Trac doesn't allow me to close bugs any more though.

mwhansen commented 14 years ago
comment:7

This was fixed by #5777 and #5390